-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfuzz_test.go
154 lines (133 loc) · 3.32 KB
/
fuzz_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
145
146
147
148
149
150
151
152
153
154
package fuzzing
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func Example() {
f := New([]byte{1, 2, 3})
i := f.Int()
fmt.Println(i)
// Output: 3851489450890114710
}
var seedBytes = []byte{0, 0, 0, 0, 0, 0, 0, 1}
func new(input ...byte) *Fuzz {
return New(append(seedBytes, input...))
}
func TestFuzzRand(t *testing.T) {
t.Parallel()
f := new()
assert.Equal(t, 5980212987775051087, f.Int())
assert.Equal(t, 1603104512986455410, f.Int())
}
func TestFuzzRead(t *testing.T) {
t.Parallel()
f := new(9, 10)
// 9th byte.
assert.Equal(t, []byte{9}, f.Bytes(1))
// 10th byte.
assert.Equal(t, []byte{10}, f.Bytes(1))
// From seed.
assert.Equal(t, []byte{82}, f.Bytes(1))
}
func TestFuzzRead_combine(t *testing.T) {
t.Parallel()
f := new(9)
assert.Equal(t, []byte{9 /* 9th byte */, 82 /* From seed */}, f.Bytes(2))
}
func TestFuncs(t *testing.T) {
t.Parallel()
tests := []struct {
data []byte
wantInt int
wantUint uint
wantInt64 int64
wantInt63 int64
wantUint64 uint64
wantInt32 int32
wantInt31 int32
wantUint32 uint32
wantBool bool
wantRune rune
}{
{
data: []byte{0, 0, 0, 0, 0, 0, 0, 1},
wantInt: 1,
wantUint: 1,
wantInt64: 1,
wantInt63: 0,
wantUint64: 1,
wantInt32: 0,
wantInt31: 0,
wantUint32: 0,
wantBool: false,
wantRune: '\x00',
},
{
data: []byte{0, 0, 0, 0, 0, 0, 2, 3},
wantInt: 515,
wantUint: 515,
wantInt64: 515,
wantInt63: 257,
wantUint64: 515,
wantInt32: 0,
wantInt31: 0,
wantUint32: 0,
wantBool: false,
wantRune: '\x00',
},
{
data: []byte{255, 255, 255, 255, 255, 255, 255, 255},
wantInt: -1,
wantUint: 0xffffffffffffffff,
wantInt64: -1,
wantInt63: 0x7fffffffffffffff,
wantUint64: 0xffffffffffffffff,
wantInt32: -1,
wantInt31: 0x7fffffff,
wantUint32: 0xffffffff,
wantBool: true,
wantRune: '�',
},
{
data: nil,
wantInt: 5980212987775051087,
wantUint: 5980212987775051087,
wantInt64: 5980212987775051087,
wantInt63: 2990106493887525543,
wantUint64: 5980212987775051087,
wantInt32: 1392376839,
wantInt31: 696188419,
wantUint32: 1392376839,
wantBool: false,
wantRune: 'R',
},
}
for _, tt := range tests {
t.Run(string(tt.data), func(t *testing.T) {
assert.Equal(t, tt.wantInt, new(tt.data...).Int(), "int")
assert.Equal(t, tt.wantUint, new(tt.data...).Uint(), "uint")
assert.Equal(t, tt.wantInt64, new(tt.data...).Int64(), "int64")
assert.Equal(t, tt.wantInt63, new(tt.data...).Int63(), "int63")
assert.Equal(t, tt.wantUint64, new(tt.data...).Uint64(), "uint64")
assert.Equal(t, tt.wantInt32, new(tt.data...).Int32(), "int32")
assert.Equal(t, tt.wantInt31, new(tt.data...).Int31(), "int31")
assert.Equal(t, tt.wantUint32, new(tt.data...).Uint32(), "uint32")
assert.Equal(t, tt.wantBool, new(tt.data...).Bool(), "bool")
assert.Equal(t, tt.wantRune, new(tt.data...).Rune(), "rune")
})
}
}
func TestFuzzString(t *testing.T) {
t.Parallel()
f := new('a', 'b', 'c')
// 9th byte as a rune.
assert.Equal(t, "a", f.String(1))
// 10th, 11th bytes as runes.
assert.Equal(t, "bc", f.String(2))
// From seed.
assert.Equal(t, "R", f.String(1))
}
func TestNewEmpty(t *testing.T) {
assert.Equal(t, New(nil).Int(), 113994904454036672)
}