diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index bdae2e5a..97deb0fb 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -89,7 +89,7 @@ jobs: cache-all-crates: true - name: Cargo check - run: cargo check + run: cargo check --all-features doc: name: Check documentation @@ -126,7 +126,7 @@ jobs: cache-all-crates: true - name: Run clippy - run: cargo clippy + run: cargo clippy --all-features test: name: Test @@ -147,4 +147,4 @@ jobs: cache-all-crates: true - name: Run tests - run: cargo test + run: cargo test --all-features diff --git a/Cargo.toml b/Cargo.toml index ab657c15..6c70d58d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,7 +26,7 @@ network-interface = "1.1.1" parking_lot = "0.12.3" pin-project = "1.1.0" prost = "0.12.6" -quinn = { version = "0.9.3", default-features = false, features = ["tls-rustls", "runtime-tokio"] } +quinn = { version = "0.9.3", default-features = false, features = ["tls-rustls", "runtime-tokio"], optional = true } rand = { version = "0.8.0", features = ["getrandom"] } rcgen = "0.10.0" ring = "0.16.20" @@ -36,10 +36,10 @@ simple-dns = "0.7.0" smallvec = "1.13.2" snow = { version = "0.9.3", features = ["ring-resolver"], default-features = false } socket2 = { version = "0.5.7", features = ["all"] } -str0m = "0.5.1" +str0m = { version = "0.5.1", optional = true } thiserror = "1.0.61" tokio-stream = "0.1.12" -tokio-tungstenite = { version = "0.20.0", features = ["rustls-tls-native-roots"] } +tokio-tungstenite = { version = "0.20.0", features = ["rustls-tls-native-roots"], optional = true } tokio-util = { version = "0.7.11", features = ["compat", "io", "codec"] } tokio = { version = "1.26.0", features = ["rt", "net", "io-util", "time", "macros", "sync", "parking_lot"] } tracing = { version = "0.1.40", features = ["log"] } @@ -47,7 +47,7 @@ trust-dns-resolver = "0.23.2" uint = "0.9.5" unsigned-varint = { version = "0.8.0", features = ["codec"] } url = "2.4.0" -webpki = "0.22.4" +webpki = { version = "0.22.4", optional = true } x25519-dalek = "2.0.0" x509-parser = "0.16.0" yasna = "0.5.0" @@ -87,6 +87,17 @@ futures_ringbuf = "0.4.0" [features] custom_sc_network = [] +quic = ["dep:webpki", "dep:quinn"] +webrtc = ["dep:str0m"] +websocket = ["dep:tokio-tungstenite"] [profile.release] debug = true + +[[example]] +name = "echo_notification" +required-features = ["quic"] + +[[example]] +name = "syncing" +required-features = ["quic"] diff --git a/src/config.rs b/src/config.rs index 75620a59..34d67ca0 100644 --- a/src/config.rs +++ b/src/config.rs @@ -29,14 +29,20 @@ use crate::{ notification, request_response, UserProtocol, }, transport::{ - manager::limits::ConnectionLimitsConfig, quic::config::Config as QuicConfig, - tcp::config::Config as TcpConfig, webrtc::config::Config as WebRtcConfig, - websocket::config::Config as WebSocketConfig, MAX_PARALLEL_DIALS, + manager::limits::ConnectionLimitsConfig, tcp::config::Config as TcpConfig, + MAX_PARALLEL_DIALS, }, types::protocol::ProtocolName, PeerId, }; +#[cfg(feature = "quic")] +use crate::transport::quic::config::Config as QuicConfig; +#[cfg(feature = "webrtc")] +use crate::transport::webrtc::config::Config as WebRtcConfig; +#[cfg(feature = "websocket")] +use crate::transport::websocket::config::Config as WebSocketConfig; + use multiaddr::Multiaddr; use std::{collections::HashMap, sync::Arc}; @@ -66,12 +72,15 @@ pub struct ConfigBuilder { tcp: Option, /// QUIC transport config. + #[cfg(feature = "quic")] quic: Option, /// WebRTC transport config. + #[cfg(feature = "webrtc")] webrtc: Option, /// WebSocket transport config. + #[cfg(feature = "websocket")] websocket: Option, /// Keypair. @@ -125,8 +134,11 @@ impl ConfigBuilder { pub fn new() -> Self { Self { tcp: None, + #[cfg(feature = "quic")] quic: None, + #[cfg(feature = "webrtc")] webrtc: None, + #[cfg(feature = "websocket")] websocket: None, keypair: None, ping: None, @@ -151,18 +163,21 @@ impl ConfigBuilder { } /// Add QUIC transport configuration, enabling the transport. + #[cfg(feature = "quic")] pub fn with_quic(mut self, config: QuicConfig) -> Self { self.quic = Some(config); self } /// Add WebRTC transport configuration, enabling the transport. + #[cfg(feature = "webrtc")] pub fn with_webrtc(mut self, config: WebRtcConfig) -> Self { self.webrtc = Some(config); self } /// Add WebSocket transport configuration, enabling the transport. + #[cfg(feature = "websocket")] pub fn with_websocket(mut self, config: WebSocketConfig) -> Self { self.websocket = Some(config); self @@ -264,8 +279,11 @@ impl ConfigBuilder { keypair, tcp: self.tcp.take(), mdns: self.mdns.take(), + #[cfg(feature = "quic")] quic: self.quic.take(), + #[cfg(feature = "webrtc")] webrtc: self.webrtc.take(), + #[cfg(feature = "websocket")] websocket: self.websocket.take(), ping: self.ping.take(), identify: self.identify.take(), @@ -288,12 +306,15 @@ pub struct Litep2pConfig { pub(crate) tcp: Option, /// QUIC transport config. + #[cfg(feature = "quic")] pub(crate) quic: Option, /// WebRTC transport config. + #[cfg(feature = "webrtc")] pub(crate) webrtc: Option, /// WebSocket transport config. + #[cfg(feature = "websocket")] pub(crate) websocket: Option, /// Keypair. diff --git a/src/crypto/mod.rs b/src/crypto/mod.rs index 50436e98..b565639a 100644 --- a/src/crypto/mod.rs +++ b/src/crypto/mod.rs @@ -25,6 +25,7 @@ use crate::{error::*, peer_id::*}; pub mod ed25519; pub(crate) mod noise; +#[cfg(feature = "quic")] pub(crate) mod tls; pub(crate) mod keys_proto { include!(concat!(env!("OUT_DIR"), "/keys_proto.rs")); @@ -94,8 +95,8 @@ impl TryFrom for PublicKey { type Error = Error; fn try_from(pubkey: keys_proto::PublicKey) -> Result { - let key_type = keys_proto::KeyType::from_i32(pubkey.r#type) - .ok_or_else(|| Error::Other(format!("Unknown key type: {}", pubkey.r#type)))?; + let key_type = keys_proto::KeyType::try_from(pubkey.r#type) + .map_err(|_| Error::Other(format!("Unknown key type: {}", pubkey.r#type)))?; match key_type { keys_proto::KeyType::Ed25519 => diff --git a/src/crypto/noise/mod.rs b/src/crypto/noise/mod.rs index 1a877da7..f130042c 100644 --- a/src/crypto/noise/mod.rs +++ b/src/crypto/noise/mod.rs @@ -143,6 +143,7 @@ impl NoiseContext { } /// Create new [`NoiseContext`] with prologue. + #[cfg(feature = "webrtc")] pub fn with_prologue(id_keys: &Keypair, prologue: Vec) -> crate::Result { let noise: Builder<'_> = Builder::with_resolver( NOISE_PARAMETERS.parse().expect("qed; Valid noise pattern"), @@ -160,6 +161,7 @@ impl NoiseContext { } /// Get remote public key from the received Noise payload. + #[cfg(feature = "webrtc")] pub fn get_remote_public_key(&mut self, reply: &[u8]) -> crate::Result { let (len_slice, reply) = reply.split_at(2); let len = u16::from_be_bytes(len_slice.try_into().map_err(|_| error::Error::InvalidData)?) diff --git a/src/crypto/noise/protocol.rs b/src/crypto/noise/protocol.rs index 38924880..ce745f35 100644 --- a/src/crypto/noise/protocol.rs +++ b/src/crypto/noise/protocol.rs @@ -18,7 +18,7 @@ // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. -use crate::crypto::{self, noise::x25519_spec}; +use crate::crypto::noise::x25519_spec; use rand::SeedableRng; use zeroize::Zeroize; @@ -30,16 +30,6 @@ pub struct Keypair { pub public: PublicKey, } -/// The associated public identity of a DH keypair. -#[derive(Clone)] -pub struct KeypairIdentity { - /// The public identity key. - pub public: crypto::PublicKey, - - /// The signature over the public DH key. - pub signature: Option>, -} - /// DH secret key. #[derive(Clone)] pub struct SecretKey(pub T); diff --git a/src/error.rs b/src/error.rs index 05dbbd82..947f2239 100644 --- a/src/error.rs +++ b/src/error.rs @@ -83,18 +83,21 @@ pub enum Error { DnsAddressResolutionFailed, #[error("Transport error: `{0}`")] TransportError(String), + #[cfg(feature = "quic")] #[error("Failed to generate certificate: `{0}`")] CertificateGeneration(#[from] crate::crypto::tls::certificate::GenError), #[error("Invalid data")] InvalidData, #[error("Input rejected")] InputRejected, + #[cfg(feature = "websocket")] #[error("WebSocket error: `{0}`")] WebSocket(#[from] tokio_tungstenite::tungstenite::error::Error), #[error("Insufficient peers")] InsufficientPeers, #[error("Substream doens't exist")] SubstreamDoesntExist, + #[cfg(feature = "webrtc")] #[error("`str0m` error: `{0}`")] WebRtc(#[from] str0m::RtcError), #[error("Remote peer disconnected")] @@ -109,6 +112,7 @@ pub enum Error { NoAddressAvailable(PeerId), #[error("Connection closed")] ConnectionClosed, + #[cfg(feature = "quic")] #[error("Quinn error: `{0}`")] Quinn(quinn::ConnectionError), #[error("Invalid certificate")] @@ -237,6 +241,7 @@ impl From for Error { } } +#[cfg(feature = "quic")] impl From for Error { fn from(error: quinn::ConnectionError) -> Self { match error { diff --git a/src/lib.rs b/src/lib.rs index ef75a3b7..c63e465e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -38,14 +38,18 @@ use crate::{ }, transport::{ manager::{SupportedTransport, TransportManager}, - quic::QuicTransport, tcp::TcpTransport, - webrtc::WebRtcTransport, - websocket::WebSocketTransport, TransportBuilder, TransportEvent, }, }; +#[cfg(feature = "quic")] +use crate::transport::quic::QuicTransport; +#[cfg(feature = "webrtc")] +use crate::transport::webrtc::WebRtcTransport; +#[cfg(feature = "websocket")] +use crate::transport::websocket::WebSocketTransport; + use multiaddr::{Multiaddr, Protocol}; use multihash::Multihash; use transport::Endpoint; @@ -72,9 +76,11 @@ pub mod types; pub mod yamux; mod bandwidth; -mod mock; mod multistream_select; +#[cfg(test)] +mod mock; + /// Public result type used by the crate. pub type Result = std::result::Result; @@ -297,6 +303,7 @@ impl Litep2p { } // enable quic transport if the config exists + #[cfg(feature = "quic")] if let Some(config) = litep2p_config.quic.take() { let handle = transport_manager.transport_handle(Arc::clone(&litep2p_config.executor)); let (transport, transport_listen_addresses) = @@ -313,6 +320,7 @@ impl Litep2p { } // enable webrtc transport if the config exists + #[cfg(feature = "webrtc")] if let Some(config) = litep2p_config.webrtc.take() { let handle = transport_manager.transport_handle(Arc::clone(&litep2p_config.executor)); let (transport, transport_listen_addresses) = @@ -329,6 +337,7 @@ impl Litep2p { } // enable websocket transport if the config exists + #[cfg(feature = "websocket")] if let Some(config) = litep2p_config.websocket.take() { let handle = transport_manager.transport_handle(Arc::clone(&litep2p_config.executor)); let (transport, transport_listen_addresses) = @@ -396,14 +405,17 @@ impl Litep2p { .tcp .is_some() .then(|| supported_transports.insert(SupportedTransport::Tcp)); + #[cfg(feature = "quic")] config .quic .is_some() .then(|| supported_transports.insert(SupportedTransport::Quic)); + #[cfg(feature = "websocket")] config .websocket .is_some() .then(|| supported_transports.insert(SupportedTransport::WebSocket)); + #[cfg(feature = "webrtc")] config .webrtc .is_some() @@ -515,7 +527,6 @@ mod tests { let config = ConfigBuilder::new() .with_tcp(Default::default()) - .with_quic(Default::default()) .with_notification_protocol(config1) .with_notification_protocol(config2) .with_libp2p_ping(ping_config) @@ -591,7 +602,6 @@ mod tests { let config = ConfigBuilder::new() .with_tcp(Default::default()) - .with_quic(Default::default()) .with_notification_protocol(config1) .with_notification_protocol(config2) .with_libp2p_ping(ping_config) diff --git a/src/multistream_select/length_delimited.rs b/src/multistream_select/length_delimited.rs index 9d5d3ce3..7052d629 100644 --- a/src/multistream_select/length_delimited.rs +++ b/src/multistream_select/length_delimited.rs @@ -25,7 +25,6 @@ use std::{ io, pin::Pin, task::{Context, Poll}, - u16, }; const MAX_LEN_BYTES: u16 = 2; diff --git a/src/protocol/notification/mod.rs b/src/protocol/notification/mod.rs index 867e75a6..eefe5904 100644 --- a/src/protocol/notification/mod.rs +++ b/src/protocol/notification/mod.rs @@ -531,7 +531,7 @@ impl NotificationProtocol { let Some(context) = self.peers.get_mut(&peer) else { tracing::error!(target: LOG_TARGET, ?peer, "peer doesn't exist for outbound substream"); debug_assert!(false); - return Err(Error::PeerDoesntExist(peer.clone())); + return Err(Error::PeerDoesntExist(peer)); }; let pending_peer = self.pending_outbound.remove(&substream_id); @@ -661,7 +661,7 @@ impl NotificationProtocol { let Some(context) = self.peers.get_mut(&peer) else { tracing::error!(target: LOG_TARGET, ?peer, "peer doesn't exist for inbound substream"); debug_assert!(false); - return Err(Error::PeerDoesntExist(peer.clone())); + return Err(Error::PeerDoesntExist(peer)); }; tracing::debug!( diff --git a/src/protocol/protocol_set.rs b/src/protocol/protocol_set.rs index f2328ebe..a0458146 100644 --- a/src/protocol/protocol_set.rs +++ b/src/protocol/protocol_set.rs @@ -38,14 +38,13 @@ use futures::{stream::FuturesUnordered, Stream, StreamExt}; use multiaddr::Multiaddr; use tokio::sync::mpsc::{channel, Receiver, Sender}; +#[cfg(any(feature = "quic", feature = "webrtc", feature = "websocket"))] +use std::sync::atomic::Ordering; use std::{ collections::HashMap, fmt::Debug, pin::Pin, - sync::{ - atomic::{AtomicUsize, Ordering}, - Arc, - }, + sync::{atomic::AtomicUsize, Arc}, task::{Context, Poll}, }; @@ -213,6 +212,7 @@ pub struct ProtocolSet { mgr_tx: Sender, connection: ConnectionHandle, rx: Receiver, + #[allow(unused)] next_substream_id: Arc, fallback_names: HashMap, } @@ -253,6 +253,7 @@ impl ProtocolSet { } /// Get next substream ID. + #[cfg(any(feature = "quic", feature = "webrtc", feature = "websocket"))] pub fn next_substream_id(&self) -> SubstreamId { SubstreamId::from(self.next_substream_id.fetch_add(1usize, Ordering::Relaxed)) } diff --git a/src/substream/mod.rs b/src/substream/mod.rs index 7cc41fa3..5fc1ec3c 100644 --- a/src/substream/mod.rs +++ b/src/substream/mod.rs @@ -24,11 +24,18 @@ use crate::{ codec::ProtocolCodec, error::{Error, SubstreamError}, - transport::{quic, tcp, webrtc, websocket}, + transport::tcp, types::SubstreamId, PeerId, }; +#[cfg(feature = "quic")] +use crate::transport::quic; +#[cfg(feature = "webrtc")] +use crate::transport::webrtc; +#[cfg(feature = "websocket")] +use crate::transport::websocket; + use bytes::{Buf, Bytes, BytesMut}; use futures::{Sink, Stream}; use tokio::io::{AsyncRead, AsyncWrite, AsyncWriteExt, ReadBuf}; @@ -50,8 +57,11 @@ macro_rules! poll_flush { ($substream:expr, $cx:ident) => {{ match $substream { SubstreamType::Tcp(substream) => Pin::new(substream).poll_flush($cx), + #[cfg(feature = "websocket")] SubstreamType::WebSocket(substream) => Pin::new(substream).poll_flush($cx), + #[cfg(feature = "quic")] SubstreamType::Quic(substream) => Pin::new(substream).poll_flush($cx), + #[cfg(feature = "webrtc")] SubstreamType::WebRtc(substream) => Pin::new(substream).poll_flush($cx), #[cfg(test)] SubstreamType::Mock(_) => unreachable!(), @@ -63,8 +73,11 @@ macro_rules! poll_write { ($substream:expr, $cx:ident, $frame:expr) => {{ match $substream { SubstreamType::Tcp(substream) => Pin::new(substream).poll_write($cx, $frame), + #[cfg(feature = "websocket")] SubstreamType::WebSocket(substream) => Pin::new(substream).poll_write($cx, $frame), + #[cfg(feature = "quic")] SubstreamType::Quic(substream) => Pin::new(substream).poll_write($cx, $frame), + #[cfg(feature = "webrtc")] SubstreamType::WebRtc(substream) => Pin::new(substream).poll_write($cx, $frame), #[cfg(test)] SubstreamType::Mock(_) => unreachable!(), @@ -76,8 +89,11 @@ macro_rules! poll_read { ($substream:expr, $cx:ident, $buffer:expr) => {{ match $substream { SubstreamType::Tcp(substream) => Pin::new(substream).poll_read($cx, $buffer), + #[cfg(feature = "websocket")] SubstreamType::WebSocket(substream) => Pin::new(substream).poll_read($cx, $buffer), + #[cfg(feature = "quic")] SubstreamType::Quic(substream) => Pin::new(substream).poll_read($cx, $buffer), + #[cfg(feature = "webrtc")] SubstreamType::WebRtc(substream) => Pin::new(substream).poll_read($cx, $buffer), #[cfg(test)] SubstreamType::Mock(_) => unreachable!(), @@ -89,8 +105,11 @@ macro_rules! poll_shutdown { ($substream:expr, $cx:ident) => {{ match $substream { SubstreamType::Tcp(substream) => Pin::new(substream).poll_shutdown($cx), + #[cfg(feature = "websocket")] SubstreamType::WebSocket(substream) => Pin::new(substream).poll_shutdown($cx), + #[cfg(feature = "quic")] SubstreamType::Quic(substream) => Pin::new(substream).poll_shutdown($cx), + #[cfg(feature = "webrtc")] SubstreamType::WebRtc(substream) => Pin::new(substream).poll_shutdown($cx), #[cfg(test)] SubstreamType::Mock(substream) => { @@ -150,8 +169,11 @@ macro_rules! check_size { /// Substream type. enum SubstreamType { Tcp(tcp::Substream), + #[cfg(feature = "websocket")] WebSocket(websocket::Substream), + #[cfg(feature = "quic")] Quic(quic::Substream), + #[cfg(feature = "webrtc")] WebRtc(webrtc::Substream), #[cfg(test)] Mock(Box), @@ -161,8 +183,11 @@ impl fmt::Debug for SubstreamType { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { Self::Tcp(_) => write!(f, "Tcp"), + #[cfg(feature = "websocket")] Self::WebSocket(_) => write!(f, "WebSocket"), + #[cfg(feature = "quic")] Self::Quic(_) => write!(f, "Quic"), + #[cfg(feature = "webrtc")] Self::WebRtc(_) => write!(f, "WebRtc"), #[cfg(test)] Self::Mock(_) => write!(f, "Mock"), @@ -254,6 +279,7 @@ impl Substream { } /// Create new [`Substream`] for WebSocket. + #[cfg(feature = "websocket")] pub(crate) fn new_websocket( peer: PeerId, substream_id: SubstreamId, @@ -271,6 +297,7 @@ impl Substream { } /// Create new [`Substream`] for QUIC. + #[cfg(feature = "quic")] pub(crate) fn new_quic( peer: PeerId, substream_id: SubstreamId, @@ -283,6 +310,7 @@ impl Substream { } /// Create new [`Substream`] for WebRTC. + #[cfg(feature = "webrtc")] pub(crate) fn new_webrtc( peer: PeerId, substream_id: SubstreamId, @@ -315,8 +343,11 @@ impl Substream { pub async fn close(self) { let _ = match self.substream { SubstreamType::Tcp(mut substream) => substream.shutdown().await, + #[cfg(feature = "websocket")] SubstreamType::WebSocket(mut substream) => substream.shutdown().await, + #[cfg(feature = "quic")] SubstreamType::Quic(mut substream) => substream.shutdown().await, + #[cfg(feature = "webrtc")] SubstreamType::WebRtc(mut substream) => substream.shutdown().await, #[cfg(test)] SubstreamType::Mock(mut substream) => { @@ -391,6 +422,7 @@ impl Substream { substream.flush().await.map_err(From::from) } }, + #[cfg(feature = "websocket")] SubstreamType::WebSocket(ref mut substream) => match self.codec { ProtocolCodec::Unspecified => panic!("codec is unspecified"), ProtocolCodec::Identity(payload_size) => @@ -414,6 +446,7 @@ impl Substream { substream.flush().await.map_err(From::from) } }, + #[cfg(feature = "quic")] SubstreamType::Quic(ref mut substream) => match self.codec { ProtocolCodec::Unspecified => panic!("codec is unspecified"), ProtocolCodec::Identity(payload_size) => @@ -428,6 +461,7 @@ impl Substream { substream.write_all_chunks(&mut [len.freeze(), bytes]).await } }, + #[cfg(feature = "webrtc")] SubstreamType::WebRtc(ref mut substream) => match self.codec { ProtocolCodec::Unspecified => panic!("codec is unspecified"), ProtocolCodec::Identity(payload_size) => diff --git a/src/transport/common/listener.rs b/src/transport/common/listener.rs index 275d7beb..d9afe729 100644 --- a/src/transport/common/listener.rs +++ b/src/transport/common/listener.rs @@ -370,7 +370,7 @@ fn multiaddr_to_socket_address( ?protocol, "invalid transport protocol, expected `Tcp`", ); - return Err(Error::AddressError(AddressError::InvalidProtocol)); + Err(Error::AddressError(AddressError::InvalidProtocol)) } }; diff --git a/src/transport/manager/handle.rs b/src/transport/manager/handle.rs index 833308ea..ece77f02 100644 --- a/src/transport/manager/handle.rs +++ b/src/transport/manager/handle.rs @@ -121,16 +121,18 @@ impl TransportManagerHandle { match iter.next() { None => false, - Some(Protocol::Tcp(_)) => match ( - iter.next(), - self.supported_transport.contains(&SupportedTransport::WebSocket), - ) { - (Some(Protocol::Ws(_)), true) => true, - (Some(Protocol::Wss(_)), true) => true, - (Some(Protocol::P2p(_)), _) => + Some(Protocol::Tcp(_)) => match iter.next() { + Some(Protocol::P2p(_)) => self.supported_transport.contains(&SupportedTransport::Tcp), + #[cfg(feature = "websocket")] + Some(Protocol::Ws(_)) => + self.supported_transport.contains(&SupportedTransport::WebSocket), + #[cfg(feature = "websocket")] + Some(Protocol::Wss(_)) => + self.supported_transport.contains(&SupportedTransport::WebSocket), _ => false, }, + #[cfg(feature = "quic")] Some(Protocol::Udp(_)) => match ( iter.next(), self.supported_transport.contains(&SupportedTransport::Quic), @@ -285,7 +287,6 @@ pub struct TransportHandle { pub protocols: HashMap, pub next_connection_id: Arc, pub next_substream_id: Arc, - pub protocol_names: Vec, pub bandwidth_sink: BandwidthSink, pub executor: Arc, } @@ -333,9 +334,21 @@ mod tests { } #[tokio::test] - async fn tcp_and_websocket_supported() { + async fn tcp_supported() { let (mut handle, _rx) = make_transport_manager_handle(); handle.supported_transport.insert(SupportedTransport::Tcp); + + let address = + "/dns4/google.com/tcp/24928/p2p/12D3KooWKrUnV42yDR7G6DewmgHtFaVCJWLjQRi2G9t5eJD3BvTy" + .parse() + .unwrap(); + assert!(handle.supported_transport(&address)); + } + + #[cfg(feature = "websocket")] + #[tokio::test] + async fn websocket_supported() { + let (mut handle, _rx) = make_transport_manager_handle(); handle.supported_transport.insert(SupportedTransport::WebSocket); let address = @@ -378,7 +391,7 @@ mod tests { #[test] fn zero_addresses_added() { let (mut handle, _rx) = make_transport_manager_handle(); - handle.supported_transport.insert(SupportedTransport::Quic); + handle.supported_transport.insert(SupportedTransport::Tcp); assert!( handle.add_known_address( @@ -388,9 +401,6 @@ mod tests { .with(Protocol::Ip4(std::net::Ipv4Addr::new(127, 0, 0, 1))) .with(Protocol::Udp(8888)) .with(Protocol::Utp), - Multiaddr::empty() - .with(Protocol::Ip4(std::net::Ipv4Addr::new(127, 0, 0, 1))) - .with(Protocol::Tcp(8888)), Multiaddr::empty() .with(Protocol::Ip4(std::net::Ipv4Addr::new(127, 0, 0, 1))) .with(Protocol::Tcp(8888)) diff --git a/src/transport/manager/mod.rs b/src/transport/manager/mod.rs index be732b1f..b3d9c8c1 100644 --- a/src/transport/manager/mod.rs +++ b/src/transport/manager/mod.rs @@ -357,7 +357,6 @@ impl TransportManager { keypair: self.keypair.clone(), protocols: self.protocols.clone(), bandwidth_sink: self.bandwidth_sink.clone(), - protocol_names: self.protocol_names.iter().cloned().collect(), next_substream_id: self.next_substream_id.clone(), next_connection_id: self.next_connection_id.clone(), } @@ -499,33 +498,35 @@ impl TransportManager { ); let mut transports = HashSet::new(); + #[cfg(feature = "websocket")] let mut websocket = Vec::new(); + #[cfg(feature = "quic")] let mut quic = Vec::new(); let mut tcp = Vec::new(); for (address, record) in &mut records { record.set_connection_id(connection_id); - let mut iter = address.iter(); - match iter.find(|protocol| std::matches!(protocol, Protocol::QuicV1)) { - Some(_) => { - quic.push(address.clone()); - transports.insert(SupportedTransport::Quic); - } - _ => match address - .iter() - .find(|protocol| std::matches!(protocol, Protocol::Ws(_) | Protocol::Wss(_))) - { - Some(_) => { - websocket.push(address.clone()); - transports.insert(SupportedTransport::WebSocket); - } - None => { - tcp.push(address.clone()); - transports.insert(SupportedTransport::Tcp); - } - }, + #[cfg(feature = "quic")] + if address.iter().find(|p| std::matches!(p, Protocol::QuicV1)).is_some() { + quic.push(address.clone()); + transports.insert(SupportedTransport::Quic); + continue; } + + #[cfg(feature = "websocket")] + if address + .iter() + .find(|p| std::matches!(p, Protocol::Ws(_) | Protocol::Wss(_))) + .is_some() + { + websocket.push(address.clone()); + transports.insert(SupportedTransport::WebSocket); + continue; + } + + tcp.push(address.clone()); + transports.insert(SupportedTransport::Tcp); } peers.insert( @@ -548,6 +549,7 @@ impl TransportManager { .open(connection_id, tcp)?; } + #[cfg(feature = "quic")] if !quic.is_empty() { self.transports .get_mut(&SupportedTransport::Quic) @@ -555,6 +557,7 @@ impl TransportManager { .open(connection_id, quic)?; } + #[cfg(feature = "websocket")] if !websocket.is_empty() { self.transports .get_mut(&SupportedTransport::WebSocket) @@ -604,10 +607,12 @@ impl TransportManager { .ok_or_else(|| Error::TransportNotSupported(record.address().clone()))? { Protocol::Tcp(_) => match protocol_stack.next() { + #[cfg(feature = "websocket")] Some(Protocol::Ws(_)) | Some(Protocol::Wss(_)) => SupportedTransport::WebSocket, Some(Protocol::P2p(_)) => SupportedTransport::Tcp, _ => return Err(Error::TransportNotSupported(record.address().clone())), }, + #[cfg(feature = "quic")] Protocol::Udp(_) => match protocol_stack .next() .ok_or_else(|| Error::TransportNotSupported(record.address().clone()))? @@ -622,7 +627,7 @@ impl TransportManager { tracing::error!( target: LOG_TARGET, ?protocol, - "invalid protocol, expected `tcp`" + "invalid protocol" ); return Err(Error::TransportNotSupported(record.address().clone())); @@ -2078,9 +2083,14 @@ mod tests { .with_env_filter(tracing_subscriber::EnvFilter::from_default_env()) .try_init(); + let mut transports = HashSet::new(); + transports.insert(SupportedTransport::Tcp); + #[cfg(feature = "quic")] + transports.insert(SupportedTransport::Quic); + let (_manager, handle) = TransportManager::new( Keypair::generate(), - HashSet::from_iter([SupportedTransport::Tcp, SupportedTransport::Quic]), + transports, BandwidthSink::new(), 8usize, ConnectionLimitsConfig::default(), @@ -2112,7 +2122,10 @@ mod tests { .with(Protocol::P2p( Multihash::from_bytes(&PeerId::random().to_bytes()).unwrap(), )); + #[cfg(feature = "quic")] assert!(handle.supported_transport(&address)); + #[cfg(not(feature = "quic"))] + assert!(!handle.supported_transport(&address)); // websocket let address = Multiaddr::empty() diff --git a/src/transport/manager/types.rs b/src/transport/manager/types.rs index befabf0f..b367a086 100644 --- a/src/transport/manager/types.rs +++ b/src/transport/manager/types.rs @@ -34,12 +34,15 @@ pub enum SupportedTransport { Tcp, /// QUIC. + #[cfg(feature = "quic")] Quic, /// WebRTC + #[cfg(feature = "webrtc")] WebRtc, /// WebSocket + #[cfg(feature = "websocket")] WebSocket, } diff --git a/src/transport/mod.rs b/src/transport/mod.rs index 1885a641..bad76080 100644 --- a/src/transport/mod.rs +++ b/src/transport/mod.rs @@ -28,9 +28,12 @@ use multiaddr::Multiaddr; use std::{fmt::Debug, time::Duration}; pub(crate) mod common; +#[cfg(feature = "quic")] pub mod quic; pub mod tcp; +#[cfg(feature = "webrtc")] pub mod webrtc; +#[cfg(feature = "websocket")] pub mod websocket; pub(crate) mod dummy; diff --git a/src/transport/quic/mod.rs b/src/transport/quic/mod.rs index 5972993f..d0ac2375 100644 --- a/src/transport/quic/mod.rs +++ b/src/transport/quic/mod.rs @@ -495,7 +495,6 @@ mod tests { let handle1 = TransportHandle { executor: Arc::new(DefaultExecutor {}), - protocol_names: Vec::new(), next_substream_id: Default::default(), next_connection_id: Default::default(), keypair: keypair1.clone(), @@ -522,7 +521,6 @@ mod tests { let handle2 = TransportHandle { executor: Arc::new(DefaultExecutor {}), - protocol_names: Vec::new(), next_substream_id: Default::default(), next_connection_id: Default::default(), keypair: keypair2.clone(), diff --git a/src/transport/tcp/mod.rs b/src/transport/tcp/mod.rs index 62cc00d9..9eb24d40 100644 --- a/src/transport/tcp/mod.rs +++ b/src/transport/tcp/mod.rs @@ -514,7 +514,6 @@ mod tests { let handle1 = crate::transport::manager::TransportHandle { executor: Arc::new(DefaultExecutor {}), - protocol_names: Vec::new(), next_substream_id: Default::default(), next_connection_id: Default::default(), keypair: keypair1.clone(), @@ -545,7 +544,6 @@ mod tests { let handle2 = crate::transport::manager::TransportHandle { executor: Arc::new(DefaultExecutor {}), - protocol_names: Vec::new(), next_substream_id: Default::default(), next_connection_id: Default::default(), keypair: keypair2.clone(), @@ -594,7 +592,6 @@ mod tests { let handle1 = crate::transport::manager::TransportHandle { executor: Arc::new(DefaultExecutor {}), - protocol_names: Vec::new(), next_substream_id: Default::default(), next_connection_id: Default::default(), keypair: keypair1.clone(), @@ -630,7 +627,6 @@ mod tests { let handle2 = crate::transport::manager::TransportHandle { executor: Arc::new(DefaultExecutor {}), - protocol_names: Vec::new(), next_substream_id: Default::default(), next_connection_id: Default::default(), keypair: keypair2.clone(), diff --git a/src/yamux/connection/stream.rs b/src/yamux/connection/stream.rs index 03e3fa39..d6762bb2 100644 --- a/src/yamux/connection/stream.rs +++ b/src/yamux/connection/stream.rs @@ -494,7 +494,7 @@ impl Shared { WindowUpdateMode::OnRead => { debug_assert!(self.config.receive_window >= self.window); let bytes_received = self.config.receive_window.saturating_sub(self.window); - let buffer_len: u32 = self.buffer.len().try_into().unwrap_or(std::u32::MAX); + let buffer_len: u32 = self.buffer.len().try_into().unwrap_or(u32::MAX); bytes_received.saturating_sub(buffer_len) } diff --git a/tests/common/mod.rs b/tests/common/mod.rs new file mode 100644 index 00000000..50026109 --- /dev/null +++ b/tests/common/mod.rs @@ -0,0 +1,44 @@ +// Copyright 2024 litep2p developers +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the "Software"), +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. + +use litep2p::{config::ConfigBuilder, transport::tcp::config::Config as TcpConfig}; + +#[cfg(feature = "quic")] +use litep2p::transport::quic::config::Config as QuicConfig; +#[cfg(feature = "websocket")] +use litep2p::transport::websocket::config::Config as WebSocketConfig; + +pub(crate) enum Transport { + Tcp(TcpConfig), + #[cfg(feature = "quic")] + Quic(QuicConfig), + #[cfg(feature = "websocket")] + WebSocket(WebSocketConfig), +} + +pub(crate) fn add_transport(config: ConfigBuilder, transport: Transport) -> ConfigBuilder { + match transport { + Transport::Tcp(transport) => config.with_tcp(transport), + #[cfg(feature = "quic")] + Transport::Quic(transport) => config.with_quic(transport), + #[cfg(feature = "websocket")] + Transport::WebSocket(transport) => config.with_websocket(transport), + } +} diff --git a/tests/conformance/rust/mod.rs b/tests/conformance/rust/mod.rs index 67ef0d45..ed8e3cd7 100644 --- a/tests/conformance/rust/mod.rs +++ b/tests/conformance/rust/mod.rs @@ -24,5 +24,5 @@ mod identify; mod kademlia; #[cfg(test)] mod ping; -#[cfg(test)] +#[cfg(all(test, feature = "quic"))] mod quic_ping; diff --git a/tests/connection/mod.rs b/tests/connection/mod.rs index 33524cae..769a161f 100644 --- a/tests/connection/mod.rs +++ b/tests/connection/mod.rs @@ -21,30 +21,30 @@ use litep2p::{ config::ConfigBuilder, crypto::ed25519::Keypair, - error::{AddressError, Error}, + error::Error, protocol::libp2p::ping::{Config as PingConfig, PingEvent}, - transport::{ - quic::config::Config as QuicConfig, tcp::config::Config as TcpConfig, - websocket::config::Config as WebSocketConfig, - }, + transport::tcp::config::Config as TcpConfig, Litep2p, Litep2pEvent, PeerId, }; +#[cfg(feature = "websocket")] +use litep2p::transport::websocket::config::Config as WebSocketConfig; +#[cfg(feature = "quic")] +use litep2p::{error::AddressError, transport::quic::config::Config as QuicConfig}; + use futures::{Stream, StreamExt}; use multiaddr::{Multiaddr, Protocol}; use multihash::Multihash; use network_interface::{NetworkInterface, NetworkInterfaceConfig}; -use tokio::net::{TcpListener, UdpSocket}; +use tokio::net::TcpListener; +#[cfg(feature = "quic")] +use tokio::net::UdpSocket; + +use crate::common::{add_transport, Transport}; #[cfg(test)] mod protocol_dial_invalid_address; -enum Transport { - Tcp(TcpConfig), - Quic(QuicConfig), - WebSocket(WebSocketConfig), -} - #[tokio::test] async fn two_litep2ps_work_tcp() { two_litep2ps_work( @@ -60,6 +60,7 @@ async fn two_litep2ps_work_tcp() { .await } +#[cfg(feature = "quic")] #[tokio::test] async fn two_litep2ps_work_quic() { two_litep2ps_work( @@ -69,6 +70,7 @@ async fn two_litep2ps_work_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn two_litep2ps_work_websocket() { two_litep2ps_work( @@ -94,24 +96,14 @@ async fn two_litep2ps_work(transport1: Transport, transport2: Transport) { .with_keypair(Keypair::generate()) .with_libp2p_ping(ping_config1); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - Transport::Quic(config) => config1.with_quic(config), - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let (ping_config2, _ping_event_stream2) = PingConfig::default(); let config2 = ConfigBuilder::new() .with_keypair(Keypair::generate()) .with_libp2p_ping(ping_config2); - let config2 = match transport2 { - Transport::Tcp(config) => config2.with_tcp(config), - Transport::Quic(config) => config2.with_quic(config), - Transport::WebSocket(config) => config2.with_websocket(config), - } - .build(); + let config2 = add_transport(config2, transport2).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let mut litep2p2 = Litep2p::new(config2).unwrap(); @@ -151,6 +143,7 @@ async fn dial_failure_tcp() { .await } +#[cfg(feature = "quic")] #[tokio::test] async fn dial_failure_quic() { dial_failure( @@ -166,6 +159,7 @@ async fn dial_failure_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn dial_failure_websocket() { dial_failure( @@ -197,24 +191,14 @@ async fn dial_failure(transport1: Transport, transport2: Transport, dial_address .with_keypair(Keypair::generate()) .with_libp2p_ping(ping_config1); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - Transport::Quic(config) => config1.with_quic(config), - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let (ping_config2, _ping_event_stream2) = PingConfig::default(); let config2 = ConfigBuilder::new() .with_keypair(Keypair::generate()) .with_libp2p_ping(ping_config2); - let config2 = match transport2 { - Transport::Tcp(config) => config2.with_tcp(config), - Transport::Quic(config) => config2.with_quic(config), - Transport::WebSocket(config) => config2.with_websocket(config), - } - .build(); + let config2 = add_transport(config2, transport2).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let mut litep2p2 = Litep2p::new(config2).unwrap(); @@ -316,6 +300,7 @@ async fn connection_timeout_tcp() { .await } +#[cfg(feature = "quic")] #[tokio::test] async fn connection_timeout_quic() { // create udp socket but don't respond to any inbound datagrams @@ -332,6 +317,7 @@ async fn connection_timeout_quic() { connection_timeout(Transport::Quic(Default::default()), address).await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn connection_timeout_websocket() { // create tcp listener but don't accept any inbound connections @@ -364,13 +350,7 @@ async fn connection_timeout(transport: Transport, address: Multiaddr) { let litep2p_config = ConfigBuilder::new() .with_keypair(Keypair::generate()) .with_libp2p_ping(ping_config); - - let litep2p_config = match transport { - Transport::Tcp(config) => litep2p_config.with_tcp(config), - Transport::Quic(config) => litep2p_config.with_quic(config), - Transport::WebSocket(config) => litep2p_config.with_websocket(config), - } - .build(); + let litep2p_config = add_transport(litep2p_config, transport).build(); let mut litep2p = Litep2p::new(litep2p_config).unwrap(); @@ -389,6 +369,7 @@ async fn connection_timeout(transport: Transport, address: Multiaddr) { assert!(std::matches!(error, Error::Timeout)); } +#[cfg(feature = "quic")] #[tokio::test] async fn dial_quic_peer_id_missing() { let _ = tracing_subscriber::fmt() @@ -427,11 +408,13 @@ async fn dial_self_tcp() { .await } +#[cfg(feature = "quic")] #[tokio::test] async fn dial_self_quic() { dial_self(Transport::Quic(Default::default())).await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn dial_self_websocket() { dial_self(Transport::WebSocket(WebSocketConfig { @@ -450,13 +433,7 @@ async fn dial_self(transport: Transport) { let litep2p_config = ConfigBuilder::new() .with_keypair(Keypair::generate()) .with_libp2p_ping(ping_config); - - let litep2p_config = match transport { - Transport::Tcp(config) => litep2p_config.with_tcp(config), - Transport::Quic(config) => litep2p_config.with_quic(config), - Transport::WebSocket(config) => litep2p_config.with_websocket(config), - } - .build(); + let litep2p_config = add_transport(litep2p_config, transport).build(); let mut litep2p = Litep2p::new(litep2p_config).unwrap(); let address = litep2p.listen_addresses().next().unwrap().clone(); @@ -469,7 +446,36 @@ async fn dial_self(transport: Transport) { } #[tokio::test] -async fn attempt_to_dial_using_unsupported_transport() { +async fn attempt_to_dial_using_unsupported_transport_tcp() { + let _ = tracing_subscriber::fmt() + .with_env_filter(tracing_subscriber::EnvFilter::from_default_env()) + .try_init(); + + let (ping_config, _ping_event_stream) = PingConfig::default(); + let config = ConfigBuilder::new() + .with_keypair(Keypair::generate()) + .with_tcp(Default::default()) + .with_libp2p_ping(ping_config) + .build(); + + let mut litep2p = Litep2p::new(config).unwrap(); + let address = Multiaddr::empty() + .with(Protocol::from(std::net::Ipv4Addr::new(127, 0, 0, 1))) + .with(Protocol::Tcp(8888)) + .with(Protocol::Ws(std::borrow::Cow::Borrowed("/"))) + .with(Protocol::P2p( + Multihash::from_bytes(&PeerId::random().to_bytes()).unwrap(), + )); + + assert!(std::matches!( + litep2p.dial_address(address.clone()).await, + Err(Error::TransportNotSupported(_)) + )); +} + +#[cfg(feature = "quic")] +#[tokio::test] +async fn attempt_to_dial_using_unsupported_transport_quic() { let _ = tracing_subscriber::fmt() .with_env_filter(tracing_subscriber::EnvFilter::from_default_env()) .try_init(); @@ -510,6 +516,7 @@ async fn keep_alive_timeout_tcp() { .await } +#[cfg(feature = "quic")] #[tokio::test] async fn keep_alive_timeout_quic() { keep_alive_timeout( @@ -519,6 +526,7 @@ async fn keep_alive_timeout_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn keep_alive_timeout_websocket() { keep_alive_timeout( @@ -544,12 +552,7 @@ async fn keep_alive_timeout(transport1: Transport, transport2: Transport) { .with_keypair(Keypair::generate()) .with_libp2p_ping(ping_config1); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - Transport::Quic(config) => config1.with_quic(config), - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let (ping_config2, mut ping_event_stream2) = PingConfig::default(); @@ -557,12 +560,7 @@ async fn keep_alive_timeout(transport1: Transport, transport2: Transport) { .with_keypair(Keypair::generate()) .with_libp2p_ping(ping_config2); - let config2 = match transport2 { - Transport::Tcp(config) => config2.with_tcp(config), - Transport::Quic(config) => config2.with_quic(config), - Transport::WebSocket(config) => config2.with_websocket(config), - } - .build(); + let config2 = add_transport(config2, transport2).build(); let mut litep2p2 = Litep2p::new(config2).unwrap(); let address1 = litep2p1.listen_addresses().next().unwrap().clone(); @@ -654,6 +652,7 @@ async fn simultaneous_dial_tcp() { } } +#[cfg(feature = "quic")] #[tokio::test] async fn simultaneous_dial_quic() { let _ = tracing_subscriber::fmt() @@ -706,6 +705,7 @@ async fn simultaneous_dial_quic() { } } +#[cfg(feature = "quic")] #[tokio::test] async fn simultaneous_dial_ipv6_quic() { let _ = tracing_subscriber::fmt() @@ -758,6 +758,7 @@ async fn simultaneous_dial_ipv6_quic() { } } +#[cfg(feature = "webscocket")] #[tokio::test] async fn websocket_over_ipv6() { let _ = tracing_subscriber::fmt() @@ -871,6 +872,7 @@ async fn tcp_dns_resolution() { } } +#[cfg(feature = "websocket")] #[tokio::test] async fn websocket_dns_resolution() { let _ = tracing_subscriber::fmt() @@ -955,6 +957,7 @@ async fn multiple_listen_addresses_tcp() { .await } +#[cfg(feature = "quic")] #[tokio::test] async fn multiple_listen_addresses_quic() { multiple_listen_addresses( @@ -977,6 +980,7 @@ async fn multiple_listen_addresses_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn multiple_listen_addresses_websocket() { multiple_listen_addresses( @@ -1009,7 +1013,9 @@ async fn make_dummy_litep2p( let litep2p_config = match transport { Transport::Tcp(config) => litep2p_config.with_tcp(config), + #[cfg(feature = "quic")] Transport::Quic(config) => litep2p_config.with_quic(config), + #[cfg(feature = "websocket")] Transport::WebSocket(config) => litep2p_config.with_websocket(config), } .build(); @@ -1081,6 +1087,7 @@ async fn port_in_use_tcp() { .unwrap(); } +#[cfg(feature = "websocket")] #[tokio::test] async fn port_in_use_websocket() { let _ = tracing_subscriber::fmt() @@ -1247,6 +1254,7 @@ async fn unspecified_listen_address_tcp() { } } +#[cfg(feature = "websocket")] #[tokio::test] async fn unspecified_listen_address_websocket() { let _ = tracing_subscriber::fmt() @@ -1368,6 +1376,7 @@ async fn simultaneous_dial_then_redial_tcp() { .await } +#[cfg(feature = "websocket")] #[tokio::test] async fn simultaneous_dial_then_redial_websocket() { simultaneous_dial_then_redial( @@ -1383,6 +1392,7 @@ async fn simultaneous_dial_then_redial_websocket() { .await } +#[cfg(feature = "quic")] #[tokio::test] async fn simultaneous_dial_then_redial_quic() { simultaneous_dial_then_redial( @@ -1402,24 +1412,14 @@ async fn simultaneous_dial_then_redial(transport1: Transport, transport2: Transp .with_keypair(Keypair::generate()) .with_libp2p_ping(ping_config1); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - Transport::Quic(config) => config1.with_quic(config), - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let (ping_config2, _ping_event_stream2) = PingConfig::default(); let config2 = ConfigBuilder::new() .with_keypair(Keypair::generate()) .with_libp2p_ping(ping_config2); - let config2 = match transport2 { - Transport::Tcp(config) => config2.with_tcp(config), - Transport::Quic(config) => config2.with_quic(config), - Transport::WebSocket(config) => config2.with_websocket(config), - } - .build(); + let config2 = add_transport(config2, transport2).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let mut litep2p2 = Litep2p::new(config2).unwrap(); diff --git a/tests/mod.rs b/tests/mod.rs index bdd53f26..a4bede25 100644 --- a/tests/mod.rs +++ b/tests/mod.rs @@ -18,6 +18,7 @@ // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. +mod common; mod conformance; mod connection; mod protocol; diff --git a/tests/protocol/identify.rs b/tests/protocol/identify.rs index 07f29d15..efb3da28 100644 --- a/tests/protocol/identify.rs +++ b/tests/protocol/identify.rs @@ -26,18 +26,10 @@ use litep2p::{ identify::{Config, IdentifyEvent}, ping::Config as PingConfig, }, - transport::{ - quic::config::Config as QuicConfig, tcp::config::Config as TcpConfig, - websocket::config::Config as WebSocketConfig, - }, Litep2p, Litep2pEvent, }; -enum Transport { - Quic(QuicConfig), - Tcp(TcpConfig), - WebSocket(WebSocketConfig), -} +use crate::common::{add_transport, Transport}; #[tokio::test] async fn identify_supported_tcp() { @@ -48,6 +40,7 @@ async fn identify_supported_tcp() { .await } +#[cfg(feature = "quic")] #[tokio::test] async fn identify_supported_quic() { identify_supported( @@ -57,6 +50,7 @@ async fn identify_supported_quic() { .await } +#[cfg(feature = "websocket")] #[tokio::test] async fn identify_supported_websocket() { identify_supported( @@ -76,32 +70,20 @@ async fn identify_supported(transport1: Transport, transport2: Transport) { Some("agent v1".to_string()), Vec::new(), ); - let config_builder = ConfigBuilder::new() + let config_builder1 = ConfigBuilder::new() .with_keypair(Keypair::generate()) .with_libp2p_identify(identify_config1); - - let config1 = match transport1 { - Transport::Tcp(config) => config_builder.with_tcp(config), - Transport::Quic(config) => config_builder.with_quic(config), - Transport::WebSocket(config) => config_builder.with_websocket(config), - } - .build(); + let config1 = add_transport(config_builder1, transport1).build(); let (identify_config2, mut identify_event_stream2) = Config::new( "/proto/2".to_string(), Some("agent v2".to_string()), Vec::new(), ); - let config_builder = ConfigBuilder::new() + let config_builder2 = ConfigBuilder::new() .with_keypair(Keypair::generate()) .with_libp2p_identify(identify_config2); - - let config2 = match transport2 { - Transport::Tcp(config) => config_builder.with_tcp(config), - Transport::Quic(config) => config_builder.with_quic(config), - Transport::WebSocket(config) => config_builder.with_websocket(config), - } - .build(); + let config2 = add_transport(config_builder2, transport2).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let mut litep2p2 = Litep2p::new(config2).unwrap(); @@ -180,6 +162,7 @@ async fn identify_not_supported_tcp() { .await } +#[cfg(feature = "quic")] #[tokio::test] async fn identify_not_supported_quic() { identify_not_supported( @@ -189,6 +172,7 @@ async fn identify_not_supported_quic() { .await } +#[cfg(feature = "websocket")] #[tokio::test] async fn identify_not_supported_websocket() { identify_not_supported( @@ -204,27 +188,17 @@ async fn identify_not_supported(transport1: Transport, transport2: Transport) { .try_init(); let (ping_config, _event_stream) = PingConfig::default(); - let config1 = match transport1 { - Transport::Tcp(config) => ConfigBuilder::new().with_tcp(config), - Transport::Quic(config) => ConfigBuilder::new().with_quic(config), - Transport::WebSocket(config) => ConfigBuilder::new().with_websocket(config), - } - .with_keypair(Keypair::generate()) - .with_libp2p_ping(ping_config) - .build(); + let config_builder1 = ConfigBuilder::new() + .with_keypair(Keypair::generate()) + .with_libp2p_ping(ping_config); + let config1 = add_transport(config_builder1, transport1).build(); let (identify_config2, mut identify_event_stream2) = Config::new("litep2p".to_string(), None, Vec::new()); - let config_builder = ConfigBuilder::new() + let config_builder2 = ConfigBuilder::new() .with_keypair(Keypair::generate()) .with_libp2p_identify(identify_config2); - - let config2 = match transport2 { - Transport::Tcp(config) => config_builder.with_tcp(config), - Transport::Quic(config) => config_builder.with_quic(config), - Transport::WebSocket(config) => config_builder.with_websocket(config), - } - .build(); + let config2 = add_transport(config_builder2, transport2).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let mut litep2p2 = Litep2p::new(config2).unwrap(); diff --git a/tests/protocol/notification.rs b/tests/protocol/notification.rs index f40fc3f6..ae034d49 100644 --- a/tests/protocol/notification.rs +++ b/tests/protocol/notification.rs @@ -26,30 +26,24 @@ use litep2p::{ Config as NotificationConfig, ConfigBuilder, Direction, NotificationError, NotificationEvent, NotificationHandle, ValidationResult, }, - transport::{ - quic::config::Config as QuicConfig, tcp::config::Config as TcpConfig, - websocket::config::Config as WebSocketConfig, - }, + transport::tcp::config::Config as TcpConfig, types::protocol::ProtocolName, Litep2p, Litep2pEvent, PeerId, }; +#[cfg(feature = "websocket")] +use litep2p::transport::websocket::config::Config as WebSocketConfig; + use bytes::BytesMut; use futures::StreamExt; use multiaddr::{Multiaddr, Protocol}; use multihash::Multihash; -use std::{ - net::{Ipv4Addr, Ipv6Addr}, - task::Poll, - time::Duration, -}; +#[cfg(feature = "quic")] +use std::net::Ipv4Addr; +use std::{net::Ipv6Addr, task::Poll, time::Duration}; -enum Transport { - Tcp(TcpConfig), - Quic(QuicConfig), - WebSocket(WebSocketConfig), -} +use crate::common::{add_transport, Transport}; async fn connect_peers(litep2p1: &mut Litep2p, litep2p2: &mut Litep2p) { let address = litep2p2.listen_addresses().next().unwrap().clone(); @@ -97,12 +91,7 @@ async fn make_default_litep2p(transport: Transport) -> (Litep2p, NotificationHan .with_keypair(Keypair::generate()) .with_notification_protocol(notif_config); - let config = match transport { - Transport::Tcp(transport_config) => config.with_tcp(transport_config), - Transport::Quic(transport_config) => config.with_quic(transport_config), - Transport::WebSocket(transport_config) => config.with_websocket(transport_config), - } - .build(); + let config = add_transport(config, transport).build(); (Litep2p::new(config).unwrap(), handle) } @@ -122,6 +111,7 @@ async fn open_substreams_tcp() { .await } +#[cfg(feature = "quic")] #[tokio::test] async fn open_substreams_quic() { open_substreams( @@ -131,6 +121,7 @@ async fn open_substreams_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn open_substreams_websocket() { open_substreams( @@ -165,12 +156,7 @@ async fn open_substreams(transport1: Transport, transport2: Transport) { .with_keypair(Keypair::generate()) .with_notification_protocol(notif_config1); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - Transport::Quic(config) => config1.with_quic(config), - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let (notif_config2, mut handle2) = NotificationConfig::new( ProtocolName::from("/notif/1"), @@ -186,12 +172,7 @@ async fn open_substreams(transport1: Transport, transport2: Transport) { .with_keypair(Keypair::generate()) .with_notification_protocol(notif_config2); - let config2 = match transport2 { - Transport::Tcp(config) => config2.with_tcp(config), - Transport::Quic(config) => config2.with_quic(config), - Transport::WebSocket(config) => config2.with_websocket(config), - } - .build(); + let config2 = add_transport(config2, transport2).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let mut litep2p2 = Litep2p::new(config2).unwrap(); @@ -289,6 +270,7 @@ async fn reject_substream_tcp() { .await; } +#[cfg(feature = "quic")] #[tokio::test] async fn reject_substream_quic() { reject_substream( @@ -298,6 +280,7 @@ async fn reject_substream_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn reject_substream_websocket() { reject_substream( @@ -332,12 +315,7 @@ async fn reject_substream(transport1: Transport, transport2: Transport) { .with_keypair(Keypair::generate()) .with_notification_protocol(notif_config1); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - Transport::Quic(config) => config1.with_quic(config), - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let (notif_config2, mut handle2) = NotificationConfig::new( ProtocolName::from("/notif/1"), @@ -353,12 +331,7 @@ async fn reject_substream(transport1: Transport, transport2: Transport) { .with_keypair(Keypair::generate()) .with_notification_protocol(notif_config2); - let config2 = match transport2 { - Transport::Tcp(config) => config2.with_tcp(config), - Transport::Quic(config) => config2.with_quic(config), - Transport::WebSocket(config) => config2.with_websocket(config), - } - .build(); + let config2 = add_transport(config2, transport2).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let mut litep2p2 = Litep2p::new(config2).unwrap(); @@ -414,6 +387,7 @@ async fn notification_stream_closed_tcp() { .await } +#[cfg(feature = "quic")] #[tokio::test] async fn notification_stream_closed_quic() { notification_stream_closed( @@ -423,6 +397,7 @@ async fn notification_stream_closed_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn notification_stream_closed_websocket() { notification_stream_closed( @@ -457,12 +432,7 @@ async fn notification_stream_closed(transport1: Transport, transport2: Transport .with_keypair(Keypair::generate()) .with_notification_protocol(notif_config1); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - Transport::Quic(config) => config1.with_quic(config), - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let (notif_config2, mut handle2) = NotificationConfig::new( ProtocolName::from("/notif/1"), @@ -478,12 +448,7 @@ async fn notification_stream_closed(transport1: Transport, transport2: Transport .with_keypair(Keypair::generate()) .with_notification_protocol(notif_config2); - let config2 = match transport2 { - Transport::Tcp(config) => config2.with_tcp(config), - Transport::Quic(config) => config2.with_quic(config), - Transport::WebSocket(config) => config2.with_websocket(config), - } - .build(); + let config2 = add_transport(config2, transport2).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let mut litep2p2 = Litep2p::new(config2).unwrap(); @@ -588,6 +553,7 @@ async fn reconnect_after_disconnect_tcp() { .await } +#[cfg(feature = "quic")] #[tokio::test] async fn reconnect_after_disconnect_quic() { reconnect_after_disconnect( @@ -597,6 +563,7 @@ async fn reconnect_after_disconnect_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn reconnect_after_disconnect_websocket() { reconnect_after_disconnect( @@ -631,12 +598,7 @@ async fn reconnect_after_disconnect(transport1: Transport, transport2: Transport .with_keypair(Keypair::generate()) .with_notification_protocol(notif_config1); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - Transport::Quic(config) => config1.with_quic(config), - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let (notif_config2, mut handle2) = NotificationConfig::new( ProtocolName::from("/notif/1"), @@ -652,12 +614,7 @@ async fn reconnect_after_disconnect(transport1: Transport, transport2: Transport .with_keypair(Keypair::generate()) .with_notification_protocol(notif_config2); - let config2 = match transport2 { - Transport::Tcp(config) => config2.with_tcp(config), - Transport::Quic(config) => config2.with_quic(config), - Transport::WebSocket(config) => config2.with_websocket(config), - } - .build(); + let config2 = add_transport(config2, transport2).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let mut litep2p2 = Litep2p::new(config2).unwrap(); @@ -819,6 +776,7 @@ async fn set_new_handshake_tcp() { .await } +#[cfg(feature = "quic")] #[tokio::test] async fn set_new_handshake_quic() { set_new_handshake( @@ -828,6 +786,7 @@ async fn set_new_handshake_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn set_new_handshake_websocket() { set_new_handshake( @@ -864,7 +823,9 @@ async fn set_new_handshake(transport1: Transport, transport2: Transport) { let config1 = match transport1 { Transport::Tcp(config) => config1.with_tcp(config), + #[cfg(feature = "quic")] Transport::Quic(config) => config1.with_quic(config), + #[cfg(feature = "websocket")] Transport::WebSocket(config) => config1.with_websocket(config), } .build(); @@ -883,12 +844,7 @@ async fn set_new_handshake(transport1: Transport, transport2: Transport) { .with_keypair(Keypair::generate()) .with_notification_protocol(notif_config2); - let config2 = match transport2 { - Transport::Tcp(config) => config2.with_tcp(config), - Transport::Quic(config) => config2.with_quic(config), - Transport::WebSocket(config) => config2.with_websocket(config), - } - .build(); + let config2 = add_transport(config2, transport2).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let mut litep2p2 = Litep2p::new(config2).unwrap(); @@ -1035,6 +991,7 @@ async fn both_nodes_open_substreams_tcp() { .await } +#[cfg(feature = "quic")] #[tokio::test] async fn both_nodes_open_substreams_quic() { both_nodes_open_substreams( @@ -1044,6 +1001,7 @@ async fn both_nodes_open_substreams_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn both_nodes_open_substreams_websocket() { both_nodes_open_substreams( @@ -1078,12 +1036,7 @@ async fn both_nodes_open_substreams(transport1: Transport, transport2: Transport .with_keypair(Keypair::generate()) .with_notification_protocol(notif_config1); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - Transport::Quic(config) => config1.with_quic(config), - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let (notif_config2, mut handle2) = NotificationConfig::new( ProtocolName::from("/notif/1"), @@ -1099,12 +1052,7 @@ async fn both_nodes_open_substreams(transport1: Transport, transport2: Transport .with_keypair(Keypair::generate()) .with_notification_protocol(notif_config2); - let config2 = match transport2 { - Transport::Tcp(config) => config2.with_tcp(config), - Transport::Quic(config) => config2.with_quic(config), - Transport::WebSocket(config) => config2.with_websocket(config), - } - .build(); + let config2 = add_transport(config2, transport2).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let mut litep2p2 = Litep2p::new(config2).unwrap(); @@ -1207,6 +1155,7 @@ async fn both_nodes_open_substream_one_rejects_substreams_tcp() { .await } +#[cfg(feature = "quic")] #[tokio::test] #[cfg(debug_assertions)] async fn both_nodes_open_substream_one_rejects_substreams_quic() { @@ -1217,6 +1166,7 @@ async fn both_nodes_open_substream_one_rejects_substreams_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] #[cfg(debug_assertions)] async fn both_nodes_open_substream_one_rejects_substreams_websocket() { @@ -1255,12 +1205,7 @@ async fn both_nodes_open_substream_one_rejects_substreams( .with_keypair(Keypair::generate()) .with_notification_protocol(notif_config1); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - Transport::Quic(config) => config1.with_quic(config), - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let (notif_config2, mut handle2) = NotificationConfig::new( ProtocolName::from("/notif/1"), @@ -1276,12 +1221,7 @@ async fn both_nodes_open_substream_one_rejects_substreams( .with_keypair(Keypair::generate()) .with_notification_protocol(notif_config2); - let config2 = match transport2 { - Transport::Tcp(config) => config2.with_tcp(config), - Transport::Quic(config) => config2.with_quic(config), - Transport::WebSocket(config) => config2.with_websocket(config), - } - .build(); + let config2 = add_transport(config2, transport2).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let mut litep2p2 = Litep2p::new(config2).unwrap(); @@ -1348,11 +1288,13 @@ async fn send_sync_notification_to_non_existent_peer_tcp() { .await } +#[cfg(feature = "quic")] #[tokio::test] async fn send_sync_notification_to_non_existent_peer_quic() { send_sync_notification_to_non_existent_peer(Transport::Quic(Default::default())).await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn send_sync_notification_to_non_existent_peer_websocket() { send_sync_notification_to_non_existent_peer(Transport::WebSocket(WebSocketConfig { @@ -1381,12 +1323,7 @@ async fn send_sync_notification_to_non_existent_peer(transport1: Transport) { .with_keypair(Keypair::generate()) .with_notification_protocol(notif_config1); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - Transport::Quic(config) => config1.with_quic(config), - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); @@ -1410,11 +1347,13 @@ async fn send_async_notification_to_non_existent_peer_tcp() { .await } +#[cfg(feature = "quic")] #[tokio::test] async fn send_async_notification_to_non_existent_peer_quic() { send_async_notification_to_non_existent_peer(Transport::Quic(Default::default())).await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn send_async_notification_to_non_existent_peer_websocket() { send_async_notification_to_non_existent_peer(Transport::WebSocket(WebSocketConfig { @@ -1443,12 +1382,7 @@ async fn send_async_notification_to_non_existent_peer(transport1: Transport) { .with_keypair(Keypair::generate()) .with_notification_protocol(notif_config1); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - Transport::Quic(config) => config1.with_quic(config), - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); @@ -1475,11 +1409,13 @@ async fn try_to_connect_to_non_existent_peer_tcp() { .await } +#[cfg(feature = "quic")] #[tokio::test] async fn try_to_connect_to_non_existent_peer_quic() { try_to_connect_to_non_existent_peer(Transport::Quic(Default::default())).await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn try_to_connect_to_non_existent_peer_websocket() { try_to_connect_to_non_existent_peer(Transport::WebSocket(WebSocketConfig { @@ -1508,12 +1444,7 @@ async fn try_to_connect_to_non_existent_peer(transport1: Transport) { .with_keypair(Keypair::generate()) .with_notification_protocol(notif_config1); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - Transport::Quic(config) => config1.with_quic(config), - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); @@ -1545,11 +1476,13 @@ async fn try_to_disconnect_non_existent_peer_tcp() { .await } +#[cfg(feature = "quic")] #[tokio::test] async fn try_to_disconnect_non_existent_peer_quic() { try_to_disconnect_non_existent_peer(Transport::Quic(Default::default())).await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn try_to_disconnect_non_existent_peer_websocket() { try_to_disconnect_non_existent_peer(Transport::WebSocket(WebSocketConfig { @@ -1578,12 +1511,7 @@ async fn try_to_disconnect_non_existent_peer(transport1: Transport) { .with_keypair(Keypair::generate()) .with_notification_protocol(notif_config1); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - Transport::Quic(config) => config1.with_quic(config), - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); @@ -1613,6 +1541,7 @@ async fn try_to_reopen_substream_tcp() { .await; } +#[cfg(feature = "quic")] #[tokio::test] async fn try_to_reopen_substream_quic() { try_to_reopen_substream( @@ -1622,6 +1551,7 @@ async fn try_to_reopen_substream_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn try_to_reopen_substream_websocket() { try_to_reopen_substream( @@ -1656,12 +1586,7 @@ async fn try_to_reopen_substream(transport1: Transport, transport2: Transport) { .with_keypair(Keypair::generate()) .with_notification_protocol(notif_config1); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - Transport::Quic(config) => config1.with_quic(config), - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let (notif_config2, mut handle2) = NotificationConfig::new( ProtocolName::from("/notif/1"), @@ -1677,12 +1602,7 @@ async fn try_to_reopen_substream(transport1: Transport, transport2: Transport) { .with_keypair(Keypair::generate()) .with_notification_protocol(notif_config2); - let config2 = match transport2 { - Transport::Tcp(config) => config2.with_tcp(config), - Transport::Quic(config) => config2.with_quic(config), - Transport::WebSocket(config) => config2.with_websocket(config), - } - .build(); + let config2 = add_transport(config2, transport2).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let mut litep2p2 = Litep2p::new(config2).unwrap(); @@ -1769,6 +1689,7 @@ async fn substream_validation_timeout_tcp() { .await; } +#[cfg(feature = "quic")] #[tokio::test] async fn substream_validation_timeout_quic() { substream_validation_timeout( @@ -1778,6 +1699,7 @@ async fn substream_validation_timeout_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn substream_validation_timeout_websocket() { substream_validation_timeout( @@ -1812,12 +1734,7 @@ async fn substream_validation_timeout(transport1: Transport, transport2: Transpo .with_keypair(Keypair::generate()) .with_notification_protocol(notif_config1); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - Transport::Quic(config) => config1.with_quic(config), - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let (notif_config2, mut handle2) = NotificationConfig::new( ProtocolName::from("/notif/1"), @@ -1833,12 +1750,7 @@ async fn substream_validation_timeout(transport1: Transport, transport2: Transpo .with_keypair(Keypair::generate()) .with_notification_protocol(notif_config2); - let config2 = match transport2 { - Transport::Tcp(config) => config2.with_tcp(config), - Transport::Quic(config) => config2.with_quic(config), - Transport::WebSocket(config) => config2.with_websocket(config), - } - .build(); + let config2 = add_transport(config2, transport2).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let mut litep2p2 = Litep2p::new(config2).unwrap(); @@ -1894,6 +1806,7 @@ async fn unsupported_protocol_tcp() { .await; } +#[cfg(feature = "quic")] #[tokio::test] async fn unsupported_protocol_quic() { unsupported_protocol( @@ -1903,6 +1816,7 @@ async fn unsupported_protocol_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn unsupported_protocol_websocket() { unsupported_protocol( @@ -1932,12 +1846,7 @@ async fn unsupported_protocol(transport1: Transport, transport2: Transport) { .with_keypair(Keypair::generate()) .with_notification_protocol(notif_config1); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - Transport::Quic(config) => config1.with_quic(config), - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let (notif_config2, _handle2) = ConfigBuilder::new(ProtocolName::from("/notif/2")) .with_max_size(1024usize) @@ -1947,12 +1856,7 @@ async fn unsupported_protocol(transport1: Transport, transport2: Transport) { .with_keypair(Keypair::generate()) .with_notification_protocol(notif_config2); - let config2 = match transport2 { - Transport::Tcp(config) => config2.with_tcp(config), - Transport::Quic(config) => config2.with_quic(config), - Transport::WebSocket(config) => config2.with_websocket(config), - } - .build(); + let config2 = add_transport(config2, transport2).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let mut litep2p2 = Litep2p::new(config2).unwrap(); @@ -1996,6 +1900,7 @@ async fn dialer_fallback_protocol_works_tcp() { .await; } +#[cfg(feature = "quic")] #[tokio::test] async fn dialer_fallback_protocol_works_quic() { dialer_fallback_protocol_works( @@ -2005,6 +1910,7 @@ async fn dialer_fallback_protocol_works_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn dialer_fallback_protocol_works_websocket() { dialer_fallback_protocol_works( @@ -2035,12 +1941,7 @@ async fn dialer_fallback_protocol_works(transport1: Transport, transport2: Trans .with_keypair(Keypair::generate()) .with_notification_protocol(notif_config1); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - Transport::Quic(config) => config1.with_quic(config), - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let (notif_config2, mut handle2) = ConfigBuilder::new(ProtocolName::from("/notif/1")) .with_max_size(1024usize) @@ -2050,12 +1951,7 @@ async fn dialer_fallback_protocol_works(transport1: Transport, transport2: Trans .with_keypair(Keypair::generate()) .with_notification_protocol(notif_config2); - let config2 = match transport2 { - Transport::Tcp(config) => config2.with_tcp(config), - Transport::Quic(config) => config2.with_quic(config), - Transport::WebSocket(config) => config2.with_websocket(config), - } - .build(); + let config2 = add_transport(config2, transport2).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let mut litep2p2 = Litep2p::new(config2).unwrap(); @@ -2134,6 +2030,7 @@ async fn listener_fallback_protocol_works_tcp() { .await; } +#[cfg(feature = "quic")] #[tokio::test] async fn listener_fallback_protocol_works_quic() { listener_fallback_protocol_works( @@ -2143,6 +2040,7 @@ async fn listener_fallback_protocol_works_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn listener_fallback_protocol_works_websocket() { listener_fallback_protocol_works( @@ -2172,12 +2070,7 @@ async fn listener_fallback_protocol_works(transport1: Transport, transport2: Tra .with_keypair(Keypair::generate()) .with_notification_protocol(notif_config1); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - Transport::Quic(config) => config1.with_quic(config), - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let (notif_config2, mut handle2) = ConfigBuilder::new(ProtocolName::from("/notif/2")) .with_max_size(1024usize) @@ -2188,12 +2081,7 @@ async fn listener_fallback_protocol_works(transport1: Transport, transport2: Tra .with_keypair(Keypair::generate()) .with_notification_protocol(notif_config2); - let config2 = match transport2 { - Transport::Tcp(config) => config2.with_tcp(config), - Transport::Quic(config) => config2.with_quic(config), - Transport::WebSocket(config) => config2.with_websocket(config), - } - .build(); + let config2 = add_transport(config2, transport2).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let mut litep2p2 = Litep2p::new(config2).unwrap(); @@ -2272,6 +2160,7 @@ async fn enable_auto_accept_tcp() { .await } +#[cfg(feature = "quic")] #[tokio::test] async fn enable_auto_accept_quic() { enable_auto_accept( @@ -2281,6 +2170,7 @@ async fn enable_auto_accept_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn enable_auto_accept_websocket() { enable_auto_accept( @@ -2315,12 +2205,7 @@ async fn enable_auto_accept(transport1: Transport, transport2: Transport) { .with_keypair(Keypair::generate()) .with_notification_protocol(notif_config1); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - Transport::Quic(config) => config1.with_quic(config), - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let (notif_config2, mut handle2) = NotificationConfig::new( ProtocolName::from("/notif/1"), @@ -2336,12 +2221,7 @@ async fn enable_auto_accept(transport1: Transport, transport2: Transport) { .with_keypair(Keypair::generate()) .with_notification_protocol(notif_config2); - let config2 = match transport2 { - Transport::Tcp(config) => config2.with_tcp(config), - Transport::Quic(config) => config2.with_quic(config), - Transport::WebSocket(config) => config2.with_websocket(config), - } - .build(); + let config2 = add_transport(config2, transport2).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let mut litep2p2 = Litep2p::new(config2).unwrap(); @@ -2428,6 +2308,7 @@ async fn send_using_notification_sink_tcp() { .await } +#[cfg(feature = "quic")] #[tokio::test] async fn send_using_notification_sink_quic() { send_using_notification_sink( @@ -2437,6 +2318,7 @@ async fn send_using_notification_sink_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn send_using_notification_sink_websocket() { send_using_notification_sink( @@ -2471,12 +2353,7 @@ async fn send_using_notification_sink(transport1: Transport, transport2: Transpo .with_keypair(Keypair::generate()) .with_notification_protocol(notif_config1); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - Transport::Quic(config) => config1.with_quic(config), - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let (notif_config2, mut handle2) = NotificationConfig::new( ProtocolName::from("/notif/1"), @@ -2492,12 +2369,7 @@ async fn send_using_notification_sink(transport1: Transport, transport2: Transpo .with_keypair(Keypair::generate()) .with_notification_protocol(notif_config2); - let config2 = match transport2 { - Transport::Tcp(config) => config2.with_tcp(config), - Transport::Quic(config) => config2.with_quic(config), - Transport::WebSocket(config) => config2.with_websocket(config), - } - .build(); + let config2 = add_transport(config2, transport2).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let mut litep2p2 = Litep2p::new(config2).unwrap(); @@ -2609,6 +2481,7 @@ async fn dial_peer_when_opening_substream_tcp() { .await } +#[cfg(feature = "quic")] #[tokio::test] async fn dial_peer_when_opening_substream_quic() { dial_peer_when_opening_substream( @@ -2618,6 +2491,7 @@ async fn dial_peer_when_opening_substream_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn dial_peer_when_opening_substream_websocket() { dial_peer_when_opening_substream( @@ -2652,12 +2526,7 @@ async fn dial_peer_when_opening_substream(transport1: Transport, transport2: Tra .with_keypair(Keypair::generate()) .with_notification_protocol(notif_config1); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - Transport::Quic(config) => config1.with_quic(config), - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let (notif_config2, mut handle2) = NotificationConfig::new( ProtocolName::from("/notif/1"), @@ -2673,12 +2542,7 @@ async fn dial_peer_when_opening_substream(transport1: Transport, transport2: Tra .with_keypair(Keypair::generate()) .with_notification_protocol(notif_config2); - let config2 = match transport2 { - Transport::Tcp(config) => config2.with_tcp(config), - Transport::Quic(config) => config2.with_quic(config), - Transport::WebSocket(config) => config2.with_websocket(config), - } - .build(); + let config2 = add_transport(config2, transport2).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let mut litep2p2 = Litep2p::new(config2).unwrap(); @@ -2796,6 +2660,7 @@ async fn open_and_close_batched_tcp() { .await } +#[cfg(feature = "quic")] #[tokio::test] async fn open_and_close_batched_quic() { open_and_close_batched( @@ -2806,6 +2671,7 @@ async fn open_and_close_batched_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn open_and_close_batched_websocket() { open_and_close_batched( @@ -2999,6 +2865,7 @@ async fn open_and_close_batched_duplicate_peer_tcp() { .await } +#[cfg(feature = "quic")] #[tokio::test] async fn open_and_close_batched_duplicate_peer_quic() { open_and_close_batched_duplicate_peer( @@ -3009,6 +2876,7 @@ async fn open_and_close_batched_duplicate_peer_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn open_and_close_batched_duplicate_peer_websocket() { open_and_close_batched_duplicate_peer( @@ -3245,6 +3113,7 @@ async fn no_listener_address_for_one_peer_tcp() { .await } +#[cfg(feature = "quic")] #[tokio::test] async fn no_listener_address_for_one_peer_quic() { no_listener_address_for_one_peer( @@ -3254,6 +3123,7 @@ async fn no_listener_address_for_one_peer_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn no_listener_address_for_one_peer_websocket() { no_listener_address_for_one_peer( @@ -3365,6 +3235,7 @@ async fn auto_accept_inbound_tcp() { .await } +#[cfg(feature = "quic")] #[tokio::test] async fn auto_accept_inbound_quic() { auto_accept_inbound( @@ -3374,6 +3245,7 @@ async fn auto_accept_inbound_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn auto_accept_inbound_websocket() { auto_accept_inbound( @@ -3400,12 +3272,7 @@ async fn auto_accept_inbound(transport1: Transport, transport2: Transport) { .with_keypair(Keypair::generate()) .with_notification_protocol(notif_config1); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - Transport::Quic(config) => config1.with_quic(config), - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let (mut notif_config2, mut handle2) = ConfigBuilder::new(ProtocolName::from("/notif/1")) .with_max_size(1024usize) @@ -3422,12 +3289,7 @@ async fn auto_accept_inbound(transport1: Transport, transport2: Transport) { .with_keypair(Keypair::generate()) .with_notification_protocol(notif_config2); - let config2 = match transport2 { - Transport::Tcp(config) => config2.with_tcp(config), - Transport::Quic(config) => config2.with_quic(config), - Transport::WebSocket(config) => config2.with_websocket(config), - } - .build(); + let config2 = add_transport(config2, transport2).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let mut litep2p2 = Litep2p::new(config2).unwrap(); @@ -3507,6 +3369,7 @@ async fn dial_failure_tcp() { .await } +#[cfg(feature = "quic")] #[tokio::test] async fn dial_failure_quic() { dial_failure( @@ -3516,6 +3379,7 @@ async fn dial_failure_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn dial_failure_websocket() { dial_failure( @@ -3547,12 +3411,7 @@ async fn dial_failure(transport1: Transport, transport2: Transport) { .with_notification_protocol(notif_config1) .with_notification_protocol(notif_config2); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - Transport::Quic(config) => config1.with_quic(config), - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let (notif_config3, _handle3) = ConfigBuilder::new(ProtocolName::from("/notif/1")) .with_max_size(1024usize) @@ -3570,22 +3429,19 @@ async fn dial_failure(transport1: Transport, transport2: Transport) { Transport::Tcp(_) => Multiaddr::empty() .with(Protocol::Ip6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1))) .with(Protocol::Tcp(5)), + #[cfg(feature = "quic")] Transport::Quic(_) => Multiaddr::empty() .with(Protocol::Ip4(Ipv4Addr::new(127, 0, 0, 1))) .with(Protocol::Udp(5)) .with(Protocol::QuicV1), + #[cfg(feature = "websocket")] Transport::WebSocket(_) => Multiaddr::empty() .with(Protocol::Ip6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1))) .with(Protocol::Tcp(5)) .with(Protocol::Ws(std::borrow::Cow::Owned("/".to_string()))), }; - let config2 = match transport2 { - Transport::Tcp(config) => config2.with_tcp(config), - Transport::Quic(config) => config2.with_quic(config), - Transport::WebSocket(config) => config2.with_websocket(config), - } - .build(); + let config2 = add_transport(config2, transport2).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let mut litep2p2 = Litep2p::new(config2).unwrap(); @@ -3630,6 +3486,7 @@ async fn dialing_disabled_tcp() { .await } +#[cfg(feature = "quic")] #[tokio::test] async fn dialing_disabled_quic() { dialing_disabled( @@ -3639,6 +3496,7 @@ async fn dialing_disabled_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn dialing_disabled_websocket() { dialing_disabled( @@ -3672,12 +3530,7 @@ async fn dialing_disabled(transport1: Transport, transport2: Transport) { .with_notification_protocol(notif_config1) .with_notification_protocol(notif_config2); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - Transport::Quic(config) => config1.with_quic(config), - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let (notif_config3, _handle3) = ConfigBuilder::new(ProtocolName::from("/notif/1")) .with_max_size(1024usize) @@ -3691,12 +3544,7 @@ async fn dialing_disabled(transport1: Transport, transport2: Transport) { .with_keypair(Keypair::generate()) .with_notification_protocol(notif_config3); - let config2 = match transport2 { - Transport::Tcp(config) => config2.with_tcp(config), - Transport::Quic(config) => config2.with_quic(config), - Transport::WebSocket(config) => config2.with_websocket(config), - } - .build(); + let config2 = add_transport(config2, transport2).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let mut litep2p2 = Litep2p::new(config2).unwrap(); @@ -3741,6 +3589,7 @@ async fn validation_takes_too_long_tcp() { .await } +#[cfg(feature = "quic")] #[tokio::test] async fn validation_takes_too_long_quic() { validation_takes_too_long( @@ -3750,6 +3599,7 @@ async fn validation_takes_too_long_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn validation_takes_too_long_websocket() { validation_takes_too_long( @@ -3773,12 +3623,7 @@ async fn validation_takes_too_long(transport1: Transport, transport2: Transport) .with_keypair(Keypair::generate()) .with_notification_protocol(notif_config1); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - Transport::Quic(config) => config1.with_quic(config), - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let (notif_config3, mut handle2) = ConfigBuilder::new(ProtocolName::from("/notif/1")) .with_max_size(1024usize) @@ -3789,12 +3634,7 @@ async fn validation_takes_too_long(transport1: Transport, transport2: Transport) .with_keypair(Keypair::generate()) .with_notification_protocol(notif_config3); - let config2 = match transport2 { - Transport::Tcp(config) => config2.with_tcp(config), - Transport::Quic(config) => config2.with_quic(config), - Transport::WebSocket(config) => config2.with_websocket(config), - } - .build(); + let config2 = add_transport(config2, transport2).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let mut litep2p2 = Litep2p::new(config2).unwrap(); @@ -3856,6 +3696,7 @@ async fn ignored_validation_open_substream_tcp() { .await } +#[cfg(feature = "quic")] #[tokio::test] async fn ignored_validation_open_substream_quic() { ignored_validation_open_substream( @@ -3865,6 +3706,7 @@ async fn ignored_validation_open_substream_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn ignored_validation_open_substream_websocket() { ignored_validation_open_substream( @@ -3888,12 +3730,7 @@ async fn ignored_validation_open_substream(transport1: Transport, transport2: Tr .with_keypair(Keypair::generate()) .with_notification_protocol(notif_config1); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - Transport::Quic(config) => config1.with_quic(config), - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let (notif_config3, mut handle2) = ConfigBuilder::new(ProtocolName::from("/notif/1")) .with_max_size(1024usize) @@ -3904,12 +3741,7 @@ async fn ignored_validation_open_substream(transport1: Transport, transport2: Tr .with_keypair(Keypair::generate()) .with_notification_protocol(notif_config3); - let config2 = match transport2 { - Transport::Tcp(config) => config2.with_tcp(config), - Transport::Quic(config) => config2.with_quic(config), - Transport::WebSocket(config) => config2.with_websocket(config), - } - .build(); + let config2 = add_transport(config2, transport2).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let mut litep2p2 = Litep2p::new(config2).unwrap(); @@ -4007,6 +3839,7 @@ async fn clogged_channel_disconnects_peer_tcp() { .await } +#[cfg(feature = "quic")] #[tokio::test] async fn clogged_channel_disconnects_peer_quic() { clogged_channel_disconnects_peer( @@ -4016,6 +3849,7 @@ async fn clogged_channel_disconnects_peer_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn clogged_channel_disconnects_peer_websocket() { clogged_channel_disconnects_peer( @@ -4040,12 +3874,7 @@ async fn clogged_channel_disconnects_peer(transport1: Transport, transport2: Tra .with_keypair(Keypair::generate()) .with_notification_protocol(notif_config1); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - Transport::Quic(config) => config1.with_quic(config), - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let (notif_config3, mut handle2) = ConfigBuilder::new(ProtocolName::from("/notif/1")) .with_max_size(100 * 1024) @@ -4057,12 +3886,7 @@ async fn clogged_channel_disconnects_peer(transport1: Transport, transport2: Tra .with_keypair(Keypair::generate()) .with_notification_protocol(notif_config3); - let config2 = match transport2 { - Transport::Tcp(config) => config2.with_tcp(config), - Transport::Quic(config) => config2.with_quic(config), - Transport::WebSocket(config) => config2.with_websocket(config), - } - .build(); + let config2 = add_transport(config2, transport2).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let mut litep2p2 = Litep2p::new(config2).unwrap(); diff --git a/tests/protocol/ping.rs b/tests/protocol/ping.rs index 32beb41d..5a6fecbc 100644 --- a/tests/protocol/ping.rs +++ b/tests/protocol/ping.rs @@ -20,20 +20,10 @@ use futures::StreamExt; use litep2p::{ - config::ConfigBuilder, - protocol::libp2p::ping::ConfigBuilder as PingConfigBuilder, - transport::{ - quic::config::Config as QuicConfig, tcp::config::Config as TcpConfig, - websocket::config::Config as WebSocketConfig, - }, - Litep2p, + config::ConfigBuilder, protocol::libp2p::ping::ConfigBuilder as PingConfigBuilder, Litep2p, }; -enum Transport { - Tcp(TcpConfig), - Quic(QuicConfig), - WebSocket(WebSocketConfig), -} +use crate::common::{add_transport, Transport}; #[tokio::test] async fn ping_supported_tcp() { @@ -44,6 +34,7 @@ async fn ping_supported_tcp() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn ping_supported_websocket() { ping_supported( @@ -53,6 +44,7 @@ async fn ping_supported_websocket() { .await; } +#[cfg(feature = "quic")] #[tokio::test] async fn ping_supported_quic() { ping_supported( @@ -69,22 +61,12 @@ async fn ping_supported(transport1: Transport, transport2: Transport) { let (ping_config1, mut ping_event_stream1) = PingConfigBuilder::new().with_max_failure(3usize).build(); - let config1 = match transport1 { - Transport::Tcp(config) => ConfigBuilder::new().with_tcp(config), - Transport::Quic(config) => ConfigBuilder::new().with_quic(config), - Transport::WebSocket(config) => ConfigBuilder::new().with_websocket(config), - } - .with_libp2p_ping(ping_config1) - .build(); + let config1 = ConfigBuilder::new().with_libp2p_ping(ping_config1); + let config1 = add_transport(config1, transport1).build(); let (ping_config2, mut ping_event_stream2) = PingConfigBuilder::new().build(); - let config2 = match transport2 { - Transport::Tcp(config) => ConfigBuilder::new().with_tcp(config), - Transport::Quic(config) => ConfigBuilder::new().with_quic(config), - Transport::WebSocket(config) => ConfigBuilder::new().with_websocket(config), - } - .with_libp2p_ping(ping_config2) - .build(); + let config2 = ConfigBuilder::new().with_libp2p_ping(ping_config2); + let config2 = add_transport(config2, transport2).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let mut litep2p2 = Litep2p::new(config2).unwrap(); diff --git a/tests/protocol/request_response.rs b/tests/protocol/request_response.rs index 4d15e42e..39d7e046 100644 --- a/tests/protocol/request_response.rs +++ b/tests/protocol/request_response.rs @@ -25,14 +25,14 @@ use litep2p::{ Config as RequestResponseConfig, ConfigBuilder, DialOptions, RequestResponseError, RequestResponseEvent, }, - transport::{ - quic::config::Config as QuicConfig, tcp::config::Config as TcpConfig, - websocket::config::Config as WebSocketConfig, - }, + transport::tcp::config::Config as TcpConfig, types::{protocol::ProtocolName, RequestId}, Litep2p, Litep2pEvent, PeerId, }; +#[cfg(feature = "websocket")] +use litep2p::transport::websocket::config::Config as WebSocketConfig; + use futures::{channel, StreamExt}; use multiaddr::{Multiaddr, Protocol}; use multihash::Multihash; @@ -40,18 +40,16 @@ use rand::{Rng, SeedableRng}; use rand_xorshift::XorShiftRng; use tokio::time::sleep; +#[cfg(feature = "quic")] +use std::net::Ipv4Addr; use std::{ collections::{HashMap, HashSet}, - net::{Ipv4Addr, Ipv6Addr}, + net::Ipv6Addr, task::Poll, time::Duration, }; -enum Transport { - Tcp(TcpConfig), - Quic(QuicConfig), - WebSocket(WebSocketConfig), -} +use crate::common::{add_transport, Transport}; async fn connect_peers(litep2p1: &mut Litep2p, litep2p2: &mut Litep2p) { let address = litep2p2.listen_addresses().next().unwrap().clone(); @@ -100,6 +98,7 @@ async fn send_request_receive_response_tcp() { .await; } +#[cfg(feature = "quic")] #[tokio::test] async fn send_request_receive_response_quic() { send_request_receive_response( @@ -109,6 +108,7 @@ async fn send_request_receive_response_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn send_request_receive_response_websocket() { send_request_receive_response( @@ -140,12 +140,7 @@ async fn send_request_receive_response(transport1: Transport, transport2: Transp .with_keypair(Keypair::generate()) .with_request_response_protocol(req_resp_config1); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - Transport::Quic(config) => config1.with_quic(config), - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let (req_resp_config2, mut handle2) = RequestResponseConfig::new( ProtocolName::from("/protocol/1"), @@ -158,12 +153,7 @@ async fn send_request_receive_response(transport1: Transport, transport2: Transp .with_keypair(Keypair::generate()) .with_request_response_protocol(req_resp_config2); - let config2 = match transport2 { - Transport::Tcp(config) => config2.with_tcp(config), - Transport::Quic(config) => config2.with_quic(config), - Transport::WebSocket(config) => config2.with_websocket(config), - } - .build(); + let config2 = add_transport(config2, transport2).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let mut litep2p2 = Litep2p::new(config2).unwrap(); @@ -225,6 +215,7 @@ async fn reject_request_tcp() { .await; } +#[cfg(feature = "quic")] #[tokio::test] async fn reject_request_quic() { reject_request( @@ -234,6 +225,7 @@ async fn reject_request_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn reject_request_websocket() { reject_request( @@ -266,12 +258,7 @@ async fn reject_request(transport1: Transport, transport2: Transport) { .with_keypair(Keypair::generate()) .with_request_response_protocol(req_resp_config1); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - Transport::Quic(config) => config1.with_quic(config), - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let (req_resp_config2, mut handle2) = RequestResponseConfig::new( ProtocolName::from("/protocol/1"), @@ -284,12 +271,7 @@ async fn reject_request(transport1: Transport, transport2: Transport) { .with_keypair(Keypair::generate()) .with_request_response_protocol(req_resp_config2); - let config2 = match transport2 { - Transport::Tcp(config) => config2.with_tcp(config), - Transport::Quic(config) => config2.with_quic(config), - Transport::WebSocket(config) => config2.with_websocket(config), - } - .build(); + let config2 = add_transport(config2, transport2).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let mut litep2p2 = Litep2p::new(config2).unwrap(); @@ -352,6 +334,7 @@ async fn multiple_simultaneous_requests_tcp() { .await; } +#[cfg(feature = "quic")] #[tokio::test] async fn multiple_simultaneous_requests_quic() { multiple_simultaneous_requests( @@ -361,6 +344,7 @@ async fn multiple_simultaneous_requests_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn multiple_simultaneous_requests_websocket() { multiple_simultaneous_requests( @@ -393,12 +377,7 @@ async fn multiple_simultaneous_requests(transport1: Transport, transport2: Trans .with_keypair(Keypair::generate()) .with_request_response_protocol(req_resp_config1); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - Transport::Quic(config) => config1.with_quic(config), - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let (req_resp_config2, mut handle2) = RequestResponseConfig::new( ProtocolName::from("/protocol/1"), @@ -411,12 +390,7 @@ async fn multiple_simultaneous_requests(transport1: Transport, transport2: Trans .with_keypair(Keypair::generate()) .with_request_response_protocol(req_resp_config2); - let config2 = match transport2 { - Transport::Tcp(config) => config2.with_tcp(config), - Transport::Quic(config) => config2.with_quic(config), - Transport::WebSocket(config) => config2.with_websocket(config), - } - .build(); + let config2 = add_transport(config2, transport2).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let mut litep2p2 = Litep2p::new(config2).unwrap(); @@ -516,6 +490,7 @@ async fn request_timeout_tcp() { .await; } +#[cfg(feature = "quic")] #[tokio::test] async fn request_timeout_quic() { request_timeout( @@ -525,6 +500,7 @@ async fn request_timeout_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn request_timeout_websocket() { request_timeout( @@ -557,12 +533,7 @@ async fn request_timeout(transport1: Transport, transport2: Transport) { .with_keypair(Keypair::generate()) .with_request_response_protocol(req_resp_config1); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - Transport::Quic(config) => config1.with_quic(config), - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let (req_resp_config2, _handle2) = RequestResponseConfig::new( ProtocolName::from("/protocol/1"), @@ -575,12 +546,7 @@ async fn request_timeout(transport1: Transport, transport2: Transport) { .with_keypair(Keypair::generate()) .with_request_response_protocol(req_resp_config2); - let config2 = match transport2 { - Transport::Tcp(config) => config2.with_tcp(config), - Transport::Quic(config) => config2.with_quic(config), - Transport::WebSocket(config) => config2.with_websocket(config), - } - .build(); + let config2 = add_transport(config2, transport2).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let mut litep2p2 = Litep2p::new(config2).unwrap(); @@ -632,6 +598,7 @@ async fn protocol_not_supported_tcp() { .await; } +#[cfg(feature = "quic")] #[tokio::test] async fn protocol_not_supported_quic() { protocol_not_supported( @@ -641,6 +608,7 @@ async fn protocol_not_supported_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn protocol_not_supported_websocket() { protocol_not_supported( @@ -673,12 +641,7 @@ async fn protocol_not_supported(transport1: Transport, transport2: Transport) { .with_keypair(Keypair::generate()) .with_request_response_protocol(req_resp_config1); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - Transport::Quic(config) => config1.with_quic(config), - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let (req_resp_config2, _handle2) = RequestResponseConfig::new( ProtocolName::from("/protocol/2"), @@ -691,12 +654,7 @@ async fn protocol_not_supported(transport1: Transport, transport2: Transport) { .with_keypair(Keypair::generate()) .with_request_response_protocol(req_resp_config2); - let config2 = match transport2 { - Transport::Tcp(config) => config2.with_tcp(config), - Transport::Quic(config) => config2.with_quic(config), - Transport::WebSocket(config) => config2.with_websocket(config), - } - .build(); + let config2 = add_transport(config2, transport2).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let mut litep2p2 = Litep2p::new(config2).unwrap(); @@ -746,6 +704,7 @@ async fn connection_close_while_request_is_pending_tcp() { .await; } +#[cfg(feature = "quic")] #[tokio::test] async fn connection_close_while_request_is_pending_quic() { connection_close_while_request_is_pending( @@ -755,6 +714,7 @@ async fn connection_close_while_request_is_pending_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn connection_close_while_request_is_pending_websocket() { connection_close_while_request_is_pending( @@ -786,12 +746,7 @@ async fn connection_close_while_request_is_pending(transport1: Transport, transp .with_keypair(Keypair::generate()) .with_request_response_protocol(req_resp_config1); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - Transport::Quic(config) => config1.with_quic(config), - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let (req_resp_config2, handle2) = RequestResponseConfig::new( ProtocolName::from("/protocol/1"), @@ -804,12 +759,7 @@ async fn connection_close_while_request_is_pending(transport1: Transport, transp .with_keypair(Keypair::generate()) .with_request_response_protocol(req_resp_config2); - let config2 = match transport2 { - Transport::Tcp(config) => config2.with_tcp(config), - Transport::Quic(config) => config2.with_quic(config), - Transport::WebSocket(config) => config2.with_websocket(config), - } - .build(); + let config2 = add_transport(config2, transport2).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let mut litep2p2 = Litep2p::new(config2).unwrap(); @@ -859,6 +809,7 @@ async fn request_too_big_tcp() { .await; } +#[cfg(feature = "quic")] #[tokio::test] async fn request_too_big_quic() { request_too_big( @@ -868,6 +819,7 @@ async fn request_too_big_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn request_too_big_websocket() { request_too_big( @@ -899,12 +851,7 @@ async fn request_too_big(transport1: Transport, transport2: Transport) { .with_keypair(Keypair::generate()) .with_request_response_protocol(req_resp_config1); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - Transport::Quic(config) => config1.with_quic(config), - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let (req_resp_config2, _handle2) = RequestResponseConfig::new( ProtocolName::from("/protocol/1"), @@ -917,12 +864,7 @@ async fn request_too_big(transport1: Transport, transport2: Transport) { .with_keypair(Keypair::generate()) .with_request_response_protocol(req_resp_config2); - let config2 = match transport2 { - Transport::Tcp(config) => config2.with_tcp(config), - Transport::Quic(config) => config2.with_quic(config), - Transport::WebSocket(config) => config2.with_websocket(config), - } - .build(); + let config2 = add_transport(config2, transport2).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let mut litep2p2 = Litep2p::new(config2).unwrap(); @@ -968,6 +910,7 @@ async fn response_too_big_tcp() { .await; } +#[cfg(feature = "quic")] #[tokio::test] async fn response_too_big_quic() { response_too_big( @@ -977,6 +920,7 @@ async fn response_too_big_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn response_too_big_websocket() { response_too_big( @@ -1008,12 +952,7 @@ async fn response_too_big(transport1: Transport, transport2: Transport) { .with_keypair(Keypair::generate()) .with_request_response_protocol(req_resp_config1); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - Transport::Quic(config) => config1.with_quic(config), - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let (req_resp_config2, mut handle2) = RequestResponseConfig::new( ProtocolName::from("/protocol/1"), @@ -1026,12 +965,7 @@ async fn response_too_big(transport1: Transport, transport2: Transport) { .with_keypair(Keypair::generate()) .with_request_response_protocol(req_resp_config2); - let config2 = match transport2 { - Transport::Tcp(config) => config2.with_tcp(config), - Transport::Quic(config) => config2.with_quic(config), - Transport::WebSocket(config) => config2.with_websocket(config), - } - .build(); + let config2 = add_transport(config2, transport2).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let mut litep2p2 = Litep2p::new(config2).unwrap(); @@ -1206,6 +1140,7 @@ async fn dialer_fallback_protocol_works_tcp() { .await; } +#[cfg(feature = "quic")] #[tokio::test] async fn dialer_fallback_protocol_works_quic() { dialer_fallback_protocol_works( @@ -1215,6 +1150,7 @@ async fn dialer_fallback_protocol_works_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn dialer_fallback_protocol_works_websocket() { dialer_fallback_protocol_works( @@ -1245,12 +1181,7 @@ async fn dialer_fallback_protocol_works(transport1: Transport, transport2: Trans .with_keypair(Keypair::generate()) .with_request_response_protocol(req_resp_config1); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - Transport::Quic(config) => config1.with_quic(config), - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let (req_resp_config2, mut handle2) = RequestResponseConfig::new( ProtocolName::from("/protocol/1"), @@ -1263,12 +1194,7 @@ async fn dialer_fallback_protocol_works(transport1: Transport, transport2: Trans .with_keypair(Keypair::generate()) .with_request_response_protocol(req_resp_config2); - let config2 = match transport2 { - Transport::Tcp(config) => config2.with_tcp(config), - Transport::Quic(config) => config2.with_quic(config), - Transport::WebSocket(config) => config2.with_websocket(config), - } - .build(); + let config2 = add_transport(config2, transport2).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let mut litep2p2 = Litep2p::new(config2).unwrap(); @@ -1330,6 +1256,7 @@ async fn listener_fallback_protocol_works_tcp() { .await; } +#[cfg(feature = "quic")] #[tokio::test] async fn listener_fallback_protocol_works_quic() { listener_fallback_protocol_works( @@ -1339,6 +1266,7 @@ async fn listener_fallback_protocol_works_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn listener_fallback_protocol_works_websocket() { listener_fallback_protocol_works( @@ -1370,12 +1298,7 @@ async fn listener_fallback_protocol_works(transport1: Transport, transport2: Tra .with_keypair(Keypair::generate()) .with_request_response_protocol(req_resp_config1); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - Transport::Quic(config) => config1.with_quic(config), - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let (req_resp_config2, mut handle2) = RequestResponseConfig::new( ProtocolName::from("/protocol/1/improved"), @@ -1388,12 +1311,7 @@ async fn listener_fallback_protocol_works(transport1: Transport, transport2: Tra .with_keypair(Keypair::generate()) .with_request_response_protocol(req_resp_config2); - let config2 = match transport2 { - Transport::Tcp(config) => config2.with_tcp(config), - Transport::Quic(config) => config2.with_quic(config), - Transport::WebSocket(config) => config2.with_websocket(config), - } - .build(); + let config2 = add_transport(config2, transport2).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let mut litep2p2 = Litep2p::new(config2).unwrap(); @@ -1455,6 +1373,7 @@ async fn dial_peer_when_sending_request_tcp() { .await; } +#[cfg(feature = "quic")] #[tokio::test] async fn dial_peer_when_sending_request_quic() { dial_peer_when_sending_request( @@ -1464,6 +1383,7 @@ async fn dial_peer_when_sending_request_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn dial_peer_when_sending_request_websocket() { dial_peer_when_sending_request( @@ -1495,12 +1415,7 @@ async fn dial_peer_when_sending_request(transport1: Transport, transport2: Trans .with_keypair(Keypair::generate()) .with_request_response_protocol(req_resp_config1); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - Transport::Quic(config) => config1.with_quic(config), - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let (req_resp_config2, mut handle2) = RequestResponseConfig::new( ProtocolName::from("/protocol/1/improved"), @@ -1513,12 +1428,7 @@ async fn dial_peer_when_sending_request(transport1: Transport, transport2: Trans .with_keypair(Keypair::generate()) .with_request_response_protocol(req_resp_config2); - let config2 = match transport2 { - Transport::Tcp(config) => config2.with_tcp(config), - Transport::Quic(config) => config2.with_quic(config), - Transport::WebSocket(config) => config2.with_websocket(config), - } - .build(); + let config2 = add_transport(config2, transport2).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let mut litep2p2 = Litep2p::new(config2).unwrap(); @@ -1580,6 +1490,7 @@ async fn dial_peer_but_no_known_address_tcp() { .await; } +#[cfg(feature = "quic")] #[tokio::test] async fn dial_peer_but_no_known_address_quic() { dial_peer_but_no_known_address( @@ -1589,6 +1500,7 @@ async fn dial_peer_but_no_known_address_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn dial_peer_but_no_known_address_websocket() { dial_peer_but_no_known_address( @@ -1620,12 +1532,7 @@ async fn dial_peer_but_no_known_address(transport1: Transport, transport2: Trans .with_keypair(Keypair::generate()) .with_request_response_protocol(req_resp_config1); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - Transport::Quic(config) => config1.with_quic(config), - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let (req_resp_config2, _handle2) = RequestResponseConfig::new( ProtocolName::from("/protocol/1/improved"), @@ -1638,12 +1545,7 @@ async fn dial_peer_but_no_known_address(transport1: Transport, transport2: Trans .with_keypair(Keypair::generate()) .with_request_response_protocol(req_resp_config2); - let config2 = match transport2 { - Transport::Tcp(config) => config2.with_tcp(config), - Transport::Quic(config) => config2.with_quic(config), - Transport::WebSocket(config) => config2.with_websocket(config), - } - .build(); + let config2 = add_transport(config2, transport2).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let mut litep2p2 = Litep2p::new(config2).unwrap(); @@ -1687,6 +1589,7 @@ async fn cancel_request_tcp() { .await; } +#[cfg(feature = "quic")] #[tokio::test] async fn cancel_request_quic() { cancel_request( @@ -1696,6 +1599,7 @@ async fn cancel_request_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn cancel_request_websocket() { cancel_request( @@ -1727,12 +1631,7 @@ async fn cancel_request(transport1: Transport, transport2: Transport) { .with_keypair(Keypair::generate()) .with_request_response_protocol(req_resp_config1); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - Transport::Quic(config) => config1.with_quic(config), - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let (req_resp_config2, mut handle2) = RequestResponseConfig::new( ProtocolName::from("/protocol/1"), @@ -1745,12 +1644,7 @@ async fn cancel_request(transport1: Transport, transport2: Transport) { .with_keypair(Keypair::generate()) .with_request_response_protocol(req_resp_config2); - let config2 = match transport2 { - Transport::Tcp(config) => config2.with_tcp(config), - Transport::Quic(config) => config2.with_quic(config), - Transport::WebSocket(config) => config2.with_websocket(config), - } - .build(); + let config2 = add_transport(config2, transport2).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let mut litep2p2 = Litep2p::new(config2).unwrap(); @@ -1812,6 +1706,7 @@ async fn substream_open_failure_reported_once_tcp() { .await; } +#[cfg(feature = "quic")] #[tokio::test] async fn substream_open_failure_reported_once_quic() { substream_open_failure_reported_once( @@ -1821,6 +1716,7 @@ async fn substream_open_failure_reported_once_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn substream_open_failure_reported_once_websocket() { substream_open_failure_reported_once( @@ -1852,12 +1748,7 @@ async fn substream_open_failure_reported_once(transport1: Transport, transport2: .with_keypair(Keypair::generate()) .with_request_response_protocol(req_resp_config1); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - Transport::Quic(config) => config1.with_quic(config), - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let (req_resp_config2, _handle2) = RequestResponseConfig::new( ProtocolName::from("/protocol/2"), @@ -1870,12 +1761,7 @@ async fn substream_open_failure_reported_once(transport1: Transport, transport2: .with_keypair(Keypair::generate()) .with_request_response_protocol(req_resp_config2); - let config2 = match transport2 { - Transport::Tcp(config) => config2.with_tcp(config), - Transport::Quic(config) => config2.with_quic(config), - Transport::WebSocket(config) => config2.with_websocket(config), - } - .build(); + let config2 = add_transport(config2, transport2).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let mut litep2p2 = Litep2p::new(config2).unwrap(); @@ -1937,6 +1823,7 @@ async fn excess_inbound_request_rejected_tcp() { .await; } +#[cfg(feature = "quic")] #[tokio::test] async fn excess_inbound_request_rejected_quic() { excess_inbound_request_rejected( @@ -1946,6 +1833,7 @@ async fn excess_inbound_request_rejected_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn excess_inbound_request_rejected_websocket() { excess_inbound_request_rejected( @@ -1974,12 +1862,7 @@ async fn excess_inbound_request_rejected(transport1: Transport, transport2: Tran .with_keypair(Keypair::generate()) .with_request_response_protocol(req_resp_config1); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - Transport::Quic(config) => config1.with_quic(config), - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let (req_resp_config2, _handle2) = ConfigBuilder::new(ProtocolName::from("/protocol/1")) .with_max_size(1024) @@ -1990,12 +1873,7 @@ async fn excess_inbound_request_rejected(transport1: Transport, transport2: Tran .with_keypair(Keypair::generate()) .with_request_response_protocol(req_resp_config2); - let config2 = match transport2 { - Transport::Tcp(config) => config2.with_tcp(config), - Transport::Quic(config) => config2.with_quic(config), - Transport::WebSocket(config) => config2.with_websocket(config), - } - .build(); + let config2 = add_transport(config2, transport2).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let mut litep2p2 = Litep2p::new(config2).unwrap(); @@ -2058,6 +1936,7 @@ async fn feedback_received_for_succesful_response_tcp() { .await; } +#[cfg(feature = "quic")] #[tokio::test] async fn feedback_received_for_succesful_response_quic() { feedback_received_for_succesful_response( @@ -2067,6 +1946,7 @@ async fn feedback_received_for_succesful_response_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn feedback_received_for_succesful_response_websocket() { feedback_received_for_succesful_response( @@ -2095,12 +1975,7 @@ async fn feedback_received_for_succesful_response(transport1: Transport, transpo .with_keypair(Keypair::generate()) .with_request_response_protocol(req_resp_config1); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - Transport::Quic(config) => config1.with_quic(config), - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let (req_resp_config2, mut handle2) = ConfigBuilder::new(ProtocolName::from("/protocol/1")) .with_max_size(1024) @@ -2110,12 +1985,7 @@ async fn feedback_received_for_succesful_response(transport1: Transport, transpo .with_keypair(Keypair::generate()) .with_request_response_protocol(req_resp_config2); - let config2 = match transport2 { - Transport::Tcp(config) => config2.with_tcp(config), - Transport::Quic(config) => config2.with_quic(config), - Transport::WebSocket(config) => config2.with_websocket(config), - } - .build(); + let config2 = add_transport(config2, transport2).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let mut litep2p2 = Litep2p::new(config2).unwrap(); @@ -2179,6 +2049,7 @@ async fn feedback_received_for_succesful_response(transport1: Transport, transpo // .await; // } +#[cfg(feature = "quic")] #[tokio::test] async fn feedback_not_received_for_failed_response_quic() { feedback_not_received_for_failed_response( @@ -2203,6 +2074,7 @@ async fn feedback_not_received_for_failed_response_quic() { // .await; // } +#[cfg(feature = "quic")] async fn feedback_not_received_for_failed_response(transport1: Transport, transport2: Transport) { let _ = tracing_subscriber::fmt() .with_env_filter(tracing_subscriber::EnvFilter::from_default_env()) @@ -2216,12 +2088,7 @@ async fn feedback_not_received_for_failed_response(transport1: Transport, transp .with_keypair(Keypair::generate()) .with_request_response_protocol(req_resp_config1); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - Transport::Quic(config) => config1.with_quic(config), - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let (req_resp_config2, mut handle2) = ConfigBuilder::new(ProtocolName::from("/protocol/1")) .with_max_size(1024) @@ -2231,12 +2098,7 @@ async fn feedback_not_received_for_failed_response(transport1: Transport, transp .with_keypair(Keypair::generate()) .with_request_response_protocol(req_resp_config2); - let config2 = match transport2 { - Transport::Tcp(config) => config2.with_tcp(config), - Transport::Quic(config) => config2.with_quic(config), - Transport::WebSocket(config) => config2.with_websocket(config), - } - .build(); + let config2 = add_transport(config2, transport2).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let mut litep2p2 = Litep2p::new(config2).unwrap(); @@ -2289,6 +2151,7 @@ async fn custom_timeout_tcp() { .await; } +#[cfg(feature = "quic")] #[tokio::test] async fn custom_timeout_quic() { custom_timeout( @@ -2298,6 +2161,7 @@ async fn custom_timeout_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn custom_timeout_websocket() { custom_timeout( @@ -2321,12 +2185,7 @@ async fn custom_timeout(transport1: Transport, transport2: Transport) { .with_keypair(Keypair::generate()) .with_request_response_protocol(req_resp_config1); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - Transport::Quic(config) => config1.with_quic(config), - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let (req_resp_config2, _handle2) = ConfigBuilder::new(ProtocolName::from("/protocol/1")) .with_max_size(1024) @@ -2336,12 +2195,7 @@ async fn custom_timeout(transport1: Transport, transport2: Transport) { .with_keypair(Keypair::generate()) .with_request_response_protocol(req_resp_config2); - let config2 = match transport2 { - Transport::Tcp(config) => config2.with_tcp(config), - Transport::Quic(config) => config2.with_quic(config), - Transport::WebSocket(config) => config2.with_websocket(config), - } - .build(); + let config2 = add_transport(config2, transport2).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let mut litep2p2 = Litep2p::new(config2).unwrap(); @@ -2383,11 +2237,13 @@ async fn outbound_request_for_unconnected_peer_tcp() { outbound_request_for_unconnected_peer(Transport::Tcp(Default::default())).await; } +#[cfg(feature = "quic")] #[tokio::test] async fn outbound_request_for_unconnected_peer_quic() { outbound_request_for_unconnected_peer(Transport::Quic(Default::default())).await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn outbound_request_for_unconnected_peer_websocket() { outbound_request_for_unconnected_peer(Transport::WebSocket(Default::default())).await; @@ -2406,12 +2262,7 @@ async fn outbound_request_for_unconnected_peer(transport1: Transport) { .with_keypair(Keypair::generate()) .with_request_response_protocol(req_resp_config1); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - Transport::Quic(config) => config1.with_quic(config), - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); tokio::spawn(async move { let mut litep2p1 = Litep2p::new(config1).unwrap(); @@ -2440,11 +2291,13 @@ async fn dial_failure_tcp() { dial_failure(Transport::Tcp(Default::default())).await; } +#[cfg(feature = "quic")] #[tokio::test] async fn dial_failure_quic() { dial_failure(Transport::Quic(Default::default())).await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn dial_failure_websocket() { dial_failure(Transport::WebSocket(Default::default())).await; @@ -2469,11 +2322,13 @@ async fn dial_failure(transport: Transport) { .with(Protocol::Ip6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1))) .with(Protocol::Tcp(5)) .with(Protocol::P2p(Multihash::from(peer))), + #[cfg(feature = "quic")] Transport::Quic(_) => Multiaddr::empty() .with(Protocol::Ip4(Ipv4Addr::new(127, 0, 0, 1))) .with(Protocol::Udp(5)) .with(Protocol::QuicV1) .with(Protocol::P2p(Multihash::from(peer))), + #[cfg(feature = "websocket")] Transport::WebSocket(_) => Multiaddr::empty() .with(Protocol::Ip6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1))) .with(Protocol::Tcp(5)) @@ -2481,12 +2336,7 @@ async fn dial_failure(transport: Transport) { .with(Protocol::P2p(Multihash::from(peer))), }; - let config = match transport { - Transport::Tcp(config) => litep2p_config.with_tcp(config), - Transport::Quic(config) => litep2p_config.with_quic(config), - Transport::WebSocket(config) => litep2p_config.with_websocket(config), - } - .build(); + let config = add_transport(litep2p_config, transport).build(); let mut litep2p = Litep2p::new(config).unwrap(); litep2p.add_known_address(peer, vec![known_address].into_iter()); @@ -2514,6 +2364,7 @@ async fn large_response_tcp() { .await; } +#[cfg(feature = "quic")] #[tokio::test] async fn large_response_quic() { large_response( @@ -2523,6 +2374,7 @@ async fn large_response_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn large_response_websocket() { large_response( @@ -2546,12 +2398,7 @@ async fn large_response(transport1: Transport, transport2: Transport) { .with_keypair(Keypair::generate()) .with_request_response_protocol(req_resp_config1); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - Transport::Quic(config) => config1.with_quic(config), - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let (req_resp_config2, mut handle2) = ConfigBuilder::new(ProtocolName::from("/protocol/1")) .with_max_size(16 * 1024 * 1024) @@ -2561,12 +2408,7 @@ async fn large_response(transport1: Transport, transport2: Transport) { .with_keypair(Keypair::generate()) .with_request_response_protocol(req_resp_config2); - let config2 = match transport2 { - Transport::Tcp(config) => config2.with_tcp(config), - Transport::Quic(config) => config2.with_quic(config), - Transport::WebSocket(config) => config2.with_websocket(config), - } - .build(); + let config2 = add_transport(config2, transport2).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let mut litep2p2 = Litep2p::new(config2).unwrap(); @@ -2624,6 +2466,7 @@ async fn binary_incompatible_fallback_tcp() { .await; } +#[cfg(feature = "quic")] #[tokio::test] async fn binary_incompatible_fallback_quic() { binary_incompatible_fallback( @@ -2633,6 +2476,7 @@ async fn binary_incompatible_fallback_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn binary_incompatible_fallback_websocket() { binary_incompatible_fallback( @@ -2657,12 +2501,7 @@ async fn binary_incompatible_fallback(transport1: Transport, transport2: Transpo .with_keypair(Keypair::generate()) .with_request_response_protocol(req_resp_config1); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - Transport::Quic(config) => config1.with_quic(config), - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let (req_resp_config2, mut handle2) = ConfigBuilder::new(ProtocolName::from("/protocol/1")) .with_max_size(16 * 1024 * 1024) @@ -2672,12 +2511,7 @@ async fn binary_incompatible_fallback(transport1: Transport, transport2: Transpo .with_keypair(Keypair::generate()) .with_request_response_protocol(req_resp_config2); - let config2 = match transport2 { - Transport::Tcp(config) => config2.with_tcp(config), - Transport::Quic(config) => config2.with_quic(config), - Transport::WebSocket(config) => config2.with_websocket(config), - } - .build(); + let config2 = add_transport(config2, transport2).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let mut litep2p2 = Litep2p::new(config2).unwrap(); @@ -2737,6 +2571,7 @@ async fn binary_incompatible_fallback_inbound_request_tcp() { .await; } +#[cfg(feature = "quic")] #[tokio::test] async fn binary_incompatible_fallback_inbound_request_quic() { binary_incompatible_fallback_inbound_request( @@ -2746,6 +2581,7 @@ async fn binary_incompatible_fallback_inbound_request_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn binary_incompatible_fallback_inbound_request_websocket() { binary_incompatible_fallback_inbound_request( @@ -2773,12 +2609,7 @@ async fn binary_incompatible_fallback_inbound_request( .with_keypair(Keypair::generate()) .with_request_response_protocol(req_resp_config1); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - Transport::Quic(config) => config1.with_quic(config), - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let (req_resp_config2, mut handle2) = ConfigBuilder::new(ProtocolName::from("/protocol/1")) .with_max_size(16 * 1024 * 1024) @@ -2788,12 +2619,7 @@ async fn binary_incompatible_fallback_inbound_request( .with_keypair(Keypair::generate()) .with_request_response_protocol(req_resp_config2); - let config2 = match transport2 { - Transport::Tcp(config) => config2.with_tcp(config), - Transport::Quic(config) => config2.with_quic(config), - Transport::WebSocket(config) => config2.with_websocket(config), - } - .build(); + let config2 = add_transport(config2, transport2).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let mut litep2p2 = Litep2p::new(config2).unwrap(); @@ -2848,6 +2674,7 @@ async fn binary_incompatible_fallback_two_fallback_protocols_tcp() { .await; } +#[cfg(feature = "quic")] #[tokio::test] async fn binary_incompatible_fallback_two_fallback_protocols_quic() { binary_incompatible_fallback_two_fallback_protocols( @@ -2857,6 +2684,7 @@ async fn binary_incompatible_fallback_two_fallback_protocols_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn binary_incompatible_fallback_two_fallback_protocols_websocket() { binary_incompatible_fallback_two_fallback_protocols( @@ -2888,12 +2716,7 @@ async fn binary_incompatible_fallback_two_fallback_protocols( .with_keypair(Keypair::generate()) .with_request_response_protocol(req_resp_config1); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - Transport::Quic(config) => config1.with_quic(config), - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let (req_resp_config2, mut handle2) = ConfigBuilder::new(ProtocolName::from("/genesis/protocol/1")) @@ -2905,12 +2728,7 @@ async fn binary_incompatible_fallback_two_fallback_protocols( .with_keypair(Keypair::generate()) .with_request_response_protocol(req_resp_config2); - let config2 = match transport2 { - Transport::Tcp(config) => config2.with_tcp(config), - Transport::Quic(config) => config2.with_quic(config), - Transport::WebSocket(config) => config2.with_websocket(config), - } - .build(); + let config2 = add_transport(config2, transport2).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let mut litep2p2 = Litep2p::new(config2).unwrap(); @@ -2970,6 +2788,7 @@ async fn binary_incompatible_fallback_two_fallback_protocols_inbound_request_tcp .await; } +#[cfg(feature = "quic")] #[tokio::test] async fn binary_incompatible_fallback_two_fallback_protocols_inbound_request_quic() { binary_incompatible_fallback_two_fallback_protocols_inbound_request( @@ -2979,6 +2798,7 @@ async fn binary_incompatible_fallback_two_fallback_protocols_inbound_request_qui .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn binary_incompatible_fallback_two_fallback_protocols_inbound_request_websocket() { binary_incompatible_fallback_two_fallback_protocols_inbound_request( @@ -3010,12 +2830,7 @@ async fn binary_incompatible_fallback_two_fallback_protocols_inbound_request( .with_keypair(Keypair::generate()) .with_request_response_protocol(req_resp_config1); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - Transport::Quic(config) => config1.with_quic(config), - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let (req_resp_config2, mut handle2) = ConfigBuilder::new(ProtocolName::from("/genesis/protocol/1")) @@ -3027,12 +2842,7 @@ async fn binary_incompatible_fallback_two_fallback_protocols_inbound_request( .with_keypair(Keypair::generate()) .with_request_response_protocol(req_resp_config2); - let config2 = match transport2 { - Transport::Tcp(config) => config2.with_tcp(config), - Transport::Quic(config) => config2.with_quic(config), - Transport::WebSocket(config) => config2.with_websocket(config), - } - .build(); + let config2 = add_transport(config2, transport2).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let mut litep2p2 = Litep2p::new(config2).unwrap(); @@ -3087,6 +2897,7 @@ async fn binary_incompatible_fallback_compatible_nodes_tcp() { .await; } +#[cfg(feature = "quic")] #[tokio::test] async fn binary_incompatible_fallback_compatible_nodes_quic() { binary_incompatible_fallback_compatible_nodes( @@ -3096,6 +2907,7 @@ async fn binary_incompatible_fallback_compatible_nodes_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn binary_incompatible_fallback_compatible_nodes_websocket() { binary_incompatible_fallback_compatible_nodes( @@ -3127,12 +2939,7 @@ async fn binary_incompatible_fallback_compatible_nodes( .with_keypair(Keypair::generate()) .with_request_response_protocol(req_resp_config1); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - Transport::Quic(config) => config1.with_quic(config), - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let (req_resp_config2, mut handle2) = ConfigBuilder::new(ProtocolName::from("/genesis/protocol/2")) @@ -3148,12 +2955,7 @@ async fn binary_incompatible_fallback_compatible_nodes( .with_keypair(Keypair::generate()) .with_request_response_protocol(req_resp_config2); - let config2 = match transport2 { - Transport::Tcp(config) => config2.with_tcp(config), - Transport::Quic(config) => config2.with_quic(config), - Transport::WebSocket(config) => config2.with_websocket(config), - } - .build(); + let config2 = add_transport(config2, transport2).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let mut litep2p2 = Litep2p::new(config2).unwrap(); diff --git a/tests/substream.rs b/tests/substream.rs index c4b50f6c..efcf5011 100644 --- a/tests/substream.rs +++ b/tests/substream.rs @@ -23,14 +23,16 @@ use litep2p::{ config::ConfigBuilder, protocol::{Direction, TransportEvent, TransportService, UserProtocol}, substream::{Substream, SubstreamSet}, - transport::{ - quic::config::Config as QuicConfig, tcp::config::Config as TcpConfig, - websocket::config::Config as WebSocketConfig, - }, + transport::tcp::config::Config as TcpConfig, types::{protocol::ProtocolName, SubstreamId}, Error, Litep2p, Litep2pEvent, PeerId, }; +#[cfg(feature = "quic")] +use litep2p::transport::quic::config::Config as QuicConfig; +#[cfg(feature = "websocket")] +use litep2p::transport::websocket::config::Config as WebSocketConfig; + use bytes::Bytes; use futures::{Sink, SinkExt, StreamExt}; use tokio::{ @@ -50,7 +52,9 @@ use std::{ enum Transport { Tcp(TcpConfig), + #[cfg(feature = "quic")] Quic(QuicConfig), + #[cfg(feature = "websocket")] WebSocket(WebSocketConfig), } @@ -243,6 +247,7 @@ async fn too_big_identity_payload_framed_tcp() { .await; } +#[cfg(feature = "quic")] #[tokio::test] async fn too_big_identity_payload_framed_quic() { too_big_identity_payload_framed( @@ -252,6 +257,7 @@ async fn too_big_identity_payload_framed_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn too_big_identity_payload_framed_websocket() { too_big_identity_payload_framed( @@ -270,7 +276,9 @@ async fn too_big_identity_payload_framed(transport1: Transport, transport2: Tran let (custom_protocol1, tx1) = CustomProtocol::new(ProtocolCodec::Identity(10usize)); let config1 = match transport1 { Transport::Tcp(config) => ConfigBuilder::new().with_tcp(config), + #[cfg(feature = "quic")] Transport::Quic(config) => ConfigBuilder::new().with_quic(config), + #[cfg(feature = "websocket")] Transport::WebSocket(config) => ConfigBuilder::new().with_websocket(config), } .with_user_protocol(Box::new(custom_protocol1)) @@ -279,7 +287,9 @@ async fn too_big_identity_payload_framed(transport1: Transport, transport2: Tran let (custom_protocol2, _tx2) = CustomProtocol::new(ProtocolCodec::Identity(10usize)); let config2 = match transport2 { Transport::Tcp(config) => ConfigBuilder::new().with_tcp(config), + #[cfg(feature = "quic")] Transport::Quic(config) => ConfigBuilder::new().with_quic(config), + #[cfg(feature = "websocket")] Transport::WebSocket(config) => ConfigBuilder::new().with_websocket(config), } .with_user_protocol(Box::new(custom_protocol2)) @@ -328,6 +338,7 @@ async fn too_big_identity_payload_sink_tcp() { .await; } +#[cfg(feature = "quic")] #[tokio::test] async fn too_big_identity_payload_sink_quic() { too_big_identity_payload_sink( @@ -337,6 +348,7 @@ async fn too_big_identity_payload_sink_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn too_big_identity_payload_sink_websocket() { too_big_identity_payload_sink( @@ -355,7 +367,9 @@ async fn too_big_identity_payload_sink(transport1: Transport, transport2: Transp let (custom_protocol1, tx1) = CustomProtocol::new(ProtocolCodec::Identity(10usize)); let config1 = match transport1 { Transport::Tcp(config) => ConfigBuilder::new().with_tcp(config), + #[cfg(feature = "quic")] Transport::Quic(config) => ConfigBuilder::new().with_quic(config), + #[cfg(feature = "websocket")] Transport::WebSocket(config) => ConfigBuilder::new().with_websocket(config), } .with_user_protocol(Box::new(custom_protocol1)) @@ -364,7 +378,9 @@ async fn too_big_identity_payload_sink(transport1: Transport, transport2: Transp let (custom_protocol2, _tx2) = CustomProtocol::new(ProtocolCodec::Identity(10usize)); let config2 = match transport2 { Transport::Tcp(config) => ConfigBuilder::new().with_tcp(config), + #[cfg(feature = "quic")] Transport::Quic(config) => ConfigBuilder::new().with_quic(config), + #[cfg(feature = "websocket")] Transport::WebSocket(config) => ConfigBuilder::new().with_websocket(config), } .with_user_protocol(Box::new(custom_protocol2)) @@ -415,6 +431,7 @@ async fn correct_payload_size_sink_tcp() { .await; } +#[cfg(feature = "quic")] #[tokio::test] async fn correct_payload_size_sink_quic() { correct_payload_size_sink( @@ -424,6 +441,7 @@ async fn correct_payload_size_sink_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn correct_payload_size_sink_websocket() { correct_payload_size_sink( @@ -442,7 +460,9 @@ async fn correct_payload_size_sink(transport1: Transport, transport2: Transport) let (custom_protocol1, tx1) = CustomProtocol::new(ProtocolCodec::Identity(10usize)); let config1 = match transport1 { Transport::Tcp(config) => ConfigBuilder::new().with_tcp(config), + #[cfg(feature = "quic")] Transport::Quic(config) => ConfigBuilder::new().with_quic(config), + #[cfg(feature = "websocket")] Transport::WebSocket(config) => ConfigBuilder::new().with_websocket(config), } .with_user_protocol(Box::new(custom_protocol1)) @@ -451,7 +471,9 @@ async fn correct_payload_size_sink(transport1: Transport, transport2: Transport) let (custom_protocol2, _tx2) = CustomProtocol::new(ProtocolCodec::Identity(10usize)); let config2 = match transport2 { Transport::Tcp(config) => ConfigBuilder::new().with_tcp(config), + #[cfg(feature = "quic")] Transport::Quic(config) => ConfigBuilder::new().with_quic(config), + #[cfg(feature = "websocket")] Transport::WebSocket(config) => ConfigBuilder::new().with_websocket(config), } .with_user_protocol(Box::new(custom_protocol2)) @@ -499,6 +521,7 @@ async fn correct_payload_size_async_write_tcp() { .await; } +#[cfg(feature = "quic")] #[tokio::test] async fn correct_payload_size_async_write_quic() { correct_payload_size_async_write( @@ -508,6 +531,7 @@ async fn correct_payload_size_async_write_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn correct_payload_size_async_write_websocket() { correct_payload_size_async_write( @@ -526,7 +550,9 @@ async fn correct_payload_size_async_write(transport1: Transport, transport2: Tra let (custom_protocol1, tx1) = CustomProtocol::new(ProtocolCodec::Identity(10usize)); let config1 = match transport1 { Transport::Tcp(config) => ConfigBuilder::new().with_tcp(config), + #[cfg(feature = "quic")] Transport::Quic(config) => ConfigBuilder::new().with_quic(config), + #[cfg(feature = "websocket")] Transport::WebSocket(config) => ConfigBuilder::new().with_websocket(config), } .with_user_protocol(Box::new(custom_protocol1)) @@ -535,7 +561,9 @@ async fn correct_payload_size_async_write(transport1: Transport, transport2: Tra let (custom_protocol2, _tx2) = CustomProtocol::new(ProtocolCodec::Identity(10usize)); let config2 = match transport2 { Transport::Tcp(config) => ConfigBuilder::new().with_tcp(config), + #[cfg(feature = "quic")] Transport::Quic(config) => ConfigBuilder::new().with_quic(config), + #[cfg(feature = "websocket")] Transport::WebSocket(config) => ConfigBuilder::new().with_websocket(config), } .with_user_protocol(Box::new(custom_protocol2)) diff --git a/tests/webrtc.rs b/tests/webrtc.rs index d80c6cb2..fd0651f8 100644 --- a/tests/webrtc.rs +++ b/tests/webrtc.rs @@ -18,6 +18,8 @@ // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. +#![cfg(feature = "webrtc")] + use futures::StreamExt; use litep2p::{ config::ConfigBuilder as Litep2pConfigBuilder,