network_transports.rs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2026 Dyne.org foundation
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as
  7. * published by the Free Software Foundation, either version 3 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. use std::sync::Arc;
  19. use async_trait::async_trait;
  20. use darkfi_serial::{AsyncDecodable, AsyncEncodable};
  21. use smol::{io, LocalExecutor};
  22. use url::Url;
  23. use darkfi::net::{
  24. channel::Channel,
  25. session::{Session, SessionBitFlag, SESSION_OUTBOUND},
  26. transport::{Dialer, Listener},
  27. P2pPtr,
  28. };
  29. struct TestSession;
  30. #[async_trait]
  31. impl Session for TestSession {
  32. fn p2p(&self) -> P2pPtr {
  33. unreachable!("channel address tests do not access P2P state")
  34. }
  35. fn type_id(&self) -> SessionBitFlag {
  36. SESSION_OUTBOUND
  37. }
  38. async fn reload(self: Arc<Self>) {}
  39. }
  40. #[test]
  41. fn tcp_transport() {
  42. let executor = LocalExecutor::new();
  43. smol::block_on(executor.run(async {
  44. let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
  45. let port = listener.local_addr().unwrap().port();
  46. drop(listener);
  47. let url = Url::parse(&format!("tcp://127.0.0.1:{port}")).unwrap();
  48. let listener =
  49. Listener::new(url.clone(), None, true).await.unwrap().listen().await.unwrap();
  50. executor
  51. .spawn(async move {
  52. let (stream, _) = listener.next().await.unwrap().await.unwrap();
  53. let (mut reader, mut writer) = smol::io::split(stream);
  54. io::copy(&mut reader, &mut writer).await.unwrap();
  55. })
  56. .detach();
  57. let payload = "ohai tcp";
  58. let dialer = Dialer::new(url, None, None, true).await.unwrap();
  59. let mut client = dialer.dial(None).await.unwrap();
  60. payload.encode_async(&mut client).await.unwrap();
  61. let buf: String = AsyncDecodable::decode_async(&mut client).await.unwrap();
  62. assert_eq!(buf, payload);
  63. }));
  64. }
  65. #[test]
  66. fn transport_mixed_channel_addresses() {
  67. let executor = LocalExecutor::new();
  68. smol::block_on(executor.run(async {
  69. let (stream, _peer) = smol::net::unix::UnixStream::pair().unwrap();
  70. let session: Arc<dyn Session + Send + Sync> = Arc::new(TestSession);
  71. let canonical = Url::parse("tcp+tls://peer.example:28880").unwrap();
  72. let derived = Url::parse("tor+tls://peer.example:28880").unwrap();
  73. let channel = Channel::new(
  74. Box::new(stream),
  75. Some(derived.clone()),
  76. canonical.clone(),
  77. Arc::downgrade(&session),
  78. true,
  79. )
  80. .await;
  81. assert_eq!(channel.address(), &canonical);
  82. assert_eq!(channel.connect_addr(), &canonical);
  83. assert_eq!(channel.display_address(), &derived);
  84. assert_eq!(channel.resolve_addr(), Some(derived));
  85. }));
  86. }
  87. #[test]
  88. fn tcp_tls_transport() {
  89. // Register a CryptoProvider for rustls
  90. use futures_rustls::rustls::crypto::{ring, CryptoProvider};
  91. let _ = CryptoProvider::install_default(ring::default_provider());
  92. let executor = LocalExecutor::new();
  93. smol::block_on(executor.run(async {
  94. let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
  95. let port = listener.local_addr().unwrap().port();
  96. drop(listener);
  97. let url = Url::parse(&format!("tcp+tls://127.0.0.1:{port}")).unwrap();
  98. let listener =
  99. Listener::new(url.clone(), None, true).await.unwrap().listen().await.unwrap();
  100. executor
  101. .spawn(async move {
  102. let (stream, _) = listener.next().await.unwrap().await.unwrap();
  103. let (mut reader, mut writer) = smol::io::split(stream);
  104. io::copy(&mut reader, &mut writer).await.unwrap();
  105. })
  106. .detach();
  107. let payload = "ohai tls";
  108. let dialer = Dialer::new(url, None, None, true).await.unwrap();
  109. let mut client = dialer.dial(None).await.unwrap();
  110. payload.encode_async(&mut client).await.unwrap();
  111. let buf: String = AsyncDecodable::decode_async(&mut client).await.unwrap();
  112. assert_eq!(buf, payload);
  113. }));
  114. }
  115. #[test]
  116. fn quic_transport() {
  117. let executor = LocalExecutor::new();
  118. smol::block_on(executor.run(async {
  119. let listener = std::net::UdpSocket::bind("127.0.0.1:0").unwrap();
  120. let port = listener.local_addr().unwrap().port();
  121. drop(listener);
  122. let url = Url::parse(&format!("quic://127.0.0.1:{port}")).unwrap();
  123. let listener =
  124. Listener::new(url.clone(), None, true).await.unwrap().listen().await.unwrap();
  125. executor
  126. .spawn(async move {
  127. let (stream, _) = listener.next().await.unwrap().await.unwrap();
  128. let (mut reader, mut writer) = smol::io::split(stream);
  129. io::copy(&mut reader, &mut writer).await.unwrap();
  130. })
  131. .detach();
  132. let payload = "ohai quic";
  133. let dialer = Dialer::new(url, None, None, true).await.unwrap();
  134. let mut client = dialer.dial(None).await.unwrap();
  135. payload.encode_async(&mut client).await.unwrap();
  136. let buf: String = AsyncDecodable::decode_async(&mut client).await.unwrap();
  137. assert_eq!(buf, payload);
  138. }));
  139. }
  140. #[test]
  141. fn unix_transport() {
  142. let executor = LocalExecutor::new();
  143. let tmpdir = std::env::temp_dir();
  144. let url = Url::parse(&format!(
  145. "unix://{}/darkfi_unix_plain.sock",
  146. tmpdir.as_os_str().to_str().unwrap()
  147. ))
  148. .unwrap();
  149. smol::block_on(executor.run(async {
  150. let listener =
  151. Listener::new(url.clone(), None, true).await.unwrap().listen().await.unwrap();
  152. executor
  153. .spawn(async move {
  154. let (stream, _) = listener.next().await.unwrap().await.unwrap();
  155. let (mut reader, mut writer) = smol::io::split(stream);
  156. io::copy(&mut reader, &mut writer).await.unwrap();
  157. })
  158. .detach();
  159. let payload = "ohai unix";
  160. let dialer = Dialer::new(url, None, None, true).await.unwrap();
  161. let mut client = dialer.dial(None).await.unwrap();
  162. payload.encode_async(&mut client).await.unwrap();
  163. let buf: String = AsyncDecodable::decode_async(&mut client).await.unwrap();
  164. assert_eq!(buf, payload);
  165. }));
  166. }