Skip to content

Commit

Permalink
refactor(estree-ast-utils/test): migrate to test_runner
Browse files Browse the repository at this point in the history
  • Loading branch information
FredGuiou committed Mar 9, 2024
1 parent 9e4ad1f commit 7f25cd0
Show file tree
Hide file tree
Showing 13 changed files with 223 additions and 282 deletions.
2 changes: 1 addition & 1 deletion workspaces/estree-ast-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"scripts": {
"lint": "eslint src test",
"prepublishOnly": "pkg-ok",
"test": "cross-env esm-tape-runner 'test/**/*.spec.js' | tap-monkey",
"test": "node --test",
"check": "cross-env npm run lint && npm run test",
"coverage": "c8 -r html npm test"
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
// Import Third-party Dependencies
import test from "tape";
// Import Node.js Dependencies
import { test } from "node:test";
import assert from "node:assert";

// Import Internal Dependencies
import { createTracer } from "../utils.js";

test("getDataFromIdentifier must return primitive null is there is no kwown traced identifier", (tape) => {
test("getDataFromIdentifier must return primitive null is there is no kwown traced identifier", () => {
const helpers = createTracer(true);

const result = helpers.tracer.getDataFromIdentifier("foobar");

tape.strictEqual(result, null);
tape.end();
assert.strictEqual(result, null);
});

test("it should be able to Trace a malicious code with Global, BinaryExpr, Assignments and Hexadecimal", (tape) => {
test("it should be able to Trace a malicious code with Global, BinaryExpr, Assignments and Hexadecimal", () => {
const helpers = createTracer(true);
const assignments = helpers.getAssignmentArray();

Expand All @@ -27,24 +27,22 @@ test("it should be able to Trace a malicious code with Global, BinaryExpr, Assig
`);

const evil = helpers.tracer.getDataFromIdentifier("evil");
tape.deepEqual(evil, {
assert.deepEqual(evil, {
name: "require",
identifierOrMemberExpr: "process.mainModule.require",
assignmentMemory: ["p", "evil"]
});
tape.strictEqual(assignments.length, 2);
assert.strictEqual(assignments.length, 2);

const [eventOne, eventTwo] = assignments;
tape.strictEqual(eventOne.identifierOrMemberExpr, "process");
tape.strictEqual(eventOne.id, "p");
assert.strictEqual(eventOne.identifierOrMemberExpr, "process");
assert.strictEqual(eventOne.id, "p");

tape.strictEqual(eventTwo.identifierOrMemberExpr, "process.mainModule.require");
tape.strictEqual(eventTwo.id, "evil");

tape.end();
assert.strictEqual(eventTwo.identifierOrMemberExpr, "process.mainModule.require");
assert.strictEqual(eventTwo.id, "evil");
});

test("it should be able to Trace a malicious CallExpression by recombining segments of the MemberExpression", (tape) => {
test("it should be able to Trace a malicious CallExpression by recombining segments of the MemberExpression", () => {
const helpers = createTracer(true);
const assignments = helpers.getAssignmentArray();

Expand All @@ -57,36 +55,32 @@ test("it should be able to Trace a malicious CallExpression by recombining segme
`);

const evil = helpers.tracer.getDataFromIdentifier("r.require");
tape.deepEqual(evil, {
assert.deepEqual(evil, {
name: "require",
identifierOrMemberExpr: "process.mainModule.require",
assignmentMemory: ["g", "r", "c"]
});
tape.strictEqual(assignments.length, 3);
assert.strictEqual(assignments.length, 3);

const [eventOne, eventTwo, eventThree] = assignments;
tape.strictEqual(eventOne.identifierOrMemberExpr, "process");
tape.strictEqual(eventOne.id, "g");

tape.strictEqual(eventTwo.identifierOrMemberExpr, "process.mainModule");
tape.strictEqual(eventTwo.id, "r");
assert.strictEqual(eventOne.identifierOrMemberExpr, "process");
assert.strictEqual(eventOne.id, "g");

tape.strictEqual(eventThree.identifierOrMemberExpr, "process.mainModule.require");
tape.strictEqual(eventThree.id, "c");
assert.strictEqual(eventTwo.identifierOrMemberExpr, "process.mainModule");
assert.strictEqual(eventTwo.id, "r");

tape.end();
assert.strictEqual(eventThree.identifierOrMemberExpr, "process.mainModule.require");
assert.strictEqual(eventThree.id, "c");
});

test("given a MemberExpression segment that doesn't match anything then it should return null", (tape) => {
test("given a MemberExpression segment that doesn't match anything then it should return null", () => {
const helpers = createTracer(true);

const result = helpers.tracer.getDataFromIdentifier("foo.bar");
tape.strictEqual(result, null);

tape.end();
assert.strictEqual(result, null);
});

test("it should be able to Trace a require using Function.prototype.call", (tape) => {
test("it should be able to Trace a require using Function.prototype.call", () => {
const helpers = createTracer();
helpers.tracer.trace("http");
const assignments = helpers.getAssignmentArray();
Expand All @@ -97,17 +91,15 @@ test("it should be able to Trace a require using Function.prototype.call", (tape

const proto = helpers.tracer.getDataFromIdentifier("proto");

tape.strictEqual(proto, null);
tape.strictEqual(assignments.length, 1);
assert.strictEqual(proto, null);
assert.strictEqual(assignments.length, 1);

const [eventOne] = assignments;
tape.strictEqual(eventOne.identifierOrMemberExpr, "http");
tape.strictEqual(eventOne.id, "proto");

tape.end();
assert.strictEqual(eventOne.identifierOrMemberExpr, "http");
assert.strictEqual(eventOne.id, "proto");
});

test("it should be able to Trace an unsafe crypto.createHash using Function.prototype.call reassignment", (tape) => {
test("it should be able to Trace an unsafe crypto.createHash using Function.prototype.call reassignment", () => {
const helpers = createTracer(true);
helpers.tracer.trace("crypto.createHash", { followConsecutiveAssignment: true });
const assignments = helpers.getAssignmentArray();
Expand All @@ -122,24 +114,22 @@ test("it should be able to Trace an unsafe crypto.createHash using Function.prot
`);

const createHashBis = helpers.tracer.getDataFromIdentifier("createHashBis");
tape.deepEqual(createHashBis, {
assert.deepEqual(createHashBis, {
name: "crypto.createHash",
identifierOrMemberExpr: "crypto.createHash",
assignmentMemory: ["crr", "createHashBis"]
});

tape.strictEqual(helpers.tracer.importedModules.has("crypto"), true);
tape.strictEqual(assignments.length, 3);
assert.strictEqual(helpers.tracer.importedModules.has("crypto"), true);
assert.strictEqual(assignments.length, 3);

const [eventOne, eventTwo, eventThree] = assignments;
tape.strictEqual(eventOne.identifierOrMemberExpr, "require");
tape.strictEqual(eventOne.id, "bB");

tape.strictEqual(eventTwo.identifierOrMemberExpr, "crypto");
tape.strictEqual(eventTwo.id, "crr");
assert.strictEqual(eventOne.identifierOrMemberExpr, "require");
assert.strictEqual(eventOne.id, "bB");

tape.strictEqual(eventThree.identifierOrMemberExpr, "crypto.createHash");
tape.strictEqual(eventThree.id, "createHashBis");
assert.strictEqual(eventTwo.identifierOrMemberExpr, "crypto");
assert.strictEqual(eventTwo.id, "crr");

tape.end();
assert.strictEqual(eventThree.identifierOrMemberExpr, "crypto.createHash");
assert.strictEqual(eventThree.id, "createHashBis");
});
87 changes: 38 additions & 49 deletions workspaces/estree-ast-utils/test/VariableTracer/assignments.spec.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// Import Third-party Dependencies
import test from "tape";
// Import Node.js Dependencies
import { test } from "node:test";
import assert from "node:assert";

// Import Internal Dependencies
import { createTracer } from "../utils.js";

test("it should be able to Trace a require Assignment (using a global variable)", (tape) => {
test("it should be able to Trace a require Assignment (using a global variable)", () => {
const helpers = createTracer(true);
const assignments = helpers.getAssignmentArray();

Expand All @@ -15,21 +16,19 @@ test("it should be able to Trace a require Assignment (using a global variable)"
`);

const foo = helpers.tracer.getDataFromIdentifier("foo");
tape.deepEqual(foo, {
assert.deepEqual(foo, {
name: "require",
identifierOrMemberExpr: "require",
assignmentMemory: ["foo"]
});
tape.strictEqual(assignments.length, 1);
assert.strictEqual(assignments.length, 1);

const [eventOne] = assignments;
tape.strictEqual(eventOne.identifierOrMemberExpr, "require");
tape.strictEqual(eventOne.id, "foo");

tape.end();
assert.strictEqual(eventOne.identifierOrMemberExpr, "require");
assert.strictEqual(eventOne.id, "foo");
});

test("it should be able to Trace a require Assignment (using a MemberExpression)", (tape) => {
test("it should be able to Trace a require Assignment (using a MemberExpression)", () => {
const helpers = createTracer(true);
const assignments = helpers.getAssignmentArray();

Expand All @@ -39,21 +38,19 @@ test("it should be able to Trace a require Assignment (using a MemberExpression)
`);

const foo = helpers.tracer.getDataFromIdentifier("foo");
tape.deepEqual(foo, {
assert.deepEqual(foo, {
name: "require",
identifierOrMemberExpr: "require.resolve",
assignmentMemory: ["foo"]
});
tape.strictEqual(assignments.length, 1);
assert.strictEqual(assignments.length, 1);

const [eventOne] = assignments;
tape.strictEqual(eventOne.identifierOrMemberExpr, "require.resolve");
tape.strictEqual(eventOne.id, "foo");

tape.end();
assert.strictEqual(eventOne.identifierOrMemberExpr, "require.resolve");
assert.strictEqual(eventOne.id, "foo");
});

test("it should be able to Trace a global Assignment using an ESTree ObjectPattern", (tape) => {
test("it should be able to Trace a global Assignment using an ESTree ObjectPattern", () => {
const helpers = createTracer(true);
const assignments = helpers.getAssignmentArray();

Expand All @@ -65,24 +62,22 @@ test("it should be able to Trace a global Assignment using an ESTree ObjectPatte

const boo = helpers.tracer.getDataFromIdentifier("boo");

tape.deepEqual(boo, {
assert.deepEqual(boo, {
name: "require",
identifierOrMemberExpr: "process.mainModule.require",
assignmentMemory: ["yoo", "boo"]
});
tape.strictEqual(assignments.length, 2);
assert.strictEqual(assignments.length, 2);

const [eventOne, eventTwo] = assignments;
tape.strictEqual(eventOne.identifierOrMemberExpr, "process");
tape.strictEqual(eventOne.id, "yoo");

tape.strictEqual(eventTwo.identifierOrMemberExpr, "process.mainModule.require");
tape.strictEqual(eventTwo.id, "boo");
assert.strictEqual(eventOne.identifierOrMemberExpr, "process");
assert.strictEqual(eventOne.id, "yoo");

tape.end();
assert.strictEqual(eventTwo.identifierOrMemberExpr, "process.mainModule.require");
assert.strictEqual(eventTwo.id, "boo");
});

test("it should be able to Trace an Unsafe Function() Assignment using an ESTree ObjectPattern", (tape) => {
test("it should be able to Trace an Unsafe Function() Assignment using an ESTree ObjectPattern", () => {
const helpers = createTracer(true);
const assignments = helpers.getAssignmentArray();

Expand All @@ -94,24 +89,22 @@ test("it should be able to Trace an Unsafe Function() Assignment using an ESTree

const boo = helpers.tracer.getDataFromIdentifier("boo");

tape.deepEqual(boo, {
assert.deepEqual(boo, {
name: "require",
identifierOrMemberExpr: "process.mainModule.require",
assignmentMemory: ["yoo", "boo"]
});
tape.strictEqual(assignments.length, 2);
assert.strictEqual(assignments.length, 2);

const [eventOne, eventTwo] = assignments;
tape.strictEqual(eventOne.identifierOrMemberExpr, "process");
tape.strictEqual(eventOne.id, "yoo");
assert.strictEqual(eventOne.identifierOrMemberExpr, "process");
assert.strictEqual(eventOne.id, "yoo");

tape.strictEqual(eventTwo.identifierOrMemberExpr, "process.mainModule.require");
tape.strictEqual(eventTwo.id, "boo");

tape.end();
assert.strictEqual(eventTwo.identifierOrMemberExpr, "process.mainModule.require");
assert.strictEqual(eventTwo.id, "boo");
});

test("it should be able to Trace a require Assignment with atob", (tape) => {
test("it should be able to Trace a require Assignment with atob", () => {
const helpers = createTracer(true);
const assignments = helpers.getAssignmentArray();

Expand All @@ -120,19 +113,17 @@ test("it should be able to Trace a require Assignment with atob", (tape) => {
const yo = 'b3M=';
const ff = xo(yo);
`);
tape.strictEqual(assignments.length, 1);
assert.strictEqual(assignments.length, 1);

const [eventOne] = assignments;
tape.strictEqual(eventOne.identifierOrMemberExpr, "atob");
tape.strictEqual(eventOne.id, "xo");

tape.true(helpers.tracer.literalIdentifiers.has("ff"));
tape.strictEqual(helpers.tracer.literalIdentifiers.get("ff"), "os");
assert.strictEqual(eventOne.identifierOrMemberExpr, "atob");
assert.strictEqual(eventOne.id, "xo");

tape.end();
assert.ok(helpers.tracer.literalIdentifiers.has("ff"));
assert.strictEqual(helpers.tracer.literalIdentifiers.get("ff"), "os");
});

test("it should be able to Trace a global assignment using a LogicalExpression", (tape) => {
test("it should be able to Trace a global assignment using a LogicalExpression", () => {
const helpers = createTracer(true);
const assignments = helpers.getAssignmentArray();

Expand All @@ -142,16 +133,14 @@ test("it should be able to Trace a global assignment using a LogicalExpression",
foo("http");
`);
const foo = helpers.tracer.getDataFromIdentifier("foo");
tape.deepEqual(foo, {
assert.deepEqual(foo, {
name: "require",
identifierOrMemberExpr: "require",
assignmentMemory: ["foo"]
});
tape.strictEqual(assignments.length, 1);
assert.strictEqual(assignments.length, 1);

const [eventOne] = assignments;
tape.strictEqual(eventOne.identifierOrMemberExpr, "require");
tape.strictEqual(eventOne.id, "foo");

tape.end();
assert.strictEqual(eventOne.identifierOrMemberExpr, "require");
assert.strictEqual(eventOne.id, "foo");
});
Loading

0 comments on commit 7f25cd0

Please sign in to comment.