aboutsummaryrefslogtreecommitdiff
path: root/type_test.go
blob: 1389467331293a0d428e26a93594d95b0063ffff (plain)
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
package stfe

import (
	"testing"

	"crypto/tls"

	"github.com/system-transparency/stfe/server/testdata"
	"github.com/system-transparency/stfe/x509util"
)

var (
	testLogId          = make([]byte, 32)
	testSignature      = make([]byte, 32)
	testNodeHash       = make([]byte, 32)
	testMessage        = []byte("test message")
	testPackage        = []byte("foobar")
	testChecksum       = make([]byte, 32)
	testTreeSize       = uint64(128)
	testTreeSizeLarger = uint64(256)
	testTimestamp      = uint64(0)
	testProof          = [][]byte{
		make([]byte, 32),
		make([]byte, 32),
	}
	testIndex           = uint64(0)
	testSignatureScheme = tls.Ed25519
)

// TestEncDecAppendix tests that valid appendices can be (un)marshaled, and that
// invalid ones in fact dail.
//
// TODO: max limits for certificate chains are not tested.
func TestEncDecAppendix(t *testing.T) {
	chain, err := x509util.NewCertificateList(testdata.PemChain)
	if err != nil {
		t.Fatalf("must decode certificate chain: %v", err)
	}

	signatureMin := 1
	signatureMax := 16383
	for _, table := range []struct {
		description string
		appendix    *Appendix
		wantErr     bool
	}{
		{
			description: "too short signature",
			appendix:    NewAppendix(chain, make([]byte, signatureMin-1), uint16(testSignatureScheme)),
			wantErr:     true,
		},
		{
			description: "too large signature",
			appendix:    NewAppendix(chain, make([]byte, signatureMax+1), uint16(testSignatureScheme)),
			wantErr:     true,
		},
		{
			description: "ok signature: min",
			appendix:    NewAppendix(chain, make([]byte, signatureMin), uint16(testSignatureScheme)),
		},
		{
			description: "ok signature: max",
			appendix:    NewAppendix(chain, make([]byte, signatureMax), uint16(testSignatureScheme)),
		},
		{
			description: "too short chain",
			appendix:    NewAppendix(nil, testSignature, uint16(testSignatureScheme)),
			wantErr:     true,
		},
	} {
		b, err := table.appendix.Marshal()
		if err != nil && !table.wantErr {
			t.Errorf("failed marshaling Appendix for %q: %v", table.description, err)
		} else if err == nil && table.wantErr {
			t.Errorf("succeeded marshaling Appendix but wanted error for %q", table.description)
		}
		if err != nil || table.wantErr {
			continue // nothing to unmarshal
		}

		var appendix Appendix
		if err := appendix.Unmarshal(b); err != nil {
			t.Errorf("failed unmarshaling Appendix: %v", err)
		}
	}
}

