File size: 3,004 Bytes
e36aeda
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Copyright 2024 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.

// Package impl is a registry of alternative implementations of cryptographic
// primitives, to allow selecting them for testing.
package impl

import "strings"

type implementation struct {
	Package   string
	Name      string
	Available bool
	Toggle    *bool
}

var allImplementations []implementation

// Register records an alternative implementation of a cryptographic primitive.
// The implementation might be available or not based on CPU support. If
// available is false, the implementation is unavailable and can't be tested on
// this machine. If available is true, it can be set to false to disable the
// implementation. If all alternative implementations but one are disabled, the
// remaining one must be used (i.e. disabling one implementation must not
// implicitly disable any other). Each package has an implicit base
// implementation that is selected when all alternatives are unavailable or
// disabled. pkg must be the package name, not path (e.g. "aes" not "crypto/aes").
func Register(pkg, name string, available *bool) {
	if strings.Contains(pkg, "/") {
		panic("impl: package name must not contain slashes")
	}
	allImplementations = append(allImplementations, implementation{
		Package:   pkg,
		Name:      name,
		Available: *available,
		Toggle:    available,
	})
}

// Packages returns the list of all packages for which alternative
// implementations are registered.
func Packages() []string {
	var pkgs []string
	seen := make(map[string]bool)
	for _, i := range allImplementations {
		if !seen[i.Package] {
			pkgs = append(pkgs, i.Package)
			seen[i.Package] = true
		}
	}
	return pkgs
}

// List returns the names of all alternative implementations registered for the
// given package, whether available or not. The implicit base implementation is
// not included.
func List(pkg string) []string {
	var names []string
	for _, i := range allImplementations {
		if i.Package == pkg {
			names = append(names, i.Name)
		}
	}
	return names
}

func available(pkg, name string) bool {
	for _, i := range allImplementations {
		if i.Package == pkg && i.Name == name {
			return i.Available
		}
	}
	panic("unknown implementation")
}

// Select disables all implementations for the given package except the one
// with the given name. If name is empty, the base implementation is selected.
// It returns whether the selected implementation is available.
func Select(pkg, name string) bool {
	if name == "" {
		for _, i := range allImplementations {
			if i.Package == pkg {
				*i.Toggle = false
			}
		}
		return true
	}
	if !available(pkg, name) {
		return false
	}
	for _, i := range allImplementations {
		if i.Package == pkg {
			*i.Toggle = i.Name == name
		}
	}
	return true
}

func Reset(pkg string) {
	for _, i := range allImplementations {
		if i.Package == pkg {
			*i.Toggle = i.Available
			return
		}
	}
}