summaryrefslogtreecommitdiffstats
path: root/third_party/rust/neqo-http3/src/frames/tests/mod.rs
blob: 33eea5497a75dffe98dbe4a8410f5c705eb01219 (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
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use std::mem;

use neqo_common::Encoder;
use neqo_crypto::AuthenticationStatus;
use neqo_transport::StreamType;
use test_fixture::{default_client, default_server, now};

use crate::frames::{
    reader::FrameDecoder, FrameReader, HFrame, StreamReaderConnectionWrapper, WebTransportFrame,
};

#[allow(clippy::many_single_char_names)]
pub(crate) fn enc_dec<T: FrameDecoder<T>>(d: &Encoder, st: &str, remaining: usize) -> T {
    // For data, headers and push_promise we do not read all bytes from the buffer
    let d2 = Encoder::from_hex(st);
    assert_eq!(d.as_ref(), &d2.as_ref()[..d.as_ref().len()]);

    let mut conn_c = default_client();
    let mut conn_s = default_server();
    let out = conn_c.process(None, now());
    let out = conn_s.process(out.as_dgram_ref(), now());
    let out = conn_c.process(out.as_dgram_ref(), now());
    mem::drop(conn_s.process(out.as_dgram_ref(), now()));
    conn_c.authenticated(AuthenticationStatus::Ok, now());
    let out = conn_c.process(None, now());
    mem::drop(conn_s.process(out.as_dgram_ref(), now()));

    // create a stream
    let stream_id = conn_s.stream_create(StreamType::BiDi).unwrap();

    let mut fr: FrameReader = FrameReader::new();

    // conver string into u8 vector
    let buf = Encoder::from_hex(st);
    conn_s.stream_send(stream_id, buf.as_ref()).unwrap();
    let out = conn_s.process(None, now());
    mem::drop(conn_c.process(out.as_dgram_ref(), now()));

    let (frame, fin) = fr
        .receive::<T>(&mut StreamReaderConnectionWrapper::new(
            &mut conn_c,
            stream_id,
        ))
        .unwrap();
    assert!(!fin);
    assert!(frame.is_some());

    // Check remaining data.
    let mut buf = [0_u8; 100];
    let (amount, _) = conn_c.stream_recv(stream_id, &mut buf).unwrap();
    assert_eq!(amount, remaining);

    frame.unwrap()
}

pub fn enc_dec_hframe(f: &HFrame, st: &str, remaining: usize) {
    let mut d = Encoder::default();

    f.encode(&mut d);

    let frame = enc_dec::<HFrame>(&d, st, remaining);

    assert_eq!(*f, frame);
}

pub fn enc_dec_wtframe(f: &WebTransportFrame, st: &str, remaining: usize) {
    let mut d = Encoder::default();

    f.encode(&mut d);

    let frame = enc_dec::<WebTransportFrame>(&d, st, remaining);

    assert_eq!(*f, frame);
}

mod hframe;
mod reader;
mod wtframe;