Skip to content

Remove mockedFunction for Node.js test runner mock method #201

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 14, 2024
Merged
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
53 changes: 0 additions & 53 deletions test/probes/isArrayExpression.js

This file was deleted.

56 changes: 56 additions & 0 deletions test/probes/isArrayExpression.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Import Node.js dependencies
import { test } from "node:test";
import assert from "node:assert";

// Import Internal Dependencies
import { getSastAnalysis, parseScript } from "../utils/index.js";
import isArrayExpression from "../../src/probes/isArrayExpression.js";

test("it should trigger analyzeLiteral method one time", (t) => {
const str = "['foo']";

const ast = parseScript(str);
const sastAnalysis = getSastAnalysis(str, isArrayExpression);

t.mock.method(sastAnalysis.analysis, "analyzeLiteral");
sastAnalysis.execute(ast.body);

assert.strictEqual(sastAnalysis.warnings().length, 0);

const calls = sastAnalysis.analysis.analyzeLiteral.mock.calls;
assert.strictEqual(calls.length, 1);

const literalNode = calls[0].arguments[0];
assert.strictEqual(literalNode.value, "foo");
});

test("it should trigger analyzeLiteral method two times (ignoring the holey between)", (t) => {
const str = "[5, ,10]";

const ast = parseScript(str);
const sastAnalysis = getSastAnalysis(str, isArrayExpression);

t.mock.method(sastAnalysis.analysis, "analyzeLiteral");
sastAnalysis.execute(ast.body);

const calls = sastAnalysis.analysis.analyzeLiteral.mock.calls;
assert.strictEqual(calls.length, 2);
assert.strictEqual(calls[0].arguments[0].value, 5);
assert.strictEqual(calls[1].arguments[0].value, 10);
});

test("it should trigger analyzeLiteral one time (ignoring non-literal Node)", (t) => {
const str = "[5, () => void 0]";

const ast = parseScript(str);
const sastAnalysis = getSastAnalysis(str, isArrayExpression);

t.mock.method(sastAnalysis.analysis, "analyzeLiteral");
sastAnalysis.execute(ast.body);

const calls = sastAnalysis.analysis.analyzeLiteral.mock.calls;
assert.strictEqual(calls.length, 1);

const literalNode = calls[0].arguments[0];
assert.strictEqual(literalNode.value, 5);
});
33 changes: 17 additions & 16 deletions test/probes/isLiteral.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,43 @@ import { test } from "node:test";
import assert from "node:assert";

// Import Internal Dependencies
import { getSastAnalysis, parseScript, mockedFunction } from "../utils/index.js";
import { getSastAnalysis, parseScript } from "../utils/index.js";
import isLiteral from "../../src/probes/isLiteral.js";

test("should throw an unsafe-import because the hexadecimal string is equal to the core 'http' dependency", () => {
test("should throw an unsafe-import because the hexadecimal string is equal to the core 'http' dependency", (t) => {
const str = "const foo = '68747470'";
const ast = parseScript(str);

const analyzeStringMock = mockedFunction();
const sastAnalysis = getSastAnalysis(str, isLiteral);
sastAnalysis.analysis.analyzeString = analyzeStringMock.callback.bind(analyzeStringMock);
t.mock.method(sastAnalysis.analysis, "analyzeString");
sastAnalysis.execute(ast.body);

assert.strictEqual(sastAnalysis.warnings().length, 1);
const warning = sastAnalysis.getWarning("unsafe-import");
assert.strictEqual(warning.kind, "unsafe-import");

assert.ok(sastAnalysis.dependencies().has("http"));
assert.ok(analyzeStringMock.haveBeenCalledTimes(1));
assert.ok(analyzeStringMock.haveBeenCalledWith("http"));
const calls = sastAnalysis.analysis.analyzeString.mock.calls;
assert.strictEqual(calls.length, 1);
assert.ok(calls[0].arguments.includes("http"));
});

test("should throw an encoded-literal warning because the hexadecimal value is equal to 'require'", () => {

test("should throw an encoded-literal warning because the hexadecimal value is equal to 'require'", (t) => {
const str = "const _t = globalThis['72657175697265']";
const ast = parseScript(str);

const analyzeStringMock = mockedFunction();
const sastAnalysis = getSastAnalysis(str, isLiteral);
sastAnalysis.analysis.analyzeString = analyzeStringMock.callback.bind(analyzeStringMock);
t.mock.method(sastAnalysis.analysis, "analyzeString");
sastAnalysis.execute(ast.body);

assert.strictEqual(sastAnalysis.warnings().length, 1);
const warning = sastAnalysis.getWarning("encoded-literal");
assert.strictEqual(warning.value, "72657175697265");

assert.ok(analyzeStringMock.haveBeenCalledTimes(1));
assert.ok(analyzeStringMock.haveBeenCalledWith("require"));
const calls = sastAnalysis.analysis.analyzeString.mock.calls;
assert.strictEqual(calls.length, 1);
assert.ok(calls[0].arguments.includes("require"));
});

test("should not throw an encoded-literal warning because hexadecimal value is safe", () => {
Expand All @@ -62,19 +63,19 @@ test("should throw an encoded-literal warning because hexadecimal value is not s
assert.strictEqual(warning.value, "68656c6c6f20776f726c64");
});

test("should not throw any warnings without hexadecimal value (and should call analyzeLiteral of Analysis class)", () => {
test("should not throw any warnings without hexadecimal value (and should call analyzeLiteral of Analysis class)", (t) => {
const str = "const foo = 'hello world!'";
const ast = parseScript(str);

const analyzeLiteralMock = mockedFunction();
const sastAnalysis = getSastAnalysis(str, isLiteral);
sastAnalysis.analysis.analyzeLiteral = analyzeLiteralMock.callback.bind(analyzeLiteralMock);
t.mock.method(sastAnalysis.analysis, "analyzeLiteral");
sastAnalysis.execute(ast.body);

assert.strictEqual(sastAnalysis.warnings().length, 0);
assert.ok(analyzeLiteralMock.haveBeenCalledTimes(1));
const calls = sastAnalysis.analysis.analyzeLiteral.mock.calls;
assert.strictEqual(calls.length, 1);

const astNode = analyzeLiteralMock.args[0];
const astNode = calls[0].arguments[0];
assert.strictEqual(astNode.value, "hello world!");
});

Expand Down
20 changes: 0 additions & 20 deletions test/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,6 @@ export function getWarningKind(warnings) {
return warnings.slice().map((warn) => warn.kind).sort();
}

export function mockedFunction() {
return {
called: 0,
args: [],
at(position) {
return this.args[position];
},
haveBeenCalledTimes(count = 0) {
return this.called === count;
},
haveBeenCalledWith(value) {
return this.args.includes(value);
},
callback(...args) {
this.args.push(...args);
this.called++;
}
};
}

export function parseScript(str) {
return meriyah.parseScript(str, {
next: true,
Expand Down