xref: /openbsd-src/gnu/usr.bin/perl/cpan/Digest-MD5/t/files.t (revision fac98b93b71777a71b1e912ccaf68ce33d7b87c4)
1eac174f2Safresh1use strict;
2eac174f2Safresh1use warnings;
3eac174f2Safresh1
4eac174f2Safresh1use Digest::MD5 qw(md5 md5_hex md5_base64);
548950c12Ssthen
6b39c5158Smillertprint "1..3\n";
7b39c5158Smillert
8b39c5158Smillert# To update the EBCDIC section even on a Latin 1 platform,
9b39c5158Smillert# run this script with $ENV{EBCDIC_MD5SUM} set to a true value.
10b39c5158Smillert# (You'll need to have Perl 5.7.3 or later, to have the Encode installed.)
11b39c5158Smillert# (And remember that under the Perl core distribution you should
12b39c5158Smillert#  also have the $ENV{PERL_CORE} set to a true value.)
13b39c5158Smillert
14b39c5158Smillertmy $EXPECT;
15b39c5158Smillertif (ord "A" == 193) { # EBCDIC
16b39c5158Smillert    $EXPECT = <<EOT;
17e9ce3842Safresh10956ffb4f6416082b27d6680b4cf73fc  README
18eac174f2Safresh13fce99bf3f4df26d65843a6990849df0  MD5.xs
19b39c5158Smillert276da0aa4e9a08b7fe09430c9c5690aa  rfc1321.txt
20b39c5158SmillertEOT
21b39c5158Smillert} else {
22b39c5158Smillert    # This is the output of: 'md5sum README MD5.xs rfc1321.txt'
23b39c5158Smillert    $EXPECT = <<EOT;
24e9ce3842Safresh12f93400875dbb56f36691d5f69f3eba5  README
25*fac98b93Safresh116d90fd139c5eae51f786daa1ea6eb24  MD5.xs
26b39c5158Smillert754b9db19f79dbc4992f7166eb0f37ce  rfc1321.txt
27b39c5158SmillertEOT
28b39c5158Smillert}
29b39c5158Smillert
30b39c5158Smillertif (!(-f "README") && -f "../README") {
31b39c5158Smillert   chdir("..") or die "Can't chdir: $!";
32b39c5158Smillert}
33b39c5158Smillert
34b39c5158Smillertmy $testno = 0;
35b39c5158Smillert
36b39c5158Smillertmy $B64 = 1;
37b39c5158Smillerteval { require MIME::Base64; };
38b39c5158Smillertif ($@) {
39b39c5158Smillert    print "# $@: Will not test base64 methods\n";
40b39c5158Smillert    $B64 = 0;
41b39c5158Smillert}
42b39c5158Smillert
43b39c5158Smillertfor (split /^/, $EXPECT) {
44b39c5158Smillert     my($md5hex, $file) = split ' ';
45b39c5158Smillert     my $base = $file;
46b39c5158Smillert#     print "# $base\n";
47b39c5158Smillert     if ($ENV{PERL_CORE}) {
48e5157e49Safresh1         # Don't have these in core.
49e5157e49Safresh1         if ($file eq 'rfc1321.txt' or $file eq 'README') {
50b39c5158Smillert	     print "ok ", ++$testno, " # Skip: PERL_CORE\n";
51b39c5158Smillert	     next;
52b39c5158Smillert	 }
53b39c5158Smillert     }
54b39c5158Smillert#     print "# file = $file\n";
55b39c5158Smillert     unless (-f $file) {
56b39c5158Smillert	warn "No such file: $file\n";
57b39c5158Smillert	next;
58b39c5158Smillert     }
59b39c5158Smillert     if ($ENV{EBCDIC_MD5SUM}) {
60b39c5158Smillert         require Encode;
61b39c5158Smillert	 my $data = cat_file($file);
62b39c5158Smillert	 Encode::from_to($data, 'latin1', 'cp1047');
63b39c5158Smillert	 print md5_hex($data), "  $base\n";
64b39c5158Smillert	 next;
65b39c5158Smillert     }
66b39c5158Smillert     my $md5bin = pack("H*", $md5hex);
67b39c5158Smillert     my $md5b64;
68b39c5158Smillert     if ($B64) {
69b39c5158Smillert	 $md5b64 = MIME::Base64::encode($md5bin, "");
70b39c5158Smillert	 chop($md5b64); chop($md5b64);   # remove padding
71b39c5158Smillert     }
72b39c5158Smillert     my $failed;
73b39c5158Smillert     my $got;
74b39c5158Smillert
75b39c5158Smillert     if (digest_file($file, 'digest') ne $md5bin) {
76b39c5158Smillert	 print "$file: Bad digest\n";
77b39c5158Smillert	 $failed++;
78b39c5158Smillert     }
79b39c5158Smillert
80b39c5158Smillert     if (($got = digest_file($file, 'hexdigest')) ne $md5hex) {
81b39c5158Smillert	 print "$file: Bad hexdigest: got $got expected $md5hex\n";
82b39c5158Smillert	 $failed++;
83b39c5158Smillert     }
84b39c5158Smillert
85b39c5158Smillert     if ($B64 && digest_file($file, 'b64digest') ne $md5b64) {
86b39c5158Smillert	 print "$file: Bad b64digest\n";
87b39c5158Smillert	 $failed++;
88b39c5158Smillert     }
89b39c5158Smillert
90b39c5158Smillert     my $data = cat_file($file);
91b39c5158Smillert     if (md5($data) ne $md5bin) {
92b39c5158Smillert	 print "$file: md5() failed\n";
93b39c5158Smillert	 $failed++;
94b39c5158Smillert     }
95b39c5158Smillert     if (md5_hex($data) ne $md5hex) {
96b39c5158Smillert	 print "$file: md5_hex() failed\n";
97b39c5158Smillert	 $failed++;
98b39c5158Smillert     }
99b39c5158Smillert     if ($B64 && md5_base64($data) ne $md5b64) {
100b39c5158Smillert	 print "$file: md5_base64() failed\n";
101b39c5158Smillert	 $failed++;
102b39c5158Smillert     }
103b39c5158Smillert
104b39c5158Smillert     if (Digest::MD5->new->add($data)->digest ne $md5bin) {
105b39c5158Smillert	 print "$file: MD5->new->add(...)->digest failed\n";
106b39c5158Smillert	 $failed++;
107b39c5158Smillert     }
108b39c5158Smillert     if (Digest::MD5->new->add($data)->hexdigest ne $md5hex) {
109b39c5158Smillert	 print "$file: MD5->new->add(...)->hexdigest failed\n";
110b39c5158Smillert	 $failed++;
111b39c5158Smillert     }
112b39c5158Smillert     if ($B64 && Digest::MD5->new->add($data)->b64digest ne $md5b64) {
113b39c5158Smillert	 print "$file: MD5->new->add(...)->b64digest failed\n";
114b39c5158Smillert	 $failed++;
115b39c5158Smillert     }
116b39c5158Smillert
117b39c5158Smillert     my @data = split //, $data;
118b39c5158Smillert     if (md5(@data) ne $md5bin) {
119b39c5158Smillert	 print "$file: md5(\@data) failed\n";
120b39c5158Smillert	 $failed++;
121b39c5158Smillert     }
122b39c5158Smillert     if (Digest::MD5->new->add(@data)->digest ne $md5bin) {
123b39c5158Smillert	 print "$file: MD5->new->add(\@data)->digest failed\n";
124b39c5158Smillert	 $failed++;
125b39c5158Smillert     }
126b39c5158Smillert     my $md5 = Digest::MD5->new;
127b39c5158Smillert     for (@data) {
128b39c5158Smillert	 $md5->add($_);
129b39c5158Smillert     }
130b39c5158Smillert     if ($md5->digest ne $md5bin) {
131b39c5158Smillert	 print "$file: $md5->add()-loop failed\n";
132b39c5158Smillert	 $failed++;
133b39c5158Smillert     }
134b39c5158Smillert
135b39c5158Smillert     print "not " if $failed;
136b39c5158Smillert     print "ok ", ++$testno, "\n";
137b39c5158Smillert}
138b39c5158Smillert
139b39c5158Smillert
140b39c5158Smillertsub digest_file
141b39c5158Smillert{
142b39c5158Smillert    my($file, $method) = @_;
143b39c5158Smillert    $method ||= "digest";
144b39c5158Smillert    #print "$file $method\n";
145b39c5158Smillert
146b39c5158Smillert    open(FILE, $file) or die "Can't open $file: $!";
147b39c5158Smillert    my $digest = Digest::MD5->new->addfile(*FILE)->$method();
148b39c5158Smillert    close(FILE);
149b39c5158Smillert
150b39c5158Smillert    $digest;
151b39c5158Smillert}
152b39c5158Smillert
153b39c5158Smillertsub cat_file
154b39c5158Smillert{
155b39c5158Smillert    my($file) = @_;
156b39c5158Smillert    local $/;  # slurp
157b39c5158Smillert    open(FILE, $file) or die "Can't open $file: $!";
158b39c5158Smillert
159b39c5158Smillert    # For PerlIO in case of UTF-8 locales.
160b39c5158Smillert    eval 'binmode(FILE, ":bytes")' if $] >= 5.008;
161b39c5158Smillert
162b39c5158Smillert    my $tmp = <FILE>;
163b39c5158Smillert    close(FILE);
164b39c5158Smillert    $tmp;
165b39c5158Smillert}
166b39c5158Smillert
167