aboutsummaryrefslogtreecommitdiff
path: root/tests/net_panic.rs
blob: fc2209ad4b0a65aac3781eb5ee691972cd678200 (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
#![warn(rust_2018_idioms)]
#![cfg(all(feature = "full", not(tokio_wasi)))]

use std::error::Error;
use tokio::net::{TcpListener, TcpStream};
use tokio::runtime::{Builder, Runtime};

mod support {
    pub mod panic;
}
use support::panic::test_panic;

#[test]
fn udp_socket_from_std_panic_caller() -> Result<(), Box<dyn Error>> {
    use std::net::SocketAddr;
    use tokio::net::UdpSocket;

    let addr = "127.0.0.1:0".parse::<SocketAddr>().unwrap();
    let std_sock = std::net::UdpSocket::bind(addr).unwrap();
    std_sock.set_nonblocking(true).unwrap();

    let panic_location_file = test_panic(|| {
        let rt = runtime_without_io();
        rt.block_on(async {
            let _sock = UdpSocket::from_std(std_sock);
        });
    });

    // The panic location should be in this file
    assert_eq!(&panic_location_file.unwrap(), file!());

    Ok(())
}

#[test]
fn tcp_listener_from_std_panic_caller() -> Result<(), Box<dyn Error>> {
    let std_listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
    std_listener.set_nonblocking(true).unwrap();

    let panic_location_file = test_panic(|| {
        let rt = runtime_without_io();
        rt.block_on(async {
            let _ = TcpListener::from_std(std_listener);
        });
    });

    // The panic location should be in this file
    assert_eq!(&panic_location_file.unwrap(), file!());

    Ok(())
}

#[test]
fn tcp_stream_from_std_panic_caller() -> Result<(), Box<dyn Error>> {
    let std_listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();

    let std_stream = std::net::TcpStream::connect(std_listener.local_addr().unwrap()).unwrap();
    std_stream.set_nonblocking(true).unwrap();

    let panic_location_file = test_panic(|| {
        let rt = runtime_without_io();
        rt.block_on(async {
            let _ = TcpStream::from_std(std_stream);
        });
    });

    // The panic location should be in this file
    assert_eq!(&panic_location_file.unwrap(), file!());

    Ok(())
}

#[test]
#[cfg(unix)]
fn unix_listener_bind_panic_caller() -> Result<(), Box<dyn Error>> {
    use tokio::net::UnixListener;

    let dir = tempfile::tempdir().unwrap();
    let sock_path = dir.path().join("socket");

    let panic_location_file = test_panic(|| {
        let rt = runtime_without_io();
        rt.block_on(async {
            let _ = UnixListener::bind(&sock_path);
        });
    });

    // The panic location should be in this file
    assert_eq!(&panic_location_file.unwrap(), file!());

    Ok(())
}

#[test]
#[cfg(unix)]
fn unix_listener_from_std_panic_caller() -> Result<(), Box<dyn Error>> {
    use tokio::net::UnixListener;

    let dir = tempfile::tempdir().unwrap();
    let sock_path = dir.path().join("socket");
    let std_listener = std::os::unix::net::UnixListener::bind(&sock_path).unwrap();

    let panic_location_file = test_panic(|| {
        let rt = runtime_without_io();
        rt.block_on(async {
            let _ = UnixListener::from_std(std_listener);
        });
    });

    // The panic location should be in this file
    assert_eq!(&panic_location_file.unwrap(), file!());

    Ok(())
}

#[test]
#[cfg(unix)]
fn unix_stream_from_std_panic_caller() -> Result<(), Box<dyn Error>> {
    use tokio::net::UnixStream;

    let dir = tempfile::tempdir().unwrap();
    let sock_path = dir.path().join("socket");
    let _std_listener = std::os::unix::net::UnixListener::bind(&sock_path).unwrap();
    let std_stream = std::os::unix::net::UnixStream::connect(&sock_path).unwrap();

    let panic_location_file = test_panic(|| {
        let rt = runtime_without_io();
        rt.block_on(async {
            let _ = UnixStream::from_std(std_stream);
        });
    });

    // The panic location should be in this file
    assert_eq!(&panic_location_file.unwrap(), file!());

    Ok(())
}

#[test]
#[cfg(unix)]
fn unix_datagram_from_std_panic_caller() -> Result<(), Box<dyn Error>> {
    use std::os::unix::net::UnixDatagram as StdUDS;
    use tokio::net::UnixDatagram;

    let dir = tempfile::tempdir().unwrap();
    let sock_path = dir.path().join("socket");

    // Bind the socket to a filesystem path
    // /let socket_path = tmp.path().join("socket");
    let std_socket = StdUDS::bind(&sock_path).unwrap();
    std_socket.set_nonblocking(true).unwrap();

    let panic_location_file = test_panic(move || {
        let rt = runtime_without_io();
        rt.block_on(async {
            let _ = UnixDatagram::from_std(std_socket);
        });
    });

    // The panic location should be in this file
    assert_eq!(&panic_location_file.unwrap(), file!());

    Ok(())
}

#[test]
#[cfg(windows)]
fn server_options_max_instances_panic_caller() -> Result<(), Box<dyn Error>> {
    use tokio::net::windows::named_pipe::ServerOptions;

    let panic_location_file = test_panic(move || {
        let rt = runtime_without_io();
        rt.block_on(async {
            let mut options = ServerOptions::new();
            options.max_instances(255);
        });
    });

    // The panic location should be in this file
    assert_eq!(&panic_location_file.unwrap(), file!());

    Ok(())
}

// Runtime without `enable_io` so it has no IO driver set.
fn runtime_without_io() -> Runtime {
    Builder::new_current_thread().build().unwrap()
}