crates/edge/proto/tests/cbor.rs 6.4 KiB 250 lines Raw
1 //! The CBOR contract: canonical bytes out, strict bytes in.2 //!3 //! These tests define the API. The implementation must satisfy them without a4 //! change to this file.5 6 use inf3r_proto::cbor::{CborError, MAX_DEPTH, Value, decode, encode};7 8 fn enc(value: &Value) -> Vec<u8> {9     let mut out = Vec::new();10     encode(value, &mut out);11     out12 }13 14 fn text(s: &str) -> Value {15     Value::Text(s.to_string())16 }17 18 fn bytes(v: &[u8]) -> Value {19     Value::Bytes(v.to_vec())20 }21 22 #[test]23 fn uint_uses_the_shortest_form() {24     let cases: [(u64, &[u8]); 10] = [25         (0, &[0x00]),26         (1, &[0x01]),27         (23, &[0x17]),28         (24, &[0x18, 0x18]),29         (255, &[0x18, 0xFF]),30         (256, &[0x19, 0x01, 0x00]),31         (65535, &[0x19, 0xFF, 0xFF]),32         (65536, &[0x1A, 0x00, 0x01, 0x00, 0x00]),33         (u32::MAX as u64, &[0x1A, 0xFF, 0xFF, 0xFF, 0xFF]),34         (35             u64::MAX,36             &[0x1B, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF],37         ),38     ];39     for (n, want) in cases {40         assert_eq!(enc(&Value::Uint(n)), want, "uint {n}");41         assert_eq!(decode(want).unwrap(), Value::Uint(n));42     }43 }44 45 #[test]46 fn nint_uses_the_shortest_form() {47     let cases: [(i64, &[u8]); 7] = [48         (-1, &[0x20]),49         (-24, &[0x37]),50         (-25, &[0x38, 0x18]),51         (-256, &[0x38, 0xFF]),52         (-257, &[0x39, 0x01, 0x00]),53         (-65536, &[0x39, 0xFF, 0xFF]),54         (-65537, &[0x3A, 0x00, 0x01, 0x00, 0x00]),55     ];56     for (n, want) in cases {57         assert_eq!(enc(&Value::Nint(n)), want, "nint {n}");58         assert_eq!(decode(want).unwrap(), Value::Nint(n));59     }60 }61 62 #[test]63 fn text_and_bytes_carry_their_length() {64     assert_eq!(enc(&text("")), [0x60]);65     assert_eq!(enc(&text("a")), [0x61, b'a']);66     assert_eq!(enc(&text("hello")), [0x65, b'h', b'e', b'l', b'l', b'o']);67     assert_eq!(enc(&bytes(b"")), [0x40]);68     assert_eq!(enc(&bytes(&[0xFF])), [0x41, 0xFF]);69     assert_eq!(enc(&bytes(&[0xAB; 24])), {70         let mut v = vec![0x58, 24];71         v.extend([0xAB; 24]);72         v73     });74 }75 76 #[test]77 fn arrays_and_maps_carry_their_length() {78     assert_eq!(enc(&Value::Array(vec![])), [0x80]);79     assert_eq!(80         enc(&Value::Array(vec![Value::Uint(1), Value::Uint(2)])),81         [0x82, 0x01, 0x02]82     );83     assert_eq!(enc(&Value::map([])), [0xA0]);84     assert_eq!(85         enc(&Value::map([(text("a"), Value::Uint(1))])),86         [0xA1, 0x61, b'a', 0x01]87     );88 }89 90 #[test]91 fn map_keys_are_sorted_by_encoded_bytes() {92     let value = Value::map([(text("b"), Value::Uint(1)), (text("a"), Value::Uint(2))]);93     assert_eq!(enc(&value), [0xA2, 0x61, b'a', 0x02, 0x61, b'b', 0x01]);94 95     let mixed = Value::map([96         (text("a"), Value::Uint(0)),97         (Value::Uint(1), Value::Uint(0)),98     ]);99     assert_eq!(100         enc(&mixed),101         [0xA2, 0x01, 0x00, 0x61, b'a', 0x00],102         "a one-byte uint key sorts before a two-byte text key"103     );104 }105 106 #[test]107 fn simple_values_have_their_fixed_bytes() {108     assert_eq!(enc(&Value::Bool(false)), [0xF4]);109     assert_eq!(enc(&Value::Bool(true)), [0xF5]);110     assert_eq!(enc(&Value::Null), [0xF6]);111 }112 113 #[test]114 fn floats_are_written_as_double_precision() {115     assert_eq!(116         enc(&Value::Float(1.5)),117         [0xFB, 0x3F, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]118     );119     assert_eq!(120         decode(&[0xFB, 0x3F, 0xF8, 0, 0, 0, 0, 0, 0]).unwrap(),121         Value::Float(1.5)122     );123 }124 125 #[test]126 fn narrowed_floats_widen_on_decode() {127     assert_eq!(decode(&[0xF9, 0x3C, 0x00]).unwrap(), Value::Float(1.0));128     assert_eq!(129         decode(&[0xFA, 0x3F, 0xC0, 0x00, 0x00]).unwrap(),130         Value::Float(1.5)131     );132 }133 134 #[test]135 fn a_full_value_round_trips() {136     let value = Value::map([137         (138             text("v"),139             Value::Array(vec![Value::Uint(1), Value::Uint(1)]),140         ),141         (text("role"), text("call")),142         (text("id"), text("plugin-7")),143         (text("n"), Value::Null),144         (text("f"), Value::Float(-0.25)),145         (text("b"), bytes(&[0, 1, 2])),146     ]);147     let wire = enc(&value);148     assert_eq!(decode(&wire).unwrap(), value);149 }150 151 #[test]152 fn indefinite_lengths_are_refused() {153     for head in [0x5F, 0x7F, 0x9F, 0xBF] {154         assert_eq!(decode(&[head, 0xFF]).unwrap_err(), CborError::Indefinite);155     }156 }157 158 #[test]159 fn a_length_must_be_in_its_shortest_form() {160     let cases: [&[u8]; 5] = [161         &[0x18, 0x17],162         &[0x19, 0x00, 0xFF],163         &[0x38, 0x00],164         &[0x78, 0x01, b'a'],165         &[0x98, 0x00],166     ];167     for wire in cases {168         assert_eq!(169             decode(wire).unwrap_err(),170             CborError::NonCanonical,171             "{wire:02x?}"172         );173     }174 }175 176 #[test]177 fn tags_are_unsupported() {178     assert_eq!(179         decode(&[0xC0, 0x00]).unwrap_err(),180         CborError::Malformed(0xC0)181     );182 }183 184 #[test]185 fn bytes_after_the_item_are_refused() {186     assert_eq!(decode(&[0x00, 0x00]).unwrap_err(), CborError::Trailing);187 }188 189 #[test]190 fn a_short_item_is_eof() {191     for wire in [&[][..], &[0x18][..], &[0x19, 0x01][..], &[0x65, b'a'][..]] {192         assert_eq!(decode(wire).unwrap_err(), CborError::Eof, "{wire:02x?}");193     }194 }195 196 fn nested(levels: usize) -> Vec<u8> {197     let mut wire = vec![0x81; levels];198     wire.push(0x00);199     wire200 }201 202 #[test]203 fn nesting_is_bounded() {204     assert!(decode(&nested(MAX_DEPTH)).is_ok());205     assert_eq!(206         decode(&nested(MAX_DEPTH + 1)).unwrap_err(),207         CborError::TooDeep208     );209 }210 211 #[test]212 fn duplicate_keys_are_refused() {213     assert_eq!(214         decode(&[0xA2, 0x61, b'a', 0x01, 0x61, b'a', 0x02]).unwrap_err(),215         CborError::Duplicate216     );217 }218 219 #[test]220 fn text_must_be_utf8() {221     assert_eq!(decode(&[0x61, 0xFF]).unwrap_err(), CborError::Utf8);222 }223 224 #[test]225 fn unassigned_simple_values_are_refused() {226     for head in [0xE0, 0xF7, 0xF8] {227         assert_eq!(decode(&[head]).unwrap_err(), CborError::Malformed(head));228     }229 }230 231 #[test]232 fn map_get_reads_a_text_key() {233     let value = Value::map([(text("id"), text("x"))]);234     assert_eq!(value.get("id"), Some(&text("x")));235     assert_eq!(value.get("nope"), None);236     assert_eq!(Value::Uint(1).get("id"), None);237 }238 239 #[test]240 fn kind_names_the_item() {241     assert_eq!(Value::Uint(1).kind(), "uint");242     assert_eq!(Value::Nint(-1).kind(), "nint");243     assert_eq!(bytes(b"").kind(), "bytes");244     assert_eq!(text("").kind(), "text");245     assert_eq!(Value::Array(vec![]).kind(), "array");246     assert_eq!(Value::map([]).kind(), "map");247     assert_eq!(Value::Bool(true).kind(), "bool");248     assert_eq!(Value::Null.kind(), "null");249     assert_eq!(Value::Float(0.0).kind(), "float");250 }