crates/state/blobs/tests/blobs.rs 7.4 KiB 253 lines Raw
1 //! The blob store and the SHA-256 that names its contents.2 3 use std::io::Write;4 use std::sync::{Arc, Mutex};5 6 use inf3r_blobs::{BlobError, Blobs, is_hash, sha256_hex};7 8 /// A fresh directory under the test's own name.9 fn store(name: &str, limit: u64) -> (Blobs, std::path::PathBuf) {10     let dir = std::env::temp_dir().join(format!("inf3r-blobs-{name}-{}", std::process::id()));11     let _ = std::fs::remove_dir_all(&dir);12     std::fs::create_dir_all(&dir).unwrap();13     (Blobs::open(&dir, limit).unwrap(), dir)14 }15 16 fn cleanup(dir: &std::path::Path) {17     let _ = std::fs::remove_dir_all(dir);18 }19 20 #[test]21 fn sha256_matches_the_published_vectors() {22     // FIPS 180-4, appendix B.23     assert_eq!(24         sha256_hex(b""),25         "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"26     );27     assert_eq!(28         sha256_hex(b"abc"),29         "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"30     );31     assert_eq!(32         sha256_hex(b"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"),33         "248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1"34     );35 }36 37 #[test]38 fn sha256_covers_every_block_boundary() {39     // The tail of the message is where an off-by-one lives, so walk it.40     for len in [1, 55, 56, 57, 63, 64, 65, 119, 120, 128, 200] {41         let bytes = vec![0x61u8; len];42         let hash = sha256_hex(&bytes);43         assert!(is_hash(&hash), "len {len} gave {hash}");44         assert_ne!(hash, sha256_hex(&vec![0x62u8; len]), "len {len} collided");45     }46     // A million 'a' is the FIPS long-message vector.47     let million = vec![b'a'; 1_000_000];48     assert_eq!(49         sha256_hex(&million),50         "cdc76e5c9914fb9281a1c7e284d73e67f1809a48a497200e046d39ccc7112cd0"51     );52 }53 54 #[test]55 fn a_blob_is_named_by_its_bytes() {56     let (mut blobs, dir) = store("named", 1024);57     let (hash, wrote) = blobs.put(b"hello").unwrap();58 59     assert!(wrote);60     assert_eq!(hash, sha256_hex(b"hello"));61     assert_eq!(blobs.get(&hash).unwrap(), b"hello");62     assert_eq!(blobs.bytes(), 5);63 64     // The same bytes are the same blob and are written once.65     let (again, wrote) = blobs.put(b"hello").unwrap();66     assert_eq!(again, hash);67     assert!(!wrote);68     assert_eq!(blobs.bytes(), 5);69     cleanup(&dir);70 }71 72 #[test]73 fn publish_refuses_bytes_that_are_not_the_address() {74     let (mut blobs, dir) = store("publish", 1024);75     let want = sha256_hex(b"hello");76 77     assert_eq!(78         blobs.publish(&want, b"goodbye").unwrap_err(),79         BlobError::Mismatch {80             want: want.clone(),81             got: sha256_hex(b"goodbye"),82         }83     );84     assert_eq!(85         blobs.publish("not-a-hash", b"hello").unwrap_err(),86         BlobError::Address("not-a-hash".into())87     );88     assert!(blobs.publish(&want, b"hello").is_ok());89     cleanup(&dir);90 }91 92 #[test]93 fn the_lru_removes_the_blob_that_was_read_longest_ago() {94     let (mut blobs, dir) = store("lru", 8);95     let (a, _) = blobs.put(b"aaaa").unwrap();96     let (b, _) = blobs.put(b"bbbb").unwrap();97     assert_eq!(blobs.bytes(), 8);98 99     // Reading `a` makes `b` the least recently used.100     assert_eq!(blobs.get(&a).unwrap(), b"aaaa");101     let (c, _) = blobs.put(b"cccc").unwrap();102 103     assert_eq!(blobs.get(&a).unwrap(), b"aaaa");104     assert_eq!(blobs.get(&c).unwrap(), b"cccc");105     assert_eq!(blobs.get(&b).unwrap_err(), BlobError::Missing(b.clone()));106     assert_eq!(blobs.bytes(), 8);107     cleanup(&dir);108 }109 110 #[test]111 fn a_held_blob_is_never_removed_and_dropping_the_hold_releases_it() {112     let (blobs, dir) = store("held", 8);113     let blobs = Arc::new(Mutex::new(blobs));114     let (a, b) = {115         let mut store = blobs.lock().unwrap();116         let (a, _) = store.put(b"aaaa").unwrap();117         let (b, _) = store.put(b"bbbb").unwrap();118         (a, b)119     };120     let hold = Blobs::hold(&blobs, &a).unwrap();121     let other = Blobs::hold(&blobs, &b).unwrap();122 123     // Both blobs are held, so nothing can make room for a third.124     assert_eq!(125         blobs.lock().unwrap().put(b"cccc").unwrap_err(),126         BlobError::Full { limit: 8, held: 8 }127     );128     assert_eq!(blobs.lock().unwrap().get(&a).unwrap(), b"aaaa");129     assert_eq!(130         blobs.lock().unwrap().remove(&a).unwrap_err(),131         BlobError::Held(a.clone())132     );133 134     drop(hold);135     assert!(blobs.lock().unwrap().get(&a).is_ok());136     assert_eq!(blobs.lock().unwrap().remove(&a).unwrap(), ());137     drop(other);138 139     let (c, _) = blobs.lock().unwrap().put(b"cccc").unwrap();140     assert_eq!(blobs.lock().unwrap().get(&c).unwrap(), b"cccc");141     cleanup(&dir);142 }143 144 #[test]145 fn a_blob_larger_than_the_store_is_refused_before_anything_is_removed() {146     let (mut blobs, dir) = store("large", 4);147     let (a, _) = blobs.put(b"aaaa").unwrap();148 149     assert_eq!(150         blobs.put(b"bbbbb").unwrap_err(),151         BlobError::TooLarge { bytes: 5, limit: 4 }152     );153     assert_eq!(blobs.get(&a).unwrap(), b"aaaa");154     cleanup(&dir);155 }156 157 #[test]158 fn a_reopened_store_finds_its_blobs() {159     let (mut blobs, dir) = store("reopen", 1024);160     let (hash, _) = blobs.put(b"persistent").unwrap();161     drop(blobs);162 163     let mut again = Blobs::open(&dir, 1024).unwrap();164     assert_eq!(again.bytes(), 10);165     assert_eq!(again.get(&hash).unwrap(), b"persistent");166     assert_eq!(again.entries().count(), 1);167     cleanup(&dir);168 }169 170 #[test]171 fn put_reader_takes_a_file_that_a_plugin_wrote() {172     let (mut blobs, dir) = store("reader", 1024);173     let mut temp = tempfile(dir.as_path());174     temp.write_all(b"artifact").unwrap();175     temp.rewind_ok();176     let (hash, _) = blobs.put_reader(&mut temp).unwrap();177 178     assert_eq!(blobs.get(&hash).unwrap(), b"artifact");179     cleanup(&dir);180 }181 182 #[test]183 fn put_reader_len_refuses_a_short_descriptor() {184     let (mut blobs, dir) = store("short", 1024);185     let mut temp = tempfile(dir.as_path());186     temp.write_all(b"artifact").unwrap();187     temp.rewind_ok();188 189     assert_eq!(190         blobs.put_reader_len(&mut temp, 20).unwrap_err(),191         BlobError::Length {192             declared: 20,193             got: 8194         }195     );196     assert_eq!(blobs.bytes(), 0);197     cleanup(&dir);198 }199 200 #[test]201 fn put_reader_len_refuses_a_long_descriptor() {202     let (mut blobs, dir) = store("long", 1024);203     let mut temp = tempfile(dir.as_path());204     temp.write_all(b"artifact").unwrap();205     temp.rewind_ok();206 207     assert_eq!(208         blobs.put_reader_len(&mut temp, 4).unwrap_err(),209         BlobError::Length {210             declared: 4,211             got: 5212         }213     );214     assert_eq!(blobs.bytes(), 0);215     cleanup(&dir);216 }217 218 #[test]219 fn opening_the_store_removes_a_leftover_part_file() {220     let dir = std::env::temp_dir().join(format!("inf3r-blobs-leftover-{}", std::process::id()));221     let _ = std::fs::remove_dir_all(&dir);222     std::fs::create_dir_all(&dir).unwrap();223     let leftover = dir.join(".upload-1-1.part");224     std::fs::write(&leftover, b"half a blob").unwrap();225 226     let blobs = Blobs::open(&dir, 1024).unwrap();227     assert_eq!(blobs.entries().count(), 0);228     assert!(!leftover.exists());229     cleanup(&dir);230 }231 232 /// A file in `dir` that the test can write and then read from the start.233 fn tempfile(dir: &std::path::Path) -> std::fs::File {234     let path = dir.join("artifact.part");235     std::fs::OpenOptions::new()236         .create(true)237         .truncate(true)238         .read(true)239         .write(true)240         .open(path)241         .unwrap()242 }243 244 trait Rewind {245     fn rewind_ok(&mut self);246 }247 248 impl Rewind for std::fs::File {249     fn rewind_ok(&mut self) {250         use std::io::Seek;251         self.seek(std::io::SeekFrom::Start(0)).unwrap();252     }253 }