Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_jdat/tests/jdat_to_json.rs

15.2 KiB, 1 run

created by r1870400018:67319, 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//! `Dat::jdat_to_json`: a jdat settings file written as plain JSON for a browser's `JSON.parse`
2//! (the wasm core exports it; U0, D-20261005-12). It reads with `Dat::decode_string`'s rules, so
3//! what the format allows by design stays allowed (a comment ends at its mark or at the end of
4//! its line, trailing commas, `(kind|value)` annotations), and what is wrong is refused with the
5//! line and column, and no key or value, since either may be personal data. What comes out is held
6//! to `serde_json`, which did not come from here and stands for the browser's `JSON.parse`.
7
8use oxedyne_fe2o3_core::prelude::*;
9use oxedyne_fe2o3_jdat::{
10 prelude::*,
11 bdat::DecodeLimits,
12};
13
14use serde_json::Value;
15
16
17const SHAPE: &str = include_str!("fixtures/ui_settings_shape.jdat");
18
19fn to_json(s: &str) -> Outcome<String> {
20 Dat::jdat_to_json(s, &DecodeLimits::text(), true)
21}
22
23// The JSON `s` becomes, read back by an independent reader.
24fn browser(s: &str) -> Outcome<Value> {
25 let json = res!(to_json(s));
26 match serde_json::from_str::<Value>(&json) {
27 Ok(v) => Ok(v),
28 Err(e) => Err(err!("jdat_to_json wrote text that serde_json refuses ({}): {}", e, json;
29 Invalid, Input, Decode)),
30 }
31}
32
33// Refuse `s`, and require the message to hold every word in `words`.
34fn refused(s: &str, words: &[&str]) {
35 refused_without(s, words, &[]);
36}
37
38// As `refused`, and the message must hold none of `absent` (a key or a value of `s`).
39fn refused_without(s: &str, words: &[&str], absent: &[&str]) {
40 match to_json(s) {
41 Ok(json) => panic!("{:?} should be refused, and became {}", s, json),
42 Err(e) => {
43 let msg = e.plain();
44 for w in words {
45 assert!(msg.contains(w), "the refusal of {:?} should hold {:?}, and reads: {}",
46 s, w, msg);
47 }
48 for w in absent {
49 assert!(!msg.contains(w), "the refusal of {:?} should not hold {:?}, and reads: {}",
50 s, w, msg);
51 }
52 },
53 }
54}
55
56#[test]
57fn test_shape_reads_as_json_00() -> Outcome<()> {
58 let v = res!(browser(SHAPE));
59 assert_eq!(v["copy"]["identity.create"], "Create Oxenym");
60 assert_eq!(v["copy"]["identity.remaining"],
61 "{real} real and {vuser} virtual oxenyms left, for life");
62 assert_eq!(v["copy"]["common.close"], "Close");
63 assert_eq!(v["roles"]["popup"]["mark"], "none");
64 assert_eq!(v["roles"]["qr"]["size"], 296.4);
65 assert_eq!(v["roles"]["identity-card"]["photo"], "avatar");
66 assert_eq!(v["kinds"]["oxenym"]["bot"]["icon"], "bot_icon");
67 assert_eq!(v["screens"]["current_status"].as_array().map(|a| a.len()), Some(6));
68 assert_eq!(v["screens"]["current_status"][5], "scan");
69 Ok(())
70}
71
72#[test]
73fn test_members_keep_file_order_00() -> Outcome<()> {
74 // The editor draws a field per key "grouped as the file is", so order is data.
75 let json = res!(to_json(SHAPE));
76 let at = |k: &str| json.find(k).unwrap_or(usize::MAX);
77 assert!(at("\"real\"") < at("\"mask\"") && at("\"mask\"") < at("\"org\"")
78 && at("\"org\"") < at("\"bot\""), "oxenym kinds out of file order in {}", json);
79 assert!(at("\"copy\"") < at("\"roles\"") && at("\"roles\"") < at("\"kinds\"")
80 && at("\"kinds\"") < at("\"screens\""), "sections out of file order");
81 assert!(json.find("\"identity.current_title\"").unwrap_or(usize::MAX)
82 < json.find("\"identity.create\"").unwrap_or(0), "copy keys out of file order");
83 // Order is the file's and not alphabetical: "z" is written before "a".
84 assert_eq!(res!(to_json(r#"{"z": 1, "a": {"y": 2, "b": 3}, "m": [4]}"#)),
85 r#"{"z":1,"a":{"y":2,"b":3},"m":[4]}"#);
86 Ok(())
87}
88
89#[test]
90fn test_comments_are_dropped_everywhere_00() -> Outcome<()> {
91 let plain = r#"{"a":{"b":[1,2],"c":"x"},"d":[],"e":{}}"#;
92 let commented = "! top ! { ! after brace !\n\t\"a\" ! before colon ! : ! after colon ! {\n\
93 \"b\": [ ! in list ! 1 ! after one ! , # hash comment # 2 ! before bracket ! ] ! after \
94 list !, \"c\": \"x\" # before brace # }, \"d\": [ ! empty ! ], \"e\": { # empty # } \
95 ! last member ! } ! end !";
96 assert_eq!(res!(to_json(commented)), plain);
97 Ok(())
98}
99
100#[test]
101fn test_comment_text_is_free_00() -> Outcome<()> {
102 // Quotes, braces, brackets, colons, commas and the other mark are all inert inside a comment.
103 let s = "{ ! was \"logo\": { [ 1, 2 ] } # not a close ! \"a\": 1, # a ! is inert here # \"b\": 2 }";
104 assert_eq!(res!(to_json(s)), r#"{"a":1,"b":2}"#);
105 // An apostrophe in prose does not open a string.
106 assert_eq!(res!(to_json("{ ! the names are the gate's ! \"a\": 1 }")), r#"{"a":1}"#);
107 // A mark inside a string is not a comment.
108 assert_eq!(res!(to_json(r##"{"a": "# not ! a comment #"}"##)), r##"{"a":"# not ! a comment #"}"##);
109 Ok(())
110}
111
112#[test]
113fn test_a_comment_ends_at_its_mark_or_its_line_00() -> Outcome<()> {
114 // A comment with no closing mark runs to the end of its line, and the next line is the file's.
115 assert_eq!(res!(to_json("{\"a\": 1, ! to the end of the line\n\"b\": 2}")), r#"{"a":1,"b":2}"#);
116 assert_eq!(res!(to_json("{\"a\": 1, # likewise\n\"b\": 2}")), r#"{"a":1,"b":2}"#);
117 // A comment ends at its own mark, so what follows on the line is read.
118 assert_eq!(res!(to_json("{ ! one ! \"a\": 1, ! two ! \"b\": 2 }")), r#"{"a":1,"b":2}"#);
119 // At the end of the text a comment runs to the end, and nothing is left to read.
120 assert_eq!(res!(to_json("{\"a\": 1} ! last")), r#"{"a":1}"#);
121 assert_eq!(res!(to_json("{\"a\": 1}\n# last\n")), r#"{"a":1}"#);
122 // A comment of several lines is one mark pair per line: a '#..#' across a line break is not
123 // a comment, because its second line is read as the file.
124 assert_eq!(res!(to_json("{ # one\n # two\n \"a\": 1 }")), r#"{"a":1}"#);
125 refused("{ # one\n two # \"a\": 1 }", &["line 2"]);
126 Ok(())
127}
128
129#[test]
130fn test_every_value_kind_00() -> Outcome<()> {
131 let s = r#"{
132 "s": "plain", "empty": "", "esc": "q\"b\\s\/n\nt\tu\u00e9", "raw": "é — 日本 😀",
133 "int": 12, "neg": -3, "zero": 0, "float": 296.4, "negf": -0.5, "exp": 1.5e3, "bigexp": 2E-2,
134 "t": true, "f": false, "nothing": null,
135 "emptyobj": {}, "emptylist": [], "nest": [[ "a", ["b"] ], { "k": [ 1, { "z": null } ] }],
136 }"#;
137 let v = res!(browser(s));
138 assert_eq!(v["s"], "plain");
139 assert_eq!(v["empty"], "");
140 assert_eq!(v["esc"], "q\"b\\s/n\nt\tu\u{e9}");
141 assert_eq!(v["raw"], "é — 日本 😀");
142 assert_eq!(v["int"], 12);
143 assert_eq!(v["neg"], -3);
144 assert_eq!(v["zero"], 0);
145 assert_eq!(v["float"], 296.4);
146 assert_eq!(v["negf"], -0.5);
147 assert_eq!(v["exp"], 1500.0);
148 assert_eq!(v["bigexp"], 0.02);
149 assert_eq!(v["t"], true);
150 assert_eq!(v["f"], false);
151 assert!(v["nothing"].is_null());
152 assert_eq!(v["emptyobj"], serde_json::json!({}));
153 assert_eq!(v["emptylist"], serde_json::json!([]));
154 assert_eq!(v["nest"][1]["k"][1]["z"], Value::Null);
155 assert_eq!(v["nest"][0][1][0], "b");
156 // Whole numbers stay integers and fractions stay fractions.
157 assert_eq!(res!(to_json(r#"{"i": 12, "f": 296.4}"#)), r#"{"i":12,"f":296.4}"#);
158 // A root need not be a map.
159 assert_eq!(res!(to_json(r#"[1, "a", null]"#)), r#"[1,"a",null]"#);
160 assert_eq!(res!(to_json("\"x\"")), "\"x\"");
161 Ok(())
162}
163
164#[test]
165fn test_trailing_commas_00() -> Outcome<()> {
166 assert_eq!(res!(to_json(r#"{"a": [1, 2,], "b": {"c": 3,},}"#)), r#"{"a":[1,2],"b":{"c":3}}"#);
167 // A comma with nothing before it is not a trailing one.
168 refused("{,}", &["line 1"]);
169 refused("[,]", &["nothing", "line 1, column 2"]);
170 refused("{\"a\": 1,, \"b\": 2}", &["nothing", "line 1, column 9"]);
171 refused("[1,,2]", &["nothing", "line 1, column 4"]);
172 Ok(())
173}
174
175#[test]
176fn test_kind_annotations_take_the_value_00() -> Outcome<()> {
177 let s = r#"{"a": (u8|1), "b": (i16|-5), "c": (f64|1.5), "d": (str|"x"), "e": (u32|70000),
178 "g": (f32 | 2), "h": ! note ! (u64|9007199254740991)}"#;
179 assert_eq!(res!(to_json(s)),
180 r#"{"a":1,"b":-5,"c":1.5,"d":"x","e":70000,"g":2,"h":9007199254740991}"#);
181 // The str kind takes whatever follows as text.
182 assert_eq!(res!(to_json(r#"{"a": (str|3)}"#)), r#"{"a":"3"}"#);
183 Ok(())
184}
185
186#[test]
187fn test_kind_annotations_are_checked_00() -> Outcome<()> {
188 refused("{\n\"n\": (u8|300)}", &["line 2", "column"]);
189 refused("{\n\"n\": (u8|-1)}", &["line 2", "column"]);
190 refused("{\n\"n\": (u8|1.5)}", &["line 2", "column"]);
191 refused("{\n\"n\": (f64|\"x\")}", &["line 2", "column"]);
192 refused("{\n\"n\": (zzz|1)}", &["line 2", "column"]);
193 refused("{\n\"n\": (u8|1}", &["line 2", "column"]);
194 refused("{\n\"n\": (u8|1", &["line 2", "column"]);
195 Ok(())
196}
197
198#[test]
199fn test_error_names_line_and_position_00() -> Outcome<()> {
200 // A bad value, three levels down, on line 4, and no key in the words.
201 refused_without("{\n\t\"roles\": {\n\t\t\"qr\": {\n\t\t\t\"size\": (u8|300),\n\t\t},\n\t},\n}",
202 &["line 4", "column"], &["roles", "size", "300"]);
203 // Column counts characters and not bytes, and a tab as one: the value starts after a tab,
204 // a quoted name, a colon and a space.
205 refused("{\n\t\"a\": (u8|300)}", &["line 2", "column "]);
206 let wide = refused_text("{\n\t\"é日\": (u8|300)}");
207 let narrow = refused_text("{\n\t\"ab\": (u8|300)}");
208 assert_eq!(wide, narrow, "a multi-byte name must not move the column");
209 // An array element is named by its line, and no key.
210 refused_without("{\n\"screens\": {\"cs\": [\"a\",\n (u8|300)]}}", &["line 3"], &["screens", "cs"]);
211 Ok(())
212}
213
214// The message of the refusal of `s`, with the name in the key, to compare columns.
215fn refused_text(s: &str) -> String {
216 match to_json(s) {
217 Ok(json) => panic!("{:?} should be refused, and became {}", s, json),
218 Err(e) => {
219 let msg = e.plain();
220 match msg.find("column ") {
221 Some(i) => msg[i..].chars().take_while(|c| *c != ',' && *c != '.').collect(),
222 None => panic!("the refusal of {:?} names no column: {}", s, msg),
223 }
224 },
225 }
226}
227
228#[test]
229fn test_malformed_files_are_refused_00() -> Outcome<()> {
230 // A missing colon after a name.
231 refused("{\n\"a\": \"b\",\n\"c\" \"d\"}", &["line 3"]);
232 // A missing comma between members.
233 refused("{\n\"a\": 1\n\"b\": 2}", &["line 3"]);
234 // A map or list left open: the decoder once returned the inner value and dropped the rest.
235 refused("{\n\"a\": {\n\"size\": 1,\n\n}", &["closure"]);
236 refused("{\n\"a\": [1,\n2", &["closure"]);
237 // A string left open.
238 refused("{\n\"a\": \"never", &["line 2"]);
239 // Values parted by a space are two values, not one.
240 refused("{\n\"a\": [1 2]}", &["line 2"]);
241 refused("{\n\"a\": \"x\" \"y\"}", &["line 2"]);
242 // Anything after the root but a comment.
243 refused("{\n\"a\": 1}\n}", &["line 3"]);
244 refused("{\"a\": 1} {\"b\": 2}", &["line 1"]);
245 refused("{\"a\": 1} x", &["line 1"]);
246 // Nothing at all, or nothing but a comment.
247 refused("", &[]);
248 refused(" \n ! only a comment ! ", &[]);
249 // Bad escapes.
250 refused("{\n\"a\": \"\\x\"}", &["line 2"]);
251 refused("{\n\"a\": \"\\ud800\"}", &["line 2"]);
252 Ok(())
253}
254
255#[test]
256fn test_a_member_with_no_value_is_refused_00() -> Outcome<()> {
257 refused("{\n\"a\": }", &["line 2"]);
258 refused("{\n\"a\": 1,\n\"bKeyQ\":\n}", &["line 4"]);
259 refused_without("{\n\"a\": 1,\n\"bKeyQ\":\n}", &[], &["bKeyQ"]);
260 Ok(())
261}
262
263#[test]
264fn test_duplicate_key_is_refused_00() -> Outcome<()> {
265 refused_without("{\n\"roles\": {\n\"qrKeyQ\": 1,\n\"qrKeyQ\": 2}}",
266 &["line 4", "duplicate key"], &["roles", "qrKeyQ"]);
267 Ok(())
268}
269
270#[test]
271fn test_numbers_a_browser_cannot_hold_are_refused_00() -> Outcome<()> {
272 // These are found after the text is read, so they name the position in the data (the first
273 // member, then the first member of that, then its second item) and not the line or the key.
274 refused_without("{\n\"nKeyQ\": 9007199254740993}", &["the position {0}", "2^53"], &["nKeyQ", "9007"]);
275 refused("{\n\"n\": -9007199254740993}", &["the position {0}", "2^53"]);
276 refused("{\n\"n\": (u64|18446744073709551615)}", &["the position {0}", "2^53"]);
277 refused_without("{\"aKeyQ\": {\"n\": [0, 1e999]}}", &["{0}{0}[1]", "finite"], &["aKeyQ", "999"]);
278 assert_eq!(res!(to_json(r#"{"n": 9007199254740991}"#)), r#"{"n":9007199254740991}"#);
279 assert_eq!(res!(to_json(r#"{"n": -9007199254740991}"#)), r#"{"n":-9007199254740991}"#);
280 Ok(())
281}
282
283#[test]
284fn test_js_safe_off_lets_large_whole_numbers_through_00() -> Outcome<()> {
285 let limits = DecodeLimits::text();
286 assert_eq!(res!(Dat::jdat_to_json("{\"n\": 9007199254740993}", &limits, false)),
287 r#"{"n":9007199254740993}"#);
288 assert_eq!(res!(Dat::jdat_to_json("{\"n\": (u64|18446744073709551615)}", &limits, false)),
289 r#"{"n":18446744073709551615}"#);
290 // A float that is not finite has no JSON form either way.
291 assert!(Dat::jdat_to_json("{\"n\": 1e999}", &limits, false).is_err());
292 Ok(())
293}
294
295#[test]
296fn test_limits_apply_00() -> Outcome<()> {
297 let deep = fmt!("{}{}", "[".repeat(100_000), "]".repeat(100_000));
298 assert!(to_json(&deep).is_err(), "a hundred thousand levels must be refused, not recursed");
299 let tiny = DecodeLimits::new(8, 4);
300 assert!(Dat::jdat_to_json("[1,2,3]", &tiny, true).is_err(), "seven bytes is past four");
301 let flat = DecodeLimits::new(3, 1024);
302 res!(Dat::jdat_to_json("[[1]]", &flat, true));
303 assert!(Dat::jdat_to_json("[[[1]]]", &flat, true).is_err(), "depth 4 is past 3");
304 Ok(())
305}
306
307#[test]
308fn test_plain_json_reads_as_itself_00() -> Outcome<()> {
309 // Every JSON document is a settings document: the text of the shape is a fixed point.
310 let once = res!(to_json(SHAPE));
311 assert_eq!(res!(to_json(&once)), once);
312 // And the independent reader agrees on the value, before and after.
313 let doc = r#"{"b":[1,"x",{"c":null}],"a":{"d":true,"e":-4,"f":1.25}}"#;
314 assert_eq!(res!(to_json(doc)), doc);
315 let a: Value = match serde_json::from_str(doc) {
316 Ok(v) => v,
317 Err(e) => return Err(err!("serde_json refuses its own fixture: {}", e; Invalid, Input)),
318 };
319 assert_eq!(res!(browser(doc)), a);
320 Ok(())
321}
322
323#[test]
324fn test_a_kind_with_no_plain_form_is_refused_00() -> Outcome<()> {
325 refused("{\n\"a\": (bin|1,2)}", &["line 2"]);
326 Ok(())
327}
328
329#[test]
330fn test_a_map_of_many_keys_is_read_in_linear_time_00() -> Outcome<()> {
331 // The ordered map searched every key it held for each key it added: 40,000 keys took 10 s in
332 // a release build.
333 let mut s = String::from("{");
334 for i in 0..30000 {
335 s.push_str(&fmt!("\"k{}\": {}, ", i, i));
336 }
337 s.push('}');
338 let t0 = std::time::Instant::now();
339 let json = res!(to_json(&s));
340 let took = t0.elapsed();
341 assert!(json.len() > 300000, "the JSON is {} bytes", json.len());
342 assert!(took.as_secs() < 5, "30,000 keys took {:?}", took);
343 Ok(())
344}
345
346#[test]
347fn test_a_comment_where_whitespace_could_be_changes_nothing_00() -> Outcome<()> {
348 let plain = res!(to_json("{\"a\": 1, \"b\": [1, 2]}"));
349 for s in [
350 "{\"a\" # c\n: 1, \"b\": [1, 2]}",
351 "{\"a\": 1 # c\n, \"b\": [1 # d\n, 2]}",
352 "{\"a\": 1, \"b\": [1, 2] # c\n}",
353 "# c\n{\"a\": 1, \"b\": [1, 2]}",
354 "\u{feff}{\"a\": 1, \"b\": [1, 2]}",
355 ] {
356 assert_eq!(res!(to_json(s)), plain, "{:?}", s);
357 }
358 Ok(())
359}