-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathparams_test.go
More file actions
266 lines (230 loc) · 9.14 KB
/
params_test.go
File metadata and controls
266 lines (230 loc) · 9.14 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
package testing
import (
"os"
"path/filepath"
"testing"
)
func TestBuildToolParams_TestConfig(t *testing.T) {
// Create a temp test fixture with test-config.json
dir := t.TempDir()
testDir := filepath.Join(dir, "tools", "my_tool", "my_test")
os.MkdirAll(filepath.Join(testDir, "before"), 0o755)
os.MkdirAll(filepath.Join(testDir, "after"), 0o755)
os.WriteFile(filepath.Join(testDir, "test-config.json"),
[]byte(`{"toolName":"my_tool","arguments":{"key":"value"}}`), 0o600)
params, err := buildToolParams(dir, "my_tool", "my_test", testDir)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if params["key"] != "value" {
t.Errorf("params[key] = %v, want value", params["key"])
}
}
func TestBuildToolParams_MonitoringStateParams(t *testing.T) {
dir := t.TempDir()
testDir := filepath.Join(dir, "tools", "codeql_lsp_completion", "basic")
os.MkdirAll(filepath.Join(testDir, "before"), 0o755)
os.MkdirAll(filepath.Join(testDir, "after"), 0o755)
os.WriteFile(filepath.Join(testDir, "before", "monitoring-state.json"),
[]byte(`{"sessions":[],"parameters":{"file_path":"test.ql","line":3}}`), 0o600)
params, err := buildToolParams(dir, "codeql_lsp_completion", "basic", testDir)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if params["file_path"] != "test.ql" {
t.Errorf("params[file_path] = %v, want test.ql", params["file_path"])
}
// line comes as float64 from JSON
if params["line"] != float64(3) {
t.Errorf("params[line] = %v, want 3", params["line"])
}
}
func TestBuildToolParams_ValidateCodeqlQuery(t *testing.T) {
dir := t.TempDir()
testDir := filepath.Join(dir, "tools", "validate_codeql_query", "syntax_validation")
os.MkdirAll(filepath.Join(testDir, "before"), 0o755)
os.MkdirAll(filepath.Join(testDir, "after"), 0o755)
os.WriteFile(filepath.Join(testDir, "before", "monitoring-state.json"),
[]byte(`{"sessions":[]}`), 0o600)
params, err := buildToolParams(dir, "validate_codeql_query", "syntax_validation", testDir)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if params["query"] != "from int i select i" {
t.Errorf("params[query] = %v, want 'from int i select i'", params["query"])
}
if params["language"] != "java" {
t.Errorf("params[language] = %v, want java", params["language"])
}
}
func TestBuildToolParams_ResolveQueries_UsesDirectoryKey(t *testing.T) {
dir := t.TempDir()
testDir := filepath.Join(dir, "tools", "codeql_resolve_queries", "resolve_queries")
os.MkdirAll(filepath.Join(testDir, "before"), 0o755)
os.MkdirAll(filepath.Join(testDir, "after"), 0o755)
params, err := buildToolParams(dir, "codeql_resolve_queries", "resolve_queries", testDir)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
// The server schema expects "directory", not "path"
if _, ok := params["directory"]; !ok {
t.Errorf("expected params to contain 'directory' key, got keys: %v", params)
}
if _, ok := params["path"]; ok {
t.Errorf("params should NOT contain 'path' key for codeql_resolve_queries; use 'directory' instead")
}
}
func TestBuildToolParams_ResolveLanguages(t *testing.T) {
dir := t.TempDir()
testDir := filepath.Join(dir, "tools", "codeql_resolve_languages", "list_languages")
os.MkdirAll(filepath.Join(testDir, "before"), 0o755)
os.MkdirAll(filepath.Join(testDir, "after"), 0o755)
os.WriteFile(filepath.Join(testDir, "before", "monitoring-state.json"),
[]byte(`{"sessions":[]}`), 0o600)
params, err := buildToolParams(dir, "codeql_resolve_languages", "list_languages", testDir)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
// codeql_resolve_languages takes no params
if len(params) != 0 {
t.Errorf("expected empty params for codeql_resolve_languages, got %v", params)
}
}
func TestBuildToolParams_UnknownTool(t *testing.T) {
dir := t.TempDir()
testDir := filepath.Join(dir, "tools", "unknown_tool_xyz", "test1")
os.MkdirAll(filepath.Join(testDir, "before"), 0o755)
os.MkdirAll(filepath.Join(testDir, "after"), 0o755)
os.WriteFile(filepath.Join(testDir, "before", "monitoring-state.json"),
[]byte(`{"sessions":[]}`), 0o600)
_, err := buildToolParams(dir, "unknown_tool_xyz", "test1", testDir)
if err == nil {
t.Fatal("expected error for unknown tool, got nil")
}
}
func TestFindFilesByExt(t *testing.T) {
dir := t.TempDir()
os.WriteFile(filepath.Join(dir, "a.ql"), []byte(""), 0o600)
os.WriteFile(filepath.Join(dir, "b.ql"), []byte(""), 0o600)
os.WriteFile(filepath.Join(dir, "c.txt"), []byte(""), 0o600)
qlFiles := findFilesByExt(dir, ".ql")
if len(qlFiles) != 2 {
t.Errorf("found %d .ql files, want 2", len(qlFiles))
}
if qlFiles[0] != "a.ql" || qlFiles[1] != "b.ql" {
t.Errorf("files = %v, want [a.ql b.ql]", qlFiles)
}
txtFiles := findFilesByExt(dir, ".txt")
if len(txtFiles) != 1 {
t.Errorf("found %d .txt files, want 1", len(txtFiles))
}
}
func TestIsSARIFTool(t *testing.T) {
tests := []struct {
name string
want bool
}{
{"sarif_extract_rule", true},
{"sarif_list_rules", true},
{"sarif_compare_alerts", true},
{"sarif_diff_runs", true},
{"sarif_rule_to_markdown", true},
{"codeql_query_run", false},
{"validate_codeql_query", false},
{"codeql_pack_install", false},
}
for _, tt := range tests {
if got := isSARIFTool(tt.name); got != tt.want {
t.Errorf("isSARIFTool(%q) = %v, want %v", tt.name, got, tt.want)
}
}
}
func TestBuildToolParams_SARIFToolWithConfig(t *testing.T) {
dir := t.TempDir()
testDir := filepath.Join(dir, "tools", "sarif_extract_rule", "extract_sql_injection")
beforeDir := filepath.Join(testDir, "before")
os.MkdirAll(beforeDir, 0o755)
os.MkdirAll(filepath.Join(testDir, "after"), 0o755)
// Write test-config.json with ruleId but no sarifPath
os.WriteFile(filepath.Join(testDir, "test-config.json"),
[]byte(`{"toolName":"sarif_extract_rule","arguments":{"ruleId":"js/sql-injection"}}`), 0o600)
// Write a SARIF file in before/
os.WriteFile(filepath.Join(beforeDir, "test-input.sarif"),
[]byte(`{"version":"2.1.0"}`), 0o600)
params, err := buildToolParams(dir, "sarif_extract_rule", "extract_sql_injection", testDir)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
// Should have sarifPath injected from before/
sarifPath, ok := params["sarifPath"].(string)
if !ok || sarifPath == "" {
t.Error("expected sarifPath to be injected from before/ directory")
}
// Should still have ruleId from config
if params["ruleId"] != "js/sql-injection" {
t.Errorf("params[ruleId] = %v, want js/sql-injection", params["ruleId"])
}
}
func TestBuildToolParams_SARIFCompareAlertsWithConfig(t *testing.T) {
dir := t.TempDir()
testDir := filepath.Join(dir, "tools", "sarif_compare_alerts", "sink_overlap")
beforeDir := filepath.Join(testDir, "before")
os.MkdirAll(beforeDir, 0o755)
os.MkdirAll(filepath.Join(testDir, "after"), 0o755)
// Write test-config.json with alertA/alertB but no sarifPath
os.WriteFile(filepath.Join(testDir, "test-config.json"),
[]byte(`{"toolName":"sarif_compare_alerts","arguments":{"alertA":{"ruleId":"r1","resultIndex":0},"alertB":{"ruleId":"r2","resultIndex":0},"overlapMode":"sink"}}`), 0o600)
// Write a SARIF file in before/
os.WriteFile(filepath.Join(beforeDir, "test-input.sarif"),
[]byte(`{"version":"2.1.0"}`), 0o600)
params, err := buildToolParams(dir, "sarif_compare_alerts", "sink_overlap", testDir)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
// alertA should have sarifPath injected
alertA, ok := params["alertA"].(map[string]any)
if !ok {
t.Fatal("expected alertA in params")
}
if alertA["sarifPath"] == nil || alertA["sarifPath"] == "" {
t.Error("expected sarifPath injected into alertA")
}
// alertB should have sarifPath injected
alertB, ok := params["alertB"].(map[string]any)
if !ok {
t.Fatal("expected alertB in params")
}
if alertB["sarifPath"] == nil || alertB["sarifPath"] == "" {
t.Error("expected sarifPath injected into alertB")
}
}
func TestBuildToolParams_SARIFDiffByCommitsWithConfig(t *testing.T) {
dir := t.TempDir()
testDir := filepath.Join(dir, "tools", "sarif_diff_by_commits", "file_level_classification")
beforeDir := filepath.Join(testDir, "before")
os.MkdirAll(beforeDir, 0o755)
os.MkdirAll(filepath.Join(testDir, "after"), 0o755)
// Write test-config.json with refRange and granularity but no sarifPath
os.WriteFile(filepath.Join(testDir, "test-config.json"),
[]byte(`{"toolName":"sarif_diff_by_commits","arguments":{"refRange":"HEAD..HEAD","granularity":"file"}}`), 0o600)
// Write a SARIF file in before/
os.WriteFile(filepath.Join(beforeDir, "results.sarif"),
[]byte(`{"version":"2.1.0","runs":[{"tool":{"driver":{"name":"CodeQL","rules":[]}},"results":[]}]}`), 0o600)
params, err := buildToolParams(dir, "sarif_diff_by_commits", "file_level_classification", testDir)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
// Should have sarifPath injected from before/
sarifPath, ok := params["sarifPath"].(string)
if !ok || sarifPath == "" {
t.Error("expected sarifPath to be injected from before/ directory")
}
// Should have refRange from config
if params["refRange"] != "HEAD..HEAD" {
t.Errorf("params[refRange] = %v, want HEAD..HEAD", params["refRange"])
}
// Should have granularity from config
if params["granularity"] != "file" {
t.Errorf("params[granularity] = %v, want file", params["granularity"])
}
}