Skip to content

Commit

Permalink
refactor(sec-literal/test): use the Node.js native test runner (#242)
Browse files Browse the repository at this point in the history
  • Loading branch information
fabnguess authored Feb 19, 2024
1 parent 3f51fed commit 425f270
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 174 deletions.
2 changes: 1 addition & 1 deletion workspaces/estree-ast-utils/LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2023 NodeSecure
Copyright (c) 2023-2024 NodeSecure

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion workspaces/sec-literal/LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2023 NodeSecure
Copyright (c) 2023-2024 NodeSecure

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion workspaces/sec-literal/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"type": "module",
"scripts": {
"lint": "eslint --ext .js",
"test-only": "cross-env esm-tape-runner 'test/*.spec.js' | tap-monkey",
"test-only": "node --test",
"test": "cross-env npm run lint && npm run test-only"
},
"repository": {
Expand Down
99 changes: 46 additions & 53 deletions workspaces/sec-literal/test/hex.spec.js
Original file line number Diff line number Diff line change
@@ -1,77 +1,70 @@
// Import Node.js Dependencies
import { randomBytes } from "node:crypto";

// Import Third-party Dependencies
import test from "tape";
import { describe, test } from "node:test";
import assert from "node:assert";

// Import Internal Dependencies
import { isHex, isSafe, CONSTANTS } from "../src/hex.js";
import { createLiteral } from "./utils/index.js";

test("isHex() of a random Hexadecimal value must return true", (tape) => {
const hexValue = randomBytes(4).toString("hex");
describe("isHex()", () => {
test("must return true for random 4 character hexadecimal values", () => {
const hexValue = randomBytes(4).toString("hex");

tape.strictEqual(isHex(hexValue), true, `Hexadecimal value '${hexValue}' must return true`);
tape.end();
});
assert.strictEqual(isHex(hexValue), true, `Hexadecimal value '${hexValue}' must return true`);
});

test("isHex() of an ESTree Literal containing a random Hexadecimal value must return true", (tape) => {
const hexValue = createLiteral(randomBytes(4).toString("hex"));
test("must return true for ESTree Literals containing random 4 character hexadecimal values", () => {
const hexValue = createLiteral(randomBytes(4).toString("hex"));

tape.strictEqual(isHex(hexValue), true, `Hexadecimal value '${hexValue.value}' must return true`);
tape.end();
});
assert.strictEqual(isHex(hexValue), true, `Hexadecimal value '${hexValue.value}' must return true`);
});

test("An hexadecimal value must be at least 4 chars long", (tape) => {
const hexValue = randomBytes(1).toString("hex");
test("An hexadecimal value must be at least 4 chars long", () => {
const hexValue = randomBytes(1).toString("hex");

tape.strictEqual(isHex(hexValue), false, `Hexadecimal value '${hexValue}' must return false`);
tape.end();
});
assert.strictEqual(isHex(hexValue), false, `Hexadecimal value '${hexValue}' must return false`);
});

test("isHex() of a value that is not a string or an ESTree Literal must return false", (tape) => {
const hexValue = 100;
test("should return false for non-string/ESTree Literal values", () => {
const hexValue = 100;

tape.strictEqual(isHex(hexValue), false, "100 is typeof number so it must always return false");
tape.end();
assert.strictEqual(isHex(hexValue), false, "100 is typeof number so it must always return false");
});
});

test("isSafe must return true for a value with a length lower or equal five characters", (tape) => {
tape.ok(isSafe("h2l5x"));
tape.end();
});
describe("isSafe()", () => {
test("must return true for a value with a length lower or equal five characters", () => {
assert.ok(isSafe("h2l5x"));
});

test("isSafe must return true if the string diversity is only two characters or lower", (tape) => {
tape.ok(isSafe("aaaaaaaaaaaaaabbbbbbbbbbbbb"));
tape.end();
});
test("must return true if the string diversity is only two characters or lower", () => {
assert.ok(isSafe("aaaaaaaaaaaaaabbbbbbbbbbbbb"));
});

test("isSafe must always return true if argument is only number, lower or upper letters", (tape) => {
const values = ["00000000", "aaaaaaaa", "AAAAAAAAA"];
test("must always return true if argument is only number, lower or upper letters", () => {
const values = ["00000000", "aaaaaaaa", "AAAAAAAAA"];

for (const hexValue of values) {
tape.ok(isSafe(hexValue));
}
tape.end();
});
for (const hexValue of values) {
assert.ok(isSafe(hexValue));
}
});

test("isSafe() must always return true if the value start with one of the 'safe' values", (tape) => {
for (const safeValue of CONSTANTS.SAFE_HEXA_VALUES) {
const hexValue = safeValue + randomBytes(4).toString("hex");
test("must always return true if the value start with one of the 'safe' values", () => {
for (const safeValue of CONSTANTS.SAFE_HEXA_VALUES) {
const hexValue = safeValue + randomBytes(4).toString("hex");

tape.ok(isSafe(hexValue));
}
tape.end();
});
assert.ok(isSafe(hexValue));
}
});

test("isSafe must return true because it start with a safe pattern (and it must lowerCase the string)", (tape) => {
tape.ok(isSafe("ABCDEF1234567890"));
tape.end();
});
test("must return true because it start with a safe pattern (and it must lowerCase the string)", () => {
assert.ok(isSafe("ABCDEF1234567890"));
});

test("isSafe() must always return false if the value start with one of the 'unsafe' values", (tape) => {
for (const unsafeValue of CONSTANTS.UNSAFE_HEXA_VALUES) {
tape.strictEqual(isSafe(unsafeValue), false);
}
tape.end();
test("must always return false if the value start with one of the 'unsafe' values", () => {
for (const unsafeValue of CONSTANTS.UNSAFE_HEXA_VALUES) {
assert.strictEqual(isSafe(unsafeValue), false);
}
});
});
58 changes: 24 additions & 34 deletions workspaces/sec-literal/test/literal.spec.js
Original file line number Diff line number Diff line change
@@ -1,99 +1,89 @@
// Import Node.js Dependencies
import { randomBytes } from "node:crypto";

// Import Third-party Dependencies
import test from "tape";
import { test } from "node:test";
import assert from "node:assert";

// Import Internal Dependencies
import { isLiteral, toValue, toRaw, defaultAnalysis } from "../src/literal.js";
import { createLiteral } from "./utils/index.js";

test("isLiteral must return true for a valid ESTree Literal Node", (tape) => {
test("isLiteral must return true for a valid ESTree Literal Node", () => {
const literalSample = createLiteral("boo");

tape.strictEqual(isLiteral(literalSample), true);
tape.strictEqual(isLiteral("hey"), false);
tape.strictEqual(isLiteral({ type: "fake", value: "boo" }), false);
tape.end();
assert.strictEqual(isLiteral(literalSample), true);
assert.strictEqual(isLiteral("hey"), false);
assert.strictEqual(isLiteral({ type: "fake", value: "boo" }), false);
});

test("toValue must return a string when we give a valid EStree Literal", (tape) => {
test("toValue must return a string when we give a valid EStree Literal", () => {
const literalSample = createLiteral("boo");

tape.strictEqual(toValue(literalSample), "boo");
tape.strictEqual(toValue("hey"), "hey");
tape.end();
assert.strictEqual(toValue(literalSample), "boo");
assert.strictEqual(toValue("hey"), "hey");
});

test("toRaw must return a string when we give a valid EStree Literal", (tape) => {
test("toRaw must return a string when we give a valid EStree Literal", () => {
const literalSample = createLiteral("boo", true);

tape.strictEqual(toRaw(literalSample), "boo");
tape.strictEqual(toRaw("hey"), "hey");
tape.end();
assert.strictEqual(toRaw(literalSample), "boo");
assert.strictEqual(toRaw("hey"), "hey");
});

test("defaultAnalysis() of something else than a Literal must always return null", (tape) => {
tape.strictEqual(defaultAnalysis(10), null);
tape.end();
test("defaultAnalysis() of something else than a Literal must always return null", () => {
assert.strictEqual(defaultAnalysis(10), null);
});

test("defaultAnalysis() of an Hexadecimal value", (tape) => {
test("defaultAnalysis() of an Hexadecimal value", () => {
const hexValue = randomBytes(10).toString("hex");

const result = defaultAnalysis(createLiteral(hexValue, true));
const expected = {
isBase64: true, hasHexadecimalSequence: false, hasUnicodeSequence: false
};

tape.deepEqual(result, expected);
tape.end();
assert.deepEqual(result, expected);
});

test("defaultAnalysis() of an Base64 value", (tape) => {
test("defaultAnalysis() of an Base64 value", () => {
const hexValue = randomBytes(10).toString("base64");

const result = defaultAnalysis(createLiteral(hexValue, true));
const expected = {
isBase64: true, hasHexadecimalSequence: false, hasUnicodeSequence: false
};

tape.deepEqual(result, expected);
tape.end();
assert.deepEqual(result, expected);
});

test("defaultAnalysis() of an Unicode Sequence", (tape) => {
test("defaultAnalysis() of an Unicode Sequence", () => {
const unicodeSequence = createLiteral("'\\u0024\\u0024'", true);

const result = defaultAnalysis(unicodeSequence);
const expected = {
isBase64: false, hasHexadecimalSequence: false, hasUnicodeSequence: true
};

tape.deepEqual(result, expected);
tape.end();
assert.deepEqual(result, expected);
});

test("defaultAnalysis() of an Unicode Sequence", (tape) => {
test("defaultAnalysis() of an Unicode Sequence", () => {
const hexSequence = createLiteral("'\\x64\\x61\\x74\\x61'", true);

const result = defaultAnalysis(hexSequence);
const expected = {
isBase64: false, hasHexadecimalSequence: true, hasUnicodeSequence: false
};

tape.deepEqual(result, expected);
tape.end();
assert.deepEqual(result, expected);
});

test("defaultAnalysis() with a Literal with no 'raw' property must return two null values", (tape) => {
test("defaultAnalysis() with a Literal with no 'raw' property must return two null values", () => {
const hexValue = randomBytes(10).toString("base64");

const result = defaultAnalysis(createLiteral(hexValue));
const expected = {
isBase64: true, hasHexadecimalSequence: null, hasUnicodeSequence: null
};

tape.deepEqual(result, expected);
tape.end();
assert.deepEqual(result, expected);
});
94 changes: 49 additions & 45 deletions workspaces/sec-literal/test/patterns.spec.js
Original file line number Diff line number Diff line change
@@ -1,64 +1,68 @@
// Import Node.js Dependencies
import { describe, test } from "node:test";
import assert from "node:assert";

// Import Internal Dependencies
import {
commonStringPrefix,
commonStringSuffix,
commonHexadecimalPrefix
} from "../src/patterns.js";

// Import Third-party Dependencies
import test from "tape";

test("commonStringPrefix of two strings that does not start with the same set of characters must return null", (tape) => {
tape.strictEqual(commonStringPrefix("boo", "foo"), null,
"there is no common prefix between 'boo' and 'foo' so the result must be null");
tape.end();
});
describe("commonStringPrefix()", () => {
test("must return null for two strings that have no common prefix", () => {
assert.strictEqual(commonStringPrefix("boo", "foo"), null,
"there is no common prefix between 'boo' and 'foo' so the result must be null");
});

test("commonStringPrefix of two strings that start with the same set of characters must return it as result", (tape) => {
tape.strictEqual(commonStringPrefix("bromance", "brother"), "bro",
"the common prefix between bromance and brother must be 'bro'.");
tape.end();
test("should return the common prefix for strings with a shared prefix", () => {
assert.strictEqual(commonStringPrefix("bromance", "brother"), "bro",
"the common prefix between bromance and brother must be 'bro'.");
});
});

test("commonStringSuffix of two strings that end with the same set of characters must return it as result", (tape) => {
tape.strictEqual(commonStringSuffix("boo", "foo"), "oo",
"the common suffix between boo and foo must be 'oo'");
tape.end();
});
describe("commonStringSuffix()", () => {
test("must return the common suffix for the two strings with a shared suffix", () => {
assert.strictEqual(commonStringSuffix("boo", "foo"), "oo",
"the common suffix between boo and foo must be 'oo'");
});

test("commonStringSuffix of two strings that does not end with the same set of characters must return null", (tape) => {
tape.strictEqual(commonStringSuffix("bromance", "brother"), null,
"there is no common suffix between 'bromance' and 'brother' so the result must be null");
tape.end();
test("must return null for two strings with no common suffix", () => {
assert.strictEqual(commonStringSuffix("bromance", "brother"), null,
"there is no common suffix between 'bromance' and 'brother' so the result must be null");
});
});

test("commonHexadecimalPrefix - throw a TypeError if identifiersArray is not an Array", (tape) => {
tape.throws(() => commonHexadecimalPrefix(10), "identifiersArray must be an Array");
tape.end();
});
describe("commonHexadecimalPrefix()", () => {
test("should throw a TypeError if identifiersArray is not an Array", () => {
assert.throws(() => commonHexadecimalPrefix(10), {
name: "TypeError",
message: "identifiersArray must be an Array"
});
});

test("commonHexadecimalPrefix - only hexadecimal identifiers", (tape) => {
const data = [
"_0x3c0c55", "_0x1185d5", "_0x160fc8", "_0x18a66f", "_0x18a835", "_0x1a8356",
"_0x1adf3b", "_0x1e4510", "_0x1e9a2a", "_0x215558", "_0x2b0194", "_0x2fffe5",
"_0x32c822", "_0x33bb79"
];
const result = commonHexadecimalPrefix(data);
test("should handle only hexadecimal identifiers", () => {
const data = [
"_0x3c0c55", "_0x1185d5", "_0x160fc8", "_0x18a66f", "_0x18a835", "_0x1a8356",
"_0x1adf3b", "_0x1e4510", "_0x1e9a2a", "_0x215558", "_0x2b0194", "_0x2fffe5",
"_0x32c822", "_0x33bb79"
];
const result = commonHexadecimalPrefix(data);

tape.strictEqual(result.oneTimeOccurence, 0);
tape.strictEqual(result.prefix._0x, data.length);
tape.end();
});
assert.strictEqual(result.oneTimeOccurence, 0);
assert.strictEqual(result.prefix._0x, data.length);
});

test("commonHexadecimalPrefix - add one non-hexadecimal identifier", (tape) => {
const data = [
"_0x3c0c55", "_0x1185d5", "_0x160fc8", "_0x18a66f", "_0x18a835", "_0x1a8356",
"_0x1adf3b", "_0x1e4510", "_0x1e9a2a", "_0x215558", "_0x2b0194", "_0x2fffe5",
"_0x32c822", "_0x33bb79", "foo"
];
const result = commonHexadecimalPrefix(data);
test("should add one non-hexadecimal identifier", () => {
const data = [
"_0x3c0c55", "_0x1185d5", "_0x160fc8", "_0x18a66f", "_0x18a835", "_0x1a8356",
"_0x1adf3b", "_0x1e4510", "_0x1e9a2a", "_0x215558", "_0x2b0194", "_0x2fffe5",
"_0x32c822", "_0x33bb79", "foo"
];
const result = commonHexadecimalPrefix(data);

tape.strictEqual(result.oneTimeOccurence, 1);
tape.strictEqual(result.prefix._0x, data.length - 1);
tape.end();
assert.strictEqual(result.oneTimeOccurence, 1);
assert.strictEqual(result.prefix._0x, data.length - 1);
});
});
Loading

0 comments on commit 425f270

Please sign in to comment.