Fix cfg for test

Fix up cfg attributes to work on an xor basis.

Previously, the cfg(any()) attributes would cause issues when
both native-tls and tls features were enabled. Now, https functions
and enum variants will only be created when tls xor native-tls are
enabled. Additionally, a compile error has been added for when
both tls and native-tls features are enabled.
This commit is contained in:
k3d3
2020-04-13 22:06:01 -04:00
committed by Martin Algesten
parent 9f7f712dde
commit de3416e260
4 changed files with 45 additions and 17 deletions

View File

@@ -24,9 +24,9 @@ use crate::unit::Unit;
#[allow(clippy::large_enum_variant)]
pub enum Stream {
Http(TcpStream),
#[cfg(feature = "tls")]
#[cfg(all(feature = "tls", not(feature = "native-tls")))]
Https(rustls::StreamOwned<rustls::ClientSession, TcpStream>),
#[cfg(feature = "native-tls")]
#[cfg(all(feature = "native-tls", not(feature = "tls")))]
Https(TlsStream<TcpStream>),
Cursor(Cursor<Vec<u8>>),
#[cfg(test)]
@@ -40,7 +40,10 @@ impl ::std::fmt::Debug for Stream {
"Stream[{}]",
match self {
Stream::Http(_) => "http",
#[cfg(any(feature = "tls", feature = "native-tls"))]
#[cfg(any(
all(feature = "tls", not(feature = "native-tls")),
all(feature = "native-tls", not(feature = "tls")),
))]
Stream::Https(_) => "https",
Stream::Cursor(_) => "cursor",
#[cfg(test)]
@@ -81,7 +84,10 @@ impl Stream {
pub fn is_poolable(&self) -> bool {
match self {
Stream::Http(_) => true,
#[cfg(any(feature = "tls", feature = "native-tls"))]
#[cfg(any(
all(feature = "tls", not(feature = "native-tls")),
all(feature = "native-tls", not(feature = "tls")),
))]
Stream::Https(_) => true,
_ => false,
}
@@ -100,7 +106,10 @@ impl Read for Stream {
fn read(&mut self, buf: &mut [u8]) -> IoResult<usize> {
match self {
Stream::Http(sock) => sock.read(buf),
#[cfg(any(feature = "tls", feature = "native-tls"))]
#[cfg(any(
all(feature = "tls", not(feature = "native-tls")),
all(feature = "native-tls", not(feature = "tls")),
))]
Stream::Https(stream) => read_https(stream, buf),
Stream::Cursor(read) => read.read(buf),
#[cfg(test)]
@@ -109,7 +118,7 @@ impl Read for Stream {
}
}
#[cfg(feature = "tls")]
#[cfg(all(feature = "tls", not(feature = "native-tls")))]
fn read_https(
stream: &mut StreamOwned<ClientSession, TcpStream>,
buf: &mut [u8],
@@ -121,11 +130,8 @@ fn read_https(
}
}
#[cfg(feature = "native-tls")]
fn read_https(
stream: &mut TlsStream<TcpStream>,
buf: &mut [u8],
) -> IoResult<usize> {
#[cfg(all(feature = "native-tls", not(feature = "tls")))]
fn read_https(stream: &mut TlsStream<TcpStream>, buf: &mut [u8]) -> IoResult<usize> {
match stream.read(buf) {
Ok(size) => Ok(size),
Err(ref e) if is_close_notify(e) => Ok(0),
@@ -153,7 +159,10 @@ impl Write for Stream {
fn write(&mut self, buf: &[u8]) -> IoResult<usize> {
match self {
Stream::Http(sock) => sock.write(buf),
#[cfg(any(feature = "tls", feature = "native-tls"))]
#[cfg(any(
all(feature = "tls", not(feature = "native-tls")),
all(feature = "native-tls", not(feature = "tls")),
))]
Stream::Https(stream) => stream.write(buf),
Stream::Cursor(_) => panic!("Write to read only stream"),
#[cfg(test)]
@@ -163,7 +172,10 @@ impl Write for Stream {
fn flush(&mut self) -> IoResult<()> {
match self {
Stream::Http(sock) => sock.flush(),
#[cfg(any(feature = "tls", feature = "native-tls"))]
#[cfg(any(
all(feature = "tls", not(feature = "native-tls")),
all(feature = "native-tls", not(feature = "tls")),
))]
Stream::Https(stream) => stream.flush(),
Stream::Cursor(_) => panic!("Flush read only stream"),
#[cfg(test)]
@@ -194,7 +206,7 @@ fn configure_certs(config: &mut rustls::ClientConfig) {
.add_server_trust_anchors(&webpki_roots::TLS_SERVER_ROOTS);
}
#[cfg(feature = "tls")]
#[cfg(all(feature = "tls", not(feature = "native-tls")))]
pub(crate) fn connect_https(unit: &Unit) -> Result<Stream, Error> {
use lazy_static::lazy_static;
use std::sync::Arc;
@@ -223,7 +235,7 @@ pub(crate) fn connect_https(unit: &Unit) -> Result<Stream, Error> {
Ok(Stream::Https(stream))
}
#[cfg(feature = "native-tls")]
#[cfg(all(feature = "native-tls", not(feature = "tls")))]
pub(crate) fn connect_https(unit: &Unit) -> Result<Stream, Error> {
let hostname = unit.url.host_str().unwrap();
let port = unit.url.port().unwrap_or(443);