-
Notifications
You must be signed in to change notification settings - Fork 3
/
hkp_test.go
247 lines (221 loc) · 9.58 KB
/
hkp_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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package hkp_test
import (
"bytes"
"net/http/httptest"
"reflect"
"strings"
"testing"
"time"
"github.com/ProtonMail/go-crypto/openpgp"
hkp "github.com/emersion/go-openpgp-hkp"
)
var stallmanPubkey openpgp.EntityList
func init() {
var err error
stallmanPubkey, err = openpgp.ReadArmoredKeyRing(strings.NewReader(stallmanPubkeyStr))
if err != nil {
panic(err)
}
}
type mockBackend struct {
added openpgp.EntityList
}
func (mb *mockBackend) Get(req *hkp.LookupRequest) (openpgp.EntityList, error) {
if req.Search != "stallman" {
return nil, nil
}
return stallmanPubkey, nil
}
func (mb *mockBackend) Index(req *hkp.LookupRequest) ([]hkp.IndexKey, error) {
if req.Search != "stallman" {
return nil, nil
}
keys := make([]hkp.IndexKey, len(stallmanPubkey))
for i, e := range stallmanPubkey {
key, err := hkp.IndexKeyFromEntity(e)
if err != nil {
return nil, err
}
keys[i] = *key
}
return keys, nil
}
func (mb *mockBackend) Add(keys openpgp.EntityList) error {
mb.added = append(mb.added, keys...)
return nil
}
func Test_index(t *testing.T) {
h := hkp.Handler{Lookuper: &mockBackend{}}
ts := httptest.NewServer(&h)
defer ts.Close()
c := hkp.Client{Host: ts.URL, Insecure: true}
req := hkp.LookupRequest{Search: "stallman"}
index, err := c.Index(&req)
if err != nil {
t.Fatalf("Client.Index(): %v", err)
}
creationTime, _ := time.Parse(time.RFC3339, "2013-07-20T18:32:38+02:00")
key := hkp.IndexKey{
CreationTime: creationTime.Local(),
Algo: 1,
Fingerprint: stallmanPubkey[0].PrimaryKey.Fingerprint,
BitLength: 4096,
Flags: 0,
Identities: []hkp.IndexIdentity{
{
Name: "Richard Stallman <rms@gnu.org>",
CreationTime: creationTime.Local(),
Flags: 0,
},
},
}
if len(index) != 1 {
t.Errorf("Client.Index: got %v results, want 1", len(index))
} else if !reflect.DeepEqual(index[0], key) {
t.Errorf("Client.Index: got %+v, want %+v", index[0], key)
}
}
func Test_get(t *testing.T) {
h := hkp.Handler{Lookuper: &mockBackend{}}
ts := httptest.NewServer(&h)
defer ts.Close()
c := hkp.Client{Host: ts.URL, Insecure: true}
req := hkp.LookupRequest{Search: "stallman"}
keys, err := c.Get(&req)
if err != nil {
t.Fatalf("Client.Get(): %v", err)
}
if len(keys) != 1 {
t.Errorf("Client.Get: got %v key, want 1", len(keys))
} else if !bytes.Equal(keys[0].PrimaryKey.Fingerprint[:], stallmanPubkey[0].PrimaryKey.Fingerprint[:]) {
t.Errorf("Client.Get: got %+v, want %+v", keys[0], stallmanPubkey[0])
}
}
func Test_add(t *testing.T) {
mb := mockBackend{}
h := hkp.Handler{Adder: &mb}
ts := httptest.NewServer(&h)
defer ts.Close()
c := hkp.Client{Host: ts.URL, Insecure: true}
if err := c.Add(stallmanPubkey); err != nil {
t.Fatalf("Client.Add(): %v", err)
}
if len(mb.added) != 1 {
t.Errorf("want 1 key added, got %v", len(mb.added))
}
}
func TestKeyIDSearch(t *testing.T) {
shortKeyIDSearch := hkp.ParseKeyIDSearch("0x2A8E4C02")
if id := shortKeyIDSearch.KeyIdShort(); id == nil {
t.Errorf("short.KeyIdShort() = nil, want non-nil")
} else if *id != 0x2A8E4C02 {
t.Errorf("short.KeyIdShort() = 0x%X, want 0x%X", *id, 0x2A8E4C02)
}
if fingerprint := shortKeyIDSearch.Fingerprint(); fingerprint != nil {
t.Errorf("short.Fingerprint() = %v, want nil", fingerprint)
}
fingerprintIDSearch := hkp.ParseKeyIDSearch("0x67819B343B2AB70DED9320872C6464AF2A8E4C02")
if id := fingerprintIDSearch.KeyId(); id == nil {
t.Errorf("fingerprint.KeyId() = nil, want non-nil")
} else if *id != 0x2C6464AF2A8E4C02 {
t.Errorf("fingerprint.KeyId() = 0x%X, want 0x%X", id, 0x2C6464AF2A8E4C02)
}
if fingerprint := fingerprintIDSearch.Fingerprint(); fingerprint == nil {
t.Errorf("fingerprint.Fingerprint() = nil, want non-nil")
} else if !bytes.Equal((fingerprint)[:], stallmanPubkey[0].PrimaryKey.Fingerprint[:]) {
t.Errorf("fingerprint.Fingerprint() = %v, want %v", fingerprint, stallmanPubkey[0].PrimaryKey.Fingerprint)
}
}
const stallmanPubkeyStr = `-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: GnuPG v1.4.10 (GNU/Linux)
mQINBFHqu6YBEAC/f9aXkt2t+58gGhQiInr3yK/uhQYtmTwxvVVVAEcorRhjMFjC
1PhFsJ7qh0oiCKs7YGh5YSuGTR4YWrF9qS7BzJJNWiu+sFmVPTHiiJ4OoFx4f4dM
9Cl+k3I+orPSuTv5LkMz3omBwl8bt/zPxAeOMV1h6H87zKjTvRdt8K0/XOKuP83d
8pK8gHhIPIBsrQ5YhGImyT8Ni+ffZnjm7IApFKqDJSeMWJ0qJrefwC92i2H/eYcf
LGo/R7VZec9S5Y8xvMejzey9jwPWaQ/Nrxkl2wicg8A3QB4zkqfC61EUGXQr3DE4
fCFv8C5osmiO5kcrMOXZ4GvX4A3CB8O5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