| 1 | use publication::{ |
| 2 | Cleanup, Durability, Error, Limits, Outcome, Publication, Report, Request, Stop, Usage, |
| 3 | }; |
| 4 | use std::{ |
| 5 | ffi::{OsStr, OsString}, |
| 6 | fs::{self, DirBuilder, OpenOptions}, |
| 7 | io::Write, |
| 8 | os::unix::fs::{DirBuilderExt, MetadataExt, OpenOptionsExt}, |
| 9 | path::{Path, PathBuf}, |
| 10 | sync::atomic::{AtomicU64, Ordering}, |
| 11 | }; |
| 12 | |
| 13 | static NEXT: AtomicU64 = AtomicU64::new(1); |
| 14 | |
| 15 | pub struct Sandbox { |
| 16 | pub root: PathBuf, |
| 17 | identity: (u64, u64), |
| 18 | } |
| 19 | |
| 20 | impl Sandbox { |
| 21 | pub fn new() -> Self { |
| 22 | let runs = Path::new(env!("CARGO_MANIFEST_DIR")).join("runs"); |
| 23 | let info = fs::symlink_metadata(&runs).expect("runner가 전용 runs를 준비해야 함"); |
| 24 | assert!(info.is_dir() && !info.file_type().is_symlink()); |
| 25 | assert_eq!(info.mode() & 0o777, 0o700); |
| 26 | assert_eq!(info.uid(), unsafe { libc::geteuid() }); |
| 27 | assert_eq!(runs.canonicalize().unwrap(), runs); |
| 28 | let root = runs.join(format!( |
| 29 | "case-{}-{}", |
| 30 | std::process::id(), |
| 31 | NEXT.fetch_add(1, Ordering::Relaxed) |
| 32 | )); |
| 33 | DirBuilder::new().mode(0o700).create(&root).unwrap(); |
| 34 | let info = fs::symlink_metadata(&root).unwrap(); |
| 35 | let sandbox = Self { |
| 36 | root, |
| 37 | identity: (info.dev(), info.ino()), |
| 38 | }; |
| 39 | sandbox.dir("parent"); |
| 40 | sandbox |
| 41 | } |
| 42 | |
| 43 | pub fn path(&self, name: impl AsRef<Path>) -> PathBuf { |
| 44 | self.root.join(name) |
| 45 | } |
| 46 | |
| 47 | pub fn parent(&self) -> PathBuf { |
| 48 | self.path("parent") |
| 49 | } |
| 50 | |
| 51 | pub fn dir(&self, name: impl AsRef<Path>) -> PathBuf { |
| 52 | let path = self.path(name); |
| 53 | DirBuilder::new().mode(0o700).create(&path).unwrap(); |
| 54 | path |
| 55 | } |
| 56 | |
| 57 | pub fn file(&self, name: impl AsRef<Path>, bytes: &[u8]) -> PathBuf { |
| 58 | let path = self.path(name); |
| 59 | let mut handle = OpenOptions::new() |
| 60 | .write(true) |
| 61 | .create_new(true) |
| 62 | .mode(0o600) |
| 63 | .open(&path) |
| 64 | .unwrap(); |
| 65 | handle.write_all(bytes).unwrap(); |
| 66 | handle.sync_all().unwrap(); |
| 67 | path |
| 68 | } |
| 69 | |
| 70 | pub fn finish(self) { |
| 71 | assert!( |
| 72 | owned_descriptors(&self.root).is_empty(), |
| 73 | "작업 디렉터리의 열린 fd가 남아 있음" |
| 74 | ); |
| 75 | let info = fs::symlink_metadata(&self.root).unwrap(); |
| 76 | assert!(info.is_dir() && !info.file_type().is_symlink()); |
| 77 | assert_eq!((info.dev(), info.ino()), self.identity); |
| 78 | fs::remove_dir_all(&self.root).unwrap(); |
| 79 | assert!(!self.root.exists()); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | pub fn pattern(length: usize) -> Vec<u8> { |
| 84 | (0..length).map(|i| ((i * 37 + 11) % 251) as u8).collect() |
| 85 | } |
| 86 | |
| 87 | pub fn names(parent: &Path) -> Vec<OsString> { |
| 88 | let mut result: Vec<_> = fs::read_dir(parent) |
| 89 | .unwrap() |
| 90 | .map(|e| e.unwrap().file_name()) |
| 91 | .collect(); |
| 92 | result.sort(); |
| 93 | result |
| 94 | } |
| 95 | |
| 96 | pub fn owned_descriptors(root: &Path) -> Vec<u32> { |
| 97 | let mut result = Vec::new(); |
| 98 | for item in fs::read_dir("/proc/self/fd").unwrap() { |
| 99 | let item = item.unwrap(); |
| 100 | if let Ok(target) = fs::read_link(item.path()) { |
| 101 | if target.starts_with(root) { |
| 102 | result.push(item.file_name().to_str().unwrap().parse().unwrap()); |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | result.sort(); |
| 107 | result |
| 108 | } |
| 109 | |
| 110 | pub fn publish(parent: &Path, leaf: &OsStr, bytes: &[u8], limits: Limits) -> Report<()> { |
| 111 | let (task, cancel) = Publication::new(); |
| 112 | let report = task.publish( |
| 113 | Request { |
| 114 | parent, |
| 115 | leaf, |
| 116 | bytes, |
| 117 | limits, |
| 118 | }, |
| 119 | |_| Ok::<_, Stop<()>>(()), |
| 120 | ); |
| 121 | assert_eq!(cancel.request(), publication::CancelDecision::Finished); |
| 122 | report |
| 123 | } |
| 124 | |
| 125 | pub fn not_committed<E>(report: Report<E>) -> (Error<E>, Usage, Cleanup) { |
| 126 | let Report { |
| 127 | outcome, |
| 128 | usage, |
| 129 | cleanup, |
| 130 | } = report; |
| 131 | match outcome { |
| 132 | Outcome::NotCommitted(error) => (error, usage, cleanup), |
| 133 | Outcome::CommitUncertain { source } => panic!("unexpected uncertain commit: {source}"), |
| 134 | Outcome::Committed { .. } => panic!("unexpected committed result"), |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | pub fn cleanup(cleanup: &Cleanup, attempts: u8) { |
| 139 | assert_eq!(cleanup.attempted, attempts); |
| 140 | assert!(cleanup.all_close_calls_succeeded(), "{cleanup:?}"); |
| 141 | assert!(cleanup.failures.iter().all(Option::is_none)); |
| 142 | } |
| 143 | |
| 144 | pub fn full_usage(usage: Usage, length: usize, linked: bool) { |
| 145 | let chunks = (length as u64).div_ceil(65_536); |
| 146 | assert_eq!(usage.open_calls, 4); |
| 147 | assert_eq!(usage.metadata_calls, 5); |
| 148 | assert_eq!(usage.write_calls, chunks); |
| 149 | assert_eq!(usage.read_calls, chunks + 1); |
| 150 | assert_eq!(usage.actual_written_bytes, length as u64); |
| 151 | assert_eq!(usage.actual_read_bytes, length as u64); |
| 152 | assert_eq!(usage.reserved_io_bytes, 2 * length as u64 + 1); |
| 153 | assert_eq!(usage.link_calls, 1); |
| 154 | assert_eq!(usage.sync_calls, if linked { 2 } else { 1 }); |
| 155 | } |
| 156 | |
| 157 | pub fn committed(report: Report<()>, parent: &Path, leaf: &OsStr, bytes: &[u8]) { |
| 158 | cleanup(&report.cleanup, 4); |
| 159 | full_usage(report.usage, bytes.len(), true); |
| 160 | let (receipt, durability) = match report.outcome { |
| 161 | Outcome::Committed { |
| 162 | receipt, |
| 163 | durability, |
| 164 | } => (receipt, durability), |
| 165 | Outcome::NotCommitted(error) => panic!("unexpected noncommit: {error:?}"), |
| 166 | Outcome::CommitUncertain { source } => panic!("unexpected uncertain commit: {source}"), |
| 167 | }; |
| 168 | assert!(matches!(durability, Durability::FileAndParentSynced)); |
| 169 | let parent_meta = fs::metadata(parent).unwrap(); |
| 170 | let path = parent.join(leaf); |
| 171 | let file_meta = fs::symlink_metadata(&path).unwrap(); |
| 172 | assert!(file_meta.is_file() && !file_meta.file_type().is_symlink()); |
| 173 | assert_eq!(file_meta.nlink(), 1); |
| 174 | assert_eq!(file_meta.mode() & 0o077, 0); |
| 175 | assert_eq!(file_meta.uid(), unsafe { libc::geteuid() }); |
| 176 | assert_eq!(receipt.length(), bytes.len() as u64); |
| 177 | assert_eq!(receipt.parent_identity().device(), parent_meta.dev()); |
| 178 | assert_eq!(receipt.parent_identity().inode(), parent_meta.ino()); |
| 179 | assert_eq!(receipt.file_identity().device(), file_meta.dev()); |
| 180 | assert_eq!(receipt.file_identity().inode(), file_meta.ino()); |
| 181 | assert_eq!(fs::read(path).unwrap(), bytes); |
| 182 | assert_eq!(names(parent), vec![leaf.to_os_string()]); |
| 183 | } |
| 184 | |
| 185 | pub fn conflict(report: Report<()>, length: usize) { |
| 186 | let (error, usage, closed) = not_committed(report); |
| 187 | match error { |
| 188 | Error::Io { operation, source } => { |
| 189 | assert_eq!(operation, "linkat_conflict"); |
| 190 | assert_eq!(source.raw_os_error(), Some(libc::EEXIST)); |
| 191 | } |
| 192 | other => panic!("unexpected: {other:?}"), |
| 193 | } |
| 194 | full_usage(usage, length, false); |
| 195 | cleanup(&closed, 4); |
| 196 | } |