File: methods.t

package info (click to toggle)
perl 5.20.2-3%2Bdeb8u11
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 102,964 kB
  • sloc: perl: 555,553; ansic: 214,041; sh: 38,121; pascal: 8,783; cpp: 3,895; makefile: 2,393; xml: 2,325; yacc: 1,741
file content (121 lines) | stat: -rw-r--r-- 3,133 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
use strict;
use FileHandle;

my $MODULE;

BEGIN {
	$MODULE = (-d "src") ? "Digest::SHA" : "Digest::SHA::PurePerl";
	eval "require $MODULE" || die $@;
	$MODULE->import(qw());
}

BEGIN {
	if ($ENV{PERL_CORE}) {
		chdir 't' if -d 't';
		@INC = '../lib';
	}
}

my @out = (
	"ungWv48Bz+pBQUDeXa4iI7ADYaOWF3qctBD/YfIAFa0",
	"248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1",
);

my $numtests = 8 + scalar @out;
print "1..$numtests\n";

	# attempt to use an invalid algorithm, and check for failure

my $testnum = 1;
my $NSA = "SHA-42";	# No Such Algorithm
print "not " if $MODULE->new($NSA);
print "ok ", $testnum++, "\n";

my $tempfile = "methods.tmp";
END { 1 while unlink $tempfile }

	# test OO methods using first two SHA-256 vectors from NIST

my $fh = FileHandle->new($tempfile, "w");
binmode($fh);
print $fh "bcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
$fh->close;

my $sha = $MODULE->new()->reset("SHA-256")->new();
$sha->add_bits("a", 5)->add_bits("001");

my $rsp = shift(@out);
print "not " unless $sha->clone->add("b", "c")->b64digest eq $rsp;
print "ok ", $testnum++, "\n";

$rsp = shift(@out);

	# test addfile with bareword filehandle

open(FILE, "<$tempfile");
binmode(FILE);
print "not " unless
	$sha->clone->addfile(*FILE)->hexdigest eq $rsp;
print "ok ", $testnum++, "\n";
close(FILE);

	# test addfile with indirect filehandle

$fh = FileHandle->new($tempfile, "r");
binmode($fh);
print "not " unless $sha->clone->addfile($fh)->hexdigest eq $rsp;
print "ok ", $testnum++, "\n";
$fh->close;

	# test addfile using file name instead of handle

print "not " unless $sha->addfile($tempfile, "b")->hexdigest eq $rsp;
print "ok ", $testnum++, "\n";

	# test addfile portable mode

$fh = FileHandle->new($tempfile, "w");
binmode($fh);
print $fh "abc\012" x 2048;		# using UNIX newline
$fh->close;

print "not " unless $sha->new(1)->addfile($tempfile, "p")->hexdigest eq
	"d449e19c1b0b0c191294c8dc9fa2e4a6ff77fc51";
print "ok ", $testnum++, "\n";

$fh = FileHandle->new($tempfile, "w");
binmode($fh);
print $fh "abc\015\012" x 2048;		# using DOS/Windows newline
$fh->close;

print "not " unless $sha->new(1)->addfile($tempfile, "p")->hexdigest eq
	"d449e19c1b0b0c191294c8dc9fa2e4a6ff77fc51";
print "ok ", $testnum++, "\n";

$fh = FileHandle->new($tempfile, "w");
binmode($fh);
print $fh "abc\015" x 2048;		# using early-Mac newline
$fh->close;

print "not " unless $sha->new(1)->addfile($tempfile, "p")->hexdigest eq
	"d449e19c1b0b0c191294c8dc9fa2e4a6ff77fc51";
print "ok ", $testnum++, "\n";

	# test addfile BITS mode

$fh = FileHandle->new($tempfile, "w");
print $fh "0100010";			# using NIST 7-bit test vector
$fh->close;

print "not " unless $sha->new(1)->addfile($tempfile, "0")->hexdigest eq
	"04f31807151181ad0db278a1660526b0aeef64c2";
print "ok ", $testnum++, "\n";

$fh = FileHandle->new($tempfile, "w");
binmode($fh);
print $fh map(chr, (0..127));		# this is actually NIST 2-bit test
$fh->close;				# vector "01" (other chars ignored)

print "not " unless $sha->new(1)->addfile($tempfile, "0")->hexdigest eq
	"ec6b39952e1a3ec3ab3507185cf756181c84bbe2";
print "ok ", $testnum++, "\n";