| 1 | use crate::support::*; |
| 2 | use snapshot::{Error, IoOperation, Limits, Unsupported, Usage}; |
| 3 | use std::{ |
| 4 | ffi::{CString, OsString}, |
| 5 | fs, |
| 6 | os::unix::{ |
| 7 | ffi::{OsStrExt, OsStringExt}, |
| 8 | fs::symlink, |
| 9 | net::UnixListener, |
| 10 | }, |
| 11 | path::{Path, PathBuf}, |
| 12 | }; |
| 13 | |
| 14 | fn invalid(path: &Path) { |
| 15 | let failure = get(path, Limits::default()).unwrap_err(); |
| 16 | assert!(matches!(failure.cause, Error::InvalidPath)); |
| 17 | assert_eq!(failure.usage, Usage::default()); |
| 18 | } |
| 19 | |
| 20 | #[test] |
| 21 | fn empty_path() { |
| 22 | invalid(Path::new("")); |
| 23 | } |
| 24 | |
| 25 | #[test] |
| 26 | fn relative_path() { |
| 27 | invalid(Path::new("relative")); |
| 28 | } |
| 29 | |
| 30 | #[test] |
| 31 | fn root_only() { |
| 32 | invalid(Path::new("/")); |
| 33 | } |
| 34 | |
| 35 | #[test] |
| 36 | fn dot_component() { |
| 37 | invalid(Path::new("/owned/./input")); |
| 38 | } |
| 39 | |
| 40 | #[test] |
| 41 | fn parent_component() { |
| 42 | invalid(Path::new("/owned/../input")); |
| 43 | } |
| 44 | |
| 45 | #[test] |
| 46 | fn repeated_slash() { |
| 47 | invalid(Path::new("/owned//input")); |
| 48 | } |
| 49 | |
| 50 | #[test] |
| 51 | fn trailing_slash() { |
| 52 | invalid(Path::new("/owned/input/")); |
| 53 | } |
| 54 | |
| 55 | #[test] |
| 56 | fn nul_byte() { |
| 57 | invalid(Path::new("/owned/in\0put")); |
| 58 | } |
| 59 | |
| 60 | #[test] |
| 61 | fn component_over_255() { |
| 62 | invalid(&PathBuf::from(format!("/{}", "x".repeat(256)))); |
| 63 | } |
| 64 | |
| 65 | #[test] |
| 66 | fn path_over_4096() { |
| 67 | invalid(&PathBuf::from(format!("/{}", "x/".repeat(2049)))); |
| 68 | } |
| 69 | |
| 70 | fn pin_error(path: &Path, code: i32) { |
| 71 | let failure = get(path, Limits::default()).unwrap_err(); |
| 72 | match failure.cause { |
| 73 | Error::Io { operation, source } => { |
| 74 | assert_eq!(operation, IoOperation::PinPath); |
| 75 | assert_eq!(source.raw_os_error(), Some(code)); |
| 76 | } |
| 77 | other => panic!("unexpected: {other:?}"), |
| 78 | } |
| 79 | assert_eq!(failure.usage.open_calls, 1); |
| 80 | assert_eq!(failure.usage.metadata_calls, 0); |
| 81 | assert_eq!(failure.usage.read_calls, 0); |
| 82 | } |
| 83 | |
| 84 | #[test] |
| 85 | fn missing_path_preserves_enoent_and_stage() { |
| 86 | let sandbox = Sandbox::new(); |
| 87 | pin_error(&sandbox.path("missing"), libc::ENOENT); |
| 88 | sandbox.finish(); |
| 89 | } |
| 90 | |
| 91 | #[test] |
| 92 | fn regular_parent_component_preserves_enotdir() { |
| 93 | let sandbox = Sandbox::new(); |
| 94 | let file = sandbox.file("parent", b"unchanged"); |
| 95 | pin_error(&file.join("input"), libc::ENOTDIR); |
| 96 | assert_eq!(fs::read(file).unwrap(), b"unchanged"); |
| 97 | sandbox.finish(); |
| 98 | } |
| 99 | |
| 100 | #[test] |
| 101 | fn leaf_symlink_rejected_without_reading_target() { |
| 102 | let sandbox = Sandbox::new(); |
| 103 | let target = sandbox.file("target", b"unchanged"); |
| 104 | let path = sandbox.path("link"); |
| 105 | symlink(&target, &path).unwrap(); |
| 106 | pin_error(&path, libc::ELOOP); |
| 107 | assert_eq!(fs::read(target).unwrap(), b"unchanged"); |
| 108 | sandbox.finish(); |
| 109 | } |
| 110 | |
| 111 | #[test] |
| 112 | fn ancestor_symlink_rejected() { |
| 113 | let sandbox = Sandbox::new(); |
| 114 | let parent = sandbox.dir("parent"); |
| 115 | sandbox.file("parent/input", b"unchanged"); |
| 116 | let link = sandbox.path("alias"); |
| 117 | symlink(parent, &link).unwrap(); |
| 118 | pin_error(&link.join("input"), libc::ELOOP); |
| 119 | sandbox.finish(); |
| 120 | } |
| 121 | |
| 122 | #[test] |
| 123 | fn dangling_symlink_preserves_eloop() { |
| 124 | let sandbox = Sandbox::new(); |
| 125 | let path = sandbox.path("link"); |
| 126 | symlink(sandbox.path("missing"), &path).unwrap(); |
| 127 | pin_error(&path, libc::ELOOP); |
| 128 | sandbox.finish(); |
| 129 | } |
| 130 | |
| 131 | fn unsupported(path: &Path, expected: Unsupported) { |
| 132 | let failure = get(path, Limits::default()).unwrap_err(); |
| 133 | assert!(matches!(failure.cause, Error::Unsupported(value) if value == expected)); |
| 134 | assert_eq!(failure.usage.open_calls, 1); |
| 135 | assert_eq!(failure.usage.metadata_calls, 1); |
| 136 | assert_eq!(failure.usage.read_calls, 0); |
| 137 | } |
| 138 | |
| 139 | #[test] |
| 140 | fn directory_rejected_before_data_open() { |
| 141 | let sandbox = Sandbox::new(); |
| 142 | unsupported(&sandbox.dir("directory"), Unsupported::FileType); |
| 143 | sandbox.finish(); |
| 144 | } |
| 145 | |
| 146 | #[test] |
| 147 | fn fifo_without_writer_rejected_before_data_open() { |
| 148 | let sandbox = Sandbox::new(); |
| 149 | let path = sandbox.path("fifo"); |
| 150 | let raw = CString::new(path.as_os_str().as_bytes()).unwrap(); |
| 151 | // 작업 전용 경로에 합성 FIFO만 생성함. |
| 152 | assert_eq!(unsafe { libc::mkfifo(raw.as_ptr(), 0o600) }, 0); |
| 153 | unsupported(&path, Unsupported::FileType); |
| 154 | sandbox.finish(); |
| 155 | } |
| 156 | |
| 157 | #[test] |
| 158 | fn unix_socket_rejected_before_data_open() { |
| 159 | let sandbox = Sandbox::new(); |
| 160 | let path = sandbox.path("socket"); |
| 161 | let socket = UnixListener::bind(&path).unwrap(); |
| 162 | unsupported(&path, Unsupported::FileType); |
| 163 | drop(socket); |
| 164 | sandbox.finish(); |
| 165 | } |
| 166 | |
| 167 | #[test] |
| 168 | fn multiple_hard_links_rejected_and_contents_preserved() { |
| 169 | let sandbox = Sandbox::new(); |
| 170 | let path = sandbox.file("input", b"unchanged"); |
| 171 | let link = sandbox.path("hardlink"); |
| 172 | fs::hard_link(&path, &link).unwrap(); |
| 173 | unsupported(&path, Unsupported::MultipleLinks); |
| 174 | assert_eq!(fs::read(path).unwrap(), b"unchanged"); |
| 175 | assert_eq!(fs::read(link).unwrap(), b"unchanged"); |
| 176 | sandbox.finish(); |
| 177 | } |
| 178 | |
| 179 | #[test] |
| 180 | fn non_utf8_linux_name_is_supported() { |
| 181 | let sandbox = Sandbox::new(); |
| 182 | let path = sandbox.root.join(OsString::from_vec(vec![b'i', 0xff])); |
| 183 | fs::write(&path, b"abc").unwrap(); |
| 184 | assert_eq!(get(&path, Limits::default()).unwrap().bytes(), b"abc"); |
| 185 | sandbox.finish(); |
| 186 | } |
| 187 | |
| 188 | #[test] |
| 189 | fn linux_backslash_and_colon_are_name_bytes() { |
| 190 | let sandbox = Sandbox::new(); |
| 191 | let path = sandbox.file("a\\b:c", b"abc"); |
| 192 | assert_eq!(get(&path, Limits::default()).unwrap().bytes(), b"abc"); |
| 193 | sandbox.finish(); |
| 194 | } |
| 195 | |
| 196 | #[test] |
| 197 | fn component_255_bytes_is_supported() { |
| 198 | let sandbox = Sandbox::new(); |
| 199 | let path = sandbox.file(&"x".repeat(255), b"abc"); |
| 200 | assert_eq!(get(&path, Limits::default()).unwrap().bytes(), b"abc"); |
| 201 | sandbox.finish(); |
| 202 | } |