oxedyne/fe2o3/fe2o3_o3db_sync/tests/encrypted_delete.rs
8.0 KiB, 1 run
created by r1870400018:74394, 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 | //! A delete through [`O3dbCas`] or [`O3dbStorage`] on an encrypted store leaves the key absent. |
| 2 | //! |
| 3 | //! Both adapters wrote their tombstone with `store`, which encrypts it like any value. The |
| 4 | //! stored record was then neither a tombstone's length nor its bytes, so the key index, which |
| 5 | //! recognises only the unencrypted tombstone `delete` writes, called the key present: `has` |
| 6 | //! answered true for a deleted chunk, a second `delete` answered `Ok(true)` and wrote another |
| 7 | //! tombstone, and a scan listed the deleted key (OZ-HAVE check, 2026-10-16). |
| 8 | //! |
| 9 | //! A tombstone written the old way, through `store`, must still read as absent: `decode_stored` |
| 10 | //! treats a deleted-kind value found after decryption as no value. |
| 11 | //! |
| 12 | //! [Written with AI entirely](https://need2know.ai/entirely-ai/code)\ |
| 13 | //! Anthropic Claude |
| 14 | |
| 15 | use oxedyne_fe2o3_core::prelude::*; |
| 16 | use oxedyne_fe2o3_crypto::enc::EncryptionScheme; |
| 17 | use oxedyne_fe2o3_hash::{ |
| 18 | csum::ChecksumScheme, |
| 19 | hash::HashScheme, |
| 20 | }; |
| 21 | use oxedyne_fe2o3_iop_db::api::ScanOpts; |
| 22 | use oxedyne_fe2o3_jdat::prelude::*; |
| 23 | use oxedyne_fe2o3_o3db_sync::{ |
| 24 | O3db, |
| 25 | base::id::usr_kind_id_deleted, |
| 26 | cas::{ |
| 27 | Cas, |
| 28 | Chunk, |
| 29 | }, |
| 30 | cas_o3db::O3dbCas, |
| 31 | data::core::RestSchemesInput, |
| 32 | test::setup::{ |
| 33 | self, |
| 34 | Uid, |
| 35 | UID_LEN, |
| 36 | }, |
| 37 | }; |
| 38 | |
| 39 | use std::{ |
| 40 | path::PathBuf, |
| 41 | sync::Arc, |
| 42 | }; |
| 43 | |
| 44 | |
| 45 | type Db = O3db<{ UID_LEN }, Uid, EncryptionScheme, HashScheme, HashScheme, ChecksumScheme>; |
| 46 | type TestCas = O3dbCas<{ UID_LEN }, Uid, EncryptionScheme, HashScheme, HashScheme, ChecksumScheme>; |
| 47 | |
| 48 | fn root(name: &str) -> Outcome<PathBuf> { |
| 49 | let dir = PathBuf::from(env!("CARGO_TARGET_TMPDIR")).join(name); |
| 50 | let _ = std::fs::remove_dir_all(&dir); |
| 51 | res!(std::fs::create_dir_all(&dir)); |
| 52 | Ok(res!(dir.canonicalize())) |
| 53 | } |
| 54 | |
| 55 | fn open(db_root: &PathBuf, wipe: bool) -> Outcome<Db> { |
| 56 | let aes = res!(EncryptionScheme::new_aes_256_gcm_with_key(&[0x5au8; 32][..])); |
| 57 | let schms_input = RestSchemesInput::new( |
| 58 | Some(aes), |
| 59 | None::<HashScheme>, |
| 60 | None::<HashScheme>, |
| 61 | Some(ChecksumScheme::new_crc32()), |
| 62 | ); |
| 63 | let mut cfg = res!(setup::default_cfg()); |
| 64 | // Every zone in this test's own directory. |
| 65 | cfg.zone_overrides = Default::default(); |
| 66 | setup::start_db(db_root.clone(), Some(cfg), schms_input, None, false, wipe) |
| 67 | } |
| 68 | |
| 69 | fn check_gone(db: &Db, key: &Dat, prefix: &str, when: &str) -> Outcome<()> { |
| 70 | if res!(db.api().contains_wait(key, None)) { |
| 71 | return Err(err!("The deleted key {:?} reads present {}.", key, when; Test, Mismatch)); |
| 72 | } |
| 73 | if res!(db.api().get_wait(key, None)).is_some() { |
| 74 | return Err(err!("get finds the deleted key {:?} {}.", key, when; Test, Mismatch)); |
| 75 | } |
| 76 | let listed = res!(db.api().scan(&ScanOpts::with_str_prefix(prefix.to_string()), None)); |
| 77 | if !listed.is_empty() { |
| 78 | return Err(err!( |
| 79 | "A scan lists {} keys under '{}' {}, after the only one was deleted.", |
| 80 | listed.len(), prefix, when; |
| 81 | Test, Mismatch, Size)); |
| 82 | } |
| 83 | Ok(()) |
| 84 | } |
| 85 | |
| 86 | #[test] |
| 87 | fn cas_delete_on_an_encrypted_store_leaves_the_chunk_absent() -> Outcome<()> { |
| 88 | log_set_level!("warn"); |
| 89 | let outcome = run_cas(); |
| 90 | log_finish_wait!(); |
| 91 | outcome |
| 92 | } |
| 93 | |
| 94 | fn run_cas() -> Outcome<()> { |
| 95 | let db_root = res!(root("test_db_encrypted_delete_cas")); |
| 96 | let chunk = Chunk::new(b"a chunk deleted from an encrypted store".to_vec()); |
| 97 | let key = Dat::Str(fmt!("chunk:{}", chunk.id.to_hex())); |
| 98 | { |
| 99 | let db = Arc::new(res!(open(&db_root, true))); |
| 100 | let cas = TestCas::new(db.clone(), Uid::default()); |
| 101 | res!(cas.put(&chunk)); |
| 102 | if !res!(cas.has(&chunk.id)) { |
| 103 | return Err(err!("has() denies a chunk just put."; Test, Mismatch)); |
| 104 | } |
| 105 | if !res!(cas.delete(&chunk.id)) { |
| 106 | return Err(err!("The first delete says the chunk was not there."; Test, Mismatch)); |
| 107 | } |
| 108 | if res!(cas.has(&chunk.id)) { |
| 109 | return Err(err!("has() answers true for a deleted chunk."; Test, Mismatch)); |
| 110 | } |
| 111 | if res!(cas.get(&chunk.id)).is_some() { |
| 112 | return Err(err!("get() returns a deleted chunk."; Test, Mismatch)); |
| 113 | } |
| 114 | if res!(cas.delete(&chunk.id)) { |
| 115 | return Err(err!("A second delete answers Ok(true)."; Test, Mismatch)); |
| 116 | } |
| 117 | let ids = res!(cas.ids()); |
| 118 | if !ids.is_empty() { |
| 119 | return Err(err!("ids() lists {} chunks after the only one was deleted.", ids.len(); |
| 120 | Test, Mismatch, Size)); |
| 121 | } |
| 122 | res!(check_gone(&db, &key, "chunk:", "in the session that deleted it")); |
| 123 | res!(db.close()); |
| 124 | } |
| 125 | { |
| 126 | let db = Arc::new(res!(open(&db_root, false))); |
| 127 | let cas = TestCas::new(db.clone(), Uid::default()); |
| 128 | if res!(cas.has(&chunk.id)) { |
| 129 | return Err(err!("has() answers true for a deleted chunk after a reopen."; Test, Mismatch)); |
| 130 | } |
| 131 | if res!(cas.delete(&chunk.id)) { |
| 132 | return Err(err!("A delete after a reopen answers Ok(true)."; Test, Mismatch)); |
| 133 | } |
| 134 | res!(check_gone(&db, &key, "chunk:", "after a reopen")); |
| 135 | res!(db.close()); |
| 136 | } |
| 137 | Ok(()) |
| 138 | } |
| 139 | |
| 140 | #[cfg(feature = "dist")] |
| 141 | #[test] |
| 142 | fn storage_delete_on_an_encrypted_store_leaves_the_record_absent() -> Outcome<()> { |
| 143 | log_set_level!("warn"); |
| 144 | let outcome = run_storage(); |
| 145 | log_finish_wait!(); |
| 146 | outcome |
| 147 | } |
| 148 | |
| 149 | #[cfg(feature = "dist")] |
| 150 | fn run_storage() -> Outcome<()> { |
| 151 | use oxedyne_fe2o3_o3db_sync::dist::{ |
| 152 | o3db_storage::O3dbStorage, |
| 153 | record::{ |
| 154 | Record, |
| 155 | RecordId, |
| 156 | }, |
| 157 | storage::Storage, |
| 158 | }; |
| 159 | |
| 160 | let db_root = res!(root("test_db_encrypted_delete_storage")); |
| 161 | let id = RecordId::from_bytes([0xa5; 32]); |
| 162 | let rec = Record::new(id, "ident", b"a record deleted from an encrypted store".to_vec()); |
| 163 | let key = Dat::Str(fmt!("ident:{}", "a5".repeat(32))); |
| 164 | { |
| 165 | let db = Arc::new(res!(open(&db_root, true))); |
| 166 | let storage = O3dbStorage::new(db.clone(), Uid::default()); |
| 167 | res!(storage.put(&rec)); |
| 168 | if !res!(db.api().contains_wait(&key, None)) { |
| 169 | return Err(err!("The record just put reads absent under {:?}.", key; Test, Mismatch)); |
| 170 | } |
| 171 | if !res!(storage.delete("ident", &id)) { |
| 172 | return Err(err!("The first delete says the record was not there."; Test, Mismatch)); |
| 173 | } |
| 174 | if res!(storage.get("ident", &id)).is_some() { |
| 175 | return Err(err!("get() returns a deleted record."; Test, Mismatch)); |
| 176 | } |
| 177 | if res!(storage.delete("ident", &id)) { |
| 178 | return Err(err!("A second delete answers Ok(true)."; Test, Mismatch)); |
| 179 | } |
| 180 | let digests = res!(storage.digests("ident")); |
| 181 | if !digests.is_empty() { |
| 182 | return Err(err!("digests() lists {} records after the only one was deleted.", |
| 183 | digests.len(); Test, Mismatch, Size)); |
| 184 | } |
| 185 | res!(check_gone(&db, &key, "ident:", "in the session that deleted it")); |
| 186 | res!(db.close()); |
| 187 | } |
| 188 | { |
| 189 | let db = Arc::new(res!(open(&db_root, false))); |
| 190 | let storage = O3dbStorage::new(db.clone(), Uid::default()); |
| 191 | if res!(storage.delete("ident", &id)) { |
| 192 | return Err(err!("A delete after a reopen answers Ok(true)."; Test, Mismatch)); |
| 193 | } |
| 194 | res!(check_gone(&db, &key, "ident:", "after a reopen")); |
| 195 | res!(db.close()); |
| 196 | } |
| 197 | Ok(()) |
| 198 | } |
| 199 | |
| 200 | #[test] |
| 201 | fn a_tombstone_stored_the_old_way_reads_absent_on_an_encrypted_store() -> Outcome<()> { |
| 202 | log_set_level!("warn"); |
| 203 | let outcome = run_legacy(); |
| 204 | log_finish_wait!(); |
| 205 | outcome |
| 206 | } |
| 207 | |
| 208 | fn check_legacy_absent(db: &Db, cas: &TestCas, chunk: &Chunk, key: &Dat, when: &str) |
| 209 | -> Outcome<()> |
| 210 | { |
| 211 | if let Some(found) = res!(db.api().get_wait(key, None)) { |
| 212 | return Err(err!("get_wait reads {:?} for a tombstone stored through store {}.", |
| 213 | found.0, when; Test, Mismatch)); |
| 214 | } |
| 215 | if res!(cas.get(&chunk.id)).is_some() { |
| 216 | return Err(err!("O3dbCas::get returns a chunk deleted through store {}.", when; |
| 217 | Test, Mismatch)); |
| 218 | } |
| 219 | Ok(()) |
| 220 | } |
| 221 | |
| 222 | fn run_legacy() -> Outcome<()> { |
| 223 | let db_root = res!(root("test_db_encrypted_delete_legacy")); |
| 224 | let chunk = Chunk::new(b"a chunk deleted the old way, through store".to_vec()); |
| 225 | let key = Dat::Str(fmt!("chunk:{}", chunk.id.to_hex())); |
| 226 | { |
| 227 | let db = Arc::new(res!(open(&db_root, true))); |
| 228 | let cas = TestCas::new(db.clone(), Uid::default()); |
| 229 | res!(cas.put(&chunk)); |
| 230 | // The pre-2026-10-16 delete: a tombstone through store, so encrypted. |
| 231 | let tomb = Dat::Usr(usr_kind_id_deleted(), Some(Box::new(Dat::Empty))); |
| 232 | let resp = res!(db.api().store(key.clone(), tomb, Uid::default())); |
| 233 | res!(resp.recv_store_ack()); |
| 234 | res!(check_legacy_absent(&db, &cas, &chunk, &key, "in the session that stored it")); |
| 235 | res!(db.close()); |
| 236 | } |
| 237 | { |
| 238 | let db = Arc::new(res!(open(&db_root, false))); |
| 239 | let cas = TestCas::new(db.clone(), Uid::default()); |
| 240 | res!(check_legacy_absent(&db, &cas, &chunk, &key, "after a reopen")); |
| 241 | res!(db.close()); |
| 242 | } |
| 243 | Ok(()) |
| 244 | } |