summaryrefslogtreecommitdiff
path: root/crates/utils/tcp_connection/tcp_connection_test/src/test_file_transfer.rs
blob: 4237ea74aa99ee8018bd53ecde40b742e6df6494 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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(())
}