summaryrefslogtreecommitdiffstats
path: root/vendor/tokio/tests/io_panic.rs
blob: 922d7c076e722773e210cdcbc5bc3c17d52001b5 (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
#![warn(rust_2018_idioms)]
#![cfg(all(feature = "full", not(tokio_wasi)))] // Wasi does not support panic recovery

use std::task::{Context, Poll};
use std::{error::Error, pin::Pin};
use tokio::io::{self, split, AsyncRead, AsyncWrite, ReadBuf};

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

struct RW;

impl AsyncRead for RW {
    fn poll_read(
        self: Pin<&mut Self>,
        _cx: &mut Context<'_>,
        buf: &mut ReadBuf<'_>,
    ) -> Poll<io::Result<()>> {
        buf.put_slice(&[b'z']);
        Poll::Ready(Ok(()))
    }
}

impl AsyncWrite for RW {
    fn poll_write(
        self: Pin<&mut Self>,
        _cx: &mut Context<'_>,
        _buf: &[u8],
    ) -> Poll<Result<usize, io::Error>> {
        Poll::Ready(Ok(1))
    }

    fn poll_flush(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Result<(), io::Error>> {
        Poll::Ready(Ok(()))
    }

    fn poll_shutdown(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Result<(), io::Error>> {
        Poll::Ready(Ok(()))
    }
}

#[cfg(unix)]
mod unix {
    use std::os::unix::prelude::{AsRawFd, RawFd};

    pub struct MockFd;

    impl AsRawFd for MockFd {
        fn as_raw_fd(&self) -> RawFd {
            0
        }
    }
}

#[test]
fn read_buf_initialize_unfilled_to_panic_caller() -> Result<(), Box<dyn Error>> {
    let panic_location_file = test_panic(|| {
        let mut buffer = Vec::<u8>::new();
        let mut read_buf = ReadBuf::new(&mut buffer);

        read_buf.initialize_unfilled_to(2);
    });

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

    Ok(())
}

#[test]
fn read_buf_advance_panic_caller() -> Result<(), Box<dyn Error>> {
    let panic_location_file = test_panic(|| {
        let mut buffer = Vec::<u8>::new();
        let mut read_buf = ReadBuf::new(&mut buffer);

        read_buf.advance(2);
    });

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

    Ok(())
}

#[test]
fn read_buf_set_filled_panic_caller() -> Result<(), Box<dyn Error>> {
    let panic_location_file = test_panic(|| {
        let mut buffer = Vec::<u8>::new();
        let mut read_buf = ReadBuf::new(&mut buffer);

        read_buf.set_filled(2);
    });

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

    Ok(())
}

#[test]
fn read_buf_put_slice_panic_caller() -> Result<(), Box<dyn Error>> {
    let panic_location_file = test_panic(|| {
        let mut buffer = Vec::<u8>::new();
        let mut read_buf = ReadBuf::new(&mut buffer);

        let new_slice = [0x40_u8, 0x41_u8];

        read_buf.put_slice(&new_slice);
    });

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

    Ok(())
}

#[test]
fn unsplit_panic_caller() -> Result<(), Box<dyn Error>> {
    let panic_location_file = test_panic(|| {
        let (r1, _w1) = split(RW);
        let (_r2, w2) = split(RW);
        r1.unsplit(w2);
    });

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

    Ok(())
}

#[test]
#[cfg(unix)]
fn async_fd_new_panic_caller() -> Result<(), Box<dyn Error>> {
    use tokio::io::unix::AsyncFd;
    use tokio::runtime::Builder;

    let panic_location_file = test_panic(|| {
        // Runtime without `enable_io` so it has no IO driver set.
        let rt = Builder::new_current_thread().build().unwrap();
        rt.block_on(async {
            let fd = unix::MockFd;

            let _ = AsyncFd::new(fd);
        });
    });

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

    Ok(())
}

#[test]
#[cfg(unix)]
fn async_fd_with_interest_panic_caller() -> Result<(), Box<dyn Error>> {
    use tokio::io::unix::AsyncFd;
    use tokio::io::Interest;
    use tokio::runtime::Builder;

    let panic_location_file = test_panic(|| {
        // Runtime without `enable_io` so it has no IO driver set.
        let rt = Builder::new_current_thread().build().unwrap();
        rt.block_on(async {
            let fd = unix::MockFd;

            let _ = AsyncFd::with_interest(fd, Interest::READABLE);
        });
    });

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

    Ok(())
}