MCPcopy Create free account
hub / github.com/NodeDB-Lab/nodedb / wal_encrypted_restart_roundtrip

Function wal_encrypted_restart_roundtrip

nodedb-wal/src/segmented.rs:619–675  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

617 /// is read from the on-disk preamble rather than the current key's epoch.
618 #[test]
619 fn wal_encrypted_restart_roundtrip() {
620 let dir = tempfile::tempdir().unwrap();
621 let wal_dir = dir.path().join("wal");
622 let key_bytes = [0xABu8; 32];
623
624 let config = SegmentedWalConfig::for_testing(wal_dir.clone());
625
626 // Write with key lifetime 1.
627 {
628 let key = crate::crypto::WalEncryptionKey::from_bytes(&key_bytes).unwrap();
629 let ring = crate::crypto::KeyRing::new(key);
630 let mut wal = SegmentedWal::open(config.clone()).unwrap();
631 wal.set_encryption_ring(ring).unwrap();
632
633 for i in 0..5u32 {
634 wal.append(
635 RecordType::Put as u32,
636 1,
637 0,
638 0,
639 format!("restart-{i}").as_bytes(),
640 )
641 .unwrap();
642 }
643 wal.sync().unwrap();
644 }
645
646 // Simulate restart: new WalEncryptionKey with same bytes (fresh random epoch).
647 let key_restart = crate::crypto::WalEncryptionKey::from_bytes(&key_bytes).unwrap();
648 let ring_restart = crate::crypto::KeyRing::new(key_restart);
649
650 // Replay all segments and decrypt.
651 let segments = crate::segment::discover_segments(&wal_dir).unwrap();
652 let mut payloads = Vec::new();
653
654 for seg in &segments {
655 let reader = crate::reader::WalReader::open(&seg.path).unwrap();
656 let epoch = *reader
657 .segment_preamble()
658 .expect("segment must have preamble after encrypted write")
659 .epoch();
660 let preamble_bytes = reader.segment_preamble().unwrap().to_bytes();
661
662 for record_result in reader.records() {
663 let record = record_result.unwrap();
664 let pt = record
665 .decrypt_payload_ring(&epoch, Some(&preamble_bytes), Some(&ring_restart))
666 .unwrap();
667 payloads.push(pt);
668 }
669 }
670
671 assert_eq!(payloads.len(), 5);
672 for (i, pt) in payloads.iter().enumerate() {
673 assert_eq!(pt, format!("restart-{i}").as_bytes());
674 }
675 }
676

Callers

nothing calls this directly

Calls 15

discover_segmentsFunction · 0.85
joinMethod · 0.80
decrypt_payload_ringMethod · 0.80
openFunction · 0.50
pathMethod · 0.45
cloneMethod · 0.45
set_encryption_ringMethod · 0.45
appendMethod · 0.45
as_bytesMethod · 0.45
syncMethod · 0.45
epochMethod · 0.45
expectMethod · 0.45

Tested by

no test coverage detected