forked from adamlaska/boulder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel_test.go
More file actions
226 lines (209 loc) · 6.67 KB
/
model_test.go
File metadata and controls
226 lines (209 loc) · 6.67 KB
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
package sa
import (
"testing"
"github.com/letsencrypt/boulder/grpc"
"github.com/letsencrypt/boulder/probs"
"github.com/letsencrypt/boulder/core"
corepb "github.com/letsencrypt/boulder/core/proto"
"github.com/letsencrypt/boulder/test"
)
func TestModelToRegistrationNilContact(t *testing.T) {
reg, err := modelToRegistration(®Model{
Key: []byte(`{"kty":"RSA","n":"AQAB","e":"AQAB"}`),
Contact: nil,
})
if err != nil {
t.Errorf("Got error from modelToRegistration: %s", err)
}
if reg.Contact == nil {
t.Errorf("Expected non-nil Contact field, got %#v", reg.Contact)
}
if len(*reg.Contact) != 0 {
t.Errorf("Expected empty Contact field, got %#v", reg.Contact)
}
}
// TestModelToRegistrationBadJSON tests that converting a model with an invalid
// JWK JSON produces the expected bad JSON error.
func TestModelToRegistrationBadJSON(t *testing.T) {
badJSON := []byte(`{`)
_, err := modelToRegistration(®Model{
Key: badJSON,
})
test.AssertError(t, err, "expected error from truncated reg model key")
var badJSONErr errBadJSON
test.AssertErrorWraps(t, err, &badJSONErr)
test.AssertEquals(t, string(badJSONErr.json), string(badJSON))
}
func TestModelToRegistrationNonNilContact(t *testing.T) {
reg, err := modelToRegistration(®Model{
Key: []byte(`{"kty":"RSA","n":"AQAB","e":"AQAB"}`),
Contact: []string{},
})
if err != nil {
t.Errorf("Got error from modelToRegistration: %s", err)
}
if reg.Contact == nil {
t.Errorf("Expected non-nil Contact field, got %#v", reg.Contact)
}
if len(*reg.Contact) != 0 {
t.Errorf("Expected empty Contact field, got %#v", reg.Contact)
}
}
func TestAuthzModel(t *testing.T) {
authzPB := &corepb.Authorization{
Id: "1",
Identifier: "example.com",
RegistrationID: 1,
Status: string(core.StatusValid),
Expires: 1234,
Challenges: []*corepb.Challenge{
{
Type: string(core.ChallengeTypeHTTP01),
Status: string(core.StatusValid),
Token: "MTIz",
Validated: 1234,
Validationrecords: []*corepb.ValidationRecord{
{
Hostname: "hostname",
Port: "port",
AddressUsed: []byte("1.2.3.4"),
Url: "url",
AddressesResolved: [][]byte{{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4}},
AddressesTried: [][]byte{{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4}},
},
},
},
},
}
model, err := authzPBToModel(authzPB)
test.AssertNotError(t, err, "authzPBToModel failed")
authzPBOut, err := modelToAuthzPB(*model)
test.AssertNotError(t, err, "modelToAuthzPB failed")
test.AssertDeepEquals(t, authzPB.Challenges, authzPBOut.Challenges)
validationErr := probs.ConnectionFailure("weewoo")
authzPB.Challenges[0].Status = string(core.StatusInvalid)
authzPB.Challenges[0].Error, err = grpc.ProblemDetailsToPB(validationErr)
test.AssertNotError(t, err, "grpc.ProblemDetailsToPB failed")
model, err = authzPBToModel(authzPB)
test.AssertNotError(t, err, "authzPBToModel failed")
authzPBOut, err = modelToAuthzPB(*model)
test.AssertNotError(t, err, "modelToAuthzPB failed")
test.AssertDeepEquals(t, authzPB.Challenges, authzPBOut.Challenges)
authzPB = &corepb.Authorization{
Id: "1",
Identifier: "example.com",
RegistrationID: 1,
Status: string(core.StatusInvalid),
Expires: 1234,
Challenges: []*corepb.Challenge{
{
Type: string(core.ChallengeTypeHTTP01),
Status: string(core.StatusInvalid),
Token: "MTIz",
Validationrecords: []*corepb.ValidationRecord{
{
Hostname: "hostname",
Port: "port",
AddressUsed: []byte("1.2.3.4"),
Url: "url",
AddressesResolved: [][]byte{{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4}},
AddressesTried: [][]byte{{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4}},
},
},
},
{
Type: string(core.ChallengeTypeDNS01),
Status: string(core.StatusInvalid),
Token: "MTIz",
Validationrecords: []*corepb.ValidationRecord{
{
Hostname: "hostname",
Port: "port",
AddressUsed: []byte("1.2.3.4"),
Url: "url",
AddressesResolved: [][]byte{{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4}},
AddressesTried: [][]byte{{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4}},
},
},
},
},
}
_, err = authzPBToModel(authzPB)
test.AssertError(t, err, "authzPBToModel didn't fail with multiple non-pending challenges")
}
// TestModelToChallengeBadJSON tests that converting a challenge model with an
// invalid validation error field or validation record field produces the
// expected bad JSON error.
func TestModelToChallengeBadJSON(t *testing.T) {
badJSON := []byte(`{`)
testCases := []struct {
Name string
Model *challModel
}{
{
Name: "Bad error field",
Model: &challModel{
Error: badJSON,
},
},
{
Name: "Bad validation record field",
Model: &challModel{
ValidationRecord: badJSON,
},
},
}
for _, tc := range testCases {
t.Run(tc.Name, func(t *testing.T) {
_, err := modelToChallenge(tc.Model)
test.AssertError(t, err, "expected error from modelToChallenge")
var badJSONErr errBadJSON
test.AssertErrorWraps(t, err, &badJSONErr)
test.AssertEquals(t, string(badJSONErr.json), string(badJSON))
})
}
}
// TestModelToOrderBADJSON tests that converting an order model with an invalid
// validation error JSON field to an Order produces the expected bad JSON error.
func TestModelToOrderBadJSON(t *testing.T) {
badJSON := []byte(`{`)
_, err := modelToOrder(&orderModel{
Error: badJSON,
})
test.AssertError(t, err, "expected error from modelToOrder")
var badJSONErr errBadJSON
test.AssertErrorWraps(t, err, &badJSONErr)
test.AssertEquals(t, string(badJSONErr.json), string(badJSON))
}
// TestPopulateAttemptedFieldsBadJSON tests that populating a challenge from an
// authz2 model with an invalid validation error or an invalid validation record
// produces the expected bad JSON error.
func TestPopulateAttemptedFieldsBadJSON(t *testing.T) {
badJSON := []byte(`{`)
testCases := []struct {
Name string
Model *authzModel
}{
{
Name: "Bad validation error field",
Model: &authzModel{
ValidationError: badJSON,
},
},
{
Name: "Bad validation record field",
Model: &authzModel{
ValidationRecord: badJSON,
},
},
}
for _, tc := range testCases {
t.Run(tc.Name, func(t *testing.T) {
err := populateAttemptedFields(*tc.Model, &corepb.Challenge{})
test.AssertError(t, err, "expected error from populateAttemptedFields")
var badJSONErr errBadJSON
test.AssertErrorWraps(t, err, &badJSONErr)
test.AssertEquals(t, string(badJSONErr.json), string(badJSON))
})
}
}