| 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 cryptotest |
| 6 |
|
| 7 |
import ( |
| 8 |
"crypto/internal/boring" |
| 9 |
"crypto/internal/impl" |
| 10 |
"internal/goarch" |
| 11 |
"internal/goos" |
| 12 |
"internal/testenv" |
| 13 |
"testing" |
| 14 |
) |
| 15 |
|
| 16 |
// TestAllImplementations runs the provided test function with each available |
| 17 |
// implementation of the package registered with crypto/internal/impl. If there |
| 18 |
// are no alternative implementations for pkg, f is invoked directly once. |
| 19 |
func TestAllImplementations(t *testing.T, pkg string, f func(t *testing.T)) { |
| 20 |
// BoringCrypto bypasses the multiple Go implementations. |
| 21 |
if boring.Enabled { |
| 22 |
f(t) |
| 23 |
return |
| 24 |
} |
| 25 |
|
| 26 |
impls := impl.List(pkg) |
| 27 |
if len(impls) == 0 { |
| 28 |
f(t) |
| 29 |
return |
| 30 |
} |
| 31 |
|
| 32 |
t.Cleanup(func() { impl.Reset(pkg) }) |
| 33 |
|
| 34 |
for _, name := range impls { |
| 35 |
if available := impl.Select(pkg, name); available { |
| 36 |
t.Run(name, f) |
| 37 |
} else { |
| 38 |
t.Run(name, func(t *testing.T) { |
| 39 |
// Report an error if we're on the most capable builder for the |
| 40 |
// architecture and the builder can't test this implementation. |
| 41 |
flagship := goos.GOOS == "linux" && goarch.GOARCH != "arm64" || |
| 42 |
goos.GOOS == "darwin" && goarch.GOARCH == "arm64" |
| 43 |
if testenv.Builder() != "" && flagship { |
| 44 |
if name == "SHA-NI" { |
| 45 |
t.Skip("known issue, see golang.org/issue/69592") |
| 46 |
} |
| 47 |
t.Error("builder doesn't support CPU features needed to test this implementation") |
| 48 |
} else { |
| 49 |
t.Skip("implementation not supported") |
| 50 |
} |
| 51 |
}) |
| 52 |
} |
| 53 |
|
| 54 |
} |
| 55 |
|
| 56 |
// Test the generic implementation. |
| 57 |
impl.Select(pkg, "") |
| 58 |
t.Run("Base", f) |
| 59 |
} |