Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/synapse/blob/main/crates/synapse-evidence/src/tests.rs # synapse A component of the AienOS ecosystem. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `synapse / crates / synapse-evidence / src / tests.rs` 495 lines ยท 17023 bytes Raw Blame History 1 use super::*; 2 use synapse_core::DOMAIN_CHANGESET_SIGN_V1; 3 4 fn executor() -> SigningKey { 5 SigningKey::from_bytes(&[7u8; 32]) 6 } 7 8 fn body() -> ReceiptBody { 9 let repo = RepoId::new("aien-spark/synapse").unwrap(); 10 let cs = ChangesetId([3; 32]); 11 ReceiptBody { 12 repo_id: repo.clone(), 13 changeset_id: cs, 14 source: SourceRevision { 15 base_git_oid: GitOid([1; 20]), 16 parent_changesets: vec![ChangesetId([0x10; 32]), ChangesetId([0x20; 32])], 17 }, 18 crumb_contexts: vec![ 19 CrumbContextBinding { 20 dir: ".".into(), 21 at_base: CrumbContextDigest([0xa1; 32]), 22 in_changeset: CrumbContextDigest([0xb1; 32]), 23 }, 24 CrumbContextBinding { 25 dir: "src".into(), 26 at_base: CrumbContextDigest([0xa2; 32]), 27 in_changeset: CrumbContextDigest([0xa2; 32]), 28 }, 29 ], 30 input_root: InputRoot::single_repo(repo, cs), 31 test: TestIdentity { 32 name: "cargo-test-workspace".into(), 33 command: vec!["cargo".into(), "test".into(), "--workspace".into()], 34 toolchain: "rustc 1.89.0".into(), 35 platform: "aarch64-unknown-linux-gnu".into(), 36 environment: vec![ 37 EnvVar { 38 name: "CARGO_TERM_COLOR".into(), 39 value: "never".into(), 40 }, 41 EnvVar { 42 name: "RUST_BACKTRACE".into(), 43 value: "1".into(), 44 }, 45 ], 46 }, 47 result: ReceiptResult { 48 exit_status: ExitStatus::Code(0), 49 outcome: Outcome::Pass, 50 outputs: vec![ 51 DeclaredOutput { 52 name: "junit.xml".into(), 53 digest: OutputDigest::of_bytes(b""), 54 }, 55 DeclaredOutput { 56 name: "verdicts".into(), 57 digest: OutputDigest::of_bytes(b"all pass"), 58 }, 59 ], 60 }, 61 observation: Observation { 62 started_at_ms: 1_000, 63 duration_ms: 42_000, 64 log: b"running 120 tests\ntest result: ok\n".to_vec(), 65 }, 66 } 67 } 68 69 fn signed() -> EvidenceReceipt { 70 EvidenceReceipt::sign(body(), &executor()).unwrap() 71 } 72 73 #[test] 74 fn round_trip_encoding_is_canonical() { 75 let r = signed(); 76 let bytes = r.encode().unwrap(); 77 assert!(bytes.starts_with(DOMAIN_EVIDENCE_RECEIPT_V1)); 78 let (back, id) = EvidenceReceipt::decode_verified(&bytes).unwrap(); 79 assert_eq!(back, r); 80 assert_eq!(id, r.id()); 81 assert_eq!(back.encode().unwrap(), bytes); 82 // Deterministic: signing the same body twice gives identical bytes (Ed25519 is deterministic). 83 assert_eq!(signed().encode().unwrap(), bytes); 84 } 85 86 #[test] 87 fn every_bound_field_is_tamper_evident() { 88 type Tamper = (&'static str, fn(&mut EvidenceReceipt)); 89 let cases: Vec = vec![ 90 ("repo_id", |r| { 91 let other = RepoId::new("aien-spark/other").unwrap(); 92 r.body.repo_id = other.clone(); 93 r.body.input_root = InputRoot::single_repo(other, r.body.changeset_id); 94 }), 95 ("changeset_id", |r| { 96 r.body.changeset_id = ChangesetId([4; 32]); 97 r.body.input_root = InputRoot::single_repo(r.body.repo_id.clone(), r.body.changeset_id); 98 }), 99 ("source.base_git_oid", |r| { 100 r.body.source.base_git_oid = GitOid([2; 20]) 101 }), 102 ("source.parent_changesets", |r| { 103 r.body.source.parent_changesets.pop(); 104 }), 105 ("crumb_contexts.at_base", |r| { 106 r.body.crumb_contexts[1].at_base = CrumbContextDigest([0xb2; 32]) 107 }), 108 ("crumb_contexts.in_changeset", |r| { 109 r.body.crumb_contexts[1].in_changeset = CrumbContextDigest([0xb2; 32]) 110 }), 111 ("crumb_contexts.dir", |r| { 112 r.body.crumb_contexts[1].dir = "srd".into() 113 }), 114 ("crumb_contexts.count", |r| { 115 r.body.crumb_contexts.pop(); 116 }), 117 ("test.name", |r| r.body.test.name = "cargo-test".into()), 118 ("test.command", |r| r.body.test.command.push("-q".into())), 119 ("test.toolchain", |r| { 120 r.body.test.toolchain = "rustc 1.90.0".into() 121 }), 122 ("test.platform", |r| { 123 r.body.test.platform = "x86_64-unknown-linux-gnu".into() 124 }), 125 ("test.environment", |r| { 126 r.body.test.environment[1].value = "0".into() 127 }), 128 ("result.exit_status", |r| { 129 r.body.result.exit_status = ExitStatus::Code(1); 130 r.body.result.outcome = Outcome::Fail; 131 }), 132 ("result.outcome", |r| r.body.result.outcome = Outcome::Flaky), 133 ("result.outputs", |r| { 134 r.body.result.outputs[0].digest = OutputDigest::of_bytes(b"") 135 }), 136 ("observation.log", |r| r.body.observation.log.push(b'!')), 137 ("observation.duration_ms", |r| { 138 r.body.observation.duration_ms += 1 139 }), 140 ("observation.started_at_ms", |r| { 141 r.body.observation.started_at_ms += 1 142 }), 143 ("executor_did", |r| { 144 r.executor_did = encode_did_key_`ed25519`( 145 &SigningKey::from_bytes(&[8u8; 32]) 146 .verifying_key() 147 .to_bytes(), 148 ) 149 }), 150 ]; 151 for (field, tamper) in cases { 152 let mut r = signed(); 153 tamper(&mut r); 154 assert_eq!( 155 r.verify(), 156 Err(ReceiptError::SignatureInvalid), 157 "tampering {field} must break the signature" 158 ); 159 // The same tamper through the wire form is refused too. 160 assert_eq!( 161 EvidenceReceipt::decode_verified(&r.encode().unwrap()).map(|_| ()), 162 Err(ReceiptError::SignatureInvalid), 163 "{field} via bytes" 164 ); 165 } 166 } 167 168 #[test] 169 fn every_single_byte_flip_is_refused() { 170 let bytes = signed().encode().unwrap(); 171 for i in 0..bytes.len() { 172 let mut b = bytes.clone(); 173 b[i] ^= 0x01; 174 assert!( 175 EvidenceReceipt::decode_verified(&b).is_err(), 176 "flip at byte {i} accepted" 177 ); 178 } 179 } 180 181 #[test] 182 fn forged_signatures_are_refused() { 183 // Signed by a different key than the one named. 184 let mut r = signed(); 185 let forger = SigningKey::from_bytes(&[9u8; 32]); 186 r.signature = forger.sign(&r.signable_bytes()).to_bytes(); 187 assert_eq!(r.verify(), Err(ReceiptError::SignatureInvalid)); 188 189 // Forger names itself but replays the executor's signature. 190 let mut r = signed(); 191 r.executor_did = encode_did_key_`ed25519`(&forger.verifying_key().to_bytes()); 192 assert_eq!(r.verify(), Err(ReceiptError::SignatureInvalid)); 193 194 // Zero signature. 195 let mut r = signed(); 196 r.signature = [0; 64]; 197 assert_eq!(r.verify(), Err(ReceiptError::SignatureInvalid)); 198 199 // Executor DID that is not an Ed25519 did:key. 200 let mut r = signed(); 201 r.executor_did = "did:web:example.com".into(); 202 assert_eq!(r.verify(), Err(ReceiptError::ExecutorDidInvalid)); 203 } 204 205 #[test] 206 fn wrong_domain_is_refused() { 207 // Record bytes under another tag. 208 let mut bytes = signed().encode().unwrap(); 209 bytes[..DOMAIN_EVIDENCE_RECEIPT_V1.len()].copy_from_slice(b"SYNAPSE_EVIDENCE_RECEIPT_V2\0"); 210 assert_eq!( 211 EvidenceReceipt::decode(&bytes), 212 Err(ReceiptError::WrongDomain) 213 ); 214 assert_eq!( 215 EvidenceReceipt::decode(b"SYN"), 216 Err(ReceiptError::WrongDomain) 217 ); 218 219 // A signature the executor made over the same content under another domain (the 220 // changeset signing tag) does not verify as a receipt signature. 221 let key = executor(); 222 let mut r = signed(); 223 let mut cross = r.signable_bytes(); 224 cross.splice( 225 ..DOMAIN_EVIDENCE_RECEIPT_SIGN_V1.len(), 226 DOMAIN_CHANGESET_SIGN_V1.iter().copied(), 227 ); 228 r.signature = key.sign(&cross).to_bytes(); 229 assert_eq!(r.verify(), Err(ReceiptError::SignatureInvalid)); 230 } 231 232 #[test] 233 fn unknown_version_is_refused() { 234 let r = signed(); 235 let mut bytes = r.encode().unwrap(); 236 let at = DOMAIN_EVIDENCE_RECEIPT_V1.len(); 237 bytes[at..at + 2].copy_from_slice(&2u16.to_le_bytes()); 238 assert_eq!( 239 EvidenceReceipt::decode(&bytes), 240 Err(ReceiptError::UnknownVersion(2)) 241 ); 242 243 let mut r2 = r.clone(); 244 r2.record_version = 2; 245 assert_eq!(r2.verify(), Err(ReceiptError::UnknownVersion(2))); 246 // Even when re-signed by the executor under version 2. 247 r2.signature = executor().sign(&r2.signable_bytes()).to_bytes(); 248 assert_eq!(r2.verify(), Err(ReceiptError::UnknownVersion(2))); 249 } 250 251 #[test] 252 fn flaky_and_error_never_count_as_pass() { 253 assert!(Outcome::Pass.is_pass()); 254 assert!(!Outcome::Fail.is_pass()); 255 assert!(!Outcome::Flaky.is_pass()); 256 assert!(!Outcome::Error.is_pass()); 257 258 // A Flaky run that exited 0 is still not a pass and has a different identity from Pass. 259 let mut b = body(); 260 b.result.outcome = Outcome::Flaky; 261 let flaky = EvidenceReceipt::sign(b, &executor()).unwrap(); 262 assert!(!flaky.body.result.outcome.is_pass()); 263 assert_ne!(flaky.result_digest(), signed().result_digest()); 264 assert_ne!(flaky.id(), signed().id()); 265 } 266 267 #[test] 268 fn outcome_must_agree_with_exit_status() { 269 let mut b = body(); 270 b.result.exit_status = ExitStatus::Code(1); 271 assert_eq!( 272 EvidenceReceipt::sign(b, &executor()), 273 Err(ReceiptError::OutcomeInconsistent) 274 ); 275 let mut b = body(); 276 b.result.exit_status = ExitStatus::Signal(9); 277 assert_eq!( 278 EvidenceReceipt::sign(b, &executor()), 279 Err(ReceiptError::OutcomeInconsistent) 280 ); 281 let mut b = body(); 282 b.result.outcome = Outcome::Fail; 283 assert_eq!( 284 EvidenceReceipt::sign(b, &executor()), 285 Err(ReceiptError::OutcomeInconsistent) 286 ); 287 } 288 289 #[test] 290 fn identity_excludes_logs_and_timings() { 291 let a = signed(); 292 let mut b2 = body(); 293 b2.observation = Observation { 294 started_at_ms: 9_999_999, 295 duration_ms: 1, 296 log: b"completely different log, other timings\n".to_vec(), 297 }; 298 let b = EvidenceReceipt::sign(b2, &executor()).unwrap(); 299 // Both verify, carry different observations, and share one identity. 300 let ida = a.verify().unwrap(); 301 let idb = b.verify().unwrap(); 302 assert_ne!(a.encode().unwrap(), b.encode().unwrap()); 303 assert_eq!(a.result_digest(), b.result_digest()); 304 assert_eq!(a.body.action_digest(), b.body.action_digest()); 305 assert_eq!(ida, idb); 306 307 // The deterministic part does move the identity. 308 let mut b3 = body(); 309 b3.result.outputs[1].digest = OutputDigest::of_bytes(b"one fail"); 310 let c = EvidenceReceipt::sign(b3, &executor()).unwrap(); 311 assert_ne!(c.result_digest(), a.result_digest()); 312 assert_ne!(c.id(), ida); 313 let mut b4 = body(); 314 b4.result.exit_status = ExitStatus::Code(3); 315 b4.result.outcome = Outcome::Error; 316 assert_ne!(EvidenceReceipt::sign(b4, &executor()).unwrap().id(), ida); 317 // So do the action (context seen) and the executor. 318 let mut b5 = body(); 319 b5.crumb_contexts[0].at_base = CrumbContextDigest([0; 32]); 320 assert_ne!(EvidenceReceipt::sign(b5, &executor()).unwrap().id(), ida); 321 let mut b6 = body(); 322 b6.crumb_contexts[0].in_changeset = CrumbContextDigest([0; 32]); 323 assert_ne!(EvidenceReceipt::sign(b6, &executor()).unwrap().id(), ida); 324 let other = SigningKey::from_bytes(&[11u8; 32]); 325 assert_ne!(EvidenceReceipt::sign(body(), &other).unwrap().id(), ida); 326 } 327 328 #[test] 329 fn digest_types_and_tags_are_distinct() { 330 let tags: [&[u8]; 7] = [ 331 DOMAIN_EVIDENCE_RECEIPT_V1, 332 DOMAIN_EVIDENCE_RECEIPT_SIGN_V1, 333 DOMAIN_EVIDENCE_RECEIPT_ID_V1, 334 DOMAIN_EVIDENCE_ACTION_V1, 335 DOMAIN_EVIDENCE_RESULT_V1, 336 DOMAIN_EVIDENCE_INPUT_ROOT_V1, 337 DOMAIN_EVIDENCE_OUTPUT_V1, 338 ]; 339 let others: [&[u8]; 4] = [ 340 synapse_core::DOMAIN_BLOB_V1, 341 synapse_core::DOMAIN_CHANGESET_ID_V1, 342 synapse_core::DOMAIN_CHANGESET_SIGN_V1, 343 synapse_crumb::DOMAIN_CRUMB_CONTEXT_V1, 344 ]; 345 for (i, t) in tags.iter().enumerate() { 346 assert!(t.ends_with(b"\0")); 347 for u in tags.iter().skip(i + 1) { 348 assert_ne!(t, u); 349 } 350 for o in others { 351 assert_ne!(*t, o); 352 } 353 } 354 // Same 32 bytes under OutputDigest and BlobDigest differ. 355 assert_ne!( 356 OutputDigest::of_bytes(b"x").0, 357 synapse_core::BlobDigest::compute(b"x").0 358 ); 359 } 360 361 #[test] 362 fn structural_rules_are_enforced() { 363 let k = executor(); 364 let mut b = body(); 365 b.crumb_contexts.reverse(); 366 assert_eq!( 367 EvidenceReceipt::sign(b, &k), 368 Err(ReceiptError::Unsorted("crumb_contexts")) 369 ); 370 let mut b = body(); 371 b.crumb_contexts.clear(); 372 assert_eq!( 373 EvidenceReceipt::sign(b, &k), 374 Err(ReceiptError::Empty("crumb_contexts")) 375 ); 376 let mut b = body(); 377 b.crumb_contexts[1].dir = "../etc".into(); 378 assert!(matches!( 379 EvidenceReceipt::sign(b, &k), 380 Err(ReceiptError::InvalidPath(_)) 381 )); 382 let mut b = body(); 383 b.input_root.members[0].changeset_id = ChangesetId([5; 32]); 384 assert_eq!( 385 EvidenceReceipt::sign(b, &k), 386 Err(ReceiptError::InputRootMismatch) 387 ); 388 let mut b = body(); 389 let extra = b.input_root.members[0].clone(); 390 b.input_root.members.push(extra); 391 assert_eq!( 392 EvidenceReceipt::sign(b, &k), 393 Err(ReceiptError::InputRootUnsupported(2)) 394 ); 395 let mut b = body(); 396 b.result.outputs.reverse(); 397 assert_eq!( 398 EvidenceReceipt::sign(b, &k), 399 Err(ReceiptError::Unsorted("result.outputs")) 400 ); 401 let mut b = body(); 402 b.observation.log = vec![0; MAX_LOG_BYTES + 1]; 403 assert_eq!( 404 EvidenceReceipt::sign(b, &k), 405 Err(ReceiptError::OverCap("observation.log")) 406 ); 407 } 408 409 #[test] 410 fn malformed_bytes_are_refused_without_panic() { 411 let bytes = signed().encode().unwrap(); 412 for n in 0..bytes.len() { 413 assert!(EvidenceReceipt::decode(&bytes[..n]).is_err(), "prefix {n}"); 414 } 415 let mut long = bytes.clone(); 416 long.push(0); 417 assert_eq!( 418 EvidenceReceipt::decode(&long), 419 Err(ReceiptError::TrailingBytes) 420 ); 421 // A huge declared length is refused by its cap before any allocation. 422 let mut huge = DOMAIN_EVIDENCE_RECEIPT_V1.to_vec(); 423 huge.extend_from_slice(&1u16.to_le_bytes()); 424 huge.extend_from_slice(&u32::MAX.to_le_bytes()); 425 assert_eq!( 426 EvidenceReceipt::decode(&huge), 427 Err(ReceiptError::OverCap("repo_id")) 428 ); 429 } 430 431 #[test] 432 fn executor_did_must_be_canonical() { 433 // Same key, same body, DID spelled as a bare multibase key: refused even when re-signed, 434 // so one key has one spelling and one receipt identity. 435 let mut r = signed(); 436 r.executor_did = r.executor_did.strip_prefix("did:key:").unwrap().to_string(); 437 r.signature = executor().sign(&r.signable_bytes()).to_bytes(); 438 assert_eq!(r.verify(), Err(ReceiptError::ExecutorDidInvalid)); 439 } 440 441 #[test] 442 fn encode_refuses_what_validate_refuses() { 443 // A record built by hand with more items than a u32-prefixed list may carry under v1 is 444 // refused, not written with a silently truncated count. 445 let mut r = signed(); 446 r.body.source.parent_changesets = (0..=MAX_LIST_ITEMS as u32) 447 .map(|i| { 448 let mut id = [0u8; 32]; 449 id[..4].copy_from_slice(&i.to_be_bytes()); 450 ChangesetId(id) 451 }) 452 .collect(); 453 assert_eq!( 454 r.encode(), 455 Err(ReceiptError::OverCap("source.parent_changesets")) 456 ); 457 let mut r = signed(); 458 r.record_version = 2; 459 assert_eq!(r.encode(), Err(ReceiptError::UnknownVersion(2))); 460 } 461 462 #[test] 463 fn total_record_size_is_capped() { 464 // Decode refuses an oversized record before reading any field, even with a valid prefix. 465 let mut big = signed().encode().unwrap(); 466 big.resize(MAX_RECEIPT_BYTES + 1, 0); 467 assert_eq!( 468 EvidenceReceipt::decode(&big), 469 Err(ReceiptError::OverCap("receipt")) 470 ); 471 // A body whose every field is within its own cap but whose record is over the total cap 472 // is refused at signing and at encoding. 473 let mut b = body(); 474 b.observation.log = vec![b'x'; MAX_LOG_BYTES]; 475 b.test.command = (0..300) 476 .map(|i| format!("{i:04}{}", "a".repeat(4000))) 477 .collect(); 478 assert_eq!( 479 EvidenceReceipt::sign(b.clone(), &executor()), 480 Err(ReceiptError::OverCap("receipt")) 481 ); 482 let mut r = signed(); 483 r.body = b; 484 assert_eq!(r.encode(), Err(ReceiptError::OverCap("receipt"))); 485 // The largest log that fits still round-trips. 486 let mut b = body(); 487 b.observation.log = vec![b'x'; MAX_LOG_BYTES]; 488 let r = EvidenceReceipt::sign(b, &executor()).unwrap(); 489 assert_eq!( 490 EvidenceReceipt::decode_verified(&r.encode().unwrap()) 491 .unwrap() 492 .0, 493 r 494 ); 495 }