forked from PumpkinSeed/sqlfuzz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
263 lines (247 loc) · 5.94 KB
/
main_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
260
261
262
263
package main
import (
"fmt"
"testing"
"github.com/PumpkinSeed/sqlfuzz/drivers"
"github.com/PumpkinSeed/sqlfuzz/drivers/types"
"github.com/PumpkinSeed/sqlfuzz/pkg/connector"
"github.com/PumpkinSeed/sqlfuzz/pkg/flags"
"github.com/PumpkinSeed/sqlfuzz/pkg/fuzzer"
"github.com/brianvoe/gofakeit/v5"
)
const (
testTableName = "Persons"
)
func TestFuzz(t *testing.T) {
f := flags.Flags{}
f.Driver = types.Flags{
Username: "test",
Password: "test",
Database: "test",
Host: "localhost",
Port: "3306",
Driver: "mysql",
}
f.Table = testTableName
f.Parsed = true
f.Num = 10
f.Workers = 2
f.Seed = 1
gofakeit.Seed(int64(f.Seed))
driver := drivers.New(f.Driver)
testable := drivers.NewTestable(f.Driver)
db := connector.Connection(driver, f)
defer db.Close()
if _, err := db.Exec(fmt.Sprintf("DROP TABLE IF EXISTS %s", f.Table)); err != nil {
t.Fatal(err)
}
if err := testable.TestTable(db, "single", f.Table); err != nil {
t.Fatal(err)
}
fields, err := driver.Describe(f.Table, db)
if err != nil {
t.Fatal(err.Error())
}
err = fuzzer.Run(fields, f)
if err != nil {
t.Fatal(err)
}
res, err := db.Query(fmt.Sprintf("SELECT * FROM %s", f.Table))
if err != nil {
t.Fatal(err)
}
defer res.Close()
var i int
for res.Next() {
tt := testTable{}
if err := res.Scan(&tt.id, &tt.firstname, &tt.lastname, &tt.email, &tt.reqDate); err != nil {
t.Error(err)
continue
}
if err := tt.Validate(); err != nil {
t.Error(err)
}
i++
}
if i == 0 {
t.Error("the table should not be empty")
}
}
func TestFuzzPostgres(t *testing.T) {
f := flags.Flags{}
f.Driver = types.Flags{
Username: "test",
Password: "test",
Database: "test",
Host: "localhost",
Port: "5432",
Driver: "postgres",
}
f.Table = testTableName
f.Parsed = true
f.Num = 10
f.Workers = 2
f.Seed = 1
gofakeit.Seed(int64(f.Seed))
driver := drivers.New(f.Driver)
testable := drivers.NewTestable(f.Driver)
db := connector.Connection(driver, f)
defer db.Close()
if _, err := db.Exec(fmt.Sprintf("DROP TABLE IF EXISTS %s", f.Table)); err != nil {
t.Fatal(err)
}
if err := testable.TestTable(db, "single", f.Table); err != nil {
t.Fatal(err)
}
fields, err := driver.Describe(f.Table, db)
if err != nil {
t.Fatal(err.Error())
}
err = fuzzer.Run(fields, f)
if err != nil {
t.Fatal(err)
}
res, err := db.Query(fmt.Sprintf("SELECT * FROM %s", f.Table))
if err != nil {
t.Fatal(err)
}
defer res.Close()
var i int
for res.Next() {
columns, err := res.Columns()
if err != nil {
t.Error(err)
}
var valueHolders []interface{}
for j := 0; j < len(columns); j++ {
var tempInterface interface{}
valueHolders = append(valueHolders, &tempInterface)
}
if err := res.Scan(valueHolders...); err != nil {
t.Error(err)
continue
}
for index, val := range valueHolders {
// If given column index is valid and value is nil, return error
if val == nil {
t.Error(fmt.Sprintf("Invalid value received for column %s", columns[index]))
}
}
i++
}
if i == 0 {
t.Error("the table should not be empty")
}
}
func TestMysqlMultiInsert(t *testing.T) {
f := flags.Flags{}
f.Driver = types.Flags{
Username: "test",
Password: "test",
Database: "test",
Host: "localhost",
Port: "3306",
Driver: "mysql",
}
f.Table = testTableName
f.Parsed = true
f.Num = 10
f.Workers = 2
f.Seed = 1
gofakeit.Seed(int64(f.Seed))
driver := drivers.New(f.Driver)
testable := drivers.NewTestable(f.Driver)
test, err := testable.GetTestCase("multi")
if err != nil {
t.Error(fmt.Sprintf("postgres : error fetching test case for multi. %v", err.Error()))
}
db := connector.Connection(driver, f)
defer db.Close()
if _, err := db.Exec(fmt.Sprintf("DROP TABLE IF EXISTS %s", f.Table)); err != nil {
t.Fatal(err)
}
if err := testable.TestTable(db, "multi", f.Table); err != nil {
t.Fatal(err)
}
tables := test.TableCreationOrder
tableFieldMap, insertionOrder, err := driver.MultiDescribe(tables, db)
if err != nil {
t.Errorf("Error describing tables %v. Error %v", tables, err)
}
err = fuzzer.RunMulti(tableFieldMap, insertionOrder, f)
if err != nil {
t.Errorf("error during multi insert %v", err.Error())
}
}
func TestPostgresMultiInsert(t *testing.T) {
f := flags.Flags{}
f.Driver = types.Flags{
Username: "test",
Password: "test",
Database: "test",
Host: "localhost",
Port: "5432",
Driver: "postgres",
}
f.Table = testTableName
f.Parsed = true
f.Num = 10
f.Workers = 2
f.Seed = 1
gofakeit.Seed(int64(f.Seed))
driver := drivers.New(f.Driver)
testable := drivers.NewTestable(f.Driver)
test, err := testable.GetTestCase("multi")
if err != nil {
t.Error(fmt.Sprintf("postgres : error fetching test case for multi. %v", err.Error()))
}
db := connector.Connection(driver, f)
defer db.Close()
if _, err := db.Exec(fmt.Sprintf("DROP TABLE IF EXISTS %s", f.Table)); err != nil {
t.Fatal(err)
}
if err := testable.TestTable(db, "multi", f.Table); err != nil {
t.Fatal(err)
}
tables := test.TableCreationOrder
tableFieldMap, insertionOrder, err := driver.MultiDescribe(tables, db)
if err != nil {
t.Errorf("Error describing tables %v. Error %v", tables, err)
}
err = fuzzer.RunMulti(tableFieldMap, insertionOrder, f)
if err != nil {
t.Errorf("error during multi insert %v", err.Error())
}
}
type testTable struct {
id int
firstname string
lastname string
email string
reqDate string
}
func (tt testTable) Validate() error {
var err error
if tt.id == 0 {
err = warpErr(err, "id should not be 0")
}
if tt.firstname == "" {
err = warpErr(err, "firstname should not be empty string")
}
if tt.lastname == "" {
err = warpErr(err, "lastname should not be empty string")
}
if tt.email == "" {
err = warpErr(err, "email should not be empty string")
}
if tt.reqDate == "" {
err = warpErr(err, "reqDate should not be null")
}
return err
}
func warpErr(err error, msg string) error {
if err != nil {
return fmt.Errorf("%w;"+msg, err)
}
return fmt.Errorf(msg)
}