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