Skip to content

Commit

Permalink
test: add coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
j4k0xb committed Jul 12, 2024
1 parent afd8aea commit 72a821f
Show file tree
Hide file tree
Showing 9 changed files with 366 additions and 229 deletions.
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@
"lint": "turbo run lint",
"lint:fix": "turbo run lint:fix",
"test": "vitest --no-isolate",
"test:coverage": "vitest run --coverage --no-isolate",
"format": "prettier --write \"**/*.{ts,tsx,md}\""
},
"devDependencies": {
"@vitest/coverage-istanbul": "^2.0.2",
"@vitest/coverage-v8": "^2.0.2",
"@webcrack/eslint-config": "workspace:*",
"eslint": "^9.4.0",
"prettier": "^3.3.1",
"turbo": "^2.0.1",
"vitest": "^1.6.0"
"vitest": "^2.0.2"
},
"packageManager": "[email protected]",
"pnpm": {
Expand Down
47 changes: 47 additions & 0 deletions packages/webcrack/src/ast-utils/test/inline.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { parse } from '@babel/parser';
import traverse from '@babel/traverse';
import * as t from '@babel/types';
import { expect, test } from 'vitest';
import {
inlineArrayElements,
inlineFunction,
inlineObjectProperties,
inlineVariable,
} from '..';
Expand Down Expand Up @@ -56,3 +58,48 @@ test('inline object properties', () => {
});
expect(ast).toMatchInlineSnapshot(`console.log(0x2f2, '0x396');`);
});

test('inline function', () => {
const ast = parse(`
function f(a, b) {
return a + b;
}
fn(1, 2);
`);
traverse(ast, {
CallExpression(path) {
const fn = path.parentPath.getPrevSibling().node as t.FunctionDeclaration;
inlineFunction(fn, path);
},
});
expect(ast).toMatchInlineSnapshot(`
function f(a, b) {
return a + b;
}
1 + 2;
`);
});

test('inline function with rest arg', () => {
const ast = parse(`
function f(a, ...b) {
return a(...b);
}
fn(x, 1, 2, 3);
`);
traverse(ast, {
CallExpression(path) {
if (t.isIdentifier(path.node.callee, { name: 'fn' })) {
const fn = path.parentPath.getPrevSibling()
.node as t.FunctionDeclaration;
inlineFunction(fn, path);
}
},
});
expect(ast).toMatchInlineSnapshot(`
function f(a, ...b) {
return a(...b);
}
x(1, 2, 3);
`);
});
2 changes: 2 additions & 0 deletions packages/webcrack/src/ast-utils/test/rename.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ describe('rename variable', () => {
const ast = parse(`
var a = 1;
var a = 2;
a = 3;
a++;
[a] = [2];
({...a} = {});
Expand All @@ -53,6 +54,7 @@ describe('rename variable', () => {
expect(ast).toMatchInlineSnapshot(`
var b = 1;
var b = 2;
b = 3;
b++;
[b] = [2];
({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`inline decoder > inline function 1`] = `
"function decoder() {}
decoder(1);
"function decoder(a, b) {}
decoder(1, 2);
function ignore() {
return decoder(3, 4);
}
(() => {
decoder(2 - 625, 3);
(() => {
Expand Down
7 changes: 7 additions & 0 deletions packages/webcrack/src/deobfuscate/test/dead-code.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ test('keep false branch', () =>
}
`).toMatchInlineSnapshot(`console.log("bar");`));

test('remove false branch without else', () =>
expectJS(`
if ("a" !== "a") {
console.log("foo");
}
`).toMatchInlineSnapshot(``));

test('merge scopes', () =>
expectJS(`
let foo = 1;
Expand Down
7 changes: 5 additions & 2 deletions packages/webcrack/src/deobfuscate/test/deobfuscate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,11 @@ describe('inline decoder', () => {

test('inline function', () => {
const ast = parse(`
function decoder() {}
decoder(1);
function decoder(a, b) {}
decoder(1, 2);
function ignore() {
return decoder(3, 4);
}
(() => {
function alias(a, b) {
return decoder(a - 625, b);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,6 @@ function unshift(template: t.TemplateLiteral, value: t.Expression) {
if (value.type === 'StringLiteral') {
const firstQuasi = template.quasis[0];
firstQuasi.value.raw = escape(value.value) + firstQuasi.value.raw;
} else if (value.type === 'TemplateLiteral') {
const firstQuasi = template.quasis[0];
firstQuasi.value.raw = value.quasis[0].value.raw + firstQuasi.value.raw;
template.expressions.unshift(...value.expressions);
template.quasis.unshift(...value.quasis.slice(0, -1));
} else {
template.expressions.unshift(value);
template.quasis.unshift(t.templateElement({ raw: '' }));
Expand Down
Loading

0 comments on commit 72a821f

Please sign in to comment.