network_transports.rs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2024 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()).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).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. let executor = LocalExecutor::new();
  46. let url = Url::parse("tcp+tls://127.0.0.1:5433").unwrap();
  47. smol::block_on(executor.run(async {
  48. let listener = Listener::new(url.clone()).await.unwrap().listen().await.unwrap();
  49. executor
  50. .spawn(async move {
  51. let (stream, _) = listener.next().await.unwrap();
  52. let (mut reader, mut writer) = smol::io::split(stream);
  53. io::copy(&mut reader, &mut writer).await.unwrap();
  54. })
  55. .detach();
  56. let payload = "ohai tls";
  57. let dialer = Dialer::new(url).await.unwrap();
  58. let mut client = dialer.dial(None).await.unwrap();
  59. payload.encode_async(&mut client).await.unwrap();
  60. let buf: String = AsyncDecodable::decode_async(&mut client).await.unwrap();
  61. assert_eq!(buf, payload);
  62. }));
  63. }
  64. #[test]
  65. fn unix_transport() {
  66. let executor = LocalExecutor::new();
  67. let tmpdir = std::env::temp_dir();
  68. let url = Url::parse(&format!(
  69. "unix://{}/darkfi_unix_plain.sock",
  70. tmpdir.as_os_str().to_str().unwrap()
  71. ))
  72. .unwrap();
  73. smol::block_on(executor.run(async {
  74. let listener = Listener::new(url.clone()).await.unwrap().listen().await.unwrap();
  75. executor
  76. .spawn(async move {
  77. let (stream, _) = listener.next().await.unwrap();
  78. let (mut reader, mut writer) = smol::io::split(stream);
  79. io::copy(&mut reader, &mut writer).await.unwrap();
  80. })
  81. .detach();
  82. let payload = "ohai unix";
  83. let dialer = Dialer::new(url).await.unwrap();
  84. let mut client = dialer.dial(None).await.unwrap();
  85. payload.encode_async(&mut client).await.unwrap();
  86. let buf: String = AsyncDecodable::decode_async(&mut client).await.unwrap();
  87. assert_eq!(buf, payload);
  88. }));
  89. }