Source file src/crypto/internal/fips140/compile_test.go

     1  // Copyright 2026 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 fips140_test
     6  
     7  import (
     8  	"crypto/internal/fips140"
     9  	"slices"
    10  	"testing"
    11  )
    12  
    13  // This test checks that we can compile and link specific
    14  // code patterns in a FIPS package, where there are restrictions
    15  // on what relocations are allowed to use.
    16  // Also checks that the code inside and outside of FIPS mode
    17  // produce same result. The code in fips140.Literals and
    18  // wantLiterals are the same. Keep them in sync.
    19  
    20  func wantLiterals() (a []fips140.TestType, b []fips140.TestType2) {
    21  	a = append(a, fips140.TestType{
    22  		A: "a",
    23  		B: "",
    24  		C: "",
    25  	})
    26  	a = append(a, fips140.TestType{
    27  		A: "a",
    28  		B: fips140.DynamicString(),
    29  		C: "",
    30  	})
    31  	b = append(b, fips140.TestType2{
    32  		A: "a",
    33  		B: "",
    34  		C: "",
    35  		D: "",
    36  		E: "",
    37  	})
    38  	b = append(b, fips140.TestType2{
    39  		A: "a",
    40  		B: fips140.DynamicString(),
    41  		C: "",
    42  		D: "d",
    43  		E: fips140.DynamicString(),
    44  	})
    45  	return a, b
    46  }
    47  
    48  func TestCompile(t *testing.T) {
    49  	wantA, wantB := wantLiterals()
    50  	gotA, gotB := fips140.Literals()
    51  	if !slices.Equal(gotA, wantA) {
    52  		t.Errorf("FIPS and non-FIPS mode produce different results: want %q, got %q", wantA, gotA)
    53  	}
    54  	if !slices.Equal(gotB, wantB) {
    55  		t.Errorf("FIPS and non-FIPS mode produce different results: want %q, got %q", wantB, gotB)
    56  	}
    57  }
    58  

View as plain text