test: Actually ship the test keyring file.
[catacomb-perl] / t / key.t
CommitLineData
fcd15e0b 1# -*-mode: perl; comment-column: 68-*-
2use Test;
3BEGIN { plan tests => 19; }
4use Catacomb qw(:const mp);
5
6sub dumphash {
7 my ($n, $h) = @_;
8 print "# $n\n";
9 foreach my $k (keys %$h) {
10 print "# $k -> $h->{$k}\n";
11 }
12}
13
14sub checkhash {
15 my ($t, $r) = @_;
16 my @t = sort keys(%$t);
17 my @r = sort keys(%$r);
18 unless (@t == @r) {
19 print "# key count: ", scalar(@t), " != ", scalar(@r), "\n";
20 dumphash "t", $t;
21 dumphash "r", $r;
22 return undef;
23 }
24 for (my $i = 0; $i < @t; $i++) {
25 unless ($t[$i] eq $r[$i] && $t->{$t[$i]} eq $r->{$r[$i]}) {
26 print "# hash: $t[$i] -> $t->{$t[$i]} != $r[$i] -> $r->{$r[$i]}\n";
27 dumphash "t", $t;
28 dumphash "r", $r;
29 return undef;
30 }
31 }
32 return 1;
33}
34
8c042512
MW
35system "cp keyring.test keyring";
36
fcd15e0b 37# Simple stuff
38$f = Catacomb::Key::File->new("keyring"); ok defined $f; #t 1
39$k = $f->bytag("tux"); ok defined $k; #t 2
40$d = $k->data(); ok defined $d; #t 3
41ok $d->flags() == KENC_STRUCT; #t 4
bfdf19cb 42$h = $d->open();
fcd15e0b 43ok exists $h->{"p"}; #t 5
44ok !exists $h->{"bogus"}; #t 6
45
46($C, undef, $r) =
bfdf19cb 47 Catacomb::EC::Curve->getinfo($h->{"curve"}->str());
48$p = $C->pt($h->{"p"}->ec());
fcd15e0b 49ok +($p * $r)->atinfp(); #t 7
50
51$h = $k->attrs;
52ok checkhash $h, { #t 8
53 "hash" => "sha256",
54 "mac" => "sha256-hmac/128",
55 "cipher" => "blowfish-cbc"
56};
57
58
bfdf19cb 59($n, $k, $d) = $f->qtag("rsa.private");
fcd15e0b 60ok $k->type, "rsa"; #t 9
61ok $d->flags == KENC_ENCRYPT; #t 10
62ok $n, sprintf("%08x:rsa.private", $k->id); #t 11
63
bfdf19cb 64$h = $f->bytag("rsa")->data()->find("private")
65 ->unlock("pass")->open();
fcd15e0b 66ok defined $h; #t 12
67
68
69# Key data
70($kd, $rest) = Catacomb::Key::Data->read
71 ("struct:[p=integer,public:23,q=integer,public:11],zqzqv");
72ok $rest, ",zqzqv"; #t 13
73ok defined $kd; #t 14
bfdf19cb 74$h = $kd->open(); ok defined $h; #t 15
75ok $h->{"p"}->mp() == 23; #t 16
76ok $h->{"q"}->mp() == 11; #t 17
fcd15e0b 77$pkd = $kd->lock("passphrase");
78ok !defined $pkd->unlock("wrong"); #t 18
79$ukd = $pkd->unlock("passphrase"); ok defined $ukd; #t 19