Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_jdat/tests/canonical.rs

4.6 KiB, 1 run

created by r1870400018:74643, 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 canonical-JSON decode refuses every text `Dat::json_canonical` would not have written.
2//!
3//! Each refusal below is accepted by the plain JSON configuration, which is why the option exists:
4//! a field a signature or a hash covers must have exactly one text, so `"\u0041"` and `"A"` cannot
5//! both be read as the same record.
6
7use oxedyne_fe2o3_jdat::{
8 prelude::*,
9 string::dec::DecoderConfig,
10};
11
12use oxedyne_fe2o3_core::prelude::*;
13
14
15fn plain(s: &str) -> Outcome<Dat> {
16 Dat::decode_string_with_config(s, &DecoderConfig::<(), ()>::json(None))
17}
18
19fn canonical(s: &str) -> Outcome<Dat> {
20 Dat::decode_string_with_config(s, &DecoderConfig::<(), ()>::json_canonical(None))
21}
22
23/// Texts the plain JSON decoder reads but the canonical form never writes.
24const REFUSED: &[(&str, &str)] = &[
25 (r#"{"a":"x\/y"}"#, "escaped solidus"),
26 (r#"{"a":"\u0041"}"#, "printable ASCII as \\u"),
27 (r#"{"a":"\u00e9"}"#, "non-ASCII as \\u"),
28 (r#"{"a":"\ud83d\ude00"}"#, "surrogate pair as \\u"),
29 (r#"{"a":"\u000a"}"#, "newline as \\u, not \\n"),
30 (r#"{"a":"\u001F"}"#, "control escape in upper-case hex"),
31 (r#"{"b":1,"a":2}"#, "keys out of order"),
32 (r#"{"a": 1}"#, "space after a colon"),
33 (r#"{"a":1, "b":2}"#, "space after a comma"),
34 (r#"[1,2 ]"#, "space before a bracket"),
35 (" {\"a\":1}", "leading whitespace"),
36 ("{\"a\":1}\n", "trailing newline"),
37 (r#"{"a":-0}"#, "negative zero"),
38 (r#"{"a":1.0}"#, "a float"),
39 (r#"{"a":1e2}"#, "an exponent"),
40];
41
42#[test]
43fn canonical_refuses_what_the_writer_never_writes() -> Outcome<()> {
44 for (text, why) in REFUSED {
45 if let Err(e) = plain(text) {
46 // Not every case is a gap in the plain decoder, but each must be refused here.
47 println!("plain json already refuses {} ({}): {}", text, why, e);
48 }
49 match canonical(text) {
50 Ok(dat) => return Err(err!(
51 "The canonical decode accepted {:?} ({}) as {:?}.", text, why, dat;
52 Test, Unexpected)),
53 Err(e) => println!("refused {} ({}): {}", text, why, e),
54 }
55 }
56 Ok(())
57}
58
59/// The gap the option fills: the plain decoder reads these as the same value as their canonical
60/// text, so a field has more than one text.
61#[test]
62fn plain_json_reads_the_escapes_as_their_characters() -> Outcome<()> {
63 assert_eq!(res!(plain(r#""\u0041\/""#)), dat!("A/"));
64 assert_eq!(res!(plain(r#"{"b":1,"a":2}"#)), res!(plain(r#"{"a":2,"b":1}"#)));
65 Ok(())
66}
67
68#[test]
69fn canonical_refuses_a_duplicate_key() -> Outcome<()> {
70 assert!(canonical(r#"{"a":1,"a":1}"#).is_err(), "a duplicate key must be refused");
71 Ok(())
72}
73
74#[test]
75fn canonical_refuses_jdat_forms() -> Outcome<()> {
76 for text in ["(u8|1)", "{\"a\":(u8|1)}", "{\"a\":1,}"] {
77 assert!(canonical(text).is_err(), "{:?} must be refused", text);
78 }
79 Ok(())
80}
81
82/// Every text the writer produces reads back to the same value, and that value writes the same
83/// text again.
84#[test]
85fn canonical_text_round_trips() -> Outcome<()> {
86 let texts = [
87 "null",
88 "true",
89 "-17",
90 "9007199254740991",
91 "\"\"",
92 "[]",
93 "{}",
94 "\"a\\\"b\\\\c\\nd\\re\\tf\\bg\\fh\\u001fi\\u0000\"",
95 "\"Cœur — 日本 😀 /\"",
96 "{\"\\n\":\"Newline\",\"\\u000b\":\"Vertical Tab\",\"\\r\":\"CR\",\"1\":\"One\",\
97 \"\u{80}\":\"Control\",\"\u{f6}\":\"o\",\"\u{20ac}\":\"Euro\"}",
98 "{\"age_band\":\"adult\",\"captures\":{\"face\":\"aG91c2U\",\"hands\":[1,-2,null]},\
99 \"evidence\":\"\",\"place\":{\"cell\":null,\"how\":\"device\"}}",
100 ];
101 for text in texts {
102 let dat = res!(canonical(text));
103 assert_eq!(res!(dat.json_canonical()), text);
104 assert_eq!(dat, res!(plain(text)));
105 }
106 Ok(())
107}
108
109#[test]
110fn canonical_refusal_names_the_position() -> Outcome<()> {
111 match canonical(r#"{"a":"x\/y"}"#) {
112 Ok(dat) => Err(err!("Accepted an escaped solidus as {:?}.", dat; Test, Unexpected)),
113 Err(e) => {
114 let msg = fmt!("{}", e);
115 assert!(msg.contains("at character 7"), "the error should name the position: {}", msg);
116 Ok(())
117 },
118 }
119}
120
121/// A refusal is placed at the character that leaves the canonical form, is counted under its own
122/// class, and holds none of the text.
123#[test]
124fn canonical_refusal_is_located_and_classed() -> Outcome<()> {
125 let cfg = DecoderConfig::<(), ()>::json_canonical(None);
126 for (text, line, col) in [
127 (r#"{"a":"x\/y"}"#, 1, 8),
128 ("{\"a\":1}\n", 1, 8),
129 ("\u{feff}{\"a\":1}", 1, 1),
130 ] {
131 match Dat::decode_string_located(text, &cfg) {
132 Ok(dat) => return Err(err!("Accepted {:?} as {:?}.", text, dat; Test, Unexpected)),
133 Err(at) => {
134 assert_eq!((at.line, at.col), (line, col), "the place of the refusal of {:?}", text);
135 assert_eq!(at.class(text), "not canonical");
136 assert!(!fmt!("{}", at.error).contains("x/y"), "the refusal must hold no text");
137 },
138 }
139 }
140 Ok(())
141}