@@ -27,7 +27,7 @@ import (
2727 "encoding/pem"
2828 "errors"
2929 "flag"
30- "io/ioutil "
30+ "io"
3131 "log"
3232 "net"
3333 "net/http"
@@ -250,14 +250,18 @@ func TestCACerts(t *testing.T) {
250250 var testcases = []struct {
251251 name string
252252 args []string
253- err error
253+ err [] error
254254 }{
255255 {
256256 name : "NoAnchor" ,
257257 args : []string {
258258 "-" + serverFlag , uri ,
259259 },
260- err : errors .New ("certificate signed by unknown authority" ),
260+ err : []error {
261+ errors .New ("failed to verify certificate" ),
262+ errors .New ("certificate is not trusted" ),
263+ errors .New ("certificate signed by unknown authority" ),
264+ },
261265 },
262266 {
263267 name : "Insecure" ,
@@ -289,7 +293,7 @@ func TestCACerts(t *testing.T) {
289293 "-" + apsFlag , "triggererrors" ,
290294 "-" + explicitAnchorFlag , cafile ,
291295 },
292- err : errors .New ("internal server error" ),
296+ err : [] error { errors .New ("internal server error" )} ,
293297 },
294298 }
295299
@@ -299,7 +303,7 @@ func TestCACerts(t *testing.T) {
299303 t .Run (tc .name , func (t * testing.T ) {
300304 buf := bytes .NewBuffer ([]byte {})
301305 err := cacerts (buf , makeCmdFlagSet (t , cacertsCmd , tc .args ))
302- verifyErrorTextContains (t , err , tc .err )
306+ verifyErrorTextContainsOneOf (t , err , tc .err )
303307 if tc .err != nil {
304308 return
305309 }
@@ -322,14 +326,18 @@ func TestCSRAttrs(t *testing.T) {
322326 name string
323327 args []string
324328 length int
325- err error
329+ err [] error
326330 }{
327331 {
328332 name : "NoAnchor" ,
329333 args : []string {
330334 "-" + serverFlag , uri ,
331335 },
332- err : errors .New ("certificate signed by unknown authority" ),
336+ err : []error {
337+ errors .New ("failed to verify certificate" ),
338+ errors .New ("certificate is not trusted" ),
339+ errors .New ("certificate signed by unknown authority" ),
340+ },
333341 },
334342 {
335343 name : "Insecure" ,
@@ -364,7 +372,9 @@ func TestCSRAttrs(t *testing.T) {
364372 "-" + apsFlag , "triggererrors" ,
365373 "-" + explicitAnchorFlag , cafile ,
366374 },
367- err : errors .New ("internal server error" ),
375+ err : []error {
376+ errors .New ("internal server error" ),
377+ },
368378 },
369379 }
370380
@@ -374,7 +384,7 @@ func TestCSRAttrs(t *testing.T) {
374384 t .Run (tc .name , func (t * testing.T ) {
375385 buf := bytes .NewBuffer ([]byte {})
376386 err := csrattrs (buf , makeCmdFlagSet (t , csrattrsCmd , tc .args ))
377- verifyErrorTextContains (t , err , tc .err )
387+ verifyErrorTextContainsOneOf (t , err , tc .err )
378388
379389 if tc .err != nil {
380390 return
@@ -507,7 +517,7 @@ func TestReenroll(t *testing.T) {
507517 tc := tc
508518
509519 t .Run (tc .name , func (t * testing.T ) {
510- f , err := ioutil . TempFile ("" , "reenroll_test_" )
520+ f , err := os . CreateTemp ("" , "reenroll_test_" )
511521 if err != nil {
512522 t .Fatalf ("failed to create temporary file: %v" , err )
513523 }
@@ -809,9 +819,9 @@ func newTestServer(t *testing.T) (*httptest.Server, *x509.Certificate, string) {
809819 }
810820
811821 checkBasicAuth := func (
812- ctx context.Context ,
813- r * http.Request ,
814- aps , username , password string ,
822+ _ context.Context ,
823+ _ * http.Request ,
824+ _ , username , password string ,
815825 ) error {
816826 if username != "testuser" || password != "xyzzy" {
817827 return errors .New ("bad credentials" )
@@ -834,7 +844,7 @@ func newTestServer(t *testing.T) (*httptest.Server, *x509.Certificate, string) {
834844
835845 s := httptest .NewUnstartedServer (r )
836846
837- s .Config .ErrorLog = log .New (ioutil .Discard , "" , 0 )
847+ s .Config .ErrorLog = log .New (io .Discard , "" , 0 )
838848
839849 var clientCAs = x509 .NewCertPool ()
840850 clientCAs .AddCert (caCerts [len (caCerts )- 1 ])
@@ -907,7 +917,7 @@ func makeCmdFlagSet(t *testing.T, cmd string, args []string) *flag.FlagSet {
907917 t .Fatalf ("command not recognized: %s" , cmd )
908918 }
909919
910- set := fcmd .FlagSet (ioutil .Discard , 80 )
920+ set := fcmd .FlagSet (io .Discard , 80 )
911921 if err := set .Parse (args ); err != nil {
912922 t .Fatalf ("failed to parse flag set: %v" , err )
913923 }
@@ -918,7 +928,7 @@ func makeCmdFlagSet(t *testing.T, cmd string, args []string) *flag.FlagSet {
918928func makeRootCertFile (t * testing.T , cert * x509.Certificate ) (string , func (t * testing.T )) {
919929 t .Helper ()
920930
921- f , err := ioutil . TempFile ("" , "estclient_test_" )
931+ f , err := os . CreateTemp ("" , "estclient_test_" )
922932 if err != nil {
923933 t .Fatalf ("failed to create temporary file: %v" , err )
924934 }
@@ -951,6 +961,38 @@ func verifyErrorTextContains(t *testing.T, got, want error) {
951961 }
952962}
953963
964+ // verifyErrorTextContainsOneOf tests if the error text contains one of the strings.
965+ // This is useful for testing errors that output different text on different
966+ // platforms or between versions.
967+ func verifyErrorTextContainsOneOf (t * testing.T , got error , wants []error ) {
968+ t .Helper ()
969+
970+ if got == nil && len (wants ) == 0 {
971+ return
972+ }
973+
974+ if got == nil && len (wants ) > 0 {
975+ t .Fatalf ("got nil, want one of %v" , wants )
976+ }
977+
978+ if got != nil && len (wants ) == 0 {
979+ t .Fatalf ("got %v, want no error" , got )
980+ }
981+
982+ // got != nil && len(wants) > 0
983+
984+ contains := false
985+ for _ , w := range wants {
986+ if strings .Contains (got .Error (), w .Error ()) {
987+ contains = true
988+ }
989+ }
990+
991+ if ! contains {
992+ t .Fatalf ("got error %v, want one of %v" , got , wants )
993+ }
994+ }
995+
954996// assertPKIXNamesEqual tests if two pkix.Name objects are equal in all
955997// respects other than the ordering of the name attributes.
956998func assertPKIXNamesEqual (t * testing.T , first , second pkix.Name ) {
@@ -972,11 +1014,7 @@ func assertPKIXNamesEqual(t *testing.T, first, second pkix.Name) {
9721014 }
9731015 }
9741016
975- if s [i ].Value .(string ) < s [j ].Value .(string ) {
976- return true
977- }
978-
979- return false
1017+ return s [i ].Value .(string ) < s [j ].Value .(string )
9801018 }
9811019 }
9821020
0 commit comments