| 1 | use crate::support::*; |
| 2 | use publication::{Error, Limits, Publication, Request, Stop, Usage}; |
| 3 | use std::{ |
| 4 | ffi::{CString, OsStr, OsString}, |
| 5 | fs, |
| 6 | os::unix::{ |
| 7 | ffi::{OsStrExt, OsStringExt}, |
| 8 | fs::{MetadataExt, symlink}, |
| 9 | net::UnixListener, |
| 10 | }, |
| 11 | path::{Path, PathBuf}, |
| 12 | }; |
| 13 | |
| 14 | fn invalid(parent: &Path, leaf: &OsStr) { |
| 15 | let (task, _) = Publication::new(); |
| 16 | let (error, usage, closed) = not_committed(task.publish( |
| 17 | Request { |
| 18 | parent, |
| 19 | leaf, |
| 20 | bytes: b"abc", |
| 21 | limits: Limits::default(), |
| 22 | }, |
| 23 | |_| -> Result<(), Stop<()>> { panic!("invalid path callback") }, |
| 24 | )); |
| 25 | assert!(matches!(error, Error::InvalidPath)); |
| 26 | assert_eq!(usage, Usage::default()); |
| 27 | cleanup(&closed, 0); |
| 28 | } |
| 29 | |
| 30 | #[test] |
| 31 | fn invalid_parent_forms_rejected_before_callback() { |
| 32 | for value in [ |
| 33 | "", |
| 34 | "relative", |
| 35 | "/owned/./x", |
| 36 | "/owned/../x", |
| 37 | "/owned//x", |
| 38 | "/owned/", |
| 39 | "/owned/\0x", |
| 40 | ] { |
| 41 | invalid(Path::new(value), OsStr::new("output")); |
| 42 | } |
| 43 | invalid( |
| 44 | &PathBuf::from(format!("/{}", "x".repeat(4095))), |
| 45 | OsStr::new("output"), |
| 46 | ); |
| 47 | } |
| 48 | |
| 49 | #[test] |
| 50 | fn invalid_leaf_forms_rejected_before_callback() { |
| 51 | for value in ["", ".", "..", "a/b", "/absolute", "nul\0byte"] { |
| 52 | invalid(Path::new("/owned"), OsStr::new(value)); |
| 53 | } |
| 54 | invalid(Path::new("/owned"), OsStr::new(&"x".repeat(256))); |
| 55 | } |
| 56 | |
| 57 | #[test] |
| 58 | fn root_parent_is_lexically_valid_without_opening_root() { |
| 59 | let (task, _) = Publication::new(); |
| 60 | let (error, usage, closed) = not_committed(task.publish( |
| 61 | Request { |
| 62 | parent: Path::new("/"), |
| 63 | leaf: OsStr::new("unused"), |
| 64 | bytes: &[], |
| 65 | limits: Limits::default(), |
| 66 | }, |
| 67 | |_| Err(Stop::<()>::Cancelled), |
| 68 | )); |
| 69 | assert!(matches!(error, Error::Cancelled)); |
| 70 | assert_eq!(usage, Usage::default()); |
| 71 | cleanup(&closed, 0); |
| 72 | } |
| 73 | |
| 74 | fn open_error(parent: &Path, expected: i32) { |
| 75 | let (error, usage, closed) = not_committed(publish( |
| 76 | parent, |
| 77 | OsStr::new("output"), |
| 78 | b"abc", |
| 79 | Limits::default(), |
| 80 | )); |
| 81 | match error { |
| 82 | Error::Io { operation, source } => { |
| 83 | assert_eq!(operation, "openat2"); |
| 84 | assert_eq!(source.raw_os_error(), Some(expected)); |
| 85 | } |
| 86 | other => panic!("unexpected: {other:?}"), |
| 87 | } |
| 88 | assert_eq!(usage.open_calls, 1); |
| 89 | assert_eq!( |
| 90 | usage.metadata_calls + usage.link_calls + usage.write_calls + usage.read_calls, |
| 91 | 0 |
| 92 | ); |
| 93 | cleanup(&closed, 0); |
| 94 | } |
| 95 | |
| 96 | #[test] |
| 97 | fn missing_and_regular_parent_preserve_errno() { |
| 98 | let sandbox = Sandbox::new(); |
| 99 | open_error(&sandbox.path("missing"), libc::ENOENT); |
| 100 | let regular = sandbox.file("regular", b"untouched"); |
| 101 | open_error(®ular, libc::ENOTDIR); |
| 102 | assert_eq!(fs::read(regular).unwrap(), b"untouched"); |
| 103 | assert!(names(&sandbox.parent()).is_empty()); |
| 104 | sandbox.finish(); |
| 105 | } |
| 106 | |
| 107 | #[test] |
| 108 | fn parent_and_ancestor_symlinks_rejected() { |
| 109 | let sandbox = Sandbox::new(); |
| 110 | let link = sandbox.path("alias"); |
| 111 | symlink(sandbox.parent(), &link).unwrap(); |
| 112 | open_error(&link, libc::ELOOP); |
| 113 | let ancestor = sandbox.path("ancestor"); |
| 114 | symlink(&sandbox.root, &ancestor).unwrap(); |
| 115 | open_error(&ancestor.join("parent"), libc::ELOOP); |
| 116 | assert!(names(&sandbox.parent()).is_empty()); |
| 117 | sandbox.finish(); |
| 118 | } |
| 119 | |
| 120 | #[test] |
| 121 | fn non_utf8_leaf_and_parent_are_supported() { |
| 122 | let sandbox = Sandbox::new(); |
| 123 | let parent_name = OsString::from_vec(vec![b'p', 0xfe]); |
| 124 | let parent = sandbox.dir(Path::new(&parent_name)); |
| 125 | let leaf = OsString::from_vec(vec![b'o', 0xff]); |
| 126 | committed( |
| 127 | publish(&parent, &leaf, b"abc", Limits::default()), |
| 128 | &parent, |
| 129 | &leaf, |
| 130 | b"abc", |
| 131 | ); |
| 132 | sandbox.finish(); |
| 133 | } |
| 134 | |
| 135 | #[test] |
| 136 | fn maximum_leaf_length_is_supported() { |
| 137 | let sandbox = Sandbox::new(); |
| 138 | let leaf = OsString::from("x".repeat(255)); |
| 139 | let parent = sandbox.parent(); |
| 140 | committed( |
| 141 | publish(&parent, &leaf, b"abc", Limits::default()), |
| 142 | &parent, |
| 143 | &leaf, |
| 144 | b"abc", |
| 145 | ); |
| 146 | sandbox.finish(); |
| 147 | } |
| 148 | |
| 149 | #[test] |
| 150 | fn linux_backslash_and_colon_are_leaf_bytes() { |
| 151 | let sandbox = Sandbox::new(); |
| 152 | let parent = sandbox.parent(); |
| 153 | let leaf = OsStr::new("a\\b:c"); |
| 154 | committed( |
| 155 | publish(&parent, leaf, b"abc", Limits::default()), |
| 156 | &parent, |
| 157 | leaf, |
| 158 | b"abc", |
| 159 | ); |
| 160 | sandbox.finish(); |
| 161 | } |
| 162 | |
| 163 | #[derive(Clone, Copy)] |
| 164 | enum Existing { |
| 165 | Regular, |
| 166 | Directory, |
| 167 | Symlink, |
| 168 | Dangling, |
| 169 | HardLink, |
| 170 | Fifo, |
| 171 | Socket, |
| 172 | } |
| 173 | |
| 174 | fn existing(kind: Existing) { |
| 175 | let sandbox = Sandbox::new(); |
| 176 | let parent = sandbox.parent(); |
| 177 | let leaf = OsStr::new("output"); |
| 178 | let output = parent.join(leaf); |
| 179 | let sentinel = sandbox.file("sentinel", b"untouched"); |
| 180 | let mut listener = None; |
| 181 | match kind { |
| 182 | Existing::Regular => { |
| 183 | sandbox.file("parent/output", b"existing"); |
| 184 | } |
| 185 | Existing::Directory => { |
| 186 | sandbox.dir("parent/output"); |
| 187 | sandbox.file("parent/output/child", b"child"); |
| 188 | } |
| 189 | Existing::Symlink => symlink(&sentinel, &output).unwrap(), |
| 190 | Existing::Dangling => symlink(sandbox.path("missing"), &output).unwrap(), |
| 191 | Existing::HardLink => fs::hard_link(&sentinel, &output).unwrap(), |
| 192 | Existing::Fifo => { |
| 193 | let raw = CString::new(output.as_os_str().as_bytes()).unwrap(); |
| 194 | // 작업 전용 이름의 합성 FIFO만 생성함. |
| 195 | assert_eq!(unsafe { libc::mkfifo(raw.as_ptr(), 0o600) }, 0); |
| 196 | } |
| 197 | Existing::Socket => { |
| 198 | listener = Some(UnixListener::bind(&output).unwrap()); |
| 199 | } |
| 200 | } |
| 201 | let before = fs::symlink_metadata(&output).unwrap(); |
| 202 | conflict( |
| 203 | publish(&parent, leaf, b"replacement", Limits::default()), |
| 204 | 11, |
| 205 | ); |
| 206 | let after = fs::symlink_metadata(&output).unwrap(); |
| 207 | assert_eq!( |
| 208 | ( |
| 209 | before.dev(), |
| 210 | before.ino(), |
| 211 | before.mode(), |
| 212 | before.nlink(), |
| 213 | before.len() |
| 214 | ), |
| 215 | ( |
| 216 | after.dev(), |
| 217 | after.ino(), |
| 218 | after.mode(), |
| 219 | after.nlink(), |
| 220 | after.len() |
| 221 | ) |
| 222 | ); |
| 223 | assert_eq!(fs::read(&sentinel).unwrap(), b"untouched"); |
| 224 | match kind { |
| 225 | Existing::Regular => assert_eq!(fs::read(&output).unwrap(), b"existing"), |
| 226 | Existing::Directory => { |
| 227 | assert_eq!(names(&output), vec![OsString::from("child")]); |
| 228 | assert_eq!(fs::read(output.join("child")).unwrap(), b"child"); |
| 229 | } |
| 230 | Existing::Symlink => assert_eq!(fs::read_link(&output).unwrap(), sentinel), |
| 231 | Existing::Dangling => assert_eq!(fs::read_link(&output).unwrap(), sandbox.path("missing")), |
| 232 | Existing::HardLink => assert_eq!(fs::read(&output).unwrap(), b"untouched"), |
| 233 | Existing::Fifo | Existing::Socket => {} |
| 234 | } |
| 235 | assert_eq!(names(&parent), vec![leaf.to_os_string()]); |
| 236 | drop(listener); |
| 237 | sandbox.finish(); |
| 238 | } |
| 239 | |
| 240 | #[test] |
| 241 | fn existing_regular_file_preserved() { |
| 242 | existing(Existing::Regular); |
| 243 | } |
| 244 | #[test] |
| 245 | fn existing_directory_and_child_preserved() { |
| 246 | existing(Existing::Directory); |
| 247 | } |
| 248 | #[test] |
| 249 | fn existing_symlink_and_target_preserved() { |
| 250 | existing(Existing::Symlink); |
| 251 | } |
| 252 | #[test] |
| 253 | fn dangling_symlink_preserved() { |
| 254 | existing(Existing::Dangling); |
| 255 | } |
| 256 | #[test] |
| 257 | fn existing_hard_links_preserved() { |
| 258 | existing(Existing::HardLink); |
| 259 | } |
| 260 | #[test] |
| 261 | fn existing_fifo_preserved_without_opening_it() { |
| 262 | existing(Existing::Fifo); |
| 263 | } |
| 264 | #[test] |
| 265 | fn existing_socket_preserved_without_accepting_connections() { |
| 266 | existing(Existing::Socket); |
| 267 | } |