chiark / gitweb /
infra: Add a copy of the GPL.
[catacomb-perl] / t / key.t
1 # -*-mode: perl; comment-column: 68-*-
2 use Test;
3 BEGIN { plan tests => 19; }
4 use Catacomb qw(:const mp);
5
6 sub dumphash {
7   my ($n, $h) = @_;
8   print "# $n\n";
9   foreach my $k (keys %$h) {
10     print "#   $k -> $h->{$k}\n";
11   }
12 }
13
14 sub 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
35 system "cp keyring.test keyring";
36
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
41 ok $d->flags() == KENC_STRUCT;                                      #t    4
42 $h = $d->open();
43 ok exists $h->{"p"};                                                #t    5
44 ok !exists $h->{"bogus"};                                           #t    6
45
46 ($C, undef, $r) =
47   Catacomb::EC::Curve->getinfo($h->{"curve"}->str());
48 $p = $C->pt($h->{"p"}->ec());
49 ok +($p * $r)->atinfp();                                            #t    7
50
51 $h = $k->attrs;
52 ok checkhash $h, {                                                  #t    8
53   "hash" => "sha256",
54   "mac" => "sha256-hmac/128",
55   "cipher" => "blowfish-cbc"
56 };
57
58
59 ($n, $k, $d) = $f->qtag("rsa.private");
60 ok $k->type, "rsa";                                                 #t    9
61 ok $d->flags == KENC_ENCRYPT;                                       #t   10
62 ok $n, sprintf("%08x:rsa.private", $k->id);                         #t   11
63
64 $h = $f->bytag("rsa")->data()->find("private")
65   ->unlock("pass")->open();
66 ok 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");
72 ok $rest, ",zqzqv";                                                 #t   13
73 ok defined $kd;                                                     #t   14
74 $h = $kd->open(); ok defined $h;                                    #t   15
75 ok $h->{"p"}->mp() == 23;                                           #t   16
76 ok $h->{"q"}->mp() == 11;                                           #t   17
77 $pkd = $kd->lock("passphrase");
78 ok !defined $pkd->unlock("wrong");                                  #t   18
79 $ukd = $pkd->unlock("passphrase"); ok defined $ukd;                 #t   19