network_transports.rs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 darkfi_serial::{AsyncDecodable, AsyncEncodable};
  19. use smol::{io, LocalExecutor};
  20. use url::Url;
  21. use darkfi::net::transport::{Dialer, Listener};
  22. #[test]
  23. fn tcp_transport() {
  24. let executor = LocalExecutor::new();
  25. smol::block_on(executor.run(async {
  26. let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
  27. let port = listener.local_addr().unwrap().port();
  28. drop(listener);
  29. let url = Url::parse(&format!("tcp://127.0.0.1:{port}")).unwrap();
  30. let listener = Listener::new(url.clone(), None).await.unwrap().listen().await.unwrap();
  31. executor
  32. .spawn(async move {
  33. let (stream, _) = listener.next().await.unwrap();
  34. let (mut reader, mut writer) = smol::io::split(stream);
  35. io::copy(&mut reader, &mut writer).await.unwrap();
  36. })
  37. .detach();
  38. let payload = "ohai tcp";
  39. let dialer = Dialer::new(url, None, None).await.unwrap();
  40. let mut client = dialer.dial(None).await.unwrap();
  41. payload.encode_async(&mut client).await.unwrap();
  42. let buf: String = AsyncDecodable::decode_async(&mut client).await.unwrap();
  43. assert_eq!(buf, payload);
  44. }));
  45. }
  46. #[test]
  47. fn tcp_tls_transport() {
  48. // Register a CryptoProvider for rustls
  49. use futures_rustls::rustls::crypto::{ring, CryptoProvider};
  50. let _ = CryptoProvider::install_default(ring::default_provider());
  51. let executor = LocalExecutor::new();
  52. smol::block_on(executor.run(async {
  53. let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
  54. let port = listener.local_addr().unwrap().port();
  55. drop(listener);
  56. let url = Url::parse(&format!("tcp://127.0.0.1:{port}")).unwrap();
  57. let listener = Listener::new(url.clone(), None).await.unwrap().listen().await.unwrap();
  58. executor
  59. .spawn(async move {
  60. let (stream, _) = listener.next().await.unwrap();
  61. let (mut reader, mut writer) = smol::io::split(stream);
  62. io::copy(&mut reader, &mut writer).await.unwrap();
  63. })
  64. .detach();
  65. let payload = "ohai tls";
  66. let dialer = Dialer::new(url, None, None).await.unwrap();
  67. let mut client = dialer.dial(None).await.unwrap();
  68. payload.encode_async(&mut client).await.unwrap();
  69. let buf: String = AsyncDecodable::decode_async(&mut client).await.unwrap();
  70. assert_eq!(buf, payload);
  71. }));
  72. }
  73. #[test]
  74. fn quic_transport() {
  75. let executor = LocalExecutor::new();
  76. smol::block_on(executor.run(async {
  77. let listener = std::net::UdpSocket::bind("127.0.0.1:0").unwrap();
  78. let port = listener.local_addr().unwrap().port();
  79. drop(listener);
  80. let url = Url::parse(&format!("quic://127.0.0.1:{port}")).unwrap();
  81. let listener = Listener::new(url.clone(), None).await.unwrap().listen().await.unwrap();
  82. executor
  83. .spawn(async move {
  84. let (stream, _) = listener.next().await.unwrap();
  85. let (mut reader, mut writer) = smol::io::split(stream);
  86. io::copy(&mut reader, &mut writer).await.unwrap();
  87. })
  88. .detach();
  89. let payload = "ohai quic";
  90. let dialer = Dialer::new(url, None, None).await.unwrap();
  91. let mut client = dialer.dial(None).await.unwrap();
  92. payload.encode_async(&mut client).await.unwrap();
  93. let buf: String = AsyncDecodable::decode_async(&mut client).await.unwrap();
  94. assert_eq!(buf, payload);
  95. }));
  96. }
  97. #[test]
  98. fn unix_transport() {
  99. let executor = LocalExecutor::new();
  100. let tmpdir = std::env::temp_dir();
  101. let url = Url::parse(&format!(
  102. "unix://{}/darkfi_unix_plain.sock",
  103. tmpdir.as_os_str().to_str().unwrap()
  104. ))
  105. .unwrap();
  106. smol::block_on(executor.run(async {
  107. let listener = Listener::new(url.clone(), None).await.unwrap().listen().await.unwrap();
  108. executor
  109. .spawn(async move {
  110. let (stream, _) = listener.next().await.unwrap();
  111. let (mut reader, mut writer) = smol::io::split(stream);
  112. io::copy(&mut reader, &mut writer).await.unwrap();
  113. })
  114. .detach();
  115. let payload = "ohai unix";
  116. let dialer = Dialer::new(url, None, None).await.unwrap();
  117. let mut client = dialer.dial(None).await.unwrap();
  118. payload.encode_async(&mut client).await.unwrap();
  119. let buf: String = AsyncDecodable::decode_async(&mut client).await.unwrap();
  120. assert_eq!(buf, payload);
  121. }));
  122. }