summaryrefslogtreecommitdiff
path: root/crates/tor-rtcompat/src/test.rs
blob: b8f360fef2dd6b9d4df16b1662c1d66ead6c87ec (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#![allow(clippy::unwrap_used, clippy::unnecessary_wraps)]
use crate::Runtime;
use crate::SleepProviderExt;

use crate::traits::*;

use futures::io::{AsyncReadExt, AsyncWriteExt};
use futures::stream::StreamExt;
use std::io::Result as IoResult;
use std::net::{Ipv4Addr, SocketAddrV4};
use std::time::{Duration, Instant, SystemTime};

// Test "sleep" with a tiny delay, and make sure that at least that
// much delay happens.
fn small_delay<R: Runtime>(runtime: &R) -> IoResult<()> {
    let rt = runtime.clone();
    runtime.block_on(async {
        let i1 = Instant::now();
        let one_msec = Duration::from_millis(1);
        rt.sleep(one_msec).await;
        let i2 = Instant::now();
        assert!(i2 >= i1 + one_msec);
    });
    Ok(())
}

// Try a timeout operation that will succeed.
fn small_timeout_ok<R: Runtime>(runtime: &R) -> IoResult<()> {
    let rt = runtime.clone();
    runtime.block_on(async {
        let one_day = Duration::from_secs(86400);
        let outcome = rt.timeout(one_day, async { 413_u32 }).await;
        assert_eq!(outcome, Ok(413));
    });
    Ok(())
}

// Try a timeout operation that will time out.
fn small_timeout_expire<R: Runtime>(runtime: &R) -> IoResult<()> {
    use futures::future::pending;

    let rt = runtime.clone();
    runtime.block_on(async {
        let one_micros = Duration::from_micros(1);
        let outcome = rt.timeout(one_micros, pending::<()>()).await;
        assert_eq!(outcome, Err(crate::TimeoutError));
        assert_eq!(
            outcome.err().unwrap().to_string(),
            "Timeout expired".to_string()
        );
    });
    Ok(())
}
// Try a little wallclock delay.
//
// NOTE: This test will fail if the clock jumps a lot while it's
// running.  We should use simulated time instead.
fn tiny_wallclock<R: Runtime>(runtime: &R) -> IoResult<()> {
    let rt = runtime.clone();
    runtime.block_on(async {
        let i1 = Instant::now();
        let now = SystemTime::now();
        let one_millis = Duration::from_millis(1);
        let one_millis_later = now + one_millis;

        rt.sleep_until_wallclock(one_millis_later).await;

        let i2 = Instant::now();
        let newtime = SystemTime::now();
        assert!(newtime >= one_millis_later);
        assert!(i2 - i1 >= one_millis);
    });
    Ok(())
}

// Try connecting to ourself and sending a little data.
//
// NOTE: requires Ipv4 localhost.
fn self_connect<R: Runtime>(runtime: &R) -> IoResult<()> {
    let localhost = SocketAddrV4::new(Ipv4Addr::LOCALHOST, 0);
    let rt1 = runtime.clone();

    let listener = runtime.block_on(rt1.listen(&(localhost.into())))?;
    let addr = listener.local_addr()?;

    runtime.block_on(async {
        let task1 = async {
            let mut buf = vec![0_u8; 11];
            let (mut con, _addr) = listener.accept().await?;
            con.read_exact(&mut buf[..]).await?;
            IoResult::Ok(buf)
        };
        let task2 = async {
            let mut con = rt1.connect(&addr).await?;
            con.write_all(b"Hello world").await?;
            con.flush().await?;
            IoResult::Ok(())
        };

        let (data, send_r) = futures::join!(task1, task2);
        send_r?;

        assert_eq!(&data?[..], b"Hello world");

        Ok(())
    })
}

// Try out our incoming connection stream code.
//
// We launch a few connections and make sure that we can read data on
// them.
fn listener_stream<R: Runtime>(runtime: &R) -> IoResult<()> {
    let localhost = SocketAddrV4::new(Ipv4Addr::LOCALHOST, 0);
    let rt1 = runtime.clone();

    let listener = runtime.block_on(rt1.listen(&(localhost.into()))).unwrap();
    let addr = listener.local_addr().unwrap();
    let mut stream = listener.incoming();

    runtime.block_on(async {
        let task1 = async {
            let mut n = 0_u32;
            loop {
                let (mut con, _addr) = stream.next().await.unwrap()?;
                let mut buf = vec![0_u8; 11];
                con.read_exact(&mut buf[..]).await?;
                n += 1;
                if &buf[..] == b"world done!" {
                    break IoResult::Ok(n);
                }
            }
        };
        let task2 = async {
            for _ in 0_u8..5 {
                let mut con = rt1.connect(&addr).await?;
                con.write_all(b"Hello world").await?;
                con.flush().await?;
            }
            let mut con = rt1.connect(&addr).await?;
            con.write_all(b"world done!").await?;
            con.flush().await?;
            con.close().await?;
            IoResult::Ok(())
        };

        let (n, send_r) = futures::join!(task1, task2);
        send_r?;

        assert_eq!(n?, 6);

        Ok(())
    })
}

// Try listening on an address and connecting there, except using TLS.
//
// Note that since we don't have async tls server support yet, I'm just
// going to use a thread.
fn simple_tls<R: Runtime>(runtime: &R) -> IoResult<()> {
    /*
     A simple expired self-signed rsa-2048 certificate.

     Generated using OpenSSL 1.1.1k with:

     openssl genpkey -algorithm RSA > test.key
     openssl req -new -out - -key test.key > test.csr
     openssl x509 -in test.csr -out test.crt -req -signkey test.key -days 0
     openssl pkcs12 -export -certpbe PBE-SHA1-3DES -out test.pfx -inkey test.key -in test.crt
    */
    static PFX_ID: &[u8] = include_bytes!("test.pfx");
    // Note that we need to set a password on the pkcs12 file, since apparently
    // OSX doesn't support pkcs12 with empty passwords. (That was arti#111).
    static PFX_PASSWORD: &str = "abc";

    let localhost = SocketAddrV4::new(Ipv4Addr::LOCALHOST, 0);
    let listener = std::net::TcpListener::bind(localhost)?;
    let addr = listener.local_addr()?;

    let identity = native_tls::Identity::from_pkcs12(PFX_ID, PFX_PASSWORD).unwrap();

    // See note on function for why we're using a thread here.
    let th = std::thread::spawn(move || {
        // Accept a single TLS connection and run an echo server
        use std::io::{Read, Write};
        let acceptor = native_tls::TlsAcceptor::new(identity).unwrap();
        let (con, _addr) = listener.accept()?;
        let mut con = acceptor.accept(con).unwrap();
        let mut buf = [0_u8; 16];
        loop {
            let n = con.read(&mut buf)?;
            if n == 0 {
                break;
            }
            con.write_all(&buf[..n])?;
        }
        IoResult::Ok(())
    });

    let connector = runtime.tls_connector();

    runtime.block_on(async {
        let text = b"I Suddenly Dont Understand Anything";
        let mut buf = vec![0_u8; text.len()];
        let mut conn = connector.connect_unvalidated(&addr, "Kan.Aya").await?;
        assert!(conn.peer_certificate()?.is_some());
        conn.write_all(text).await?;
        conn.flush().await?;
        conn.read_exact(&mut buf[..]).await?;
        assert_eq!(&buf[..], text);
        conn.close().await?;
        IoResult::Ok(())
    })?;

    th.join().unwrap()?;
    IoResult::Ok(())
}

macro_rules! runtime_tests {
    { $($id:ident),* $(,)? } => {
        #[cfg(feature="tokio")]
        mod tokio_runtime_tests {
            use std::io::Result as IoResult;
            $(
                #[test]
                fn $id() -> IoResult<()> {
                    super::$id(&crate::tokio::create_runtime()?)
                }
            )*
        }
        #[cfg(feature="async-std")]
        mod async_std_runtime_tests {
            use std::io::Result as IoResult;
            $(
                #[test]
                fn $id() -> IoResult<()> {
                    super::$id(&crate::async_std::create_runtime()?)
                }
            )*
        }
    }
}

runtime_tests! {
    small_delay,
    small_timeout_ok,
    small_timeout_expire,
    tiny_wallclock,
    self_connect,
    listener_stream,
    simple_tls,
}