forked from letsencrypt/boulder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtype-converter_test.go
153 lines (123 loc) · 3.79 KB
/
type-converter_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
package sa
import (
"encoding/json"
"testing"
"github.com/letsencrypt/boulder/core"
"github.com/letsencrypt/boulder/identifier"
"github.com/letsencrypt/boulder/test"
jose "gopkg.in/square/go-jose.v2"
)
const JWK1JSON = `{
"kty": "RSA",
"n": "vuc785P8lBj3fUxyZchF_uZw6WtbxcorqgTyq-qapF5lrO1U82Tp93rpXlmctj6fyFHBVVB5aXnUHJ7LZeVPod7Wnfl8p5OyhlHQHC8BnzdzCqCMKmWZNX5DtETDId0qzU7dPzh0LP0idt5buU7L9QNaabChw3nnaL47iu_1Di5Wp264p2TwACeedv2hfRDjDlJmaQXuS8Rtv9GnRWyC9JBu7XmGvGDziumnJH7Hyzh3VNu-kSPQD3vuAFgMZS6uUzOztCkT0fpOalZI6hqxtWLvXUMj-crXrn-Maavz8qRhpAyp5kcYk3jiHGgQIi7QSK2JIdRJ8APyX9HlmTN5AQ",
"e": "AQAB"
}`
func TestAcmeIdentifier(t *testing.T) {
tc := BoulderTypeConverter{}
ai := identifier.ACMEIdentifier{Type: "data1", Value: "data2"}
out := identifier.ACMEIdentifier{}
marshaledI, err := tc.ToDb(ai)
test.AssertNotError(t, err, "Could not ToDb")
scanner, ok := tc.FromDb(&out)
test.Assert(t, ok, "FromDb failed")
if !ok {
t.FailNow()
return
}
marshaled := marshaledI.(string)
err = scanner.Binder(&marshaled, &out)
test.AssertNotError(t, err, "failed to scanner.Binder")
test.AssertMarshaledEquals(t, ai, out)
}
func TestAcmeIdentifierBadJSON(t *testing.T) {
badJSON := `{`
tc := BoulderTypeConverter{}
out := identifier.ACMEIdentifier{}
scanner, _ := tc.FromDb(&out)
err := scanner.Binder(&badJSON, &out)
test.AssertError(t, err, "expected error from scanner.Binder")
badJSONErr, ok := err.(errBadJSON)
test.AssertEquals(t, ok, true)
test.AssertEquals(t, string(badJSONErr.json), string(badJSON))
}
func TestJSONWebKey(t *testing.T) {
tc := BoulderTypeConverter{}
var jwk, out jose.JSONWebKey
err := json.Unmarshal([]byte(JWK1JSON), &jwk)
if err != nil {
t.Fatal(err)
}
marshaledI, err := tc.ToDb(jwk)
test.AssertNotError(t, err, "Could not ToDb")
scanner, ok := tc.FromDb(&out)
test.Assert(t, ok, "FromDb failed")
if !ok {
t.FailNow()
return
}
marshaled := marshaledI.(string)
err = scanner.Binder(&marshaled, &out)
test.AssertNotError(t, err, "failed to scanner.Binder")
test.AssertMarshaledEquals(t, jwk, out)
}
func TestJSONWebKeyBadJSON(t *testing.T) {
badJSON := `{`
tc := BoulderTypeConverter{}
out := jose.JSONWebKey{}
scanner, _ := tc.FromDb(&out)
err := scanner.Binder(&badJSON, &out)
test.AssertError(t, err, "expected error from scanner.Binder")
badJSONErr, ok := err.(errBadJSON)
test.AssertEquals(t, ok, true)
test.AssertEquals(t, string(badJSONErr.json), string(badJSON))
}
func TestAcmeStatus(t *testing.T) {
tc := BoulderTypeConverter{}
var as, out core.AcmeStatus
as = "core.AcmeStatus"
marshaledI, err := tc.ToDb(as)
test.AssertNotError(t, err, "Could not ToDb")
scanner, ok := tc.FromDb(&out)
test.Assert(t, ok, "FromDb failed")
if !ok {
t.FailNow()
return
}
marshaled := marshaledI.(string)
err = scanner.Binder(&marshaled, &out)
test.AssertNotError(t, err, "failed to scanner.Binder")
test.AssertMarshaledEquals(t, as, out)
}
func TestOCSPStatus(t *testing.T) {
tc := BoulderTypeConverter{}
var os, out core.OCSPStatus
os = "core.OCSPStatus"
marshaledI, err := tc.ToDb(os)
test.AssertNotError(t, err, "Could not ToDb")
scanner, ok := tc.FromDb(&out)
test.Assert(t, ok, "FromDb failed")
if !ok {
t.FailNow()
return
}
marshaled := marshaledI.(string)
err = scanner.Binder(&marshaled, &out)
test.AssertNotError(t, err, "failed to scanner.Binder")
test.AssertMarshaledEquals(t, os, out)
}
func TestStringSlice(t *testing.T) {
tc := BoulderTypeConverter{}
var au, out []string
marshaledI, err := tc.ToDb(au)
test.AssertNotError(t, err, "Could not ToDb")
scanner, ok := tc.FromDb(&out)
test.Assert(t, ok, "FromDb failed")
if !ok {
t.FailNow()
return
}
marshaled := marshaledI.(string)
err = scanner.Binder(&marshaled, &out)
test.AssertNotError(t, err, "failed to scanner.Binder")
test.AssertMarshaledEquals(t, au, out)
}