-
Notifications
You must be signed in to change notification settings - Fork 2
/
handlers_test.go
590 lines (524 loc) · 22.6 KB
/
handlers_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
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
// Copyright 2016 Google LLC. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package sctfe
import (
"bufio"
"bytes"
"context"
"crypto"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/golang/mock/gomock"
"github.com/google/certificate-transparency-go/x509"
"github.com/google/certificate-transparency-go/x509util"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/google/trillian/monitoring"
"github.com/transparency-dev/trillian-tessera/ctonly"
"github.com/transparency-dev/static-ct/mockstorage"
"github.com/transparency-dev/static-ct/testdata"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/proto"
"k8s.io/klog/v2"
ct "github.com/google/certificate-transparency-go"
)
// Arbitrary time for use in tests
var fakeTime = time.Date(2016, 7, 22, 11, 01, 13, 0, time.UTC)
var fakeTimeMillis = uint64(fakeTime.UnixNano() / millisPerNano)
// The deadline should be the above bumped by 500ms
var fakeDeadlineTime = time.Date(2016, 7, 22, 11, 01, 13, 500*1000*1000, time.UTC)
var fakeTimeSource = NewFixedTimeSource(fakeTime)
type handlerTestInfo struct {
mockCtrl *gomock.Controller
roots *x509util.PEMCertPool
storage *mockstorage.MockStorage
li *logInfo
}
// setupTest creates mock objects and contexts. Caller should invoke info.mockCtrl.Finish().
func setupTest(t *testing.T, pemRoots []string, signer crypto.Signer) handlerTestInfo {
t.Helper()
info := handlerTestInfo{
mockCtrl: gomock.NewController(t),
roots: x509util.NewPEMCertPool(),
}
info.storage = mockstorage.NewMockStorage(info.mockCtrl)
vOpts := CertValidationOpts{
trustedRoots: info.roots,
rejectExpired: false,
}
cfg := &ValidatedLogConfig{Origin: "example.com"}
iOpts := InstanceOptions{Validated: cfg, Deadline: time.Millisecond * 500, MetricFactory: monitoring.InertMetricFactory{}, RequestLog: new(DefaultRequestLog)}
info.li = newLogInfo(iOpts, vOpts, signer, fakeTimeSource, info.storage)
for _, pemRoot := range pemRoots {
if !info.roots.AppendCertsFromPEM([]byte(pemRoot)) {
klog.Fatal("failed to load cert pool")
}
}
return info
}
func (info handlerTestInfo) getHandlers() map[string]AppHandler {
return map[string]AppHandler{
"get-roots": {Info: info.li, Handler: getRoots, Name: "GetRoots", Method: http.MethodGet},
}
}
func (info handlerTestInfo) postHandlers() map[string]AppHandler {
return map[string]AppHandler{
"add-chain": {Info: info.li, Handler: addChain, Name: "AddChain", Method: http.MethodPost},
"add-pre-chain": {Info: info.li, Handler: addPreChain, Name: "AddPreChain", Method: http.MethodPost},
}
}
func TestPostHandlersRejectGet(t *testing.T) {
info := setupTest(t, []string{testdata.FakeCACertPEM}, nil)
defer info.mockCtrl.Finish()
// Anything in the post handler list should reject GET
for path, handler := range info.postHandlers() {
t.Run(path, func(t *testing.T) {
s := httptest.NewServer(handler)
defer s.Close()
resp, err := http.Get(s.URL + "/ct/v1/" + path)
if err != nil {
t.Fatalf("http.Get(%s)=(_,%q); want (_,nil)", path, err)
}
if got, want := resp.StatusCode, http.StatusMethodNotAllowed; got != want {
t.Errorf("http.Get(%s)=(%d,nil); want (%d,nil)", path, got, want)
}
})
}
}
func TestGetHandlersRejectPost(t *testing.T) {
info := setupTest(t, []string{testdata.FakeCACertPEM}, nil)
defer info.mockCtrl.Finish()
// Anything in the get handler list should reject POST.
for path, handler := range info.getHandlers() {
t.Run(path, func(t *testing.T) {
s := httptest.NewServer(handler)
defer s.Close()
resp, err := http.Post(s.URL+"/ct/v1/"+path, "application/json", nil)
if err != nil {
t.Fatalf("http.Post(%s)=(_,%q); want (_,nil)", path, err)
}
if got, want := resp.StatusCode, http.StatusMethodNotAllowed; got != want {
t.Errorf("http.Post(%s)=(%d,nil); want (%d,nil)", path, got, want)
}
})
}
}
func TestPostHandlersFailure(t *testing.T) {
var tests = []struct {
descr string
body io.Reader
want int
}{
{"nil", nil, http.StatusBadRequest},
{"''", strings.NewReader(""), http.StatusBadRequest},
{"malformed-json", strings.NewReader("{ !$%^& not valid json "), http.StatusBadRequest},
{"empty-chain", strings.NewReader(`{ "chain": [] }`), http.StatusBadRequest},
{"wrong-chain", strings.NewReader(`{ "chain": [ "test" ] }`), http.StatusBadRequest},
}
info := setupTest(t, []string{testdata.FakeCACertPEM}, nil)
defer info.mockCtrl.Finish()
for path, handler := range info.postHandlers() {
t.Run(path, func(t *testing.T) {
s := httptest.NewServer(handler)
for _, test := range tests {
resp, err := http.Post(s.URL+"/ct/v1/"+path, "application/json", test.body)
if err != nil {
t.Errorf("http.Post(%s,%s)=(_,%q); want (_,nil)", path, test.descr, err)
continue
}
if resp.StatusCode != test.want {
t.Errorf("http.Post(%s,%s)=(%d,nil); want (%d,nil)", path, test.descr, resp.StatusCode, test.want)
}
}
})
}
}
func TestHandlers(t *testing.T) {
path := "/test-prefix/ct/v1/add-chain"
info := setupTest(t, nil, nil)
defer info.mockCtrl.Finish()
for _, test := range []string{
"/test-prefix/",
"test-prefix/",
"/test-prefix",
"test-prefix",
} {
t.Run(test, func(t *testing.T) {
handlers := info.li.Handlers(test)
if h, ok := handlers[path]; !ok {
t.Errorf("Handlers(%s)[%q]=%+v; want _", test, path, h)
} else if h.Name != "AddChain" {
t.Errorf("Handlers(%s)[%q].Name=%q; want 'AddChain'", test, path, h.Name)
}
// Check each entrypoint has a handler
if got, want := len(handlers), len(Entrypoints); got != want {
t.Fatalf("len(Handlers(%s))=%d; want %d", test, got, want)
}
// We want to see the same set of handler names that we think we registered.
var hNames []EntrypointName
for _, v := range handlers {
hNames = append(hNames, v.Name)
}
if !cmp.Equal(Entrypoints, hNames, cmpopts.SortSlices(func(n1, n2 EntrypointName) bool {
return n1 < n2
})) {
t.Errorf("Handler names mismatch got: %v, want: %v", hNames, Entrypoints)
}
})
}
}
func parseChain(t *testing.T, isPrecert bool, pemChain []string, root *x509.Certificate) (*ctonly.Entry, []*x509.Certificate) {
t.Helper()
pool := loadCertsIntoPoolOrDie(t, pemChain)
leafChain := pool.RawCertificates()
if !leafChain[len(leafChain)-1].Equal(root) {
// The submitted chain may not include a root, but the generated LogLeaf will
fullChain := make([]*x509.Certificate, len(leafChain)+1)
copy(fullChain, leafChain)
fullChain[len(leafChain)] = root
leafChain = fullChain
}
entry, err := entryFromChain(leafChain, isPrecert, fakeTimeMillis)
if err != nil {
t.Fatalf("failed to create entry")
}
return entry, leafChain
}
func TestAddChainWhitespace(t *testing.T) {
signer, err := setupSigner(fakeSignature)
if err != nil {
t.Fatalf("Failed to create test signer: %v", err)
}
info := setupTest(t, []string{testdata.FakeCACertPEM}, signer)
defer info.mockCtrl.Finish()
// Throughout we use variants of a hard-coded POST body derived from a chain of:
pemChain := []string{testdata.LeafSignedByFakeIntermediateCertPEM, testdata.FakeIntermediateCertPEM}
// Break the JSON into chunks:
intro := "{\"chain\""
// followed by colon then the first line of the PEM file
chunk1a := "[\"MIIH6DCCBtCgAwIBAgIIQoIqW4Zvv+swDQYJKoZIhvcNAQELBQAwcjELMAkGA1UE"
// straight into rest of first entry
chunk1b := "BhMCR0IxDzANBgNVBAgMBkxvbmRvbjEPMA0GA1UEBwwGTG9uZG9uMQ8wDQYDVQQKDAZHb29nbGUxDDAKBgNVBAsMA0VuZzEiMCAGA1UEAwwZRmFrZUludGVybWVkaWF0Z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JhT9tmVRL3bpgpT8Kr4aoFhU2LXe45YIB1A9DjUm5TrHZ+iNtvE0YfYMR9L9C1HPppmX1CahEhTdog7laE1198UCAwEAAaM4MDYwDwYDVR0jBAgwBoAEAQIDBDASBgNVHRMBAf8ECDAGAQH/AgEAMA8GA1UdDwEB/wQFAwMH/4AwDQYJKoZIhvcNAQELBQADggEBAAHiOgwAvEzhrNMQVAz8a+SsyMIABXQ5P8WbJeHjkIipE4+5ZpkrZVXq9p8wOdkYnOHx4WNi9PVGQbLG9Iufh9fpk8cyyRWDi+V20/CNNtawMq3ClV3dWC98Tj4WX/BXDCeY2jK4jYGV+ds43HYV0ToBmvvrccq/U7zYMGFcQiKBClz5bTE+GMvrZWcO5A/Lh38i2YSF1i8SfDVnAOBlAgZmllcheHpGsWfSnduIllUvTsRvEIsaaqfVLl5QpRXBOq8tbjK85/2g6ear1oxPhJ1w9hds+WTFXkmHkWvKJebY13t3OfSjAyhaRSt8hdzDzHTFwjPjHT8h6dU7/hMdkUg=\""
epilog := "]}\n"
req, leafChain := parseChain(t, false, pemChain, info.roots.RawCertificates()[0])
rsp := uint64(0)
var tests = []struct {
descr string
body string
want int
}{
{
descr: "valid",
body: intro + ":" + chunk1a + chunk1b + "," + chunk2 + epilog,
want: http.StatusOK,
},
{
descr: "valid-space-between",
body: intro + " : " + chunk1a + chunk1b + " , " + chunk2 + epilog,
want: http.StatusOK,
},
{
descr: "valid-newline-between",
body: intro + " : " + chunk1a + chunk1b + ",\n" + chunk2 + epilog,
want: http.StatusOK,
},
{
descr: "invalid-raw-newline-in-string",
body: intro + ":" + chunk1a + "\n" + chunk1b + "," + chunk2 + epilog,
want: http.StatusBadRequest,
},
{
descr: "valid-escaped-newline-in-string",
body: intro + ":" + chunk1a + "\\n" + chunk1b + "," + chunk2 + epilog,
want: http.StatusOK,
},
}
for _, test := range tests {
t.Run(test.descr, func(t *testing.T) {
if test.want == http.StatusOK {
info.storage.EXPECT().GetCertIndex(deadlineMatcher(), cmpMatcher{leafChain[0]}).Return(uint64(0), false, nil)
info.storage.EXPECT().AddIssuerChain(deadlineMatcher(), cmpMatcher{leafChain[1:]}).Return(nil)
info.storage.EXPECT().Add(deadlineMatcher(), cmpMatcher{req}).Return(func() (uint64, error) { return rsp, nil })
info.storage.EXPECT().AddCertIndex(deadlineMatcher(), cmpMatcher{leafChain[0]}, uint64(0)).Return(nil)
}
recorder := httptest.NewRecorder()
handler := AppHandler{Info: info.li, Handler: addChain, Name: "AddChain", Method: http.MethodPost}
req, err := http.NewRequest(http.MethodPost, "http://example.com/ct/v1/add-chain", strings.NewReader(test.body))
if err != nil {
t.Fatalf("Failed to create POST request: %v", err)
}
handler.ServeHTTP(recorder, req)
if recorder.Code != test.want {
t.Fatalf("addChain()=%d (body:%v); want %dv", recorder.Code, recorder.Body, test.want)
}
})
}
}
func TestAddChain(t *testing.T) {
var tests = []struct {
descr string
chain []string
// TODO(phboneff): can this be removed?
toSign string // hex-encoded
want int
err error
}{
{
descr: "leaf-only",
chain: []string{testdata.LeafSignedByFakeIntermediateCertPEM},
want: http.StatusBadRequest,
},
{
descr: "wrong-entry-type",
chain: []string{testdata.PrecertPEMValid},
want: http.StatusBadRequest,
},
{
descr: "backend-storage-fail",
chain: []string{testdata.LeafSignedByFakeIntermediateCertPEM, testdata.FakeIntermediateCertPEM},
toSign: "1337d72a403b6539f58896decba416d5d4b3603bfa03e1f94bb9b4e898af897d",
want: http.StatusInternalServerError,
err: status.Errorf(codes.Internal, "error"),
},
{
descr: "success-without-root",
chain: []string{testdata.LeafSignedByFakeIntermediateCertPEM, testdata.FakeIntermediateCertPEM},
toSign: "1337d72a403b6539f58896decba416d5d4b3603bfa03e1f94bb9b4e898af897d",
want: http.StatusOK,
},
{
descr: "success",
chain: []string{testdata.LeafSignedByFakeIntermediateCertPEM, testdata.FakeIntermediateCertPEM, testdata.FakeCACertPEM},
toSign: "1337d72a403b6539f58896decba416d5d4b3603bfa03e1f94bb9b4e898af897d",
want: http.StatusOK,
},
}
signer, err := setupSigner(fakeSignature)
if err != nil {
t.Fatalf("Failed to create test signer: %v", err)
}
info := setupTest(t, []string{testdata.FakeCACertPEM}, signer)
defer info.mockCtrl.Finish()
for _, test := range tests {
t.Run(test.descr, func(t *testing.T) {
pool := loadCertsIntoPoolOrDie(t, test.chain)
chain := createJSONChain(t, *pool)
if len(test.toSign) > 0 {
req, leafChain := parseChain(t, false, test.chain, info.roots.RawCertificates()[0])
rsp := uint64(0)
info.storage.EXPECT().GetCertIndex(deadlineMatcher(), cmpMatcher{leafChain[0]}).Return(uint64(0), false, nil)
info.storage.EXPECT().AddIssuerChain(deadlineMatcher(), cmpMatcher{leafChain[1:]}).Return(nil)
info.storage.EXPECT().Add(deadlineMatcher(), cmpMatcher{req}).Return(func() (uint64, error) { return rsp, test.err })
if test.want == http.StatusOK {
info.storage.EXPECT().AddCertIndex(deadlineMatcher(), cmpMatcher{leafChain[0]}, uint64(0)).Return(nil)
}
}
recorder := makeAddChainRequest(t, info.li, chain)
if recorder.Code != test.want {
t.Fatalf("addChain()=%d (body:%v); want %dv", recorder.Code, recorder.Body, test.want)
}
if test.want == http.StatusOK {
var resp ct.AddChainResponse
if err := json.NewDecoder(recorder.Body).Decode(&resp); err != nil {
t.Fatalf("json.Decode(%s)=%v; want nil", recorder.Body.Bytes(), err)
}
if got, want := ct.Version(resp.SCTVersion), ct.V1; got != want {
t.Errorf("resp.SCTVersion=%v; want %v", got, want)
}
if got, want := resp.ID, demoLogID[:]; !bytes.Equal(got, want) {
t.Errorf("resp.ID=%v; want %v", got, want)
}
if got, want := resp.Timestamp, uint64(1469185273000); got != want {
t.Errorf("resp.Timestamp=%d; want %d", got, want)
}
if got, want := hex.EncodeToString(resp.Signature), "040300067369676e6564"; got != want {
t.Errorf("resp.Signature=%s; want %s", got, want)
}
// TODO(phboneff): check that the index is in the SCT
}
})
}
}
func TestAddPrechain(t *testing.T) {
var tests = []struct {
descr string
chain []string
root string
toSign string // hex-encoded
err error
want int
}{
{
descr: "leaf-signed-by-different",
chain: []string{testdata.PrecertPEMValid, testdata.FakeIntermediateCertPEM},
want: http.StatusBadRequest,
},
{
descr: "wrong-entry-type",
chain: []string{testdata.TestCertPEM},
want: http.StatusBadRequest,
},
{
descr: "backend-storage-fail",
chain: []string{testdata.PrecertPEMValid, testdata.CACertPEM},
toSign: "92ecae1a2dc67a6c5f9c96fa5cab4c2faf27c48505b696dad926f161b0ca675a",
err: status.Errorf(codes.Internal, "error"),
want: http.StatusInternalServerError,
},
{
descr: "success",
chain: []string{testdata.PrecertPEMValid, testdata.CACertPEM},
toSign: "92ecae1a2dc67a6c5f9c96fa5cab4c2faf27c48505b696dad926f161b0ca675a",
want: http.StatusOK,
},
{
descr: "success-without-root",
chain: []string{testdata.PrecertPEMValid},
toSign: "92ecae1a2dc67a6c5f9c96fa5cab4c2faf27c48505b696dad926f161b0ca675a",
want: http.StatusOK,
},
// TODO(phboneff): add a test with an intermediate
// TODO(phboneff): add a test with a pre-issuer intermediate cert
}
signer, err := setupSigner(fakeSignature)
if err != nil {
t.Fatalf("Failed to create test signer: %v", err)
}
info := setupTest(t, []string{testdata.CACertPEM}, signer)
defer info.mockCtrl.Finish()
for _, test := range tests {
t.Run(test.descr, func(t *testing.T) {
pool := loadCertsIntoPoolOrDie(t, test.chain)
chain := createJSONChain(t, *pool)
if len(test.toSign) > 0 {
req, leafChain := parseChain(t, true, test.chain, info.roots.RawCertificates()[0])
rsp := uint64(0)
info.storage.EXPECT().GetCertIndex(deadlineMatcher(), cmpMatcher{leafChain[0]}).Return(uint64(0), false, nil)
info.storage.EXPECT().AddIssuerChain(deadlineMatcher(), cmpMatcher{leafChain[1:]}).Return(nil)
info.storage.EXPECT().Add(deadlineMatcher(), cmpMatcher{req}).Return(func() (uint64, error) { return rsp, test.err })
if test.want == http.StatusOK {
info.storage.EXPECT().AddCertIndex(deadlineMatcher(), cmpMatcher{leafChain[0]}, uint64(0)).Return(nil)
}
}
recorder := makeAddPrechainRequest(t, info.li, chain)
if recorder.Code != test.want {
t.Fatalf("addPrechain()=%d (body:%v); want %d", recorder.Code, recorder.Body, test.want)
}
if test.want == http.StatusOK {
var resp ct.AddChainResponse
if err := json.NewDecoder(recorder.Body).Decode(&resp); err != nil {
t.Fatalf("json.Decode(%s)=%v; want nil", recorder.Body.Bytes(), err)
}
if got, want := ct.Version(resp.SCTVersion), ct.V1; got != want {
t.Errorf("resp.SCTVersion=%v; want %v", got, want)
}
if got, want := resp.ID, demoLogID[:]; !bytes.Equal(got, want) {
t.Errorf("resp.ID=%x; want %x", got, want)
}
if got, want := resp.Timestamp, uint64(1469185273000); got != want {
t.Errorf("resp.Timestamp=%d; want %d", got, want)
}
if got, want := hex.EncodeToString(resp.Signature), "040300067369676e6564"; got != want {
t.Errorf("resp.Signature=%s; want %s", got, want)
}
}
})
}
}
func createJSONChain(t *testing.T, p x509util.PEMCertPool) io.Reader {
t.Helper()
var req ct.AddChainRequest
for _, rawCert := range p.RawCertificates() {
req.Chain = append(req.Chain, rawCert.Raw)
}
var buffer bytes.Buffer
// It's tempting to avoid creating and flushing the intermediate writer but it doesn't work
writer := bufio.NewWriter(&buffer)
err := json.NewEncoder(writer).Encode(&req)
if err := writer.Flush(); err != nil {
t.Error(err)
}
if err != nil {
t.Fatalf("Failed to create test json: %v", err)
}
return bufio.NewReader(&buffer)
}
type dlMatcher struct {
}
func deadlineMatcher() gomock.Matcher {
return dlMatcher{}
}
func (d dlMatcher) Matches(x interface{}) bool {
ctx, ok := x.(context.Context)
if !ok {
return false
}
deadlineTime, ok := ctx.Deadline()
if !ok {
return false // we never make RPC calls without a deadline set
}
return deadlineTime == fakeDeadlineTime
}
func (d dlMatcher) String() string {
return fmt.Sprintf("deadline is %v", fakeDeadlineTime)
}
func makeAddPrechainRequest(t *testing.T, li *logInfo, body io.Reader) *httptest.ResponseRecorder {
t.Helper()
handler := AppHandler{Info: li, Handler: addPreChain, Name: "AddPreChain", Method: http.MethodPost}
return makeAddChainRequestInternal(t, handler, "add-pre-chain", body)
}
func makeAddChainRequest(t *testing.T, li *logInfo, body io.Reader) *httptest.ResponseRecorder {
t.Helper()
handler := AppHandler{Info: li, Handler: addChain, Name: "AddChain", Method: http.MethodPost}
return makeAddChainRequestInternal(t, handler, "add-chain", body)
}
func makeAddChainRequestInternal(t *testing.T, handler AppHandler, path string, body io.Reader) *httptest.ResponseRecorder {
t.Helper()
req, err := http.NewRequest(http.MethodPost, fmt.Sprintf("http://example.com/ct/v1/%s", path), body)
if err != nil {
t.Fatalf("Failed to create POST request: %v", err)
}
w := httptest.NewRecorder()
handler.ServeHTTP(w, req)
return w
}
func loadCertsIntoPoolOrDie(t *testing.T, certs []string) *x509util.PEMCertPool {
t.Helper()
pool := x509util.NewPEMCertPool()
for _, cert := range certs {
if !pool.AppendCertsFromPEM([]byte(cert)) {
t.Fatalf("couldn't parse test certs: %v", certs)
}
}
return pool
}
// cmpMatcher is a custom gomock.Matcher that uses cmp.Equal combined with a
// cmp.Comparer that knows how to properly compare proto.Message types.
type cmpMatcher struct{ want interface{} }
func (m cmpMatcher) Matches(got interface{}) bool {
return cmp.Equal(got, m.want, cmp.Comparer(proto.Equal))
}
func (m cmpMatcher) String() string {
return fmt.Sprintf("equals %v", m.want)
}