Debian Codegraph @

tests/by-util/test_shred.rs

raw
1 // This file is part of the uutils coreutils package.
2 //
3 // For the full copyright and license information, please view the LICENSE
4 // file that was distributed with this source code.
5
6 // spell-checker:ignore wipesync
7
8 use uutests::at_and_ucmd;
9 use uutests::new_ucmd;
10 use uutests::util::TestScenario;
11 use uutests::util_name;
12
13 const PATTERNS: [&str; 22] = [
14 "000000", "ffffff", "555555", "aaaaaa", "249249", "492492", "6db6db", "924924", "b6db6d",
15 "db6db6", "111111", "222222", "333333", "444444", "666666", "777777", "888888", "999999",
16 "bbbbbb", "cccccc", "dddddd", "eeeeee",
17 ];
18
19 #[test]
20 fn test_invalid_arg() {
21 new_ucmd!().arg("--definitely-invalid").fails_with_code(1);
22 }
23
24 #[test]
25 fn test_invalid_remove_arg() {
26 new_ucmd!().arg("--remove=unknown").fails_with_code(1);
27 }
28
29 #[test]
30 fn test_ambiguous_remove_arg() {
31 new_ucmd!().arg("--remove=wip").fails_with_code(1);
32 }
33
34 #[test]
35 fn test_shred() {
36 let (at, mut ucmd) = at_and_ucmd!();
37
38 let file = "test_shred";
39 let file_original_content = "test_shred file content";
40
41 at.write(file, file_original_content);
42
43 ucmd.arg(file).succeeds();
44
45 // File exists
46 assert!(at.file_exists(file));
47 // File is obfuscated
48 assert_ne!(at.read_bytes(file), file_original_content.as_bytes());
49 }
50
51 #[test]
52 fn test_shred_remove() {
53 let (at, mut ucmd) = at_and_ucmd!();
54
55 let file = "test_shred_remove";
56 at.touch(file);
57
58 ucmd.arg("--remove").arg(file).succeeds();
59
60 // File was deleted
61 assert!(!at.file_exists(file));
62 }
63
64 #[test]
65 fn test_shred_remove_unlink() {
66 // spell-checker:disable-next-line
67 for argument in ["--remove=unlink", "--remove=unlin", "--remove=u"] {
68 let (at, mut ucmd) = at_and_ucmd!();
69 let file = "test_shred_remove_unlink";
70 at.touch(file);
71 ucmd.arg(argument).arg(file).succeeds();
72 // File was deleted
73 assert!(!at.file_exists(file));
74 }
75 }
76
77 #[test]
78 fn test_shred_remove_unlink_relative_path() {
79 let (at, mut ucmd) = at_and_ucmd!();
80
81 at.mkdir_all("dir1/dir2");
82 at.write("dir1/dir2/file1", "test data");
83
84 ucmd.arg("--remove=unlink")
85 .arg("dir1/dir2/file1")
86 .succeeds();
87
88 assert!(!at.file_exists("dir1/dir2/file1"));
89 }
90
91 #[test]
92 fn test_shred_remove_wipe() {
93 let (at, mut ucmd) = at_and_ucmd!();
94
95 let file = "test_shred_remove_wipe";
96 at.touch(file);
97
98 ucmd.arg("--remove=wipe").arg(file).succeeds();
99
100 // File was deleted
101 assert!(!at.file_exists(file));
102 }
103
104 #[test]
105 fn test_shred_remove_wipesync() {
106 // spell-checker:disable-next-line
107 for argument in ["--remove=wipesync", "--remove=wipesyn", "--remove=wipes"] {
108 let (at, mut ucmd) = at_and_ucmd!();
109 let file = "test_shred_remove_wipesync";
110 at.touch(file);
111 ucmd.arg(argument).arg(file).succeeds();
112 // File was deleted
113 assert!(!at.file_exists(file));
114 }
115 }
116
117 #[test]
118 fn test_shred_u() {
119 let (at, mut ucmd) = at_and_ucmd!();
120
121 let file_a = "test_shred_remove_a";
122 let file_b = "test_shred_remove_b";
123
124 // Create file_a and file_b.
125 at.touch(file_a);
126 at.touch(file_b);
127
128 // Shred file_a.
129 ucmd.arg("-u").arg(file_a).succeeds();
130
131 // file_a was deleted, file_b exists.
132 assert!(!at.file_exists(file_a));
133 assert!(at.file_exists(file_b));
134 }
135
136 #[test]
137 fn test_shred_force() {
138 let scene = TestScenario::new(util_name!());
139 let at = &scene.fixtures;
140
141 let file = "test_shred_force";
142
143 // Create file_a.
144 at.touch(file);
145 assert!(at.file_exists(file));
146
147 // Make file_a readonly.
148 at.set_readonly(file);
149
150 // Try shred -u.
151 scene.ucmd().arg("-u").arg(file).fails();
152
153 // file_a was not deleted because it is readonly.
154 assert!(at.file_exists(file));
155
156 // Try shred -u -f.
157 scene.ucmd().arg("-u").arg("-f").arg(file).succeeds();
158
159 // file_a was deleted.
160 assert!(!at.file_exists(file));
161 }
162
163 #[test]
164 fn test_hex() {
165 let (at, mut ucmd) = at_and_ucmd!();
166
167 let file = "test_hex";
168
169 at.touch(file);
170
171 ucmd.arg("--size=0x10").arg(file).succeeds();
172 }
173
174 #[test]
175 fn test_shred_empty() {
176 let scene = TestScenario::new(util_name!());
177 let at = &scene.fixtures;
178
179 let file_a = "test_shred_remove_a";
180
181 at.touch(file_a);
182
183 // Shred file_a and verify that, as it is empty, it doesn't have "pass 1/3 (random)"
184 scene
185 .ucmd()
186 .arg("-uv")
187 .arg(file_a)
188 .succeeds()
189 .stderr_does_not_contain("1/3 (random)");
190
191 assert!(!at.file_exists(file_a));
192
193 // if the file isn't empty, we should have random
194 at.touch(file_a);
195 at.write(file_a, "1");
196 scene
197 .ucmd()
198 .arg("-uv")
199 .arg(file_a)
200 .succeeds()
201 .stderr_contains("1/3 (random)");
202
203 assert!(!at.file_exists(file_a));
204 }
205
206 #[test]
207 #[cfg(all(unix, feature = "chmod"))]
208 fn test_shred_fail_no_perm() {
209 use std::path::Path;
210
211 let scene = TestScenario::new(util_name!());
212 let at = &scene.fixtures;
213 let dir = "dir";
214
215 let file = "test_shred_remove_a";
216
217 let binding = Path::new("dir").join(file);
218 let path = binding.to_str().unwrap();
219 at.mkdir(dir);
220 at.touch(path);
221 scene.ccmd("chmod").arg("a-w").arg(dir).succeeds();
222
223 scene
224 .ucmd()
225 .arg("-uv")
226 .arg(path)
227 .fails()
228 .stderr_contains("Couldn't rename to");
229 }
230
231 #[test]
232 fn test_shred_verbose_no_padding_1() {
233 let (at, mut ucmd) = at_and_ucmd!();
234 let file = "foo";
235 at.write(file, "non-empty");
236 ucmd.arg("-vn1")
237 .arg(file)
238 .succeeds()
239 .stderr_only("shred: foo: pass 1/1 (random)...\n");
240 }
241
242 #[test]
243 fn test_shred_verbose_no_padding_10() {
244 let (at, mut ucmd) = at_and_ucmd!();
245 let file = "foo";
246 at.write(file, "non-empty");
247 ucmd.arg("-vn10")
248 .arg(file)
249 .succeeds()
250 .stderr_contains("shred: foo: pass 1/10 (random)...\n");
251 }
252
253 #[test]
254 fn test_all_patterns_present() {
255 let (at, mut ucmd) = at_and_ucmd!();
256
257 let file = "foo.txt";
258 at.write(file, "bar");
259
260 let result = ucmd.arg("-vn25").arg(file).succeeds();
261
262 for pat in PATTERNS {
263 result.stderr_contains(pat);
264 }
265 }
266
267 #[test]
268 fn test_random_source_regular_file() {
269 let (at, mut ucmd) = at_and_ucmd!();
270
271 // Currently, our block size is 4096. If it changes, this test has to be adapted.
272 let mut many_bytes = Vec::with_capacity(4096 * 4);
273
274 for i in 0..4096u32 {
275 many_bytes.extend(i.to_le_bytes());
276 }
277
278 assert_eq!(many_bytes.len(), 4096 * 4);
279 at.write_bytes("source_long", &many_bytes);
280
281 let file = "foo.txt";
282 at.write(file, "a");
283
284 ucmd
285 .arg("-vn3")
286 .arg("--random-source=source_long")
287 .arg(file)
288 .succeeds()
289 .stderr_only("shred: foo.txt: pass 1/3 (random)...\nshred: foo.txt: pass 2/3 (random)...\nshred: foo.txt: pass 3/3 (random)...\n");
290
291 // Should rewrite the file exactly three times
292 assert_eq!(at.read_bytes(file), many_bytes[(4096 * 2)..(4096 * 3)]);
293 }
294
295 #[test]
296 fn test_random_source_open_error_includes_cause() {
297 let (at, mut ucmd) = at_and_ucmd!();
298
299 at.touch("target");
300
301 ucmd.arg("--random-source=missing")
302 .arg("target")
303 .fails()
304 .stderr_only("shred: missing: No such file or directory\n");
305 }
306
307 #[test]
308 fn test_random_source_dir() {
309 let (at, mut ucmd) = at_and_ucmd!();
310
311 at.mkdir("source");
312 let file = "foo.txt";
313 at.write(file, "a");
314
315 // The test verifies that shred stops immediately on error instead of continuing
316 // Platform differences:
317 // - Unix: Error during write ("File write pass failed: Is a directory")
318 // - Windows: Error during open ("cannot open random source")
319 // Both are correct - key is NOT seeing "pass 2/3" (which proves it stopped)
320 ucmd.arg("-v")
321 .arg("--random-source=source")
322 .arg(file)
323 .fails()
324 .stderr_does_not_contain("pass 2/3")
325 .stderr_does_not_contain("pass 3/3");
326 }
327
328 #[test]
329 fn test_shred_rename_exhaustion() {
330 // GNU: tests/shred/shred-remove.sh
331 let scene = TestScenario::new(util_name!());
332 let at = &scene.fixtures;
333
334 at.touch("test");
335 at.touch("000");
336
337 scene
338 .ucmd()
339 .arg("-vu")
340 .arg("test")
341 .succeeds()
342 .stderr_contains("renamed to 0000")
343 .stderr_contains("renamed to 001")
344 .stderr_contains("renamed to 00")
345 .stderr_contains("removed");
346
347 assert!(!at.file_exists("test"));
348 }
349
350 #[test]
351 #[cfg(target_os = "linux")]
352 fn test_shred_non_utf8_paths() {
353 use std::os::unix::ffi::OsStrExt;
354 let ts = TestScenario::new(util_name!());
355 let at = &ts.fixtures;
356
357 let file_name = std::ffi::OsStr::from_bytes(b"test_\xFF\xFE.txt");
358 std::fs::write(at.plus(file_name), "test content").unwrap();
359
360 // Test that shred can handle non-UTF-8 filenames
361 ts.ucmd().arg(file_name).succeeds();
362 }
363
364 #[test]
365 fn test_shred_twenty_passes_with_known_random_source() {
366 let (at, mut ucmd) = at_and_ucmd!();
367
368 let us_data = vec![0x55; 102_400]; // 100K of 'U' bytes
369 at.write_bytes("Us", &us_data);
370
371 let file = "f";
372 at.write(file, "1"); // Single byte file
373
374 // Run with a deterministic random source so the pass sequence is reproducible
375 let result = ucmd
376 .arg("-v")
377 .arg("-u")
378 .arg("-n20")
379 .arg("-s4096")
380 .arg("--random-source=Us")
381 .arg(file)
382 .succeeds();
383
384 let expected_passes = [
385 "pass 1/20 (random)",
386 "pass 2/20 (ffffff)",
387 "pass 3/20 (924924)",
388 "pass 4/20 (888888)",
389 "pass 5/20 (db6db6)",
390 "pass 6/20 (777777)",
391 "pass 7/20 (492492)",
392 "pass 8/20 (bbbbbb)",
393 "pass 9/20 (555555)",
394 "pass 10/20 (aaaaaa)",
395 "pass 11/20 (random)",
396 "pass 12/20 (6db6db)",
397 "pass 13/20 (249249)",
398 "pass 14/20 (999999)",
399 "pass 15/20 (111111)",
400 "pass 16/20 (000000)",
401 "pass 17/20 (b6db6d)",
402 "pass 18/20 (eeeeee)",
403 "pass 19/20 (333333)",
404 "pass 20/20 (random)",
405 ];
406
407 for pass in expected_passes {
408 result.stderr_contains(pass);
409 }
410
411 // Also verify removal messages
412 result.stderr_contains("removing");
413 result.stderr_contains("renamed to 0");
414 result.stderr_contains("removed");
415
416 // File should be deleted
417 assert!(!at.file_exists(file));
418 }
419
420 #[test]
421 fn test_shred_nineteen_passes_first_and_last_are_random() {
422 let (at, mut ucmd) = at_and_ucmd!();
423
424 let us_data = vec![0x55; 102_400];
425 at.write_bytes("Us", &us_data);
426
427 let file = "f";
428 at.write(file, "1");
429
430 // Test with 19 passes to verify it works for different counts
431 let result = ucmd
432 .arg("-v")
433 .arg("-n19")
434 .arg("--random-source=Us")
435 .arg(file)
436 .succeeds();
437
438 // Should have exactly 19 passes
439 for i in 1..=19 {
440 result.stderr_contains(format!("pass {i}/19"));
441 }
442
443 // First and last should be random
444 result.stderr_contains("pass 1/19 (random)");
445 result.stderr_contains("pass 19/19 (random)");
446 }
447
448 #[test]
449 fn test_shred_trailing_slash_on_file() {
450 let scene = TestScenario::new(util_name!());
451 let at = &scene.fixtures;
452 at.touch("a");
453 scene
454 .ucmd()
455 .arg("a/")
456 .fails()
457 .stderr_contains("Not a directory");
458 }
459
460 #[test]
461 fn test_shred_trailing_slash_on_dir() {
462 let scene = TestScenario::new(util_name!());
463 let at = &scene.fixtures;
464 at.mkdir("d");
465 scene
466 .ucmd()
467 .arg("d/")
468 .fails()
469 .stderr_contains("Is a directory");
470 }
471
472 #[test]
473 #[cfg(unix)]
474 fn test_shred_inaccessible_file_reports_real_error() {
475 // A file whose parent directory lacks search permission must not be
476 // misreported as "No such file or directory": `Path::exists()` and
477 // `Path::is_file()` collapse the permission error into `false`.
478 use std::fs::{Permissions, set_permissions};
479 use std::os::unix::fs::PermissionsExt;
480
481 let scene = TestScenario::new(util_name!());
482 let at = &scene.fixtures;
483
484 at.mkdir("locked");
485 at.touch("locked/file");
486 set_permissions(at.plus_as_string("locked"), Permissions::from_mode(0o000)).unwrap();
487
488 let result = scene.ucmd().arg("locked/file").fails();
489 result.stderr_contains("Permission denied");
490 assert!(
491 !result.stderr_str().contains("No such file"),
492 "shred misreported an inaccessible file as missing: {}",
493 result.stderr_str()
494 );
495
496 // Restore search permission so the fixture directory can be cleaned up.
497 set_permissions(at.plus_as_string("locked"), Permissions::from_mode(0o755)).unwrap();
498 }
499
500 #[cfg(all(feature = "feat_diagnostics", not(wasi_runner)))]
501 mod diagnostics {
502 use super::*;
503
504 #[cfg(unix)]
505 #[test]
506 fn test_snippet_points_at_the_unknown_unit() {
507 let (at, mut ucmd) = at_and_ucmd!();
508 at.touch("wipe_me");
509
510 let result = ucmd
511 .terminal_sim_stderr()
512 .args(&["-s", "4vv", "wipe_me"])
513 .fails_with_code(1);
514
515 // The number parsed; only the unit did not.
516 assert_eq!(
517 result.stderr_as_displayed(),
518 "\
519 shred: invalid file size: '4vv'
520 ╭─[ shred:1:11 ]
521 │
522 1 │ shred -s 4vv wipe_me
523 │ ─┬
524 │ ╰── not a known unit
525 │
526 │ Help: a size is a number and an optional unit: K, M, G and so on for 1024, KB, MB, GB for 1000
527 ───╯"
528 );
529
530 // The file must be left alone when the size does not parse.
531 assert!(at.file_exists("wipe_me"));
532 }
533
534 #[cfg(unix)]
535 #[test]
536 fn test_snippet_underlines_a_size_with_no_number() {
537 let (at, mut ucmd) = at_and_ucmd!();
538 at.touch("wipe_me");
539
540 let result = ucmd
541 .terminal_sim_stderr()
542 .args(&["--size=vv", "wipe_me"])
543 .fails_with_code(1);
544 let stderr = result.stderr_as_displayed();
545
546 // Nothing usable was read, so the whole value is underlined.
547 assert!(stderr.contains("shred:1:14"), "{stderr}");
548 assert!(!stderr.contains("not a known unit"), "{stderr}");
549 }
550
551 #[test]
552 fn test_plain_message_when_stderr_is_a_pipe() {
553 let (at, mut ucmd) = at_and_ucmd!();
554 at.touch("wipe_me");
555
556 ucmd.args(&["-s", "4vv", "wipe_me"])
557 .fails_with_code(1)
558 .stderr_is("shred: invalid file size: '4vv'\n");
559 }
560 }