Debian Codegraph @

common_test.go

raw symbols SCIP
1 package mint
2
3 import (
4 "bytes"
5 "encoding/hex"
6 "fmt"
7 "reflect"
8 "runtime"
9 "sort"
10 "testing"
11 )
12
13 func unhex(h string) []byte {
14 b, err := hex.DecodeString(h)
15 if err != nil {
16 panic(err)
17 }
18 return b
19 }
20
21 func assertTrue(t *testing.T, test bool, msg string) {
22 t.Helper()
23 prefix := string("")
24 for i := 1; ; i++ {
25 _, file, line, ok := runtime.Caller(i)
26 if !ok {
27 break
28 }
29 prefix = fmt.Sprintf("%v: %d\n", file, line) + prefix
30 }
31 if !test {
32 t.Fatalf(prefix + msg)
33 }
34 }
35
36 func assertError(t *testing.T, err error, msg string) {
37 t.Helper()
38 assertTrue(t, err != nil, msg)
39 }
40
41 func assertNotError(t *testing.T, err error, msg string) {
42 t.Helper()
43 if err != nil {
44 msg += ": " + err.Error()
45 }
46 assertTrue(t, err == nil, msg)
47 }
48
49 func assertNil(t *testing.T, x interface{}, msg string) {
50 t.Helper()
51 assertTrue(t, x == nil, msg)
52 }
53
54 func assertNotNil(t *testing.T, x interface{}, msg string) {
55 t.Helper()
56 assertTrue(t, x != nil, msg)
57 }
58
59 func assertEquals(t *testing.T, a, b interface{}) {
60 t.Helper()
61 assertTrue(t, a == b, fmt.Sprintf("%+v != %+v", a, b))
62 }
63
64 func assertByteEquals(t *testing.T, a, b []byte) {
65 t.Helper()
66 assertTrue(t, bytes.Equal(a, b), fmt.Sprintf("%+v != %+v", hex.EncodeToString(a), hex.EncodeToString(b)))
67 }
68
69 func assertNotByteEquals(t *testing.T, a, b []byte) {
70 t.Helper()
71 assertTrue(t, !bytes.Equal(a, b), fmt.Sprintf("%+v == %+v", hex.EncodeToString(a), hex.EncodeToString(b)))
72 }
73
74 func assertCipherSuiteParamsEquals(t *testing.T, a, b CipherSuiteParams) {
75 t.Helper()
76 assertEquals(t, a.Suite, b.Suite)
77 // Can't compare AEADFactory values
78 assertEquals(t, a.Hash, b.Hash)
79 assertEquals(t, len(a.KeyLengths), len(b.KeyLengths))
80 for k, v := range a.KeyLengths {
81 assertEquals(t, v, b.KeyLengths[k])
82 }
83 }
84
85 func assertDeepEquals(t *testing.T, a, b interface{}) {
86 t.Helper()
87 assertTrue(t, reflect.DeepEqual(a, b), fmt.Sprintf("%+v != %+v", a, b))
88 }
89
90 func assertSameType(t *testing.T, a, b interface{}) {
91 t.Helper()
92 A := reflect.TypeOf(a)
93 B := reflect.TypeOf(b)
94 assertTrue(t, A == B, fmt.Sprintf("%s != %s", A.Name(), B.Name()))
95 }
96
97 // Utilities for parametrized tests
98 // Represents the configuration for a given test instance.
99 type testInstanceState map[string]string
100
101 // Helper function.
102 func runParametrizedInner(t *testing.T, name string, state testInstanceState, inparams map[string][]string, inparamList []string, f parametrizedTest) {
103
104 paramName := inparamList[0]
105 param := inparams[paramName]
106 next := inparamList[1:]
107
108 for _, paramVal := range param {
109 state[paramName] = paramVal
110 var n string
111 if len(name) > 0 {
112 n = name + "/"
113 }
114 n = n + paramName + "=" + paramVal
115
116 if len(next) == 0 {
117 t.Run(n, func(t *testing.T) {
118 f(t, n, state)
119 })
120 continue
121 }
122 runParametrizedInner(t, n, state, inparams, next, f)
123 }
124 }
125
126 // Nominally public API.
127 type testParameter struct {
128 name string
129 vals []string
130 }
131
132 type parametrizedTest func(t *testing.T, name string, p testInstanceState)
133
134 // This is the function you call.
135 func runParametrizedTest(t *testing.T, inparams map[string][]string, f parametrizedTest) {
136 // Make a sorted list of the names, so we get a consistent order.
137 il := make([]string, 0)
138 for k := range inparams {
139 il = append(il, k)
140 }
141 sort.Slice(il, func(i, j int) bool { return il[i] < il[j] })
142
143 runParametrizedInner(t, "", make(map[string]string), inparams, il, f)
144 }