Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_jdat/tests/pipe.rs

5.8 KiB, 1 run

created by r1870400018:75875, which is this file's identity for as long as the history lasts, whatever it is later renamed to

download · who wrote it · its history

1//! `pipe`, length-prefixed BDAT frames over a tokio byte stream: whole frames go through in order,
2//! a declared length over the ceiling is refused before a byte of body is read or a buffer sized,
3//! and a stream that ends part-way through a frame is a named fault rather than a short value.
4#![cfg(feature = "pipe")]
5
6use oxedyne_fe2o3_core::prelude::*;
7use oxedyne_fe2o3_jdat::{
8 prelude::*,
9 pipe::{
10 FrameReader,
11 FrameWriter,
12 PipeFault,
13 LEN_PREFIX,
14 },
15};
16
17use tokio::{
18 io::AsyncWriteExt,
19 net::UnixStream,
20 time::{
21 timeout,
22 Duration,
23 },
24};
25
26
27const MAX: usize = 1024 * 1024;
28
29fn pair() -> (UnixStream, UnixStream) {
30 match UnixStream::pair() {
31 Ok(p) => p,
32 Err(e) => panic!("A socketpair must open: {}", e),
33 }
34}
35
36fn nth(i: usize) -> Dat {
37 let mut m = DaticleMap::new();
38 m.insert(dat!("i"), dat!(i as u64));
39 m.insert(dat!("s"), dat!(fmt!("frame {}", i)));
40 m.insert(dat!("b"), Dat::BU64(vec![(i % 251) as u8; i * 37 % 4099]));
41 Dat::Map(m)
42}
43
44fn fault_of(r: Outcome<Option<Dat>>) -> Option<PipeFault> {
45 match r {
46 Ok(v) => panic!("A fault was expected and the read gave {:?}.", v),
47 Err(e) => PipeFault::of(&e),
48 }
49}
50
51#[tokio::test]
52async fn test_thousand_frames_round_trip_over_a_socketpair_00() -> Outcome<()> {
53 let (a, b) = pair();
54 let mut w = FrameWriter::new(a, MAX);
55 let mut r = FrameReader::new(b, MAX);
56 let send = tokio::spawn(async move {
57 for i in 0..1000 {
58 res!(w.write(&nth(i)).await);
59 }
60 Ok::<(), Error<ErrTag>>(())
61 });
62 for i in 0..1000 {
63 match res!(r.read().await) {
64 Some(d) => assert_eq!(d, nth(i), "frame {} must arrive as sent", i),
65 None => panic!("The stream ended after {} of 1000 frames.", i),
66 }
67 }
68 match send.await {
69 Ok(o) => res!(o),
70 Err(e) => panic!("The sender must finish: {}", e),
71 }
72 // The writer is gone, so the next read is a clean end between frames.
73 assert_eq!(res!(r.read().await), None);
74 Ok(())
75}
76
77#[tokio::test]
78async fn test_over_max_header_refused_without_allocating_00() -> Outcome<()> {
79 let (mut a, b) = pair();
80 let mut r = FrameReader::new(b, MAX);
81 // A prefix promising 4 GiB, and no body behind it on a stream left open: a reader that sized a
82 // buffer or waited for the body would hang or fail the allocation, not answer.
83 if let Err(e) = a.write_all(&u32::MAX.to_be_bytes()).await {
84 panic!("{}", e);
85 }
86 let got = match timeout(Duration::from_secs(5), r.read()).await {
87 Ok(g) => g,
88 Err(_) => panic!("An over-max prefix must be refused at once, not waited on."),
89 };
90 assert_eq!(fault_of(got), Some(PipeFault::TooBig));
91 // One byte over the ceiling is refused the same way.
92 let (mut a, b) = pair();
93 let mut r = FrameReader::new(b, MAX);
94 if let Err(e) = a.write_all(&((MAX + 1) as u32).to_be_bytes()).await {
95 panic!("{}", e);
96 }
97 assert_eq!(fault_of(r.read().await), Some(PipeFault::TooBig));
98 drop(a);
99 Ok(())
100}
101
102#[tokio::test]
103async fn test_eof_mid_frame_is_a_typed_error_00() -> Outcome<()> {
104 // Part-way through the body.
105 let (mut a, b) = pair();
106 let mut r = FrameReader::new(b, MAX);
107 let body = res!(nth(7).as_bytes());
108 if let Err(e) = a.write_all(&(body.len() as u32).to_be_bytes()).await { panic!("{}", e); }
109 if let Err(e) = a.write_all(&body[..body.len() / 2]).await { panic!("{}", e); }
110 drop(a);
111 assert_eq!(fault_of(r.read().await), Some(PipeFault::Truncated));
112 // Part-way through the prefix.
113 let (mut a, b) = pair();
114 let mut r = FrameReader::new(b, MAX);
115 if let Err(e) = a.write_all(&[0u8; LEN_PREFIX - 2]).await { panic!("{}", e); }
116 drop(a);
117 assert_eq!(fault_of(r.read().await), Some(PipeFault::ShortPrefix));
118 // After a whole frame, a torn second one still surfaces only as the fault.
119 let (a, b) = pair();
120 let mut w = FrameWriter::new(a, MAX);
121 let mut r = FrameReader::new(b, MAX);
122 res!(w.write(&nth(1)).await);
123 let mut a = w.into_inner();
124 if let Err(e) = a.write_all(&(body.len() as u32).to_be_bytes()).await { panic!("{}", e); }
125 if let Err(e) = a.write_all(&body[..3]).await { panic!("{}", e); }
126 drop(a);
127 assert_eq!(res!(r.read().await), Some(nth(1)));
128 assert_eq!(fault_of(r.read().await), Some(PipeFault::Truncated));
129 Ok(())
130}
131
132#[tokio::test]
133async fn test_writer_refuses_over_max_before_writing_00() -> Outcome<()> {
134 let (a, b) = pair();
135 let mut w = FrameWriter::new(a, 64);
136 let mut r = FrameReader::new(b, 64);
137 let big = Dat::BU64(vec![0u8; 65]);
138 match w.write(&big).await {
139 Ok(()) => panic!("A frame over the ceiling must be refused."),
140 Err(e) => assert_eq!(PipeFault::of(&e), Some(PipeFault::TooBig)),
141 }
142 res!(w.write(&dat!("small")).await);
143 drop(w);
144 // Nothing of the refused frame reached the stream.
145 assert_eq!(res!(r.read().await), Some(dat!("small")));
146 assert_eq!(res!(r.read().await), None);
147 Ok(())
148}
149
150#[tokio::test]
151async fn test_body_that_is_not_one_value_is_refused_00() -> Outcome<()> {
152 let (mut a, b) = pair();
153 let mut r = FrameReader::new(b, MAX);
154 // Two values in one frame: the frame is one value, so the trailing one is an error.
155 let mut body = res!(dat!(1u8).as_bytes());
156 body.extend(res!(dat!(2u8).as_bytes()));
157 if let Err(e) = a.write_all(&(body.len() as u32).to_be_bytes()).await { panic!("{}", e); }
158 if let Err(e) = a.write_all(&body).await { panic!("{}", e); }
159 // A body that is not BDAT at all.
160 if let Err(e) = a.write_all(&3u32.to_be_bytes()).await { panic!("{}", e); }
161 if let Err(e) = a.write_all(&[0xff, 0xff, 0xff]).await { panic!("{}", e); }
162 drop(a);
163 assert_eq!(fault_of(r.read().await), Some(PipeFault::Undecodable));
164 assert_eq!(fault_of(r.read().await), Some(PipeFault::Undecodable));
165 Ok(())
166}