Source file src/crypto/internal/fips140test/mlkem_generate_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 fipstest
     6  
     7  import (
     8  	"bytes"
     9  	"crypto/internal/fips140"
    10  	"internal/diff"
    11  	"internal/testenv"
    12  	"os"
    13  	"path/filepath"
    14  	"strings"
    15  	"testing"
    16  )
    17  
    18  // TestMLKEM1024UpToDate checks that mlkem1024.go matches the output of
    19  // generate1024.go, so that changes to mlkem768.go are not left out of the
    20  // generated file.
    21  func TestMLKEM1024UpToDate(t *testing.T) {
    22  	testenv.MustHaveSource(t)
    23  	testenv.MustHaveGoRun(t)
    24  
    25  	if testing.Short() {
    26  		t.Skip("skipping in short mode")
    27  	}
    28  
    29  	if fips140.Version() != "latest" {
    30  		t.Skip("skipping for frozen FIPS 140-3 module snapshot")
    31  	}
    32  
    33  	// Ask "go list" for the location of the package, as it might be the
    34  	// unpacked frozen tree selected with GOFIPS140.
    35  	cmd := testenv.Command(t, testenv.GoToolPath(t), "list", "-f", `{{.Dir}}`, "crypto/internal/fips140/mlkem")
    36  	out, err := testenv.CleanCmdEnv(cmd).CombinedOutput()
    37  	if err != nil {
    38  		t.Fatalf("go list: %v\n%s", err, out)
    39  	}
    40  	dir := strings.TrimSpace(string(out))
    41  
    42  	generated := filepath.Join(t.TempDir(), "mlkem1024.go")
    43  	cmd = testenv.Command(t, testenv.GoToolPath(t), "run", "generate1024.go", "-input", "mlkem768.go", "-output", generated)
    44  	cmd.Dir = dir
    45  	if out, err := testenv.CleanCmdEnv(cmd).CombinedOutput(); err != nil {
    46  		t.Fatalf("go run generate1024.go: %v\n%s", err, out)
    47  	}
    48  
    49  	want, err := os.ReadFile(generated)
    50  	if err != nil {
    51  		t.Fatal(err)
    52  	}
    53  	got, err := os.ReadFile(filepath.Join(dir, "mlkem1024.go"))
    54  	if err != nil {
    55  		t.Fatal(err)
    56  	}
    57  	if !bytes.Equal(got, want) {
    58  		t.Errorf("mlkem1024.go is out of date; run go generate in %s\n%s", dir,
    59  			diff.Diff("mlkem1024.go", got, "generate1024.go", want))
    60  	}
    61  }
    62  

View as plain text