-
Notifications
You must be signed in to change notification settings - Fork 1
/
control_test.go
282 lines (243 loc) · 8.46 KB
/
control_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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package main
import (
"bytes"
"strings"
"testing"
"code.google.com/p/go.crypto/openpgp"
)
func TestDebControlInvalid1(t *testing.T) {
_, err := ParseDebianControl(strings.NewReader("Total rubbish"), nil)
if err == nil {
t.Errorf("Non-field value should not parse")
}
}
func TestDebControlInvalid2(t *testing.T) {
_, err := ParseDebianControl(strings.NewReader(" continuation before a field is declared"), nil)
if err == nil {
t.Errorf("Misplaced continuation field should not parse")
}
}
var testDebControlEmptyStart = []string{
"",
"Package: first",
"fieldb: bit of stuff",
"fieldc: and other stuff",
"thiny2: ",
" val1",
" val2",
" val3",
}
var testDebControlEmptyStartResult = []string{
"Package: first",
"fieldb: bit of stuff",
"fieldc: and other stuff",
"thiny2: ",
" val1",
" val2",
" val3",
}
// Check that we output unknown fields in a consistant order
func TestDebControlEmptyStart(t *testing.T) {
inStr := strings.Join(testDebControlEmptyStart, "\n") + "\n"
resStr := strings.Join(testDebControlEmptyStartResult, "\n") + "\n"
paragraphs, err := ParseDebianControl(strings.NewReader(inStr), nil)
if err != nil {
t.Errorf("parsedebiancontrol failure: %s", inStr)
}
var buf bytes.Buffer
debStartFields := []string{"Package", "Version", "Filename", "Directory", "Size"}
debEndFields := []string{"MD5Sum", "MD5sum", "SHA1", "SHA256", "Description"}
WriteDebianControl(&buf, paragraphs, debStartFields, debEndFields)
outStr := string(buf.Bytes())
if outStr != resStr {
t.Errorf("\nExpected:\n%s\nGot:\n%s", inStr, outStr)
}
}
var testDebControlPersist = []string{
"Package: first",
"fieldb: bit of stuff",
"fieldc: and other stuff",
"thiny2: ",
" val1",
" val2",
" val3",
"Description: thingy with",
" multiple lines of",
" stuff",
"",
"Package: second",
"fielda: with a",
"fieldb: bit of stuff",
"fieldc: and other stuff",
"thiny2: ",
" val1",
" val2",
" val3",
"Description: thingy with",
" multiple lines of",
" stuff",
}
// Check that we output unknown fields in a consistant order
func TestDebControlPersist(t *testing.T) {
inStr := strings.Join(testDebControlPersist, "\n") + "\n"
paragraphs, err := ParseDebianControl(strings.NewReader(inStr), nil)
if err != nil {
t.Errorf("parsedebiancontrol failure: %s", inStr)
}
var buf bytes.Buffer
debStartFields := []string{"Package", "Version", "Filename", "Directory", "Size"}
debEndFields := []string{"MD5Sum", "MD5sum", "SHA1", "SHA256", "Description"}
WriteDebianControl(&buf, paragraphs, debStartFields, debEndFields)
outStr := string(buf.Bytes())
if outStr != inStr {
t.Errorf("\nExpected:\n%s\nGot:\n%s", inStr, outStr)
}
}
var testControlSignedValid = `-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Checksums-Sha1:
7670839693da39075134c1a1f5faad6623df70af 2416 collectd_5.4.0-3.dsc
8f06307bf1c17b83351fccdd7a93b4a822ecbdf4 76417 collectd_5.4.0-3.diff.gz
Checksums-Sha256:
c0679d60f28ceecd09b3c000361c691e373dba599e3878135bc36bede14e109d 2416 collectd_5.4.0-3.dsc
e6d7f21737d2146a9bb30a46137fbd0f00be7971e8c3edc6e66a5981498a261e 76417 collectd_5.4.0-3.diff.gz
Files:
cd9aa41b337352fd160f326523a9c3d8 2416 utils optional collectd_5.4.0-3.dsc
d1270867f1c9517dd92016ea9f2d5afe 76417 utils optional collectd_5.4.0-3.diff.gz
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1
iQEcBAEBAgAGBQJTiG5VAAoJEK9K5quiVC/K9IkH/3nBYgl89/DnjI6ESksYrtkT
Zv/7Cpfg7YsvUmMrghh1hWklK8zj2Tm3NgKshW/HF7orn5cmPUMVZZU8EFa5uR43
FEJ+r1zA40dcOUtKgRESke4b8MNebWq01Op7zrlU1w/4fZ2MFxhNiQ4Xr3ziEl61
kPPR+1ZG43h+wy1h6QXzcNdqcwUnCfX4Uqlhz1giJ1/1qEdW6HlLOiIomZLGhg6b
K5JmdVY4fiH1Fv0tSq7mVnN7LbXJBo8KyzbqJAGkWNu0zh/G/5whz6n2ohgWC/SJ
uiTJMgKpAOxBFeEzO1quFyWnQePIjQ2zWVaTwqDPiZNQ6+377gCrC4Fu+SYdmlQ=
=JixZ
-----END PGP SIGNATURE-----
`
var testKrStr = `-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: GnuPG v1
mQENBFOIYWUBCACzPZm2qE7DrXc+feQ23ll24ZOQD4V7AjFPz1D20iXZqgZIJoQw
40CPu7/WRrJO4lf6Ak/VgeCnijzRuNkDUk2QT8fsNFVgSWxj6FAM1wan0oWqDyR3
yM9RYojQBpzt0uTolE4g4rs5ywWKaWmA6ra0fU6CCxETM+b1UaD+t2NNzOiQFzaZ
UNRs77Anm1/E6Yu3SRCJC5rUI4NZ3YqDUhI3B5OSdXiPQS1CHM6MoUI7fyVWsNJ9
hR/zTVAdB11nN6UAmGKyn36Xt1nIWlSJW/uPzT/sMu+xlfdHCqtzEsOYPzPNLVc4
YGUwEl++NogWZohJ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KNzj++pi/oGmfkS2d2b
Loj/6OElbUyzhAbXunJcZ/aBm+u5DPqYDr1XXg/8bp5UZ7h9vYuxPLGRyhSPdL7I
E6ZHucq5u1AleysWUUt4llmcJ69jtFK69PV4+Q1It+iFAWrS+H2kwMsZAUir2edO
7uk10RWxE1m3xhzU13Hke3osxYrN6BWViZzGUEeXRYk+DfSVegNT7dm92upzixMA
=vk0u
-----END PGP PUBLIC KEY BLOCK-----`
// Check that we output unknown fields in a consistant order
func TestDebControlVerify(t *testing.T) {
var kr openpgp.EntityList
kreader := strings.NewReader(testKrStr)
kr, _ = openpgp.ReadArmoredKeyRing(kreader)
c, err := ParseDebianControl(strings.NewReader(testControlSignedValid), kr)
if err != nil {
t.Errorf("ParseDebianControl failed, %s", err.Error())
return
}
if !c.Signed {
t.Errorf("ControlFile should have beenn signed")
return
}
if !c.SignatureVerified {
t.Errorf("ControlFile should have beenn verified")
return
}
if len(c.SignedBy) != 1 {
t.Errorf("Wrong signature count")
return
}
signer := (c.SignedBy)[0]
if signer != "Tristan Colgate (a test key) <tcolgate@gmail.com>" {
t.Errorf("Wrong signer, %s", signer)
return
}
}
// Check that we output unknown fields in a consistant order
func TestDebControlUnknownSigner(t *testing.T) {
kr := openpgp.EntityList{}
c, err := ParseDebianControl(strings.NewReader(testControlSignedValid), kr)
if err != nil {
t.Errorf("ParseDebianControl failed, %s", err.Error())
return
}
if !c.Signed {
t.Errorf("ControlFile should have beenn signed")
return
}
if c.SignatureVerified {
t.Errorf("ControlFile should not have beenn verified")
return
}
if len(c.SignedBy) != 0 {
t.Errorf("Wrong signature count, %v", len(c.SignedBy))
return
}
}
var testControlSignedInvalid = `-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Checksums-Sha1:
7670839693da39075134c1a1f5faad6623df70af 2416 collectd_5.4.0-3.dsc
8f06307bf1c17b83351fccdd7a93b4a822ecbdf4 76417 collectd_5.4.0-3.diff.gz
8f06307bf1c17b83351fccdd7a93b4a822ecbdf5 76418 EVILFILE.deb
Checksums-Sha256:
c0679d60f28ceecd09b3c000361c691e373dba599e3878135bc36bede14e109d 2416 collectd_5.4.0-3.dsc
e6d7f21737d2146a9bb30a46137fbd0f00be7971e8c3edc6e66a5981498a261e 76417 collectd_5.4.0-3.diff.gz
Files:
cd9aa41b337352fd160f326523a9c3d8 2416 utils optional collectd_5.4.0-3.dsc
d1270867f1c9517dd92016ea9f2d5afe 76417 utils optional collectd_5.4.0-3.diff.gz
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1
iQEcBAEBAgAGBQJTiG5VAAoJEK9K5quiVC/K9IkH/3nBYgl89/DnjI6ESksYrtkH
Zv/7Cpfg7YsvUmMrghh1hWklK8zj2Tm3NgKshW/HF7orn5cmPUMVZZU8EFa5uR43
FEJ+r1zA40dcOUtKgRESke4b8MNebWq01Op7zrlU1w/4fZ2MFxhNiQ4Xr3ziEl61
kPPR+1ZG43h+wy1h6QXzcNdqcwUnCfX4UqlhZ1giJ1/1qEdW6HlLOiIomZLGhg6B
K5JmdVY4fiH1Fv0tSq7mVnN7LbXJBo8KyzbqJAFkWNu0zh/G/5whz6n2ohgWC/SJ
uiTJMgKpAOxBFeEzO1quFyWnQePIjQ2zWVaTwqDPiZNQ6+377gCrC4Fu+SYdmlQ=
=JixZ
-----END PGP SIGNATURE-----
`
// Check that we output unknown fields in a consistant order
/*
func TestDebControlBadSignature(t *testing.T) {
kr := openpgp.EntityList{}
kreader := strings.NewReader(testKrStr)
kr, _ = openpgp.ReadArmoredKeyRing(kreader)
c, err := ParseDebianControl(strings.NewReader(testControlSignedInvalid), kr)
if err != nil {
t.Errorf("ParseDebianControl failed, %s", err.Error())
return
}
if c.Signed {
t.Errorf("ControlFile should not have beenn signed")
return
}
if c.SignatureVerified {
t.Errorf("ControlFile should not have beenn verified")
return
}
if len(c.SignedBy) != 0 {
t.Errorf("Wrong signature count")
return
}
}
*/