aboutsummaryrefslogtreecommitdiff
path: root/crates/utils/tcp_connection/tcp_connection_test/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/utils/tcp_connection/tcp_connection_test/src')
-rw-r--r--crates/utils/tcp_connection/tcp_connection_test/src/lib.rs17
-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.rs78
-rw-r--r--crates/utils/tcp_connection/tcp_connection_test/src/test_file_transfer.rs94
-rw-r--r--crates/utils/tcp_connection/tcp_connection_test/src/test_msgpack.rs103
-rw-r--r--crates/utils/tcp_connection/tcp_connection_test/src/test_tcp_target_build.rs32
-rw-r--r--crates/utils/tcp_connection/tcp_connection_test/src/test_utils.rs4
-rw-r--r--crates/utils/tcp_connection/tcp_connection_test/src/test_utils/handle.rs11
-rw-r--r--crates/utils/tcp_connection/tcp_connection_test/src/test_utils/target.rs201
-rw-r--r--crates/utils/tcp_connection/tcp_connection_test/src/test_utils/target_configure.rs53
-rw-r--r--crates/utils/tcp_connection/tcp_connection_test/src/test_utils/target_connection.rs89
11 files changed, 0 insertions, 842 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
deleted file mode 100644
index c9372d4..0000000
--- a/crates/utils/tcp_connection/tcp_connection_test/src/lib.rs
+++ /dev/null
@@ -1,17 +0,0 @@
-#[cfg(test)]
-pub mod test_tcp_target_build;
-
-#[cfg(test)]
-pub mod test_connection;
-
-#[cfg(test)]
-pub mod test_challenge;
-
-#[cfg(test)]
-pub mod test_file_transfer;
-
-#[cfg(test)]
-pub mod test_msgpack;
-
-pub mod test_utils;
-pub use test_utils::*;
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
deleted file mode 100644
index 9327b3e..0000000
--- a/crates/utils/tcp_connection/tcp_connection_test/src/test_challenge.rs
+++ /dev/null
@@ -1,160 +0,0 @@
-use std::{env::current_dir, time::Duration};
-
-use tcp_connection::instance::ConnectionInstance;
-use tokio::{
- join,
- time::{sleep, timeout},
-};
-
-use crate::test_utils::{
- handle::{ClientHandle, ServerHandle},
- target::TcpServerTarget,
- target_configure::ServerTargetConfig,
-};
-
-pub(crate) struct ExampleChallengeClientHandle;
-
-impl ClientHandle<ExampleChallengeServerHandle> for ExampleChallengeClientHandle {
- 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 {
- async fn process(mut instance: ConnectionInstance) {
- // Challenge with correct key
- let key_dir = current_dir().unwrap().join("res").join("key");
- let (result, key_id) = instance.challenge(key_dir).await.unwrap();
- assert!(result);
- assert_eq!(key_id, "test_key");
-
- // 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, key_id) = instance.challenge(key_dir).await.unwrap();
- assert!(!result);
- assert_eq!(key_id, "test_key");
-
- // 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, key_id) = instance.challenge(key_dir).await.unwrap();
- assert!(!result);
- assert_eq!(key_id, "test_key__");
-
- // Send response
- instance
- .write_text(if result { "OK" } else { "ERROR" })
- .await
- .unwrap();
- }
-}
-
-#[tokio::test]
-async fn test_connection_with_challenge_handle() -> Result<(), std::io::Error> {
- let host = "localhost:5011";
-
- // 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 test_timeout = Duration::from_secs(10);
-
- timeout(test_timeout, async { join!(future_client, future_server) })
- .await
- .map_err(|_| {
- std::io::Error::new(
- std::io::ErrorKind::TimedOut,
- format!("Test timed out after {:?}", test_timeout),
- )
- })?;
-
- Ok(())
-}
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
deleted file mode 100644
index 8c3ab01..0000000
--- a/crates/utils/tcp_connection/tcp_connection_test/src/test_connection.rs
+++ /dev/null
@@ -1,78 +0,0 @@
-use std::time::Duration;
-
-use tcp_connection::instance::ConnectionInstance;
-use tokio::{join, time::sleep};
-
-use crate::test_utils::{
- handle::{ClientHandle, ServerHandle},
- target::TcpServerTarget,
- target_configure::ServerTargetConfig,
-};
-
-pub(crate) struct ExampleClientHandle;
-
-impl ClientHandle<ExampleServerHandle> for ExampleClientHandle {
- 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 {
- 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!");
- };
- }
-}
-
-#[tokio::test]
-async fn test_connection_with_example_handle() {
- let host = "localhost:5012";
-
- // Server setup
- let Ok(server_target) =
- TcpServerTarget::<ExampleClientHandle, ExampleServerHandle>::from_domain(host).await
- else {
- panic!("Test target built failed from a domain named `{}`", host);
- };
-
- // Client setup
- let Ok(client_target) =
- TcpServerTarget::<ExampleClientHandle, ExampleServerHandle>::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;
-}
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
deleted file mode 100644
index 4237ea7..0000000
--- a/crates/utils/tcp_connection/tcp_connection_test/src/test_file_transfer.rs
+++ /dev/null
@@ -1,94 +0,0 @@
-use std::{env::current_dir, time::Duration};
-
-use tcp_connection::instance::ConnectionInstance;
-use tokio::{
- join,
- time::{sleep, timeout},
-};
-
-use crate::test_utils::{
- handle::{ClientHandle, ServerHandle},
- target::TcpServerTarget,
- target_configure::ServerTargetConfig,
-};
-
-pub(crate) struct ExampleFileTransferClientHandle;
-
-impl ClientHandle<ExampleFileTransferServerHandle> for ExampleFileTransferClientHandle {
- 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 {
- 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();
- }
-}
-
-#[tokio::test]
-async fn test_connection_with_challenge_handle() -> Result<(), std::io::Error> {
- let host = "localhost:5010";
-
- // Server setup
- let Ok(server_target) = TcpServerTarget::<
- ExampleFileTransferClientHandle,
- ExampleFileTransferServerHandle,
- >::from_domain(host)
- .await
- else {
- panic!("Test target built failed from a domain named `{}`", host);
- };
-
- // Client setup
- let Ok(client_target) = TcpServerTarget::<
- ExampleFileTransferClientHandle,
- ExampleFileTransferServerHandle,
- >::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 test_timeout = Duration::from_secs(10);
-
- timeout(test_timeout, async { join!(future_client, future_server) })
- .await
- .map_err(|_| {
- std::io::Error::new(
- std::io::ErrorKind::TimedOut,
- format!("Test timed out after {:?}", test_timeout),
- )
- })?;
-
- Ok(())
-}
diff --git a/crates/utils/tcp_connection/tcp_connection_test/src/test_msgpack.rs b/crates/utils/tcp_connection/tcp_connection_test/src/test_msgpack.rs
deleted file mode 100644
index 4c9c870..0000000
--- a/crates/utils/tcp_connection/tcp_connection_test/src/test_msgpack.rs
+++ /dev/null
@@ -1,103 +0,0 @@
-use serde::{Deserialize, Serialize};
-use std::time::Duration;
-use tcp_connection::instance::ConnectionInstance;
-use tokio::{join, time::sleep};
-
-use crate::test_utils::{
- handle::{ClientHandle, ServerHandle},
- target::TcpServerTarget,
- target_configure::ServerTargetConfig,
-};
-
-#[derive(Debug, PartialEq, Serialize, Deserialize, Default)]
-struct TestData {
- id: u32,
- name: String,
-}
-
-pub(crate) struct MsgPackClientHandle;
-
-impl ClientHandle<MsgPackServerHandle> for MsgPackClientHandle {
- async fn process(mut instance: ConnectionInstance) {
- // Test basic MessagePack serialization
- let test_data = TestData {
- id: 42,
- name: "Test MessagePack".to_string(),
- };
-
- // Write MessagePack data
- if let Err(e) = instance.write_msgpack(&test_data).await {
- panic!("Write MessagePack failed: {}", e);
- }
-
- // Read response
- let response: TestData = match instance.read_msgpack().await {
- Ok(data) => data,
- Err(e) => panic!("Read MessagePack response failed: {}", e),
- };
-
- // Verify response
- assert_eq!(response.id, test_data.id * 2);
- assert_eq!(response.name, format!("Processed: {}", test_data.name));
- }
-}
-
-pub(crate) struct MsgPackServerHandle;
-
-impl ServerHandle<MsgPackClientHandle> for MsgPackServerHandle {
- async fn process(mut instance: ConnectionInstance) {
- // Read MessagePack data
- let received_data: TestData = match instance.read_msgpack().await {
- Ok(data) => data,
- Err(_) => return,
- };
-
- // Process data
- let response = TestData {
- id: received_data.id * 2,
- name: format!("Processed: {}", received_data.name),
- };
-
- // Write response as MessagePack
- if let Err(e) = instance.write_msgpack(&response).await {
- panic!("Write MessagePack response failed: {}", e);
- }
- }
-}
-
-#[tokio::test]
-async fn test_msgpack_basic() {
- let host = "localhost:5013";
-
- // Server setup
- let Ok(server_target) =
- TcpServerTarget::<MsgPackClientHandle, MsgPackServerHandle>::from_domain(host).await
- else {
- panic!("Test target built failed from a domain named `{}`", host);
- };
-
- // Client setup
- let Ok(client_target) =
- TcpServerTarget::<MsgPackClientHandle, MsgPackServerHandle>::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;
-}
diff --git a/crates/utils/tcp_connection/tcp_connection_test/src/test_tcp_target_build.rs b/crates/utils/tcp_connection/tcp_connection_test/src/test_tcp_target_build.rs
deleted file mode 100644
index aa1ec74..0000000
--- a/crates/utils/tcp_connection/tcp_connection_test/src/test_tcp_target_build.rs
+++ /dev/null
@@ -1,32 +0,0 @@
-use crate::{
- test_connection::{ExampleClientHandle, ExampleServerHandle},
- test_utils::target::TcpServerTarget,
-};
-
-#[test]
-fn test_tcp_test_target_build() {
- let host = "127.0.0.1:8080";
-
- // Test build target by string
- let Ok(target) =
- TcpServerTarget::<ExampleClientHandle, ExampleServerHandle>::from_address_str(host)
- else {
- panic!("Test target built failed from a target addr `{}`", host);
- };
- assert_eq!(target.to_string(), "127.0.0.1:8080");
-}
-
-#[tokio::test]
-async fn test_tcp_test_target_build_domain() {
- let host = "localhost";
-
- // Test build target by DomainName and Connection
- let Ok(target) =
- TcpServerTarget::<ExampleClientHandle, ExampleServerHandle>::from_domain(host).await
- else {
- panic!("Test target built failed from a domain named `{}`", host);
- };
-
- // Test into string
- assert_eq!(target.to_string(), "127.0.0.1:8080");
-}
diff --git a/crates/utils/tcp_connection/tcp_connection_test/src/test_utils.rs b/crates/utils/tcp_connection/tcp_connection_test/src/test_utils.rs
deleted file mode 100644
index badf27d..0000000
--- a/crates/utils/tcp_connection/tcp_connection_test/src/test_utils.rs
+++ /dev/null
@@ -1,4 +0,0 @@
-pub mod handle;
-pub mod target;
-pub mod target_configure;
-pub mod target_connection;
diff --git a/crates/utils/tcp_connection/tcp_connection_test/src/test_utils/handle.rs b/crates/utils/tcp_connection/tcp_connection_test/src/test_utils/handle.rs
deleted file mode 100644
index 4f9bdbb..0000000
--- a/crates/utils/tcp_connection/tcp_connection_test/src/test_utils/handle.rs
+++ /dev/null
@@ -1,11 +0,0 @@
-use std::future::Future;
-
-use tcp_connection::instance::ConnectionInstance;
-
-pub trait ClientHandle<RequestServer> {
- fn process(instance: ConnectionInstance) -> impl Future<Output = ()> + Send;
-}
-
-pub trait ServerHandle<RequestClient> {
- fn process(instance: ConnectionInstance) -> impl Future<Output = ()> + Send;
-}
diff --git a/crates/utils/tcp_connection/tcp_connection_test/src/test_utils/target.rs b/crates/utils/tcp_connection/tcp_connection_test/src/test_utils/target.rs
deleted file mode 100644
index 8972b2a..0000000
--- a/crates/utils/tcp_connection/tcp_connection_test/src/test_utils/target.rs
+++ /dev/null
@@ -1,201 +0,0 @@
-use serde::{Deserialize, Serialize};
-use std::{
- fmt::{Display, Formatter},
- marker::PhantomData,
- net::{AddrParseError, IpAddr, Ipv4Addr, SocketAddr},
- str::FromStr,
-};
-use tokio::net::lookup_host;
-
-use crate::test_utils::{
- handle::{ClientHandle, ServerHandle},
- target_configure::{ClientTargetConfig, ServerTargetConfig},
-};
-
-const DEFAULT_PORT: u16 = 8080;
-
-#[derive(Debug, Serialize, Deserialize)]
-pub struct TcpServerTarget<Client, Server>
-where
- Client: ClientHandle<Server>,
- Server: ServerHandle<Client>,
-{
- /// Client Config
- client_cfg: Option<ClientTargetConfig>,
-
- /// Server Config
- server_cfg: Option<ServerTargetConfig>,
-
- /// Server port
- port: u16,
-
- /// Bind addr
- bind_addr: IpAddr,
-
- /// Client Phantom Data
- _client: PhantomData<Client>,
-
- /// Server Phantom Data
- _server: PhantomData<Server>,
-}
-
-impl<Client, Server> Default for TcpServerTarget<Client, Server>
-where
- Client: ClientHandle<Server>,
- Server: ServerHandle<Client>,
-{
- fn default() -> Self {
- Self {
- client_cfg: None,
- server_cfg: None,
- port: DEFAULT_PORT,
- bind_addr: IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)),
- _client: PhantomData,
- _server: PhantomData,
- }
- }
-}
-
-impl<Client, Server> From<SocketAddr> for TcpServerTarget<Client, Server>
-where
- Client: ClientHandle<Server>,
- Server: ServerHandle<Client>,
-{
- /// Convert SocketAddr to TcpServerTarget
- fn from(value: SocketAddr) -> Self {
- Self {
- port: value.port(),
- bind_addr: value.ip(),
- ..Self::default()
- }
- }
-}
-
-impl<Client, Server> From<TcpServerTarget<Client, Server>> for SocketAddr
-where
- Client: ClientHandle<Server>,
- Server: ServerHandle<Client>,
-{
- /// Convert TcpServerTarget to SocketAddr
- fn from(val: TcpServerTarget<Client, Server>) -> Self {
- SocketAddr::new(val.bind_addr, val.port)
- }
-}
-
-impl<Client, Server> Display for TcpServerTarget<Client, Server>
-where
- Client: ClientHandle<Server>,
- Server: ServerHandle<Client>,
-{
- fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
- write!(f, "{}:{}", self.bind_addr, self.port)
- }
-}
-
-impl<Client, Server> TcpServerTarget<Client, Server>
-where
- Client: ClientHandle<Server>,
- Server: ServerHandle<Client>,
-{
- /// Create target by address
- pub fn from_addr(addr: impl Into<IpAddr>, port: impl Into<u16>) -> Self {
- Self {
- port: port.into(),
- bind_addr: addr.into(),
- ..Self::default()
- }
- }
-
- /// Try to create target by string
- pub fn from_address_str<'a>(addr_str: impl Into<&'a str>) -> Result<Self, AddrParseError> {
- let socket_addr = SocketAddr::from_str(addr_str.into());
- match socket_addr {
- Ok(socket_addr) => Ok(Self::from_addr(socket_addr.ip(), socket_addr.port())),
- Err(err) => Err(err),
- }
- }
-
- /// Try to create target by domain name
- pub async fn from_domain<'a>(domain: impl Into<&'a str>) -> Result<Self, std::io::Error> {
- match domain_to_addr(domain).await {
- Ok(domain_addr) => Ok(Self::from(domain_addr)),
- Err(e) => Err(e),
- }
- }
-
- /// Set client config
- pub fn client_cfg(mut self, config: ClientTargetConfig) -> Self {
- self.client_cfg = Some(config);
- self
- }
-
- /// Set server config
- pub fn server_cfg(mut self, config: ServerTargetConfig) -> Self {
- self.server_cfg = Some(config);
- self
- }
-
- /// Add client config
- pub fn add_client_cfg(&mut self, config: ClientTargetConfig) {
- self.client_cfg = Some(config);
- }
-
- /// Add server config
- pub fn add_server_cfg(&mut self, config: ServerTargetConfig) {
- self.server_cfg = Some(config);
- }
-
- /// Get client config ref
- pub fn get_client_cfg(&self) -> Option<&ClientTargetConfig> {
- self.client_cfg.as_ref()
- }
-
- /// Get server config ref
- pub fn get_server_cfg(&self) -> Option<&ServerTargetConfig> {
- self.server_cfg.as_ref()
- }
-
- /// Get SocketAddr of TcpServerTarget
- pub fn get_addr(&self) -> SocketAddr {
- SocketAddr::new(self.bind_addr, self.port)
- }
-}
-
-/// Parse Domain Name to IpAddr via DNS
-async fn domain_to_addr<'a>(domain: impl Into<&'a str>) -> Result<SocketAddr, std::io::Error> {
- let domain = domain.into();
- let default_port: u16 = DEFAULT_PORT;
-
- if let Ok(socket_addr) = domain.parse::<SocketAddr>() {
- return Ok(match socket_addr.ip() {
- IpAddr::V4(_) => socket_addr,
- IpAddr::V6(_) => SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), socket_addr.port()),
- });
- }
-
- if let Ok(_v6_addr) = domain.parse::<std::net::Ipv6Addr>() {
- return Ok(SocketAddr::new(
- IpAddr::V4(Ipv4Addr::LOCALHOST),
- default_port,
- ));
- }
-
- let (host, port_str) = if let Some((host, port)) = domain.rsplit_once(':') {
- (host.trim_matches(|c| c == '[' || c == ']'), Some(port))
- } else {
- (domain, None)
- };
-
- let port = port_str
- .and_then(|p| p.parse::<u16>().ok())
- .map(|p| p.clamp(0, u16::MAX))
- .unwrap_or(default_port);
-
- let mut socket_iter = lookup_host((host, 0)).await?;
-
- if let Some(addr) = socket_iter.find(|addr| addr.is_ipv4()) {
- return Ok(SocketAddr::new(addr.ip(), port));
- }
-
- Ok(SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), port))
-}
diff --git a/crates/utils/tcp_connection/tcp_connection_test/src/test_utils/target_configure.rs b/crates/utils/tcp_connection/tcp_connection_test/src/test_utils/target_configure.rs
deleted file mode 100644
index d739ac9..0000000
--- a/crates/utils/tcp_connection/tcp_connection_test/src/test_utils/target_configure.rs
+++ /dev/null
@@ -1,53 +0,0 @@
-use serde::{Deserialize, Serialize};
-
-#[derive(Default, Debug, Clone, Copy, Serialize, Deserialize)]
-pub struct ServerTargetConfig {
- /// Only process a single connection, then shut down the server.
- once: bool,
-
- /// Timeout duration in milliseconds. (0 is Closed)
- timeout: u64,
-}
-
-impl ServerTargetConfig {
- /// Set `once` to True
- /// This method configures the `once` field of `ServerTargetConfig`.
- pub fn once(mut self) -> Self {
- self.once = true;
- self
- }
-
- /// Set `timeout` to the given value
- /// This method configures the `timeout` field of `ServerTargetConfig`.
- pub fn timeout(mut self, timeout: u64) -> Self {
- self.timeout = timeout;
- self
- }
-
- /// Set `once` to the given value
- /// This method configures the `once` field of `ServerTargetConfig`.
- pub fn set_once(&mut self, enable: bool) {
- self.once = enable;
- }
-
- /// Set `timeout` to the given value
- /// This method configures the `timeout` field of `ServerTargetConfig`.
- pub fn set_timeout(&mut self, timeout: u64) {
- self.timeout = timeout;
- }
-
- /// Check if the server is configured to process only a single connection.
- /// Returns `true` if the server will shut down after processing one connection.
- pub fn is_once(&self) -> bool {
- self.once
- }
-
- /// Get the current timeout value in milliseconds.
- /// Returns the timeout duration. A value of 0 indicates the connection is closed.
- pub fn get_timeout(&self) -> u64 {
- self.timeout
- }
-}
-
-#[derive(Default, Debug, Clone, Copy, Serialize, Deserialize)]
-pub struct ClientTargetConfig {}
diff --git a/crates/utils/tcp_connection/tcp_connection_test/src/test_utils/target_connection.rs b/crates/utils/tcp_connection/tcp_connection_test/src/test_utils/target_connection.rs
deleted file mode 100644
index d5bf2c3..0000000
--- a/crates/utils/tcp_connection/tcp_connection_test/src/test_utils/target_connection.rs
+++ /dev/null
@@ -1,89 +0,0 @@
-use tcp_connection::{error::TcpTargetError, instance::ConnectionInstance};
-use tokio::{
- net::{TcpListener, TcpSocket},
- spawn,
-};
-
-use crate::test_utils::{
- handle::{ClientHandle, ServerHandle},
- target::TcpServerTarget,
- target_configure::ServerTargetConfig,
-};
-
-impl<Client, Server> TcpServerTarget<Client, Server>
-where
- Client: ClientHandle<Server>,
- Server: ServerHandle<Client>,
-{
- /// Attempts to establish a connection to the TCP server.
- ///
- /// This function initiates a connection to the server address
- /// specified in the target configuration.
- ///
- /// This is a Block operation.
- pub async fn connect(&self) -> Result<(), TcpTargetError> {
- let addr = self.get_addr();
- let Ok(socket) = TcpSocket::new_v4() else {
- return Err(TcpTargetError::from("Create tcp socket failed!"));
- };
- let stream = match socket.connect(addr).await {
- Ok(stream) => stream,
- Err(e) => {
- let err = format!("Connect to `{}` failed: {}", addr, e);
- return Err(TcpTargetError::from(err));
- }
- };
- let instance = ConnectionInstance::from(stream);
- Client::process(instance).await;
- Ok(())
- }
-
- /// Attempts to establish a connection to the TCP server.
- ///
- /// This function initiates a connection to the server address
- /// specified in the target configuration.
- pub async fn listen(&self) -> Result<(), TcpTargetError> {
- let addr = self.get_addr();
- let listener = match TcpListener::bind(addr).await {
- Ok(listener) => listener,
- Err(_) => {
- let err = format!("Bind to `{}` failed", addr);
- return Err(TcpTargetError::from(err));
- }
- };
-
- let cfg: ServerTargetConfig = match self.get_server_cfg() {
- Some(cfg) => *cfg,
- None => ServerTargetConfig::default(),
- };
-
- if cfg.is_once() {
- // Process once (Blocked)
- let (stream, _) = match listener.accept().await {
- Ok(result) => result,
- Err(e) => {
- let err = format!("Accept connection failed: {}", e);
- return Err(TcpTargetError::from(err));
- }
- };
- let instance = ConnectionInstance::from(stream);
- Server::process(instance).await;
- } else {
- loop {
- // Process multiple times (Concurrent)
- let (stream, _) = match listener.accept().await {
- Ok(result) => result,
- Err(e) => {
- let err = format!("Accept connection failed: {}", e);
- return Err(TcpTargetError::from(err));
- }
- };
- let instance = ConnectionInstance::from(stream);
- spawn(async move {
- Server::process(instance).await;
- });
- }
- }
- Ok(())
- }
-}