forked from PaesslerAG/gval
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gval_parsingFailure_test.go
179 lines (163 loc) · 4.33 KB
/
gval_parsingFailure_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
package gval
import (
"regexp/syntax"
"testing"
)
func TestParsingFailure(t *testing.T) {
testEvaluate(
[]evaluationTest{
{
name: "Invalid equality comparator",
expression: "1 = 1",
wantErr: unexpected(`"="`, "operator"),
},
{
name: "Invalid equality comparator",
expression: "1 === 1",
wantErr: unknownOp("==="),
},
{
name: "Too many characters for logical operator",
expression: "true &&& false",
wantErr: unknownOp("&&&"),
},
{
name: "Too many characters for logical operator",
expression: "true ||| false",
wantErr: unknownOp("|||"),
},
{
name: "Premature end to expression, via modifier",
expression: "10 > 5 +",
wantErr: unexpected("EOF", "extensions"),
},
{
name: "Premature end to expression, via comparator",
expression: "10 + 5 >",
wantErr: unexpected("EOF", "extensions"),
},
{
name: "Premature end to expression, via logical operator",
expression: "10 > 5 &&",
wantErr: unexpected("EOF", "extensions"),
},
{
name: "Premature end to expression, via ternary operator",
expression: "true ?",
wantErr: unexpected("EOF", "extensions"),
},
{
name: "Hanging REQ",
expression: "`wat` =~",
wantErr: unexpected("EOF", "extensions"),
},
{
name: "Invalid operator change to REQ",
expression: " / =~",
wantErr: unexpected(`"/"`, "extensions"),
},
{
name: "Invalid starting token, comparator",
expression: "> 10",
wantErr: unexpected(`">"`, "extensions"),
},
{
name: "Invalid starting token, modifier",
expression: "+ 5",
wantErr: unexpected(`"+"`, "extensions"),
},
{
name: "Invalid starting token, logical operator",
expression: "&& 5 < 10",
wantErr: unexpected(`"&"`, "extensions"),
},
{
name: "Invalid NUMERIC transition",
expression: "10 10",
wantErr: unexpected(`Int`, "operator"),
},
{
name: "Invalid STRING transition",
expression: "`foo` `foo`",
wantErr: `String while scanning operator`, // can't use func unexpected because the token was changed from String to RawString in go 1.11
},
{
name: "Invalid operator transition",
expression: "10 > < 10",
wantErr: unexpected(`"<"`, "extensions"),
},
{
name: "Starting with unbalanced parens",
expression: " ) ( arg2",
wantErr: unexpected(`")"`, "extensions"),
},
{
name: "Unclosed bracket",
expression: "[foo bar",
wantErr: unexpected(`EOF`, "extensions"),
},
{
name: "Unclosed quote",
expression: "foo == `responseTime",
wantErr: "could not parse string",
},
{
name: "Constant regex pattern fail to compile",
expression: "foo =~ `[abc`",
wantErr: string(syntax.ErrMissingBracket),
},
{
name: "Constant unmatch regex pattern fail to compile",
expression: "foo !~ `[abc`",
wantErr: string(syntax.ErrMissingBracket),
},
{
name: "Unbalanced parentheses",
expression: "10 > (1 + 50",
wantErr: unexpected(`EOF`, "parentheses"),
},
{
name: "Multiple radix",
expression: "127.0.0.1",
wantErr: unexpected(`Float`, "operator"),
},
{
name: "Hanging accessor",
expression: "foo.Bar.",
wantErr: unexpected(`EOF`, "field"),
},
{
name: "Incomplete Hex",
expression: "0x",
wantErr: `strconv.ParseFloat: parsing "0x": invalid syntax`,
},
{
name: "Invalid Hex literal",
expression: "0x > 0",
wantErr: `strconv.ParseFloat: parsing "0x": invalid syntax`,
},
{
name: "Hex float (Unsupported)",
expression: "0x1.1",
wantErr: `strconv.ParseFloat: parsing "0x1": invalid syntax`,
},
{
name: "Hex invalid letter",
expression: "0x12g1",
wantErr: `strconv.ParseFloat: parsing "0x12": invalid syntax`,
},
{
name: "Error after camouflage",
expression: "0 + ,",
wantErr: `unexpected "," while scanning extensions`,
},
},
t,
)
}
func unknownOp(op string) string {
return "unknown operator " + op
}
func unexpected(token, unit string) string {
return "unexpected " + token + " while scanning " + unit
}