aboutsummaryrefslogtreecommitdiff
path: root/src/internal/fuzz/encoding_test.go
blob: b429d429c673723203ca324a870369dfa5f9d82f (plain)
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
// Copyright 2021 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package fuzz

import (
	"strconv"
	"strings"
	"testing"
)

func TestUnmarshalMarshal(t *testing.T) {
	var tests = []struct {
		in string
		ok bool
	}{
		{
			in: "int(1234)",
			ok: false, // missing version
		},
		{
			in: `go test fuzz v1
string("a"bcad")`,
			ok: false, // malformed
		},
		{
			in: `go test fuzz v1
int()`,
			ok: false, // empty value
		},
		{
			in: `go test fuzz v1
uint(-32)`,
			ok: false, // invalid negative uint
		},
		{
			in: `go test fuzz v1
int8(1234456)`,
			ok: false, // int8 too large
		},
		{
			in: `go test fuzz v1
int(20*5)`,
			ok: false, // expression in int value
		},
		{
			in: `go test fuzz v1
int(--5)`,
			ok: false, // expression in int value
		},
		{
			in: `go test fuzz v1
bool(0)`,
			ok: false, // malformed bool
		},
		{
			in: `go test fuzz v1
byte('aa)`,
			ok: false, // malformed byte
		},
		{
			in: `go test fuzz v1
byte('☃')`,
			ok: false, // byte out of range
		},
		{
			in: `go test fuzz v1
string("has final newline")
`,
			ok: true, // has final newline
		},
		{
			in: `go test fuzz v1
string("extra")
[]byte("spacing")  
    `,
			ok: true, // extra spaces in the final newline
		},
		{
			in: `go test fuzz v1
float64(0)
float32(0)`,
			ok: true, // will be an integer literal since there is no decimal
		},
		{
			in: `go test fuzz v1
int(-23)
int8(-2)
int64(2342425)
uint(1)
uint16(234)
uint32(352342)
uint64(123)
rune('œ')
byte('K')
byte('ÿ')
[]byte("hello¿")
[]byte("a")
bool(true)
string("hello\\xbd\\xb2=\\xbc ⌘")
float64(-12.5)
float32(2.5)`,
			ok: true,
		},
	}
	for _, test := range tests {
		t.Run(test.in, func(t *testing.T) {
			vals, err := unmarshalCorpusFile([]byte(test.in))
			if test.ok && err != nil {
				t.Fatalf("unmarshal unexpected error: %v", err)
			} else if !test.ok && err == nil {
				t.Fatalf("unmarshal unexpected success")
			}
			if !test.ok {
				return // skip the rest of the test
			}
			newB := marshalCorpusFile(vals...)
			if err != nil {
				t.Fatalf("marshal unexpected error: %v", err)
			}
			if newB[len(newB)-1] != '\n' {
				t.Error("didn't write final newline to corpus file")
			}
			before, after := strings.TrimSpace(test.in), strings.TrimSpace(string(newB))
			if before != after {
				t.Errorf("values changed after unmarshal then marshal\nbefore: %q\nafter:  %q", before, after)
			}
		})
	}
}

// BenchmarkMarshalCorpusFile measures the time it takes to serialize byte
// slices of various sizes to a corpus file. The slice contains a repeating
// sequence of bytes 0-255 to mix escaped and non-escaped characters.
func BenchmarkMarshalCorpusFile(b *testing.B) {
	buf := make([]byte, 1024*1024)
	for i := 0; i < len(buf); i++ {
		buf[i] = byte(i)
	}

	for sz := 1; sz <= len(buf); sz <<= 1 {
		sz := sz
		b.Run(strconv.Itoa(sz), func(b *testing.B) {
			for i := 0; i < b.N; i++ {
				b.SetBytes(int64(sz))
				marshalCorpusFile(buf[:sz])
			}
		})
	}
}

// BenchmarkUnmarshalCorpusfile measures the time it takes to deserialize
// files encoding byte slices of various sizes. The slice contains a repeating
// sequence of bytes 0-255 to mix escaped and non-escaped characters.
func BenchmarkUnmarshalCorpusFile(b *testing.B) {
	buf := make([]byte, 1024*1024)
	for i := 0; i < len(buf); i++ {
		buf[i] = byte(i)
	}

	for sz := 1; sz <= len(buf); sz <<= 1 {
		sz := sz
		data := marshalCorpusFile(buf[:sz])
		b.Run(strconv.Itoa(sz), func(b *testing.B) {
			for i := 0; i < b.N; i++ {
				b.SetBytes(int64(sz))
				unmarshalCorpusFile(data)
			}
		})
	}
}