summaryrefslogtreecommitdiff
path: root/crates/utils
diff options
context:
space:
mode:
Diffstat (limited to 'crates/utils')
-rw-r--r--crates/utils/cfg_file/cfg_file_test/src/lib.rs2
-rw-r--r--crates/utils/tcp_connection/tcp_connection_test/src/test_challenge.rs160
-rw-r--r--crates/utils/tcp_connection/tcp_connection_test/src/test_connection.rs42
-rw-r--r--crates/utils/tcp_connection/tcp_connection_test/src/test_file_transfer.rs34
4 files changed, 113 insertions, 125 deletions
diff --git a/crates/utils/cfg_file/cfg_file_test/src/lib.rs b/crates/utils/cfg_file/cfg_file_test/src/lib.rs
index 800dfe7..4db4c22 100644
--- a/crates/utils/cfg_file/cfg_file_test/src/lib.rs
+++ b/crates/utils/cfg_file/cfg_file_test/src/lib.rs
@@ -19,7 +19,7 @@ mod test_cfg_file {
let mut example = ExampleConfig {
name: "Weicao".to_string(),
age: 22,
- hobby: vec!["Programming", "Painting"]
+ hobby: ["Programming", "Painting"]
.iter()
.map(|m| m.to_string())
.collect(),
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();
}
}