forked from AdaLogics/go-fuzz-headers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
consumer_test.go
259 lines (237 loc) · 5.96 KB
/
consumer_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
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
// Copyright 2023 The go-fuzz-headers Authors.
//
// 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 gofuzzheaders
import (
//"archive/tar"
//"bytes"
//"io"
"testing"
corev1 "k8s.io/api/core/v1"
)
type TestStruct1 struct {
Field1 string
Field2 string
Field3 []byte
}
type TestStruct2 struct {
M map[string]string
}
func TestMapInStruct(t *testing.T) {
data := []byte{0x02,
0x00, 0x00, 0x00, 0x03,
0x41, 0x42, 0x43,
0x00, 0x00, 0x00, 0x03,
0x41, 0x42, 0x43}
f := NewConsumer(data)
ts := &TestStruct2{}
f.GenerateStruct(ts)
if _, ok := ts.M["ABC"]; !ok {
t.Fatalf("% v\n", ts)
}
}
func TestStruct_fuzzing1(t *testing.T) {
data := []byte{
0x00, 0x00, 0x00, 0x03, // Length of field 1
0x41, 0x42, 0x43, // Data of field field 1
0x00, 0x00, 0x00, 0x03, // Length of field 2
0x41, 0x42, 0x43, // Data of field 2
0x00, 0x00, 0x00, 0x01, // Length of field 3
0x41, // Data of Field3
}
ts1 := TestStruct1{}
fuzz1 := NewConsumer(data)
err := fuzz1.GenerateStruct(&ts1)
if err != nil {
t.Errorf("%v", err)
}
if ts1.Field1 != "ABC" {
t.Errorf("ts1.Field1 was %v but should be 'AB'", []byte(ts1.Field1))
}
if ts1.Field2 != "ABC" {
t.Errorf("ts1.Field2 was %v but should be 'ABC'", ts1.Field2)
}
if string(ts1.Field3) != "A" {
t.Errorf("ts1.Field3 was %v but should be 'A'", ts1.Field3)
}
}
// Tests that we can create long byte slices in structs
func TestStruct_fuzzing2(t *testing.T) {
data := []byte{
0x00, 0x00, 0x00, 0x03, // Length field 1
0x41, 0x42, 0x43, // Data of field 1
0x00, 0x00, 0x00, 0x03, // Length of Field2
0x41, 0x42, 0x43, // Content of Field2
0x00, 0x00, 0x00, 0x50, // Length of field3
0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, // All of this
0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, // should go
0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, // into Field3
0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41,
0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41,
0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41,
0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41,
0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41,
}
ts1 := TestStruct1{}
fuzz1 := NewConsumer(data)
err := fuzz1.GenerateStruct(&ts1)
if err != nil {
t.Errorf("%v", err)
}
if ts1.Field1 != "ABC" {
t.Errorf("ts1.Field1 was %v but should be 'ABC'", ts1.Field1)
}
if ts1.Field2 != "ABC" {
t.Errorf("ts1.Field2 was %v but should be 'ABC'", ts1.Field2)
}
if len(ts1.Field3) != 80 {
t.Errorf("ts1.Field3 was %v but should be 'ABCD'", ts1.Field3)
}
}
/*func TestTarBytes(t *testing.T) {
data := []byte{
0x01, // number of files
0x00, 0x00, 0x00, 0x08, // Length of first file name
0x6d, 0x61, 0x6e, 0x69, 0x66, 0x65, 0x73, 0x74, // "manifest"
0x00, 0x00, 0x00, 0x09, // Length of file body
0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, // file contents
0x04, 0x02, 0x03,
0x00, // type flag
0x01, 0x01, 0x01, 0x01,
}
f := NewConsumer(data)
tb, err := f.TarBytes()
if err != nil {
t.Fatalf("Fatal: %s", err)
}
tarReader := tar.NewReader(bytes.NewReader(tb))
for {
header, err := tarReader.Next()
if err == io.EOF {
break
}
if err != nil {
t.Fatal(err)
}
if header.Typeflag != 48 {
t.Fatalf("typeflag should be 48 (which is a tar.TypeReg) but is %v", header.Typeflag)
}
switch header.Typeflag {
case tar.TypeDir:
t.Fatal("Should not be a directory")
case tar.TypeReg:
if header.Name != "manifest" {
t.Fatalf("file name was %s but should be 'manifest'\n", header.Name)
}
}
}
}*/
func TestGetUint32(t *testing.T) {
data := []byte{
0x00,
0x00,
0x03,
0x01,
}
f := NewConsumer(data)
i, err := f.GetUint32()
if err != nil {
t.Fatalf("%v\n", err)
}
if i != uint32(769) {
t.Fatalf("i should be 636 but is %v\n", i)
}
}
func TestGeBytes1(t *testing.T) {
data := []byte{
0x00,
0x00,
0x03,
0x01,
}
for i := 0; i < 769; i {
data = append(data, 0x00)
}
f := NewConsumer(data)
b, err := f.GetBytes()
if err != nil {
t.Fatalf("%v\n", err)
}
if len(b) != 769 {
t.Fatalf("len(b) should be 769 but is %v\n", len(b))
}
for i := 0; i < 769; i {
if b[i] != 0 {
t.Fatalf("b[%d] should be 0x00 but is %v\n", i, b[i])
}
}
}
func TestGeBytes2(t *testing.T) {
data := []byte{
0x00,
0x00,
0x03,
0x01,
}
for i := 0; i < 767; i {
data = append(data, 0x00)
}
f := NewConsumer(data)
b, err := f.GetBytes()
if err != nil {
t.Fatalf("%v\n", err)
}
if len(b) != 2 {
t.Fatalf("len(b) should be 2 but is %v\n", len(b))
}
for i := 0; i < 2; i {
if b[i] != 0 {
t.Fatalf("b[%d] should be 0x00 but is %v\n", i, b[i])
}
}
}
func TestGeBytes3(t *testing.T) {
data := []byte{
0x00,
0x00,
0x03,
0x01,
}
for i := 0; i < 500; i {
data = append(data, 0x00)
}
f := NewConsumer(data)
b, err := f.GetBytes()
if err != nil {
t.Fatalf("%v\n", err)
}
if len(b) != 269 {
t.Fatalf("len(b) should be 269 but is %v\n", len(b))
}
for i := 0; i < 269; i {
if b[i] != 0 {
t.Fatalf("b[%d] should be 0x00 but is %v\n", i, b[i])
}
}
}
func TestGeneratePod(t *testing.T) {
pod := &corev1.Pod{}
data := []byte{0x01,0x00,0x00,0x00,0x03,0x50,0x06f,0x064, // "Kind: Pod"
0x01,0x00,0x00,0x00,0x02,0x76,0x31, // "APIVersion: v1"
0x00, // Skip ObjectMeta
0x01, // Do not skip Spec
}
ff := NewConsumer(data)
ff.GenerateStruct(pod)
t.Log(pod)
}