|
| 1 | +/** |
| 2 | + * Tests for `check-sarif.ts`. |
| 3 | + */ |
| 4 | + |
| 5 | +import * as assert from "node:assert/strict"; |
| 6 | +import { describe, it } from "node:test"; |
| 7 | + |
| 8 | +import type { Log } from "sarif"; |
| 9 | + |
| 10 | +import { checkSarif } from "./check-sarif"; |
| 11 | + |
| 12 | +/** Builds a minimal SARIF Log with the given rule IDs spread across extensions. */ |
| 13 | +function buildSarifLog(ruleIds: string[]): Log { |
| 14 | + return { |
| 15 | + version: "2.1.0", |
| 16 | + $schema: |
| 17 | + "https://raw.githubusercontent.com/oasis-tcs/sarif-spec/main/sarif-2.1/schema/sarif-schema-2.1.0.json", |
| 18 | + runs: [ |
| 19 | + { |
| 20 | + tool: { |
| 21 | + driver: { name: "CodeQL" }, |
| 22 | + extensions: [ |
| 23 | + { |
| 24 | + name: "test-pack", |
| 25 | + rules: ruleIds.map((id) => ({ id })), |
| 26 | + }, |
| 27 | + ], |
| 28 | + }, |
| 29 | + results: [], |
| 30 | + }, |
| 31 | + ], |
| 32 | + }; |
| 33 | +} |
| 34 | + |
| 35 | +describe("checkSarif", async () => { |
| 36 | + await it("returns 0 when all expected queries ran and no unexpected queries ran", () => { |
| 37 | + const sarif = buildSarifLog(["js/sql-injection", "js/xss"]); |
| 38 | + const exitCode = checkSarif(sarif, { |
| 39 | + sarifFile: "test.sarif", |
| 40 | + queriesRun: "js/sql-injection, js/xss", |
| 41 | + queriesNotRun: "js/hardcoded-credentials", |
| 42 | + }); |
| 43 | + assert.equal(exitCode, 0); |
| 44 | + }); |
| 45 | + |
| 46 | + await it("returns -2 when an expected query did not run", () => { |
| 47 | + const sarif = buildSarifLog(["js/sql-injection"]); |
| 48 | + const exitCode = checkSarif(sarif, { |
| 49 | + sarifFile: "test.sarif", |
| 50 | + queriesRun: "js/sql-injection, js/xss", |
| 51 | + queriesNotRun: "", |
| 52 | + }); |
| 53 | + assert.equal(exitCode, -2); |
| 54 | + }); |
| 55 | + |
| 56 | + await it("returns -2 when an unexpected query ran", () => { |
| 57 | + const sarif = buildSarifLog(["js/sql-injection", "js/xss"]); |
| 58 | + const exitCode = checkSarif(sarif, { |
| 59 | + sarifFile: "test.sarif", |
| 60 | + queriesRun: "js/sql-injection", |
| 61 | + queriesNotRun: "js/xss", |
| 62 | + }); |
| 63 | + assert.equal(exitCode, -2); |
| 64 | + }); |
| 65 | + |
| 66 | + await it("handles empty queries-run and queries-not-run inputs", () => { |
| 67 | + const sarif = buildSarifLog(["js/sql-injection"]); |
| 68 | + const exitCode = checkSarif(sarif, { |
| 69 | + sarifFile: "test.sarif", |
| 70 | + queriesRun: "", |
| 71 | + queriesNotRun: "", |
| 72 | + }); |
| 73 | + assert.equal(exitCode, 0); |
| 74 | + }); |
| 75 | + |
| 76 | + await it("handles multiple extensions with rules", () => { |
| 77 | + const sarif: Log = { |
| 78 | + version: "2.1.0", |
| 79 | + $schema: |
| 80 | + "https://raw.githubusercontent.com/oasis-tcs/sarif-spec/main/sarif-2.1/schema/sarif-schema-2.1.0.json", |
| 81 | + runs: [ |
| 82 | + { |
| 83 | + tool: { |
| 84 | + driver: { name: "CodeQL" }, |
| 85 | + extensions: [ |
| 86 | + { |
| 87 | + name: "pack-a", |
| 88 | + rules: [{ id: "js/sql-injection" }], |
| 89 | + }, |
| 90 | + { |
| 91 | + name: "pack-b", |
| 92 | + rules: [{ id: "js/xss" }], |
| 93 | + }, |
| 94 | + ], |
| 95 | + }, |
| 96 | + results: [], |
| 97 | + }, |
| 98 | + ], |
| 99 | + }; |
| 100 | + const exitCode = checkSarif(sarif, { |
| 101 | + sarifFile: "test.sarif", |
| 102 | + queriesRun: "js/sql-injection, js/xss", |
| 103 | + queriesNotRun: "", |
| 104 | + }); |
| 105 | + assert.equal(exitCode, 0); |
| 106 | + }); |
| 107 | + |
| 108 | + await it("handles extensions with no rules", () => { |
| 109 | + const sarif: Log = { |
| 110 | + version: "2.1.0", |
| 111 | + $schema: |
| 112 | + "https://raw.githubusercontent.com/oasis-tcs/sarif-spec/main/sarif-2.1/schema/sarif-schema-2.1.0.json", |
| 113 | + runs: [ |
| 114 | + { |
| 115 | + tool: { |
| 116 | + driver: { name: "CodeQL" }, |
| 117 | + extensions: [ |
| 118 | + { name: "empty-pack" }, |
| 119 | + { |
| 120 | + name: "pack-with-rules", |
| 121 | + rules: [{ id: "js/xss" }], |
| 122 | + }, |
| 123 | + ], |
| 124 | + }, |
| 125 | + results: [], |
| 126 | + }, |
| 127 | + ], |
| 128 | + }; |
| 129 | + const exitCode = checkSarif(sarif, { |
| 130 | + sarifFile: "test.sarif", |
| 131 | + queriesRun: "js/xss", |
| 132 | + queriesNotRun: "js/sql-injection", |
| 133 | + }); |
| 134 | + assert.equal(exitCode, 0); |
| 135 | + }); |
| 136 | + |
| 137 | + await it("throws when tool extensions are undefined", () => { |
| 138 | + const sarif: Log = { |
| 139 | + version: "2.1.0", |
| 140 | + $schema: |
| 141 | + "https://raw.githubusercontent.com/oasis-tcs/sarif-spec/main/sarif-2.1/schema/sarif-schema-2.1.0.json", |
| 142 | + runs: [ |
| 143 | + { |
| 144 | + tool: { driver: { name: "CodeQL" } }, |
| 145 | + results: [], |
| 146 | + }, |
| 147 | + ], |
| 148 | + }; |
| 149 | + assert.throws( |
| 150 | + () => |
| 151 | + checkSarif(sarif, { |
| 152 | + sarifFile: "test.sarif", |
| 153 | + queriesRun: "js/xss", |
| 154 | + queriesNotRun: "", |
| 155 | + }), |
| 156 | + { message: /Couldn't find tool extensions/ }, |
| 157 | + ); |
| 158 | + }); |
| 159 | +}); |
0 commit comments