synapse
A component of the AienOS ecosystem.
Brought by drakestapleton
synapse / crates / synapse-store / src / j1_corpus.rs
| 1 | //! J1 cross-check corpus for the Omega port of the binary codec (ADR 0003 section 9 step 33). |
| 2 | //! |
| 3 | //! The Rust codec (`codec.rs`) is the specification by test (ADR 0003 section 5.12). This |
| 4 | //! module writes what "decodes and re-encodes every record byte for byte, refuses the |
| 5 | //! malformed corpus identically" means for a port into one language-neutral file, sealed in |
| 6 | //! `evidence/j1-codec-corpus/corpus.bin`: every base record and checkpoint content with its |
| 7 | //! logical form, and every A1 malformed case (the mutation set of `a1_malformed_corpus.rs`) |
| 8 | //! with the outcome the Rust decoder gives. The format and the logical form are specified in |
| 9 | //! `docs/J1_OMEGA_CODEC.md`. |
| 10 | //! |
| 11 | //! `J1_CORPUS_WRITE=1 cargo test -p synapse-store --lib j1_corpus` rewrites the file; without |
| 12 | //! it the test only checks that the sealed file is what the codec gives today. |
| 13 | |
| 14 | use super::a1_tests::{edge_records, fixture_dir, fixture_json_records}; |
| 15 | use super::*; |
| 16 | use crate::codec::*; |
| 17 | use ed25519_dalek::SigningKey; |
| 18 | use synapse_core::{ChangeId, FileMode, PathMutation, RevisionContent, TreeDigest}; |
| 19 | |
| 20 | /// The sealed corpus, relative to the repository root. |
| 21 | const CORPUS_PATH: &str = "evidence/j1-codec-corpus/corpus.bin"; |
| 22 | /// SHA-256 of the sealed corpus, so the seal is checked by a test, not by hand. |
| 23 | const CORPUS_SHA256: &str = "2597e5ca9b9dfc80be67cdc7436143df462a42bc54e1504d38656730d7eb5ca6"; |
| 24 | const MAGIC: [u8; 8] = *b"SYNJ1COD"; |
| 25 | const VERSION: u16 = 1; |
| 26 | /// Record cap of the A1 malformed corpus (`a1_malformed_corpus.rs`). |
| 27 | const CAP: u64 = 2048; |
| 28 | |
| 29 | // Logical-form token types (docs/J1_OMEGA_CODEC.md section 3). |
| 30 | const T_U16: u64 = 2; |
| 31 | const T_U64: u64 = 4; |
| 32 | const T_BOOL: u64 = 5; |
| 33 | const T_TAG: u64 = 6; |
| 34 | const T_FIX: u64 = 7; |
| 35 | const T_STR: u64 = 8; |
| 36 | const T_BYTES: u64 = 9; |
| 37 | const T_REPO: u64 = 10; |
| 38 | const T_COUNT: u64 = 11; |
| 39 | |
| 40 | /// Field names a refusal can carry, in id order (id = index + 1; 0 means none). |
| 41 | const FIELDS: [&str; 15] = [ |
| 42 | "bool", |
| 43 | "option", |
| 44 | "mutation op", |
| 45 | "file mode", |
| 46 | "record kind", |
| 47 | "envelope v2", |
| 48 | "chain path", |
| 49 | "obsolescence marker", |
| 50 | "changesets", |
| 51 | "idempotency index", |
| 52 | "blob visibility", |
| 53 | "projections", |
| 54 | "canonical heads", |
| 55 | "revision records", |
| 56 | "revision record kind", |
| 57 | ]; |
| 58 | /// Refusal codes an embedded `synapse-core` decoder gives, in id order (id = index + 1). |
| 59 | const CODES: [RefusalCode; 2] = [ |
| 60 | RefusalCode::EnvelopeMalformed, |
| 61 | RefusalCode::EnvelopeVersionUnsupported, |
| 62 | ]; |
| 63 | /// `CodecRefusal` kinds in id order (id = index + 1). |
| 64 | const KINDS: [&str; 9] = [ |
| 65 | "Truncated", |
| 66 | "LengthOverCap", |
| 67 | "TrailingBytes", |
| 68 | "UnknownTag", |
| 69 | "InvalidUtf8", |
| 70 | "VersionUnsupported", |
| 71 | "NotCanonical", |
| 72 | "InvalidRepoId", |
| 73 | "EmbeddedMalformed", |
| 74 | ]; |
| 75 | |
| 76 | const ENTRY_RECORD: u8 = 1; |
| 77 | const ENTRY_CHECKPOINT: u8 = 2; |
| 78 | /// A refused base is itself the case input (op 0). |
| 79 | const OP_AS_IS: u8 = 0; |
| 80 | const OP_TRUNCATE: u8 = 1; |
| 81 | const OP_FLIP: u8 = 2; |
| 82 | const OP_FALSE_LENGTH: u8 = 3; |
| 83 | |
| 84 | // --------------------------------------------------------------------------------------- |
| 85 | // Logical form: the typed fields of a record, in encoding order, without derived lengths |
| 86 | // or constant bytes. |
| 87 | // --------------------------------------------------------------------------------------- |
| 88 | |
| 89 | #[derive(Default)] |
| 90 | struct Lf(Vec<u64>); |
| 91 | |
| 92 | impl Lf { |
| 93 | fn scalar(&mut self, t: u64, v: u64) { |
| 94 | self.0.push(t | 1 << 8); |
| 95 | self.0.push(v); |
| 96 | } |
| 97 | fn run(&mut self, t: u64, b: &[u8]) { |
| 98 | self.0.push(t | (b.len() as u64) << 8); |
| 99 | self.0.extend(b.iter().map(|&x| u64::from(x))); |
| 100 | } |
| 101 | fn u64(&mut self, v: u64) { |
| 102 | self.scalar(T_U64, v); |
| 103 | } |
| 104 | fn bool(&mut self, v: bool) { |
| 105 | self.scalar(T_BOOL, u64::from(v)); |
| 106 | } |
| 107 | fn tag(&mut self, v: u8) { |
| 108 | self.scalar(T_TAG, u64::from(v)); |
| 109 | } |
| 110 | fn count(&mut self, n: usize) { |
| 111 | self.scalar(T_COUNT, n as u64); |
| 112 | } |
| 113 | fn fix(&mut self, b: &[u8]) { |
| 114 | self.run(T_FIX, b); |
| 115 | } |
| 116 | fn str(&mut self, s: &str) { |
| 117 | self.run(T_STR, s.as_bytes()); |
| 118 | } |
| 119 | fn repo(&mut self, r: &RepoId) { |
| 120 | self.run(T_REPO, r.as_str().as_bytes()); |
| 121 | } |
| 122 | |
| 123 | fn attachment(&mut self, a: &EvidenceAttachment) { |
| 124 | self.str(&a.gate_name); |
| 125 | self.bool(a.passed); |
| 126 | self.fix(&a.artifact_digest); |
| 127 | self.str(&a.summary); |
| 128 | } |
| 129 | fn attachments(&mut self, v: &[EvidenceAttachment]) { |
| 130 | self.count(v.len()); |
| 131 | v.iter().for_each(|a| self.attachment(a)); |
| 132 | } |
| 133 | fn mutations(&mut self, v: &[PathMutation]) { |
| 134 | self.count(v.len()); |
| 135 | for m in v { |
| 136 | self.str(&m.path); |
| 137 | match &m.op { |
| 138 | MutationOp::Upsert { |
| 139 | mode, |
| 140 | blob_digest, |
| 141 | size_bytes, |
| 142 | } => { |
| 143 | self.tag(1); |
| 144 | self.tag(mode.tag_byte()); |
| 145 | self.fix(&blob_digest.0); |
| 146 | self.u64(*size_bytes); |
| 147 | } |
| 148 | MutationOp::Delete => self.tag(2), |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | fn ids(&mut self, v: impl ExactSizeIterator<Item = [u8; 32]>) { |
| 153 | self.count(v.len()); |
| 154 | v.for_each(|id| self.fix(&id)); |
| 155 | } |
| 156 | fn envelope(&mut self, e: &ChangesetEnvelope) { |
| 157 | self.scalar(T_U16, u64::from(e.version)); |
| 158 | self.repo(&e.repo_id); |
| 159 | self.fix(&e.base_git_oid.0); |
| 160 | self.ids(e.parent_changesets.iter().map(|p| p.0)); |
| 161 | self.str(&e.author_did); |
| 162 | self.fix(&e.idempotency_key.0); |
| 163 | self.u64(e.created_at_ms); |
| 164 | self.str(&e.intent_summary); |
| 165 | self.mutations(&e.mutations); |
| 166 | self.attachments(&e.evidence_receipts); |
| 167 | self.run(T_BYTES, &e.signature); |
| 168 | } |
| 169 | fn review(&mut self, r: &ReviewRecord) { |
| 170 | self.str(&r.reviewer_did); |
| 171 | self.bool(r.approved); |
| 172 | self.str(&r.comment); |
| 173 | self.u64(r.recorded_at_ms); |
| 174 | } |
| 175 | fn projection(&mut self, p: &GitHubPullRequestProjection) { |
| 176 | self.repo(&p.repo_id); |
| 177 | self.u64(p.pr_number); |
| 178 | self.fix(&p.changeset_id.0); |
| 179 | self.fix(&p.head_git_oid.0); |
| 180 | self.fix(&p.base_git_oid.0); |
| 181 | self.str(&p.pr_branch_ref); |
| 182 | self.str(&p.title); |
| 183 | self.str(&p.body_markdown); |
| 184 | self.fix(&p.evidence_summary_digest); |
| 185 | self.bool(p.github_merged); |
| 186 | } |
| 187 | fn canonical(&mut self, c: &RadicleCanonicalHead) { |
| 188 | self.repo(&c.repo_id); |
| 189 | self.str(&c.default_branch); |
| 190 | self.fix(&c.canonical_git_oid.0); |
| 191 | match &c.promoted_changeset_id { |
| 192 | Some(id) => { |
| 193 | self.tag(1); |
| 194 | self.fix(&id.0); |
| 195 | } |
| 196 | None => self.tag(0), |
| 197 | } |
| 198 | self.fix(&c.rad_id_commit_oid.0); |
| 199 | self.u64(c.threshold as u64); |
| 200 | self.count(c.verified_delegates.len()); |
| 201 | c.verified_delegates.iter().for_each(|d| self.str(d)); |
| 202 | self.u64(c.verified_at_ms); |
| 203 | } |
| 204 | fn stored(&mut self, s: &StoredChangeset) { |
| 205 | self.fix(&s.changeset_id.0); |
| 206 | self.u64(s.committed_seq.0); |
| 207 | self.envelope(&s.envelope); |
| 208 | self.attachments(&s.evaluations); |
| 209 | self.count(s.reviews.len()); |
| 210 | s.reviews.iter().for_each(|r| self.review(r)); |
| 211 | self.u64(s.lease_expires_at_ms); |
| 212 | } |
| 213 | fn chain(&mut self, c: &ChainPath) { |
| 214 | self.ids(c.links().iter().copied()); |
| 215 | } |
| 216 | fn record(&mut self, r: &WalRecord) { |
| 217 | self.tag(r.kind_byte()); |
| 218 | match r { |
| 219 | WalRecord::ChangesetIngest { |
| 220 | envelope, |
| 221 | changeset_id, |
| 222 | new_blobs, |
| 223 | } => { |
| 224 | self.envelope(envelope); |
| 225 | self.fix(&changeset_id.0); |
| 226 | self.ids(new_blobs.iter().map(|b| b.0)); |
| 227 | } |
| 228 | WalRecord::EvaluationAttached { |
| 229 | changeset_id, |
| 230 | attachment, |
| 231 | } => { |
| 232 | self.fix(&changeset_id.0); |
| 233 | self.attachment(attachment); |
| 234 | } |
| 235 | WalRecord::ReviewRecorded { |
| 236 | changeset_id, |
| 237 | review, |
| 238 | } => { |
| 239 | self.fix(&changeset_id.0); |
| 240 | self.review(review); |
| 241 | } |
| 242 | WalRecord::GitProjected { projection } => self.projection(projection), |
| 243 | WalRecord::CanonicalPromoted { canonical } => self.canonical(canonical), |
| 244 | WalRecord::LeaseUpdated { |
| 245 | changeset_id, |
| 246 | lease_expires_at_ms, |
| 247 | } => { |
| 248 | self.fix(&changeset_id.0); |
| 249 | self.u64(*lease_expires_at_ms); |
| 250 | } |
| 251 | WalRecord::RevisionAdmitted { |
| 252 | envelope, |
| 253 | chain, |
| 254 | admitted_at_ms, |
| 255 | new_blobs, |
| 256 | seal: _, |
| 257 | } => { |
| 258 | let c = &envelope.content; |
| 259 | self.fix(&c.change_id.0); |
| 260 | self.fix(&c.base_tree.0); |
| 261 | self.ids(c.parents.iter().map(|p| p.0)); |
| 262 | self.mutations(&c.mutations); |
| 263 | self.str(&c.intent_summary); |
| 264 | let a = &envelope.attestation; |
| 265 | self.repo(&a.repo_id); |
| 266 | self.fix(&a.changeset_id.0); |
| 267 | self.fix(&a.author_key); |
| 268 | self.fix(&a.idempotency_key.0); |
| 269 | self.u64(a.attested_at_ms); |
| 270 | self.fix(&a.signature); |
| 271 | self.chain(chain); |
| 272 | self.u64(*admitted_at_ms); |
| 273 | self.ids(new_blobs.iter().map(|b| b.0)); |
| 274 | } |
| 275 | WalRecord::ObsolescenceRecorded { |
| 276 | marker, |
| 277 | chain, |
| 278 | recorded_at_ms, |
| 279 | seal: _, |
| 280 | } => { |
| 281 | self.repo(&marker.repo_id); |
| 282 | self.fix(&marker.change_id.0); |
| 283 | self.fix(&marker.predecessor.0); |
| 284 | self.ids(marker.successors.iter().map(|s| s.0)); |
| 285 | self.fix(&marker.signer_key); |
| 286 | self.u64(marker.signed_at_ms); |
| 287 | self.fix(&marker.signature); |
| 288 | self.chain(chain); |
| 289 | self.u64(*recorded_at_ms); |
| 290 | } |
| 291 | // Kinds 9 to 11 landed on main after this corpus was sealed; the corpus covers |
| 292 | // kinds 1 to 8 only (see the coverage test), so no corpus record reaches here. |
| 293 | WalRecord::RevisionStateMoved { .. } |
| 294 | | WalRecord::GitBridgeImported { .. } |
| 295 | | WalRecord::DerivedRevisionAdmitted { .. } => { |
| 296 | unreachable!("J1 corpus does not yet cover record kinds 9 to 11") |
| 297 | } |
| 298 | } |
| 299 | } |
| 300 | fn checkpoint(&mut self, s: &CheckpointState) { |
| 301 | self.count(s.changesets.len()); |
| 302 | s.changesets.values().for_each(|c| self.stored(c)); |
| 303 | self.count(s.idempotency_index.len()); |
| 304 | for ((repo, key), (seq, id)) in &s.idempotency_index { |
| 305 | self.repo(repo); |
| 306 | self.fix(&key.0); |
| 307 | self.u64(seq.0); |
| 308 | self.fix(&id.0); |
| 309 | } |
| 310 | self.count(s.blob_first_visible_seq.len()); |
| 311 | for (d, seq) in &s.blob_first_visible_seq { |
| 312 | self.fix(&d.0); |
| 313 | self.u64(seq.0); |
| 314 | } |
| 315 | self.count(s.github_projections.len()); |
| 316 | s.github_projections |
| 317 | .values() |
| 318 | .for_each(|p| self.projection(p)); |
| 319 | self.count(s.canonical_heads.len()); |
| 320 | s.canonical_heads.values().for_each(|c| self.canonical(c)); |
| 321 | // Always present in the logical form; the bytes hold the section only when n > 0. |
| 322 | self.count(s.revision_log.len()); |
| 323 | for (seq, r) in &s.revision_log { |
| 324 | self.u64(seq.0); |
| 325 | self.record(r); |
| 326 | } |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | // --------------------------------------------------------------------------------------- |
| 331 | // Bases and outcomes |
| 332 | // --------------------------------------------------------------------------------------- |
| 333 | |
| 334 | /// A test signing key derived from a label (no literal key material in the source). |
| 335 | fn derived_key(label: &str) -> SigningKey { |
| 336 | SigningKey::from_bytes(&Sha256::digest(label.as_bytes()).into()) |
| 337 | } |
| 338 | |
| 339 | fn derived32(label: &str) -> [u8; 32] { |
| 340 | Sha256::digest(label.as_bytes()).into() |
| 341 | } |
| 342 | |
| 343 | fn repo() -> RepoId { |
| 344 | RepoId::new("aien-dev/synapse").unwrap() |
| 345 | } |
| 346 | |
| 347 | fn chain(depth: usize) -> ChainPath { |
| 348 | ChainPath::new( |
| 349 | (0..depth) |
| 350 | .map(|i| derived32(&format!("j1 link {i}"))) |
| 351 | .collect(), |
| 352 | ) |
| 353 | .unwrap() |
| 354 | } |
| 355 | |
| 356 | /// Kind 7 and kind 8 records (on `main`; kinds 9 to 11 are reserved by open PRs). |
| 357 | fn revision_records() -> Vec<WalRecord> { |
| 358 | let author = derived_key("j1 corpus author"); |
| 359 | let blob = |s: &str| BlobDigest::compute(s.as_bytes()); |
| 360 | let mut parents = vec![ |
| 361 | ChangesetId(derived32("j1 parent a")), |
| 362 | ChangesetId(derived32("j1 parent b")), |
| 363 | ]; |
| 364 | parents.sort(); |
| 365 | let content = RevisionContent { |
| 366 | change_id: ChangeId(derived32("j1 change")[..16].try_into().unwrap()), |
| 367 | base_tree: TreeDigest(derived32("j1 base tree")), |
| 368 | parents, |
| 369 | mutations: vec![ |
| 370 | PathMutation { |
| 371 | path: "bin/run".into(), |
| 372 | op: MutationOp::Upsert { |
| 373 | mode: FileMode::Executable755, |
| 374 | blob_digest: blob("run"), |
| 375 | size_bytes: 3, |
| 376 | }, |
| 377 | }, |
| 378 | PathMutation { |
| 379 | path: "old.txt".into(), |
| 380 | op: MutationOp::Delete, |
| 381 | }, |
| 382 | PathMutation { |
| 383 | path: "src/lib.rs".into(), |
| 384 | op: MutationOp::Upsert { |
| 385 | mode: FileMode::Regular644, |
| 386 | blob_digest: blob("lib"), |
| 387 | size_bytes: u64::MAX, |
| 388 | }, |
| 389 | }, |
| 390 | ], |
| 391 | intent_summary: "j1: été 🦀".into(), |
| 392 | }; |
| 393 | let change_id = content.change_id; |
| 394 | let env = ChangesetEnvelopeV2::sign_new( |
| 395 | &author, |
| 396 | repo(), |
| 397 | content, |
| 398 | IdempotencyKey(derived32("j1 idempotency")), |
| 399 | 1_000, |
| 400 | ); |
| 401 | let id = env.changeset_id(); |
| 402 | let mut successors = vec![ |
| 403 | ChangesetId(derived32("j1 successor a")), |
| 404 | ChangesetId(derived32("j1 successor b")), |
| 405 | ]; |
| 406 | successors.sort(); |
| 407 | vec![ |
| 408 | WalRecord::RevisionAdmitted { |
| 409 | envelope: env, |
| 410 | chain: chain(3), |
| 411 | admitted_at_ms: 5_000, |
| 412 | new_blobs: vec![blob("run"), blob("lib")], |
| 413 | seal: None, |
| 414 | }, |
| 415 | WalRecord::ObsolescenceRecorded { |
| 416 | marker: ObsolescenceMarker::sign_new(&author, repo(), change_id, id, successors, 6_000), |
| 417 | chain: chain(1), |
| 418 | recorded_at_ms: 7_000, |
| 419 | seal: None, |
| 420 | }, |
| 421 | WalRecord::ObsolescenceRecorded { |
| 422 | marker: ObsolescenceMarker::sign_new( |
| 423 | &author, |
| 424 | RepoId::new("x").unwrap(), |
| 425 | change_id, |
| 426 | id, |
| 427 | vec![], |
| 428 | 0, |
| 429 | ), |
| 430 | chain: chain(16), |
| 431 | recorded_at_ms: u64::MAX, |
| 432 | seal: None, |
| 433 | }, |
| 434 | ] |
| 435 | } |
| 436 | |
| 437 | fn fixture_state() -> CheckpointState { |
| 438 | let manifest: CheckpointManifest = |
| 439 | serde_json::from_slice(&fs::read(fixture_dir("v1-wal-log").join("checkpoint.v1")).unwrap()) |
| 440 | .unwrap(); |
| 441 | CheckpointState::from_v1_manifest(manifest).unwrap() |
| 442 | } |
| 443 | |
| 444 | /// A checkpoint with a revision section (kinds 7 and 8). |
| 445 | fn revision_state() -> CheckpointState { |
| 446 | let r = revision_records(); |
| 447 | let mut s = CheckpointState { |
| 448 | checkpoint_seq: WalSeq(9), |
| 449 | anchor_frame_sha256: derived32("j1 anchor"), |
| 450 | ..Default::default() |
| 451 | }; |
| 452 | for (i, label) in ["run", "lib"].iter().enumerate() { |
| 453 | s.blob_first_visible_seq |
| 454 | .insert(BlobDigest::compute(label.as_bytes()), WalSeq(4 + i as u64)); |
| 455 | } |
| 456 | s.revision_log = vec![(WalSeq(4), r[0].clone()), (WalSeq(9), r[1].clone())]; |
| 457 | s |
| 458 | } |
| 459 | |
| 460 | /// One base: a record or a checkpoint content, its bytes, and its logical form. |
| 461 | struct Base { |
| 462 | entry: u8, |
| 463 | cap: u64, |
| 464 | checkpoint_seq: u64, |
| 465 | anchor: [u8; 32], |
| 466 | label: String, |
| 467 | bytes: Vec<u8>, |
| 468 | /// `None`: a hand-made input the decoder refuses (no logical form, one op-0 case). |
| 469 | lf: Option<Vec<u64>>, |
| 470 | mutate: bool, |
| 471 | } |
| 472 | |
| 473 | fn record_base(label: String, r: &WalRecord, mutate: bool) -> Base { |
| 474 | let mut lf = Lf::default(); |
| 475 | lf.record(r); |
| 476 | Base { |
| 477 | entry: ENTRY_RECORD, |
| 478 | cap: CAP, |
| 479 | checkpoint_seq: 0, |
| 480 | anchor: [0u8; 32], |
| 481 | label, |
| 482 | bytes: encode_record(r).unwrap(), |
| 483 | lf: Some(lf.0), |
| 484 | mutate, |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | fn checkpoint_base(label: &str, s: &CheckpointState) -> Base { |
| 489 | let mut lf = Lf::default(); |
| 490 | lf.checkpoint(s); |
| 491 | Base { |
| 492 | entry: ENTRY_CHECKPOINT, |
| 493 | cap: 0, |
| 494 | checkpoint_seq: s.checkpoint_seq.0, |
| 495 | anchor: s.anchor_frame_sha256, |
| 496 | label: label.into(), |
| 497 | bytes: s.encode_content().unwrap(), |
| 498 | lf: Some(lf.0), |
| 499 | mutate: true, |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | fn bases() -> Vec<Base> { |
| 504 | let mut out = Vec::new(); |
| 505 | let mut first_of_kind = BTreeSet::new(); |
| 506 | for (i, json) in fixture_json_records().iter().enumerate() { |
| 507 | let r: WalRecord = serde_json::from_slice(json).unwrap(); |
| 508 | // The A1 malformed corpus mutates the first record of each kind of the v1 log. |
| 509 | let mutate = first_of_kind.insert(r.kind_byte()); |
| 510 | out.push(record_base(format!("fixture record {i}"), &r, mutate)); |
| 511 | } |
| 512 | for (i, r) in edge_records().iter().enumerate() { |
| 513 | out.push(record_base(format!("edge record {i}"), r, false)); |
| 514 | } |
| 515 | for (i, r) in revision_records().iter().enumerate() { |
| 516 | out.push(record_base(format!("revision record {i}"), r, i < 2)); |
| 517 | } |
| 518 | let fixture = checkpoint_base("fixture checkpoint", &fixture_state()); |
| 519 | let revision = checkpoint_base("revision checkpoint", &revision_state()); |
| 520 | // Hand-made inputs no mutation reaches: record kinds 9 to 11 with a cut body (refused; all |
| 521 | // three kinds decode on `main` since B4, H1 and B5), a revision section that is empty, and |
| 522 | // one that holds a kind 6 record. |
| 523 | for kind in 9u8..=11 { |
| 524 | out.push(refused_base( |
| 525 | format!("reserved record kind {kind}"), |
| 526 | ENTRY_RECORD, |
| 527 | CAP, |
| 528 | 0, |
| 529 | [0u8; 32], |
| 530 | vec![CODEC_V2, kind, 0, 0], |
| 531 | )); |
| 532 | } |
| 533 | let mut empty_section = fixture.bytes.clone(); |
| 534 | empty_section.extend_from_slice(&0u32.to_le_bytes()); |
| 535 | out.push(refused_base( |
| 536 | "empty revision section".into(), |
| 537 | ENTRY_CHECKPOINT, |
| 538 | 0, |
| 539 | fixture.checkpoint_seq, |
| 540 | fixture.anchor, |
| 541 | empty_section, |
| 542 | )); |
| 543 | // Two projections, the second renumbered below the first: keys out of order. |
| 544 | let mut two = fixture_state(); |
| 545 | let mut p = two.github_projections.values().next().unwrap().clone(); |
| 546 | let first = p.pr_number; |
| 547 | p.pr_number = first + 1; |
| 548 | let p_bytes = encode_record(&WalRecord::GitProjected { |
| 549 | projection: p.clone(), |
| 550 | }) |
| 551 | .unwrap()[2..] |
| 552 | .to_vec(); |
| 553 | two.github_projections.insert(p.pr_number, p.clone()); |
| 554 | let mut misordered = two.encode_content().unwrap(); |
| 555 | let at = misordered |
| 556 | .windows(p_bytes.len()) |
| 557 | .position(|w| w == p_bytes.as_slice()) |
| 558 | .unwrap(); |
| 559 | let num_at = at + 4 + p.repo_id.as_str().len(); |
| 560 | misordered[num_at..num_at + 8].copy_from_slice(&(first - 1).to_le_bytes()); |
| 561 | out.push(refused_base( |
| 562 | "projections out of order".into(), |
| 563 | ENTRY_CHECKPOINT, |
| 564 | 0, |
| 565 | two.checkpoint_seq.0, |
| 566 | two.anchor_frame_sha256, |
| 567 | misordered, |
| 568 | )); |
| 569 | out.extend(equal_key_bases(&fixture_state())); |
| 570 | out.extend(repo_id_bases()); |
| 571 | let mut same_seq = revision_state(); |
| 572 | same_seq.revision_log[1].0 = same_seq.revision_log[0].0; |
| 573 | out.push(refused_base( |
| 574 | "revision records with equal seqs".into(), |
| 575 | ENTRY_CHECKPOINT, |
| 576 | 0, |
| 577 | same_seq.checkpoint_seq.0, |
| 578 | same_seq.anchor_frame_sha256, |
| 579 | same_seq.encode_content().unwrap(), |
| 580 | )); |
| 581 | let mut wrong_kind = revision_state(); |
| 582 | let lease = WalRecord::LeaseUpdated { |
| 583 | changeset_id: ChangesetId(derived32("j1 lease")), |
| 584 | lease_expires_at_ms: 1, |
| 585 | }; |
| 586 | wrong_kind.revision_log = vec![(WalSeq(2), lease)]; |
| 587 | out.push(refused_base( |
| 588 | "revision section holding kind 6".into(), |
| 589 | ENTRY_CHECKPOINT, |
| 590 | 0, |
| 591 | wrong_kind.checkpoint_seq.0, |
| 592 | wrong_kind.anchor_frame_sha256, |
| 593 | wrong_kind.encode_content().unwrap(), |
| 594 | )); |
| 595 | out.push(fixture); |
| 596 | out.push(revision); |
| 597 | out |
| 598 | } |
| 599 | |
| 600 | /// Hand-made checkpoints whose map holds two entries with equal keys (each map's keys must be |
| 601 | /// strictly increasing): the map's first entry is cloned under a fresh key above it, the state |
| 602 | /// is encoded, and the clone's key bytes are set back to the first key. |
| 603 | fn equal_key_bases(s: &CheckpointState) -> Vec<Base> { |
| 604 | fn above(mut k: [u8; 32]) -> [u8; 32] { |
| 605 | assert!(k[31] < 255, "test key ends in 0xff"); |
| 606 | k[31] += 1; |
| 607 | k |
| 608 | } |
| 609 | /// `content` with its only occurrence of `from` replaced by `to` (lengths may differ: the |
| 610 | /// patched run is a length-prefixed string or fixed bytes). |
| 611 | fn patched(content: &[u8], from: &[u8], to: &[u8]) -> Vec<u8> { |
| 612 | let hits: Vec<usize> = content |
| 613 | .windows(from.len()) |
| 614 | .enumerate() |
| 615 | .filter(|(_, w)| *w == from) |
| 616 | .map(|(i, _)| i) |
| 617 | .collect(); |
| 618 | assert_eq!(hits.len(), 1, "the clone's key must occur exactly once"); |
| 619 | let mut v = content.to_vec(); |
| 620 | v.splice(hits[0]..hits[0] + from.len(), to.iter().copied()); |
| 621 | v |
| 622 | } |
| 623 | let le = |v: u64| v.to_le_bytes(); |
| 624 | let mut out = Vec::new(); |
| 625 | let mut push = |label: &str, t: &CheckpointState, from: Vec<u8>, to: Vec<u8>| { |
| 626 | out.push(refused_base( |
| 627 | format!("equal keys: {label}"), |
| 628 | ENTRY_CHECKPOINT, |
| 629 | 0, |
| 630 | t.checkpoint_seq.0, |
| 631 | t.anchor_frame_sha256, |
| 632 | patched(&t.encode_content().unwrap(), &from, &to), |
| 633 | )); |
| 634 | }; |
| 635 | |
| 636 | let (id, first) = s.changesets.iter().next().expect("fixture changeset"); |
| 637 | let mut t = s.clone(); |
| 638 | let mut c = first.clone(); |
| 639 | c.changeset_id = ChangesetId(above(id.0)); |
| 640 | t.changesets.insert(c.changeset_id, c.clone()); |
| 641 | let seq = le(c.committed_seq.0); |
| 642 | push( |
| 643 | "changesets", |
| 644 | &t, |
| 645 | [&c.changeset_id.0[..], &seq].concat(), |
| 646 | [&id.0[..], &seq].concat(), |
| 647 | ); |
| 648 | |
| 649 | let ((repo, key), v) = s |
| 650 | .idempotency_index |
| 651 | .iter() |
| 652 | .next() |
| 653 | .expect("fixture idempotency entry"); |
| 654 | let mut t = s.clone(); |
| 655 | let key2 = IdempotencyKey(above(key.0)); |
| 656 | t.idempotency_index.insert((repo.clone(), key2), *v); |
| 657 | let seq = le(v.0 .0); |
| 658 | push( |
| 659 | "idempotency index", |
| 660 | &t, |
| 661 | [&key2.0[..], &seq].concat(), |
| 662 | [&key.0[..], &seq].concat(), |
| 663 | ); |
| 664 | |
| 665 | let mut t = s.clone(); |
| 666 | let d = BlobDigest::compute(b"j1 equal key blob"); |
| 667 | t.blob_first_visible_seq.insert(d, WalSeq(1)); |
| 668 | let d2 = BlobDigest(above(d.0)); |
| 669 | t.blob_first_visible_seq.insert(d2, WalSeq(1)); |
| 670 | push( |
| 671 | "blob visibility", |
| 672 | &t, |
| 673 | [&d2.0[..], &le(1)].concat(), |
| 674 | [&d.0[..], &le(1)].concat(), |
| 675 | ); |
| 676 | |
| 677 | let first = s |
| 678 | .github_projections |
| 679 | .values() |
| 680 | .next() |
| 681 | .expect("fixture projection"); |
| 682 | let mut t = s.clone(); |
| 683 | let mut p = first.clone(); |
| 684 | p.pr_number = first.pr_number + 1; |
| 685 | t.github_projections.insert(p.pr_number, p.clone()); |
| 686 | let head = |n: u64| { |
| 687 | let r = p.repo_id.as_str().as_bytes(); |
| 688 | [ |
| 689 | &(r.len() as u32).to_le_bytes()[..], |
| 690 | r, |
| 691 | &le(n), |
| 692 | &p.changeset_id.0, |
| 693 | ] |
| 694 | .concat() |
| 695 | }; |
| 696 | push("projections", &t, head(p.pr_number), head(first.pr_number)); |
| 697 | |
| 698 | let (repo, h) = s |
| 699 | .canonical_heads |
| 700 | .iter() |
| 701 | .next() |
| 702 | .expect("fixture canonical head"); |
| 703 | let mut t = s.clone(); |
| 704 | let repo2 = RepoId::new(format!("{}-j1", repo.as_str())).unwrap(); |
| 705 | let mut h2 = h.clone(); |
| 706 | h2.repo_id = repo2.clone(); |
| 707 | t.canonical_heads.insert(repo2.clone(), h2); |
| 708 | let run = |r: &RepoId| { |
| 709 | let b = r.as_str().as_bytes(); |
| 710 | [&(b.len() as u32).to_le_bytes()[..], b].concat() |
| 711 | }; |
| 712 | push("canonical heads", &t, run(&repo2), run(repo)); |
| 713 | out |
| 714 | } |
| 715 | |
| 716 | /// Hand-made records whose repository id breaks one `RepoId::new` rule each (bit flips reach |
| 717 | /// only some of them): a projection record with its repository id bytes replaced. |
| 718 | fn repo_id_bases() -> Vec<Base> { |
| 719 | let p = fixture_json_records() |
| 720 | .iter() |
| 721 | .map(|j| serde_json::from_slice::<WalRecord>(j).unwrap()) |
| 722 | .find(|r| r.kind_byte() == 4) |
| 723 | .expect("fixture projection record"); |
| 724 | let bytes = encode_record(&p).unwrap(); |
| 725 | let n = u32::from_le_bytes(bytes[2..6].try_into().unwrap()) as usize; |
| 726 | assert!(n >= 4, "fixture repository id too short"); |
| 727 | let good = bytes[6..6 + n].to_vec(); |
| 728 | let with = |label: &str, repo: Vec<u8>| { |
| 729 | let mut b = bytes[..2].to_vec(); |
| 730 | b.extend_from_slice(&(repo.len() as u32).to_le_bytes()); |
| 731 | b.extend_from_slice(&repo); |
| 732 | b.extend_from_slice(&bytes[6 + n..]); |
| 733 | refused_base( |
| 734 | format!("repository id: {label}"), |
| 735 | ENTRY_RECORD, |
| 736 | CAP, |
| 737 | 0, |
| 738 | [0u8; 32], |
| 739 | b, |
| 740 | ) |
| 741 | }; |
| 742 | let at = |i: usize, s: &[u8]| { |
| 743 | let mut r = good.clone(); |
| 744 | r[i..i + s.len()].copy_from_slice(s); |
| 745 | r |
| 746 | }; |
| 747 | vec![ |
| 748 | with("empty", Vec::new()), |
| 749 | with("129 bytes", vec![b'a'; 129]), |
| 750 | with("leading slash", at(0, b"/")), |
| 751 | with("trailing slash", at(n - 1, b"/")), |
| 752 | with("double slash", at(1, b"//")), |
| 753 | with("double dot", at(1, b"..")), |
| 754 | with("space", at(1, b" ")), |
| 755 | with("two-byte UTF-8 letter", at(1, "\u{e9}".as_bytes())), |
| 756 | ] |
| 757 | } |
| 758 | |
| 759 | fn refused_base( |
| 760 | label: String, |
| 761 | entry: u8, |
| 762 | cap: u64, |
| 763 | checkpoint_seq: u64, |
| 764 | anchor: [u8; 32], |
| 765 | bytes: Vec<u8>, |
| 766 | ) -> Base { |
| 767 | Base { |
| 768 | entry, |
| 769 | cap, |
| 770 | checkpoint_seq, |
| 771 | anchor, |
| 772 | label, |
| 773 | bytes, |
| 774 | lf: None, |
| 775 | mutate: false, |
| 776 | } |
| 777 | } |
| 778 | |
| 779 | /// The A1 mutation set (`a1_malformed_corpus.rs` `mutations`): every truncation, every bit |
| 780 | /// flip, and a false `u32` length at every offset. |
| 781 | fn mutations(bytes: &[u8]) -> Vec<(u8, u32, u32, Vec<u8>)> { |
| 782 | let mut out = Vec::new(); |
| 783 | for n in 0..bytes.len() { |
| 784 | out.push((OP_TRUNCATE, n as u32, 0, bytes[..n].to_vec())); |
| 785 | } |
| 786 | for bit in 0..bytes.len() * 8 { |
| 787 | let mut b = bytes.to_vec(); |
| 788 | b[bit / 8] ^= 1 << (bit % 8); |
| 789 | out.push((OP_FLIP, bit as u32, 0, b)); |
| 790 | } |
| 791 | for off in 0..bytes.len().saturating_sub(3) { |
| 792 | let remaining = (bytes.len() - off - 4) as u64; |
| 793 | for declared in [remaining + 1, CAP + 1, u64::from(u32::MAX)] { |
| 794 | let mut b = bytes.to_vec(); |
| 795 | b[off..off + 4].copy_from_slice(&(declared as u32).to_le_bytes()); |
| 796 | out.push((OP_FALSE_LENGTH, off as u32, declared as u32, b)); |
| 797 | } |
| 798 | } |
| 799 | out |
| 800 | } |
| 801 | |
| 802 | /// The cases of one base: its A1 mutations, or itself when it is a hand-made refusal. |
| 803 | fn cases_of(b: &Base) -> Vec<(u8, u32, u32, Vec<u8>)> { |
| 804 | match (&b.lf, b.mutate) { |
| 805 | (None, _) => vec![(OP_AS_IS, 0, 0, b.bytes.clone())], |
| 806 | (Some(_), true) => mutations(&b.bytes), |
| 807 | (Some(_), false) => Vec::new(), |
| 808 | } |
| 809 | } |
| 810 | |
| 811 | /// `(kind, at, x, y, field)`; kind 0 is an accept. |
| 812 | type Outcome = (u8, u32, u32, u32, u8); |
| 813 | |
| 814 | fn field_id(f: &str) -> u8 { |
| 815 | let i = FIELDS.iter().position(|&n| n == f); |
| 816 | i.map(|i| i as u8 + 1) |
| 817 | .unwrap_or_else(|| panic!("field {f:?} missing from the J1 field table")) |
| 818 | } |
| 819 | |
| 820 | fn n32<T: TryInto<u32>>(v: T) -> u32 { |
| 821 | v.try_into() |
| 822 | .unwrap_or_else(|_| panic!("refusal value above u32")) |
| 823 | } |
| 824 | |
| 825 | fn outcome_of(r: &CodecRefusal) -> Outcome { |
| 826 | let k = KINDS.iter().position(|&n| n == r.kind()).unwrap() as u8 + 1; |
| 827 | match r { |
| 828 | CodecRefusal::Truncated { at, need } => (k, n32(*at), n32(*need), 0, 0), |
| 829 | CodecRefusal::LengthOverCap { at, declared, cap } => { |
| 830 | (k, n32(*at), n32(*declared), n32(*cap), 0) |
| 831 | } |
| 832 | CodecRefusal::TrailingBytes { at, count } => (k, n32(*at), n32(*count), 0, 0), |
| 833 | CodecRefusal::UnknownTag { at, field, tag } => { |
| 834 | (k, n32(*at), u32::from(*tag), 0, field_id(field)) |
| 835 | } |
| 836 | CodecRefusal::InvalidUtf8 { at } => (k, n32(*at), 0, 0, 0), |
| 837 | CodecRefusal::VersionUnsupported { found } => (k, 0, u32::from(*found), 0, 0), |
| 838 | CodecRefusal::NotCanonical { at, field } => (k, n32(*at), 0, 0, field_id(field)), |
| 839 | CodecRefusal::InvalidRepoId { at } => (k, n32(*at), 0, 0, 0), |
| 840 | CodecRefusal::EmbeddedMalformed { at, field, code } => { |
| 841 | let c = CODES.iter().position(|x| x == code).unwrap_or_else(|| { |
| 842 | panic!("embedded refusal code {code:?} missing from the J1 code table") |
| 843 | }); |
| 844 | (k, n32(*at), c as u32 + 1, 0, field_id(field)) |
| 845 | } |
| 846 | } |
| 847 | } |
| 848 | |
| 849 | /// What the Rust decoder does with `m` under base `b`. An accept must be canonical. |
| 850 | fn decide(b: &Base, m: &[u8]) -> Outcome { |
| 851 | match b.entry { |
| 852 | ENTRY_RECORD => match decode_record(m, b.cap) { |
| 853 | Ok(r) => { |
| 854 | assert_eq!( |
| 855 | encode_record(&r).unwrap(), |
| 856 | m, |
| 857 | "{}: non-canonical accept", |
| 858 | b.label |
| 859 | ); |
| 860 | (0, 0, 0, 0, 0) |
| 861 | } |
| 862 | Err(e) => outcome_of(&e), |
| 863 | }, |
| 864 | _ => match CheckpointState::decode_content(m, WalSeq(b.checkpoint_seq), b.anchor) { |
| 865 | Ok(s) => { |
| 866 | assert_eq!( |
| 867 | s.encode_content().unwrap(), |
| 868 | m, |
| 869 | "{}: non-canonical accept", |
| 870 | b.label |
| 871 | ); |
| 872 | (0, 0, 0, 0, 0) |
| 873 | } |
| 874 | Err(e) => outcome_of(&e), |
| 875 | }, |
| 876 | } |
| 877 | } |
| 878 | |
| 879 | // --------------------------------------------------------------------------------------- |
| 880 | // File |
| 881 | // --------------------------------------------------------------------------------------- |
| 882 | |
| 883 | fn put_name(out: &mut Vec<u8>, s: &str) { |
| 884 | out.extend_from_slice(&(s.len() as u16).to_le_bytes()); |
| 885 | out.extend_from_slice(s.as_bytes()); |
| 886 | } |
| 887 | |
| 888 | /// Corpus bytes and a per-class tally `(class, cases, refused)`. |
| 889 | fn build() -> (Vec<u8>, BTreeMap<String, (usize, usize)>) { |
| 890 | let bases = bases(); |
| 891 | let mut out = Vec::new(); |
| 892 | out.extend_from_slice(&MAGIC); |
| 893 | out.extend_from_slice(&VERSION.to_le_bytes()); |
| 894 | out.extend_from_slice(&0u16.to_le_bytes()); |
| 895 | out.extend_from_slice(&(CAP as u32).to_le_bytes()); |
| 896 | // Rust layout sizes the record decoder's reservation check depends on (LengthOverCap). |
| 897 | let layout = [ |
| 898 | std::mem::size_of::<EvidenceAttachment>(), |
| 899 | std::mem::size_of::<PathMutation>(), |
| 900 | std::mem::size_of::<ReviewRecord>(), |
| 901 | std::mem::size_of::<String>(), |
| 902 | ]; |
| 903 | out.extend_from_slice(&(layout.len() as u32).to_le_bytes()); |
| 904 | for s in layout { |
| 905 | out.extend_from_slice(&(s as u32).to_le_bytes()); |
| 906 | } |
| 907 | let codes: Vec<String> = CODES.iter().map(|c| format!("{c:?}")).collect(); |
| 908 | for table in [ |
| 909 | FIELDS.to_vec(), |
| 910 | codes.iter().map(String::as_str).collect(), |
| 911 | KINDS.to_vec(), |
| 912 | ] { |
| 913 | out.extend_from_slice(&(table.len() as u32).to_le_bytes()); |
| 914 | table.iter().for_each(|n| put_name(&mut out, n)); |
| 915 | } |
| 916 | out.extend_from_slice(&(bases.len() as u32).to_le_bytes()); |
| 917 | for b in &bases { |
| 918 | // A base with a logical form is accepted as is; a hand-made one is refused. |
| 919 | let refused = decide(b, &b.bytes).0 != 0; |
| 920 | assert_eq!(refused, b.lf.is_none(), "{}: unexpected outcome", b.label); |
| 921 | out.push(b.entry); |
| 922 | out.extend_from_slice(&b.cap.to_le_bytes()); |
| 923 | out.extend_from_slice(&b.checkpoint_seq.to_le_bytes()); |
| 924 | put_name(&mut out, &b.label); |
| 925 | out.extend_from_slice(&(b.bytes.len() as u32).to_le_bytes()); |
| 926 | out.extend_from_slice(&b.bytes); |
| 927 | match &b.lf { |
| 928 | Some(lf) => { |
| 929 | out.extend_from_slice(&(lf.len() as u32).to_le_bytes()); |
| 930 | lf.iter() |
| 931 | .for_each(|c| out.extend_from_slice(&c.to_le_bytes())); |
| 932 | } |
| 933 | None => out.extend_from_slice(&u32::MAX.to_le_bytes()), |
| 934 | } |
| 935 | } |
| 936 | let mut cases = Vec::new(); |
| 937 | let mut n_cases: u32 = 0; |
| 938 | let mut tally: BTreeMap<String, (usize, usize)> = BTreeMap::new(); |
| 939 | for (i, b) in bases.iter().enumerate() { |
| 940 | let entry = if b.entry == ENTRY_RECORD { |
| 941 | "record" |
| 942 | } else { |
| 943 | "checkpoint" |
| 944 | }; |
| 945 | for (op, pos, declared, m) in cases_of(b) { |
| 946 | let o = decide(b, &m); |
| 947 | cases.extend_from_slice(&(i as u16).to_le_bytes()); |
| 948 | cases.push(op); |
| 949 | cases.extend_from_slice(&pos.to_le_bytes()); |
| 950 | if op == OP_FALSE_LENGTH { |
| 951 | cases.extend_from_slice(&declared.to_le_bytes()); |
| 952 | } |
| 953 | cases.push(o.0); |
| 954 | if o.0 != 0 { |
| 955 | cases.extend_from_slice(&o.1.to_le_bytes()); |
| 956 | cases.extend_from_slice(&o.2.to_le_bytes()); |
| 957 | cases.extend_from_slice(&o.3.to_le_bytes()); |
| 958 | cases.push(o.4); |
| 959 | } |
| 960 | n_cases += 1; |
| 961 | let class = ["hand_made", "truncated", "bit_flipped", "false_length"][op as usize]; |
| 962 | let t = tally.entry(format!("{entry}_{class}")).or_default(); |
| 963 | t.0 += 1; |
| 964 | t.1 += usize::from(o.0 != 0); |
| 965 | } |
| 966 | } |
| 967 | out.extend_from_slice(&n_cases.to_le_bytes()); |
| 968 | out.extend_from_slice(&cases); |
| 969 | let seal: [u8; 32] = Sha256::digest(&out).into(); |
| 970 | out.extend_from_slice(&seal); |
| 971 | (out, tally) |
| 972 | } |
| 973 | |
| 974 | fn repo_root() -> PathBuf { |
| 975 | Path::new(env!("CARGO_MANIFEST_DIR")).join("../..") |
| 976 | } |
| 977 | |
| 978 | #[test] |
| 979 | fn j1_sealed_corpus_is_what_the_rust_codec_gives() { |
| 980 | let (bytes, tally) = build(); |
| 981 | let digest = hex::encode(Sha256::digest(&bytes)); |
| 982 | let path = repo_root().join(CORPUS_PATH); |
| 983 | if std::env::var_os("J1_CORPUS_WRITE").is_some() { |
| 984 | fs::create_dir_all(path.parent().unwrap()).unwrap(); |
| 985 | fs::write(&path, &bytes).unwrap(); |
| 986 | println!( |
| 987 | "wrote {} ({} bytes, sha256 {digest})", |
| 988 | path.display(), |
| 989 | bytes.len() |
| 990 | ); |
| 991 | } |
| 992 | let mut total = 0; |
| 993 | for (class, (cases, refused)) in &tally { |
| 994 | total += cases; |
| 995 | println!( |
| 996 | " {class}: {cases} cases, {refused} refused, {} accepted (canonical)", |
| 997 | cases - refused |
| 998 | ); |
| 999 | } |
| 1000 | println!( |
| 1001 | " total malformed cases: {total}; corpus {} bytes, sha256 {digest}", |
| 1002 | bytes.len() |
| 1003 | ); |
| 1004 | let sealed = fs::read(&path).unwrap_or_else(|e| panic!("{}: {e}", path.display())); |
| 1005 | assert!( |
| 1006 | sealed == bytes, |
| 1007 | "the sealed corpus differs from what the codec gives today" |
| 1008 | ); |
| 1009 | assert_eq!( |
| 1010 | digest, CORPUS_SHA256, |
| 1011 | "the corpus seal moved; update CORPUS_SHA256 with the file" |
| 1012 | ); |
| 1013 | } |
| 1014 | |
| 1015 | #[test] |
| 1016 | fn j1_corpus_covers_every_record_kind_on_main_and_every_refusal_kind() { |
| 1017 | let bases = bases(); |
| 1018 | let kinds: BTreeSet<u8> = bases |
| 1019 | .iter() |
| 1020 | .filter(|b| b.entry == ENTRY_RECORD && b.lf.is_some()) |
| 1021 | .map(|b| b.bytes[1]) |
| 1022 | .collect(); |
| 1023 | assert_eq!(kinds, (1u8..=8).collect(), "record kinds 1 to 8"); |
| 1024 | let mutated: BTreeSet<u8> = bases |
| 1025 | .iter() |
| 1026 | .filter(|b| b.entry == ENTRY_RECORD && b.mutate) |
| 1027 | .map(|b| b.bytes[1]) |
| 1028 | .collect(); |
| 1029 | assert_eq!(mutated, (1u8..=8).collect(), "every kind is mutated"); |
| 1030 | let mut seen = BTreeSet::new(); |
| 1031 | let mut fields = BTreeSet::new(); |
| 1032 | for b in &bases { |
| 1033 | for (_, _, _, m) in cases_of(b) { |
| 1034 | let o = decide(b, &m); |
| 1035 | seen.insert(o.0); |
| 1036 | fields.insert(o.4); |
| 1037 | } |
| 1038 | } |
| 1039 | assert_eq!( |
| 1040 | seen, |
| 1041 | (0u8..=9).collect(), |
| 1042 | "every refusal kind and an accept are present" |
| 1043 | ); |
| 1044 | assert_eq!(fields, (0u8..=15).collect(), "every field id is reached"); |
| 1045 | // One encoding per logical form: equal forms have equal bytes, and the other way round. |
| 1046 | let pairs: BTreeSet<(&Vec<u64>, &Vec<u8>)> = bases |
| 1047 | .iter() |
| 1048 | .filter_map(|b| b.lf.as_ref().map(|lf| (lf, &b.bytes))) |
| 1049 | .collect(); |
| 1050 | let forms: BTreeSet<&Vec<u64>> = pairs.iter().map(|p| p.0).collect(); |
| 1051 | let encodings: BTreeSet<&Vec<u8>> = pairs.iter().map(|p| p.1).collect(); |
| 1052 | assert_eq!(forms.len(), pairs.len()); |
| 1053 | assert_eq!(encodings.len(), pairs.len()); |
| 1054 | } |