network_transports.rs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2025 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. let url = Url::parse("tcp://127.0.0.1:5432").unwrap();
  26. smol::block_on(executor.run(async {
  27. let listener = Listener::new(url.clone(), None).await.unwrap().listen().await.unwrap();
  28. executor
  29. .spawn(async move {
  30. let (stream, _) = listener.next().await.unwrap();
  31. let (mut reader, mut writer) = smol::io::split(stream);
  32. io::copy(&mut reader, &mut writer).await.unwrap();
  33. })
  34. .detach();
  35. let payload = "ohai tcp";
  36. let dialer = Dialer::new(url, None).await.unwrap();
  37. let mut client = dialer.dial(None).await.unwrap();
  38. payload.encode_async(&mut client).await.unwrap();
  39. let buf: String = AsyncDecodable::decode_async(&mut client).await.unwrap();
  40. assert_eq!(buf, payload);
  41. }));
  42. }
  43. #[test]
  44. fn tcp_tls_transport() {
  45. // Register a CryptoProvider for rustls
  46. use futures_rustls::rustls::crypto::{ring, CryptoProvider};
  47. let _ = CryptoProvider::install_default(ring::default_provider());
  48. let executor = LocalExecutor::new();
  49. let url = Url::parse("tcp+tls://127.0.0.1:5433").unwrap();
  50. smol::block_on(executor.run(async {
  51. let listener = Listener::new(url.clone(), None).await.unwrap().listen().await.unwrap();
  52. executor
  53. .spawn(async move {
  54. let (stream, _) = listener.next().await.unwrap();
  55. let (mut reader, mut writer) = smol::io::split(stream);
  56. io::copy(&mut reader, &mut writer).await.unwrap();
  57. })
  58. .detach();
  59. let payload = "ohai tls";
  60. let dialer = Dialer::new(url, None).await.unwrap();
  61. let mut client = dialer.dial(None).await.unwrap();
  62. payload.encode_async(&mut client).await.unwrap();
  63. let buf: String = AsyncDecodable::decode_async(&mut client).await.unwrap();
  64. assert_eq!(buf, payload);
  65. }));
  66. }
  67. #[test]
  68. fn unix_transport() {
  69. let executor = LocalExecutor::new();
  70. let tmpdir = std::env::temp_dir();
  71. let url = Url::parse(&format!(
  72. "unix://{}/darkfi_unix_plain.sock",
  73. tmpdir.as_os_str().to_str().unwrap()
  74. ))
  75. .unwrap();
  76. smol::block_on(executor.run(async {
  77. let listener = Listener::new(url.clone(), None).await.unwrap().listen().await.unwrap();
  78. executor
  79. .spawn(async move {
  80. let (stream, _) = listener.next().await.unwrap();
  81. let (mut reader, mut writer) = smol::io::split(stream);
  82. io::copy(&mut reader, &mut writer).await.unwrap();
  83. })
  84. .detach();
  85. let payload = "ohai unix";
  86. let dialer = Dialer::new(url, None).await.unwrap();
  87. let mut client = dialer.dial(None).await.unwrap();
  88. payload.encode_async(&mut client).await.unwrap();
  89. let buf: String = AsyncDecodable::decode_async(&mut client).await.unwrap();
  90. assert_eq!(buf, payload);
  91. }));
  92. }