// TestEncDecStItem tests that valid StItems can be (un)marshaled, and that
// invalid ones in fact fail.
//
// TODO: max limits for inclusion and consistency proofs are not tested.
// Note: TreeHeadV1 extensions are not tested (not used by stfe)
func TestEncDecStItem(t *testing.T) {
	logIdSize := 32
	signatureMin := 1
	signatureMax := 65535
	messageMax := 65535
	nodeHashMin := 32
	nodeHashMax := 255
	packageMin := 1
	packageMax := 255
	checksumMin := 1
	checksumMax := 64
	for _, table := range []struct {
		description string
		item        *StItem
		wantErr     bool
	}{
		// signed_tree_head_v1
		{
			description: "too short log id",
			item:        NewSignedTreeHeadV1(NewTreeHeadV1(makeTrillianLogRoot(t, testTimestamp, testTreeSize, testNodeHash)), make([]byte, logIdSize-1), testSignature),
			wantErr:     true,
		},
		{
			description: "too large log id",
			item:        NewSignedTreeHeadV1(NewTreeHeadV1(makeTrillianLogRoot(t, testTimestamp, testTreeSize, testNodeHash)), make([]byte, logIdSize+1), testSignature),
			wantErr:     true,
		},
		{
			description: "ok log id: min and max",
			item:        NewSignedTreeHeadV1(NewTreeHeadV1(makeTrillianLogRoot(t, testTimestamp, testTreeSize, testNodeHash)), testLogId, testSignature),
		},
		{
			description: "too short signature",
			item:        NewSignedTreeHeadV1(NewTreeHeadV1(makeTrillianLogRoot(t, testTimestamp, testTreeSize, testNodeHash)), testLogId, make([]byte, signatureMin-1)),
			wantErr:     true,
		},
		{
			description: "too large signature",
			item:        NewSignedTreeHeadV1(NewTreeHeadV1(makeTrillianLogRoot(t, testTimestamp, testTreeSize, testNodeHash)), testLogId, make([]byte, signatureMax+1)),
			wantErr:     true,
		},
		{
			description: "ok signature: min",
			item:        NewSignedTreeHeadV1(NewTreeHeadV1(makeTrillianLogRoot(t, testTimestamp, testTreeSize, testNodeHash)), testLogId, make([]byte, signatureMin)),
		},
		{
			description: "ok signature: max",
			item:        NewSignedTreeHeadV1(NewTreeHeadV1(makeTrillianLogRoot(t, testTimestamp, testTreeSize, testNodeHash)), testLogId, make([]byte, signatureMax)),
		},
		{
			description: "too short root hash",
			item:        NewSignedTreeHeadV1(NewTreeHeadV1(makeTrillianLogRoot(t, testTimestamp, testTreeSize, make([]byte, nodeHashMin-1))), testLogId, testSignature),
			wantErr:     true,
		},
		{
			description: "too large root hash",
			item:        NewSignedTreeHeadV1(NewTreeHeadV1(makeTrillianLogRoot(t, testTimestamp, testTreeSize, make([]byte, nodeHashMax+1))), testLogId, testSignature),
			wantErr:     true,
		},
		{
			description: "ok root hash: min",
			item:        NewSignedTreeHeadV1(NewTreeHeadV1(makeTrillianLogRoot(t, testTimestamp, testTreeSize, make([]byte, nodeHashMin))), testLogId, testSignature),
		},
		{
			description: "ok root hash: min",
			item:        NewSignedTreeHeadV1(NewTreeHeadV1(makeTrillianLogRoot(t, testTimestamp, testTreeSize, make([]byte, nodeHashMax))), testLogId, testSignature),
		},
		// signed_debug_info_v1
		{
			description: "too short log id",
			item:        NewSignedDebugInfoV1(make([]byte, logIdSize-1), testMessage, testSignature),
			wantErr:     true,
		},
		{
			description: "too large log id",
			item:        NewSignedDebugInfoV1(make([]byte, logIdSize+1), testMessage, testSignature),
			wantErr:     true,
		},
		{
			description: "ok log id: min and max",
			item:        NewSignedDebugInfoV1(testLogId, testMessage, testSignature),
		},
		{
			description: "too large message",
			item:        NewSignedDebugInfoV1(testLogId, make([]byte, messageMax+1), testSignature),
			wantErr:     true,
		},
		{
			description: "ok message: max",
			item:        NewSignedDebugInfoV1(testLogId, make([]byte, messageMax), testSignature),
		},
		{
			description: "too short signature",
			item:        NewSignedDebugInfoV1(testLogId, testMessage, make([]byte, signatureMin-1)),
			wantErr:     true,
		},
		{
			description: "too large signature",
			item:        NewSignedDebugInfoV1(testLogId, testMessage, make([]byte, signatureMax+1)),
			wantErr:     true,
		},
		{
			description: "ok signature: min",
			item:        NewSignedDebugInfoV1(testLogId, testMessage, make([]byte, signatureMin)),
		},
		{
			description: "ok signature: max",
			item:        NewSignedDebugInfoV1(testLogId, testMessage, make([]byte, signatureMax)),
		},
		// consistency_proof_v1
		{
			description: "too short log id",
			item:        NewConsistencyProofV1(make([]byte, logIdSize-1), testTreeSize, testTreeSizeLarger, testProof),
			wantErr:     true,
		},
		{
			description: "too large log id",
			item:        NewConsistencyProofV1(make([]byte, logIdSize+1), testTreeSize, testTreeSizeLarger, testProof),
			wantErr:     true,
		},
		{
			description: "ok log id: min and max",
			item:        NewConsistencyProofV1(testLogId, testTreeSize, testTreeSizeLarger, testProof),
		},
		{
			description: "too small node hash in proof",
			item:        NewConsistencyProofV1(testLogId, testTreeSize, testTreeSizeLarger, [][]byte{make([]byte, nodeHashMin-1)}),
			wantErr:     true,
		},
		{
			description: "too large node hash in proof",
			item:        NewConsistencyProofV1(testLogId, testTreeSize, testTreeSizeLarger, [][]byte{make([]byte, nodeHashMax+1)}),
			wantErr:     true,
		},
		{
			description: "ok proof: min node hash",
			item:        NewConsistencyProofV1(testLogId, testTreeSize, testTreeSizeLarger, [][]byte{make([]byte, nodeHashMin)}),
		},
		{
			description: "ok proof: max node hash",
			item:        NewConsistencyProofV1(testLogId, testTreeSize, testTreeSizeLarger, [][]byte{make([]byte, nodeHashMin)}),
		},
		{
			description: "ok proof: empty",
			item:        NewConsistencyProofV1(testLogId, testTreeSize, testTreeSizeLarger, [][]byte{}),
		},
		// inclusion_proof_v1
		{
			description: "too short log id",
			item:        NewInclusionProofV1(make([]byte, logIdSize-1), testTreeSize, testIndex, testProof),
			wantErr:     true,
		},
		{
			description: "too large log id",
			item:        NewInclusionProofV1(make([]byte, logIdSize+1), testTreeSize, testIndex, testProof),
			wantErr:     true,
		},
		{
			description: "ok log id: min and max",
			item:        NewInclusionProofV1(testLogId, testTreeSize, testIndex, testProof),
		},
		{
			description: "too short node hash in proof",
			item:        NewInclusionProofV1(testLogId, testTreeSize, testIndex, [][]byte{make([]byte, nodeHashMin-1)}),
			wantErr:     true,
		},
		{
			description: "too large node hash in proof",
			item:        NewInclusionProofV1(testLogId, testTreeSize, testIndex, [][]byte{make([]byte, nodeHashMax+1)}),
			wantErr:     true,
		},
		{
			description: "ok proof: min node hash",
			item:        NewInclusionProofV1(testLogId, testTreeSize, testIndex, [][]byte{make([]byte, nodeHashMin)}),
		},
		{
			description: "ok proof: max node hash",
			item:        NewInclusionProofV1(testLogId, testTreeSize, testIndex, [][]byte{make([]byte, nodeHashMax)}),
		},
		{
			description: "ok proof: empty",
			item:        NewInclusionProofV1(testLogId, testTreeSize, testIndex, [][]byte{}),
		},
		// checksum_v1
		{
			description: "too short package",
			item:        NewChecksumV1(make([]byte, packageMin-1), testChecksum),
			wantErr:     true,
		},
		{
			description: "too large package",
			item:        NewChecksumV1(make([]byte, packageMax+1), testChecksum),
			wantErr:     true,
		},
		{
			description: "ok package: min",
			item:        NewChecksumV1(make([]byte, packageMin), testChecksum),
		},
		{
			description: "ok package: max",
			item:        NewChecksumV1(make([]byte, packageMax), testChecksum),
		},
		{
			description: "too short checksum",
			item:        NewChecksumV1(testPackage, make([]byte, checksumMin-1)),
			wantErr:     true,
		},
		{
			description: "too large checksum",
			item:        NewChecksumV1(testPackage, make([]byte, checksumMax+1)),
			wantErr:     true,
		},
		{
			description: "ok checksum: min",
			item:        NewChecksumV1(testPackage, make([]byte, checksumMin)),
		},
		{
			description: "ok checksum: max",
			item:        NewChecksumV1(testPackage, make([]byte, checksumMax)),
		},
	} {
		b, err := table.item.Marshal()
		if err != nil && !table.wantErr {
			t.Errorf("failed marshaling StItem(%s) in test %q: %v", table.item.Format, table.description, err)
		} else if err == nil && table.wantErr {
			t.Errorf("succeeded marshaling StItem(%s) in test %q but want failure", table.item.Format, table.description)
		}
		if err != nil || table.wantErr {
			continue // nothing to unmarshal
		}

		var item StItem
		if err := item.Unmarshal(b); err != nil {
			t.Errorf("failed unmarshaling StItem(%s) in test %q: %v", table.item.Format, table.description, err)
		}
	}
}