-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconverter_fuzz_test.go
More file actions
278 lines (233 loc) · 7.08 KB
/
Copy pathconverter_fuzz_test.go
File metadata and controls
278 lines (233 loc) · 7.08 KB
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package cel2squirrel
import (
"testing"
"github.com/google/cel-go/cel"
)
// FuzzConverter fuzzes the CEL to SQL converter with various inputs
func FuzzConverter(f *testing.F) {
// Seed corpus with valid CEL expressions
f.Add(`status == "published"`)
f.Add(`age >= 18`)
f.Add(`status == "published" && age >= 18`)
f.Add(`status == "published" || status == "featured"`)
f.Add(`!is_draft`)
f.Add(`age < 100`)
f.Add(`label.contains("test")`)
f.Add(`label.startsWith("prod")`)
f.Add(`label.endsWith("v2")`)
f.Add(`status in ["published", "featured"]`)
f.Add(`age in [18, 21, 25]`)
f.Add(`deletedAt == null`)
f.Add(`deletedAt != null`)
f.Add(`true`)
f.Add(`false`)
f.Add(`is_published`)
f.Add(`(status == "published" || status == "featured") && age >= 18`)
config := Config{
FieldDeclarations: map[string]ColumnMapping{
"status": {Type: cel.StringType, Column: "status"},
"age": {Type: cel.IntType, Column: "age"},
"is_draft": {Type: cel.BoolType, Column: "is_draft"},
"is_published": {Type: cel.BoolType, Column: "is_published"},
"label": {Type: cel.StringType, Column: "label"},
"deletedAt": {Type: cel.TimestampType, Column: "deletedAt"},
"rating": {Type: cel.DoubleType, Column: "rating"},
"count": {Type: cel.UintType, Column: "count"},
},
}
converter, err := NewConverter(config)
if err != nil {
f.Fatalf("failed to create converter: %v", err)
}
f.Fuzz(func(t *testing.T, celExpr string) {
// Try to convert the CEL expression
result, err := converter.Convert(celExpr)
// If conversion succeeds, verify the SQL can be generated
if err == nil && result != nil {
sql, _, sqlErr := result.Where.ToSql()
if sqlErr != nil {
t.Errorf("Valid conversion produced invalid SQL: %v", sqlErr)
return
}
// Basic sanity checks on generated SQL
if sql == "" {
t.Error("Generated SQL is empty")
}
}
// Errors are acceptable - many random inputs will be invalid CEL
// We're just checking that the converter doesn't panic or produce invalid results
})
}
// FuzzConverterWithFieldMappings fuzzes with field mappings enabled
func FuzzConverterWithFieldMappings(f *testing.F) {
f.Add(`isDraft == false`, "isDraft", "is_draft")
f.Add(`ownerId == "user123"`, "ownerId", "owner_id")
f.Add(`userName.contains("john")`, "userName", "user_name")
f.Add(`isActive && !isDeleted`, "isActive", "is_active")
config := Config{
FieldDeclarations: map[string]ColumnMapping{
"isDraft": {Type: cel.BoolType, Column: "is_draft"},
"ownerId": {Type: cel.StringType, Column: "owner_id"},
"userName": {Type: cel.StringType, Column: "user_name"},
"isActive": {Type: cel.BoolType, Column: "is_active"},
"isDeleted": {Type: cel.BoolType, Column: "is_deleted"},
},
}
converter, err := NewConverter(config)
if err != nil {
f.Fatalf("failed to create converter: %v", err)
}
f.Fuzz(func(t *testing.T, celExpr, _, _ string) {
// Note: field mappings are now configured at converter creation time
// This test verifies column mappings are applied correctly
result, err := converter.Convert(celExpr)
// If conversion succeeds, verify SQL generation
if err == nil && result != nil {
sql, _, sqlErr := result.Where.ToSql()
if sqlErr != nil {
t.Errorf("Valid conversion produced invalid SQL: %v", sqlErr)
return
}
if sql == "" {
t.Error("Generated SQL is empty")
}
}
})
}
// FuzzQuoteIdentifier fuzzes the QuoteIdentifier function
func FuzzQuoteIdentifier(f *testing.F) {
// Seed corpus
f.Add("user_id")
f.Add("user name")
f.Add(`user"id`)
f.Add(`"quoted"`)
f.Add("")
f.Add("a")
f.Add("table.column")
f.Add("123_column")
f.Add("column_123")
f.Fuzz(func(t *testing.T, identifier string) {
result := QuoteIdentifier(identifier)
// Result should always start and end with quote
if len(result) < 2 {
t.Errorf("QuoteIdentifier(%q) = %q, too short", identifier, result)
return
}
if result[0] != '"' || result[len(result)-1] != '"' {
t.Errorf("QuoteIdentifier(%q) = %q, should be quoted", identifier, result)
}
// Count quotes - should have at least 2 (start and end)
quoteCount := 0
for _, ch := range result {
if ch == '"' {
quoteCount++
}
}
if quoteCount < 2 {
t.Errorf("QuoteIdentifier(%q) = %q, insufficient quotes", identifier, result)
}
})
}
// FuzzConverterLogicalOperators fuzzes complex logical operator combinations
func FuzzConverterLogicalOperators(f *testing.F) {
// Seed with various logical combinations
f.Add(true, true, true) // a && b && c
f.Add(true, true, false) // a && b || c
f.Add(true, false, true) // a || b && c
f.Add(false, false, false) // !a && !b && !c
config := Config{
FieldDeclarations: map[string]ColumnMapping{
"a": {Type: cel.BoolType, Column: "a"},
"b": {Type: cel.BoolType, Column: "b"},
"c": {Type: cel.BoolType, Column: "c"},
},
}
converter, err := NewConverter(config)
if err != nil {
f.Fatalf("failed to create converter: %v", err)
}
f.Fuzz(func(t *testing.T, useAnd1, useAnd2, useNot bool) {
// Build a CEL expression based on fuzzer input
expr := "a"
if useNot {
expr = "!a"
}
op1 := "&&"
if !useAnd1 {
op1 = "||"
}
expr += " " + op1 + " b"
op2 := "&&"
if !useAnd2 {
op2 = "||"
}
expr += " " + op2 + " c"
result, err := converter.Convert(expr)
if err != nil {
// Errors are okay in fuzzing
return
}
if result == nil {
t.Error("Convert() returned nil result without error")
return
}
sql, _, sqlErr := result.Where.ToSql()
if sqlErr != nil {
t.Errorf("ToSql() error = %v", sqlErr)
return
}
if sql == "" {
t.Error("ToSql() returned empty string")
}
})
}
// FuzzConverterComparisons fuzzes comparison operators with various values
func FuzzConverterComparisons(f *testing.F) {
// Seed with various comparison patterns
f.Add("age", int64(0), 0) // ==
f.Add("age", int64(100), 1) // !=
f.Add("age", int64(18), 2) // <
f.Add("age", int64(21), 3) // <=
f.Add("age", int64(65), 4) // >
f.Add("age", int64(70), 5) // >=
config := Config{
FieldDeclarations: map[string]ColumnMapping{
"age": {Type: cel.IntType, Column: "age"},
"status": {Type: cel.StringType, Column: "status"},
"count": {Type: cel.UintType, Column: "count"},
"rating": {Type: cel.DoubleType, Column: "rating"},
},
}
converter, err := NewConverter(config)
if err != nil {
f.Fatalf("failed to create converter: %v", err)
}
f.Fuzz(func(t *testing.T, field string, value int64, op int) {
// Map op to operator
operators := []string{"==", "!=", "<", "<=", ">", ">="}
if op < 0 || op >= len(operators) {
return
}
operator := operators[op]
// Only use valid fields
if field != "age" && field != "count" {
return
}
celExpr := field + " " + operator + " " + string(rune(value))
result, err := converter.Convert(celExpr)
if err != nil {
// Many combinations will fail - that's okay
return
}
if result != nil {
sql, _, sqlErr := result.Where.ToSql()
if sqlErr != nil {
t.Errorf("ToSql() error = %v", sqlErr)
return
}
if sql == "" {
t.Error("ToSql() returned empty string")
}
}
})
}