Source file src/crypto/internal/fips/cast_external_test.go

     1  // Copyright 2024 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package fips_test
     6  
     7  import (
     8  	"crypto/internal/fips"
     9  	"fmt"
    10  	"internal/testenv"
    11  	"strings"
    12  	"testing"
    13  
    14  	// Import packages that define CASTs to test them.
    15  	_ "crypto/internal/fips/hmac"
    16  	_ "crypto/internal/fips/sha256"
    17  	_ "crypto/internal/fips/sha3"
    18  	_ "crypto/internal/fips/sha512"
    19  )
    20  
    21  func TestCAST(t *testing.T) {
    22  	if len(fips.AllCASTs) == 0 {
    23  		t.Errorf("no CASTs to test")
    24  	}
    25  
    26  	for _, name := range fips.AllCASTs {
    27  		t.Logf("CAST %s completed successfully", name)
    28  	}
    29  
    30  	t.Run("SimulateFailures", func(t *testing.T) {
    31  		testenv.MustHaveExec(t)
    32  		for _, name := range fips.AllCASTs {
    33  			t.Run(name, func(t *testing.T) {
    34  				t.Parallel()
    35  				cmd := testenv.Command(t, testenv.Executable(t), "-test.run=TestCAST", "-test.v")
    36  				cmd = testenv.CleanCmdEnv(cmd)
    37  				cmd.Env = append(cmd.Env, fmt.Sprintf("GODEBUG=failfipscast=%s", name))
    38  				out, err := cmd.CombinedOutput()
    39  				if err == nil {
    40  					t.Error(err)
    41  				} else {
    42  					t.Logf("CAST %s failed and caused the program to exit", name)
    43  					t.Logf("%s", out)
    44  				}
    45  				if strings.Contains(string(out), "completed successfully") {
    46  					t.Errorf("CAST %s failure did not stop the program", name)
    47  				}
    48  			})
    49  		}
    50  	})
    51  }
    52  

View as plain text