-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmock_hvca_test.go
743 lines (636 loc) · 20.9 KB
/
mock_hvca_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
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
/*
Copyright (c) 2019-2021 GMO GlobalSign Pte. Ltd.
Licensed under the MIT License (the "License"); you may not use this file except
in compliance with the License. You may obtain a copy of the License at
https://opensource.org/licenses/MIT
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 hvclient_test
import (
"context"
"crypto/x509"
"encoding/json"
"fmt"
"io/ioutil"
"math/big"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/globalsign/hvclient"
"github.com/globalsign/hvclient/internal/httputils"
"github.com/globalsign/hvclient/internal/pki"
"github.com/go-chi/chi"
)
// Note: mocking up the entire HVCA API service seems a little extreme, and
// can result in unrealistic testing. However: (1) obtaining a suitable test
// HVCA account is not a trivial process, and requiring one to perform basic
// regression tests would be an onerous requirement, particularly for third
// party contributors; (2) it is not feasible to obtain some responses from
// the live HVCA service under automated test conditions (for example, an
// affirmative response that control over a domain has been successfully
// verified); and (3) it can be difficult to induce appropriate error
// conditions from the live HVCA service. Accordingly, to provide contributors
// with a way to perform regression tests in the absence of an HVCA test
// account, and to allow more code paths to be tested, we do mock up the HVCA
// service in addition to providing a suite of integration tests for use with
// the live service.
type mockCertInfo struct {
PEM string `json:"certificate"`
Status string `json:"status"`
UpdatedAt int64 `json:"updated_at"`
}
type mockCertMeta struct {
SerialNumber string `json:"serial_number"`
NotBefore int64 `json:"not_before"`
NotAfter int64 `json:"not_after"`
}
type mockClaim struct {
ID string `json:"id"`
Status string `json:"status"`
Domain string `json:"domain"`
CreatedAt int64 `json:"created_at"`
ExpiresAt int64 `json:"expires_at"`
AssertBy int64 `json:"assert_by"`
Log []mockClaimLogEntry `json:"log"`
}
type mockClaimAssertionInfo struct {
Token string `json:"token"`
AssertBy int64 `json:"assert_by"`
ID string `json:"id"`
}
type mockClaimLogEntry struct {
Status string `json:"status"`
Description string `json:"description"`
TimeStamp int64 `json:"timestamp"`
}
type mockCounter struct {
Value int `json:"value"`
}
type mockDNSRequest struct {
AuthorizationDomain string `json:"authorization_domain"`
}
type mockHTTPRequest struct {
AuthorizationDomain string `json:"authorization_domain,omitempty"`
Scheme string `json:"scheme"`
}
type mockEmailRequest struct {
Email string `json:"email_address"`
}
type mockError struct {
Description string `json:"description"`
}
type mockLoginRequest struct {
APIKey string `json:"api_key"`
APISecret string `json:"api_secret"`
}
type mockLoginResponse struct {
Token string `json:"access_token"`
}
type mockAuthorisedEmails struct {
Constructed []string `json:"constructed"`
DNS mockDNSResults `json:"DNS"`
}
type mockDNSResults struct {
SOA mockSOAResults `json:"SOA"`
}
type mockSOAResults struct {
Emails []string `json:"emails,omitempty"`
}
type mockRevocationBody struct {
RevocationReason string `json:"revocation_reason"`
RevocationTime int64 `json:"revocation_time"`
}
const (
mockAPIKey = "mock_api_key"
mockAPISecret = "mock_api_secret"
mockCertSerial = "741DAF9EC2D5F7DC"
mockCounterIssued = 72
mockCounterRevoked = 14
mockClaimDomainVerified = "verified.com."
mockClaimEmail = "[email protected]"
mockClaimID = "113FED08"
mockClaimToken = "mock_claim_token"
mockQuotaIssuance = 42
mockSSLClientSerial = "0123456789"
mockToken = "mock_token"
sslClientSerialHeader = "X-SSL-Client-Serial"
triggerError = "triggererror"
)
var (
mockBigIntNotFound = big.NewInt(999999)
mockCert = mustReadCertFromFile("testdata/test_cert.pem")
mockClaimAssert = mockClaimAssertionInfo{
Token: mockClaimToken,
AssertBy: mockDateAssertBy.Unix(),
ID: mockClaimID,
}
mockClaimsEntries = []mockClaim{
{
ID: mockClaimID,
Status: "VERIFIED",
Domain: "fake.com.",
CreatedAt: mockDateCreated.Unix(),
ExpiresAt: mockDateExpiresAt.Unix(),
AssertBy: mockDateAssertBy.Unix(),
Log: []mockClaimLogEntry{
{
Status: "SUCCESS",
Description: "domain claim verified",
TimeStamp: mockDateUpdated.Unix(),
},
},
},
{
ID: "pending1",
Status: "PENDING",
Domain: "pending1.com.",
CreatedAt: mockDateCreated.Unix(),
ExpiresAt: mockDateExpiresAt.Unix(),
AssertBy: mockDateAssertBy.Unix(),
Log: []mockClaimLogEntry{
{
Status: "ERROR",
Description: "error verifying domain claim",
TimeStamp: mockDateUpdated.Unix(),
},
{
Status: "ERROR",
Description: "error verifying domain claim",
TimeStamp: mockDateUpdated.Add(time.Hour).Unix(),
},
},
},
{
ID: "pending2",
Status: "PENDING",
Domain: "pending2.com.",
CreatedAt: mockDateCreated.Unix(),
ExpiresAt: mockDateExpiresAt.Unix(),
AssertBy: mockDateAssertBy.Unix(),
Log: []mockClaimLogEntry{
{
Status: "ERROR",
Description: "error verifying domain claim",
TimeStamp: mockDateUpdated.Unix(),
},
},
},
}
mockDateCreated = time.Date(2021, 6, 16, 4, 19, 25, 0, time.UTC)
mockDateExpiresAt = time.Date(2021, 6, 17, 22, 7, 4, 0, time.UTC)
mockDateUpdated = time.Date(2021, 6, 18, 16, 29, 51, 0, time.UTC)
mockDateAssertBy = time.Date(2021, 6, 19, 13, 5, 31, 0, time.UTC)
mockPolicy = hvclient.Policy{
Validity: &hvclient.ValidityPolicy{
SecondsMin: 3600,
SecondsMax: 7776000,
NotBeforeNegativeSkew: 120,
NotBeforePositiveSkew: 3600,
},
SubjectDN: &hvclient.SubjectDNPolicy{
CommonName: &hvclient.StringPolicy{
Presence: hvclient.Required,
Format: `^[a-zA-Z]*$`,
},
},
PublicKey: &hvclient.PublicKeyPolicy{
KeyType: hvclient.ECDSA,
AllowedLengths: []int{256, 384, 521},
KeyFormat: hvclient.PKCS10,
},
PublicKeySignature: hvclient.Required,
}
mockStatsExpiringData = []mockCertMeta{
{
SerialNumber: "748BDAE7199CC246",
NotBefore: time.Date(2021, 7, 12, 16, 29, 51, 0, time.UTC).Unix(),
NotAfter: time.Date(2021, 10, 10, 16, 29, 51, 0, time.UTC).Unix(),
},
{
SerialNumber: "DEADBEEF44274823",
NotBefore: time.Date(2021, 7, 14, 12, 5, 37, 0, time.UTC).Unix(),
NotAfter: time.Date(2021, 10, 12, 12, 5, 37, 0, time.UTC).Unix(),
},
{
SerialNumber: "AA9915DC78BB21FF",
NotBefore: time.Date(2021, 7, 14, 17, 59, 8, 0, time.UTC).Unix(),
NotAfter: time.Date(2021, 10, 12, 17, 59, 8, 0, time.UTC).Unix(),
},
{
SerialNumber: "32897DA7B113DAB6",
NotBefore: time.Date(2021, 7, 14, 21, 11, 43, 0, time.UTC).Unix(),
NotAfter: time.Date(2021, 10, 12, 21, 11, 43, 0, time.UTC).Unix(),
},
}
mockStatsIssuedData = []mockCertMeta{
{
SerialNumber: "741DAF9EC2D5F7DC",
NotBefore: time.Date(2021, 6, 18, 16, 29, 51, 0, time.UTC).Unix(),
NotAfter: time.Date(2021, 9, 16, 16, 29, 51, 0, time.UTC).Unix(),
},
{
SerialNumber: "87BC1DC5524A2B18",
NotBefore: time.Date(2021, 6, 19, 12, 5, 37, 0, time.UTC).Unix(),
NotAfter: time.Date(2021, 9, 17, 12, 5, 37, 0, time.UTC).Unix(),
},
{
SerialNumber: "F488BCE14A56CD2A",
NotBefore: time.Date(2021, 6, 19, 17, 59, 8, 0, time.UTC).Unix(),
NotAfter: time.Date(2021, 9, 17, 17, 59, 8, 0, time.UTC).Unix(),
},
}
mockTrustChainCerts = []*x509.Certificate{
mustReadCertFromFile("testdata/test_ica_cert.pem"),
mustReadCertFromFile("testdata/test_root_cert.pem"),
}
)
func newMockClient(t *testing.T) (*hvclient.Client, func()) {
t.Helper()
var server = newMockServer(t)
var ctx, cancel = context.WithCancel(context.Background())
defer cancel()
var client, err = hvclient.NewClient(ctx, &hvclient.Config{
URL: server.URL,
APIKey: mockAPIKey,
APISecret: mockAPISecret,
ExtraHeaders: map[string]string{
sslClientSerialHeader: mockSSLClientSerial,
},
})
if err != nil {
server.Close()
t.Fatalf("failed to create new client: %v", err)
}
return client, server.Close
}
// newMockServer returns an *httptest.Server which mocks the HVCA API.
func newMockServer(t *testing.T) *httptest.Server {
t.Helper()
var r = chi.NewRouter()
r.Route("/certificates", func(r chi.Router) {
r.Post("/", mockCertificatesRequest)
r.Route("/{serial}", func(r chi.Router) {
r.Get("/", mockCertificatesRetrieve)
r.Patch("/", mockCertificatesRevoke)
})
})
r.Route("/claims", func(r chi.Router) {
r.Route("/domains", func(r chi.Router) {
r.Get("/", mockClaimsDomains)
r.Route("/{arg}", func(r chi.Router) {
r.Post("/", mockClaimsSubmit)
r.Get("/", mockClaimsRetrieve)
r.Delete("/", mockClaimsDelete)
r.Route("/dns", func(r chi.Router) {
r.Post("/", mockClaimsDNS)
})
r.Route("/http", func(r chi.Router) {
r.Post("/", mockClaimsHTTP)
})
r.Route("/email", func(r chi.Router) {
r.Get("/", mockClaimsEmailRetrieve)
r.Post("/", mockClaimsEmail)
})
r.Route("/reassert", func(r chi.Router) {
r.Post("/", mockClaimsReassert)
})
})
})
})
r.Route("/counters", func(r chi.Router) {
r.Route("/certificates", func(r chi.Router) {
r.Route("/issued", func(r chi.Router) { r.Get("/", mockCountersIssued) })
r.Route("/revoked", func(r chi.Router) { r.Get("/", mockCountersRevoked) })
})
})
r.Route("/login", func(r chi.Router) { r.Post("/", mockLogin) })
r.Route("/quotas", func(r chi.Router) {
r.Route("/issuance", func(r chi.Router) { r.Get("/", mockQuotasIssuance) })
})
r.Route("/stats", func(r chi.Router) {
r.Route("/expiring", func(r chi.Router) { r.Get("/", mockStatsExpiring) })
r.Route("/issued", func(r chi.Router) { r.Get("/", mockStatsIssued) })
r.Route("/revoked", func(r chi.Router) { r.Get("/", mockStatsRevoked) })
})
r.Route("/trustchain", func(r chi.Router) { r.Get("/", mockTrustChain) })
r.Route("/validationpolicy", func(r chi.Router) { r.Get("/", mockValidationPolicy) })
return httptest.NewServer(r)
}
// mockCertificatesRequest mocks a POST /certificates operation.
func mockCertificatesRequest(w http.ResponseWriter, r *http.Request) {
var body hvclient.Request
var err = mockUnmarshalBody(w, r, &body)
if err != nil {
return
}
// Trigger 422 for specific common name.
if body.Subject != nil && body.Subject.CommonName == triggerError {
mockWriteError(w, http.StatusUnprocessableEntity)
return
}
w.Header().Set("Location", fmt.Sprintf("http://local/certificates/%X", mockCert.SerialNumber))
mockWriteResponse(w, http.StatusCreated, nil)
}
// mockCertificatesRetrieve mocks a GET /certificates operation.
func mockCertificatesRetrieve(w http.ResponseWriter, r *http.Request) {
// Extract serial number from URL.
var sn, ok = big.NewInt(0).SetString(chi.URLParam(r, "serial"), 16)
if !ok {
mockWriteError(w, http.StatusUnprocessableEntity)
return
}
// Trigger 404 for specific serial number.
if sn.Cmp(mockBigIntNotFound) == 0 {
mockWriteError(w, http.StatusNotFound)
return
}
mockWriteResponse(w, http.StatusOK, mockCertInfo{
PEM: pki.CertToPEMString(mockCert),
Status: "ISSUED",
UpdatedAt: mockDateUpdated.Unix(),
})
}
// mockCertificatesRevoke mocks a DELETE /certificates operation.
func mockCertificatesRevoke(w http.ResponseWriter, r *http.Request) {
// Extract serial number from URL.
var sn, ok = big.NewInt(0).SetString(chi.URLParam(r, "serial"), 16)
if !ok {
mockWriteError(w, http.StatusUnprocessableEntity)
return
}
// Unmarshal body.
var body mockRevocationBody
var err = mockUnmarshalBody(w, r, &body)
if err != nil {
fmt.Printf("%s\n", err)
return
}
// Return 404 for specific serial number.
if sn.Cmp(mockBigIntNotFound) == 0 {
mockWriteError(w, http.StatusNotFound)
return
}
mockWriteResponse(w, http.StatusNoContent, nil)
}
// mockClaimsDelete mocks a DELETE /claims/domains/{id} operation.
func mockClaimsDelete(w http.ResponseWriter, r *http.Request) {
var id = chi.URLParam(r, "arg")
// Trigger 404 for specific ID
if id == triggerError {
mockWriteError(w, http.StatusNotFound)
return
}
mockWriteResponse(w, http.StatusNoContent, nil)
}
// mockClaimsDNS mocks a POST /claims/domains/{id}/dns operation.
func mockClaimsDNS(w http.ResponseWriter, r *http.Request) {
var id = chi.URLParam(r, "arg")
// Trigger 404 for specific ID
if id == triggerError {
mockWriteError(w, http.StatusNotFound)
return
}
// Unmarshal body.
var body mockDNSRequest
var err = mockUnmarshalBody(w, r, &body)
if err != nil {
return
}
if body.AuthorizationDomain == mockClaimDomainVerified {
mockWriteResponse(w, http.StatusNoContent, nil)
return
}
mockWriteResponse(w, http.StatusCreated, nil)
}
// mockClaimsEmail mocks a POST /claims/domains/{id}/email operation.
func mockClaimsEmail(w http.ResponseWriter, r *http.Request) {
var id = chi.URLParam(r, "arg")
// Trigger 404 for specific ID
if id == triggerError {
mockWriteError(w, http.StatusNotFound)
return
}
// Unmarshal body.
var body mockEmailRequest
var err = mockUnmarshalBody(w, r, &body)
if err != nil {
return
}
if body.Email == mockClaimEmail {
mockWriteResponse(w, http.StatusNoContent, nil)
return
}
mockWriteResponse(w, http.StatusCreated, nil)
}
// mockClaimsEmailRetrieve mocks a GET /claims/domains/{id}/email operation.
func mockClaimsEmailRetrieve(w http.ResponseWriter, r *http.Request) {
var id = chi.URLParam(r, "arg")
// Trigger 404 for specific ID
if id == triggerError {
mockWriteError(w, http.StatusNotFound)
return
}
var mockResponse = mockAuthorisedEmails{
Constructed: []string{
},
DNS: mockDNSResults{
SOA: mockSOAResults{
Emails: []string{
},
},
},
}
mockWriteResponse(w, http.StatusOK, &mockResponse)
}
// mockClaimsHTTP mocks a POST /claims/domains/{id}/http operation.
func mockClaimsHTTP(w http.ResponseWriter, r *http.Request) {
var id = chi.URLParam(r, "arg")
// Trigger 404 for specific ID
if id == triggerError {
mockWriteError(w, http.StatusNotFound)
return
}
// Unmarshal body.
var body mockHTTPRequest
var err = mockUnmarshalBody(w, r, &body)
if err != nil {
return
}
if body.AuthorizationDomain == mockClaimDomainVerified {
mockWriteResponse(w, http.StatusNoContent, nil)
return
}
mockWriteResponse(w, http.StatusCreated, nil)
}
// mockClaimsDomains mocks a GET /claims/domains operation.
func mockClaimsDomains(w http.ResponseWriter, r *http.Request) {
var status string
if vals := r.URL.Query()["status"]; len(vals) > 0 {
status = vals[0]
}
var entries []mockClaim
for _, entry := range mockClaimsEntries {
if (entry.Status == "VERIFIED") == (status == "VERIFIED") {
entries = append(entries, entry)
}
}
w.Header().Set("Total-Count", fmt.Sprintf("%d", len(entries)))
mockWriteResponse(w, http.StatusOK, entries)
}
// mockClaimsSubmit mocks a POST /claims/domains/{domain} operation.
func mockClaimsSubmit(w http.ResponseWriter, r *http.Request) {
var domain = chi.URLParam(r, "arg")
// Trigger 422 for specific domain
if domain == triggerError {
mockWriteError(w, http.StatusUnprocessableEntity)
return
}
w.Header().Set("Location", fmt.Sprintf("http://local/claims/domains/%s", mockClaimAssert.ID))
mockWriteResponse(w, http.StatusCreated, mockClaimAssert)
}
// mockClaimsReassert mocks a POST /claims/domains/{id}/reassert operation.
func mockClaimsReassert(w http.ResponseWriter, r *http.Request) {
var id = chi.URLParam(r, "arg")
// Trigger 422 for specific domain
if id == triggerError {
mockWriteError(w, http.StatusUnprocessableEntity)
return
}
w.Header().Set("Location", fmt.Sprintf("http://local/claims/domains/%s", mockClaimAssert.ID))
mockWriteResponse(w, http.StatusOK, mockClaimAssert)
}
// mockClaimsRetrieve mocks a GET /claims/domains/{id} operation.
func mockClaimsRetrieve(w http.ResponseWriter, r *http.Request) {
var id = chi.URLParam(r, "arg")
// Trigger 404 for specific ID
if id == triggerError {
mockWriteError(w, http.StatusNotFound)
return
}
mockWriteResponse(w, http.StatusOK, mockClaimsEntries[0])
}
// mockCountersIssued mocks a GET /counters/certificates/issued operation.
func mockCountersIssued(w http.ResponseWriter, r *http.Request) {
mockWriteResponse(w, http.StatusOK, mockCounter{Value: mockCounterIssued})
}
// mockCountersIssued mocks a GET /counters/certificates/revoked operation.
func mockCountersRevoked(w http.ResponseWriter, r *http.Request) {
mockWriteResponse(w, http.StatusOK, mockCounter{Value: mockCounterRevoked})
}
// mockLogin mocks a POST /login operation.
func mockLogin(w http.ResponseWriter, r *http.Request) {
var body mockLoginRequest
var err = mockUnmarshalBody(w, r, &body)
if err != nil {
return
}
// Trivially verify the expected SSL client serial header.
var serial = r.Header.Get(sslClientSerialHeader)
if serial != mockSSLClientSerial {
mockWriteError(w, http.StatusUnauthorized)
return
}
// Trivially verify the expected API key.
if body.APIKey != mockAPIKey {
mockWriteError(w, http.StatusUnauthorized)
return
}
mockWriteResponse(w, http.StatusOK, mockLoginResponse{Token: mockToken})
}
// mockValidationPolicy mocks a GET /validationpolicy operation.
func mockValidationPolicy(w http.ResponseWriter, r *http.Request) {
mockWriteResponse(w, http.StatusOK, mockPolicy)
}
// mockQuotasIssuance mocks a GET /quotas/issuance operation.
func mockQuotasIssuance(w http.ResponseWriter, r *http.Request) {
mockWriteResponse(w, http.StatusOK, mockCounter{Value: mockQuotaIssuance})
}
// mockStatsExpiring mocks a GET /stats/expiring operation.
func mockStatsExpiring(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Total-Count", fmt.Sprintf("%d", len(mockStatsExpiringData)))
mockWriteResponse(w, http.StatusOK, mockStatsExpiringData)
}
// mockStatsIssued mocks a GET /stats/issued operation.
func mockStatsIssued(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Total-Count", fmt.Sprintf("%d", len(mockStatsIssuedData)))
mockWriteResponse(w, http.StatusOK, mockStatsIssuedData)
}
// mockStatsRevoked mocks a GET /stats/revoked operation.
func mockStatsRevoked(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Total-Count", fmt.Sprintf("%d", len(mockStatsIssuedData[1:])))
mockWriteResponse(w, http.StatusOK, mockStatsIssuedData[1:])
}
// mockTrustChain mocks a GET /trustchain operation.
func mockTrustChain(w http.ResponseWriter, r *http.Request) {
var chain = make([]string, len(mockTrustChainCerts))
for i := range chain {
chain[i] = pki.CertToPEMString(mockTrustChainCerts[i])
}
mockWriteResponse(w, http.StatusOK, chain)
}
// mockUnmarshalBody unmarshals an HTTP request body, and writes an appropriate
// HTTP error response on failure.
func mockUnmarshalBody(w http.ResponseWriter, r *http.Request, out interface{}) error {
var err = httputils.VerifyRequestContentType(r, httputils.ContentTypeJSON)
if err != nil {
mockWriteError(w, http.StatusUnsupportedMediaType)
return err
}
// Read and parse request body.
var data []byte
data, err = ioutil.ReadAll(r.Body)
if err != nil {
mockWriteError(w, http.StatusInternalServerError)
return err
}
err = json.Unmarshal(data, &out)
if err != nil {
mockWriteError(w, http.StatusBadRequest)
return err
}
return nil
}
// mockWriteError writes an error HTTP response.
func mockWriteError(w http.ResponseWriter, status int) {
var data, err = json.Marshal(mockError{Description: http.StatusText(status)})
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
} else {
w.Header().Set(httputils.ContentTypeHeader, httputils.ContentTypeProblemJSON)
w.WriteHeader(status)
_, _ = w.Write(data)
}
}
// mockWriteResponse writes an HTTP response. If obj is not nil, it will be
// marshalled to JSON and used as the response body.
func mockWriteResponse(w http.ResponseWriter, status int, obj interface{}) {
if obj != nil {
var data, err = json.Marshal(obj)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
}
w.Header().Set(httputils.ContentTypeHeader, httputils.ContentTypeJSON)
w.WriteHeader(status)
_, _ = w.Write(data)
} else {
w.WriteHeader(status)
}
}
func mustReadCertFromFile(filename string) *x509.Certificate {
var cert, err = pki.CertFromFile(filename)
if err != nil {
panic(fmt.Sprintf("failed to open certificate at path %s: %v", filename, err))
}
return cert
}