oxedyne/fe2o3/fe2o3_jdat/tests/bdat_items.rs
6.7 KiB, 1 run
created by r1870400018:68433, 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 buffer cannot make the decoder build more values than the caller allows, and a refusal |
| 2 | //! about a map key does not repeat the key. |
| 3 | //! |
| 4 | //! A one-byte value such as `Dat::EMPTY_CODE` costs a whole `Dat` in memory, so a 16 MiB list of |
| 5 | //! them decodes to some 1 GiB when only the buffer's length is bounded. `DecodeLimits::max_items` |
| 6 | //! counts the values as they are built, containers and scalars alike, and refuses the first one |
| 7 | //! past the cap, so the decoder stops allocating at the cap and not after the buffer is spent. |
| 8 | //! |
| 9 | //! A map refusal once formatted the key it had decoded with `{:?}`. A key is as long as the peer |
| 10 | //! chose, so a megabyte key made an error of several megabytes. |
| 11 | //! |
| 12 | //! Each test names the fault it holds, and turns red when that fault is put back. |
| 13 | |
| 14 | use oxedyne_fe2o3_core::prelude::*; |
| 15 | use oxedyne_fe2o3_jdat::{ |
| 16 | bdat::DecodeLimits, |
| 17 | note::NoteConfig, |
| 18 | prelude::*, |
| 19 | }; |
| 20 | |
| 21 | |
| 22 | // The minimal `c64` of a value: a code that says how many bytes follow, then those bytes. |
| 23 | fn c64(v: u64) -> Vec<u8> { |
| 24 | let be = v.to_be_bytes(); |
| 25 | let skip = be.iter().take_while(|b| **b == 0).count(); |
| 26 | let mut out = vec![Dat::C64_CODE_START + (8 - skip) as u8]; |
| 27 | out.extend_from_slice(&be[skip..]); |
| 28 | out |
| 29 | } |
| 30 | |
| 31 | // A list of `n` one-byte values, built by hand since the encoder would build `n` `Dat`s first. |
| 32 | fn list_of_empties(n: usize) -> Vec<u8> { |
| 33 | let mut b = vec![Dat::LIST_CODE]; |
| 34 | b.extend(c64(n as u64)); |
| 35 | b.resize(b.len() + n, Dat::EMPTY_CODE); |
| 36 | b |
| 37 | } |
| 38 | |
| 39 | // The number of values in a tree, counted by the test and not by the decoder. |
| 40 | fn values(dat: &Dat) -> usize { |
| 41 | 1 + match dat { |
| 42 | Dat::List(v) => v.iter().map(values).sum::<usize>(), |
| 43 | Dat::Map(m) => m.iter().map(|(k, v)| values(k) + values(v)).sum::<usize>(), |
| 44 | Dat::OrdMap(m) => m.iter().map(|(k, v)| values(k.dat()) + values(v)).sum::<usize>(), |
| 45 | Dat::Tup2(t) => t.iter().map(values).sum::<usize>(), |
| 46 | Dat::Tup3(t) => t.iter().map(values).sum::<usize>(), |
| 47 | Dat::Box(d) => values(d), |
| 48 | Dat::Opt(o) => match &**o { Some(d) => values(d), None => 0 }, |
| 49 | Dat::ABox(_, d, _) => values(d), |
| 50 | _ => 0, |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | fn limited(buf: &[u8], max_items: usize) -> Outcome<(Dat, usize)> { |
| 55 | Dat::from_bytes_limited(buf, &DecodeLimits::default().with_max_items(max_items)) |
| 56 | } |
| 57 | |
| 58 | fn refusal(buf: &[u8], max_items: usize) -> Outcome<String> { |
| 59 | match limited(buf, max_items) { |
| 60 | Ok((d, _)) => Err(err!( |
| 61 | "A cap of {} values accepted a {:?}.", max_items, d.kind(); Test, Invalid, Data)), |
| 62 | Err(e) => Ok(fmt!("{}", e)), |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | fn named(msg: &str, parts: &[&str]) { |
| 67 | for p in parts { |
| 68 | assert!(msg.contains(p), "The refusal '{}' does not name '{}'.", msg, p); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | |
| 73 | // The value cap. |
| 74 | |
| 75 | #[test] |
| 76 | fn a_list_of_values_fits_a_cap_of_its_own_count_and_not_one_less() -> Outcome<()> { |
| 77 | // The list is a value of its own, so ninety-nine items make a hundred values. |
| 78 | let (dat, _) = res!(limited(&list_of_empties(99), 100)); |
| 79 | assert_eq!(values(&dat), 100); |
| 80 | let msg = res!(refusal(&list_of_empties(100), 100)); |
| 81 | named(&msg, &["value number 101", "maximum of 100 values"]); |
| 82 | Ok(()) |
| 83 | } |
| 84 | |
| 85 | #[test] |
| 86 | fn a_16_mib_list_of_one_byte_values_is_refused_at_the_cap() -> Outcome<()> { |
| 87 | let buf = list_of_empties(16 * 1024 * 1024 - 8); |
| 88 | assert!(buf.len() <= 16 * 1024 * 1024, "The fixture is {} bytes.", buf.len()); |
| 89 | // The count in the refusal is the cap plus one, so the decoder stopped there and did not |
| 90 | // build the sixteen million values and then count them. |
| 91 | let msg = res!(refusal(&buf, 1_000)); |
| 92 | named(&msg, &["value number 1001", "maximum of 1000 values"]); |
| 93 | Ok(()) |
| 94 | } |
| 95 | |
| 96 | #[test] |
| 97 | fn every_kind_of_container_counts_the_values_inside_it() -> Outcome<()> { |
| 98 | let tree = Dat::List(vec![ |
| 99 | mapdat!{ "k" => Dat::List(vec![Dat::U8(1), Dat::U8(2)]) }, |
| 100 | omapdat!{ "a" => 1u8, "b" => Dat::Empty }, |
| 101 | Dat::Tup2(Box::new([Dat::U8(1), Dat::Str("x".to_string())])), |
| 102 | Dat::Tup3(Box::new([Dat::U8(1), Dat::U16(2), Dat::Empty])), |
| 103 | Dat::Box(Box::new(Dat::Opt(Box::new(Some(Dat::U8(7)))))), |
| 104 | Dat::ABox(NoteConfig::default(), Box::new(Dat::U8(1)), "note".to_string()), |
| 105 | Dat::Opt(Box::new(None)), |
| 106 | ]); |
| 107 | let want = values(&tree); |
| 108 | let buf = res!(tree.to_bytes(Vec::new())); |
| 109 | let (back, _) = res!(limited(&buf, want)); |
| 110 | assert_eq!(back, tree); |
| 111 | let msg = res!(refusal(&buf, want - 1)); |
| 112 | named(&msg, &[&fmt!("value number {}", want)]); |
| 113 | // The unlimited decode counts too, and never refuses. |
| 114 | let (back, _) = res!(Dat::from_bytes(&buf)); |
| 115 | assert_eq!(back, tree); |
| 116 | Ok(()) |
| 117 | } |
| 118 | |
| 119 | #[test] |
| 120 | fn no_limit_is_set_unless_the_caller_sets_one() { |
| 121 | assert_eq!(DecodeLimits::default().max_items, usize::MAX); |
| 122 | assert_eq!(DecodeLimits::new(8, 1024).max_items, usize::MAX); |
| 123 | assert_eq!(DecodeLimits::text().max_items, usize::MAX); |
| 124 | assert_eq!(DecodeLimits::UNLIMITED.max_items, usize::MAX); |
| 125 | } |
| 126 | |
| 127 | |
| 128 | // The map-key refusals. |
| 129 | |
| 130 | // A map or ordered map holding one key, a BU64 of `n` bytes, and no value after it. |
| 131 | fn map_with_a_key_and_no_value(code: u8, n: usize) -> Vec<u8> { |
| 132 | let mut key = vec![Dat::BU64_CODE]; |
| 133 | key.extend_from_slice(&(n as u64).to_be_bytes()); |
| 134 | key.resize(key.len() + n, 0xAB); |
| 135 | let mut b = vec![code]; |
| 136 | b.extend(c64(key.len() as u64)); |
| 137 | b.extend(key); |
| 138 | b |
| 139 | } |
| 140 | |
| 141 | #[test] |
| 142 | fn a_map_refusal_for_a_megabyte_key_stays_short() -> Outcome<()> { |
| 143 | let buf = map_with_a_key_and_no_value(Dat::MAP_CODE, 1 << 20); |
| 144 | let msg = match Dat::from_bytes(&buf) { |
| 145 | Ok((d, _)) => return Err(err!("A map with a key and no value decoded to {:?}.", d.kind(); Test, Invalid, Data)), |
| 146 | Err(e) => fmt!("{}", e), |
| 147 | }; |
| 148 | assert!(msg.len() < 600, "The refusal is {} characters, and should not repeat the key.", msg.len()); |
| 149 | // The key is a code, an eight-byte length and the megabyte. |
| 150 | named(&msg, &["BU64", "(1048585 bytes)", "decoding 0 key-value pairs"]); |
| 151 | Ok(()) |
| 152 | } |
| 153 | |
| 154 | #[test] |
| 155 | fn an_ordered_map_refusal_for_a_megabyte_key_stays_short() -> Outcome<()> { |
| 156 | let buf = map_with_a_key_and_no_value(Dat::OMAP_CODE, 1 << 20); |
| 157 | let msg = match Dat::from_bytes(&buf) { |
| 158 | Ok((d, _)) => return Err(err!("An ordered map with a key and no value decoded to {:?}.", d.kind(); Test, Invalid, Data)), |
| 159 | Err(e) => fmt!("{}", e), |
| 160 | }; |
| 161 | assert!(msg.len() < 600, "The refusal is {} characters, and should not repeat the key.", msg.len()); |
| 162 | named(&msg, &["BU64", "(1048585 bytes)", "decoding 0 key-value pairs"]); |
| 163 | Ok(()) |
| 164 | } |
| 165 | |
| 166 | #[test] |
| 167 | fn a_short_key_is_still_named_in_full() -> Outcome<()> { |
| 168 | // A key that is cheap to print is printed, so the refusal says which key lacked its value. |
| 169 | let mut buf = vec![Dat::MAP_CODE]; |
| 170 | let key = res!(Dat::Str("answer".to_string()).to_bytes(Vec::new())); |
| 171 | buf.extend(c64(key.len() as u64)); |
| 172 | buf.extend(&key); |
| 173 | let msg = match Dat::from_bytes(&buf) { |
| 174 | Ok((d, _)) => return Err(err!("A map with a key and no value decoded to {:?}.", d.kind(); Test, Invalid, Data)), |
| 175 | Err(e) => fmt!("{}", e), |
| 176 | }; |
| 177 | named(&msg, &["answer", &fmt!("({} bytes)", key.len())]); |
| 178 | assert!(!msg.contains("..."), "A short key was cut: {}", msg); |
| 179 | Ok(()) |
| 180 | } |