aboutsummaryrefslogtreecommitdiff
path: root/src/frame/tests.rs
blob: 5029ea3f7d2f82612a39cbb9fe7f02e6b062f85a (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
use tokio_test::assert_ready;
use tokio_test::task;
use tokio_util::codec::*;

use futures::{pin_mut, Stream};

use crate::frame::*;

macro_rules! assert_next_eq {
    ($io:ident, $expect:expr) => {{
        task::spawn(()).enter(|cx, _| {
            let res = assert_ready!($io.as_mut().poll_next(cx));
            match res {
                Some(Ok(v)) => assert_eq!(v, $expect.as_ref()),
                Some(Err(e)) => panic!("error = {:?}", e),
                None => panic!("none"),
            }
        });
    }};
}

// macro_rules! assert_next_pending {
//     ($io:ident) => {{
//         task::spawn(()).enter(|cx, _| match $io.as_mut().poll_next(cx) {
//             Ready(Some(Ok(v))) => panic!("value = {:?}", v),
//             Ready(Some(Err(e))) => panic!("error = {:?}", e),
//             Ready(None) => panic!("done"),
//             Pending => {}
//         });
//     }};
// }

// macro_rules! assert_next_err {
//     ($io:ident) => {{
//         task::spawn(()).enter(|cx, _| match $io.as_mut().poll_next(cx) {
//             Ready(Some(Ok(v))) => panic!("value = {:?}", v),
//             Ready(Some(Err(_))) => {}
//             Ready(None) => panic!("done"),
//             Pending => panic!("pending"),
//         });
//     }};
// }

macro_rules! assert_done {
    ($io:ident) => {{
        task::spawn(()).enter(|cx, _| {
            let res = assert_ready!($io.as_mut().poll_next(cx));
            match res {
                Some(Ok(v)) => panic!("value = {:?}", v),
                Some(Err(e)) => panic!("error = {:?}", e),
                None => {}
            }
        });
    }};
}

// ======================
// =====    Test    =====
// ======================

use tokio_test::io::Builder;

#[test]
fn quasselcodec_set_options() {
    use flate2::Compression;

    let mut codec = QuasselCodec::default();

    assert_eq!(codec.max_frame_length(), 64 * 1024 * 1024);
    assert_eq!(codec.compression(), false);
    assert_eq!(codec.compression_level(), Compression::default());

    codec.set_max_frame_length(42);
    codec.set_compression(true);
    codec.set_compression_level(Compression::best());

    assert_eq!(codec.max_frame_length(), 42);
    assert_eq!(codec.compression(), true);
    assert_eq!(codec.compression_level(), Compression::best());
}

#[test]
fn quasselcodec_write_oversized() {
    use futures::sink::SinkExt;

    let mut io = Framed::new(
        Builder::new()
            .write_error(std::io::Error::new(
                std::io::ErrorKind::InvalidInput,
                QuasselCodecError { _priv: () },
            ))
            .build(),
        QuasselCodec::builder().max_frame_length(5).new_codec(),
    );

    tokio_test::block_on(async move {
        let res = io.send(b"abcdefghi".to_vec()).await.map_err(|e| e.kind());
        let want = Err(std::io::ErrorKind::InvalidInput);

        assert_eq!(want, res);
    });
}

#[test]
fn quasselcodec_read_oversized() {
    use futures::stream::StreamExt;

    let mut io = FramedRead::new(
        Builder::new().read(b"\x00\x00\x00\x09abcdefghi").build(),
        QuasselCodec::builder().max_frame_length(5).new_codec(),
    );

    tokio_test::block_on(async move {
        let res = io.next().await.unwrap().map_err(|e| e.kind());
        let want = Err(std::io::ErrorKind::InvalidData);

        assert_eq!(want, res);
    });
}

#[test]
pub fn read_single_frame() {
    let io = FramedRead::new(
        Builder::new().read(b"\x00\x00\x00\x09abcdefghi").build(),
        QuasselCodec::new(),
    );
    pin_mut!(io);

    assert_next_eq!(io, b"abcdefghi");
    assert_done!(io);
}

#[test]
pub fn read_multi_frame() {
    let mut d: Vec<u8> = vec![];
    d.extend_from_slice(b"\x00\x00\x00\x09abcdefghi");
    d.extend_from_slice(b"\x00\x00\x00\x03123");
    d.extend_from_slice(b"\x00\x00\x00\x0bhello world");

    let io = FramedRead::new(Builder::new().read(&d).build(), QuasselCodec::new());
    pin_mut!(io);

    assert_next_eq!(io, b"abcdefghi");
    assert_next_eq!(io, b"123");
    assert_next_eq!(io, b"hello world");
    assert_done!(io);
}

#[test]
pub fn read_single_frame_compressed() {
    let io = FramedRead::new(
        Builder::new().read(b"\x78\x9c\x63\x60\x60\xe0\x4c\x4c\x4a\x4e\x49\x4d\x4b\xcf\xc8\x04\x00\x11\xec\x03\x97").build(),
        QuasselCodec::builder().compression(true).new_codec(),
    );
    pin_mut!(io);

    assert_next_eq!(io, b"abcdefghi");
    assert_done!(io);
}

#[test]
fn write_single_frame() {
    use futures::sink::SinkExt;

    let mut io = Framed::new(
        Builder::new().write(b"\x00\x00\x00\x09abcdefghi").build(),
        QuasselCodec::new(),
    );

    tokio_test::block_on(async move {
        io.send(b"abcdefghi".to_vec()).await.unwrap();
    });
}

#[test]
fn write_multi_frame() {
    use futures::sink::SinkExt;

    let mut io = Framed::new(
        Builder::new()
            .write(b"\x00\x00\x00\x09abcdefghi")
            .write(b"\x00\x00\x00\x03123")
            .write(b"\x00\x00\x00\x0bhello world")
            .build(),
        QuasselCodec::new(),
    );

    tokio_test::block_on(async move {
        io.send(b"abcdefghi".to_vec()).await.unwrap();
        io.send(b"123".to_vec()).await.unwrap();
        io.send(b"hello world".to_vec()).await.unwrap();
    });
}

#[test]
fn write_single_frame_compressed() {
    use futures::sink::SinkExt;

    let mut io = Framed::new(
        Builder::new()
            .write(&[120, 156, 98, 96, 96, 224, 76, 76, 74, 78, 73, 77, 75])
            .build(),
        QuasselCodec::builder().compression(true).new_codec(),
    );

    tokio_test::block_on(async move {
        io.send(b"abcdefghi".to_vec()).await.unwrap();
    });
}