Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion tests/report/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
add_oscap_test("report.sh")
add_oscap_test("report.sh")

find_program(NODE_EXECUTABLE NAMES node nodejs)
if(NODE_EXECUTABLE)
add_oscap_ctest("report/sort_groups"
COMMAND ${NODE_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/sort_groups.js
LABELS "javascript"
)
else()
message(STATUS "Node.js not found; skipping report/sort_groups test")
endif()
77 changes: 77 additions & 0 deletions tests/report/sort_groups.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
const assert = require("assert");

Check warning on line 1 in tests/report/sort_groups.js

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Prefer `node:assert` over `assert`.

See more on https://sonarcloud.io/project/issues?id=OpenSCAP_openscap&issues=AaGXGuyK7JDpWu2qt1N_&open=AaGXGuyK7JDpWu2qt1N_&pullRequest=2479
const fs = require("fs");

Check warning on line 2 in tests/report/sort_groups.js

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Prefer `node:fs` over `fs`.

See more on https://sonarcloud.io/project/issues?id=OpenSCAP_openscap&issues=AaGXGuyK7JDpWu2qt1OA&open=AaGXGuyK7JDpWu2qt1OA&pullRequest=2479
const path = require("path");

Check warning on line 3 in tests/report/sort_groups.js

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Prefer `node:path` over `path`.

See more on https://sonarcloud.io/project/issues?id=OpenSCAP_openscap&issues=AaGXGuyK7JDpWu2qt1OB&open=AaGXGuyK7JDpWu2qt1OB&pullRequest=2479
const vm = require("vm");

Check warning on line 4 in tests/report/sort_groups.js

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Prefer `node:vm` over `vm`.

See more on https://sonarcloud.io/project/issues?id=OpenSCAP_openscap&issues=AaGXGuyK7JDpWu2qt1OC&open=AaGXGuyK7JDpWu2qt1OC&pullRequest=2479

const xslDir = path.resolve(__dirname, "../../xsl");
const source = fs.readFileSync(path.join(xslDir, "xccdf-resources/openscap.js"), "utf8");
const bundle = fs.readFileSync(path.join(xslDir, "xccdf-resources.xsl"), "utf8");
const start = bundle.indexOf("function openRuleDetailsDialog(");
const end = bundle.indexOf("]]></xsl:template>", start);
assert(start >= 0 && end > start, "Report JavaScript must be present in the generated bundle");

function permutations(values) {
if (values.length === 0)
return [[]];
return values.flatMap((value, index) =>
permutations(values.filter((_, other) => other !== index))
.map(rest => [value, ...rest]));
}

for (const [name, script] of [
["source", source],
["generated bundle", bundle.slice(start, end)]
]) {
// Loading the report's sorting functions does not require running DOM setup.
const context = vm.createContext({
document: {},
$: () => ({ready: () => {}})
});
vm.runInContext(script, context);

Check failure on line 30 in tests/report/sort_groups.js

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Make sure that this dynamic injection or execution of code is safe.

See more on https://sonarcloud.io/project/issues?id=OpenSCAP_openscap&issues=AaGXGuyK7JDpWu2qt1OD&open=AaGXGuyK7JDpWu2qt1OD&pullRequest=2479

function check(input, expected, key = "cis") {
assert.deepStrictEqual(
Array.from(context.sortGroups([...input], key)),
expected,
`${name}: ${key} sorting of ${JSON.stringify(input)}`
);
}

const referenceKeys = [
"cui", "nist", "anssi", "pcidss4", "nist-csf", "bsi", "ccn", "cis",
"cis-csc", "ism", "disa", "app-srg-ctr", "app-srg", "os-srg", "stigid",
"stigref_vulnerability_id", "stigref", "cjis", "hipaa", "isa-62443-2013",
"isa-62443-2009", "cobit5", "iso27001-2013", "nerc-cip", "ospp",
"pcidss", "dcid"
];
const mixed = ["1.2.1.1", "1.2.1.2", "enable_authselect", "reload_dconf_db"];

Check warning on line 47 in tests/report/sort_groups.js

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Make sure using a hardcoded IP address 1.2.1.1 is safe here.

See more on https://sonarcloud.io/project/issues?id=OpenSCAP_openscap&issues=AaGXGuyK7JDpWu2qt1OE&open=AaGXGuyK7JDpWu2qt1OE&pullRequest=2479

Check warning on line 47 in tests/report/sort_groups.js

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Make sure using a hardcoded IP address 1.2.1.2 is safe here.

See more on https://sonarcloud.io/project/issues?id=OpenSCAP_openscap&issues=AaGXGuyK7JDpWu2qt1OF&open=AaGXGuyK7JDpWu2qt1OF&pullRequest=2479
for (const key of referenceKeys) {
for (const input of permutations(mixed))
check(input, mixed, key);
}

for (const input of permutations(["AU-9(3)", "AU-12 b", "AU-12(c)"]))
check(input, ["AU-9(3)", "AU-12 b", "AU-12(c)"], "nist");
for (const input of permutations(["1", "01", "2", "10"]))
check(input, ["01", "1", "2", "10"]);

check(["1.2.10", "1.2.2", "1.2", "1.10.1", "1.2.1"],
["1.2", "1.2.1", "1.2.2", "1.2.10", "1.10.1"]);
check(["AC-2(10)", "AC-2(2)", "AC-2", "AC-1"],
["AC-1", "AC-2", "AC-2(2)", "AC-2(10)"], "nist");
check(["unknown", "1.2.1.2", "1.2.1.1"], ["1.2.1.1", "1.2.1.2", "unknown"]);

Check warning on line 62 in tests/report/sort_groups.js

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Make sure using a hardcoded IP address 1.2.1.2 is safe here.

See more on https://sonarcloud.io/project/issues?id=OpenSCAP_openscap&issues=AaGXGuyK7JDpWu2qt1OG&open=AaGXGuyK7JDpWu2qt1OG&pullRequest=2479

Check warning on line 62 in tests/report/sort_groups.js

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Make sure using a hardcoded IP address 1.2.1.1 is safe here.

See more on https://sonarcloud.io/project/issues?id=OpenSCAP_openscap&issues=AaGXGuyK7JDpWu2qt1OI&open=AaGXGuyK7JDpWu2qt1OI&pullRequest=2479

Check warning on line 62 in tests/report/sort_groups.js

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Make sure using a hardcoded IP address 1.2.1.1 is safe here.

See more on https://sonarcloud.io/project/issues?id=OpenSCAP_openscap&issues=AaGXGuyK7JDpWu2qt1OH&open=AaGXGuyK7JDpWu2qt1OH&pullRequest=2479

Check warning on line 62 in tests/report/sort_groups.js

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Make sure using a hardcoded IP address 1.2.1.2 is safe here.

See more on https://sonarcloud.io/project/issues?id=OpenSCAP_openscap&issues=AaGXGuyK7JDpWu2qt1OJ&open=AaGXGuyK7JDpWu2qt1OJ&pullRequest=2479
check([], []);
check(["1.2.1.2"], ["1.2.1.2"]);

Check warning on line 64 in tests/report/sort_groups.js

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Make sure using a hardcoded IP address 1.2.1.2 is safe here.

See more on https://sonarcloud.io/project/issues?id=OpenSCAP_openscap&issues=AaGXGuyK7JDpWu2qt1OL&open=AaGXGuyK7JDpWu2qt1OL&pullRequest=2479

Check warning on line 64 in tests/report/sort_groups.js

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Make sure using a hardcoded IP address 1.2.1.2 is safe here.

See more on https://sonarcloud.io/project/issues?id=OpenSCAP_openscap&issues=AaGXGuyK7JDpWu2qt1OK&open=AaGXGuyK7JDpWu2qt1OK&pullRequest=2479
check(["1.2.1.2", "1.2.1.2"], ["1.2.1.2", "1.2.1.2"]);

Check warning on line 65 in tests/report/sort_groups.js

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Make sure using a hardcoded IP address 1.2.1.2 is safe here.

See more on https://sonarcloud.io/project/issues?id=OpenSCAP_openscap&issues=AaGXGuyK7JDpWu2qt1ON&open=AaGXGuyK7JDpWu2qt1ON&pullRequest=2479

Check warning on line 65 in tests/report/sort_groups.js

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Make sure using a hardcoded IP address 1.2.1.2 is safe here.

See more on https://sonarcloud.io/project/issues?id=OpenSCAP_openscap&issues=AaGXGuyK7JDpWu2qt1OO&open=AaGXGuyK7JDpWu2qt1OO&pullRequest=2479

Check warning on line 65 in tests/report/sort_groups.js

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Make sure using a hardcoded IP address 1.2.1.2 is safe here.

See more on https://sonarcloud.io/project/issues?id=OpenSCAP_openscap&issues=AaGXGuyK7JDpWu2qt1OM&open=AaGXGuyK7JDpWu2qt1OM&pullRequest=2479

Check warning on line 65 in tests/report/sort_groups.js

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Make sure using a hardcoded IP address 1.2.1.2 is safe here.

See more on https://sonarcloud.io/project/issues?id=OpenSCAP_openscap&issues=AaGXGuyK7JDpWu2qt1OP&open=AaGXGuyK7JDpWu2qt1OP&pullRequest=2479
for (const input of permutations(["low", "high", "medium"]))
check(input, ["high", "medium", "low"], context.KeysEnum.SEVERITY);
for (const input of permutations(["pass", "notapplicable", "fail"]))
check(input, ["fail", "notapplicable", "pass"], context.KeysEnum.RESULT);

let resets = 0;
context.resetTreetable = () => { resets++; };
context.$ = () => { throw new Error("Default grouping must not rebuild the table"); };
context.groupRulesBy(context.KeysEnum.DEFAULT);
assert.strictEqual(resets, 1, `${name}: default grouping restores the original table`);
console.log(`${name}: report group sorting tests passed`);
}
Loading