@@ -1487,4 +1487,79 @@ mod tests { |
1487 | 1487 | Err(err) => panic!("recovery self-install refused at sign time: {err}"), |
1488 | 1488 | } |
1489 | 1489 | } |
| 1490 | + |
| 1491 | + // #6487: whatever sign_rotation signs, the receiving side accepts. |
| 1492 | + // A run of routine rotations, each signed by the current generation |
| 1493 | + // key at that key's own next log position, decodes to the same |
| 1494 | + // body, validates against its verified author, and advances the |
| 1495 | + // folded chain by exactly one generation. sign_rotation's only |
| 1496 | + // routine refusal (a self-rotation) is one validate refuses too, |
| 1497 | + // so signer and receiver never disagree. Key seeds are drawn from |
| 1498 | + // a small set so self-rotations and reused keys occur often. |
| 1499 | + proptest::proptest! { |
| 1500 | + #[test] |
| 1501 | + fn every_signed_routine_rotation_is_accepted_by_the_fold( |
| 1502 | + seeds in proptest::collection::vec(0u8..4, 2..7), |
| 1503 | + ) { |
| 1504 | + let keys: Vec<SigningKey> = seeds |
| 1505 | + .iter() |
| 1506 | + .map(|seed| SigningKey::from_bytes(&[seed + 1; 32])) |
| 1507 | + .collect(); |
| 1508 | + let issuer = issuer_id(&keys[0]); |
| 1509 | + let mut logs: std::collections::HashMap<IssuerId, (SeqNum, Hash)> = |
| 1510 | + std::collections::HashMap::new(); |
| 1511 | + let mut folded: Vec<(IssuerId, Hash, IssuerRotation)> = Vec::new(); |
| 1512 | + let mut current = 0usize; |
| 1513 | + for next in 1..keys.len() { |
| 1514 | + let author = &keys[current]; |
| 1515 | + let generation = folded.len() as u64; |
| 1516 | + let previous = folded.last().map(|(_, id, _)| *id); |
| 1517 | + let mut body = IssuerRotation::unsigned( |
| 1518 | + RotationMode::Routine, |
| 1519 | + issuer, |
| 1520 | + currency(), |
| 1521 | + generation, |
| 1522 | + previous, |
| 1523 | + issuer_id(&keys[next]), |
| 1524 | + None, |
| 1525 | + ); |
| 1526 | + body.prove_successor(&keys[next]).expect("prove successor"); |
| 1527 | + let (seq_num, backlink) = match logs.get(&issuer_id(author)) { |
| 1528 | + Some((seq, last)) => (seq + 1, Some(*last)), |
| 1529 | + None => (0, None), |
| 1530 | + }; |
| 1531 | + match sign_rotation(author, seq_num, backlink, &body, vec![]) { |
| 1532 | + Ok(signed) => { |
| 1533 | + let decoded = chain::decode(&signed.bytes).expect("decode rotation"); |
| 1534 | + proptest::prop_assert_eq!(author_id(&decoded), issuer_id(author)); |
| 1535 | + let carried = decoded |
| 1536 | + .header |
| 1537 | + .extensions |
| 1538 | + .rotation |
| 1539 | + .clone() |
| 1540 | + .expect("rotation in extension"); |
| 1541 | + proptest::prop_assert_eq!(&carried, &body); |
| 1542 | + proptest::prop_assert_eq!(carried.validate(&author_id(&decoded), None), Ok(())); |
| 1543 | + logs.insert(issuer_id(author), (seq_num, signed.id)); |
| 1544 | + folded.push((author_id(&decoded), signed.id, carried)); |
| 1545 | + let chain = fold_rotation_chain(&issuer, ¤cy(), None, &folded); |
| 1546 | + proptest::prop_assert_eq!(chain.status.clone(), ChainStatus::Settled); |
| 1547 | + proptest::prop_assert_eq!(chain.active_generation(), folded.len() as u64); |
| 1548 | + proptest::prop_assert_eq!(chain.active_key(), &issuer_id(&keys[next])); |
| 1549 | + current = next; |
| 1550 | + } |
| 1551 | + Err(SignError::Rotation(RotationError::RoutineSuccessorIsAuthor)) => { |
| 1552 | + proptest::prop_assert_eq!(issuer_id(&keys[next]), issuer_id(author)); |
| 1553 | + proptest::prop_assert_eq!( |
| 1554 | + body.validate(&issuer_id(author), None), |
| 1555 | + Err(RotationError::RoutineSuccessorIsAuthor) |
| 1556 | + ); |
| 1557 | + } |
| 1558 | + Err(other) => { |
| 1559 | + proptest::prop_assert!(false, "unexpected sign refusal: {other}"); |
| 1560 | + } |
| 1561 | + } |
| 1562 | + } |
| 1563 | + } |
| 1564 | + } |
1490 | 1565 | } |