mirror of
https://github.com/javascript-obfuscator/javascript-obfuscator
synced 2026-08-09 12:42:29 +00:00
CatchClauseNodeObfuscator spec
This commit is contained in:
+27
-85
@@ -6,76 +6,18 @@ import { ILiteralNode } from "../src/interfaces/nodes/ILiteralNode";
|
||||
import { INode } from "../src/interfaces/nodes/INode";
|
||||
import { IProgramNode } from "../src/interfaces/nodes/IProgramNode";
|
||||
|
||||
import { NodeType } from "../src/enums/NodeType";
|
||||
|
||||
import { NodeMocks } from './mocks/NodeMocks';
|
||||
import { NodeUtils } from '../src/NodeUtils';
|
||||
|
||||
const assert: any = require('chai').assert;
|
||||
|
||||
function getProgramNode (bodyNodes: INode[] = []): IProgramNode {
|
||||
return {
|
||||
type: NodeType.Program,
|
||||
body: bodyNodes
|
||||
};
|
||||
}
|
||||
|
||||
function getBlockStatementNode (bodyNodes: INode[] = []): IBlockStatementNode {
|
||||
return {
|
||||
type: NodeType.BlockStatement,
|
||||
body: bodyNodes
|
||||
};
|
||||
}
|
||||
|
||||
function getFunctionDeclarationNode (blockStatementNode: IBlockStatementNode): IFunctionDeclarationNode {
|
||||
return {
|
||||
type: NodeType.FunctionDeclaration,
|
||||
id: {
|
||||
type: NodeType.Identifier,
|
||||
name: 'test'
|
||||
},
|
||||
params: [],
|
||||
body: blockStatementNode,
|
||||
generator: false,
|
||||
expression: false
|
||||
};
|
||||
}
|
||||
|
||||
function getIfStatementNode (blockStatementNode: IBlockStatementNode): IIfStatementNode {
|
||||
return {
|
||||
type: 'IfStatement',
|
||||
test: {
|
||||
type: 'Literal',
|
||||
value: true,
|
||||
raw: 'true'
|
||||
},
|
||||
consequent: blockStatementNode,
|
||||
alternate: null
|
||||
};
|
||||
}
|
||||
|
||||
function getIdentifierNode (): IIdentifierNode {
|
||||
return {
|
||||
type: NodeType.Identifier,
|
||||
name: 'identifier',
|
||||
};
|
||||
}
|
||||
|
||||
function getLiteralNode (): ILiteralNode {
|
||||
return {
|
||||
type: NodeType.Literal,
|
||||
value: 'string',
|
||||
raw: `'string'`,
|
||||
'x-verbatim-property': `'string'`
|
||||
};
|
||||
}
|
||||
|
||||
describe('NodeUtils', () => {
|
||||
describe('addXVerbatimPropertyToLiterals (node: INode): void', () => {
|
||||
let literalNode: any,
|
||||
expectedLiteralNode: any;
|
||||
|
||||
beforeEach(() => {
|
||||
literalNode = getLiteralNode();
|
||||
literalNode = NodeMocks.getLiteralNode();
|
||||
|
||||
expectedLiteralNode = Object.assign({}, literalNode);
|
||||
expectedLiteralNode['x-verbatim-property'] = `'string'`;
|
||||
@@ -94,9 +36,9 @@ describe('NodeUtils', () => {
|
||||
identifierNode: IIdentifierNode;
|
||||
|
||||
beforeEach(() => {
|
||||
identifierNode = getIdentifierNode();
|
||||
identifierNode = NodeMocks.getIdentifierNode();
|
||||
|
||||
blockStatementNode = getBlockStatementNode();
|
||||
blockStatementNode = NodeMocks.getBlockStatementNode();
|
||||
|
||||
expectedBlockStatementNode = Object.assign({}, blockStatementNode);
|
||||
expectedBlockStatementNode.body.push(identifierNode);
|
||||
@@ -129,11 +71,11 @@ describe('NodeUtils', () => {
|
||||
literalNode: ILiteralNode;
|
||||
|
||||
beforeEach(() => {
|
||||
identifierNode = getIdentifierNode();
|
||||
identifierNode = NodeMocks.getIdentifierNode();
|
||||
|
||||
literalNode = getLiteralNode();
|
||||
literalNode = NodeMocks.getLiteralNode();
|
||||
|
||||
blockStatementNode = getBlockStatementNode([
|
||||
blockStatementNode = NodeMocks.getBlockStatementNode([
|
||||
identifierNode,
|
||||
literalNode
|
||||
]);
|
||||
@@ -166,32 +108,32 @@ describe('NodeUtils', () => {
|
||||
programNode: IProgramNode;
|
||||
|
||||
beforeEach(() => {
|
||||
identifierNode = getIdentifierNode();
|
||||
identifierNode = NodeMocks.getIdentifierNode();
|
||||
|
||||
literalNode1 = getLiteralNode();
|
||||
literalNode2 = getLiteralNode();
|
||||
literalNode1 = NodeMocks.getLiteralNode();
|
||||
literalNode2 = NodeMocks.getLiteralNode();
|
||||
|
||||
ifStatementBlockStatementNode2 = getBlockStatementNode([
|
||||
ifStatementBlockStatementNode2 = NodeMocks.getBlockStatementNode([
|
||||
literalNode1,
|
||||
literalNode2
|
||||
]);
|
||||
|
||||
ifStatementNode2 = getIfStatementNode(ifStatementBlockStatementNode2);
|
||||
ifStatementNode2 = NodeMocks.getIfStatementNode(ifStatementBlockStatementNode2);
|
||||
|
||||
ifStatementBlockStatementNode1 = getBlockStatementNode([
|
||||
ifStatementBlockStatementNode1 = NodeMocks.getBlockStatementNode([
|
||||
ifStatementNode2
|
||||
]);
|
||||
|
||||
ifStatementNode1 = getIfStatementNode(ifStatementBlockStatementNode1);
|
||||
ifStatementNode1 = NodeMocks.getIfStatementNode(ifStatementBlockStatementNode1);
|
||||
|
||||
functionDeclarationBlockStatementNode = getBlockStatementNode([
|
||||
functionDeclarationBlockStatementNode = NodeMocks.getBlockStatementNode([
|
||||
identifierNode,
|
||||
ifStatementNode1
|
||||
]);
|
||||
|
||||
functionDeclarationNode = getFunctionDeclarationNode(functionDeclarationBlockStatementNode);
|
||||
functionDeclarationNode = NodeMocks.getFunctionDeclarationNode(functionDeclarationBlockStatementNode);
|
||||
|
||||
programNode = getProgramNode([
|
||||
programNode = NodeMocks.getProgramNode([
|
||||
functionDeclarationNode
|
||||
]);
|
||||
|
||||
@@ -227,11 +169,11 @@ describe('NodeUtils', () => {
|
||||
literalNode: ILiteralNode;
|
||||
|
||||
beforeEach(() => {
|
||||
identifierNode = getIdentifierNode();
|
||||
identifierNode = NodeMocks.getIdentifierNode();
|
||||
|
||||
literalNode = getLiteralNode();
|
||||
literalNode = NodeMocks.getLiteralNode();
|
||||
|
||||
blockStatementNode = getBlockStatementNode([
|
||||
blockStatementNode = NodeMocks.getBlockStatementNode([
|
||||
identifierNode
|
||||
]);
|
||||
|
||||
@@ -267,16 +209,16 @@ describe('NodeUtils', () => {
|
||||
programNode: IProgramNode;
|
||||
|
||||
beforeEach(() => {
|
||||
identifierNode = getIdentifierNode();
|
||||
identifierNode = NodeMocks.getIdentifierNode();
|
||||
|
||||
literalNode = getLiteralNode();
|
||||
literalNode = NodeMocks.getLiteralNode();
|
||||
|
||||
blockStatementNode = getBlockStatementNode([
|
||||
blockStatementNode = NodeMocks.getBlockStatementNode([
|
||||
identifierNode,
|
||||
literalNode
|
||||
]);
|
||||
|
||||
programNode = getProgramNode([
|
||||
programNode = NodeMocks.getProgramNode([
|
||||
blockStatementNode
|
||||
]);
|
||||
|
||||
@@ -297,11 +239,11 @@ describe('NodeUtils', () => {
|
||||
literalNode: ILiteralNode;
|
||||
|
||||
beforeEach(() => {
|
||||
identifierNode = getIdentifierNode();
|
||||
identifierNode = NodeMocks.getIdentifierNode();
|
||||
|
||||
literalNode = getLiteralNode();
|
||||
literalNode = NodeMocks.getLiteralNode();
|
||||
|
||||
blockStatementNode = getBlockStatementNode([
|
||||
blockStatementNode = NodeMocks.getBlockStatementNode([
|
||||
identifierNode
|
||||
]);
|
||||
|
||||
|
||||
@@ -0,0 +1,101 @@
|
||||
import { IBlockStatementNode } from "../../src/interfaces/nodes/IBlockStatementNode";
|
||||
import { ICatchClauseNode } from "../../src/interfaces/nodes/ICatchClauseNode";
|
||||
import { IFunctionDeclarationNode } from "../../src/interfaces/nodes/IFunctionDeclarationNode";
|
||||
import { IIdentifierNode } from "../../src/interfaces/nodes/IIdentifierNode";
|
||||
import { IIfStatementNode } from "../../src/interfaces/nodes/IIfStatementNode";
|
||||
import { ILiteralNode } from "../../src/interfaces/nodes/ILiteralNode";
|
||||
import { INode } from "../../src/interfaces/nodes/INode";
|
||||
import { IProgramNode } from "../../src/interfaces/nodes/IProgramNode";
|
||||
|
||||
import { NodeType } from "../../src/enums/NodeType";
|
||||
|
||||
export class NodeMocks {
|
||||
/**
|
||||
* @param bodyNodes
|
||||
* @returns {IProgramNode}
|
||||
*/
|
||||
public static getProgramNode (bodyNodes: INode[] = []): IProgramNode {
|
||||
return {
|
||||
type: NodeType.Program,
|
||||
body: bodyNodes
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bodyNodes
|
||||
* @returns {IBlockStatementNode}
|
||||
*/
|
||||
public static getBlockStatementNode (bodyNodes: INode[] = []): IBlockStatementNode {
|
||||
return {
|
||||
type: NodeType.BlockStatement,
|
||||
body: bodyNodes
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bodyNodes
|
||||
* @returns {ICatchClauseNode}
|
||||
*/
|
||||
public static getCatchClauseNode (bodyNodes: INode[] = []): ICatchClauseNode {
|
||||
return {
|
||||
type: NodeType.CatchClause,
|
||||
param: NodeMocks.getIdentifierNode('err'),
|
||||
body: NodeMocks.getBlockStatementNode(bodyNodes)
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @param blockStatementNode
|
||||
* @returns {IFunctionDeclarationNode}
|
||||
*/
|
||||
public static getFunctionDeclarationNode (blockStatementNode: IBlockStatementNode): IFunctionDeclarationNode {
|
||||
return {
|
||||
type: NodeType.FunctionDeclaration,
|
||||
id: NodeMocks.getIdentifierNode('test'),
|
||||
params: [],
|
||||
body: blockStatementNode,
|
||||
generator: false,
|
||||
expression: false
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @param blockStatementNode
|
||||
* @returns {IIfStatementNode}
|
||||
*/
|
||||
public static getIfStatementNode (blockStatementNode: IBlockStatementNode): IIfStatementNode {
|
||||
return {
|
||||
type: 'IfStatement',
|
||||
test: {
|
||||
type: 'Literal',
|
||||
value: true,
|
||||
raw: 'true'
|
||||
},
|
||||
consequent: blockStatementNode,
|
||||
alternate: null
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @param identifierName
|
||||
* @returns {IIdentifierNode}
|
||||
*/
|
||||
public static getIdentifierNode (identifierName: string = 'identifier'): IIdentifierNode {
|
||||
return {
|
||||
type: NodeType.Identifier,
|
||||
name: identifierName,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @returns {ILiteralNode}
|
||||
*/
|
||||
public static getLiteralNode (): ILiteralNode {
|
||||
return {
|
||||
type: NodeType.Literal,
|
||||
value: 'string',
|
||||
raw: `'string'`,
|
||||
'x-verbatim-property': `'string'`
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
import { ICatchClauseNode } from "../../src/interfaces/nodes/ICatchClauseNode";
|
||||
import { ICustomNode } from "../../src/interfaces/ICustomNode";
|
||||
import { INode } from "../../src/interfaces/nodes/INode";
|
||||
|
||||
import { DEFAULT_PRESET } from "../../src/preset-options/DefaultPreset";
|
||||
|
||||
import { NodeType } from "../../src/enums/NodeType";
|
||||
|
||||
import { CatchClauseObfuscator } from '../../src/node-obfuscators/CatchClauseObfuscator';
|
||||
import { NodeMocks } from "../mocks/NodeMocks";
|
||||
import { Options } from "../../src/Options";
|
||||
|
||||
const assert: Chai.AssertStatic = require('chai').assert;
|
||||
|
||||
describe('CatchClauseObfuscator', () => {
|
||||
describe('obfuscateNode (catchClauseNode: ICatchClauseNode): void', () => {
|
||||
let catchClauseObfuscator: CatchClauseObfuscator,
|
||||
catchClauseNode: ICatchClauseNode;
|
||||
|
||||
beforeEach(() => {
|
||||
catchClauseObfuscator = new CatchClauseObfuscator(
|
||||
new Map<string, ICustomNode>(),
|
||||
new Options(DEFAULT_PRESET)
|
||||
);
|
||||
|
||||
catchClauseNode = NodeMocks.getCatchClauseNode([
|
||||
<INode>{
|
||||
type: NodeType.ExpressionStatement,
|
||||
expression: {
|
||||
type: NodeType.CallExpression,
|
||||
callee: {
|
||||
type: NodeType.MemberExpression,
|
||||
computed: false,
|
||||
object: {
|
||||
type: NodeType.Identifier,
|
||||
name: 'console'
|
||||
},
|
||||
property: {
|
||||
type: NodeType.Identifier,
|
||||
name: 'log'
|
||||
}
|
||||
},
|
||||
arguments: [
|
||||
{
|
||||
type: NodeType.Identifier,
|
||||
'name': 'err'
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
]);
|
||||
|
||||
catchClauseObfuscator.obfuscateNode(catchClauseNode);
|
||||
});
|
||||
|
||||
it('should obfuscate catch clause param name', () => {
|
||||
assert.match(
|
||||
(<any>catchClauseNode.body.body[0]).expression.arguments[0].name,
|
||||
/^_0x\w+$/
|
||||
);
|
||||
});
|
||||
|
||||
it('should obfuscate catch clause param calls in catch clause node body', () => {
|
||||
assert.match(
|
||||
catchClauseNode.param.name,
|
||||
/^_0x\w+$/
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user