99 "database/sql"
1010 "os"
1111 "slices"
12- "sort"
1312 "strings"
1413 "testing"
1514 "time"
@@ -30,6 +29,53 @@ func collectUserAgentsFromDNSRequests(requests []challtestsrvclient.DNSRequest)
3029 return userAgents
3130}
3231
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+
3379func TestMPICTLSALPN01 (t * testing.T ) {
3480 t .Parallel ()
3581
@@ -105,29 +151,14 @@ func TestMPICTLSALPN01(t *testing.T) {
105151 caaEvents = append (caaEvents , event )
106152 }
107153 }
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" )
119155 if os .Getenv ("BOULDER_CONFIG_DIR" ) == "test/config-next" {
120156 // We can only check the user-agent for DNS requests if the DOH
121157 // feature-flag is enabled.
122158 //
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" )
131162 }
132163}
133164
@@ -183,28 +214,14 @@ func TestMPICDNS01(t *testing.T) {
183214 validationEvents = append (validationEvents , event )
184215 }
185216 }
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" )
196218 if os .Getenv ("BOULDER_CONFIG_DIR" ) == "test/config-next" {
197219 // We can only check the user-agent for DNS requests if the DOH
198220 // feature-flag is enabled.
199221 //
200222 // TODO(#8120): Remove this once the DoH feature flag has been defaulted
201223 // 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" )
208225 }
209226
210227 domainDNSEvents , err := testSrvClient .DNSRequestHistory (domain )
@@ -218,29 +235,14 @@ func TestMPICDNS01(t *testing.T) {
218235 caaEvents = append (caaEvents , event )
219236 }
220237 }
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" )
232239 if os .Getenv ("BOULDER_CONFIG_DIR" ) == "test/config-next" {
233240 // We can only check the user-agent for DNS requests if the DOH
234241 // feature-flag is enabled.
235242 //
236243 // TODO(#8120): Remove this once the DoH feature flag has been defaulted
237244 // 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" )
244246 }
245247}
246248
@@ -290,41 +292,14 @@ func TestMPICHTTP01(t *testing.T) {
290292 t .Fatal (err )
291293 }
292294
293- validationCount := 0
295+ var validationUAs [] string
294296 for _ , event := range validationEvents {
295297 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 )
326299 }
327300 }
301+ assertUserAgentsLength (t , validationUAs , "HTTP-01 validation" )
302+ assertExpectedUserAgents (t , validationUAs , "HTTP-01 validation" )
328303
329304 dnsEvents , err := testSrvClient .DNSRequestHistory (domain )
330305 if err != nil {
@@ -338,31 +313,14 @@ func TestMPICHTTP01(t *testing.T) {
338313 }
339314 }
340315
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" )
351317 if os .Getenv ("BOULDER_CONFIG_DIR" ) == "test/config-next" {
352318 // We can only check the user-agent for DNS requests if the DOH
353319 // feature-flag is enabled.
354320 //
355321 // TODO(#8120): Remove this once the DoH feature flag has been defaulted
356322 // 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" )
366324 }
367325}
368326
0 commit comments