| 1 | use crate::support::*; |
| 2 | use snapshot::{Error, LimitKind, Limits, MAX_BYTES, MAX_IO_BYTES, MAX_READ_CALLS}; |
| 3 | use std::{fs, os::unix::fs::MetadataExt, path::Path, time::Duration}; |
| 4 | |
| 5 | fn boundary(length: usize) { |
| 6 | let sandbox = Sandbox::new(); |
| 7 | let bytes = pattern(length); |
| 8 | let path = sandbox.file("input", &bytes); |
| 9 | let before = fs::metadata(&path).unwrap(); |
| 10 | let result = get(&path, Limits::default()).unwrap(); |
| 11 | assert_eq!(result.bytes(), bytes); |
| 12 | assert_eq!(result.identity().length(), length as u64); |
| 13 | assert_eq!(result.identity().device(), before.dev()); |
| 14 | assert_eq!(result.identity().inode(), before.ino()); |
| 15 | assert_eq!(result.identity().links(), 1); |
| 16 | assert_eq!(result.identity().mode(), before.mode()); |
| 17 | assert_eq!( |
| 18 | result.identity().modified(), |
| 19 | (before.mtime(), before.mtime_nsec()) |
| 20 | ); |
| 21 | assert_eq!( |
| 22 | result.identity().changed(), |
| 23 | (before.ctime(), before.ctime_nsec()) |
| 24 | ); |
| 25 | expect_usage(&result, length); |
| 26 | assert_eq!(result.into_bytes(), bytes); |
| 27 | assert_eq!(fs::read(&path).unwrap(), bytes); |
| 28 | sandbox.finish(); |
| 29 | } |
| 30 | |
| 31 | #[test] |
| 32 | fn empty() { |
| 33 | boundary(0); |
| 34 | } |
| 35 | |
| 36 | #[test] |
| 37 | fn one() { |
| 38 | boundary(1); |
| 39 | } |
| 40 | |
| 41 | #[test] |
| 42 | fn chunk_minus_one() { |
| 43 | boundary(65535); |
| 44 | } |
| 45 | |
| 46 | #[test] |
| 47 | fn chunk() { |
| 48 | boundary(65536); |
| 49 | } |
| 50 | |
| 51 | #[test] |
| 52 | fn chunk_plus_one() { |
| 53 | boundary(65537); |
| 54 | } |
| 55 | |
| 56 | #[test] |
| 57 | fn maximum_minus_one() { |
| 58 | boundary(16777215); |
| 59 | } |
| 60 | |
| 61 | #[test] |
| 62 | fn maximum() { |
| 63 | boundary(16777216); |
| 64 | } |
| 65 | |
| 66 | #[test] |
| 67 | fn sha256_known_abc_and_debug_redaction() { |
| 68 | let sandbox = Sandbox::new(); |
| 69 | let path = sandbox.file("private-input-marker", b"abc"); |
| 70 | let result = get(&path, Limits::default()).unwrap(); |
| 71 | assert_eq!( |
| 72 | result.sha256(), |
| 73 | [ |
| 74 | 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea, 0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, |
| 75 | 0x22, 0x23, 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c, 0xb4, 0x10, 0xff, 0x61, |
| 76 | 0xf2, 0x00, 0x15, 0xad, |
| 77 | ] |
| 78 | ); |
| 79 | let debug = format!("{result:?}"); |
| 80 | for secret in ["private-input-marker", "abc", "sha256", "identity"] { |
| 81 | assert!(!debug.contains(secret)); |
| 82 | } |
| 83 | sandbox.finish(); |
| 84 | } |
| 85 | |
| 86 | fn invalid(limits: Limits) { |
| 87 | let mut called = false; |
| 88 | let result = snapshot::capture( |
| 89 | snapshot::Request { |
| 90 | path: Path::new(""), |
| 91 | limits, |
| 92 | }, |
| 93 | |_| { |
| 94 | called = true; |
| 95 | Err(snapshot::Stop::<()>::Cancelled) |
| 96 | }, |
| 97 | ) |
| 98 | .unwrap_err(); |
| 99 | assert!(matches!(result.cause, Error::InvalidLimits)); |
| 100 | assert_eq!(result.usage, snapshot::Usage::default()); |
| 101 | assert!(!called); |
| 102 | } |
| 103 | |
| 104 | #[test] |
| 105 | fn invalid_bytes_cap() { |
| 106 | invalid(Limits { |
| 107 | max_bytes: MAX_BYTES + 1, |
| 108 | ..Limits::default() |
| 109 | }); |
| 110 | } |
| 111 | |
| 112 | #[test] |
| 113 | fn invalid_io_cap() { |
| 114 | invalid(Limits { |
| 115 | max_io_bytes: MAX_IO_BYTES + 1, |
| 116 | ..Limits::default() |
| 117 | }); |
| 118 | } |
| 119 | |
| 120 | #[test] |
| 121 | fn invalid_calls_zero() { |
| 122 | invalid(Limits { |
| 123 | max_read_calls: 0, |
| 124 | ..Limits::default() |
| 125 | }); |
| 126 | } |
| 127 | |
| 128 | #[test] |
| 129 | fn invalid_calls_cap() { |
| 130 | invalid(Limits { |
| 131 | max_read_calls: MAX_READ_CALLS + 1, |
| 132 | ..Limits::default() |
| 133 | }); |
| 134 | } |
| 135 | |
| 136 | #[test] |
| 137 | fn invalid_deadline_zero() { |
| 138 | invalid(Limits { |
| 139 | max_duration: Duration::ZERO, |
| 140 | ..Limits::default() |
| 141 | }); |
| 142 | } |
| 143 | |
| 144 | #[test] |
| 145 | fn invalid_deadline_cap() { |
| 146 | invalid(Limits { |
| 147 | max_duration: Duration::from_secs(301), |
| 148 | ..Limits::default() |
| 149 | }); |
| 150 | } |
| 151 | |
| 152 | #[test] |
| 153 | fn source_over_maximum_rejected_before_data_open() { |
| 154 | let sandbox = Sandbox::new(); |
| 155 | let path = sandbox.file("input", &pattern(MAX_BYTES as usize + 1)); |
| 156 | let failure = get(&path, Limits::default()).unwrap_err(); |
| 157 | assert!(matches!(failure.cause, Error::Limit(LimitKind::Bytes))); |
| 158 | assert_eq!(failure.usage.open_calls, 1); |
| 159 | assert_eq!(failure.usage.metadata_calls, 1); |
| 160 | assert_eq!(failure.usage.read_calls, 0); |
| 161 | sandbox.finish(); |
| 162 | } |
| 163 | |
| 164 | #[test] |
| 165 | fn empty_accepts_zero_byte_limit_with_exact_eof_budget() { |
| 166 | let sandbox = Sandbox::new(); |
| 167 | let path = sandbox.file("input", b""); |
| 168 | let result = get( |
| 169 | &path, |
| 170 | Limits { |
| 171 | max_bytes: 0, |
| 172 | max_io_bytes: 2, |
| 173 | max_read_calls: 2, |
| 174 | ..Limits::default() |
| 175 | }, |
| 176 | ) |
| 177 | .unwrap(); |
| 178 | expect_usage(&result, 0); |
| 179 | sandbox.finish(); |
| 180 | } |
| 181 | |
| 182 | #[test] |
| 183 | fn nonempty_rejects_zero_byte_limit() { |
| 184 | let sandbox = Sandbox::new(); |
| 185 | let path = sandbox.file("input", b"x"); |
| 186 | let failure = get( |
| 187 | &path, |
| 188 | Limits { |
| 189 | max_bytes: 0, |
| 190 | ..Limits::default() |
| 191 | }, |
| 192 | ) |
| 193 | .unwrap_err(); |
| 194 | assert!(matches!(failure.cause, Error::Limit(LimitKind::Bytes))); |
| 195 | assert_eq!(failure.usage.read_calls, 0); |
| 196 | sandbox.finish(); |
| 197 | } |
| 198 | |
| 199 | #[test] |
| 200 | fn zero_io_budget_is_valid_but_cannot_probe_eof() { |
| 201 | let sandbox = Sandbox::new(); |
| 202 | let path = sandbox.file("input", b""); |
| 203 | let failure = get( |
| 204 | &path, |
| 205 | Limits { |
| 206 | max_io_bytes: 0, |
| 207 | ..Limits::default() |
| 208 | }, |
| 209 | ) |
| 210 | .unwrap_err(); |
| 211 | assert!(matches!(failure.cause, Error::Limit(LimitKind::IoBytes))); |
| 212 | assert_eq!(failure.usage.read_calls, 0); |
| 213 | assert_eq!(failure.usage.reserved_read_bytes, 0); |
| 214 | sandbox.finish(); |
| 215 | } |
| 216 | |
| 217 | #[test] |
| 218 | fn exact_io_and_call_budget_includes_both_eof_probes() { |
| 219 | let sandbox = Sandbox::new(); |
| 220 | let path = sandbox.file("input", &pattern(65_537)); |
| 221 | let result = get( |
| 222 | &path, |
| 223 | Limits { |
| 224 | max_io_bytes: 131_076, |
| 225 | max_read_calls: 6, |
| 226 | ..Limits::default() |
| 227 | }, |
| 228 | ) |
| 229 | .unwrap(); |
| 230 | expect_usage(&result, 65_537); |
| 231 | sandbox.finish(); |
| 232 | } |
| 233 | |
| 234 | #[test] |
| 235 | fn one_less_io_budget_fails_last_eof_without_refund() { |
| 236 | let sandbox = Sandbox::new(); |
| 237 | let path = sandbox.file("input", &pattern(65_537)); |
| 238 | let failure = get( |
| 239 | &path, |
| 240 | Limits { |
| 241 | max_io_bytes: 131_075, |
| 242 | ..Limits::default() |
| 243 | }, |
| 244 | ) |
| 245 | .unwrap_err(); |
| 246 | assert!(matches!(failure.cause, Error::Limit(LimitKind::IoBytes))); |
| 247 | assert_eq!(failure.usage.read_calls, 5); |
| 248 | assert_eq!(failure.usage.reserved_read_bytes, 131_075); |
| 249 | assert_eq!(failure.usage.actual_read_bytes, 131_074); |
| 250 | assert_eq!(failure.usage.compared_bytes, 65_537); |
| 251 | sandbox.finish(); |
| 252 | } |
| 253 | |
| 254 | #[test] |
| 255 | fn one_less_call_budget_fails_last_eof() { |
| 256 | let sandbox = Sandbox::new(); |
| 257 | let path = sandbox.file("input", &pattern(65_537)); |
| 258 | let failure = get( |
| 259 | &path, |
| 260 | Limits { |
| 261 | max_read_calls: 5, |
| 262 | ..Limits::default() |
| 263 | }, |
| 264 | ) |
| 265 | .unwrap_err(); |
| 266 | assert!(matches!(failure.cause, Error::Limit(LimitKind::ReadCalls))); |
| 267 | assert_eq!(failure.usage.read_calls, 5); |
| 268 | assert_eq!(failure.usage.reserved_read_bytes, 131_075); |
| 269 | sandbox.finish(); |
| 270 | } |
| 271 | |
| 272 | #[test] |
| 273 | fn retry_usage_is_per_attempt_not_hidden_cumulative_state() { |
| 274 | let sandbox = Sandbox::new(); |
| 275 | let path = sandbox.file("input", b"x"); |
| 276 | let failure = get( |
| 277 | &path, |
| 278 | Limits { |
| 279 | max_read_calls: 1, |
| 280 | ..Limits::default() |
| 281 | }, |
| 282 | ) |
| 283 | .unwrap_err(); |
| 284 | assert!(matches!(failure.cause, Error::Limit(LimitKind::ReadCalls))); |
| 285 | assert_eq!(failure.usage.actual_read_bytes, 1); |
| 286 | let result = get(&path, Limits::default()).unwrap(); |
| 287 | expect_usage(&result, 1); |
| 288 | sandbox.finish(); |
| 289 | } |