xref: /openbsd-src/gnu/usr.bin/perl/cpan/Digest-SHA/t/bitbuf.t (revision 5759b3d249badf144a6240f7eec4dcf9df003e6b)
1b39c5158Smillertuse strict;
2*5759b3d2Safresh1use Digest::SHA;
3b39c5158Smillert
4b39c5158Smillertmy $numtests = 4;
5b39c5158Smillertprint "1..$numtests\n";
6b39c5158Smillert
7b39c5158Smillert	# Here's the bitstring to test against, and its SHA-1 digest
8b39c5158Smillert
9b39c5158Smillertmy $ONEBITS = pack("B*", "1" x 80000);
10b39c5158Smillertmy $digest = "11003389959355c2773af6b0f36d842fe430ec49";
11b39c5158Smillert
12*5759b3d2Safresh1my $state = Digest::SHA->new("sHa1");
13b39c5158Smillertmy $testnum = 1;
14b39c5158Smillert
15b39c5158Smillert$state->add_bits($ONEBITS, 80000);
16b39c5158Smillertprint "not " unless $state->hexdigest eq $digest;
17b39c5158Smillertprint "ok ", $testnum++, "\n";
18b39c5158Smillert
19b39c5158Smillert	# buffer using a series of increasingly large bitstrings
20b39c5158Smillert
21b39c5158Smillert# Note that (1 + 2 + ... + 399) + 200 = 80000
22b39c5158Smillert
23b39c5158Smillertfor (1 .. 399) {
24b39c5158Smillert	$state->add_bits($ONEBITS, $_);
25b39c5158Smillert}
26b39c5158Smillert$state->add_bits($ONEBITS, 200);
27b39c5158Smillert
28b39c5158Smillertprint "not " unless $state->hexdigest eq $digest;
29b39c5158Smillertprint "ok ", $testnum++, "\n";
30b39c5158Smillert
31b39c5158Smillert	# create a buffer-alignment nuisance
32b39c5158Smillert
33*5759b3d2Safresh1$state = Digest::SHA->new("1");
34b39c5158Smillert
35b39c5158Smillert$state->add_bits($ONEBITS, 1);
36b39c5158Smillertfor (1 .. 99) {
37b39c5158Smillert	$state->add_bits($ONEBITS, 800);
38b39c5158Smillert}
39b39c5158Smillert$state->add_bits($ONEBITS, 799);
40b39c5158Smillert
41b39c5158Smillertprint "not " unless $state->hexdigest eq $digest;
42b39c5158Smillertprint "ok ", $testnum++, "\n";
43b39c5158Smillert
44b39c5158Smillert	# buffer randomly-sized bitstrings
45b39c5158Smillert
46b39c5158Smillertmy $reps = 80000;
47b39c5158Smillertmy $maxbits = 8 * 127;
48b39c5158Smillert
49*5759b3d2Safresh1$state = Digest::SHA->new(1);
50b39c5158Smillert
51b39c5158Smillertwhile ($reps > $maxbits) {
52b39c5158Smillert	my $num = int(rand($maxbits));
53b39c5158Smillert	$state->add_bits($ONEBITS, $num);
54b39c5158Smillert	$reps -= $num;
55b39c5158Smillert}
56b39c5158Smillert$state->add_bits($ONEBITS, $reps);
57b39c5158Smillert
58b39c5158Smillertprint "not " unless $state->hexdigest eq $digest;
59b39c5158Smillertprint "ok ", $testnum++, "\n";
60