Upgrade to Digest-SHA-5.43
[p5sagit/p5-mst-13.2.git] / ext / Digest / SHA / t / 2-nist-sha-oo.t
1 use Test;
2 use strict;
3 use integer;
4 use File::Basename qw(dirname);
5 use File::Spec;
6 use Digest::SHA;
7
8 BEGIN {
9         if ($ENV{PERL_CORE}) {
10                 chdir 't' if -d 't';
11                 @INC = '../lib';
12         }
13 }
14
15 my(@vec);
16
17 BEGIN {
18         @vec = (
19 "ungWv48Bz+pBQUDeXa4iI7ADYaOWF3qctBD/YfIAFa0",
20 "248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1"
21         );
22
23         plan tests => 6 + scalar(@vec);
24 }
25
26         # attempt to use an invalid algorithm, and check for failure
27
28 my $NSA = "SHA-42";     # No Such Algorithm
29 ok(Digest::SHA->new($NSA), undef);
30
31         # test OO methods using first two SHA-256 vectors from NIST
32
33 my $file = File::Spec->catfile(dirname($0), "oo.tmp");
34 open(my $fh, q{>}, $file);
35 binmode($fh);
36 print $fh "bcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
37 close($fh);
38
39 my $ctx = Digest::SHA->new()->reset("SHA-256")->new();
40 $ctx->add_bits("a", 5)->add_bits("001");
41
42 my $rsp = shift(@vec);
43 ok($ctx->clone->add("b", "c")->b64digest, $rsp);
44
45 $rsp = shift(@vec);
46
47         # test addfile with bareword filehandle
48
49 open(FILE, "<$file");                                   ## no critic
50 binmode(FILE);                                          ## no critic
51 ok($ctx->clone->addfile(*FILE)->hexdigest, $rsp);       ## no critic
52 close(FILE);                                            ## no critic
53
54         # test addfile with indirect filehandle
55
56 undef($fh); open($fh, q{<}, $file);
57 binmode($fh);
58 ok($ctx->clone->addfile($fh)->hexdigest, $rsp);
59 close($fh);
60
61         # test addfile using file name instead of handle
62
63 ok($ctx->addfile($file, "b")->hexdigest, $rsp);
64
65         # test addfile portable mode
66
67 undef($fh); open($fh, q{>}, $file);
68 binmode($fh);
69 print $fh "abc\012" x 2048;             # using UNIX newline
70 close($fh);
71
72 ok($ctx->new(1)->addfile($file, "p")->hexdigest,
73         "d449e19c1b0b0c191294c8dc9fa2e4a6ff77fc51");
74
75 undef($fh); open($fh, q{>}, $file);
76 binmode($fh);
77 print $fh "abc\015\012" x 2048;         # using DOS/Windows newline
78 close($fh);
79
80 ok($ctx->new(1)->addfile($file, "p")->hexdigest,
81         "d449e19c1b0b0c191294c8dc9fa2e4a6ff77fc51");
82
83 undef($fh); open($fh, q{>}, $file);
84 binmode($fh);
85 print $fh "abc\015" x 2048;             # using Apple/Mac newline
86 close($fh);
87
88 ok($ctx->new(1)->addfile($file, "p")->hexdigest,
89         "d449e19c1b0b0c191294c8dc9fa2e4a6ff77fc51");
90
91 unlink($file);