245 lines
7.8 KiB
Rust
245 lines
7.8 KiB
Rust
use dever_runtime::{
|
|
collection::Map,
|
|
wire::{
|
|
self,
|
|
document::{self, Value},
|
|
json::Json,
|
|
},
|
|
};
|
|
|
|
#[test]
|
|
fn document_keeps_numbers_order_and_postorder_ids() {
|
|
let source = r#"{"z":1e9999,"雪":[null,true,"\u96ea",1e+03]}"#;
|
|
let (root, nodes) = document::parse(source).unwrap();
|
|
assert_eq!(root, 6);
|
|
assert_eq!(nodes[0], Value::Number("1e9999".into()));
|
|
assert_eq!(nodes[3], Value::String("雪".into()));
|
|
assert_eq!(nodes[5], Value::Array(vec![1, 2, 3, 4]));
|
|
assert_eq!(
|
|
document::stringify(root, &nodes).unwrap(),
|
|
r#"{"z":1e9999,"雪":[null,true,"雪",1e+03]}"#
|
|
);
|
|
for source in [
|
|
"null",
|
|
"false",
|
|
"[]",
|
|
"{}",
|
|
"-0",
|
|
"1e+03",
|
|
r#""\u0000\n\t雪""#,
|
|
] {
|
|
let (root, nodes) = document::parse(source).unwrap();
|
|
assert_eq!(
|
|
document::parse(&document::stringify(root, &nodes).unwrap()).unwrap(),
|
|
(root, nodes)
|
|
);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn invalid_json_reports_errors_without_panicking_on_unicode() {
|
|
for source in [
|
|
"",
|
|
" ",
|
|
"[",
|
|
"[1,]",
|
|
"{}{}",
|
|
"01",
|
|
"NaN",
|
|
"1e",
|
|
r#""\uD800""#,
|
|
r#"{"x":0,"x":1}"#,
|
|
r#"{"雪":0,"\u96ea":1}"#,
|
|
"[雪]",
|
|
"[\"雪\",雪]",
|
|
] {
|
|
let error = document::parse(source).unwrap_err();
|
|
assert!(
|
|
error.position >= 0 && error.position <= source.chars().count() as i64,
|
|
"{source}: {error:?}"
|
|
);
|
|
assert!(Json::from_text(source.into()).is_err(), "{source}");
|
|
}
|
|
for source in [r#""\雪""#, r#""\u0雪雪""#, "\"雪🙂", "{\"雪🙂"] {
|
|
assert!(document::parse(source).is_err());
|
|
}
|
|
assert_eq!(document::parse("[\"雪\",]").unwrap_err().position, 5);
|
|
let duplicate = r#"{"雪":0,"\u96ea":[}"#;
|
|
let error = document::parse(duplicate).unwrap_err();
|
|
assert_eq!(error.message, "duplicate JSON object key");
|
|
assert_eq!(
|
|
error.position,
|
|
duplicate[..duplicate.rfind(':').unwrap()].chars().count() as i64
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn document_strings_match_serde_for_unicode_and_control_characters() {
|
|
let text: String = (0..=u32::from(char::MAX))
|
|
.filter_map(char::from_u32)
|
|
.collect();
|
|
let expected = wire::serde_json::to_string(&text).unwrap();
|
|
let nodes = vec![Value::String(text)];
|
|
assert_eq!(document::stringify(0, &nodes).unwrap(), expected);
|
|
assert_eq!(document::parse(&expected).unwrap().1, nodes);
|
|
}
|
|
|
|
#[test]
|
|
fn documents_validate_all_nodes_and_expansion_before_writing() {
|
|
for children in [vec![-1], vec![1], vec![2]] {
|
|
assert_eq!(
|
|
document::stringify(0, &[Value::Array(children)])
|
|
.unwrap_err()
|
|
.node,
|
|
0
|
|
);
|
|
}
|
|
assert!(document::stringify(-1, &[Value::Null]).is_err());
|
|
assert!(document::stringify(1, &[Value::Null]).is_err());
|
|
for raw in ["", "01", "1 ", " 1", "true", "1e", "[1]"] {
|
|
assert_eq!(
|
|
document::stringify(0, &[Value::Null, Value::Number(raw.into())])
|
|
.unwrap_err()
|
|
.node,
|
|
1
|
|
);
|
|
}
|
|
let mut nodes = vec![Value::Null];
|
|
for index in 0..16 {
|
|
nodes.push(Value::Array(vec![index, index]));
|
|
}
|
|
assert!(
|
|
document::stringify(16, &nodes)
|
|
.unwrap_err()
|
|
.message
|
|
.contains("value limit")
|
|
);
|
|
assert!(document::stringify(0, &[Value::String("x".repeat(wire::MAX_BYTES))]).is_err());
|
|
}
|
|
|
|
#[test]
|
|
fn json_limits_accept_boundary_and_reject_next_value() {
|
|
for (depth, valid) in [(64, true), (65, false)] {
|
|
let text = format!("{}0{}", "[".repeat(depth), "]".repeat(depth));
|
|
assert_eq!(Json::from_text(text).is_ok(), valid);
|
|
}
|
|
for (depth, valid) in [(256, true), (257, false)] {
|
|
let text = format!("{}0{}", "[".repeat(depth), "]".repeat(depth));
|
|
let parsed = document::parse(&text);
|
|
assert_eq!(parsed.is_ok(), valid);
|
|
if let Ok((root, nodes)) = parsed {
|
|
assert_eq!(document::stringify(root, &nodes).unwrap(), text);
|
|
}
|
|
}
|
|
for (values, valid) in [(wire::MAX_VALUES - 1, true), (wire::MAX_VALUES, false)] {
|
|
let text = format!("[{}0]", "0,".repeat(values - 1));
|
|
assert_eq!(document::parse(&text).is_ok(), valid);
|
|
}
|
|
let text = format!("\"{}\"", "x".repeat(wire::MAX_BYTES - 2));
|
|
assert!(Json::from_text(text.clone()).is_ok());
|
|
assert!(Json::from_text(format!("{text} ")).is_err());
|
|
}
|
|
|
|
#[test]
|
|
fn json_response_counts_the_outer_object_in_each_budget() {
|
|
let body = wire::json::response_body(|output| output.value("雪\"")).unwrap();
|
|
assert_eq!(
|
|
serde_json::from_slice::<serde_json::Value>(&body).unwrap(),
|
|
serde_json::json!({"code":0,"message":"ok","data":"雪\""})
|
|
);
|
|
let mut values = vec![0; wire::MAX_VALUES - 4];
|
|
assert!(wire::response(&values).is_ok());
|
|
values.push(0);
|
|
assert!(wire::response(&values).unwrap_err().contains("value limit"));
|
|
|
|
let mut nested = serde_json::json!(0);
|
|
for _ in 1..wire::MAX_DEPTH {
|
|
nested = serde_json::json!([nested]);
|
|
}
|
|
assert!(wire::response(&nested).is_ok());
|
|
assert!(
|
|
wire::response(&serde_json::json!([nested]))
|
|
.unwrap_err()
|
|
.contains("nesting")
|
|
);
|
|
|
|
let overhead = br#"{"code":0,"message":"ok","data":""}"#.len();
|
|
let maximum = "x".repeat(wire::MAX_BYTES - overhead);
|
|
assert_eq!(wire::response(&maximum).unwrap().len(), wire::MAX_BYTES);
|
|
assert!(
|
|
wire::response(&(maximum + "x"))
|
|
.unwrap_err()
|
|
.contains("16 MiB")
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn opaque_json_moves_text_and_composes_validated_values() {
|
|
let source = String::from(" {\"z\":1e9999,\"雪\":\"白\"} ");
|
|
let pointer = source.as_ptr();
|
|
let value = Json::from_text(source).unwrap();
|
|
assert_eq!(value.as_str().as_ptr(), pointer);
|
|
assert_eq!(value.property("z").unwrap().unwrap().as_str(), "1e9999");
|
|
assert_eq!(
|
|
value.property("雪").unwrap().unwrap().as_text().unwrap(),
|
|
"白"
|
|
);
|
|
assert!(value.property("missing").unwrap().is_none());
|
|
assert!(Json::null().property("x").is_err());
|
|
let replaced = value
|
|
.set_property("雪", &Json::integer(42).unwrap())
|
|
.unwrap();
|
|
assert_eq!(replaced.as_str(), "{\"z\":1e9999,\"雪\":42}");
|
|
assert_eq!(
|
|
replaced
|
|
.property("雪")
|
|
.unwrap()
|
|
.unwrap()
|
|
.as_integer()
|
|
.unwrap(),
|
|
42
|
|
);
|
|
assert!(value.property("z").unwrap().unwrap().as_integer().is_err());
|
|
let source = value.into_text();
|
|
assert_eq!(source.as_ptr(), pointer);
|
|
let mut fields = Map::with_capacity(1);
|
|
fields.put("a".into(), Json::boolean(true).unwrap());
|
|
let object = Json::object(&fields).unwrap();
|
|
assert_eq!(
|
|
Json::array(&[object, Json::string("雪").unwrap()])
|
|
.unwrap()
|
|
.as_str(),
|
|
"[{\"a\":true},\"雪\"]"
|
|
);
|
|
assert_eq!(
|
|
Json::from_text("{}".into())
|
|
.unwrap()
|
|
.set_property("a", &Json::null())
|
|
.unwrap()
|
|
.as_str(),
|
|
"{\"a\":null}"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn dynamic_wire_counts_all_raw_siblings_once_at_the_input_boundary() {
|
|
#[derive(wire::serde::Deserialize)]
|
|
#[serde(crate = "wire::serde", deny_unknown_fields)]
|
|
struct Pair {
|
|
left: Json,
|
|
right: Json,
|
|
}
|
|
let input = r#"{"left":{"n":1e9999},"right":null}"#;
|
|
let pair: Pair = wire::decode_dynamic(input, false).unwrap();
|
|
assert_eq!(pair.left.as_str(), r#"{"n":1e9999}"#);
|
|
assert_eq!(pair.right.as_str(), "null");
|
|
let array = format!("[{}0]", "0,".repeat(32767));
|
|
let input = format!(r#"{{"left":{array},"right":{array}}}"#);
|
|
assert!(
|
|
wire::decode_dynamic::<Pair>(&input, false)
|
|
.err()
|
|
.unwrap()
|
|
.contains("value limit")
|
|
);
|
|
}
|