Commit | Line | Data |
---|---|---|
9508959b JH |
1 | BEGIN { |
2 | if ($ENV{PERL_CORE}) { | |
3 | chdir 't' if -d 't'; | |
4 | @INC = '../lib'; | |
5 | } | |
6 | } | |
7 | ||
c8780a73 | 8 | print "1..3\n"; |
3357b1b1 JH |
9 | |
10 | use strict; | |
4d5cc332 | 11 | use Digest::MD5 qw(md5 md5_hex md5_base64); |
3357b1b1 | 12 | |
8ff693ee JH |
13 | # To update the EBCDIC section even on a Latin 1 platform, |
14 | # run this script with $ENV{EBCDIC_MD5SUM} set to a true value. | |
15 | # (You'll need to have Perl 5.7.3 or later, to have the Encode installed.) | |
16 | # (And remember that under the Perl core distribution you should | |
17 | # also have the $ENV{PERL_CORE} set to a true value.) | |
e69a2255 | 18 | # Similarly, to update MacOS section, run with $ENV{MAC_MD5SUM} set. |
8ff693ee | 19 | |
9a03235d | 20 | my $EXPECT; |
9508959b JH |
21 | if (ord "A" == 193) { # EBCDIC |
22 | $EXPECT = <<EOT; | |
efbeba86 | 23 | 11e8028ee426273db6b6db270a8bb38c README |
47a7661d | 24 | 6e556382813f67120863f4f91b7fcdc2 MD5.xs |
e69a2255 JH |
25 | 276da0aa4e9a08b7fe09430c9c5690aa rfc1321.txt |
26 | EOT | |
4c7fc0a4 | 27 | } elsif ("\n" eq "\015") { # MacOS |
e69a2255 | 28 | $EXPECT = <<EOT; |
efbeba86 | 29 | c95549c6c5e1e1c078b27042f1dc850f README |
47a7661d | 30 | 7aa380c810bc7c1a0bec22cf32bc50d4 MD5.xs |
9a03235d | 31 | 754b9db19f79dbc4992f7166eb0f37ce rfc1321.txt |
fed3f325 | 32 | EOT |
9508959b | 33 | } else { |
c8780a73 | 34 | # This is the output of: 'md5sum README MD5.xs rfc1321.txt' |
9508959b | 35 | $EXPECT = <<EOT; |
efbeba86 | 36 | c95549c6c5e1e1c078b27042f1dc850f README |
47a7661d | 37 | 7aa380c810bc7c1a0bec22cf32bc50d4 MD5.xs |
9508959b JH |
38 | 754b9db19f79dbc4992f7166eb0f37ce rfc1321.txt |
39 | EOT | |
40 | } | |
5a046520 JH |
41 | |
42 | if (!(-f "README") && -f "../README") { | |
43 | chdir("..") or die "Can't chdir: $!"; | |
fed3f325 | 44 | } |
8c42d64c | 45 | |
5a046520 JH |
46 | my $testno = 0; |
47 | ||
4d5cc332 JH |
48 | my $B64 = 1; |
49 | eval { require MIME::Base64; }; | |
50 | if ($@) { | |
9508959b | 51 | print "# $@: Will not test base64 methods\n"; |
4d5cc332 JH |
52 | $B64 = 0; |
53 | } | |
54 | ||
3357b1b1 JH |
55 | for (split /^/, $EXPECT) { |
56 | my($md5hex, $file) = split ' '; | |
8ff693ee | 57 | my $base = $file; |
f05fc781 | 58 | # print "# $base\n"; |
9508959b | 59 | if ($ENV{PERL_CORE}) { |
34791a82 | 60 | if ($file eq 'rfc1321.txt') { # Don't have it in core. |
9508959b JH |
61 | print "ok ", ++$testno, " # Skip: PERL_CORE\n"; |
62 | next; | |
63 | } | |
64 | use File::Spec; | |
ec46a77b | 65 | my @path = qw(ext Digest-MD5); |
9508959b JH |
66 | my $path = File::Spec->updir; |
67 | while (@path) { | |
68 | $path = File::Spec->catdir($path, shift @path); | |
69 | } | |
70 | $file = File::Spec->catfile($path, $file); | |
71 | } | |
72 | # print "# file = $file\n"; | |
8ff693ee JH |
73 | unless (-f $file) { |
74 | warn "No such file: $file\n"; | |
75 | next; | |
76 | } | |
77 | if ($ENV{EBCDIC_MD5SUM}) { | |
0dfa9f37 | 78 | require Encode; |
8ff693ee | 79 | my $data = cat_file($file); |
0dfa9f37 | 80 | Encode::from_to($data, 'latin1', 'cp1047'); |
e69a2255 JH |
81 | print md5_hex($data), " $base\n"; |
82 | next; | |
83 | } | |
84 | if ($ENV{MAC_MD5SUM}) { | |
f05fc781 | 85 | require Encode; |
e69a2255 | 86 | my $data = cat_file($file); |
f05fc781 | 87 | Encode::from_to($data, 'latin1', 'MacRoman'); |
e69a2255 | 88 | print md5_hex($data), " $base\n"; |
8ff693ee JH |
89 | next; |
90 | } | |
3357b1b1 | 91 | my $md5bin = pack("H*", $md5hex); |
4d5cc332 JH |
92 | my $md5b64; |
93 | if ($B64) { | |
94 | $md5b64 = MIME::Base64::encode($md5bin, ""); | |
95 | chop($md5b64); chop($md5b64); # remove padding | |
96 | } | |
3357b1b1 | 97 | my $failed; |
9508959b | 98 | my $got; |
3357b1b1 JH |
99 | |
100 | if (digest_file($file, 'digest') ne $md5bin) { | |
101 | print "$file: Bad digest\n"; | |
102 | $failed++; | |
103 | } | |
104 | ||
9508959b JH |
105 | if (($got = digest_file($file, 'hexdigest')) ne $md5hex) { |
106 | print "$file: Bad hexdigest: got $got expected $md5hex\n"; | |
3357b1b1 JH |
107 | $failed++; |
108 | } | |
109 | ||
4d5cc332 JH |
110 | if ($B64 && digest_file($file, 'b64digest') ne $md5b64) { |
111 | print "$file: Bad b64digest\n"; | |
112 | $failed++; | |
113 | } | |
114 | ||
3357b1b1 JH |
115 | my $data = cat_file($file); |
116 | if (md5($data) ne $md5bin) { | |
117 | print "$file: md5() failed\n"; | |
118 | $failed++; | |
119 | } | |
120 | if (md5_hex($data) ne $md5hex) { | |
121 | print "$file: md5_hex() failed\n"; | |
122 | $failed++; | |
123 | } | |
4d5cc332 JH |
124 | if ($B64 && md5_base64($data) ne $md5b64) { |
125 | print "$file: md5_base64() failed\n"; | |
126 | $failed++; | |
127 | } | |
3357b1b1 JH |
128 | |
129 | if (Digest::MD5->new->add($data)->digest ne $md5bin) { | |
130 | print "$file: MD5->new->add(...)->digest failed\n"; | |
131 | $failed++; | |
132 | } | |
133 | if (Digest::MD5->new->add($data)->hexdigest ne $md5hex) { | |
134 | print "$file: MD5->new->add(...)->hexdigest failed\n"; | |
135 | $failed++; | |
136 | } | |
4d5cc332 JH |
137 | if ($B64 && Digest::MD5->new->add($data)->b64digest ne $md5b64) { |
138 | print "$file: MD5->new->add(...)->b64digest failed\n"; | |
139 | $failed++; | |
140 | } | |
3357b1b1 JH |
141 | |
142 | my @data = split //, $data; | |
143 | if (md5(@data) ne $md5bin) { | |
144 | print "$file: md5(\@data) failed\n"; | |
145 | $failed++; | |
146 | } | |
147 | if (Digest::MD5->new->add(@data)->digest ne $md5bin) { | |
148 | print "$file: MD5->new->add(\@data)->digest failed\n"; | |
149 | $failed++; | |
150 | } | |
151 | my $md5 = Digest::MD5->new; | |
152 | for (@data) { | |
153 | $md5->add($_); | |
154 | } | |
155 | if ($md5->digest ne $md5bin) { | |
156 | print "$file: $md5->add()-loop failed\n"; | |
157 | $failed++; | |
158 | } | |
159 | ||
160 | print "not " if $failed; | |
161 | print "ok ", ++$testno, "\n"; | |
162 | } | |
163 | ||
164 | ||
165 | sub digest_file | |
166 | { | |
167 | my($file, $method) = @_; | |
168 | $method ||= "digest"; | |
169 | #print "$file $method\n"; | |
170 | ||
171 | open(FILE, $file) or die "Can't open $file: $!"; | |
3357b1b1 JH |
172 | my $digest = Digest::MD5->new->addfile(*FILE)->$method(); |
173 | close(FILE); | |
174 | ||
175 | $digest; | |
176 | } | |
177 | ||
178 | sub cat_file | |
179 | { | |
180 | my($file) = @_; | |
181 | local $/; # slurp | |
182 | open(FILE, $file) or die "Can't open $file: $!"; | |
19f0e64f | 183 | |
9a03235d GA |
184 | # For PerlIO in case of UTF-8 locales. |
185 | eval 'binmode(FILE, ":bytes")' if $] >= 5.008; | |
19f0e64f | 186 | |
3357b1b1 JH |
187 | my $tmp = <FILE>; |
188 | close(FILE); | |
189 | $tmp; | |
190 | } | |
191 |