summaryrefslogtreecommitdiff
path: root/crates/utils/tcp_connection/tcp_connection_test
diff options
context:
space:
mode:
author魏曹先生 <1992414357@qq.com>2025-09-21 16:15:20 +0800
committer魏曹先生 <1992414357@qq.com>2025-09-21 16:15:20 +0800
commit2b387749286ecf0295abf4d4421978680ba96dae (patch)
tree72f4bd0e91d4541c65f96c16c39d9fd316b709e2 /crates/utils/tcp_connection/tcp_connection_test
parent1c7db5aaef2a91b3f87c999d4a21794b19d49ab6 (diff)
Add challenge test for `tcp_connection`
Diffstat (limited to 'crates/utils/tcp_connection/tcp_connection_test')
-rw-r--r--crates/utils/tcp_connection/tcp_connection_test/src/lib.rs3
-rw-r--r--crates/utils/tcp_connection/tcp_connection_test/src/test_challenge.rs84
2 files changed, 87 insertions, 0 deletions
diff --git a/crates/utils/tcp_connection/tcp_connection_test/src/lib.rs b/crates/utils/tcp_connection/tcp_connection_test/src/lib.rs
index cc585d1..2774458 100644
--- a/crates/utils/tcp_connection/tcp_connection_test/src/lib.rs
+++ b/crates/utils/tcp_connection/tcp_connection_test/src/lib.rs
@@ -3,3 +3,6 @@ pub mod test_tcp_target_build;
#[cfg(test)]
pub mod test_connection;
+
+#[cfg(test)]
+pub mod test_challenge;
diff --git a/crates/utils/tcp_connection/tcp_connection_test/src/test_challenge.rs b/crates/utils/tcp_connection/tcp_connection_test/src/test_challenge.rs
new file mode 100644
index 0000000..ac57451
--- /dev/null
+++ b/crates/utils/tcp_connection/tcp_connection_test/src/test_challenge.rs
@@ -0,0 +1,84 @@
+use std::{
+ env::{current_dir, set_current_dir},
+ time::Duration,
+};
+
+use tcp_connection::{
+ handle::{ClientHandle, ServerHandle},
+ instance::ConnectionInstance,
+ target::TcpServerTarget,
+ target_configure::ServerTargetConfig,
+};
+use tokio::{join, time::sleep};
+
+pub(crate) struct ExampleChallengeClientHandle;
+
+impl ClientHandle<ExampleChallengeServerHandle> for ExampleChallengeClientHandle {
+ fn process(
+ mut instance: ConnectionInstance,
+ ) -> impl std::future::Future<Output = ()> + Send + Sync {
+ async move {
+ // TODO :: Complete the implementation
+ }
+ }
+}
+
+pub(crate) struct ExampleChallengeServerHandle;
+
+impl ServerHandle<ExampleChallengeClientHandle> for ExampleChallengeServerHandle {
+ fn process(
+ mut instance: ConnectionInstance,
+ ) -> impl std::future::Future<Output = ()> + Send + Sync {
+ async move {
+ // TODO :: Complete the implementation
+ }
+ }
+}
+
+#[tokio::test]
+async fn test_connection_with_challenge_handle() -> Result<(), std::io::Error> {
+ let host = "localhost";
+
+ // Enter temp directory
+ set_current_dir(current_dir().unwrap().join(".temp/"))?;
+
+ // Server setup
+ let Ok(server_target) = TcpServerTarget::<
+ ExampleChallengeClientHandle,
+ ExampleChallengeServerHandle,
+ >::from_domain(host)
+ .await
+ else {
+ panic!("Test target built failed from a domain named `{}`", host);
+ };
+
+ // Client setup
+ let Ok(client_target) = TcpServerTarget::<
+ ExampleChallengeClientHandle,
+ ExampleChallengeServerHandle,
+ >::from_domain(host)
+ .await
+ else {
+ panic!("Test target built failed from a domain named `{}`", host);
+ };
+
+ let future_server = async move {
+ // Only process once
+ let configured_server = server_target.server_cfg(ServerTargetConfig::default().once());
+
+ // Listen here
+ let _ = configured_server.listen().await;
+ };
+
+ let future_client = async move {
+ // Wait for server start
+ let _ = sleep(Duration::from_secs_f32(1.5)).await;
+
+ // Connect here
+ let _ = client_target.connect().await;
+ };
+
+ let _ = async { join!(future_client, future_server) }.await;
+
+ Ok(())
+}