crypto/internal/cryptotest/wycheproof: avoid reading go.sum at test time

Change-Id: I63cdd2bcb855be6f799aa5ddf461b1dd6a6a6964
Reviewed-on: https://go-review.googlesource.com/c/go/+/780481
Reviewed-by: Roland Shoemaker <roland@golang.org>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Auto-Submit: Filippo Valsorda <filippo@golang.org>
LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
This commit is contained in:
Filippo Valsorda 2026-05-20 17:03:35 +02:00 committed by Gopher Robot
parent 244c8ae4c8
commit 78f63eb790
3 changed files with 75 additions and 42 deletions

View file

@ -12,10 +12,13 @@
package main
import (
"bufio"
"fmt"
"io/fs"
"log"
"os"
"path/filepath"
"strings"
"github.com/atombender/go-jsonschema/pkg/generator"
"github.com/c2sp/wycheproof"
@ -88,4 +91,61 @@ func main() {
if err := os.WriteFile(outFile, content, 0644); err != nil {
log.Fatalf("error writing file %s: %v\n", outFile, err)
}
// Write a sibling file recording the Wycheproof module version that the
// generated schema.go was produced against. The wycheproof package uses
// this constant to fetch matching test vectors at runtime. We avoid reading
// go.sum at test time because it might not be available (e.g. if the test
// binary was copied to a remote machine).
version, err := wycheproofVersionFromSum("go.sum")
if err != nil {
log.Fatalf("extracting wycheproof version: %v", err)
}
const versionFile = "../schemaversion.go"
versionSrc := fmt.Sprintf(`// Copyright 2026 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Code generated by _schema/schema_gen.go, DO NOT EDIT.
package wycheproof
// wycheproofVersion is the github.com/c2sp/wycheproof module version that
// schema.go was generated against.
const wycheproofVersion = %q
`, version)
if err := os.WriteFile(versionFile, []byte(versionSrc), 0644); err != nil {
log.Fatalf("writing %s: %v", versionFile, err)
}
}
// wycheproofVersionFromSum returns the github.com/c2sp/wycheproof module
// version recorded in the given go.sum file.
func wycheproofVersionFromSum(path string) (string, error) {
f, err := os.Open(path)
if err != nil {
return "", err
}
defer f.Close()
var version string
found := 0
scanner := bufio.NewScanner(f)
for scanner.Scan() {
fields := strings.Fields(scanner.Text())
if len(fields) == 3 && fields[0] == "github.com/c2sp/wycheproof" {
version = strings.TrimSuffix(fields[1], "/go.mod")
found++
}
}
if err := scanner.Err(); err != nil {
return "", err
}
switch {
case found == 0:
return "", fmt.Errorf("%s: missing github.com/c2sp/wycheproof entry", path)
case found > 2:
return "", fmt.Errorf("%s: %d github.com/c2sp/wycheproof entries (run 'go mod tidy')", path, found)
}
return version, nil
}

View file

@ -0,0 +1,11 @@
// Copyright 2026 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Code generated by _schema/schema_gen.go, DO NOT EDIT.
package wycheproof
// wycheproofVersion is the github.com/c2sp/wycheproof module version that
// schema.go was generated against.
const wycheproofVersion = "v0.0.0-20260428174413-4d535535851f"

View file

@ -8,7 +8,6 @@
package wycheproof
import (
"bufio"
"crypto"
"crypto/internal/cryptotest"
"encoding/hex"
@ -18,7 +17,6 @@ import (
"os"
"path"
"reflect"
"strings"
"testing"
)
@ -34,9 +32,11 @@ func LoadVectorFile(t *testing.T, filename string, value any) {
// We want to avoid a dependency on c2sp/wycheproof or the schema generator
// in this stdlib code, so we fetch the module at runtime and read the
// vector JSON from that module clone.
// vector JSON from that module clone. The version is pinned to whatever
// the _schema generator was last run against (see schemaversion.go), so
// the vectors match the generated schema.go.
wycheproofDir := cryptotest.FetchModule(
t, "github.com/c2sp/wycheproof", findVersionFromSum(t))
t, "github.com/c2sp/wycheproof", wycheproofVersion)
content, err := os.ReadFile(path.Join(wycheproofDir, "testvectors_v1", filename))
if err != nil {
@ -49,44 +49,6 @@ func LoadVectorFile(t *testing.T, filename string, value any) {
}
}
// To make sure this code fetches the same module version as we used to
// generate the vendored schema.go we parse the _schema Go module's
// go.sum to find the Wycheproof version used.
func findVersionFromSum(t *testing.T) string {
testenv.MustHaveSource(t)
goSumPath := path.Join(
testenv.GOROOT(t),
"src/crypto/internal/cryptotest/wycheproof/_schema/go.sum")
f, err := os.Open(goSumPath)
if err != nil {
t.Fatalf("_schema module go.sum read failed: %v", err)
}
defer f.Close()
var version string
found := 0
scanner := bufio.NewScanner(f)
for scanner.Scan() {
fields := strings.Fields(scanner.Text())
if len(fields) == 3 && fields[0] == "github.com/c2sp/wycheproof" {
version = strings.TrimSuffix(fields[1], "/go.mod")
found++
}
}
// We expect 2 entries for a tidied sum file.
if found > 2 {
t.Fatalf(
"_schema module requires 'go tidy' - found %d wycheproof occurrences in go.sum",
found)
} else if found == 0 {
t.Fatal("_schema module go.sum missing wycheproof dependency")
}
return version
}
// ShouldPass returns true if a test should pass informed by expected result
// and flags.
//