Source file src/crypto/internal/cryptotest/implementations.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 cryptotest
     6  
     7  import (
     8  	"crypto/internal/boring"
     9  	"crypto/internal/impl"
    10  	"internal/goos"
    11  	"internal/testenv"
    12  	"testing"
    13  )
    14  
    15  // TestAllImplementations runs the provided test function with each available
    16  // implementation of the package registered with crypto/internal/impl. If there
    17  // are no alternative implementations for pkg, f is invoked directly once.
    18  func TestAllImplementations(t *testing.T, pkg string, f func(t *testing.T)) {
    19  	// BoringCrypto bypasses the multiple Go implementations.
    20  	if boring.Enabled {
    21  		f(t)
    22  		return
    23  	}
    24  
    25  	impls := impl.List(pkg)
    26  	if len(impls) == 0 {
    27  		f(t)
    28  		return
    29  	}
    30  
    31  	t.Cleanup(func() { impl.Reset(pkg) })
    32  
    33  	for _, name := range impls {
    34  		if available := impl.Select(pkg, name); available {
    35  			t.Run(name, f)
    36  		} else {
    37  			t.Run(name, func(t *testing.T) {
    38  				// Report an error if we're on Linux CI (assumed to be the most
    39  				// consistent) and the builder can't test this implementation.
    40  				if testenv.Builder() != "" && goos.GOOS == "linux" {
    41  					if name == "SHA-NI" {
    42  						t.Skip("known issue, see golang.org/issue/69592")
    43  					}
    44  					if name == "Armv8.2" {
    45  						t.Skip("known issue, see golang.org/issue/69593")
    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  }
    60  

View as plain text