From e130608152720af69af2142b05956db17855715f Mon Sep 17 00:00:00 2001 From: sanex3339 Date: Sat, 2 Jul 2016 18:33:01 +0300 Subject: [PATCH] CatchClauseNodeObfuscator spec --- test/NodeUtils.spec.ts | 112 +++++------------- test/mocks/NodeMocks.ts | 101 ++++++++++++++++ .../CatchClauseObfuscator.spec.ts | 70 +++++++++++ 3 files changed, 198 insertions(+), 85 deletions(-) create mode 100644 test/mocks/NodeMocks.ts create mode 100644 test/node-obfuscators/CatchClauseObfuscator.spec.ts diff --git a/test/NodeUtils.spec.ts b/test/NodeUtils.spec.ts index f1f54ea7..5450a3b6 100644 --- a/test/NodeUtils.spec.ts +++ b/test/NodeUtils.spec.ts @@ -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 ]); diff --git a/test/mocks/NodeMocks.ts b/test/mocks/NodeMocks.ts new file mode 100644 index 00000000..b2a93cab --- /dev/null +++ b/test/mocks/NodeMocks.ts @@ -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'` + }; + } +} \ No newline at end of file diff --git a/test/node-obfuscators/CatchClauseObfuscator.spec.ts b/test/node-obfuscators/CatchClauseObfuscator.spec.ts new file mode 100644 index 00000000..16ce6114 --- /dev/null +++ b/test/node-obfuscators/CatchClauseObfuscator.spec.ts @@ -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(), + new Options(DEFAULT_PRESET) + ); + + catchClauseNode = NodeMocks.getCatchClauseNode([ + { + 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( + (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+$/ + ); + }); + }); +});