9
9
"database/sql"
10
10
"os"
11
11
"slices"
12
- "sort"
13
12
"strings"
14
13
"testing"
15
14
"time"
@@ -30,6 +29,53 @@ func collectUserAgentsFromDNSRequests(requests []challtestsrvclient.DNSRequest)
30
29
return userAgents
31
30
}
32
31
32
+ func assertUserAgentsLength (t * testing.T , got []string , checkType string ) {
33
+ t .Helper ()
34
+
35
+ if os .Getenv ("BOULDER_CONFIG_DIR" ) != "test/config-next" {
36
+ // We only need 3 checks if the MPICFullResults feature-flag is not
37
+ // enabled.
38
+ //
39
+ // TODO(#8121): Remove this once MPICFullResults has been defaulted to
40
+ // true.
41
+ if len (got ) != 4 && len (got ) != 3 {
42
+ t .Errorf ("During %s, expected 3 or 4 User-Agents, got %d" , checkType , len (got ))
43
+ }
44
+ } else {
45
+ if len (got ) != 4 {
46
+ t .Errorf ("During %s, expected 4 User-Agents, got %d" , checkType , len (got ))
47
+ }
48
+ }
49
+ }
50
+
51
+ func assertExpectedUserAgents (t * testing.T , got []string , checkType string ) {
52
+ t .Helper ()
53
+
54
+ if os .Getenv ("BOULDER_CONFIG_DIR" ) != "test/config-next" {
55
+ // One User-Agent may be missing if the MPICFullResults feature-flag is
56
+ // not enabled. This will need to be modified to 2 if we have not
57
+ // removed this feature-flag by the time we get to 6+ perspectives.
58
+ //
59
+ // TODO(#8121): Remove this once MPICFullResults has been defaulted to
60
+ // true.
61
+ var alreadySkippedOne bool
62
+ for _ , ua := range expectedUserAgents {
63
+ if ! slices .Contains (got , ua ) {
64
+ if alreadySkippedOne {
65
+ t .Errorf ("During %s, missing more than 1 User-Agent in %s (got %v)" , checkType , expectedUserAgents , got )
66
+ }
67
+ alreadySkippedOne = true
68
+ }
69
+ }
70
+ } else {
71
+ for _ , ua := range expectedUserAgents {
72
+ if ! slices .Contains (got , ua ) {
73
+ t .Errorf ("During %s, expected User-Agent %q in %s (got %v)" , checkType , ua , expectedUserAgents , got )
74
+ }
75
+ }
76
+ }
77
+ }
78
+
33
79
func TestMPICTLSALPN01 (t * testing.T ) {
34
80
t .Parallel ()
35
81
@@ -105,29 +151,14 @@ func TestMPICTLSALPN01(t *testing.T) {
105
151
caaEvents = append (caaEvents , event )
106
152
}
107
153
}
108
-
109
- if os .Getenv ("BOULDER_CONFIG_DIR" ) == "test/config-next" && len (caaEvents ) != 4 {
110
- t .Errorf ("expected 4 CAA checks got %d" , len (caaEvents ))
111
- } else if len (caaEvents ) != 3 && len (caaEvents ) != 4 {
112
- // We only need 3 checks if the MPICFullResults feature-flag is not
113
- // enabled.
114
- //
115
- // TODO(#8121): Remove this once MPICFullResults has been defaulted to
116
- // true.
117
- t .Errorf ("expected 3 or 4 CAA checks got %d" , len (caaEvents ))
118
- }
154
+ assertUserAgentsLength (t , collectUserAgentsFromDNSRequests (caaEvents ), "CAA check" )
119
155
if os .Getenv ("BOULDER_CONFIG_DIR" ) == "test/config-next" {
120
156
// We can only check the user-agent for DNS requests if the DOH
121
157
// feature-flag is enabled.
122
158
//
123
- // TODO(#8120): Remove this once the DoH feature flag has been defaulted
124
- // to true.
125
- gotUserAgents := collectUserAgentsFromDNSRequests (caaEvents )
126
- for _ , ua := range expectedUserAgents {
127
- if ! slices .Contains (gotUserAgents , ua ) {
128
- t .Errorf ("expected a query from User-Agent %q but did not get one (got %+v)." , ua , gotUserAgents )
129
- }
130
- }
159
+ // TODO(#8120): Remove this conditional once the DoH feature flag has
160
+ // been defaulted to true.
161
+ assertExpectedUserAgents (t , collectUserAgentsFromDNSRequests (caaEvents ), "CAA check" )
131
162
}
132
163
}
133
164
@@ -183,28 +214,14 @@ func TestMPICDNS01(t *testing.T) {
183
214
validationEvents = append (validationEvents , event )
184
215
}
185
216
}
186
- if os .Getenv ("BOULDER_CONFIG_DIR" ) == "test/config-next" && len (validationEvents ) != 4 {
187
- t .Errorf ("expected 4 validation events got %d" , len (validationEvents ))
188
- } else if len (validationEvents ) != 3 && len (validationEvents ) != 4 {
189
- // We only need 3 checks if the MPICFullResults feature-flag is not
190
- // enabled.
191
- //
192
- // TODO(#8121): Remove this once MPICFullResults has been defaulted to
193
- // true.
194
- t .Errorf ("expected 3 or 4 validation events got %d" , len (validationEvents ))
195
- }
217
+ assertUserAgentsLength (t , collectUserAgentsFromDNSRequests (validationEvents ), "DNS-01 validation" )
196
218
if os .Getenv ("BOULDER_CONFIG_DIR" ) == "test/config-next" {
197
219
// We can only check the user-agent for DNS requests if the DOH
198
220
// feature-flag is enabled.
199
221
//
200
222
// TODO(#8120): Remove this once the DoH feature flag has been defaulted
201
223
// to true.
202
- gotUserAgents := collectUserAgentsFromDNSRequests (validationEvents )
203
- for _ , ua := range expectedUserAgents {
204
- if ! slices .Contains (gotUserAgents , ua ) {
205
- t .Errorf ("expected a query from User-Agent %q but did not get one (got %+v)." , ua , gotUserAgents )
206
- }
207
- }
224
+ assertExpectedUserAgents (t , collectUserAgentsFromDNSRequests (validationEvents ), "DNS-01 validation" )
208
225
}
209
226
210
227
domainDNSEvents , err := testSrvClient .DNSRequestHistory (domain )
@@ -218,29 +235,14 @@ func TestMPICDNS01(t *testing.T) {
218
235
caaEvents = append (caaEvents , event )
219
236
}
220
237
}
221
-
222
- if os .Getenv ("BOULDER_CONFIG_DIR" ) == "test/config-next" && len (caaEvents ) != 4 {
223
- t .Errorf ("expected 4 CAA checks got %d" , len (caaEvents ))
224
- } else if len (caaEvents ) != 3 && len (caaEvents ) != 4 {
225
- // We only need 3 checks if the MPICFullResults feature-flag is not
226
- // enabled.
227
- //
228
- // TODO(#8121): Remove this once MPICFullResults has been defaulted to
229
- // true.
230
- t .Errorf ("expected 3 or 4 CAA checks got %d" , len (caaEvents ))
231
- }
238
+ assertUserAgentsLength (t , collectUserAgentsFromDNSRequests (caaEvents ), "CAA check" )
232
239
if os .Getenv ("BOULDER_CONFIG_DIR" ) == "test/config-next" {
233
240
// We can only check the user-agent for DNS requests if the DOH
234
241
// feature-flag is enabled.
235
242
//
236
243
// TODO(#8120): Remove this once the DoH feature flag has been defaulted
237
244
// to true.
238
- gotUserAgents := collectUserAgentsFromDNSRequests (caaEvents )
239
- for _ , ua := range expectedUserAgents {
240
- if ! slices .Contains (gotUserAgents , ua ) {
241
- t .Errorf ("expected a query from User-Agent %q but did not get one (got %+v)." , ua , gotUserAgents )
242
- }
243
- }
245
+ assertExpectedUserAgents (t , collectUserAgentsFromDNSRequests (caaEvents ), "CAA check" )
244
246
}
245
247
}
246
248
@@ -290,41 +292,14 @@ func TestMPICHTTP01(t *testing.T) {
290
292
t .Fatal (err )
291
293
}
292
294
293
- validationCount := 0
295
+ var validationUAs [] string
294
296
for _ , event := range validationEvents {
295
297
if event .URL == "/.well-known/acme-challenge/" + chal .Token {
296
- validationCount ++
297
- }
298
- }
299
-
300
- if os .Getenv ("BOULDER_CONFIG_DIR" ) == "test/config-next" && validationCount != 4 {
301
- t .Errorf ("expected 4 validation events got %d" , validationCount )
302
- } else if validationCount != 3 && validationCount != 4 {
303
- // We only need 3 checks if the MPICFullResults feature-flag is not
304
- // enabled.
305
- //
306
- // TODO(#8121): Remove this once MPICFullResults has been defaulted to
307
- // true.
308
- t .Errorf ("expected 3 or 4 validation events got %d" , validationCount )
309
- }
310
-
311
- sort .Slice (validationEvents , func (i , j int ) bool {
312
- return validationEvents [i ].UserAgent < validationEvents [j ].UserAgent
313
- })
314
- for i , event := range validationEvents {
315
- if event .UserAgent != expectedUserAgents [i ] {
316
- t .Errorf ("expected user agent %s, got %s" , expectedUserAgents [i ], event .UserAgent )
317
- }
318
- }
319
-
320
- sort .Slice (validationEvents , func (i , j int ) bool {
321
- return validationEvents [i ].UserAgent < validationEvents [j ].UserAgent
322
- })
323
- for i , event := range validationEvents {
324
- if event .UserAgent != expectedUserAgents [i ] {
325
- t .Errorf ("expected user agent %s, got %s" , expectedUserAgents [i ], event .UserAgent )
298
+ validationUAs = append (validationUAs , event .UserAgent )
326
299
}
327
300
}
301
+ assertUserAgentsLength (t , validationUAs , "HTTP-01 validation" )
302
+ assertExpectedUserAgents (t , validationUAs , "HTTP-01 validation" )
328
303
329
304
dnsEvents , err := testSrvClient .DNSRequestHistory (domain )
330
305
if err != nil {
@@ -338,31 +313,14 @@ func TestMPICHTTP01(t *testing.T) {
338
313
}
339
314
}
340
315
341
- if os .Getenv ("BOULDER_CONFIG_DIR" ) == "test/config-next" && len (caaEvents ) != 4 {
342
- t .Errorf ("expected 4 CAA checks got %d" , len (caaEvents ))
343
- } else if len (caaEvents ) != 3 && len (caaEvents ) != 4 {
344
- // We only need 3 checks if the MPICFullResults feature-flag is not
345
- // enabled.
346
- //
347
- // TODO(#8121): Remove this once MPICFullResults has been defaulted to
348
- // true.
349
- t .Errorf ("expected 3 or 4 CAA checks got %d" , len (caaEvents ))
350
- }
316
+ assertUserAgentsLength (t , collectUserAgentsFromDNSRequests (caaEvents ), "CAA check" )
351
317
if os .Getenv ("BOULDER_CONFIG_DIR" ) == "test/config-next" {
352
318
// We can only check the user-agent for DNS requests if the DOH
353
319
// feature-flag is enabled.
354
320
//
355
321
// TODO(#8120): Remove this once the DoH feature flag has been defaulted
356
322
// to true.
357
- sort .Slice (caaEvents , func (i , j int ) bool {
358
- return caaEvents [i ].UserAgent < caaEvents [j ].UserAgent
359
- })
360
-
361
- for i , event := range caaEvents {
362
- if event .UserAgent != expectedUserAgents [i ] {
363
- t .Errorf ("expected user agent %s, got %s" , expectedUserAgents [i ], event .UserAgent )
364
- }
365
- }
323
+ assertExpectedUserAgents (t , collectUserAgentsFromDNSRequests (caaEvents ), "CAA check" )
366
324
}
367
325
}
368
326
0 commit comments