Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_o3db_sync/tests/dist_content.rs

5.2 KiB, 1 run

created by r1870400018:67486, which is this file's identity for as long as the history lasts, whatever it is later renamed to

download · who wrote it · its history

1#![cfg(feature = "dist")]
2//! Integration tests for the content hash behind [`RecordDigest::content`].
3//!
4//! Anti-entropy compares these digests to decide what to repair, so two peers must
5//! digest the same bytes alike and different bytes apart, whichever [`Storage`]
6//! adapter each runs. The known answers below were computed outside Rust with
7//! python3's `hashlib.sha3_256` over `len as u64 big-endian ‖ value`.
8//!
9//! [Written with AI entirely](https://need2know.ai/entirely-ai/code)\
10//! Anthropic Claude
11
12use oxedyne_fe2o3_core::prelude::*;
13use oxedyne_fe2o3_crypto::enc::EncryptionScheme;
14use oxedyne_fe2o3_hash::{
15 csum::ChecksumScheme,
16 hash::HashScheme,
17};
18use oxedyne_fe2o3_jdat::prelude::*;
19use oxedyne_fe2o3_o3db_sync::{
20 data::core::RestSchemesInput,
21 dist::{
22 o3db_storage::O3dbStorage,
23 record::{
24 Record,
25 RecordDigest,
26 RecordId,
27 content_hash,
28 },
29 resolve::LastVersionWins,
30 storage::{
31 MemoryStorage,
32 Storage,
33 },
34 },
35 test::setup,
36};
37
38use std::{
39 path::Path,
40 sync::Arc,
41 thread,
42 time::Duration,
43};
44
45
46fn hex(bytes: &[u8]) -> String {
47 let mut s = String::with_capacity(bytes.len() * 2);
48 for b in bytes {
49 s.push_str(&fmt!("{:02x}", b));
50 }
51 s
52}
53
54fn rid(b: u8) -> RecordId {
55 RecordId::from_bytes([b; 32])
56}
57
58/// The digest `storage` gives the one record it holds in the table "t".
59fn digest_of<S: Storage>(storage: &S) -> Outcome<RecordDigest> {
60 let mut d = res!(storage.digests("t"));
61 assert_eq!(d.len(), 1);
62 Ok(d.remove(0))
63}
64
65/// A `MemoryStorage` holding one record with this value at id 1 of table "t".
66fn mem_with(value: &[u8]) -> Outcome<MemoryStorage> {
67 let storage = MemoryStorage::new();
68 res!(storage.put(&Record::new(rid(1), "t", value.to_vec())));
69 Ok(storage)
70}
71
72#[test]
73fn content_hash_known_answer() {
74 // SHA3-256 of eight zero bytes, the length of the empty value.
75 assert_eq!(
76 hex(&content_hash(b"")),
77 "48dda5bbe9171a6656206ec56c595c5834b6cf38c5fe71bcb44fe43833aee9df",
78 );
79 // Seven zero bytes, 0x01 (the length), then 0xab.
80 assert_eq!(
81 hex(&content_hash(&[0xAB])),
82 "5489666bad674de9ffe884577300c6e0edabad022917e889d5fc21c9364a8b0b",
83 );
84 // Seven zero bytes, 0x02, then 0xab 0x00.
85 assert_eq!(
86 hex(&content_hash(&[0xAB, 0x00])),
87 "daf62553bc0a272a7491fcb69de7b547a95ce6ea4d6f5d6fdd687cfd7d46e1dd",
88 );
89}
90
91#[test]
92fn trailing_zero_values_digest_apart() -> Outcome<()> {
93 // A value and the same value with a trailing zero byte.
94 let a = res!(digest_of(&res!(mem_with(&[0xAB]))));
95 let b = res!(digest_of(&res!(mem_with(&[0xAB, 0x00]))));
96 assert_ne!(a.content, b.content);
97 // One zero byte and an empty payload under a zero version, which is eight zero bytes.
98 let c = res!(digest_of(&res!(mem_with(&[0]))));
99 let d = res!(digest_of(&res!(mem_with(&LastVersionWins::value(0, b"")))));
100 assert_ne!(c.content, d.content);
101 Ok(())
102}
103
104#[test]
105fn memory_storage_digests_with_the_protocol_hash() -> Outcome<()> {
106 // What the storage reports is the protocol hash of the stored value, not a copy of it.
107 let values: [&[u8]; 4] = [b"", &[0xAB], &[0xAB, 0x00], b"a longer value than one chunk of eight bytes"];
108 for value in values {
109 let d = res!(digest_of(&res!(mem_with(value))));
110 assert_eq!(d.id, rid(1));
111 assert_eq!(d.content, content_hash(value));
112 }
113 Ok(())
114}
115
116#[test]
117fn o3db_storage_digests_with_the_protocol_hash() -> Outcome<()> {
118 let db_root = res!(Path::new("./test_db_dist_content")
119 .canonicalize()
120 .or_else(|_| -> std::io::Result<_> {
121 ok!(std::fs::create_dir_all("./test_db_dist_content"));
122 Path::new("./test_db_dist_content").canonicalize()
123 }));
124 let enckey = [0x44u8; 32];
125 let aes_gcm = res!(EncryptionScheme::new_aes_256_gcm_with_key(&enckey[..]));
126 let crc32 = ChecksumScheme::new_crc32();
127 let user = setup::Uid::default();
128 let schms_input = RestSchemesInput::new(
129 Some(aes_gcm.clone()),
130 None::<HashScheme>,
131 None::<HashScheme>,
132 Some(crc32.clone()),
133 );
134 let mut cfg = res!(setup::default_cfg());
135 cfg.num_zones = 2;
136 cfg.num_cbots_per_zone = 2;
137 cfg.num_igbots_per_zone = 2;
138 cfg.data_file_max_bytes = 200_000;
139 cfg.zone_overrides = mapdat!{
140 1u16 => mapdat!{ "dir" => "", "max_size" => 1_000_000u64 },
141 }.get_map().unwrap();
142 let db = res!(setup::start_db(db_root.clone(), Some(cfg), schms_input, None, true, true));
143 thread::sleep(Duration::from_secs(1));
144 let db = Arc::new(db);
145 let o3 = O3dbStorage::new(Arc::clone(&db), user);
146 let mem = MemoryStorage::new();
147
148 // Two values that differ only by a trailing zero, and one the old hash read as the empty value.
149 let values: [(u8, &[u8]); 3] = [(1, &[0xAB]), (2, &[0xAB, 0x00]), (3, &[0, 0, 0])];
150 for (n, value) in values {
151 let record = Record::new(rid(n), "t", value.to_vec());
152 res!(o3.put(&record));
153 res!(mem.put(&record));
154 }
155 thread::sleep(Duration::from_millis(500));
156
157 let held = res!(o3.digests("t"));
158 assert_eq!(held.len(), 3);
159 for d in &held {
160 let value = values.iter().find(|(n, _)| rid(*n) == d.id).map(|(_, v)| *v);
161 let value = res!(value.ok_or_else(|| err!("Unexpected id {} in the O3db digests.", hex(d.id.as_bytes()); Test, Mismatch)));
162 assert_eq!(d.content, content_hash(value));
163 }
164 // A mixed cluster of one adapter of each kind must digest the same records alike.
165 assert_eq!(held, res!(mem.digests("t")));
166 assert_ne!(held[0].content, held[1].content);
167 Ok(())
168}