Repository navigation
test: differential byte comparison against cyberphone/json-canonicalization #49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
hyperpolymath
wants to merge
2
commits into
main
Choose a base branch
from
beta/differential-cyberphone
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+5,206
−0
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| // SPDX-License-Identifier: MPL-2.0 | ||
| //! Differential test against an independent JCS implementation's published data | ||
| //! (cyberphone/json-canonicalization, Apache-2.0; see the vendor directory's | ||
| //! PROVENANCE.adoc). Outputs are compared byte for byte. A disagreement is a | ||
| //! finding about this implementation, never a reason to edit a vendored file: | ||
| //! the SHA256SUMS test below fails if one is edited. | ||
|
|
||
| mod support; | ||
|
|
||
| use ijson_jcs::{Value, parse_jcs, to_jcs}; | ||
| use std::fs; | ||
| use std::path::PathBuf; | ||
|
|
||
| fn vendor() -> PathBuf { | ||
| PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/vendor/cyberphone-json-canonicalization") | ||
| } | ||
|
|
||
| /// Index of the first differing byte, or the shorter length when one is a prefix. | ||
| fn first_difference(a: &[u8], b: &[u8]) -> Option<usize> { | ||
| if a == b { | ||
| return None; | ||
| } | ||
| Some(a.iter().zip(b).take_while(|(x, y)| x == y).count()) | ||
| } | ||
|
|
||
| #[test] | ||
| fn vendored_files_match_their_recorded_checksums() { | ||
| let sums = fs::read_to_string(vendor().join("SHA256SUMS")).unwrap(); | ||
| let mut count = 0; | ||
| for line in sums.lines() { | ||
| let (want, name) = line.split_once(" ").expect("sha256sum format"); | ||
| let bytes = fs::read(vendor().join(name)).unwrap_or_else(|e| panic!("{name}: {e}")); | ||
| assert_eq!(support::sha256::hex(&bytes), want, "{name} was modified"); | ||
| count += 1; | ||
| } | ||
| assert_eq!( | ||
| count, | ||
| 2 + 12, | ||
| "expected licence, es6 sample and 12 JSON files" | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn comparison_fires_on_a_planted_difference_and_is_silent_on_equal_bytes() { | ||
| let good = b"{\"a\":1}"; | ||
| assert_eq!(first_difference(good, good), None); | ||
| assert_eq!(first_difference(good, b"{\"a\":2}"), Some(5)); | ||
| assert_eq!(first_difference(good, b"{\"a\":1}\n"), Some(7)); | ||
| } | ||
|
|
||
| #[test] | ||
| fn published_input_output_pairs_are_byte_identical() { | ||
| let mut findings = Vec::new(); | ||
| for name in [ | ||
| "arrays", | ||
| "french", | ||
| "structures", | ||
| "unicode", | ||
| "values", | ||
| "weird", | ||
| ] { | ||
| let input = fs::read_to_string(vendor().join(format!("input/{name}.json"))).unwrap(); | ||
| let expected = fs::read(vendor().join(format!("output/{name}.json"))).unwrap(); | ||
| match parse_jcs(&input).map(|v| to_jcs(&v)) { | ||
| Ok(Ok(actual)) => { | ||
| if let Some(at) = first_difference(&actual, &expected) { | ||
| findings.push(format!( | ||
| "{name}: first byte difference at {at}\n ours: {}\n expected: {}", | ||
| String::from_utf8_lossy(&actual), | ||
| String::from_utf8_lossy(&expected) | ||
| )); | ||
| } | ||
| } | ||
| other => findings.push(format!("{name}: refused: {other:?}")), | ||
| } | ||
| } | ||
| assert!( | ||
| findings.is_empty(), | ||
| "disagreements:\n{}", | ||
| findings.join("\n") | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn es6_number_serialisation_sample_is_byte_identical() { | ||
| let text = fs::read_to_string(vendor().join("es6-numbers-head-5000.txt")).unwrap(); | ||
| let (mut checked, mut findings) = (0usize, Vec::new()); | ||
| for (line_no, line) in text.lines().enumerate() { | ||
| let (hex, expected) = line.split_once(',').expect("hex,expected"); | ||
| let bits = u64::from_str_radix(hex, 16).unwrap(); | ||
| let number = serde_json::Number::from_f64(f64::from_bits(bits)); | ||
| let Some(number) = number else { | ||
| findings.push(format!("line {}: {hex} is not finite", line_no + 1)); | ||
| continue; | ||
| }; | ||
| let actual = to_jcs(&Value::Number(number)).unwrap(); | ||
| if actual != expected.as_bytes() { | ||
| findings.push(format!( | ||
| "line {}: {hex}: ours {} expected {expected}", | ||
| line_no + 1, | ||
| String::from_utf8_lossy(&actual) | ||
| )); | ||
| } | ||
| checked += 1; | ||
| } | ||
| assert_eq!(checked + findings.len(), 5000); | ||
| assert!( | ||
| findings.is_empty(), | ||
| "disagreements:\n{}", | ||
| findings.join("\n") | ||
| ); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| Copyright 2018 Anders Rundgren | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| https://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. |
19 changes: 19 additions & 0 deletions
19
tests/vendor/cyberphone-json-canonicalization/PROVENANCE.adoc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| = Provenance: cyberphone/json-canonicalization test data | ||
|
|
||
| Independent JCS implementation used for differential testing | ||
| (`tests/differential_cyberphone.rs`). Vendored unmodified. | ||
|
|
||
| [cols="1,3",options="header"] | ||
| |=== | ||
| | Source repository | https://github.com/cyberphone/json-canonicalization | ||
| | Commit | `32a48195b9b20fe67b1795e39e4509f14d860b45` (shallow clone of default branch, 2026-12-01) | ||
| | Licence | Apache-2.0 (Copyright 2018 Anders Rundgren), file `LICENSE` here, as served by that repository | ||
| | `input/`, `output/` | `testdata/input/*.json` and `testdata/output/*.json` at that commit (byte-compared with the clone) | ||
| | `es6-numbers-head-5000.txt` | First 5000 lines of the release asset `es6testfile/es6testfile100m.txt.gz` (https://github.com/cyberphone/json-canonicalization/releases/download/es6testfile/es6testfile100m.txt.gz), retrieved 2026-12-01. The full 100-million-line file was not downloaded, so its checksum was not computed and the remaining lines are untested. The first lines are the file's edge cases; later lines are random doubles. | ||
| | Integrity | `SHA256SUMS` (checked by the test, so a silent edit of a vendored file fails) | ||
| |=== | ||
|
|
||
| These files are deliberately non-canonical JSON. They are not covered by the | ||
| own-JSON gate, which checks only `.ci`, `interop`, `docs` and | ||
| `tests/fixtures/standards/good`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| 6821faaddedf2d78c95bb6d98b127e9e616097afd2f6bcc34389f000d13ab12d LICENSE | ||
| 725b56df661eea335941c5dfc2b8c0a01d81161de11894d6ab9ecaae5f15dbf8 es6-numbers-head-5000.txt | ||
| e503b6d71d1afa595b1c74b1016445c944cd89f90418066b23de1aeda7d17563 input/arrays.json | ||
| 03676a951cd8753ac62589f72eb2105cc782c33425418cfe1d517c111f6e5d5a input/french.json | ||
| d66893805be1784116af50af3110d08766c70a6b4aad93374723f72346e7aaa6 input/structures.json | ||
| 4621864e014d4a805a563f55b9ea20aba4a2d2dc09c7394f625496998c00702c input/unicode.json | ||
| c4a041b503d6bc236036ef44db4dac499272f60fc22c40dc3b7a54870ba6f1c3 input/values.json | ||
| a3a905266bd4a49a969274ea69baa14ee0c4af0ead926d6fa2b7612b4af75387 input/weird.json | ||
| 099601b171cafed97c333f8878d68e7f8c8f795412adb34b2fdcf0e7c7beac42 output/arrays.json | ||
| d99d0ebdcb0033cb858cfa830ae46bc0fb3309413b271f1da828c89901a27ed5 output/french.json | ||
| 605f65004ec2db7692522a0852c22f1c989e036d547e88963d1a3143cf3195d5 output/structures.json | ||
| 0d99aad92a125196ff887876643fd3206786a84ddce2cee52ba4ad256d2381d3 output/unicode.json | ||
| 2d5e01a318d0f0879ab568c4be289c8b1f64ef8921a53c6277d5e069978baacb output/values.json | ||
| 6af595a9aa80110b964b4de3f82a05fa6ae7423005019bacfa2620dddc4e94d1 output/weird.json |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Count processed rows independently of disagreements.
If one of the 5,000 finite samples differs,
checkedbecomes 5,000 andfindings.len()becomes 1. This assertion then fails with 5,001 versus 5,000, before the next assertion can print the row, bit pattern and differing output.Count each processed row once, including non-finite rows. Keep the final
findings.is_empty()assertion to report disagreements. This preserves the passing case and makes differential failures diagnosable.