From a3684686e1cfc035754b4c62187550e4b9de16bd Mon Sep 17 00:00:00 2001 From: Yujia Qiao Date: Fri, 24 Dec 2021 21:04:21 +0800 Subject: [PATCH] test: add tests for noise --- tests/config_test/valid_config/full.toml | 52 +++++++++++++++--------- tests/for_tcp/noise_transport.toml | 27 ++++++++++++ tests/for_udp/noise_transport.toml | 31 ++++++++++++++ tests/integration_test.rs | 14 ++++--- 4 files changed, 99 insertions(+), 25 deletions(-) create mode 100644 tests/for_tcp/noise_transport.toml create mode 100644 tests/for_udp/noise_transport.toml diff --git a/tests/config_test/valid_config/full.toml b/tests/config_test/valid_config/full.toml index 804d83d..0fb9fc6 100644 --- a/tests/config_test/valid_config/full.toml +++ b/tests/config_test/valid_config/full.toml @@ -1,33 +1,47 @@ [client] -remote_addr = "example.com:2333" -default_token = "default_token_if_not_specify" +remote_addr = "example.com:2333" # Necessary. The address of the server +default_token = "default_token_if_not_specify" # Optional. The default token of services, if they don't define their own ones [client.transport] -type = "tcp" -[client.transport.tls] -trusted_root = "ca.pem" -hostname = "example.com" +type = "tcp" # Optional. Possible values: ["tcp", "tls"]. Default: "tcp" -[client.services.service1] -token = "whatever" -local_addr = "127.0.0.1:1081" +[client.transport.tls] # Necessary if `type` is "tls" +trusted_root = "ca.pem" # Necessary. The certificate of CA that signed the server's certificate +hostname = "example.com" # Optional. The hostname that the client uses to validate the certificate. If not set, fallback to `client.remote_addr` -[client.services.service2] +[client.transport.noise] # Noise protocol. See `docs/security.md` for further explanation +pattern = "Noise_NK_25519_ChaChaPoly_BLAKE2s" # Optional. Default value as shown +local_private_key = "key_encoded_in_base64" # Optional +remote_public_key = "key_encoded_in_base64" # Optional + +[client.services.service1] # A service that needs forwarding. The name `service1` can change arbitrarily, as long as identical to the name in the server's configuration +type = "tcp" # Optional. The protocol that needs forwarding. Possible values: ["tcp", "udp"]. Default: "tcp" +token = "whatever" # Necessary if `client.default_token` not set +local_addr = "127.0.0.1:1081" # Necessary. The address of the service that needs to be forwarded + +[client.services.service2] # Multiple services can be defined local_addr = "127.0.0.1:1082" [server] -bind_addr = "0.0.0.0:2333" -default_token = "default_token_if_not_specify" +bind_addr = "0.0.0.0:2333" # Necessary. The address that the server listens for clients. Generally only the port needs to be change. +default_token = "default_token_if_not_specify" # Optional [server.transport] -type = "tls" -[server.transport.tls] -pkcs12 = "identify.pfx" -pkcs12_password = "password" +type = "tcp" # Same as `[client.transport]` -[server.services.service1] -token = "whatever" -bind_addr = "0.0.0.0:8081" +[server.transport.tls] # Necessary if `type` is "tls" +pkcs12 = "identify.pfx" # Necessary. pkcs12 file of server's certificate and private key +pkcs12_password = "password" # Necessary. Password of the pkcs12 file + +[server.transport.noise] # Same as `[client.transport.noise]` +pattern = "Noise_NK_25519_ChaChaPoly_BLAKE2s" +local_private_key = "key_encoded_in_base64" +remote_public_key = "key_encoded_in_base64" + +[server.services.service1] # The service name must be identical to the client side +type = "tcp" # Optional. Same as the client `[client.services.X.type] +token = "whatever" # Necesary if `server.default_token` not set +bind_addr = "0.0.0.0:8081" # Necessary. The address of the service is exposed at. Generally only the port needs to be change. [server.services.service2] bind_addr = "0.0.0.1:8082" diff --git a/tests/for_tcp/noise_transport.toml b/tests/for_tcp/noise_transport.toml new file mode 100644 index 0000000..3b63cba --- /dev/null +++ b/tests/for_tcp/noise_transport.toml @@ -0,0 +1,27 @@ +[client] +remote_addr = "localhost:2333" +default_token = "default_token_if_not_specify" + +[client.transport] +type = "noise" +[client.transport.noise] +remote_public_key = "mEnUEACy9UrTBmwoCJb6fcKWBRdvfD9XzuBVsroOLFg=" + +[client.services.echo] +local_addr = "localhost:8080" +[client.services.pingpong] +local_addr = "localhost:8081" + +[server] +bind_addr = "0.0.0.0:2333" +default_token = "default_token_if_not_specify" + +[server.transport] +type = "noise" +[server.transport.noise] +local_private_key = "kQiSRtS3bs8BoGCJYgFnl1FLrTG1lV53Dj8jSjmg8tE=" + +[server.services.echo] +bind_addr = "0.0.0.0:2334" +[server.services.pingpong] +bind_addr = "0.0.0.0:2335" diff --git a/tests/for_udp/noise_transport.toml b/tests/for_udp/noise_transport.toml new file mode 100644 index 0000000..db60a91 --- /dev/null +++ b/tests/for_udp/noise_transport.toml @@ -0,0 +1,31 @@ +[client] +remote_addr = "localhost:2332" +default_token = "default_token_if_not_specify" + +[client.transport] +type = "noise" +[client.transport.noise] +remote_public_key = "mEnUEACy9UrTBmwoCJb6fcKWBRdvfD9XzuBVsroOLFg=" + +[client.services.echo] +type = "udp" +local_addr = "localhost:8080" +[client.services.pingpong] +type = "udp" +local_addr = "localhost:8081" + +[server] +bind_addr = "0.0.0.0:2332" +default_token = "default_token_if_not_specify" + +[server.transport] +type = "noise" +[server.transport.noise] +local_private_key = "kQiSRtS3bs8BoGCJYgFnl1FLrTG1lV53Dj8jSjmg8tE=" + +[server.services.echo] +type = "udp" +bind_addr = "0.0.0.0:2334" +[server.services.pingpong] +type = "udp" +bind_addr = "0.0.0.0:2335" diff --git a/tests/integration_test.rs b/tests/integration_test.rs index 686b011..10ae2a5 100644 --- a/tests/integration_test.rs +++ b/tests/integration_test.rs @@ -56,6 +56,7 @@ async fn tcp() -> Result<()> { test("tests/for_tcp/tcp_transport.toml", Type::Tcp).await?; test("tests/for_tcp/tls_transport.toml", Type::Tcp).await?; + test("tests/for_tcp/noise_transport.toml", Type::Tcp).await?; Ok(()) } @@ -80,6 +81,7 @@ async fn udp() -> Result<()> { test("tests/for_udp/tcp_transport.toml", Type::Udp).await?; test("tests/for_udp/tls_transport.toml", Type::Udp).await?; + test("tests/for_udp/noise_transport.toml", Type::Udp).await?; Ok(()) } @@ -91,7 +93,7 @@ async fn test(config_path: &'static str, t: Type) -> Result<()> { // Start the client info!("start the client"); - tokio::spawn(async move { + let client = tokio::spawn(async move { run_rathole_client(&config_path, client_shutdown_rx) .await .unwrap(); @@ -102,12 +104,12 @@ async fn test(config_path: &'static str, t: Type) -> Result<()> { // Start the server info!("start the server"); - tokio::spawn(async move { + let server = tokio::spawn(async move { run_rathole_server(&config_path, server_shutdown_rx) .await .unwrap(); }); - time::sleep(Duration::from_secs(1)).await; // Wait for the client to retry + time::sleep(Duration::from_millis(2000)).await; // Wait for the client to retry info!("echo"); echo_hitter(ECHO_SERVER_ADDR_EXPOSED, t).await.unwrap(); @@ -119,7 +121,7 @@ async fn test(config_path: &'static str, t: Type) -> Result<()> { // Simulate the client crash and restart info!("shutdown the client"); client_shutdown_tx.send(true)?; - time::sleep(Duration::from_millis(500)).await; + let _ = tokio::join!(client); info!("restart the client"); let client_shutdown_rx = client_shutdown_tx.subscribe(); @@ -140,7 +142,7 @@ async fn test(config_path: &'static str, t: Type) -> Result<()> { // Simulate the server crash and restart info!("shutdown the server"); server_shutdown_tx.send(true)?; - time::sleep(Duration::from_millis(500)).await; + let _ = tokio::join!(server); info!("restart the server"); let server_shutdown_rx = server_shutdown_tx.subscribe(); @@ -149,7 +151,7 @@ async fn test(config_path: &'static str, t: Type) -> Result<()> { .await .unwrap(); }); - time::sleep(Duration::from_secs(1)).await; // Wait for the client to retry + time::sleep(Duration::from_millis(2000)).await; // Wait for the client to retry // Simulate heavy load info!("lots of echo and pingpong");