-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathcodeql.test.ts
More file actions
442 lines (382 loc) · 11.9 KB
/
Copy pathcodeql.test.ts
File metadata and controls
442 lines (382 loc) · 11.9 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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
import fs from "fs";
import { tmpdir } from "os";
import path, { join } from "path";
import { rmRF } from "@actions/io";
import {
describe,
expect,
it,
beforeAll,
beforeEach,
afterEach,
afterAll,
} from "vitest";
import {
runQuery,
getBqrsInfo,
getDatabaseMetadata,
BQRSInfo,
getQueryPackQueries,
injectVersionControlInfo,
getSarifResultCount,
Sarif,
getSarifOutputType,
QueryMetadata,
getBqrsResultCount,
getQueryPackInfo,
} from "./codeql";
import { BaseCodeqlCli, CodeqlCli } from "./codeql-cli";
describe("codeql", () => {
const cwd = process.cwd();
let codeql: CodeqlCli;
let db: string;
let tmpDir: string;
let dbTmpDir: string;
beforeAll(
async () => {
codeql = new BaseCodeqlCli(process.env.CODEQL_BIN_PATH || "codeql");
dbTmpDir = path.resolve(fs.mkdtempSync(path.join(tmpdir(), "db-")));
const projectDir = path.join(dbTmpDir, "project");
const dbDir = path.join(dbTmpDir, "db");
fs.mkdirSync(projectDir);
const testFile = path.join(projectDir, "test.js");
fs.writeFileSync(testFile, "const x = 1;");
await codeql.run([
"database",
"create",
"--language=javascript",
`--source-root=${projectDir}`,
dbDir,
]);
db = path.join(dbTmpDir, "database.zip");
await codeql.run(["database", "bundle", `--output=${db}`, dbDir]);
},
// 5 minute timeout to build a CodeQL database
5 * 60 * 1000,
);
afterAll(
async () => {
if (dbTmpDir) {
await rmRF(dbTmpDir);
}
},
// 30 second timeout to delete an unzipped CodeQL database
30 * 1000,
);
beforeEach(() => {
// Use a different temporary directory that tests can use
tmpDir = path.resolve(fs.mkdtempSync(path.join(tmpdir(), "tmp-")));
});
afterEach(async () => {
if (tmpDir !== undefined) {
await rmRF(tmpDir);
}
});
describe("getQueryPackInfo", () => {
it("gets query pack info", async () => {
const queryPackInfo = await getQueryPackInfo(
codeql,
join(cwd, "testdata/test_pack"),
);
const queries = {};
queries[path.resolve("testdata/test_pack/x/query.ql")] = {
name: "Test query",
description: "Test query description",
kind: "table",
id: "test/query/id",
};
expect(queryPackInfo).toEqual({
path: path.resolve("testdata/test_pack"),
name: "codeql/queries",
queries,
});
});
it("gets query pack info when there are multiple queries", async () => {
const queryPackInfo = await getQueryPackInfo(
codeql,
join(cwd, "testdata/test_pack_multiple_queries"),
);
const queries = {};
queries[path.resolve("testdata/test_pack_multiple_queries/x/query.ql")] =
{
name: "Test query 1",
kind: "table",
id: "test/query/one",
};
queries[path.resolve("testdata/test_pack_multiple_queries/z/query.ql")] =
{
name: "Test query 2",
kind: "table",
id: "test/query/two",
};
expect(queryPackInfo).toEqual({
path: path.resolve("testdata/test_pack_multiple_queries"),
name: "codeql/queries",
queries,
});
});
});
describe("runQuery", () => {
beforeEach(() => {
// Change to the temporary directory because some tests write files to the current working directory
process.chdir(tmpDir);
});
afterEach(() => {
process.chdir(cwd);
});
it(
"runs a query in a pack",
async () => {
const queryPack = await getQueryPackInfo(
codeql,
join(cwd, "testdata/test_pack"),
);
await runQuery(codeql, db, "a/b", queryPack);
expect(fs.existsSync(path.join("results", "results.bqrs"))).toBe(true);
expect(
fs.existsSync(path.join("results", "codeql/queries/x/query.bqrs")),
).toBe(false);
const bqrsInfo: BQRSInfo = await getBqrsInfo(
codeql,
path.join("results", "results.bqrs"),
);
expect(bqrsInfo.resultSets.length).toBe(1);
expect(bqrsInfo.resultSets[0].name).toBe("#select");
expect(bqrsInfo.compatibleQueryKinds.includes("Table")).toBe(true);
},
// 1 minute timeout to run a CodeQL query
60 * 1000,
);
it(
"runs multiple queries in a pack",
async () => {
const queryPack = await getQueryPackInfo(
codeql,
join(cwd, "testdata/test_pack_multiple_queries"),
);
await runQuery(codeql, db, "a/b", queryPack);
const bqrsFilePath1 = "db/results/codeql/queries/x/query.bqrs";
expect(fs.existsSync(bqrsFilePath1)).toBe(true);
const bqrsInfo1 = await getBqrsInfo(codeql, bqrsFilePath1);
expect(bqrsInfo1.resultSets.length).toBe(1);
expect(bqrsInfo1.resultSets[0].name).toBe("#select");
expect(bqrsInfo1.compatibleQueryKinds.includes("Table")).toBe(true);
const bqrsFilePath2 = "db/results/codeql/queries/z/query.bqrs";
expect(fs.existsSync(bqrsFilePath2)).toBe(true);
const bqrsInfo2 = await getBqrsInfo(codeql, bqrsFilePath2);
expect(bqrsInfo2.resultSets.length).toBe(1);
expect(bqrsInfo2.resultSets[0].name).toBe("#select");
expect(bqrsInfo2.compatibleQueryKinds.includes("Table")).toBe(true);
expect(fs.existsSync(path.join("results", "results.bqrs"))).toBe(false);
},
// 1 minute timeout to run a CodeQL query
60 * 1000,
);
});
describe("getDatabaseMetadata", () => {
it("gets the commit SHA and CLI version from a database", () => {
fs.writeFileSync(
path.join(tmpDir, "codeql-database.yml"),
`---
sourceLocationPrefix: "hello-world"
baselineLinesOfCode: 1
unicodeNewlines: true
columnKind: "utf16"
primaryLanguage: "javascript"
creationMetadata:
sha: "ccf1e13626d97b009b4da78f719f028d9f7cdf80"
cliVersion: "2.7.2"
creationTime: "2021-11-08T12:58:40.345998Z"
`,
);
expect(getDatabaseMetadata(tmpDir).creationMetadata?.sha).toBe(
"ccf1e13626d97b009b4da78f719f028d9f7cdf80",
);
expect(getDatabaseMetadata(tmpDir).creationMetadata?.cliVersion).toBe(
"2.7.2",
);
});
it("gets the commit SHA when codeql-database.yml exists, but does not contain SHA", () => {
fs.writeFileSync(
path.join(tmpDir, "codeql-database.yml"),
`---
sourceLocationPrefix: "hello-world"
baselineLinesOfCode: 17442
unicodeNewlines: true
columnKind: "utf16"
primaryLanguage: "javascript"
`,
);
expect(getDatabaseMetadata(tmpDir).creationMetadata?.sha).toBe(undefined);
});
it("gets the commit SHA when codeql-database.yml exists, but is invalid", () => {
fs.writeFileSync(
path.join(tmpDir, "codeql-database.yml"),
` foo:"
bar
`,
);
expect(getDatabaseMetadata(tmpDir).creationMetadata?.sha).toBe(undefined);
});
it("gets the commit SHA when the codeql-database.yml does not exist", () => {
expect(getDatabaseMetadata(tmpDir).creationMetadata?.sha).toBe(undefined);
});
});
describe("getQueryPackQueries", () => {
it("gets the queries from a pack", async () => {
expect(
await getQueryPackQueries(
codeql,
"testdata/test_pack",
"codeql/queries",
),
).toEqual([path.resolve("testdata/test_pack/x/query.ql")]);
});
});
describe("injectVersionControlInfo", () => {
it("populates the SARIF versionControlProvenance property", () => {
const sarif: Sarif = {
runs: [
{
results: [],
},
],
};
const nwo = "a/b";
const sha = "testsha123";
injectVersionControlInfo(sarif, nwo, sha);
const expected = {
repositoryUri: `https://github.com/${nwo}`,
revisionId: sha,
};
expect(sarif.runs[0].versionControlProvenance?.[0]).toEqual(expected);
});
});
describe("getSarifResultCount", () => {
it("counts the number of results in a SARIF file)", () => {
const sarif: Sarif = {
runs: [
{
results: [
{
ruleId: "test-rule1",
},
{
ruleId: "test-rule2",
},
{
ruleId: "test-rule3",
},
],
},
],
};
expect(getSarifResultCount(sarif)).toBe(3);
});
});
describe("getSarifOutputType", () => {
it("gets the SARIF output type when there is no `@kind` metadata", () => {
const queryMetadata: QueryMetadata = {};
const compatibleQueryKinds = [
"Problem",
"PathProblem",
"Table",
"Diagnostic",
];
expect(getSarifOutputType(queryMetadata, compatibleQueryKinds)).toBe(
undefined,
);
});
it("gets the SARIF output type when the `@kind` metadata is not compatible with output", () => {
const queryMetadata: QueryMetadata = {
kind: "path-problem",
};
const compatibleQueryKinds = ["Problem", "Table", "Diagnostic"];
expect(getSarifOutputType(queryMetadata, compatibleQueryKinds)).toBe(
undefined,
);
});
it("gets the SARIF output type when the `@kind` metadata is compatible with output", () => {
const queryMetadata: QueryMetadata = {
kind: "problem",
};
const compatibleQueryKinds = [
"Problem",
"PathProblem",
"Table",
"Diagnostic",
];
expect(getSarifOutputType(queryMetadata, compatibleQueryKinds)).toBe(
"problem",
);
});
it("gets the SARIF output type when the `@kind` metadata is an alert alias", () => {
const queryMetadata: QueryMetadata = {
kind: "alert",
};
const compatibleQueryKinds = [
"Problem",
"PathProblem",
"Table",
"Diagnostic",
];
expect(getSarifOutputType(queryMetadata, compatibleQueryKinds)).toBe(
"problem",
);
});
it("gets the SARIF output type when the `@kind` metadata is a path-alert alias", () => {
const queryMetadata: QueryMetadata = {
kind: "path-alert",
};
const compatibleQueryKinds = [
"Problem",
"PathProblem",
"Table",
"Diagnostic",
];
expect(getSarifOutputType(queryMetadata, compatibleQueryKinds)).toBe(
"path-problem",
);
});
});
describe("getBqrsResultCount", () => {
it("uses result count from #select result set if it exists", () => {
const bqrsInfo: BQRSInfo = {
resultSets: [{ name: "#select", rows: 3 }],
compatibleQueryKinds: [],
};
expect(getBqrsResultCount(bqrsInfo)).toBe(3);
});
it("uses result count from problems result set if it exists", () => {
const bqrsInfo: BQRSInfo = {
resultSets: [{ name: "problems", rows: 4 }],
compatibleQueryKinds: [],
};
expect(getBqrsResultCount(bqrsInfo)).toBe(4);
});
it("uses result count from #select result set if both #select and problems result sets exist", () => {
const bqrsInfo: BQRSInfo = {
resultSets: [
{ name: "#select", rows: 3 },
{ name: "problems", rows: 4 },
],
compatibleQueryKinds: [],
};
expect(getBqrsResultCount(bqrsInfo)).toBe(3);
});
it("throws error if neither #select or problems result sets exist", () => {
const bqrsInfo: BQRSInfo = {
resultSets: [
{ name: "something", rows: 13 },
{ name: "unknown", rows: 42 },
],
compatibleQueryKinds: [],
};
expect(() => getBqrsResultCount(bqrsInfo)).toThrow(
new Error(
"BQRS does not contain any result sets matching known names. Expected one of #select or problems but found something, unknown",
),
);
});
});
});