forked from bifurcation/mint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common_test.go
144 lines (122 loc) · 3.27 KB
/
common_test.go
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package mint
import (
"bytes"
"encoding/hex"
"fmt"
"reflect"
"runtime"
"sort"
"testing"
)
func unhex(h string) []byte {
b, err := hex.DecodeString(h)
if err != nil {
panic(err)
}
return b
}
func assertTrue(t *testing.T, test bool, msg string) {
t.Helper()
prefix := string("")
for i := 1; ; i++ {
_, file, line, ok := runtime.Caller(i)
if !ok {
break
}
prefix = fmt.Sprintf("%v: %d\n", file, line) + prefix
}
if !test {
t.Fatalf(prefix + msg)
}
}
func assertError(t *testing.T, err error, msg string) {
t.Helper()
assertTrue(t, err != nil, msg)
}
func assertNotError(t *testing.T, err error, msg string) {
t.Helper()
if err != nil {
msg += ": " + err.Error()
}
assertTrue(t, err == nil, msg)
}
func assertNil(t *testing.T, x interface{}, msg string) {
t.Helper()
assertTrue(t, x == nil, msg)
}
func assertNotNil(t *testing.T, x interface{}, msg string) {
t.Helper()
assertTrue(t, x != nil, msg)
}
func assertEquals(t *testing.T, a, b interface{}) {
t.Helper()
assertTrue(t, a == b, fmt.Sprintf("%+v != %+v", a, b))
}
func assertByteEquals(t *testing.T, a, b []byte) {
t.Helper()
assertTrue(t, bytes.Equal(a, b), fmt.Sprintf("%+v != %+v", hex.EncodeToString(a), hex.EncodeToString(b)))
}
func assertNotByteEquals(t *testing.T, a, b []byte) {
t.Helper()
assertTrue(t, !bytes.Equal(a, b), fmt.Sprintf("%+v == %+v", hex.EncodeToString(a), hex.EncodeToString(b)))
}
func assertCipherSuiteParamsEquals(t *testing.T, a, b CipherSuiteParams) {
t.Helper()
assertEquals(t, a.Suite, b.Suite)
// Can't compare AEADFactory values
assertEquals(t, a.Hash, b.Hash)
assertEquals(t, len(a.KeyLengths), len(b.KeyLengths))
for k, v := range a.KeyLengths {
assertEquals(t, v, b.KeyLengths[k])
}
}
func assertDeepEquals(t *testing.T, a, b interface{}) {
t.Helper()
assertTrue(t, reflect.DeepEqual(a, b), fmt.Sprintf("%+v != %+v", a, b))
}
func assertSameType(t *testing.T, a, b interface{}) {
t.Helper()
A := reflect.TypeOf(a)
B := reflect.TypeOf(b)
assertTrue(t, A == B, fmt.Sprintf("%s != %s", A.Name(), B.Name()))
}
// Utilities for parametrized tests
// Represents the configuration for a given test instance.
type testInstanceState map[string]string
// Helper function.
func runParametrizedInner(t *testing.T, name string, state testInstanceState, inparams map[string][]string, inparamList []string, f parametrizedTest) {
paramName := inparamList[0]
param := inparams[paramName]
next := inparamList[1:]
for _, paramVal := range param {
state[paramName] = paramVal
var n string
if len(name) > 0 {
n = name + "/"
}
n = n + paramName + "=" + paramVal
if len(next) == 0 {
t.Run(n, func(t *testing.T) {
f(t, n, state)
})
continue
}
runParametrizedInner(t, n, state, inparams, next, f)
}
}
// Nominally public API.
type testParameter struct {
name string
vals []string
}
type parametrizedTest func(t *testing.T, name string, p testInstanceState)
// This is the function you call.
func runParametrizedTest(t *testing.T, inparams map[string][]string, f parametrizedTest) {
// Make a sorted list of the names, so we get a consistent order.
il := make([]string, 0)
for k := range inparams {
il = append(il, k)
}
sort.Slice(il, func(i, j int) bool { return il[i] < il[j] })
runParametrizedInner(t, "", make(map[string]string), inparams, il, f)
}