-
Notifications
You must be signed in to change notification settings - Fork 2
/
pgp_test.go
166 lines (143 loc) · 3.48 KB
/
pgp_test.go
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package fugl
import (
"bytes"
"fmt"
"golang.org/x/crypto/openpgp"
"golang.org/x/crypto/openpgp/armor"
"testing"
)
var (
pair keypair
)
func init() {
p, err := newKeypair()
if err != nil {
panic(fmt.Sprintf("error creating pgp keys = %v", err))
}
if len(p.public) == 0 || len(p.private) == 0 {
panic(fmt.Sprint("public (len=%d) or private (len=%d) keys don't contain data", len(p.public), len(p.private)))
}
pair = p
}
func TestPGP__LoadKeypair(t *testing.T) {
// private
priv, err := PGPLoadPrivateKey([]byte(pair.private))
if err != nil {
t.Fatalf("error reading private key, err=%v", err)
}
if priv == nil {
t.Fatalf("private key is read as nil")
}
// public
pub, err := PGPLoadPublicKey([]byte(pair.public))
if err != nil {
t.Fatalf("error reading public key, err=%v", err)
}
if pub == nil {
t.Fatalf("public key is read as nil")
}
}
func TestPGP__LoadInvalidKeypair(t *testing.T) {
// private
priv, err := PGPLoadPrivateKey([]byte(""))
if err == nil {
t.Fatal("no error reading invalid key")
}
if priv != nil {
t.Fatal("given a non-nil private key")
}
// public
pub, err := PGPLoadPublicKey([]byte(""))
if err == nil {
t.Fatal("no error reading invalid pub key")
}
if pub != nil {
t.Fatal("given a non-nil public key")
}
}
func TestPGP__SignAndVerify(t *testing.T) {
// ignore errors, they should be picked up by __LoadKeypair
pub, _ := PGPLoadPublicKey([]byte(pair.public))
priv, _ := PGPLoadPrivateKey([]byte(pair.private))
// sign and verify a message
message := []byte("this is a test message")
sig, err := PGPSign(priv, message)
if err != nil {
t.Fatalf("error signing message, err=%v", err)
}
if len(sig) == 0 {
t.Fatal("empty signature created")
}
block, err := PGPVerify(pub, []byte(sig))
if err != nil {
t.Fatalf("error verifying signature, err=%v", err)
}
if block == nil || len(block.Bytes) == 0 {
t.Fatal("verify block is empty")
}
}
func TestPGP__InvalidSignAndVerify(t *testing.T) {
pub, _ := PGPLoadPublicKey([]byte(pair.public))
// sign and verify a message
message := []byte("this is a test message")
sig, err := PGPSign(pub, message)
if err == nil {
t.Fatal("expected an error when signing with wrong key")
}
if len(sig) != 0 {
t.Fatal("should not get signature on error")
}
block, err := PGPVerify(pub, []byte(""))
if err == nil {
t.Fatal("expected an error verifying an invalid signature")
}
if block != nil {
t.Fatal("should not get a block on invalid verify")
}
}
// helper functions to generate keys
type keypair struct {
public, private string
}
func (p *keypair) empty() bool {
return len(p.public) == 0 || len(p.private) == 0
}
func newKeypair() (keypair, error) {
p := keypair{}
entity, err := openpgp.NewEntity("", "", "", nil)
entity.Subkeys = entity.Subkeys[:0]
if len(entity.Identities) != 1 {
return p, nil
}
// Serialize private key
var secretArmor bytes.Buffer
secArmIn, err := armor.Encode(&secretArmor, openpgp.PrivateKeyType, nil)
if err != nil {
return p, err
}
err = entity.SerializePrivate(secArmIn, nil)
if err != nil {
return p, err
}
err = secArmIn.Close()
if err != nil {
return p, err
}
p.private = secretArmor.String()
// Serialize public key
var publicArmor bytes.Buffer
pubArmIn, err := armor.Encode(&publicArmor, openpgp.PublicKeyType, nil)
if err != nil {
return p, err
}
err = entity.Serialize(pubArmIn)
if err != nil {
return p, err
}
err = pubArmIn.Close()
if err != nil {
return p, err
}
p.public = publicArmor.String()
return p, nil
}