forked from gopwn/pwn
-
Notifications
You must be signed in to change notification settings - Fork 1
/
pwn_test.go
73 lines (66 loc) · 1.17 KB
/
pwn_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
package pwn
import (
"bytes"
"testing"
)
// Test the ToBytes function in misc.go
func TestToBytes(t *testing.T) {
t.Parallel()
var tt = []struct {
// name of the subtest to execute
name string
// expected inputs and outputs
input interface{}
output []byte
// do you expect the conversion to fail?
fail bool
}{
{
name: "test string",
input: "hello",
output: []byte("hello"),
},
{
// may be convertable some day
name: "test int",
input: 42,
fail: true,
},
{
name: "test byte",
input: byte(5),
output: []byte{5},
},
{
name: "test struct",
input: struct{}{},
fail: true,
},
{
name: "test rune",
input: 'h',
output: []byte{'h'},
},
{
name: "test []byte",
input: []byte{131, 41, 48},
output: []byte{131, 41, 48},
},
}
// execute the tests
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
defer func() {
e := recover()
// if the error is not nil and it is not expected then fail
if e != nil && !tc.fail {
t.Fatal(e)
}
}()
b := Bytes(tc.input)
if !bytes.Equal(b, tc.output) {
t.Fatalf("wanted %q got %q", tc.output, b)
}
})
}
}