Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_jdat/tests/bdat_width.rs

6.9 KiB, 2 runs

created by r1870400018:67478, 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//! A fixed-width byte variant refuses a payload longer than its length field can state.
2//!
3//! `Dat::BU8` writes its length in one byte, `BU16` in two and `BU32` in four. The encoder once
4//! cut the length to that width and wrote the whole payload after it, so a 300-byte `BU8` was
5//! written with the length 44 and read back as 44 bytes: no error on either side, and the bytes
6//! after the cut were taken for the next item. These tests require the encoder to refuse, at the
7//! width's boundary, and `Dat::wrap_dat` to choose a variant that always fits.
8//!
9//! Each test names the fault it holds, and turns red when that fault is put back.
10
11use oxedyne_fe2o3_core::prelude::*;
12use oxedyne_fe2o3_jdat::prelude::*;
13
14
15fn payload(n: usize) -> Vec<u8> {
16 (0..n).map(|i| (i % 251) as u8).collect()
17}
18
19fn encode(dat: &Dat) -> Outcome<Vec<u8>> {
20 dat.to_bytes(Vec::new())
21}
22
23fn round_trip(dat: &Dat) -> Outcome<Dat> {
24 let buf = res!(encode(dat));
25 let (back, used) = res!(Dat::from_bytes(&buf));
26 if used != buf.len() {
27 return Err(err!(
28 "Decoding {} encoded bytes used {}.", buf.len(), used;
29 Test, Mismatch, Size));
30 }
31 Ok(back)
32}
33
34/// The message of the refusal, or an error when the encoder accepted the value.
35fn refusal(dat: &Dat) -> Outcome<String> {
36 match encode(dat) {
37 Ok(buf) => Err(err!(
38 "The encoder accepted a payload its length field cannot state, writing {} bytes.",
39 buf.len();
40 Test, Invalid, Data)),
41 Err(e) => Ok(fmt!("{}", e)),
42 }
43}
44
45fn check_names(msg: &str, parts: &[&str]) -> Outcome<()> {
46 for p in parts {
47 if !msg.contains(p) {
48 return Err(err!(
49 "The refusal '{}' does not name '{}'.", msg, p;
50 Test, Missing, Data));
51 }
52 }
53 Ok(())
54}
55
56
57// A BU8 length field holds 255.
58
59#[test]
60fn bu8_holds_255_and_refuses_256() -> Outcome<()> {
61 let full = Dat::BU8(payload(255));
62 let buf = res!(encode(&full));
63 assert_eq!(buf[1], 255, "the length byte of a 255-byte BU8");
64 assert_eq!(buf.len(), 1 + 1 + 255);
65 assert_eq!(res!(round_trip(&full)), full);
66
67 let msg = res!(refusal(&Dat::BU8(payload(256))));
68 res!(check_names(&msg, &["BU8", "256", "255"]));
69 Ok(())
70}
71
72#[test]
73fn bu8_refuses_300_bytes_that_were_read_back_as_44() -> Outcome<()> {
74 res!(refusal(&Dat::BU8(payload(300))));
75 Ok(())
76}
77
78// A BU16 length field holds 65,535.
79
80#[test]
81fn bu16_holds_65535_and_refuses_65536() -> Outcome<()> {
82 let full = Dat::BU16(payload(65_535));
83 let buf = res!(encode(&full));
84 assert_eq!(&buf[1..3], &[0xFF, 0xFF], "the length bytes of a 65,535-byte BU16");
85 assert_eq!(buf.len(), 1 + 2 + 65_535);
86 assert_eq!(res!(round_trip(&full)), full);
87
88 let msg = res!(refusal(&Dat::BU16(payload(65_536))));
89 res!(check_names(&msg, &["BU16", "65536", "65535"]));
90 Ok(())
91}
92
93// A BU32 length field holds 4,294,967,295. The vector is zeroed in one allocation, so its pages
94// are mapped and never touched while the encoder reads only its length and refuses.
95
96#[cfg(target_pointer_width = "64")]
97#[test]
98fn bu32_refuses_a_length_past_four_gibibytes() -> Outcome<()> {
99 let n = u32::MAX as usize + 1;
100 let msg = res!(refusal(&Dat::BU32(vec![0u8; n])));
101 res!(check_names(&msg, &["BU32", "4294967296", "4294967295"]));
102 Ok(())
103}
104
105#[test]
106fn bu32_holds_a_length_a_bu16_cannot() -> Outcome<()> {
107 let big = Dat::BU32(payload(70_000));
108 assert_eq!(res!(round_trip(&big)), big);
109 Ok(())
110}
111
112// BU64 states any length a vector can have.
113
114#[test]
115fn bu64_holds_what_bu8_cannot() -> Outcome<()> {
116 let big = Dat::BU64(payload(300));
117 assert_eq!(res!(round_trip(&big)), big);
118 Ok(())
119}
120
121// The refusal reaches the caller through a container, where a value is usually met.
122
123#[test]
124fn a_refusal_inside_a_list_or_map_reaches_the_caller() -> Outcome<()> {
125 let in_list = Dat::List(vec![Dat::U8(1), Dat::BU8(payload(256))]);
126 res!(refusal(&in_list));
127
128 let mut map = DaticleMap::new();
129 map.insert(dat!("k"), Dat::BU16(payload(65_536)));
130 res!(refusal(&Dat::Map(map)));
131 Ok(())
132}
133
134// `wrap_dat` picks the narrowest variant that fits, so a payload it wraps is never refused.
135
136#[test]
137fn wrap_dat_chooses_a_width_that_fits() -> Outcome<()> {
138 for n in [0usize, 1, 254, 255, 256, 65_534, 65_535, 65_536, 70_000] {
139 let dat = Dat::wrap_dat(payload(n));
140 assert_eq!(res!(round_trip(&dat)), dat, "a wrapped payload of {} bytes", n);
141 let bytes = match &dat {
142 Dat::BU8(b) | Dat::BU16(b) | Dat::BU32(b) | Dat::BU64(b) => b.clone(),
143 other => return Err(err!(
144 "wrap_dat gave {:?} for {} bytes, which is not a byte variant.", other, n;
145 Test, Unexpected, Data)),
146 };
147 assert_eq!(bytes, payload(n));
148 }
149 Ok(())
150}
151
152#[test]
153fn wrap_bytes_var_chooses_a_width_that_fits() -> Outcome<()> {
154 for n in [0usize, 254, 255, 256, 65_534, 65_535, 65_536, 70_000] {
155 let wrapped = res!(Dat::wrap_bytes_var(payload(n)));
156 let (back, used) = res!(Dat::from_bytes(&wrapped));
157 assert_eq!(used, wrapped.len());
158 let got = match back {
159 Dat::BU8(b) | Dat::BU16(b) | Dat::BU32(b) | Dat::BU64(b) => b,
160 other => return Err(err!(
161 "wrap_bytes_var gave {:?} for {} bytes, which is not a byte variant.", other, n;
162 Test, Unexpected, Data)),
163 };
164 assert_eq!(got, payload(n), "a wrapped payload of {} bytes", n);
165 }
166 Ok(())
167}
168
169
170// The text decoder. A text that names a width writes a payload the encoder would refuse, so
171// the decoder refuses it too, at the parse and not when the value is stored.
172
173fn text_bytes(kind: &str, n: usize) -> String {
174 let items: Vec<String> = payload(n).iter().map(|b| fmt!("{}", b)).collect();
175 fmt!("({}|[{}])", kind, items.join(", "))
176}
177
178#[test]
179fn text_bu8_holds_255_and_refuses_256() -> Outcome<()> {
180 let full = res!(Dat::decode_string(text_bytes("bu8", 255)));
181 assert_eq!(full, Dat::BU8(payload(255)));
182 // What the text decoder accepts, the encoder writes.
183 assert_eq!(res!(round_trip(&full)), full);
184
185 let msg = match Dat::decode_string(text_bytes("bu8", 256)) {
186 Ok(d) => return Err(err!("The text decoder built {:?} wider than a BU8 states.", d.kind(); Test, Invalid, Data)),
187 Err(e) => fmt!("{}", e),
188 };
189 res!(check_names(&msg, &["BU8", "256", "255"]));
190 Ok(())
191}
192
193#[test]
194fn text_bu8_refuses_300_bytes_that_the_encoder_would_cut_to_44() -> Outcome<()> {
195 assert!(Dat::decode_string(text_bytes("bu8", 300)).is_err());
196 Ok(())
197}
198
199#[test]
200fn text_bu16_holds_65535_and_refuses_65536() -> Outcome<()> {
201 let full = res!(Dat::decode_string(text_bytes("bu16", 65_535)));
202 assert_eq!(full, Dat::BU16(payload(65_535)));
203 assert_eq!(res!(round_trip(&full)), full);
204
205 let msg = match Dat::decode_string(text_bytes("bu16", 65_536)) {
206 Ok(d) => return Err(err!("The text decoder built {:?} wider than a BU16 states.", d.kind(); Test, Invalid, Data)),
207 Err(e) => fmt!("{}", e),
208 };
209 res!(check_names(&msg, &["BU16", "65536", "65535"]));
210 Ok(())
211}
212
213#[test]
214fn text_bu32_and_bu64_take_what_a_narrower_width_refuses() -> Outcome<()> {
215 let wide = res!(Dat::decode_string(text_bytes("bu32", 70_000)));
216 assert_eq!(wide, Dat::BU32(payload(70_000)));
217 assert_eq!(res!(round_trip(&wide)), wide);
218 let wide = res!(Dat::decode_string(text_bytes("bu64", 300)));
219 assert_eq!(wide, Dat::BU64(payload(300)));
220 Ok(())
221}