-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoverage100_test.go
More file actions
270 lines (236 loc) · 9.15 KB
/
Copy pathcoverage100_test.go
File metadata and controls
270 lines (236 loc) · 9.15 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
package eql
import (
"strings"
"testing"
"time"
)
// This file drives the remaining defensive, recovery, and depth-limit branches
// to complete statement coverage. Several are white-box tests of unexported
// helpers or use the extractHook seam, because the corresponding paths cannot
// be reached with well-formed input.
// --- Panic recovery and timeout (via the extractHook seam) ---
func TestExtractPanicRecovery(t *testing.T) {
extractHook = func(string) { panic("boom") }
defer func() { extractHook = nil }()
res := ExtractConditions(`process where a == 1`)
if len(res.Errors) == 0 || !strings.Contains(res.Errors[0], "parser panic") {
t.Fatalf("expected panic recovery, got %v", res.Errors)
}
if res.Conditions == nil {
t.Error("Conditions must be non-nil after panic")
}
}
func TestExtractTimeout(t *testing.T) {
// A genuinely large input takes far longer than the tiny deadline to
// parse, so the timeout branch fires deterministically. No hook is used,
// so the abandoned parse goroutine touches only immutable package state
// (race-free with the rest of the suite).
old := MaxParseTime
MaxParseTime = 1 * time.Millisecond
defer func() { MaxParseTime = old }()
var b strings.Builder
b.WriteString("process where ")
for i := 0; i < 40000; i++ {
if i > 0 {
b.WriteString(" and ")
}
b.WriteString(`a == "x"`)
}
res := ExtractConditions(b.String())
if len(res.Errors) == 0 || !strings.Contains(res.Errors[0], "timeout") {
t.Fatalf("expected timeout, got %v", res.Errors)
}
}
// --- MaxInputSize guards ---
func TestParseInputTooLarge(t *testing.T) {
huge := "process where a == " + strings.Repeat("1", MaxInputSize+1)
if _, err := Parse(huge); err == nil || !strings.Contains(err.Error(), "maximum size") {
t.Errorf("Parse: %v", err)
}
if _, err := ParseExpression(huge); err == nil || !strings.Contains(err.Error(), "maximum size") {
t.Errorf("ParseExpression: %v", err)
}
res := ExtractConditions(huge)
if len(res.Errors) == 0 || !strings.Contains(res.Errors[0], "maximum size") {
t.Errorf("ExtractConditions: %v", res.Errors)
}
}
// --- Depth-limit branches across the parser ---
// TestDeepNestingErrors confirms deeply nested input is rejected (not crashed)
// end to end. The exact per-function guard coverage is in TestDepthGuardsWhiteBox.
func TestDeepNestingErrors(t *testing.T) {
rep := func(s string, k int) string { return strings.Repeat(s, k) }
for _, q := range []string{
`process where ` + rep("not ", 5000) + `a == 1`,
`process where ` + rep("(", 5000) + "a == 1" + rep(")", 5000),
`process where ` + rep("f(", 5000) + "x" + rep(")", 5000),
} {
res := ExtractConditions(q)
if res == nil || res.Conditions == nil {
t.Fatal("nil result for deep input")
}
if len(res.Errors) == 0 {
t.Errorf("expected depth error for %q", truncate(q, 50))
}
}
}
// --- Specific value-parse error branches ---
func TestRunsNonIntegerValue(t *testing.T) {
// `runs=1.5` lexes as a number token but fails integer conversion.
res := ExtractConditions(`sequence [a where true] with runs=1.5 [b where true]`)
if len(res.Errors) == 0 {
t.Error("expected error for non-integer runs")
}
}
func TestArrayIndexOverflow(t *testing.T) {
// An out-of-range array index fails integer conversion but still parses.
res := ExtractConditions(`process where process.args[99999999999999999999999] == "x"`)
if res == nil || res.Conditions == nil {
t.Fatal("nil result")
}
if len(res.Errors) == 0 {
t.Error("expected array index error")
}
}
func TestUntilWithoutBracket(t *testing.T) {
// `until` not followed by '[' drives parseSequenceStep's default (nil) path.
res := ExtractConditions(`sequence [a where true] [b where true] until x == 1`)
if len(res.Errors) == 0 {
t.Error("expected error for malformed until")
}
}
// --- Lexer edge escapes ---
func TestBackspaceEscape(t *testing.T) {
toks := lex(`"a\bc"`)
if toks[0].Value != "a\bc" {
t.Errorf("value = %q", toks[0].Value)
}
}
func TestCarriageReturnEscape(t *testing.T) {
toks := lex(`"a\rb"`)
if toks[0].Value != "a\rb" {
t.Errorf("value = %q", toks[0].Value)
}
}
func TestNormalizeCodeFenceWithBlankLines(t *testing.T) {
// Trailing blank lines after the closing fence exercise the empty-line
// skip in the backward closing-fence scan.
res := ExtractConditions("```eql\nprocess where a == 1\n```\n\n")
requireNoErrors(t, res)
requireConditions(t, res, `a == 1`)
}
// --- Renderer: right-side same-precedence parenthesization ---
func TestRenderChildRightSideParens(t *testing.T) {
// a - (b - c): the right operand of '-' has equal precedence and must be
// parenthesized. Built directly since the left-associative parser never
// produces this shape without an explicit Paren node.
fld := func(name string) Expr { return &Field{Path: []PathSeg{{Name: name}}} }
e := &Binary{Op: "-", L: fld("a"), R: &Binary{Op: "-", L: fld("b"), R: fld("c")}}
if got := ExprString(e); got != "a - (b - c)" {
t.Errorf("render = %q, want %q", got, "a - (b - c)")
}
// And the left side at equal precedence is NOT parenthesized.
e2 := &Binary{Op: "-", L: &Binary{Op: "-", L: fld("a"), R: fld("b")}, R: fld("c")}
if got := ExprString(e2); got != "a - b - c" {
t.Errorf("render = %q, want %q", got, "a - b - c")
}
}
// --- DeduplicateConditions case-insensitive key branch ---
func TestDedupCaseInsensitiveKey(t *testing.T) {
conds := []Condition{
{Field: "f", Operator: ":", Value: "x", CaseInsensitive: true},
{Field: "f", Operator: ":", Value: "x", CaseInsensitive: true}, // dup
{Field: "f", Operator: ":", Value: "x", CaseInsensitive: false},
}
got := DeduplicateConditions(conds)
if len(got) != 2 {
t.Errorf("dedup = %d, want 2", len(got))
}
}
// --- ClassifyFieldUsage pipe-stage branch ---
func TestClassifyFieldUsagePipeStage(t *testing.T) {
res := ExtractConditions(`process where a == 1 | filter b : "x"`)
requireNoErrors(t, res)
u := ClassifyFieldUsage(res, "b")
if !u.InPipes {
t.Errorf("b should be InPipes: %+v", u)
}
}
// --- White-box tests of unexported defensive guards ---
func TestNilGuardsWhiteBox(t *testing.T) {
if extractSubquery(nil) == nil {
t.Error("extractSubquery(nil) should return an empty result, not nil")
}
ex := newExtractor(&ParseResult{Conditions: []Condition{}})
ex.extractQuery(nil) // q == nil guard
ex.enterEventQuery(nil, 0, false, false, "") // e == nil guard
ex.extractSequence(nil) // s == nil guard
if got := ex.registerJoinKey(nil); got != nil { // ExprString(nil) == "" guard
t.Errorf("registerJoinKey(nil) = %v", got)
}
ex.emit(Condition{Field: "x"}) // Operator defaults to "=="
if len(ex.res.Conditions) != 1 || ex.res.Conditions[0].Operator != "==" {
t.Errorf("emit default operator: %+v", ex.res.Conditions)
}
}
func TestLexerAdvanceAtEOF(t *testing.T) {
l := &lexer{input: "", line: 1, col: 1}
l.advance() // pos >= len guard: must be a no-op
if l.pos != 0 {
t.Errorf("advance at EOF moved pos to %d", l.pos)
}
}
func TestPeekTypePastEnd(t *testing.T) {
lx := newLexer("a")
p := &parser{toks: lx.tokens}
if got := p.peekType(9); got != TokenEOF {
t.Errorf("peekType past end = %v, want EOF", got)
}
}
func TestFieldInTextEmptyField(t *testing.T) {
if fieldInText("some text", "") {
t.Error("fieldInText with empty field should be false")
}
}
// --- White-box depth guards ---
// TestDepthGuardsWhiteBox drives each depth guard deterministically by
// constructing a parser already at (or over) the ceiling. This is race-free
// (no shared global is mutated) and covers guards the recursive-descent loops
// short-circuit before reaching in normal parsing.
func TestDepthGuardsWhiteBox(t *testing.T) {
// atCeiling builds a parser primed at the depth ceiling for the given input.
atCeiling := func(src string) *parser {
return &parser{toks: newLexer(src).tokens, depth: maxExprDepth}
}
tooDeep := func(src string) *parser {
return &parser{toks: newLexer(src).tokens, tooDeep: true}
}
badExpr := func(name string, e Expr) {
if _, ok := e.(*BadExpr); !ok {
t.Errorf("%s: expected BadExpr, got %T", name, e)
}
}
// enter() failures at the ceiling.
badExpr("parseExpression", atCeiling("a").parseExpression())
badExpr("parseNot", atCeiling("not a").parseNot())
badExpr("parseUnary(-)", atCeiling("-a").parseUnary())
badExpr("parseUnary(+)", atCeiling("+a").parseUnary())
badExpr("parsePrimary(paren)", atCeiling("(a)").parsePrimary())
badExpr("parseCall", atCeiling("f(x)").parsePrimary())
badExpr("parseLineage", atCeiling("child of [process where a == 1]").parsePrimary())
// tooDeep short-circuits.
badExpr("parsePrimary(tooDeep)", tooDeep("a").parsePrimary())
badExpr("parseExpression(tooDeep)", tooDeep("a").parseExpression())
if got := tooDeep("by a, b").parseByKeys(); len(got) != 0 {
t.Errorf("parseByKeys(tooDeep) = %v, want none", got)
}
if got := tooDeep("| head 1").parsePipes(); len(got) != 0 {
t.Errorf("parsePipes(tooDeep) = %v, want none", got)
}
// parseSequence step-loop tooDeep guard.
if s := tooDeep("sequence [a where true] [b where true]").parseSequence(KindSequence); len(s.Steps) != 0 {
t.Errorf("parseSequence(tooDeep) steps = %d, want 0", len(s.Steps))
}
// parseParenList tooDeep guard (via an in-list).
tooDeep("a in (b, c)").parsePredicated()
}