-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser_opts_test.go
102 lines (94 loc) · 2.47 KB
/
parser_opts_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
package ocmf_go
import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"testing"
"github.com/stretchr/testify/suite"
)
type parserOptsTestSuite struct {
suite.Suite
}
func (s *parserOptsTestSuite) TestParserOptions() {
curve := elliptic.P256()
privateKey, err := ecdsa.GenerateKey(curve, rand.Reader)
s.Require().NoError(err)
tests := []struct {
name string
opts []Opt
expectedOptions ParserOpts
}{
{
name: "Default options",
opts: []Opt{},
expectedOptions: ParserOpts{
withAutomaticValidation: false,
withAutomaticSignatureVerification: false,
publicKey: nil,
},
},
{
name: "With automatic validation",
opts: []Opt{
WithAutomaticValidation(),
},
expectedOptions: ParserOpts{
withAutomaticValidation: true,
withAutomaticSignatureVerification: false,
publicKey: nil,
},
},
{
name: "With automatic signature verification but public key is empty",
opts: []Opt{
WithAutomaticSignatureVerification(nil),
},
expectedOptions: ParserOpts{
withAutomaticValidation: false,
withAutomaticSignatureVerification: true,
publicKey: nil,
},
},
{
name: "With automatic signature verification",
opts: []Opt{
WithAutomaticSignatureVerification(&privateKey.PublicKey),
},
expectedOptions: ParserOpts{
withAutomaticValidation: false,
withAutomaticSignatureVerification: true,
publicKey: &privateKey.PublicKey,
},
},
{
name: "With automatic validation and signature verification",
opts: []Opt{
WithAutomaticValidation(),
WithAutomaticSignatureVerification(&privateKey.PublicKey),
},
expectedOptions: ParserOpts{
withAutomaticValidation: true,
withAutomaticSignatureVerification: true,
publicKey: &privateKey.PublicKey,
},
},
}
for _, tt := range tests {
s.T().Run(tt.name, func(t *testing.T) {
parser := NewParser(tt.opts...)
s.Equal(tt.expectedOptions, parser.opts)
})
}
}
func (s *parserOptsTestSuite) TestParserDefaultOptions() {
opts := defaultOpts()
expectedDefaults := ParserOpts{
withAutomaticValidation: false,
withAutomaticSignatureVerification: false,
publicKey: nil,
}
s.Equal(expectedDefaults, opts)
}
func TestParserOpts(t *testing.T) {
suite.Run(t, new(parserOptsTestSuite))
}