summaryrefslogtreecommitdiff
path: root/lib/Digest/t/base.t
blob: c398346caceb9aaf0f678ae0e6169e70652f6de4 (plain)
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
#!perl -w

use Test qw(plan ok);
plan tests => 12;

{
   package LenDigest;
   require Digest::base;
   use vars qw(@ISA);
   @ISA = qw(Digest::base);

   sub new {
	my $class = shift;
	my $str = "";
	bless \$str, $class;
   }

   sub add {
	my $self = shift;
	$$self .= join("", @_);
	return $self;
   }

   sub digest {
	my $self = shift;
	my $len = length($$self);
	my $first = ($len > 0) ? substr($$self, 0, 1) : "X";
	$$self = "";
	return sprintf "$first%04d", $len;
   }
}

my $ctx = LenDigest->new;
ok($ctx->digest, "X0000");
ok($ctx->hexdigest, "5830303030");
ok($ctx->b64digest, "WDAwMDA");

$ctx->add("foo");
ok($ctx->digest, "f0003");

$ctx->add("foo");
ok($ctx->hexdigest, "6630303033");

$ctx->add("foo");
ok($ctx->b64digest, "ZjAwMDM");

open(F, ">xxtest$$") || die;
binmode(F);
print F "abc" x 100, "\n";
close(F) || die;

open(F, "xxtest$$") || die;
$ctx->addfile(*F);
close(F);
unlink("xxtest$$") || warn;

ok($ctx->digest, "a0301");

eval {
    $ctx->add_bits("1010");
};
ok($@ =~ /^Number of bits must be multiple of 8/);

$ctx->add_bits("01010101");
ok($ctx->digest, "U0001");

eval {
    $ctx->add_bits("abc", 12);
};
ok($@ =~ /^Number of bits must be multiple of 8/);

$ctx->add_bits("abc", 16);
ok($ctx->digest, "a0002");

$ctx->add_bits("abc", 32);
ok($ctx->digest, "a0003");