interplane.Get Omega
Read in your language

crumb-spec

Crumb Protocol specification and filesystem standard for sovereign AI agents

Brought by drakestapleton

branch main Branches Tags History

crumb-spec / src / lib.rs

317 lines · 12072 bytes Raw Blame History
1pub mod ledger;
2
3pub use ledger::*;
4
5#[cfg(test)]
6mod tests {
7 use super::*;
8
9 #[test]
10 fn test_crumb_ledger_cryptographic_hash_chaining() {
11 let mut ledger = CrumbLedger::new();
12 assert!(ledger.is_empty());
13
14 let base_time: u64 = 1_700_000_000;
15 let actions = [
16 (CrumbAction::Create, "crates/core/src/lib.rs", "Init core crate"),
17 (CrumbAction::Modify, "crates/core/src/lib.rs", "Implement protocol handler"),
18 (CrumbAction::Test, "crates/core/tests/test.rs", "Add unit tests"),
19 (CrumbAction::Build, "target/release/core", "Compile release binary"),
20 (CrumbAction::Audit, "crates/core/src/lib.rs", "Security audit"),
21 (CrumbAction::Modify, "Cargo.toml", "Bump version to 1.0.0"),
22 (CrumbAction::Test, "crates/core/tests/integration.rs", "Run integration tests"),
23 (CrumbAction::Create, "README.md", "Document architecture"),
24 ];
25
26 for (i, (action, target, intent)) in actions.iter().enumerate() {
27 let payload = format!("payload-content-step-{}", i);
28 let timestamp = base_time + (i as u64 * 10);
29 ledger
30 .append(
31 "agent-atlas-prime".into(),
32 action.clone(),
33 target.to_string(),
34 intent.to_string(),
35 payload.as_bytes(),
36 timestamp,
37 )
38 .expect("append should succeed");
39 }
40
41 assert_eq!(ledger.len(), 8);
42 assert!(ledger.verify_chain().is_ok(), "Untampered chain must verify successfully");
43
44 // Scenario 1: Tamper with genesis event payload
45 {
46 let mut tampered = ledger.clone();
47 tampered.events[0].intent = "Malicious backdoor inserted".to_string();
48 let err = tampered.verify_chain();
49 assert!(
50 matches!(err, Err(LedgerError::HashMismatch { index: 0, .. })),
51 "Tampering genesis event must trigger hash mismatch at index 0"
52 );
53
54 // If attacker updates event 0's hash to match tampered payload, event 1 fails parent check
55 tampered.events[0].hash = LedgerEvent::compute_hash(
56 tampered.events[0].index,
57 tampered.events[0].timestamp,
58 &tampered.events[0].agent,
59 &tampered.events[0].action,
60 &tampered.events[0].target,
61 &tampered.events[0].intent,
62 &tampered.events[0].payload_hash,
63 &tampered.events[0].parent_hash,
64 );
65 let err2 = tampered.verify_chain();
66 assert!(
67 matches!(err2, Err(LedgerError::ParentHashMismatch { index: 1, .. })),
68 "Downstream event 1 must reject modified genesis parent hash"
69 );
70 }
71
72 // Scenario 2: Tamper with historical event in the middle of the chain (index 3)
73 {
74 let mut tampered = ledger.clone();
75 tampered.events[3].target = "crates/tampered/path.rs".to_string();
76 let err = tampered.verify_chain();
77 assert!(
78 matches!(err, Err(LedgerError::HashMismatch { index: 3, .. })),
79 "Tampering historical event 3 must trigger hash mismatch at index 3"
80 );
81
82 // Recompute event 3 hash to match tampered data
83 tampered.events[3].hash = LedgerEvent::compute_hash(
84 tampered.events[3].index,
85 tampered.events[3].timestamp,
86 &tampered.events[3].agent,
87 &tampered.events[3].action,
88 &tampered.events[3].target,
89 &tampered.events[3].intent,
90 &tampered.events[3].payload_hash,
91 &tampered.events[3].parent_hash,
92 );
93 // Event 4 parent_hash still expects original event 3 hash
94 let err_downstream = tampered.verify_chain();
95 assert!(
96 matches!(err_downstream, Err(LedgerError::ParentHashMismatch { index: 4, .. })),
97 "Downstream event 4 must fail verification due to broken parent hash link"
98 );
99 }
100
101 // Scenario 3: Single bit flip in recorded hash
102 {
103 let mut tampered = ledger.clone();
104 tampered.events[5].hash[0] ^= 0x01;
105 let err = tampered.verify_chain();
106 assert!(
107 matches!(err, Err(LedgerError::HashMismatch { index: 5, .. })),
108 "Single bit alteration in hash must trigger hash mismatch"
109 );
110 }
111 }
112
113 #[test]
114 fn test_crumb_ledger_chronological_sequencing_validation() {
115 let mut ledger = CrumbLedger::new();
116 let base_time: u64 = 1_700_000_100;
117
118 ledger
119 .append(
120 "agent-alpha".into(),
121 CrumbAction::Create,
122 "src/main.rs".into(),
123 "Initial commit".into(),
124 b"fn main() {}",
125 base_time,
126 )
127 .expect("first append must succeed");
128
129 // Attempt to append an event with a past timestamp (chronological violation)
130 let past_time = base_time - 50;
131 let append_res = ledger.append(
132 "agent-beta".into(),
133 CrumbAction::Modify,
134 "src/main.rs".into(),
135 "Retroactive edit attempt".into(),
136 b"fn main() { println!(); }",
137 past_time,
138 );
139
140 assert!(
141 matches!(
142 append_res,
143 Err(LedgerError::ChronologicalViolation {
144 index: 1,
145 previous: 1_700_000_100,
146 current: 1_700_000_050
147 })
148 ),
149 "Appending past timestamp must be rejected with ChronologicalViolation"
150 );
151
152 // Identical timestamp within same batch succeeds
153 assert!(
154 ledger
155 .append(
156 "agent-gamma".into(),
157 CrumbAction::Audit,
158 "src/main.rs".into(),
159 "Concurrent audit in same second".into(),
160 b"audit-proof",
161 base_time,
162 )
163 .is_ok()
164 );
165
166 // Forward timestamp succeeds
167 assert!(
168 ledger
169 .append(
170 "agent-delta".into(),
171 CrumbAction::Test,
172 "tests/unit.rs".into(),
173 "Pass test".into(),
174 b"test-proof",
175 base_time + 10,
176 )
177 .is_ok()
178 );
179
180 assert!(ledger.verify_chain().is_ok());
181 }
182
183 #[test]
184 fn test_crumb_ledger_missing_parent_detection() {
185 let mut ledger = CrumbLedger::new();
186 let base_time: u64 = 1_700_000_000;
187
188 for i in 0..5 {
189 ledger
190 .append(
191 "agent-worker".into(),
192 CrumbAction::Modify,
193 format!("file_{}.rs", i),
194 format!("Intent {}", i),
195 format!("data-{}", i).as_bytes(),
196 base_time + i,
197 )
198 .unwrap();
199 }
200
201 // Test missing parent when index 2 is deleted from the sequence
202 let mut gapped_ledger = CrumbLedger::new();
203 gapped_ledger.events.push(ledger.events[0].clone());
204 gapped_ledger.events.push(ledger.events[1].clone());
205 // Skip index 2, append index 3
206 gapped_ledger.events.push(ledger.events[3].clone());
207
208 let err = gapped_ledger.verify_chain();
209 assert!(
210 matches!(err, Err(LedgerError::InvalidIndex { index: 3, expected: 2 })),
211 "Gapped chain must be rejected for index inconsistency"
212 );
213
214 // Test altered parent_hash directly
215 let mut corrupted_parent = ledger.clone();
216 corrupted_parent.events[2].parent_hash = [0xFF; 32];
217 let err_parent = corrupted_parent.verify_chain();
218 // Since hash depends on parent_hash, hash verification fails first
219 assert!(matches!(err_parent, Err(LedgerError::HashMismatch { index: 2, .. })));
220
221 // If attacker recomputes hash to match corrupted parent_hash:
222 corrupted_parent.events[2].hash = LedgerEvent::compute_hash(
223 corrupted_parent.events[2].index,
224 corrupted_parent.events[2].timestamp,
225 &corrupted_parent.events[2].agent,
226 &corrupted_parent.events[2].action,
227 &corrupted_parent.events[2].target,
228 &corrupted_parent.events[2].intent,
229 &corrupted_parent.events[2].payload_hash,
230 &corrupted_parent.events[2].parent_hash,
231 );
232 let err_parent2 = corrupted_parent.verify_chain();
233 assert!(
234 matches!(err_parent2, Err(LedgerError::ParentHashMismatch { index: 2, .. })),
235 "Parent hash mismatch must be detected when referencing non-existent parent"
236 );
237
238 // Test verify_parent method
239 assert!(ledger.verify_parent(&ledger.events[1]).is_ok());
240 let mut detached_event = ledger.events[3].clone();
241 detached_event.parent_hash = [0xAA; 32];
242 assert!(matches!(
243 ledger.verify_parent(&detached_event),
244 Err(LedgerError::ParentHashMismatch { .. })
245 ));
246 }
247
248 #[test]
249 fn test_crumb_ledger_serialization_roundtrip_json() {
250 let mut ledger = CrumbLedger::new();
251 let base_time: u64 = 1_700_000_500;
252
253 ledger
254 .append(
255 "agent-builder".into(),
256 CrumbAction::Create,
257 "Cargo.toml".into(),
258 "Define workspace manifest".into(),
259 b"[workspace]\nresolver = \"2\"",
260 base_time,
261 )
262 .unwrap();
263
264 ledger
265 .append(
266 "agent-reviewer".into(),
267 CrumbAction::Audit,
268 "Cargo.toml".into(),
269 "Verify workspace dependencies".into(),
270 b"audit: clean",
271 base_time + 5,
272 )
273 .unwrap();
274
275 let json_str = ledger.to_json().expect("JSON serialization must succeed");
276 assert!(json_str.contains("agent-builder"));
277 assert!(json_str.contains("agent-reviewer"));
278 assert!(json_str.contains("payload_hash"));
279
280 let restored = CrumbLedger::from_json(&json_str).expect("JSON deserialization must succeed");
281 assert_eq!(ledger, restored, "Deserialized ledger must exactly equal original");
282 assert!(restored.verify_chain().is_ok(), "Restored ledger chain must verify");
283
284 // Corrupted JSON must fail
285 assert!(CrumbLedger::from_json("invalid json payload").is_err());
286 }
287
288 #[test]
289 fn test_crumb_ledger_serialization_roundtrip_canonical_binary() {
290 let mut ledger = CrumbLedger::new();
291 let base_time: u64 = 1_700_000_900;
292
293 for i in 0..10 {
294 ledger
295 .append(
296 format!("agent-binary-{}", i),
297 CrumbAction::Modify,
298 format!("kernel/module_{}.bin", i),
299 format!("Install binary patch {}", i),
300 &[i as u8; 64],
301 base_time + (i as u64 * 10),
302 )
303 .unwrap();
304 }
305
306 let binary_data = ledger.to_binary().expect("Binary serialization must succeed");
307 assert!(!binary_data.is_empty());
308
309 let restored = CrumbLedger::from_binary(&binary_data).expect("Binary deserialization must succeed");
310 assert_eq!(ledger, restored, "Canonical binary roundtrip must yield identical ledger");
311 assert!(restored.verify_chain().is_ok(), "Restored ledger from binary must verify");
312
313 // Corrupted binary payload must fail
314 let corrupted_bytes = vec![0xFF, 0x00, 0xAA];
315 assert!(CrumbLedger::from_binary(&corrupted_bytes).is_err());
316 }
317}