ptx: prefer xpalloc to x2nrealloc
[coreutils.git] / tests / cksum / md5sum.pl
blob7a165c56911124402a40524107d186eebc64c274
1 #!/usr/bin/perl
2 # Basic tests for "md5sum".
4 # Copyright (C) 1998-2024 Free Software Foundation, Inc.
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <https://www.gnu.org/licenses/>.
19 use strict;
21 my $prog = 'md5sum';
23 # Turn off localization of executable's output.
24 @ENV{qw(LANGUAGE LANG LC_ALL)} = ('C') x 3;
26 my $degenerate = "d41d8cd98f00b204e9800998ecf8427e";
28 my $try_help = "Try 'md5sum --help' for more information.\n";
30 my @Tests =
32 ['1', {IN=> {f=> ''}}, {OUT=>"$degenerate f\n"}],
33 ['2', {IN=> {f=> 'a'}}, {OUT=>"0cc175b9c0f1b6a831c399e269772661 f\n"}],
34 ['3', {IN=> {f=> 'abc'}}, {OUT=>"900150983cd24fb0d6963f7d28e17f72 f\n"}],
35 ['4', {IN=> {f=> 'message digest'}},
36 {OUT=>"f96b697d7cb7938d525a2f31aaf161d0 f\n"}],
37 ['5', {IN=> {f=> 'abcdefghijklmnopqrstuvwxyz'}},
38 {OUT=>"c3fcd3d76192e4007dfb496cca67e13b f\n"}],
39 ['6', {IN=> {f=> join ('', 'A'..'Z', 'a'..'z', '0'..'9')}},
40 {OUT=>"d174ab98d277d9f5a5611c2c9f419d9f f\n"}],
41 ['7', {IN=> {f=> '1234567890' x 8}},
42 {OUT=>"57edf4a22be3c955ac49da2e2107b67a f\n"}],
43 ['backslash-1', {IN=> {".\nfoo"=> ''}},
44 {OUT=>"\\$degenerate .\\nfoo\n"}],
45 ['backslash-2', {IN=> {".\\foo"=> ''}},
46 {OUT=>"\\$degenerate .\\\\foo\n"}],
47 ['backslash-3', {IN=> {".\rfoo"=> ''}},
48 {OUT=>"\\$degenerate .\\rfoo\n"}],
49 ['check-1', '--check', {AUX=> {f=> ''}},
50 {IN=> {'f.md5' => "$degenerate f\n"}},
51 {OUT=>"f: OK\n"}],
52 ['check-windows', '--check', {AUX=> {f=> ''}},
53 {IN=> {'f.md5' => "$degenerate f\r\n"}},
54 {OUT=>"f: OK\n"}],
56 # Same as above, but with an added empty line, to test --strict.
57 ['ck-strict-1', '--check --strict', {AUX=> {f=> ''}},
58 {IN=> {'f.md5' => "$degenerate f\n\n"}},
59 {OUT=>"f: OK\n"}],
61 # As above, but with the invalid line first, to ensure that following
62 # lines are processed in spite of the preceding invalid input line.
63 ['ck-strict-2', '--check --strict', {AUX=> {f=> ''}},
64 {IN=> {'in.md5' => "ERR\n$degenerate f\n"}},
65 {OUT=>"f: OK\n"},
66 {ERR=>"md5sum: "
67 . "WARNING: 1 line is improperly formatted\n"},
68 {EXIT=> 1}],
69 ['check-2', '--check', '--status', {IN=>{'f.md5' => "$degenerate f\n"}},
70 {AUX=> {f=> 'foo'}}, {EXIT=> 1}],
71 ['check-quiet1', '--check', '--quiet', {AUX=> {f=> ''}},
72 {IN=> {'f.md5' => "$degenerate f\n"}},
73 {OUT=>""}],
74 ['check-quiet2', '--check', '--quiet',
75 {IN=>{'f.md5' => "$degenerate f\n"}},
76 {AUX=> {f=> 'foo'}}, {OUT=>"f: FAILED\n"},
77 {ERR=>"md5sum: WARNING: 1 computed"
78 . " checksum did NOT match\n"},
79 {EXIT=> 1}],
80 # Exercise new-after-8.6, easier-to-translate diagnostics.
81 ['check-multifail', '--check',
82 {IN=>{'f.md5' =>
83 "$degenerate f\n"
84 . "$degenerate f\n"
85 . "invalid\n" }},
86 {AUX=> {f=> 'foo'}},
87 {OUT=>"f: FAILED\nf: FAILED\n"},
88 {ERR=>"md5sum: WARNING: 1 line is improperly formatted\n"
89 . "md5sum: WARNING: 2 computed checksums did NOT match\n"},
90 {EXIT=> 1}],
91 # Similar to the above, but use --warn to evoke one more diagnostic.
92 ['check-multifail-warn', '--check', '--warn',
93 {IN=>{'f.md5' =>
94 "$degenerate f\n"
95 . "$degenerate f\n"
96 . "invalid\n" }},
97 {AUX=> {f=> 'foo'}},
98 {OUT=>"f: FAILED\nf: FAILED\n"},
99 {ERR=>"md5sum: f.md5: 3: "
100 . "improperly formatted MD5 checksum line\n"
101 . "md5sum: WARNING: 1 line is improperly formatted\n"
102 . "md5sum: WARNING: 2 computed checksums did NOT match\n"},
103 {EXIT=> 1}],
104 # Ensure we use appropriate state to track failures (broken in 9.2)
105 ['check-multifail-state', '--check', '--warn',
106 {IN=>{'f.md5' =>
107 "$degenerate f\n"
108 . "$degenerate g\n"
109 . "$degenerate f\n" }},
110 {AUX=> {f=> ''}}, {AUX=> {g=> 'a'}},
111 {OUT=>"f: OK\ng: FAILED\nf: OK\n"},
112 {ERR=>"md5sum: WARNING: 1 computed checksum did NOT match\n"},
113 {EXIT=> 1}],
114 # The sha1sum and md5sum drivers share a lot of code.
115 # Ensure that md5sum does *not* share the part that makes
116 # sha1sum accept BSD format.
117 ['check-bsd', '--check', {IN=> {'f.sha1' => "SHA1 (f) = $degenerate\n"}},
118 {AUX=> {f=> ''}},
119 {ERR=>"md5sum: f.sha1: no properly formatted "
120 . "checksum lines found\n"},
121 {EXIT=> 1}],
122 ['check-bsd2', '--check', {IN=> {'f.md5' => "MD5 (f) = $degenerate\n"}},
123 {AUX=> {f=> ''}}, {OUT=>"f: OK\n"}],
124 ['check-bsd3', '--check', '--status',
125 {IN=> {'f.md5' => "MD5 (f) = $degenerate\n"}},
126 {AUX=> {f=> 'bar'}}, {EXIT=> 1}],
127 ['check-openssl', '--check', {IN=> {'f.sha1' => "SHA1(f)= $degenerate\n"}},
128 {AUX=> {f=> ''}},
129 {ERR=>"md5sum: f.sha1: no properly formatted "
130 . "checksum lines found\n"},
131 {EXIT=> 1}],
132 ['check-openssl2', '--check', {IN=> {'f.md5' => "MD5(f)= $degenerate\n"}},
133 {AUX=> {f=> ''}}, {OUT=>"f: OK\n"}],
134 ['check-openssl3', '--check', '--status',
135 {IN=> {'f.md5' => "MD5(f)= $degenerate\n"}},
136 {AUX=> {f=> 'bar'}}, {EXIT=> 1}],
137 ['check-ignore-missing-1', '--check', '--ignore-missing',
138 {AUX=> {f=> ''}},
139 {IN=> {'f.md5' => "$degenerate f\n".
140 "$degenerate f.missing\n"}},
141 {OUT=>"f: OK\n"}],
142 ['check-ignore-missing-2', '--check', '--ignore-missing',
143 {AUX=> {f=> ''}},
144 {IN=> {'f.md5' => "$degenerate f\n".
145 "$degenerate f.missing\n"}},
146 {OUT=>"f: OK\n"}],
147 ['check-ignore-missing-3', '--check', '--quiet', '--ignore-missing',
148 {AUX=> {f=> ''}},
149 {IN=> {'f.md5' => "$degenerate missing/f\n".
150 "$degenerate f\n"}},
151 {OUT=>""}],
152 ['check-ignore-missing-4', '--ignore-missing',
153 {IN=> {f=> ''}},
154 {ERR=>"md5sum: the --ignore-missing option is ".
155 "meaningful only when verifying checksums\n".
156 $try_help},
157 {EXIT=> 1}],
158 ['check-ignore-missing-5', '--check', '--ignore-missing',
159 {AUX=> {f=> ''}},
160 {IN=> {'f.md5' => "$degenerate missing\n"}},
161 {ERR=>
162 "md5sum: f.md5: no file was verified\n"},
163 {EXIT=> 1}],
164 # coreutils-8.25 with --ignore-missing treated checksums starting with 00
165 # as if the file was not present
166 ['check-ignore-missing-6', '--check', '--ignore-missing',
167 {AUX=> {f=> '9t'}},
168 {IN=> {'f.md5' =>
169 "006999e6df389641adf1fa3a74801d9d f\n"}},
170 {OUT=>"f: OK\n"}],
171 ['bsd-segv', '--check', {IN=> {'z' => "MD5 ("}}, {EXIT=> 1},
172 {ERR=> "$prog: z: no properly formatted checksum lines found\n"}],
174 # Ensure that when there's a NUL byte among the checksum hex digits
175 # we detect the invalid formatting and don't even open the file.
176 # Up to coreutils-6.10, this would report:
177 # h: FAILED
178 # md5sum: WARNING: 1 of 1 computed checksum did NOT match
179 ['nul-in-cksum', '--check', {IN=> {'h'=>("\0"x32)." h\n"}}, {EXIT=> 1},
180 {ERR=> "$prog: h: no properly formatted checksum lines found\n"}],
183 # Insert the '--text' argument for each test.
184 my $t;
185 foreach $t (@Tests)
187 splice @$t, 1, 0, '--text' unless @$t[1] =~ /--check/;
190 my $save_temps = $ENV{DEBUG};
191 my $verbose = $ENV{VERBOSE};
193 my $fail = run_tests ($prog, $prog, \@Tests, $save_temps, $verbose);
194 exit $fail;