Commit | Line | Data |
3357b1b1 |
1 | BEGIN { |
2 | chdir 't' if -d 't'; |
3 | @INC = '../lib'; |
4 | } |
5 | |
6 | print "1..2\n"; |
7 | |
8 | use strict; |
9 | use Digest::MD5 qw(md5 md5_hex); |
10 | |
11 | # |
12 | # This is the output of: 'md5sum MD5.pm MD5.xs' |
13 | # |
fed3f325 |
14 | my $EXPECT; |
15 | |
8c42d64c |
16 | if (ord('A') == 193) { # EBCDIC |
fed3f325 |
17 | $EXPECT = <<EOT; |
8c42d64c |
18 | dcc1f89cf6144d81e5f9d291d9849ef1 ext/Digest/MD5/MD5.pm |
3357b1b1 |
19 | 61debd0ec12e131e1ba220e2f3ad2d26 ext/Digest/MD5/MD5.xs |
20 | EOT |
fed3f325 |
21 | } else { # ASCII |
22 | $EXPECT = <<EOT; |
23 | 9e1d1183ff41717c91a563c41e08d672 ext/Digest/MD5/MD5.pm |
24 | 61debd0ec12e131e1ba220e2f3ad2d26 ext/Digest/MD5/MD5.xs |
25 | EOT |
26 | } |
8c42d64c |
27 | |
3357b1b1 |
28 | my $testno = 0; |
29 | |
30 | use File::Spec; |
31 | |
32 | for (split /^/, $EXPECT) { |
33 | my($md5hex, $file) = split ' '; |
34 | my @path = split(m:/:, $file); |
35 | my $last = pop @path; |
36 | my $path = File::Spec->updir; |
37 | while (@path) { |
38 | $path = File::Spec->catdir($path, shift @path); |
39 | } |
40 | $file = File::Spec->catfile($path, $last); |
41 | my $md5bin = pack("H*", $md5hex); |
42 | my $failed; |
43 | |
44 | if (digest_file($file, 'digest') ne $md5bin) { |
45 | print "$file: Bad digest\n"; |
46 | $failed++; |
47 | } |
48 | |
49 | if (digest_file($file, 'hexdigest') ne $md5hex) { |
50 | print "$file: Bad hexdigest\n"; |
51 | $failed++; |
52 | } |
53 | |
54 | my $data = cat_file($file); |
55 | if (md5($data) ne $md5bin) { |
56 | print "$file: md5() failed\n"; |
57 | $failed++; |
58 | } |
59 | if (md5_hex($data) ne $md5hex) { |
60 | print "$file: md5_hex() failed\n"; |
61 | $failed++; |
62 | } |
63 | |
64 | if (Digest::MD5->new->add($data)->digest ne $md5bin) { |
65 | print "$file: MD5->new->add(...)->digest failed\n"; |
66 | $failed++; |
67 | } |
68 | if (Digest::MD5->new->add($data)->hexdigest ne $md5hex) { |
69 | print "$file: MD5->new->add(...)->hexdigest failed\n"; |
70 | $failed++; |
71 | } |
72 | |
73 | my @data = split //, $data; |
74 | if (md5(@data) ne $md5bin) { |
75 | print "$file: md5(\@data) failed\n"; |
76 | $failed++; |
77 | } |
78 | if (Digest::MD5->new->add(@data)->digest ne $md5bin) { |
79 | print "$file: MD5->new->add(\@data)->digest failed\n"; |
80 | $failed++; |
81 | } |
82 | my $md5 = Digest::MD5->new; |
83 | for (@data) { |
84 | $md5->add($_); |
85 | } |
86 | if ($md5->digest ne $md5bin) { |
87 | print "$file: $md5->add()-loop failed\n"; |
88 | $failed++; |
89 | } |
90 | |
91 | print "not " if $failed; |
92 | print "ok ", ++$testno, "\n"; |
93 | } |
94 | |
95 | |
96 | sub digest_file |
97 | { |
98 | my($file, $method) = @_; |
99 | $method ||= "digest"; |
100 | #print "$file $method\n"; |
101 | |
102 | open(FILE, $file) or die "Can't open $file: $!"; |
103 | binmode(FILE); |
104 | my $digest = Digest::MD5->new->addfile(*FILE)->$method(); |
105 | close(FILE); |
106 | |
107 | $digest; |
108 | } |
109 | |
110 | sub cat_file |
111 | { |
112 | my($file) = @_; |
113 | local $/; # slurp |
114 | open(FILE, $file) or die "Can't open $file: $!"; |
115 | binmode(FILE); |
116 | my $tmp = <FILE>; |
117 | close(FILE); |
118 | $tmp; |
119 | } |
120 | |