| 1 | use super::*; |
| 2 | use std::sync::atomic::{AtomicU64, Ordering}; |
| 3 | |
| 4 | static NEXT: AtomicU64 = AtomicU64::new(0); |
| 5 | struct Source(std::path::PathBuf); |
| 6 | impl Source { |
| 7 | fn new() -> Self { |
| 8 | let root = std::env::var_os("TMPDIR") |
| 9 | .map(std::path::PathBuf::from) |
| 10 | .unwrap_or_else(|| { |
| 11 | std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../tests/work/tmp") |
| 12 | }); |
| 13 | std::fs::create_dir_all(&root).unwrap(); |
| 14 | let path = root.join(format!( |
| 15 | "direct-source-{}-{}", |
| 16 | std::process::id(), |
| 17 | NEXT.fetch_add(1, Ordering::Relaxed) |
| 18 | )); |
| 19 | std::fs::create_dir(&path).unwrap(); |
| 20 | std::fs::write(path.join("input"), vec![42; 1024 * 1024]).unwrap(); |
| 21 | Self(path) |
| 22 | } |
| 23 | fn path(&self) -> std::path::PathBuf { |
| 24 | self.0.join("input") |
| 25 | } |
| 26 | } |
| 27 | impl Drop for Source { |
| 28 | fn drop(&mut self) { |
| 29 | std::fs::remove_dir_all(&self.0).unwrap(); |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | #[test] |
| 34 | fn direct_source_matches_copied_identity_length_and_hash() { |
| 35 | let source = Source::new(); |
| 36 | let (direct, mut guard) = |
| 37 | Snapshot::open_direct(&source.path(), u64::MAX, &mut |_| Ok(())).unwrap(); |
| 38 | let copied = Snapshot::open_reporting(&source.path(), u64::MAX, &mut |_| Ok(())).unwrap(); |
| 39 | assert_eq!(direct.object, copied.object); |
| 40 | assert_eq!(direct.len, copied.len); |
| 41 | assert_eq!(direct.hash, copied.hash); |
| 42 | guard.verify(&mut |_| Ok(())).unwrap(); |
| 43 | } |
| 44 | |
| 45 | #[cfg(unix)] |
| 46 | #[test] |
| 47 | fn direct_source_detects_same_size_and_restored_mtime_changes() { |
| 48 | let source = Source::new(); |
| 49 | let (_, mut guard) = Snapshot::open_direct(&source.path(), u64::MAX, &mut |_| Ok(())).unwrap(); |
| 50 | let modified = std::fs::metadata(source.path()) |
| 51 | .unwrap() |
| 52 | .modified() |
| 53 | .unwrap(); |
| 54 | let mut file = OpenOptions::new().write(true).open(source.path()).unwrap(); |
| 55 | file.seek(SeekFrom::Start(512)).unwrap(); |
| 56 | file.write_all(b"x").unwrap(); |
| 57 | file.set_times(std::fs::FileTimes::new().set_modified(modified)) |
| 58 | .unwrap(); |
| 59 | assert!(matches!( |
| 60 | guard.verify(&mut |_| Ok(())), |
| 61 | Err(ErrorCode::SourceChanged) |
| 62 | )); |
| 63 | } |
| 64 | |
| 65 | #[cfg(unix)] |
| 66 | #[test] |
| 67 | fn direct_source_detects_named_replacement_even_when_open_handle_survives() { |
| 68 | let source = Source::new(); |
| 69 | let (_, mut guard) = Snapshot::open_direct(&source.path(), u64::MAX, &mut |_| Ok(())).unwrap(); |
| 70 | std::fs::rename(source.path(), source.0.join("old")).unwrap(); |
| 71 | std::fs::write(source.path(), vec![42; 1024 * 1024]).unwrap(); |
| 72 | assert!(matches!( |
| 73 | guard.verify(&mut |_| Ok(())), |
| 74 | Err(ErrorCode::SourceChanged) |
| 75 | )); |
| 76 | } |
| 77 | |
| 78 | #[cfg(windows)] |
| 79 | #[test] |
| 80 | fn direct_source_allows_existing_readers_and_blocks_writers_until_closed() { |
| 81 | use std::os::windows::fs::OpenOptionsExt; |
| 82 | let source = Source::new(); |
| 83 | let reader = OpenOptions::new() |
| 84 | .read(true) |
| 85 | .share_mode(3) |
| 86 | .open(source.path()) |
| 87 | .unwrap(); |
| 88 | let (snapshot, guard) = |
| 89 | Snapshot::open_direct(&source.path(), u64::MAX, &mut |_| Ok(())).unwrap(); |
| 90 | assert!(OpenOptions::new().read(true).open(source.path()).is_ok()); |
| 91 | assert!(OpenOptions::new().write(true).open(source.path()).is_err()); |
| 92 | drop((snapshot, guard, reader)); |
| 93 | assert!(OpenOptions::new().write(true).open(source.path()).is_ok()); |
| 94 | } |
| 95 | |
| 96 | #[cfg(unix)] |
| 97 | #[test] |
| 98 | fn direct_source_detects_transient_change_even_after_bytes_and_mtime_are_restored() { |
| 99 | let source = Source::new(); |
| 100 | let (_, mut guard) = Snapshot::open_direct(&source.path(), u64::MAX, &mut |_| Ok(())).unwrap(); |
| 101 | let modified = std::fs::metadata(source.path()) |
| 102 | .unwrap() |
| 103 | .modified() |
| 104 | .unwrap(); |
| 105 | let mut file = OpenOptions::new().write(true).open(source.path()).unwrap(); |
| 106 | file.write_all(b"x").unwrap(); |
| 107 | file.rewind().unwrap(); |
| 108 | file.write_all(b"*").unwrap(); |
| 109 | file.set_times(std::fs::FileTimes::new().set_modified(modified)) |
| 110 | .unwrap(); |
| 111 | assert!(matches!( |
| 112 | guard.verify(&mut |_| Ok(())), |
| 113 | Err(ErrorCode::SourceChanged) |
| 114 | )); |
| 115 | } |