-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathparser_test.go
More file actions
206 lines (172 loc) · 6.56 KB
/
Copy pathparser_test.go
File metadata and controls
206 lines (172 loc) · 6.56 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
// Copyright 2023 Felipe Zipitria
// SPDX-License-Identifier: Apache-2.0
package main
import (
"os"
"testing"
"github.com/antlr4-go/antlr/v4"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"
"github.com/coreruleset/seclang_parser/parser"
)
// TestData represents the structure of the shared test YAML file
type TestData struct {
CRSTestFiles []string `yaml:"crs_test_files"`
PluginsTestFiles []string `yaml:"plugins_test_files"`
GenericTests map[string]GenericTestCase `yaml:"generic_tests"`
CheckOutputTests map[string]CheckOutputTestCase `yaml:"check_output_tests"`
}
type GenericTestCase struct {
ErrorCount int `yaml:"error_count"`
Comment string `yaml:"comment"`
}
type CheckOutputTestCase struct {
ErrorCount int `yaml:"error_count"`
Comment string `yaml:"comment"`
ExpectedResult ParserResult `yaml:"expected_result"`
}
var (
crsTestFiles []string
pluginsTestFiles []string
genericTests map[string]GenericTestCase
checkOutputTests map[string]CheckOutputTestCase
)
func init() {
// Load test data from shared YAML file
data, err := os.ReadFile("testdata/tests.yaml")
if err != nil {
panic("Failed to read test data file: " + err.Error())
}
var testData TestData
if err := yaml.Unmarshal(data, &testData); err != nil {
panic("Failed to parse test data file: " + err.Error())
}
crsTestFiles = testData.CRSTestFiles
pluginsTestFiles = testData.PluginsTestFiles
genericTests = testData.GenericTests
checkOutputTests = testData.CheckOutputTests
}
func TestSecLang(t *testing.T) {
for file, data := range genericTests {
t.Logf("Testing file %s", file)
input, err := antlr.NewFileStream(file)
if err != nil {
t.Errorf("Error reading file %s", file)
continue
}
lexer := parser.NewSecLangLexer(input)
lexerErrors := NewCustomErrorListener()
lexer.RemoveErrorListeners()
lexer.AddErrorListener(lexerErrors)
parserErrors := NewCustomErrorListener()
stream := antlr.NewCommonTokenStream(lexer, 0)
p := parser.NewSecLangParser(stream)
p.RemoveErrorListeners()
p.AddErrorListener(parserErrors)
p.BuildParseTrees = true
tree := p.Configuration()
antlr.ParseTreeWalkerDefault.Walk(NewTreeShapeListener(), tree)
if len(lexerErrors.Errors) > 0 && data.ErrorCount != (len(lexerErrors.Errors)+len(parserErrors.Errors)) {
t.Logf("Lexer %d errors found\n", len(lexerErrors.Errors))
t.Logf("First error: %v\n", lexerErrors.Errors[0])
}
if len(parserErrors.Errors) > 0 && data.ErrorCount != (len(lexerErrors.Errors)+len(parserErrors.Errors)) {
t.Logf("Parser %d errors found\n", len(parserErrors.Errors))
t.Logf("First error: %v\n", parserErrors.Errors[0])
}
require.Equalf(t, data.ErrorCount, (len(lexerErrors.Errors) + len(parserErrors.Errors)), "Error count mismatch for file %s -> we want: %s\n", file, data.Comment)
}
}
func TestSeclangOutput(t *testing.T) {
for file, data := range checkOutputTests {
t.Logf("Testing file %s", file)
input, err := antlr.NewFileStream(file)
if err != nil {
t.Errorf("Error reading file %s", file)
continue
}
lexer := parser.NewSecLangLexer(input)
lexerErrors := NewCustomErrorListener()
lexer.RemoveErrorListeners()
lexer.AddErrorListener(lexerErrors)
parserErrors := NewCustomErrorListener()
stream := antlr.NewCommonTokenStream(lexer, 0)
p := parser.NewSecLangParser(stream)
p.RemoveErrorListeners()
p.AddErrorListener(parserErrors)
p.BuildParseTrees = true
tree := p.Configuration()
listener := NewTreeShapeListener()
antlr.ParseTreeWalkerDefault.Walk(listener, tree)
if len(lexerErrors.Errors) > 0 && data.ErrorCount != (len(lexerErrors.Errors)+len(parserErrors.Errors)) {
t.Logf("Lexer %d errors found\n", len(lexerErrors.Errors))
t.Logf("First error: %v\n", lexerErrors.Errors[0])
}
if len(parserErrors.Errors) > 0 && data.ErrorCount != (len(lexerErrors.Errors)+len(parserErrors.Errors)) {
t.Logf("Parser %d errors found\n", len(parserErrors.Errors))
t.Logf("First error: %v\n", parserErrors.Errors[0])
}
require.Equalf(t, data.ErrorCount, (len(lexerErrors.Errors) + len(parserErrors.Errors)), "Error count mismatch for file %s -> we want: %s\n", file, data.Comment)
require.Equalf(t, data.ExpectedResult, listener.results, "Expected result mismatch for file %s -> we want: %v\n", file, data.ExpectedResult)
}
}
func TestCRSLang(t *testing.T) {
for _, file := range crsTestFiles {
t.Logf("Testing file %s", file)
input, err := antlr.NewFileStream(file)
if err != nil {
t.Fatalf("Error reading file %s", file)
}
lexer := parser.NewSecLangLexer(input)
lexerErrors := NewCustomErrorListener()
lexer.RemoveErrorListeners()
lexer.AddErrorListener(lexerErrors)
parserErrors := NewCustomErrorListener()
stream := antlr.NewCommonTokenStream(lexer, 0)
p := parser.NewSecLangParser(stream)
p.RemoveErrorListeners()
p.AddErrorListener(parserErrors)
p.BuildParseTrees = true
tree := p.Configuration()
antlr.ParseTreeWalkerDefault.Walk(NewTreeShapeListener(), tree)
if len(lexerErrors.Errors) > 0 {
t.Logf("Lexer %d errors found\n", len(lexerErrors.Errors))
t.Logf("First error: %v\n", lexerErrors.Errors[0])
}
if len(parserErrors.Errors) > 0 {
t.Logf("Parser %d errors found\n", len(parserErrors.Errors))
t.Logf("First error: %v\n", parserErrors.Errors[0])
}
require.Equalf(t, 0, (len(lexerErrors.Errors) + len(parserErrors.Errors)), "Error count mismatch for file %s -> we want no errors\n", file)
}
}
func TestPlugins(t *testing.T) {
for _, file := range pluginsTestFiles {
t.Logf("Testing file %s", file)
input, err := antlr.NewFileStream(file)
if err != nil {
t.Fatalf("Error reading file %s", file)
}
lexer := parser.NewSecLangLexer(input)
lexerErrors := NewCustomErrorListener()
lexer.RemoveErrorListeners()
lexer.AddErrorListener(lexerErrors)
parserErrors := NewCustomErrorListener()
stream := antlr.NewCommonTokenStream(lexer, 0)
p := parser.NewSecLangParser(stream)
p.RemoveErrorListeners()
p.AddErrorListener(parserErrors)
p.BuildParseTrees = true
tree := p.Configuration()
antlr.ParseTreeWalkerDefault.Walk(NewTreeShapeListener(), tree)
if len(lexerErrors.Errors) > 0 {
t.Logf("Lexer %d errors found\n", len(lexerErrors.Errors))
t.Logf("First error: %v\n", lexerErrors.Errors[0])
}
if len(parserErrors.Errors) > 0 {
t.Logf("Parser %d errors found\n", len(parserErrors.Errors))
t.Logf("First error: %v\n", parserErrors.Errors[0])
}
require.Equalf(t, 0, (len(lexerErrors.Errors) + len(parserErrors.Errors)), "Error count mismatch for file %s -> we want no errors\n", file)
}
}