1
0

integration_test.rs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. use anyhow::Result;
  2. use common::{run_rathole_client, PING, PONG};
  3. use rand::Rng;
  4. use std::time::Duration;
  5. use tokio::{
  6. io::{AsyncReadExt, AsyncWriteExt},
  7. net::{TcpStream, UdpSocket},
  8. sync::broadcast,
  9. time,
  10. };
  11. use tracing::{debug, info, instrument};
  12. use tracing_subscriber::EnvFilter;
  13. use crate::common::run_rathole_server;
  14. mod common;
  15. const ECHO_SERVER_ADDR: &str = "127.0.0.1:8080";
  16. const PINGPONG_SERVER_ADDR: &str = "127.0.0.1:8081";
  17. const ECHO_SERVER_ADDR_EXPOSED: &str = "127.0.0.1:2334";
  18. const PINGPONG_SERVER_ADDR_EXPOSED: &str = "127.0.0.1:2335";
  19. const HITTER_NUM: usize = 4;
  20. #[derive(Clone, Copy, Debug)]
  21. enum Type {
  22. Tcp,
  23. Udp,
  24. }
  25. fn init() {
  26. let level = "info";
  27. let _ = tracing_subscriber::fmt()
  28. .with_env_filter(
  29. EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::from(level)),
  30. )
  31. .try_init();
  32. }
  33. #[tokio::test]
  34. async fn tcp() -> Result<()> {
  35. init();
  36. // Spawn a echo server
  37. tokio::spawn(async move {
  38. if let Err(e) = common::tcp::echo_server(ECHO_SERVER_ADDR).await {
  39. panic!("Failed to run the echo server for testing: {:?}", e);
  40. }
  41. });
  42. // Spawn a pingpong server
  43. tokio::spawn(async move {
  44. if let Err(e) = common::tcp::pingpong_server(PINGPONG_SERVER_ADDR).await {
  45. panic!("Failed to run the pingpong server for testing: {:?}", e);
  46. }
  47. });
  48. test("tests/for_tcp/tcp_transport.toml", Type::Tcp).await?;
  49. // FIXME: Self-signed certificate on Mac requires mannual interference. Disable CI for now
  50. #[cfg(not(target_os = "macos"))]
  51. test("tests/for_tcp/tls_transport.toml", Type::Tcp).await?;
  52. test("tests/for_tcp/noise_transport.toml", Type::Tcp).await?;
  53. test("tests/for_tcp/websocket_transport.toml", Type::Tcp).await?;
  54. #[cfg(not(target_os = "macos"))]
  55. test("tests/for_tcp/websocket_tls_transport.toml", Type::Tcp).await?;
  56. Ok(())
  57. }
  58. #[tokio::test]
  59. async fn udp() -> Result<()> {
  60. init();
  61. // Spawn a echo server
  62. tokio::spawn(async move {
  63. if let Err(e) = common::udp::echo_server(ECHO_SERVER_ADDR).await {
  64. panic!("Failed to run the echo server for testing: {:?}", e);
  65. }
  66. });
  67. // Spawn a pingpong server
  68. tokio::spawn(async move {
  69. if let Err(e) = common::udp::pingpong_server(PINGPONG_SERVER_ADDR).await {
  70. panic!("Failed to run the pingpong server for testing: {:?}", e);
  71. }
  72. });
  73. test("tests/for_udp/tcp_transport.toml", Type::Udp).await?;
  74. // See above
  75. #[cfg(not(target_os = "macos"))]
  76. test("tests/for_udp/tls_transport.toml", Type::Udp).await?;
  77. test("tests/for_udp/noise_transport.toml", Type::Udp).await?;
  78. test("tests/for_udp/websocket_transport.toml", Type::Udp).await?;
  79. #[cfg(not(target_os = "macos"))]
  80. test("tests/for_udp/websocket_tls_transport.toml", Type::Udp).await?;
  81. Ok(())
  82. }
  83. #[instrument]
  84. async fn test(config_path: &'static str, t: Type) -> Result<()> {
  85. let (client_shutdown_tx, client_shutdown_rx) = broadcast::channel(1);
  86. let (server_shutdown_tx, server_shutdown_rx) = broadcast::channel(1);
  87. // Start the client
  88. info!("start the client");
  89. let client = tokio::spawn(async move {
  90. run_rathole_client(config_path, client_shutdown_rx)
  91. .await
  92. .unwrap();
  93. });
  94. // Sleep for 1 second. Expect the client keep retrying to reach the server
  95. time::sleep(Duration::from_secs(1)).await;
  96. // Start the server
  97. info!("start the server");
  98. let server = tokio::spawn(async move {
  99. run_rathole_server(config_path, server_shutdown_rx)
  100. .await
  101. .unwrap();
  102. });
  103. time::sleep(Duration::from_millis(2500)).await; // Wait for the client to retry
  104. info!("echo");
  105. echo_hitter(ECHO_SERVER_ADDR_EXPOSED, t).await.unwrap();
  106. info!("pingpong");
  107. pingpong_hitter(PINGPONG_SERVER_ADDR_EXPOSED, t)
  108. .await
  109. .unwrap();
  110. // Simulate the client crash and restart
  111. info!("shutdown the client");
  112. client_shutdown_tx.send(true)?;
  113. let _ = tokio::join!(client);
  114. info!("restart the client");
  115. let client_shutdown_rx = client_shutdown_tx.subscribe();
  116. let client = tokio::spawn(async move {
  117. run_rathole_client(config_path, client_shutdown_rx)
  118. .await
  119. .unwrap();
  120. });
  121. time::sleep(Duration::from_secs(1)).await; // Wait for the client to start
  122. info!("echo");
  123. echo_hitter(ECHO_SERVER_ADDR_EXPOSED, t).await.unwrap();
  124. info!("pingpong");
  125. pingpong_hitter(PINGPONG_SERVER_ADDR_EXPOSED, t)
  126. .await
  127. .unwrap();
  128. // Simulate the server crash and restart
  129. info!("shutdown the server");
  130. server_shutdown_tx.send(true)?;
  131. let _ = tokio::join!(server);
  132. info!("restart the server");
  133. let server_shutdown_rx = server_shutdown_tx.subscribe();
  134. let server = tokio::spawn(async move {
  135. run_rathole_server(config_path, server_shutdown_rx)
  136. .await
  137. .unwrap();
  138. });
  139. time::sleep(Duration::from_millis(2500)).await; // Wait for the client to retry
  140. // Simulate heavy load
  141. info!("lots of echo and pingpong");
  142. let mut v = Vec::new();
  143. for _ in 0..HITTER_NUM / 2 {
  144. v.push(tokio::spawn(async move {
  145. echo_hitter(ECHO_SERVER_ADDR_EXPOSED, t).await.unwrap();
  146. }));
  147. v.push(tokio::spawn(async move {
  148. pingpong_hitter(PINGPONG_SERVER_ADDR_EXPOSED, t)
  149. .await
  150. .unwrap();
  151. }));
  152. }
  153. for h in v {
  154. assert!(tokio::join!(h).0.is_ok());
  155. }
  156. // Shutdown
  157. info!("shutdown the server and the client");
  158. server_shutdown_tx.send(true)?;
  159. client_shutdown_tx.send(true)?;
  160. let _ = tokio::join!(server, client);
  161. Ok(())
  162. }
  163. async fn echo_hitter(addr: &'static str, t: Type) -> Result<()> {
  164. match t {
  165. Type::Tcp => tcp_echo_hitter(addr).await,
  166. Type::Udp => udp_echo_hitter(addr).await,
  167. }
  168. }
  169. async fn pingpong_hitter(addr: &'static str, t: Type) -> Result<()> {
  170. match t {
  171. Type::Tcp => tcp_pingpong_hitter(addr).await,
  172. Type::Udp => udp_pingpong_hitter(addr).await,
  173. }
  174. }
  175. async fn tcp_echo_hitter(addr: &'static str) -> Result<()> {
  176. let mut conn = TcpStream::connect(addr).await?;
  177. let mut wr = [0u8; 1024];
  178. let mut rd = [0u8; 1024];
  179. for _ in 0..100 {
  180. rand::thread_rng().fill(&mut wr);
  181. conn.write_all(&wr).await?;
  182. conn.read_exact(&mut rd).await?;
  183. assert_eq!(wr, rd);
  184. }
  185. Ok(())
  186. }
  187. async fn udp_echo_hitter(addr: &'static str) -> Result<()> {
  188. let conn = UdpSocket::bind("127.0.0.1:0").await?;
  189. conn.connect(addr).await?;
  190. let mut wr = [0u8; 128];
  191. let mut rd = [0u8; 128];
  192. for _ in 0..3 {
  193. rand::thread_rng().fill(&mut wr);
  194. conn.send(&wr).await?;
  195. debug!("send");
  196. conn.recv(&mut rd).await?;
  197. debug!("recv");
  198. assert_eq!(wr, rd);
  199. }
  200. Ok(())
  201. }
  202. async fn tcp_pingpong_hitter(addr: &'static str) -> Result<()> {
  203. let mut conn = TcpStream::connect(addr).await?;
  204. let wr = PING.as_bytes();
  205. let mut rd = [0u8; PONG.len()];
  206. for _ in 0..100 {
  207. conn.write_all(wr).await?;
  208. conn.read_exact(&mut rd).await?;
  209. assert_eq!(rd, PONG.as_bytes());
  210. }
  211. Ok(())
  212. }
  213. async fn udp_pingpong_hitter(addr: &'static str) -> Result<()> {
  214. let conn = UdpSocket::bind("127.0.0.1:0").await?;
  215. conn.connect(&addr).await?;
  216. let wr = PING.as_bytes();
  217. let mut rd = [0u8; PONG.len()];
  218. for _ in 0..3 {
  219. conn.send(wr).await?;
  220. debug!("ping");
  221. conn.recv(&mut rd).await?;
  222. debug!("pong");
  223. assert_eq!(rd, PONG.as_bytes());
  224. }
  225. Ok(())
  226. }