Debian Codegraph @

tests/by-util/test_md5sum.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 use uutests::new_ucmd;
7 use uutests::util::TestScenario;
8 use uutests::util_name;
9 // spell-checker:ignore checkfile, testf, ntestf
10 macro_rules! get_hash(
11 ($str:expr) => (
12 $str.split(' ').collect::<Vec<&str>>()[0]
13 );
14 );
15
16 macro_rules! test_digest {
17 ($id:ident) => {
18 mod $id {
19 use uutests::util::*;
20 use uutests::util_name;
21 static EXPECTED_FILE: &'static str = concat!(stringify!($id), ".expected");
22 static CHECK_FILE: &'static str = concat!(stringify!($id), ".checkfile");
23 static INPUT_FILE: &'static str = "input.txt";
24
25 #[test]
26 fn test_single_file() {
27 let ts = TestScenario::new(util_name!());
28 assert_eq!(
29 ts.fixtures.read(EXPECTED_FILE),
30 get_hash!(
31 ts.ucmd()
32 .arg(INPUT_FILE)
33 .succeeds()
34 .no_stderr()
35 .stdout_str()
36 )
37 );
38 }
39
40 #[test]
41 fn test_stdin() {
42 let ts = TestScenario::new(util_name!());
43 assert_eq!(
44 ts.fixtures.read(EXPECTED_FILE),
45 get_hash!(
46 ts.ucmd()
47 .pipe_in_fixture(INPUT_FILE)
48 .succeeds()
49 .no_stderr()
50 .stdout_str()
51 )
52 );
53 }
54
55 #[test]
56 fn test_stdin_with_dash_directory() {
57 let ts = TestScenario::new(util_name!());
58 ts.fixtures.mkdir("-");
59 assert_eq!(
60 ts.fixtures.read(EXPECTED_FILE),
61 get_hash!(
62 ts.ucmd()
63 .pipe_in_fixture(INPUT_FILE)
64 .succeeds()
65 .no_stderr()
66 .stdout_str()
67 )
68 );
69 }
70
71 #[test]
72 fn test_check() {
73 let ts = TestScenario::new(util_name!());
74 println!("File content='{}'", ts.fixtures.read(INPUT_FILE));
75 println!("Check file='{}'", ts.fixtures.read(CHECK_FILE));
76
77 ts.ucmd()
78 .args(&["--check", CHECK_FILE])
79 .succeeds()
80 .no_stderr()
81 .stdout_is("input.txt: OK\n");
82 }
83
84 #[test]
85 fn test_zero() {
86 let ts = TestScenario::new(util_name!());
87 assert_eq!(
88 ts.fixtures.read(EXPECTED_FILE),
89 get_hash!(
90 ts.ucmd()
91 .arg("--zero")
92 .arg(INPUT_FILE)
93 .succeeds()
94 .no_stderr()
95 .stdout_str()
96 )
97 );
98 }
99
100 #[test]
101 fn test_missing_file() {
102 let ts = TestScenario::new(util_name!());
103 let at = &ts.fixtures;
104
105 at.write("a", "file1\n");
106 at.write("c", "file3\n");
107
108 ts.ucmd()
109 .args(&["a", "b", "c"])
110 .fails()
111 .stdout_contains("a\n")
112 .stdout_contains("c\n")
113 .stderr_contains("b: No such file or directory");
114 }
115 }
116 };
117 }
118
119 test_digest! {md5}
120
121 #[test]
122 fn test_check_md5_ignore_missing() {
123 let scene = TestScenario::new(util_name!());
124 let at = &scene.fixtures;
125
126 at.write("testf", "foobar\n");
127 at.write(
128 "testf.sha1",
129 "14758f1afd44c09b7992073ccf00b43d testf\n14758f1afd44c09b7992073ccf00b43d testf2\n",
130 );
131 scene
132 .ccmd("md5sum")
133 .arg("-c")
134 .arg(at.subdir.join("testf.sha1"))
135 .fails()
136 .stdout_contains("testf2: FAILED open or read");
137
138 scene
139 .ccmd("md5sum")
140 .arg("-c")
141 .arg("--ignore-missing")
142 .arg(at.subdir.join("testf.sha1"))
143 .succeeds()
144 .stdout_only("testf: OK\n");
145
146 scene
147 .ccmd("md5sum")
148 .arg("--ignore-missing")
149 .arg(at.subdir.join("testf.sha1"))
150 .fails()
151 .stderr_contains(
152 "md5sum: the --ignore-missing option is meaningful only when verifying checksums",
153 );
154 }
155
156 // Asterisk `*` is a reserved paths character on win32, nor the path can end with a whitespace.
157 // ref: https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file#naming-conventions
158 #[test]
159 fn test_check_md5sum() {
160 let scene = TestScenario::new(util_name!());
161 let at = &scene.fixtures;
162
163 #[cfg(not(windows))]
164 {
165 for f in &["a", " b", "*c", "dd", " "] {
166 at.write(f, &format!("{f}\n"));
167 }
168 at.write(
169 "check.md5sum",
170 "60b725f10c9c85c70d97880dfe8191b3 a\n\
171 bf35d7536c785cf06730d5a40301eba2 b\n\
172 f5b61709718c1ecf8db1aea8547d4698 *c\n\
173 b064a020db8018f18ff5ae367d01b212 dd\n\
174 d784fa8b6d98d27699781bd9a7cf19f0 ",
175 );
176 scene
177 .ccmd("md5sum")
178 .arg("--strict")
179 .arg("-c")
180 .arg("check.md5sum")
181 .succeeds()
182 .stdout_only("a: OK\n' b': OK\n'*c': OK\ndd: OK\n' ': OK\n");
183 }
184 #[cfg(windows)]
185 {
186 for f in &["a", " b", "dd"] {
187 at.write(f, &format!("{f}\n"));
188 }
189 at.write(
190 "check.md5sum",
191 "60b725f10c9c85c70d97880dfe8191b3 a\n\
192 bf35d7536c785cf06730d5a40301eba2 b\n\
193 b064a020db8018f18ff5ae367d01b212 dd",
194 );
195 scene
196 .ccmd("md5sum")
197 .arg("--strict")
198 .arg("-c")
199 .arg("check.md5sum")
200 .succeeds()
201 .stdout_only("a: OK\n' b': OK\ndd: OK\n");
202 }
203 }
204
205 // GNU also supports one line sep
206 #[test]
207 fn test_check_md5sum_only_one_space() {
208 let scene = TestScenario::new(util_name!());
209 let at = &scene.fixtures;
210
211 for f in ["a", " b", "c"] {
212 at.write(f, &format!("{f}\n"));
213 }
214 at.write(
215 "check.md5sum",
216 "60b725f10c9c85c70d97880dfe8191b3 a\n\
217 bf35d7536c785cf06730d5a40301eba2 b\n\
218 2cd6ee2c70b0bde53fbe6cac3c8b8bb1 c\n",
219 );
220 scene
221 .ccmd("md5sum")
222 .arg("--strict")
223 .arg("-c")
224 .arg("check.md5sum")
225 .succeeds()
226 .stdout_only("a: OK\n' b': OK\nc: OK\n");
227 }
228
229 // A generated checksum file must verify against the same file on all
230 // platforms: generation and --check both hash raw bytes, so on Windows a file
231 // containing CRLFs must not be hashed with CRLF -> LF conversion in check mode.
232 #[test]
233 fn test_check_generate_round_trip_crlf_file() {
234 let scene = TestScenario::new(util_name!());
235 let at = &scene.fixtures;
236
237 at.write_bytes("f", b"abc\r\nd\r");
238 let result = scene.ccmd("md5sum").arg("f").succeeds();
239 at.write_bytes("CHECKSUM", result.stdout());
240
241 scene
242 .ccmd("md5sum")
243 .arg("--check")
244 .arg("CHECKSUM")
245 .succeeds()
246 .stdout_only("f: OK\n");
247 }
248
249 #[test]
250 fn test_check_md5sum_reverse_bsd() {
251 let scene = TestScenario::new(util_name!());
252 let at = &scene.fixtures;
253
254 #[cfg(not(windows))]
255 {
256 for f in &["a", " b", "*c", "dd", " "] {
257 at.write(f, &format!("{f}\n"));
258 }
259 at.write(
260 "check.md5sum",
261 "60b725f10c9c85c70d97880dfe8191b3 a\n\
262 bf35d7536c785cf06730d5a40301eba2 b\n\
263 f5b61709718c1ecf8db1aea8547d4698 *c\n\
264 b064a020db8018f18ff5ae367d01b212 dd\n\
265 d784fa8b6d98d27699781bd9a7cf19f0 ",
266 );
267 scene
268 .ccmd("md5sum")
269 .arg("--strict")
270 .arg("-c")
271 .arg("check.md5sum")
272 .succeeds()
273 .stdout_only("a: OK\n' b': OK\n'*c': OK\ndd: OK\n' ': OK\n");
274 }
275 #[cfg(windows)]
276 {
277 for f in &["a", " b", "dd"] {
278 at.write(f, &format!("{f}\n"));
279 }
280 at.write(
281 "check.md5sum",
282 "60b725f10c9c85c70d97880dfe8191b3 a\n\
283 bf35d7536c785cf06730d5a40301eba2 b\n\
284 b064a020db8018f18ff5ae367d01b212 dd",
285 );
286 scene
287 .ccmd("md5sum")
288 .arg("--strict")
289 .arg("-c")
290 .arg("check.md5sum")
291 .succeeds()
292 .stdout_only("a: OK\n' b': OK\ndd: OK\n");
293 }
294 }
295
296 #[test]
297 fn test_check_md5sum_mixed_format() {
298 let scene = TestScenario::new(util_name!());
299 let at = &scene.fixtures;
300
301 #[cfg(not(windows))]
302 {
303 for f in &[" b", "*c", "dd", " "] {
304 at.write(f, &format!("{f}\n"));
305 }
306 at.write(
307 "check.md5sum",
308 "bf35d7536c785cf06730d5a40301eba2 b\n\
309 f5b61709718c1ecf8db1aea8547d4698 *c\n\
310 b064a020db8018f18ff5ae367d01b212 dd\n\
311 d784fa8b6d98d27699781bd9a7cf19f0 ",
312 );
313 }
314 #[cfg(windows)]
315 {
316 for f in &[" b", "dd"] {
317 at.write(f, &format!("{f}\n"));
318 }
319 at.write(
320 "check.md5sum",
321 "bf35d7536c785cf06730d5a40301eba2 b\n\
322 b064a020db8018f18ff5ae367d01b212 dd",
323 );
324 }
325 scene
326 .ccmd("md5sum")
327 .arg("--strict")
328 .arg("-c")
329 .arg("check.md5sum")
330 .fails_with_code(1);
331 }
332
333 #[test]
334 fn test_invalid_arg() {
335 new_ucmd!().arg("--definitely-invalid").fails_with_code(1);
336 }
337
338 #[test]
339 fn test_conflicting_arg() {
340 new_ucmd!().arg("--tag").arg("--check").fails_with_code(1);
341 new_ucmd!().arg("--tag").arg("--text").fails_with_code(1);
342 }
343
344 #[test]
345 #[cfg(windows)]
346 fn test_windows_path_separator_round_trip() {
347 let scene = TestScenario::new(util_name!());
348 scene.fixtures.mkdir("subdir");
349 scene.fixtures.write("subdir/file.txt", "abc");
350
351 let result = scene
352 .ucmd()
353 .args(&["--text", "subdir\\file.txt"])
354 .succeeds();
355
356 result
357 .no_stderr()
358 .stdout_is("900150983cd24fb0d6963f7d28e17f72 subdir/file.txt\n");
359
360 scene
361 .ucmd()
362 .args(&["--check", "--strict"])
363 .pipe_in(result.stdout_str())
364 .succeeds()
365 .no_stderr()
366 .stdout_is("subdir/file.txt: OK\n");
367 }
368
369 #[test]
370 #[cfg_attr(windows, ignore = "Disabled on windows")]
371 fn test_with_escape_filename() {
372 let scene = TestScenario::new(util_name!());
373
374 let at = &scene.fixtures;
375 let filename = "a\nb";
376 at.touch(filename);
377 let result = scene.ccmd("md5sum").arg("--text").arg(filename).succeeds();
378 let stdout = result.stdout_str();
379 println!("stdout {stdout}");
380 assert!(stdout.starts_with('\\'));
381 assert!(stdout.trim().ends_with("a\\nb"));
382 }
383
384 #[test]
385 #[cfg_attr(windows, ignore = "Disabled on windows")]
386 fn test_with_escape_filename_zero_text() {
387 let scene = TestScenario::new(util_name!());
388
389 let at = &scene.fixtures;
390 let filename = "a\nb";
391 at.touch(filename);
392 let result = scene
393 .ccmd("md5sum")
394 .arg("--text")
395 .arg("--zero")
396 .arg(filename)
397 .succeeds();
398 let stdout = result.stdout_str();
399 println!("stdout {stdout}");
400 assert!(!stdout.starts_with('\\'));
401 assert!(stdout.contains("a\nb"));
402 }
403
404 #[test]
405 fn test_check_empty_line() {
406 let scene = TestScenario::new(util_name!());
407 let at = &scene.fixtures;
408
409 at.touch("f");
410 at.write(
411 "in.md5",
412 "d41d8cd98f00b204e9800998ecf8427e f\n\nd41d8cd98f00b204e9800998ecf8427e f\ninvalid\n\n",
413 );
414 scene
415 .ccmd("md5sum")
416 .arg("--check")
417 .arg(at.subdir.join("in.md5"))
418 .succeeds()
419 .stderr_contains("WARNING: 1 line is improperly formatted");
420 }
421
422 #[test]
423 #[cfg(windows)]
424 fn test_check_invalid_utf8_reports_read_error() {
425 new_ucmd!()
426 .args(&["--check", "-"])
427 .pipe_in(b"invalid\xff\n")
428 .fails_with_code(1)
429 .no_stdout()
430 .stderr_is("md5sum: -: read error\n");
431 }
432
433 #[test]
434 #[cfg_attr(windows, ignore = "Disabled on windows")]
435 fn test_check_with_escape_filename() {
436 let scene = TestScenario::new(util_name!());
437
438 let at = &scene.fixtures;
439
440 let filename = "a\nb";
441 at.touch(filename);
442 let result = scene.ccmd("md5sum").arg("--tag").arg(filename).succeeds();
443 let stdout = result.stdout_str();
444 println!("stdout {stdout}");
445 assert!(stdout.starts_with("\\MD5"));
446 assert!(stdout.contains("a\\nb"));
447 at.write("check.md5", stdout);
448 let result = scene
449 .ccmd("md5sum")
450 .arg("--strict")
451 .arg("-c")
452 .arg("check.md5")
453 .succeeds();
454 result.stdout_is("'a'$'\\n''b': OK\n");
455 }
456
457 #[test]
458 fn test_check_strict_error() {
459 let scene = TestScenario::new(util_name!());
460 let at = &scene.fixtures;
461
462 at.touch("f");
463 at.write(
464 "in.md5",
465 "ERR\nERR\nd41d8cd98f00b204e9800998ecf8427e f\nERR\n",
466 );
467 scene
468 .ccmd("md5sum")
469 .arg("--check")
470 .arg("--strict")
471 .arg(at.subdir.join("in.md5"))
472 .fails()
473 .stderr_contains("WARNING: 3 lines are improperly formatted");
474 }
475
476 #[test]
477 fn test_check_warn() {
478 let scene = TestScenario::new(util_name!());
479 let at = &scene.fixtures;
480
481 at.touch("f");
482 at.write(
483 "in.md5",
484 "d41d8cd98f00b204e9800998ecf8427e f\nd41d8cd98f00b204e9800998ecf8427e f\ninvalid\n",
485 );
486 scene
487 .ccmd("md5sum")
488 .arg("--check")
489 .arg("--warn")
490 .arg(at.subdir.join("in.md5"))
491 .succeeds()
492 .stderr_contains("in.md5: 3: improperly formatted MD5 checksum line")
493 .stderr_contains("WARNING: 1 line is improperly formatted");
494
495 // with strict, we should fail the execution
496 scene
497 .ccmd("md5sum")
498 .arg("--check")
499 .arg("--strict")
500 .arg(at.subdir.join("in.md5"))
501 .fails();
502 }
503
504 #[test]
505 fn test_check_status() {
506 let scene = TestScenario::new(util_name!());
507 let at = &scene.fixtures;
508
509 at.touch("f");
510 at.write("in.md5", "MD5(f)= d41d8cd98f00b204e9800998ecf8427f\n");
511 scene
512 .ccmd("md5sum")
513 .arg("--check")
514 .arg("--status")
515 .arg(at.subdir.join("in.md5"))
516 .fails()
517 .no_output();
518 }
519
520 #[test]
521 fn test_check_status_code() {
522 let scene = TestScenario::new(util_name!());
523 let at = &scene.fixtures;
524
525 at.touch("f");
526 at.write("in.md5", "d41d8cd98f00b204e9800998ecf8427f f\n");
527 scene
528 .ccmd("md5sum")
529 .arg("--check")
530 .arg("--status")
531 .arg(at.subdir.join("in.md5"))
532 .fails()
533 .no_output();
534 }
535
536 #[test]
537 fn test_check_status_reports_malformed_input() {
538 // --status should still print "no properly formatted checksum lines found"
539 // when all input lines are invalid (issue #12867)
540 new_ucmd!()
541 .args(&["-c", "--status"])
542 .pipe_in("I'mNotAHash\n")
543 .fails()
544 .no_stdout()
545 .stderr_contains("no properly formatted checksum lines found");
546 }
547
548 #[test]
549 fn test_sha1_with_md5sum_should_fail() {
550 let scene = TestScenario::new(util_name!());
551 let at = &scene.fixtures;
552
553 at.touch("f");
554 at.write("f.sha1", "SHA1 (f) = d41d8cd98f00b204e9800998ecf8427e\n");
555 scene
556 .ccmd("md5sum")
557 .arg("--check")
558 .arg(at.subdir.join("f.sha1"))
559 .fails()
560 .stderr_contains("f.sha1: no properly formatted checksum lines found")
561 .stderr_does_not_contain("WARNING: 1 line is improperly formatted");
562 }
563
564 #[test]
565 // Disabled on Windows because of the "*"
566 #[cfg_attr(windows, ignore = "Disabled on windows")]
567 fn test_check_one_two_space_star() {
568 let scene = TestScenario::new(util_name!());
569 let at = &scene.fixtures;
570
571 at.touch("empty");
572
573 // with one space, the "*" is removed
574 at.write("in.md5", "d41d8cd98f00b204e9800998ecf8427e *empty\n");
575
576 scene
577 .ccmd("md5sum")
578 .arg("--check")
579 .arg(at.subdir.join("in.md5"))
580 .succeeds()
581 .stdout_is("empty: OK\n");
582
583 // with two spaces, the "*" is not removed
584 at.write("in.md5", "d41d8cd98f00b204e9800998ecf8427e *empty\n");
585 // First should fail as *empty doesn't exit
586 scene
587 .ccmd("md5sum")
588 .arg("--check")
589 .arg(at.subdir.join("in.md5"))
590 .fails()
591 .stdout_is("'*empty': FAILED open or read\n");
592
593 at.touch("*empty");
594 // Should pass as we have the file
595 scene
596 .ccmd("md5sum")
597 .arg("--check")
598 .arg(at.subdir.join("in.md5"))
599 .succeeds()
600 .stdout_is("'*empty': OK\n");
601 }
602
603 #[test]
604 // Disabled on Windows because of the "*"
605 #[cfg_attr(windows, ignore = "Disabled on windows")]
606 fn test_check_space_star_or_not() {
607 let scene = TestScenario::new(util_name!());
608 let at = &scene.fixtures;
609
610 at.touch("a");
611 at.touch("*c");
612
613 // with one space, the "*" is removed
614 at.write(
615 "in.md5",
616 "d41d8cd98f00b204e9800998ecf8427e *c\n
617 d41d8cd98f00b204e9800998ecf8427e a\n",
618 );
619
620 scene
621 .ccmd("md5sum")
622 .arg("--check")
623 .arg(at.subdir.join("in.md5"))
624 .fails()
625 .stdout_contains("c: FAILED")
626 .stdout_does_not_contain("a: FAILED")
627 .stderr_contains("WARNING: 1 line is improperly formatted");
628
629 at.write(
630 "in.md5",
631 "d41d8cd98f00b204e9800998ecf8427e a\n
632 d41d8cd98f00b204e9800998ecf8427e *c\n",
633 );
634
635 // First should fail as *empty doesn't exit
636 scene
637 .ccmd("md5sum")
638 .arg("--check")
639 .arg(at.subdir.join("in.md5"))
640 .succeeds()
641 .stdout_contains("a: OK")
642 .stderr_contains("WARNING: 1 line is improperly formatted");
643 }
644
645 #[test]
646 fn test_check_no_backslash_no_space() {
647 let scene = TestScenario::new(util_name!());
648 let at = &scene.fixtures;
649
650 at.touch("f");
651 at.write("in.md5", "MD5(f)= d41d8cd98f00b204e9800998ecf8427e\n");
652 scene
653 .ccmd("md5sum")
654 .arg("--check")
655 .arg(at.subdir.join("in.md5"))
656 .succeeds()
657 .stdout_is("f: OK\n");
658 }
659
660 #[test]
661 fn test_incomplete_format() {
662 let scene = TestScenario::new(util_name!());
663 let at = &scene.fixtures;
664
665 at.touch("f");
666 at.write("in.md5", "MD5 (\n");
667 scene
668 .ccmd("md5sum")
669 .arg("--check")
670 .arg(at.subdir.join("in.md5"))
671 .fails()
672 .stderr_contains("no properly formatted checksum lines found");
673 }
674
675 #[test]
676 fn test_start_error() {
677 let scene = TestScenario::new(util_name!());
678 let at = &scene.fixtures;
679
680 at.touch("f");
681 at.write("in.md5", "ERR\nd41d8cd98f00b204e9800998ecf8427e f\n");
682 scene
683 .ccmd("md5sum")
684 .arg("--check")
685 .arg("--strict")
686 .arg(at.subdir.join("in.md5"))
687 .fails()
688 .stdout_is("f: OK\n")
689 .stderr_contains("WARNING: 1 line is improperly formatted");
690 }
691
692 #[test]
693 fn test_check_check_ignore_no_file() {
694 let scene = TestScenario::new(util_name!());
695 let at = &scene.fixtures;
696
697 at.touch("f");
698 at.write("in.md5", "d41d8cd98f00b204e9800998ecf8427f missing\n");
699 scene
700 .ccmd("md5sum")
701 .arg("--check")
702 .arg("--ignore-missing")
703 .arg(at.subdir.join("in.md5"))
704 .fails()
705 .stderr_contains("in.md5: no file was verified");
706 }
707
708 #[test]
709 fn test_check_directory_error() {
710 let scene = TestScenario::new(util_name!());
711 let at = &scene.fixtures;
712
713 at.mkdir("d");
714 at.write("in.md5", "d41d8cd98f00b204e9800998ecf8427f d\n");
715 #[cfg(not(windows))]
716 let err_msg = "md5sum: d: Is a directory\n";
717 #[cfg(windows)]
718 let err_msg = "md5sum: d: Permission denied\n";
719 scene
720 .ccmd("md5sum")
721 .arg("--check")
722 .arg(at.subdir.join("in.md5"))
723 .fails()
724 .stderr_contains(err_msg);
725 }
726
727 #[test]
728 #[cfg(not(windows))]
729 fn test_continue_after_directory_error() {
730 let scene = TestScenario::new(util_name!());
731 let at = &scene.fixtures;
732
733 at.mkdir("d");
734 at.touch("file");
735 at.touch("no_read_perms");
736 at.set_mode("no_read_perms", 200);
737
738 let (out, err_msg) = (
739 "d41d8cd98f00b204e9800998ecf8427e file\n",
740 [
741 "md5sum: d: Is a directory",
742 "md5sum: dne: No such file or directory",
743 "md5sum: no_read_perms: Permission denied\n",
744 ]
745 .join("\n"),
746 );
747
748 scene
749 .ccmd("md5sum")
750 .arg("d")
751 .arg("dne")
752 .arg("no_read_perms")
753 .arg("file")
754 .fails()
755 .stdout_is(out)
756 .stderr_is(err_msg);
757 }
758
759 #[test]
760 fn test_check_quiet() {
761 let scene = TestScenario::new(util_name!());
762 let at = &scene.fixtures;
763
764 at.touch("f");
765 at.write("in.md5", "d41d8cd98f00b204e9800998ecf8427e f\n");
766 scene
767 .ccmd("md5sum")
768 .arg("--quiet")
769 .arg("--check")
770 .arg(at.subdir.join("in.md5"))
771 .succeeds()
772 .no_output();
773
774 // incorrect md5
775 at.write("in.md5", "d41d8cd98f00b204e9800998ecf8427f f\n");
776 scene
777 .ccmd("md5sum")
778 .arg("--quiet")
779 .arg("--check")
780 .arg(at.subdir.join("in.md5"))
781 .fails()
782 .stdout_contains("f: FAILED")
783 .stderr_contains("WARNING: 1 computed checksum did NOT match");
784
785 scene
786 .ccmd("md5sum")
787 .arg("--quiet")
788 .arg(at.subdir.join("in.md5"))
789 .fails()
790 .stderr_contains("md5sum: the --quiet option is meaningful only when verifying checksums");
791 scene
792 .ccmd("md5sum")
793 .arg("--strict")
794 .arg(at.subdir.join("in.md5"))
795 .fails()
796 .stderr_contains("md5sum: the --strict option is meaningful only when verifying checksums");
797 }
798
799 #[test]
800 fn test_star_to_start() {
801 let scene = TestScenario::new(util_name!());
802 let at = &scene.fixtures;
803
804 at.touch("f");
805 at.write("in.md5", "d41d8cd98f00b204e9800998ecf8427e *f\n");
806 scene
807 .ccmd("md5sum")
808 .arg("--check")
809 .arg(at.subdir.join("in.md5"))
810 .succeeds()
811 .stdout_only("f: OK\n");
812 }
813
814 #[test]
815 fn test_check_md5_comment_line() {
816 // A comment in a checksum file shall be discarded unnoticed.
817
818 let scene = TestScenario::new(util_name!());
819 let at = &scene.fixtures;
820
821 at.write("foo", "foo-content\n");
822 at.write(
823 "MD5SUM",
824 "\
825 # This is a comment\n\
826 8411029f3f5b781026a93db636aca721 foo\n\
827 # next comment is empty\n#",
828 );
829
830 scene
831 .ccmd("md5sum")
832 .arg("--check")
833 .arg("MD5SUM")
834 .succeeds()
835 .stdout_contains("foo: OK")
836 .no_stderr();
837 }
838
839 #[test]
840 fn test_check_md5_comment_only() {
841 // A file only filled with comments is equivalent to an empty file,
842 // and therefore produces an error.
843
844 let scene = TestScenario::new(util_name!());
845 let at = &scene.fixtures;
846
847 at.write("foo", "foo-content\n");
848 at.write("MD5SUM", "# This is a comment\n");
849
850 scene
851 .ccmd("md5sum")
852 .arg("--check")
853 .arg("MD5SUM")
854 .fails()
855 .stderr_contains("no properly formatted checksum lines found");
856 }
857
858 #[test]
859 fn test_check_md5_comment_leading_space() {
860 // A file only filled with comments is equivalent to an empty file,
861 // and therefore produces an error.
862
863 let scene = TestScenario::new(util_name!());
864 let at = &scene.fixtures;
865
866 at.write("foo", "foo-content\n");
867 at.write(
868 "MD5SUM",
869 " # This is a comment\n\
870 8411029f3f5b781026a93db636aca721 foo\n",
871 );
872
873 scene
874 .ccmd("md5sum")
875 .arg("--check")
876 .arg("MD5SUM")
877 .succeeds()
878 .stdout_contains("foo: OK")
879 .stderr_contains("WARNING: 1 line is improperly formatted");
880 }