-
Notifications
You must be signed in to change notification settings - Fork 1
/
server_fuzz_test.go
143 lines (113 loc) · 2.32 KB
/
server_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
// +build fuzz
package main
import (
"context"
"net"
"runtime"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/google/gofuzz"
"github.com/vinyl-linux/vin/server"
"go.uber.org/zap"
"google.golang.org/grpc"
"google.golang.org/grpc/test/bufconn"
)
const (
bufSize = 1024 * 1024
fuzzDur = time.Minute * 5
)
var (
lis *bufconn.Listener
fuzzConc int
fuzzCount int64
fuzzWG sync.WaitGroup
)
func init() {
lis = bufconn.Listen(bufSize)
pkgDir = "testdata/manifests/valid-manifests"
configFile = "testdata/test-config.toml"
cacheDir = "/tmp"
stateDB = "/tmp/vinyl-fuzz.db"
// Disable logs for fuzz test (otherwise it's super verbose and we miss anything useful)
logger = zap.NewNop()
s := Setup()
go func() {
if err := s.Serve(lis); err != nil {
panic(err)
}
}()
// set concurrent fuzz threads
switch n := runtime.NumCPU(); n {
case 1, 2:
fuzzConc = 1
case 3, 4, 5, 6, 7, 8:
fuzzConc = int(n / 2)
default:
fuzzConc = 4
}
}
func bufDialer(context.Context, string) (net.Conn, error) {
return lis.Dial()
}
func TestFuzzServer_Install(t *testing.T) {
end := time.Now().Add(fuzzDur)
fuzzWG.Add(fuzzConc)
t.Logf("fuzzing Install until %v", end)
t.Logf("with %d workers", fuzzConc)
for i := 0; i < fuzzConc; i++ {
go fuzzLoop(t, end)
}
fuzzWG.Wait()
t.Logf("ran %d tests", fuzzCount)
}
func fuzzLoop(t *testing.T, end time.Time) {
defer func() {
err := recover()
if err != nil {
t.Fatalf("unexpected error: %#v", err)
}
}()
defer fuzzWG.Done()
f := fuzz.New().Funcs(
func(is *server.InstallSpec, c fuzz.Continue) {
switch c.Intn(3) {
case 0:
is.Version = ""
case 1:
is.Version = "1.0.1"
default:
c.Fuzz(&is.Version)
}
c.Fuzz(&is.Pkg)
c.Fuzz(&is.Force)
},
)
ctx := context.Background()
conn, err := grpc.DialContext(ctx, "bufnet", grpc.WithContextDialer(bufDialer), grpc.WithInsecure())
if err != nil {
t.Fatalf("Failed to dial bufnet: %v", err)
}
defer conn.Close()
client := server.NewVinClient(conn)
for {
if time.Now().After(end) {
return
}
is := &server.InstallSpec{}
f.Fuzz(is)
resp, err := client.Install(context.Background(), is)
if err != nil {
t.Fatalf("unexpected error: %#v", err)
}
for {
_, err = resp.Recv()
if err != nil {
goto nxt
}
}
nxt:
atomic.AddInt64(&fuzzCount, 1)
}
}