-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomponent_p2p_test.rs
More file actions
191 lines (163 loc) · 6.74 KB
/
Copy pathcomponent_p2p_test.rs
File metadata and controls
191 lines (163 loc) · 6.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// SPDX-License-Identifier: MPL-2.0
// Copyright (c) Jonathan D.A. Jewell <j.d.a.jewell@open.ac.uk>
//
// Point-to-point tests: verify component interactions
// Tests: content_store↔metadata, keys↔attestation, transaction↔operations
#[cfg(test)]
mod p2p_tests {
use std::path::PathBuf;
use tempfile::TempDir;
/// Helper: create a temp directory for test isolation
fn test_dir() -> TempDir {
tempfile::tempdir().expect("Failed to create temp dir")
}
// --- content_store ↔ metadata ---
#[test]
fn content_store_metadata_roundtrip() {
let dir = test_dir();
let store_path = dir.path().join(".jk/content");
let meta_path = dir.path().join(".jk/metadata");
std::fs::create_dir_all(&store_path).unwrap();
std::fs::create_dir_all(&meta_path).unwrap();
// Write content
let content = b"test file content for p2p";
let hash = sha256(content);
let hash_hex = hex::encode(hash);
std::fs::write(store_path.join(&hash_hex), content).unwrap();
// Write metadata referencing the content hash
let meta = format!(
r#"{{"hash":"{}","size":{},"op":"copy"}}"#,
hash_hex,
content.len()
);
std::fs::write(meta_path.join("0001.json"), &meta).unwrap();
// Verify: metadata hash matches stored content
let stored = std::fs::read(store_path.join(&hash_hex)).unwrap();
let stored_hash = hex::encode(sha256(&stored));
assert_eq!(hash_hex, stored_hash, "Content hash must match metadata reference");
}
#[test]
fn content_store_deduplication() {
let dir = test_dir();
let store_path = dir.path().join(".jk/content");
std::fs::create_dir_all(&store_path).unwrap();
let content = b"deduplicated content";
let hash = hex::encode(sha256(content));
// Store same content twice
std::fs::write(store_path.join(&hash), content).unwrap();
std::fs::write(store_path.join(&hash), content).unwrap();
// Only one file should exist (same hash = same path)
let entries: Vec<_> = std::fs::read_dir(&store_path)
.unwrap()
.collect();
assert_eq!(entries.len(), 1, "Deduplication: same content = one file");
}
// --- keys ↔ attestation ---
#[test]
fn key_operation_creates_attestation_entry() {
let dir = test_dir();
let attest_path = dir.path().join(".jk/attestation");
std::fs::create_dir_all(&attest_path).unwrap();
// Simulate key generation
let key_id = "550e8400-e29b-41d4-a716-446655440000";
let entry = format!(
r#"{{"op":"key_gen","key_id":"{}","algo":"aes256gcm","timestamp":{}}}"#,
key_id,
chrono_timestamp()
);
std::fs::write(attest_path.join("0001.json"), &entry).unwrap();
// Verify attestation references the key
let read_back = ({ use std::io::Read; std::fs::File::open(attest_path.join("0001.json").and_then(|mut f| { let mut buf = String::new(); f.take(10 * 1024 * 1024).read_to_string(&mut buf)?; Ok(buf) }) })).unwrap();
assert!(read_back.contains(key_id), "Attestation must reference key ID");
}
#[test]
fn attestation_chain_integrity() {
let dir = test_dir();
let attest_path = dir.path().join(".jk/attestation");
std::fs::create_dir_all(&attest_path).unwrap();
// Create chain of 3 entries
let mut prev_hash = "0000000000000000000000000000000000000000000000000000000000000000".to_string();
for i in 0..3 {
let entry = format!(
r#"{{"seq":{},"prev_hash":"{}","op":"copy","timestamp":{}}}"#,
i, prev_hash, chrono_timestamp()
);
let entry_hash = hex::encode(sha256(entry.as_bytes()));
std::fs::write(attest_path.join(format!("{:04}.json", i)), &entry).unwrap();
prev_hash = entry_hash;
}
// Verify chain: each entry's content hashes to the next's prev_hash
let entries: Vec<String> = (0..3)
.map(|i| {
({ use std::io::Read; std::fs::File::open(attest_path.join(format!("{:04}.json", i).and_then(|mut f| { let mut buf = String::new(); f.take(10 * 1024 * 1024).read_to_string(&mut buf)?; Ok(buf) }) }))).unwrap()
})
.collect();
for i in 1..entries.len() {
let prev_content_hash = hex::encode(sha256(entries[i - 1].as_bytes()));
assert!(
entries[i].contains(&prev_content_hash),
"Entry {} must reference hash of entry {}",
i,
i - 1
);
}
}
// --- transaction ↔ operations ---
#[test]
fn transaction_groups_operations() {
let dir = test_dir();
let tx_path = dir.path().join(".jk/transactions");
let ops_path = dir.path().join(".jk/operations");
std::fs::create_dir_all(&tx_path).unwrap();
std::fs::create_dir_all(&ops_path).unwrap();
// Begin transaction
let tx_id = "tx-001";
std::fs::write(
tx_path.join(format!("{}.json", tx_id)),
r#"{"state":"active","ops":[]}"#,
)
.unwrap();
// Execute operations within transaction
for i in 0..3 {
std::fs::write(
ops_path.join(format!("{}-op-{:04}.json", tx_id, i)),
format!(r#"{{"tx":"{}","op":"copy","seq":{}}}"#, tx_id, i),
)
.unwrap();
}
// Verify all ops reference the transaction
let op_files: Vec<_> = std::fs::read_dir(&ops_path)
.unwrap()
.filter_map(|e| e.ok())
.filter(|e| {
e.file_name()
.to_str()
.map_or(false, |n| n.starts_with(tx_id))
})
.collect();
assert_eq!(op_files.len(), 3, "Transaction must group all 3 operations");
}
// --- Helpers ---
fn sha256(data: &[u8]) -> [u8; 32] {
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
// Simplified hash for testing — real impl uses sha2 crate
let mut hasher = DefaultHasher::new();
data.hash(&mut hasher);
let h = hasher.finish();
let mut result = [0u8; 32];
result[..8].copy_from_slice(&h.to_le_bytes());
result[8..16].copy_from_slice(&h.to_be_bytes());
// Fill remaining with deterministic bytes
for i in 16..32 {
result[i] = result[i - 16] ^ (i as u8);
}
result
}
fn chrono_timestamp() -> u64 {
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_secs()
}
}