-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcose_test.go
73 lines (69 loc) · 1.53 KB
/
cose_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
package mdoc
import (
"crypto/x509"
"crypto/x509/pkix"
"errors"
"github.com/google/go-cmp/cmp"
"github.com/veraison/go-cose"
"testing"
"time"
)
func TestX509Chain(t *testing.T) {
rand := NewDeterministicRand()
cert := createCA(t, rand, x509.Certificate{
Subject: pkix.Name{CommonName: "cert"},
Issuer: pkix.Name{CommonName: "cert"},
NotBefore: time.UnixMilli(1000),
NotAfter: time.UnixMilli(2000),
}).cert
tests := []struct {
name string
unprotectedHeaders cose.UnprotectedHeader
want []*x509.Certificate
wantErr error
}{
{
name: "individual cert",
unprotectedHeaders: cose.UnprotectedHeader{
cose.HeaderLabelX5Chain: cert.Raw,
},
want: []*x509.Certificate{
cert,
},
},
{
name: "multiple certs",
unprotectedHeaders: cose.UnprotectedHeader{
cose.HeaderLabelX5Chain: [][]byte{
cert.Raw,
cert.Raw,
},
},
want: []*x509.Certificate{
cert,
cert,
},
},
{
name: "incorrect type",
unprotectedHeaders: cose.UnprotectedHeader{
cose.HeaderLabelX5Chain: 123,
},
wantErr: ErrUnrecognisedHeaderType,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := x509Chain(tt.unprotectedHeaders)
if err != nil && !errors.Is(err, tt.wantErr) {
t.Fatalf("error = %v, wantErr %v", err, tt.wantErr)
}
if err == nil && tt.wantErr != nil {
t.Fatalf("error = %v, wantErr %v", err, tt.wantErr)
}
if diff := cmp.Diff(tt.want, got); diff != "" {
t.Error(diff)
}
})
}
}