Upgrade to Digest-SHA-5.47
[p5sagit/p5-mst-13.2.git] / ext / Digest / SHA / t / methods.t
1 use strict;
2 use FileHandle;
3
4 my $MODULE;
5
6 BEGIN {
7         $MODULE = ($ENV{PERL_CORE} || -d "src") ? "Digest::SHA" : "Digest::SHA::PurePerl";
8         eval "require $MODULE" || die $@;
9         $MODULE->import(qw());
10 }
11
12 BEGIN {
13         if ($ENV{PERL_CORE}) {
14                 chdir 't' if -d 't';
15                 @INC = '../lib';
16         }
17 }
18
19 my @out = (
20         "ungWv48Bz+pBQUDeXa4iI7ADYaOWF3qctBD/YfIAFa0",
21         "248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1",
22 );
23
24 my $numtests = 6 + scalar @out;
25 print "1..$numtests\n";
26
27         # attempt to use an invalid algorithm, and check for failure
28
29 my $testnum = 1;
30 my $NSA = "SHA-42";     # No Such Algorithm
31 print "not " if $MODULE->new($NSA);
32 print "ok ", $testnum++, "\n";
33
34 my $tempfile = "methods.tmp";
35 END { 1 while unlink $tempfile }
36
37         # test OO methods using first two SHA-256 vectors from NIST
38
39 my $fh = FileHandle->new($tempfile, "w");
40 binmode($fh);
41 print $fh "bcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
42 $fh->close;
43
44 my $sha = $MODULE->new()->reset("SHA-256")->new();
45 $sha->add_bits("a", 5)->add_bits("001");
46
47 my $rsp = shift(@out);
48 print "not " unless $sha->clone->add("b", "c")->b64digest eq $rsp;
49 print "ok ", $testnum++, "\n";
50
51 $rsp = shift(@out);
52
53         # test addfile with bareword filehandle
54
55 open(FILE, "<$tempfile");
56 binmode(FILE);
57 print "not " unless
58         $sha->clone->addfile(*FILE)->hexdigest eq $rsp;
59 print "ok ", $testnum++, "\n";
60 close(FILE);
61
62         # test addfile with indirect filehandle
63
64 $fh = FileHandle->new($tempfile, "r");
65 binmode($fh);
66 print "not " unless $sha->clone->addfile($fh)->hexdigest eq $rsp;
67 print "ok ", $testnum++, "\n";
68 $fh->close;
69
70         # test addfile using file name instead of handle
71
72 print "not " unless $sha->addfile($tempfile, "b")->hexdigest eq $rsp;
73 print "ok ", $testnum++, "\n";
74
75         # test addfile portable mode
76
77 $fh = FileHandle->new($tempfile, "w");
78 binmode($fh);
79 print $fh "abc\012" x 2048;             # using UNIX newline
80 $fh->close;
81
82 print "not " unless $sha->new(1)->addfile($tempfile, "p")->hexdigest eq
83         "d449e19c1b0b0c191294c8dc9fa2e4a6ff77fc51";
84 print "ok ", $testnum++, "\n";
85
86 $fh = FileHandle->new($tempfile, "w");
87 binmode($fh);
88 print $fh "abc\015\012" x 2048;         # using DOS/Windows newline
89 $fh->close;
90
91 print "not " unless $sha->new(1)->addfile($tempfile, "p")->hexdigest eq
92         "d449e19c1b0b0c191294c8dc9fa2e4a6ff77fc51";
93 print "ok ", $testnum++, "\n";
94
95 $fh = FileHandle->new($tempfile, "w");
96 binmode($fh);
97 print $fh "abc\015" x 2048;             # using early-Mac newline
98 $fh->close;
99
100 print "not " unless $sha->new(1)->addfile($tempfile, "p")->hexdigest eq
101         "d449e19c1b0b0c191294c8dc9fa2e4a6ff77fc51";
102 print "ok ", $testnum++, "\n";