Commit | Line | Data |
6bc89f92 |
1 | use Test; |
2 | use strict; |
3 | use integer; |
4 | use File::Basename qw(dirname); |
5 | use File::Spec; |
6 | use Digest::SHA; |
7 | |
41c686de |
8 | BEGIN { |
77d2a621 |
9 | if ($ENV{PERL_CORE}) { |
10 | chdir 't' if -d 't'; |
11 | @INC = '../lib'; |
12 | } |
41c686de |
13 | } |
14 | |
6bc89f92 |
15 | my(@vec); |
16 | |
77d2a621 |
17 | BEGIN { |
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 | |
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 | |
cccd5831 |
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); |
6bc89f92 |
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); |
c7e5c266 |
46 | |
cccd5831 |
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 |
84c0b84e |
53 | |
cccd5831 |
54 | # test addfile with indirect filehandle |
55 | |
56 | undef($fh); open($fh, q{<}, $file); |
84c0b84e |
57 | binmode($fh); |
58 | ok($ctx->clone->addfile($fh)->hexdigest, $rsp); |
59 | close($fh); |
60 | |
c7e5c266 |
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 | |
cccd5831 |
67 | undef($fh); open($fh, q{>}, $file); |
68 | binmode($fh); |
69 | print $fh "abc\012" x 2048; # using UNIX newline |
70 | close($fh); |
c7e5c266 |
71 | |
72 | ok($ctx->new(1)->addfile($file, "p")->hexdigest, |
73 | "d449e19c1b0b0c191294c8dc9fa2e4a6ff77fc51"); |
74 | |
cccd5831 |
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); |
c7e5c266 |
79 | |
80 | ok($ctx->new(1)->addfile($file, "p")->hexdigest, |
81 | "d449e19c1b0b0c191294c8dc9fa2e4a6ff77fc51"); |
82 | |
cccd5831 |
83 | undef($fh); open($fh, q{>}, $file); |
84 | binmode($fh); |
85 | print $fh "abc\015" x 2048; # using Apple/Mac newline |
86 | close($fh); |
c7e5c266 |
87 | |
88 | ok($ctx->new(1)->addfile($file, "p")->hexdigest, |
89 | "d449e19c1b0b0c191294c8dc9fa2e4a6ff77fc51"); |
90 | |
6bc89f92 |
91 | unlink($file); |