1
2
3
4
5 package packet
6
7 import (
8 "bytes"
9 "encoding/hex"
10 "testing"
11 "time"
12 )
13
14 var pubKeyV3Test = struct {
15 hexFingerprint string
16 creationTime time.Time
17 pubKeyAlgo PublicKeyAlgorithm
18 keyId uint64
19 keyIdString string
20 keyIdShort string
21 }{
22 "103BECF5BD1E837C89D19E98487767F7",
23 time.Unix(779753634, 0),
24 PubKeyAlgoRSA,
25 0xDE0F188A5DA5E3C9,
26 "DE0F188A5DA5E3C9",
27 "5DA5E3C9"}
28
29 func TestPublicKeyV3Read(t *testing.T) {
30 i, test := 0, pubKeyV3Test
31 packet, err := Read(v3KeyReader(t))
32 if err != nil {
33 t.Fatalf("#%d: Read error: %s", i, err)
34 }
35 pk, ok := packet.(*PublicKeyV3)
36 if !ok {
37 t.Fatalf("#%d: failed to parse, got: %#v", i, packet)
38 }
39 if pk.PubKeyAlgo != test.pubKeyAlgo {
40 t.Errorf("#%d: bad public key algorithm got:%x want:%x", i, pk.PubKeyAlgo, test.pubKeyAlgo)
41 }
42 if !pk.CreationTime.Equal(test.creationTime) {
43 t.Errorf("#%d: bad creation time got:%v want:%v", i, pk.CreationTime, test.creationTime)
44 }
45 expectedFingerprint, _ := hex.DecodeString(test.hexFingerprint)
46 if !bytes.Equal(expectedFingerprint, pk.Fingerprint[:]) {
47 t.Errorf("#%d: bad fingerprint got:%x want:%x", i, pk.Fingerprint[:], expectedFingerprint)
48 }
49 if pk.KeyId != test.keyId {
50 t.Errorf("#%d: bad keyid got:%x want:%x", i, pk.KeyId, test.keyId)
51 }
52 if g, e := pk.KeyIdString(), test.keyIdString; g != e {
53 t.Errorf("#%d: bad KeyIdString got:%q want:%q", i, g, e)
54 }
55 if g, e := pk.KeyIdShortString(), test.keyIdShort; g != e {
56 t.Errorf("#%d: bad KeyIdShortString got:%q want:%q", i, g, e)
57 }
58 }
59
60 func TestPublicKeyV3Serialize(t *testing.T) {
61
62 i := 0
63 packet, err := Read(v3KeyReader(t))
64 if err != nil {
65 t.Fatalf("#%d: Read error: %s", i, err)
66 }
67 pk, ok := packet.(*PublicKeyV3)
68 if !ok {
69 t.Fatalf("#%d: failed to parse, got: %#v", i, packet)
70 }
71 var serializeBuf bytes.Buffer
72 if err = pk.Serialize(&serializeBuf); err != nil {
73 t.Fatalf("#%d: failed to serialize: %s", i, err)
74 }
75
76 if packet, err = Read(bytes.NewBuffer(serializeBuf.Bytes())); err != nil {
77 t.Fatalf("#%d: Read error (from serialized data): %s", i, err)
78 }
79 if pk, ok = packet.(*PublicKeyV3); !ok {
80 t.Fatalf("#%d: failed to parse serialized data, got: %#v", i, packet)
81 }
82 }
83
View as plain text