-
Notifications
You must be signed in to change notification settings - Fork 153
/
helpers.go
356 lines (312 loc) · 9.72 KB
/
helpers.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
// Copyright 2018 ETH Zurich
// Copyright 2020 ETH Zurich, Anapaya Systems
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package xtest
import (
"bytes"
"encoding/hex"
"encoding/json"
"flag"
"net"
"net/netip"
"os"
"path/filepath"
"regexp"
"strings"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/scionproto/scion/pkg/addr"
)
// Update registers the '-update' flag for the test.
//
// This flag should be checked by golden file tests to see whether the golden
// files should be updated or not. The golden files should be deterministic.
// Use UpdateNonDeterminsticGoldenFiles instead, if they are not deterministic.
//
// To update all golden files, run the following command:
//
// go test ./... -update
//
// To update a specific package, run the following command:
//
// go test ./path/to/package -update
//
// The flag should be registered as a package global variable:
//
// var update = xtest.UpdateGoldenFiles()
func UpdateGoldenFiles() *bool {
return flag.Bool("update", false, "set to regenerate the golden files")
}
// UpdateNonDeterminsticGoldenFiles registers the '-update-non-deterministic'
// flag for the test.
//
// This flag should be checked by golden file tests to see whether the
// non-deterministic golden files should be updated or not.
//
// To update all golden files, run the following command:
//
// go test ./... -update-non-deterministic
//
// To update a specific package, run the following command:
//
// go test ./path/to/package -update-non-deterministic
//
// The flag should be registered as a package global variable:
//
// var updateNonDeterministic = xtest.UpdateNonDeterminsticGoldenFiles()
func UpdateNonDeterminsticGoldenFiles() *bool {
return flag.Bool("update-non-deterministic", false,
"set to regenerate the non-deterministic golden files",
)
}
// TempFileName creates a temporary file in dir with the specified prefix, and
// then closes and deletes the file and returns its name. It is useful for
// testing packages that care about a unique path without being able to
// overwrite it (e.g., UNIX domain socket addresses or databases).
func TempFileName(dir, prefix string) (string, error) {
file, err := os.CreateTemp(dir, prefix)
if err != nil {
return "", err
}
name := file.Name()
if err := file.Close(); err != nil {
return "", err
}
if err := os.Remove(name); err != nil {
return "", err
}
return name, nil
}
// MustTempFileName is a wrapper around TempFileName. The function panics if an
// error occurs. It is intended for tests where error handling is not
// necessary, and for chaining functions.
func MustTempFileName(dir, prefix string) string {
name, err := TempFileName(dir, prefix)
if err != nil {
panic(err)
}
return name
}
// MustTempDir creates a new temporary directory under dir with the specified
// prefix. If the function encounters an error it panics. The second return
// value is a clean-up function that can be called to recursively delete the
// entire directory.
func MustTempDir(dir, prefix string) (string, func()) {
name, err := os.MkdirTemp(dir, prefix)
if err != nil {
panic(err)
}
return name, func() {
os.RemoveAll(name)
}
}
// SanitizedName sanitizes the test name such that it can be used as a file name.
func SanitizedName(t testing.TB) string {
return strings.NewReplacer(" ", "_", "/", "_", "\\", "_", ":", "_").Replace(t.Name())
}
// CopyFile copies the file.
func CopyFile(t testing.TB, src, dst string) {
t.Helper()
raw, err := os.ReadFile(src)
require.NoError(t, err)
require.NoError(t, os.WriteFile(dst, raw, 0666))
}
// MustMarshalJSONToFile marshals v and writes the result to file
// testdata/baseName. If the file exists, it is truncated; if it doesn't exist,
// it is created. On errors, t.Fatal() is called.
func MustMarshalJSONToFile(t testing.TB, v interface{}, baseName string) {
t.Helper()
enc, err := json.MarshalIndent(v, "", " ")
if err != nil {
t.Fatal(err)
}
buffer := bytes.NewBuffer(enc)
if err := buffer.WriteByte('\n'); err != nil {
t.Fatal(err)
}
MustWriteToFile(t, buffer.Bytes(), baseName)
}
// MustWriteToFile writes b to file testdata/baseName. If the file exists, it
// is truncated; if it doesn't exist, it is created. On errors, t.Fatal() is
// called.
func MustWriteToFile(t testing.TB, b []byte, baseName string) {
t.Helper()
if err := os.WriteFile(ExpandPath(baseName), b, 0644); err != nil {
t.Fatal(err)
}
}
// MustReadFromFile reads testdata/baseName and returns the raw content. On
// errors, t.Fatal() is called.
func MustReadFromFile(t testing.TB, baseName string) []byte {
t.Helper()
name := filepath.Join("testdata", baseName)
b, err := os.ReadFile(name)
if err != nil {
t.Fatal(err)
}
return b
}
// ExpandPath returns testdata/file.
func ExpandPath(file string) string {
return filepath.Join("testdata", file)
}
// MustParseIA parses s and returns the corresponding addr.IA object. It
// panics if s is not a valid ISD-AS representation.
func MustParseIA(s string) addr.IA {
ia, err := addr.ParseIA(s)
if err != nil {
panic(err)
}
return ia
}
// MustParseIAs parses a list of comma separated ISD-AS strings. It panics in case
// parsing fails.
func MustParseIAs(list string) []addr.IA {
l := strings.Split(list, ",")
var ias []addr.IA
for _, raw := range l {
ias = append(ias, MustParseIA(raw))
}
return ias
}
// MustParseAS parses s and returns the corresponding addr.AS object. It panics
// if s is not valid AS representation.
func MustParseAS(s string) addr.AS {
ia, err := addr.ParseAS(s)
if err != nil {
panic(err)
}
return ia
}
// MustParseASes parses a list of comma separated AS strings. It panics in case
// parsing fails.
func MustParseASes(list string) []addr.AS {
l := strings.Split(list, ",")
var ases []addr.AS
for _, raw := range l {
ases = append(ases, MustParseAS(raw))
}
return ases
}
// MustParseHexString parses s and returns the corresponding byte slice.
// It panics if the decoding fails.
func MustParseHexString(s string) []byte {
// remove whitespace
reg, err := regexp.Compile(`\s+`)
if err != nil {
panic(err)
}
s = reg.ReplaceAllString(s, "")
decoded, err := hex.DecodeString(s)
if err != nil {
panic(err)
}
return decoded
}
// MustParseCIDR parses s and returns the corresponding net.IPNet object. It
// fails the test if s is not a valid CIDR string.
func MustParseCIDR(t *testing.T, s string) *net.IPNet {
t.Helper()
_, network, err := net.ParseCIDR(s)
require.NoError(t, err)
return network
}
// MustParseCIDRs parses the CIDR entries and returns a list containing the
// parsed net.IPNet objects.
func MustParseCIDRs(t *testing.T, entries ...string) []*net.IPNet {
t.Helper()
result := make([]*net.IPNet, 0, len(entries))
for _, e := range entries {
result = append(result, MustParseCIDR(t, e))
}
return result
}
// MustParseIPPrefixes parses the CIDR entries and returns a list containing the
// parsed netip.Prefix objects.
func MustParseIPPrefixes(t *testing.T, prefixes ...string) []netip.Prefix {
t.Helper()
var result []netip.Prefix
for _, prefix := range prefixes {
p, err := netip.ParsePrefix(prefix)
require.NoError(t, err)
result = append(result, p)
}
return result
}
// MustParseIP parses an IP address and returns the parsed net.IP object.
func MustParseIP(t *testing.T, addr string) net.IP {
t.Helper()
ip := net.ParseIP(addr)
require.NotNil(t, ip)
return ip
}
// MustParseUDPAddr parses s and returns the corresponding net.UDPAddr object.
// It fails the test if s is not a valid UDP address string.
func MustParseUDPAddr(t *testing.T, s string) *net.UDPAddr {
t.Helper()
a, err := net.ResolveUDPAddr("udp", s)
require.NoError(t, err)
if ipv4 := a.IP.To4(); ipv4 != nil {
a.IP = ipv4
}
return a
}
// MustParseUDPAddrs parses the UPD address entries and returns a list
// containing the parsed net.UDPAddr objects.
func MustParseUDPAddrs(t *testing.T, entries ...string) []*net.UDPAddr {
t.Helper()
result := make([]*net.UDPAddr, 0, len(entries))
for _, e := range entries {
result = append(result, MustParseUDPAddr(t, e))
}
return result
}
// MustParseTime parses s and returns the corresponding time.Time object. It
// fails the test if s is not a valid time string.
func MustParseTime(t *testing.T, s string) time.Time {
t.Helper()
res, err := time.Parse(time.RFC3339, s)
if err != nil {
t.Fatal(err)
}
return res
}
// AssertReadReturnsBetween will call t.Fatalf if the first read from the
// channel doesn't happen between x and y.
func AssertReadReturnsBetween(t testing.TB, ch <-chan struct{}, x, y time.Duration) {
AssertReadDoesNotReturnBefore(t, ch, x)
// Above aborts the test if it returns before x time passed, so if we get
// here x time has passed.
AssertReadReturnsBefore(t, ch, y-x)
}
// AssertReadReturnsBefore will call t.Fatalf if the first read from the
// channel doesn't happen before timeout.
func AssertReadReturnsBefore(t testing.TB, ch <-chan struct{}, timeout time.Duration) {
t.Helper()
select {
case <-ch:
case <-time.After(timeout):
t.Fatalf("goroutine took too long to finish")
}
}
// AssertReadDoesNotReturnBefore will call t.Fatalf if the first read from the
// channel happens before timeout.
func AssertReadDoesNotReturnBefore(t testing.TB, ch <-chan struct{}, timeout time.Duration) {
select {
case <-ch:
t.Fatalf("goroutine finished too quickly")
case <-time.After(timeout):
}
}