Much wider support for Catacomb in all its glory.
[catacomb-perl] / t / key.t
diff --git a/t/key.t b/t/key.t
new file mode 100644 (file)
index 0000000..7a8074f
--- /dev/null
+++ b/t/key.t
@@ -0,0 +1,77 @@
+# -*-mode: perl; comment-column: 68-*-
+use Test;
+BEGIN { plan tests => 19; }
+use Catacomb qw(:const mp);
+
+sub dumphash {
+  my ($n, $h) = @_;
+  print "# $n\n";
+  foreach my $k (keys %$h) {
+    print "#   $k -> $h->{$k}\n";
+  }
+}
+
+sub checkhash {
+  my ($t, $r) = @_;
+  my @t = sort keys(%$t);
+  my @r = sort keys(%$r);
+  unless (@t == @r) {
+    print "# key count: ", scalar(@t), " != ", scalar(@r), "\n";
+    dumphash "t", $t;
+    dumphash "r", $r;
+    return undef;
+  }
+  for (my $i = 0; $i < @t; $i++) {
+    unless ($t[$i] eq $r[$i] && $t->{$t[$i]} eq $r->{$r[$i]}) {
+      print "# hash: $t[$i] -> $t->{$t[$i]} != $r[$i] -> $r->{$r[$i]}\n";
+      dumphash "t", $t;
+      dumphash "r", $r;
+      return undef;
+    }
+  }
+  return 1;
+}
+
+# Simple stuff
+$f = Catacomb::Key::File->new("keyring"); ok defined $f;           #t    1
+$k = $f->bytag("tux"); ok defined $k;                              #t    2
+$d = $k->data(); ok defined $d;                                            #t    3
+ok $d->flags() == KENC_STRUCT;                                     #t    4
+$h = $d->structopen();
+ok exists $h->{"p"};                                               #t    5
+ok !exists $h->{"bogus"};                                          #t    6
+
+($C, undef, $r) =
+  Catacomb::EC::Curve->getinfo($h->{"curve"}->getstring());
+$p = $C->pt($h->{"p"}->getec());
+ok +($p * $r)->atinfp();                                           #t    7
+
+$h = $k->attrs;
+ok checkhash $h, {                                                 #t    8
+  "hash" => "sha256",
+  "mac" => "sha256-hmac/128",
+  "cipher" => "blowfish-cbc"
+};
+
+
+($k, $d, $n) = $f->qtag("rsa.private");
+ok $k->type, "rsa";                                                #t    9
+ok $d->flags == KENC_ENCRYPT;                                      #t   10
+ok $n, sprintf("%08x:rsa.private", $k->id);                        #t   11
+
+$h = $f->bytag("rsa")->data()->structfind("private")
+  ->unlock("pass")->structopen();
+ok defined $h;                                                     #t   12
+
+
+# Key data
+($kd, $rest) = Catacomb::Key::Data->read
+  ("struct:[p=integer,public:23,q=integer,public:11],zqzqv");
+ok $rest, ",zqzqv";                                                #t   13
+ok defined $kd;                                                            #t   14
+$h = $kd->structopen(); ok defined $h;                             #t   15
+ok $h->{"p"}->getmp() == 23;                                       #t   16
+ok $h->{"q"}->getmp() == 11;                                       #t   17
+$pkd = $kd->lock("passphrase");
+ok !defined $pkd->unlock("wrong");                                 #t   18
+$ukd = $pkd->unlock("passphrase"); ok defined $ukd;                #t   19