diff options
| author | 魏曹先生 <1992414357@qq.com> | 2025-09-25 15:49:28 +0800 |
|---|---|---|
| committer | 魏曹先生 <1992414357@qq.com> | 2025-09-25 15:49:28 +0800 |
| commit | 0eeb842c21ff1681e77ccbd47fe7a3c9779a65d5 (patch) | |
| tree | 8c3ffb1f97dbbedb7756fb33f519547083047f24 /crates/utils/tcp_connection/tcp_connection_test/src | |
| parent | 2865206dda1d57df1c95dd8e49d5599db89407ae (diff) | |
Fix clippy warnings in test files
- Convert manual async functions to async fn syntax
- Replace assert_eq!(true/false, ...) with assert!(...) and assert!(!...)
- Fix useless vec warning by using array directly
- All tests continue to pass after optimizations
Diffstat (limited to 'crates/utils/tcp_connection/tcp_connection_test/src')
3 files changed, 112 insertions, 124 deletions
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 index 0ca8540..95b0e3c 100644 --- a/crates/utils/tcp_connection/tcp_connection_test/src/test_challenge.rs +++ b/crates/utils/tcp_connection/tcp_connection_test/src/test_challenge.rs @@ -14,94 +14,90 @@ use tokio::{ pub(crate) struct ExampleChallengeClientHandle; impl ClientHandle<ExampleChallengeServerHandle> for ExampleChallengeClientHandle { - fn process(mut instance: ConnectionInstance) -> impl std::future::Future<Output = ()> + Send { - async move { - // Accept challenge with correct key - let key = current_dir() - .unwrap() - .join("res") - .join("key") - .join("test_key_private.pem"); - let result = instance.accept_challenge(key, "test_key").await.unwrap(); - - // Sent success - assert_eq!(true, result); - let response = instance.read_text().await.unwrap(); - - // Verify success - assert_eq!("OK", response); - - // Accept challenge with wrong key - let key = current_dir() - .unwrap() - .join("res") - .join("key") - .join("wrong_key_private.pem"); - let result = instance.accept_challenge(key, "test_key").await.unwrap(); - - // Sent success - assert_eq!(true, result); - let response = instance.read_text().await.unwrap(); - - // Verify fail - assert_eq!("ERROR", response); - - // Accept challenge with wrong name - let key = current_dir() - .unwrap() - .join("res") - .join("key") - .join("test_key_private.pem"); - let result = instance.accept_challenge(key, "test_key__").await.unwrap(); - - // Sent success - assert_eq!(true, result); - let response = instance.read_text().await.unwrap(); - - // Verify fail - assert_eq!("ERROR", response); - } + async fn process(mut instance: ConnectionInstance) { + // Accept challenge with correct key + let key = current_dir() + .unwrap() + .join("res") + .join("key") + .join("test_key_private.pem"); + let result = instance.accept_challenge(key, "test_key").await.unwrap(); + + // Sent success + assert!(result); + let response = instance.read_text().await.unwrap(); + + // Verify success + assert_eq!("OK", response); + + // Accept challenge with wrong key + let key = current_dir() + .unwrap() + .join("res") + .join("key") + .join("wrong_key_private.pem"); + let result = instance.accept_challenge(key, "test_key").await.unwrap(); + + // Sent success + assert!(result); + let response = instance.read_text().await.unwrap(); + + // Verify fail + assert_eq!("ERROR", response); + + // Accept challenge with wrong name + let key = current_dir() + .unwrap() + .join("res") + .join("key") + .join("test_key_private.pem"); + let result = instance.accept_challenge(key, "test_key__").await.unwrap(); + + // Sent success + assert!(result); + let response = instance.read_text().await.unwrap(); + + // Verify fail + assert_eq!("ERROR", response); } } pub(crate) struct ExampleChallengeServerHandle; impl ServerHandle<ExampleChallengeClientHandle> for ExampleChallengeServerHandle { - fn process(mut instance: ConnectionInstance) -> impl std::future::Future<Output = ()> + Send { - async move { - // Challenge with correct key - let key_dir = current_dir().unwrap().join("res").join("key"); - let result = instance.challenge(key_dir).await.unwrap(); - assert_eq!(true, result); - - // Send response - instance - .write_text(if result { "OK" } else { "ERROR" }) - .await - .unwrap(); - - // Challenge again - let key_dir = current_dir().unwrap().join("res").join("key"); - let result = instance.challenge(key_dir).await.unwrap(); - assert_eq!(false, result); - - // Send response - instance - .write_text(if result { "OK" } else { "ERROR" }) - .await - .unwrap(); - - // Challenge again - let key_dir = current_dir().unwrap().join("res").join("key"); - let result = instance.challenge(key_dir).await.unwrap(); - assert_eq!(false, result); - - // Send response - instance - .write_text(if result { "OK" } else { "ERROR" }) - .await - .unwrap(); - } + async fn process(mut instance: ConnectionInstance) { + // Challenge with correct key + let key_dir = current_dir().unwrap().join("res").join("key"); + let result = instance.challenge(key_dir).await.unwrap(); + assert!(result); + + // Send response + instance + .write_text(if result { "OK" } else { "ERROR" }) + .await + .unwrap(); + + // Challenge again + let key_dir = current_dir().unwrap().join("res").join("key"); + let result = instance.challenge(key_dir).await.unwrap(); + assert!(!result); + + // Send response + instance + .write_text(if result { "OK" } else { "ERROR" }) + .await + .unwrap(); + + // Challenge again + let key_dir = current_dir().unwrap().join("res").join("key"); + let result = instance.challenge(key_dir).await.unwrap(); + assert!(!result); + + // Send response + instance + .write_text(if result { "OK" } else { "ERROR" }) + .await + .unwrap(); } } diff --git a/crates/utils/tcp_connection/tcp_connection_test/src/test_connection.rs b/crates/utils/tcp_connection/tcp_connection_test/src/test_connection.rs index 6238c39..79aac65 100644 --- a/crates/utils/tcp_connection/tcp_connection_test/src/test_connection.rs +++ b/crates/utils/tcp_connection/tcp_connection_test/src/test_connection.rs @@ -11,35 +11,31 @@ use tokio::{join, time::sleep}; pub(crate) struct ExampleClientHandle; impl ClientHandle<ExampleServerHandle> for ExampleClientHandle { - fn process(mut instance: ConnectionInstance) -> impl std::future::Future<Output = ()> + Send { - async move { - // Write name - let Ok(_) = instance.write_text("Peter").await else { - panic!("Write text failed!"); - }; - // Read msg - let Ok(result) = instance.read_text().await else { - return; - }; - assert_eq!("Hello Peter!", result); - } + async fn process(mut instance: ConnectionInstance) { + // Write name + let Ok(_) = instance.write_text("Peter").await else { + panic!("Write text failed!"); + }; + // Read msg + let Ok(result) = instance.read_text().await else { + return; + }; + assert_eq!("Hello Peter!", result); } } pub(crate) struct ExampleServerHandle; impl ServerHandle<ExampleClientHandle> for ExampleServerHandle { - fn process(mut instance: ConnectionInstance) -> impl std::future::Future<Output = ()> + Send { - async move { - // Read name - let Ok(name) = instance.read_text().await else { - return; - }; - // Write msg - let Ok(_) = instance.write_text(format!("Hello {}!", name)).await else { - panic!("Write text failed!"); - }; - } + async fn process(mut instance: ConnectionInstance) { + // Read name + let Ok(name) = instance.read_text().await else { + return; + }; + // Write msg + let Ok(_) = instance.write_text(format!("Hello {}!", name)).await else { + panic!("Write text failed!"); + }; } } diff --git a/crates/utils/tcp_connection/tcp_connection_test/src/test_file_transfer.rs b/crates/utils/tcp_connection/tcp_connection_test/src/test_file_transfer.rs index d316080..9425d30 100644 --- a/crates/utils/tcp_connection/tcp_connection_test/src/test_file_transfer.rs +++ b/crates/utils/tcp_connection/tcp_connection_test/src/test_file_transfer.rs @@ -14,31 +14,27 @@ use tokio::{ pub(crate) struct ExampleFileTransferClientHandle; impl ClientHandle<ExampleFileTransferServerHandle> for ExampleFileTransferClientHandle { - fn process(mut instance: ConnectionInstance) -> impl std::future::Future<Output = ()> + Send { - async move { - let image_path = current_dir() - .unwrap() - .join("res") - .join("image") - .join("test_transfer.png"); - instance.write_file(image_path).await.unwrap(); - } + async fn process(mut instance: ConnectionInstance) { + let image_path = current_dir() + .unwrap() + .join("res") + .join("image") + .join("test_transfer.png"); + instance.write_file(image_path).await.unwrap(); } } pub(crate) struct ExampleFileTransferServerHandle; impl ServerHandle<ExampleFileTransferClientHandle> for ExampleFileTransferServerHandle { - fn process(mut instance: ConnectionInstance) -> impl std::future::Future<Output = ()> + Send { - async move { - let save_path = current_dir() - .unwrap() - .join("res") - .join(".temp") - .join("image") - .join("test_transfer.png"); - instance.read_file(save_path).await.unwrap(); - } + async fn process(mut instance: ConnectionInstance) { + let save_path = current_dir() + .unwrap() + .join("res") + .join(".temp") + .join("image") + .join("test_transfer.png"); + instance.read_file(save_path).await.unwrap(); } } |
