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
|
// Copyright 2016 syzkaller project authors. All rights reserved.
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
package state
import (
"io/ioutil"
"os"
"sort"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/google/syzkaller/pkg/rpctype"
)
type TestState struct {
t *testing.T
dir string
state *State
}
func MakeTestState(t *testing.T) *TestState {
t.Parallel()
dir, err := ioutil.TempDir("", "syz-hub-state-test")
if err != nil {
t.Fatalf("failed to create temp dir: %v", err)
}
state, err := Make(dir)
if err != nil {
os.RemoveAll(dir)
t.Fatalf("failed to make state: %v", err)
}
return &TestState{t, dir, state}
}
func (ts *TestState) Close() {
os.RemoveAll(ts.dir)
}
func (ts *TestState) Reload() {
ts.state.Flush()
state, err := Make(ts.dir)
if err != nil {
ts.t.Fatalf("failed to make state: %v", err)
}
ts.state = state
}
func (ts *TestState) Connect(name, domain string, fresh bool, calls []string, corpus [][]byte) {
ts.t.Helper()
if err := ts.state.Connect(name, domain, fresh, calls, corpus); err != nil {
ts.t.Fatalf("Connect failed: %v", err)
}
}
func (ts *TestState) Sync(name string, add [][]byte, del []string) (string, []rpctype.HubInput, int) {
ts.t.Helper()
domain, inputs, pending, err := ts.state.Sync(name, add, del)
if err != nil {
ts.t.Fatalf("Sync failed: %v", err)
}
sort.Slice(inputs, func(i, j int) bool {
if inputs[i].Domain != inputs[j].Domain {
return inputs[i].Domain < inputs[j].Domain
}
return string(inputs[i].Prog) < string(inputs[j].Prog)
})
return domain, inputs, pending
}
func (ts *TestState) AddRepro(name string, repro []byte) {
ts.t.Helper()
if err := ts.state.AddRepro(name, repro); err != nil {
ts.t.Fatalf("AddRepro failed: %v", err)
}
}
func (ts *TestState) PendingRepro(name string) []byte {
ts.t.Helper()
repro, err := ts.state.PendingRepro(name)
if err != nil {
ts.t.Fatalf("PendingRepro failed: %v", err)
}
return repro
}
func TestBasic(t *testing.T) {
st := MakeTestState(t)
defer st.Close()
if _, _, _, err := st.state.Sync("foo", nil, nil); err == nil {
t.Fatalf("synced with unconnected manager")
}
calls := []string{"read", "write"}
st.Connect("foo", "", false, calls, nil)
st.Sync("foo", nil, nil)
}
func TestRepro(t *testing.T) {
st := MakeTestState(t)
defer st.Close()
st.Connect("foo", "", false, []string{"open", "read", "write"}, nil)
st.Connect("bar", "", false, []string{"open", "read", "close"}, nil)
expectPendingRepro := func(name, result string) {
t.Helper()
repro := st.PendingRepro(name)
if string(repro) != result {
t.Fatalf("PendingRepro returned %q, want %q", string(repro), result)
}
}
expectPendingRepro("foo", "")
expectPendingRepro("bar", "")
st.AddRepro("foo", []byte("open()"))
expectPendingRepro("foo", "")
expectPendingRepro("bar", "open()")
expectPendingRepro("bar", "")
// This repro is already present.
st.AddRepro("bar", []byte("open()"))
st.AddRepro("bar", []byte("read()"))
st.AddRepro("bar", []byte("open()\nread()"))
// This does not satisfy foo's call set.
st.AddRepro("bar", []byte("close()"))
expectPendingRepro("bar", "")
// Check how persistence works.
st.Reload()
st.Connect("foo", "", false, []string{"open", "read", "write"}, nil)
st.Connect("bar", "", false, []string{"open", "read", "close"}, nil)
expectPendingRepro("bar", "")
expectPendingRepro("foo", "read()")
expectPendingRepro("foo", "open()\nread()")
expectPendingRepro("foo", "")
}
func TestDomain(t *testing.T) {
st := MakeTestState(t)
defer st.Close()
st.Connect("client0", "", false, []string{"open"}, nil)
st.Connect("client1", "domain1", false, []string{"open"}, nil)
st.Connect("client2", "domain2", false, []string{"open"}, nil)
st.Connect("client3", "domain3", false, []string{"open"}, nil)
{
domain, inputs, pending := st.Sync("client0", [][]byte{[]byte("open(0x0)")}, nil)
if domain != "" || len(inputs) != 0 || pending != 0 {
t.Fatalf("bad sync result: %v, %v, %v", domain, inputs, pending)
}
}
{
domain, inputs, pending := st.Sync("client0", [][]byte{[]byte("open(0x1)")}, nil)
if domain != "" || len(inputs) != 0 || pending != 0 {
t.Fatalf("bad sync result: %v, %v, %v", domain, inputs, pending)
}
}
{
domain, inputs, pending := st.Sync("client1", [][]byte{[]byte("open(0x2)"), []byte("open(0x1)")}, nil)
if domain != "domain1" || pending != 0 {
t.Fatalf("bad sync result: %v, %v, %v", domain, inputs, pending)
}
if diff := cmp.Diff(inputs, []rpctype.HubInput{
{Domain: "", Prog: []byte("open(0x0)")},
}); diff != "" {
t.Fatal(diff)
}
}
{
_, inputs, _ := st.Sync("client2", [][]byte{[]byte("open(0x3)")}, nil)
if diff := cmp.Diff(inputs, []rpctype.HubInput{
{Domain: "", Prog: []byte("open(0x0)")},
{Domain: "domain1", Prog: []byte("open(0x1)")},
{Domain: "domain1", Prog: []byte("open(0x2)")},
}); diff != "" {
t.Fatal(diff)
}
}
{
_, inputs, _ := st.Sync("client0", nil, nil)
if diff := cmp.Diff(inputs, []rpctype.HubInput{
{Domain: "domain1", Prog: []byte("open(0x2)")},
{Domain: "domain2", Prog: []byte("open(0x3)")},
}); diff != "" {
t.Fatal(diff)
}
}
st.Reload()
st.Connect("client3", "domain3", false, []string{"open"}, nil)
{
_, inputs, _ := st.Sync("client3", nil, nil)
if diff := cmp.Diff(inputs, []rpctype.HubInput{
{Domain: "", Prog: []byte("open(0x0)")},
{Domain: "domain1", Prog: []byte("open(0x1)")},
{Domain: "domain1", Prog: []byte("open(0x2)")},
{Domain: "domain2", Prog: []byte("open(0x3)")},
}); diff != "" {
t.Fatal(diff)
}
}
}
|