fix(domain): refuse three non-canonical v3 shapes, R5.5 LOW fixes, R5.6 plan
check_v3_shape at read_report_document v3 and parse_envelope (sheet, practitioner as non-objects, severity as non-string); Protocol errors final; DEV address must be an own unicast address. SQL confirm function and DEV keys characterised, no change.
This commit is contained in:
1 parent
13db5539a3
commit
d502dfa923
11 files changed
+369
-37
No files matched your search
@@ -1,14 +1,15 @@
|
||||
//! Characterisation, not a requirement (owner 2026-10-23, R5.5 brief §7): the serde derives of
|
||||
//! the R2 contracts read a struct from a positional array and a unit enum from a one-key map,
|
||||
//! where the frozen Zod reader refused both. These cases pin today's Rust verdict so that any
|
||||
//! change to the gated readers is visible; they are to be turned around, not extended, if the
|
||||
//! owner decides a strict deserialisation (`docs/development/RUST.md` §11.16).
|
||||
//! The serde tolerance of the R2 report contract (RUST.md §11.16): serde's derives read a struct
|
||||
//! from a positional array and a unit enum from a one-key map, where the frozen Zod reader
|
||||
//! refused both. Owner go 2026-10-24: the three v3 shapes found are refused at the readers'
|
||||
//! entry (`check_v3_shape`), with a controlled `ReadFailure::Invalid`; the canonical forms and
|
||||
//! the non-strict unknown fields are read as before (RUST.md §11.18).
|
||||
#![allow(clippy::disallowed_methods, clippy::unwrap_used, clippy::expect_used)]
|
||||
|
||||
mod common;
|
||||
|
||||
use faunosteo_core::domain::report::Report;
|
||||
use serde::Deserialize;
|
||||
use faunosteo_core::domain::archive::parse_envelope;
|
||||
use faunosteo_core::domain::report::read::check_v3_shape;
|
||||
use faunosteo_core::domain::report::{ReadFailure, ReportMigrationMap, read_report_document};
|
||||
use serde_json::{Value, json};
|
||||
|
||||
fn demo() -> Value {
|
||||
@@ -18,41 +19,90 @@ fn demo() -> Value {
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
fn read(value: &Value) -> Result<Report, serde_json::Error> {
|
||||
Report::deserialize(value)
|
||||
fn map() -> ReportMigrationMap {
|
||||
let (source, _) = common::zod_oracle("migration-map.draft.json");
|
||||
serde_json::from_str(&source).unwrap()
|
||||
}
|
||||
|
||||
fn read(value: &Value) -> Result<(), ReadFailure> {
|
||||
read_report_document(value, &map()).map(|_| ())
|
||||
}
|
||||
|
||||
fn sheet_as_array() -> Value {
|
||||
let mut report = demo();
|
||||
let parts = [
|
||||
report["sheet"]["sheetId"].clone(),
|
||||
Value::Null,
|
||||
report["sheet"]["documentSha256"].clone(),
|
||||
];
|
||||
report["sheet"] = json!(parts);
|
||||
report
|
||||
}
|
||||
|
||||
fn practitioner_as_array() -> Value {
|
||||
let mut report = demo();
|
||||
let p = report["practitioner"].clone();
|
||||
report["practitioner"] = json!([p["name"], p["cabinet"], p["contact"], p["siret"]]);
|
||||
report
|
||||
}
|
||||
|
||||
fn severity_as_map() -> Value {
|
||||
let mut report = demo();
|
||||
report["observations"][0]["severity"] = json!({ "mild": null });
|
||||
report
|
||||
}
|
||||
|
||||
fn refused_with(value: &Value, what: &str) {
|
||||
match read(value) {
|
||||
Err(ReadFailure::Invalid(message)) => assert!(message.starts_with(what), "{message}"),
|
||||
other => panic!("{what}: {other:?}"),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_canonical_report_is_read() {
|
||||
assert!(read(&demo()).is_ok());
|
||||
assert!(check_v3_shape(&demo()).is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_struct_written_as_a_positional_array_is_accepted_where_zod_refused() {
|
||||
let mut sheet = demo();
|
||||
let parts = [
|
||||
sheet["sheet"]["sheetId"].clone(),
|
||||
Value::Null,
|
||||
sheet["sheet"]["documentSha256"].clone(),
|
||||
];
|
||||
sheet["sheet"] = json!(parts);
|
||||
let mut practitioner = demo();
|
||||
let p = practitioner["practitioner"].clone();
|
||||
practitioner["practitioner"] = json!([p["name"], p["cabinet"], p["contact"], p["siret"]]);
|
||||
assert!(read(&sheet).is_ok());
|
||||
assert!(read(&practitioner).is_ok());
|
||||
fn a_struct_written_as_a_positional_array_is_refused() {
|
||||
refused_with(&sheet_as_array(), "sheet: expected an object");
|
||||
refused_with(&practitioner_as_array(), "practitioner: expected an object");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_unit_enum_written_as_a_one_key_map_is_accepted_where_zod_refused() {
|
||||
let mut report = demo();
|
||||
report["observations"][0]["severity"] = json!({ "mild": null });
|
||||
assert!(read(&report).is_ok());
|
||||
fn a_unit_enum_written_as_a_one_key_map_is_refused() {
|
||||
refused_with(
|
||||
&severity_as_map(),
|
||||
"observations[0].severity: expected a string",
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_unknown_field_is_accepted_as_the_non_strict_zod_objects_did() {
|
||||
fn an_absent_severity_and_an_unknown_field_are_read_as_before() {
|
||||
let mut report = demo();
|
||||
report["observations"][0]
|
||||
.as_object_mut()
|
||||
.unwrap()
|
||||
.remove("severity");
|
||||
report["extra"] = json!(1);
|
||||
assert!(read(&report).is_ok());
|
||||
// `severity` is optional, never nullable: null stays refused, as Zod refused it.
|
||||
report["observations"][0]["severity"] = Value::Null;
|
||||
refused_with(&report, "observations[0].severity: expected a string");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_envelope_entry_refuses_the_same_shapes() {
|
||||
let envelope: Value = serde_json::from_str(&common::repo_file(
|
||||
"apps/app/tests/fixtures/goldens/r2/envelope.demo.fr.nologos.json",
|
||||
))
|
||||
.unwrap();
|
||||
assert!(parse_envelope(&envelope).is_ok());
|
||||
for bad in [sheet_as_array(), practitioner_as_array(), severity_as_map()] {
|
||||
let mut wrapped = envelope.clone();
|
||||
wrapped["report"] = bad;
|
||||
assert!(parse_envelope(&wrapped).is_err());
|
||||
}
|
||||
}
|
||||
@@ -25,6 +25,8 @@ struct State {
|
||||
row: Option<(String, bool)>,
|
||||
bucket_status: u16,
|
||||
fail_confirm: bool,
|
||||
/// Answer every request with bytes that are not HTTP.
|
||||
garbage: bool,
|
||||
log: Vec<String>,
|
||||
}
|
||||
|
||||
@@ -121,7 +123,14 @@ fn serve(state: Shared) -> u16 {
|
||||
for stream in listener.incoming() {
|
||||
let mut stream = stream.unwrap();
|
||||
let (method, path, body) = read_request(&mut stream);
|
||||
let (status, reply) = route(&mut state.lock().unwrap(), &method, &path, body);
|
||||
let mut held = state.lock().unwrap();
|
||||
if held.garbage {
|
||||
held.log.push(format!("{method} {path}"));
|
||||
stream.write_all(b"NOT-HTTP garbage\r\n\r\n").unwrap();
|
||||
continue;
|
||||
}
|
||||
let (status, reply) = route(&mut held, &method, &path, body);
|
||||
drop(held);
|
||||
respond(&mut stream, status, &reply);
|
||||
}
|
||||
});
|
||||
@@ -352,3 +361,14 @@ fn a_dev_address_that_is_not_this_machine_is_ignored() {
|
||||
assert_eq!(output.status.code(), Some(1), "{said}");
|
||||
assert!(said.contains("Destination"), "{said}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_malformed_answer_is_final_and_not_retried() {
|
||||
let (state, url) = stub();
|
||||
state.lock().unwrap().garbage = true;
|
||||
let output = run(&url, &["--check", DRAFT]);
|
||||
let said = text(&output);
|
||||
assert_eq!(output.status.code(), Some(1), "{said}");
|
||||
assert!(said.contains("not retried"), "{said}");
|
||||
assert_eq!(state.lock().unwrap().log.len(), 1);
|
||||
}
|
||||
Reference in new issue
Block a user