network_transports.rs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2023 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 smol::{
  19. io::{self, AsyncReadExt, AsyncWriteExt},
  20. LocalExecutor,
  21. };
  22. use url::Url;
  23. use darkfi::net::transport::{Dialer, Listener};
  24. #[test]
  25. fn tcp_transport() {
  26. let executor = LocalExecutor::new();
  27. let url = Url::parse("tcp://127.0.0.1:5432").unwrap();
  28. smol::block_on(executor.run(async {
  29. let listener = Listener::new(url.clone()).await.unwrap().listen().await.unwrap();
  30. executor
  31. .spawn(async move {
  32. let (stream, _) = listener.next().await.unwrap();
  33. let (mut reader, mut writer) = smol::io::split(stream);
  34. io::copy(&mut reader, &mut writer).await.unwrap();
  35. })
  36. .detach();
  37. let payload = b"ohai tcp";
  38. let dialer = Dialer::new(url).await.unwrap();
  39. let mut client = dialer.dial(None).await.unwrap();
  40. client.write_all(payload).await.unwrap();
  41. let mut buf = vec![0u8; 8];
  42. client.read_exact(&mut buf).await.unwrap();
  43. assert_eq!(buf, payload);
  44. }));
  45. }
  46. #[test]
  47. fn tcp_tls_transport() {
  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()).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 = b"ohai tls";
  60. let dialer = Dialer::new(url).await.unwrap();
  61. let mut client = dialer.dial(None).await.unwrap();
  62. client.write_all(payload).await.unwrap();
  63. let mut buf = vec![0u8; 8];
  64. client.read_exact(&mut buf).await.unwrap();
  65. assert_eq!(buf, payload);
  66. }));
  67. }
  68. #[test]
  69. fn unix_transport() {
  70. let executor = LocalExecutor::new();
  71. let tmpdir = std::env::temp_dir();
  72. let url = Url::parse(&format!(
  73. "unix://{}/darkfi_unix_plain.sock",
  74. tmpdir.as_os_str().to_str().unwrap()
  75. ))
  76. .unwrap();
  77. smol::block_on(executor.run(async {
  78. let listener = Listener::new(url.clone()).await.unwrap().listen().await.unwrap();
  79. executor
  80. .spawn(async move {
  81. let (stream, _) = listener.next().await.unwrap();
  82. let (mut reader, mut writer) = smol::io::split(stream);
  83. io::copy(&mut reader, &mut writer).await.unwrap();
  84. })
  85. .detach();
  86. let payload = b"ohai unix";
  87. let dialer = Dialer::new(url).await.unwrap();
  88. let mut client = dialer.dial(None).await.unwrap();
  89. client.write_all(payload).await.unwrap();
  90. let mut buf = vec![0u8; 9];
  91. client.read_exact(&mut buf).await.unwrap();
  92. assert_eq!(buf, payload);
  93. }));
  94. }