diff --git a/test/fixtures/node-obfuscators/catch-clause-obfuscator/catch-clause-obfuscator.js b/test/fixtures/node-obfuscators/catch-clause-obfuscator/catch-clause-obfuscator.js new file mode 100644 index 00000000..6f011b12 --- /dev/null +++ b/test/fixtures/node-obfuscators/catch-clause-obfuscator/catch-clause-obfuscator.js @@ -0,0 +1,5 @@ +try { + +} catch (error) { + console.log(error); +} \ No newline at end of file diff --git a/test/functional-tests/node-obfuscators/CatchClauseObfuscator.spec.ts b/test/functional-tests/node-obfuscators/CatchClauseObfuscator.spec.ts new file mode 100644 index 00000000..f014ff99 --- /dev/null +++ b/test/functional-tests/node-obfuscators/CatchClauseObfuscator.spec.ts @@ -0,0 +1,38 @@ +import { IObfuscationResult } from '../../../src/interfaces/IObfuscationResult'; + +import { NO_CUSTOM_NODES_PRESET } from '../../../src/preset-options/NoCustomNodesPreset'; + +import { readFileAsString } from '../../helpers/readFileAsString'; + +import { JavaScriptObfuscator } from '../../../src/JavaScriptObfuscator'; + +const assert: Chai.AssertStatic = require('chai').assert; + +describe('CatchClauseObfuscator', () => { + describe('obfuscateNode (catchClauseNode: ICatchClauseNode): void', () => { + const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate( + readFileAsString('./test/fixtures/node-obfuscators/catch-clause-obfuscator/catch-clause-obfuscator.js'), + Object.assign({}, NO_CUSTOM_NODES_PRESET) + ); + const obfuscatedCode: string = obfuscationResult.getObfuscatedCode(); + const paramNameRegExp: RegExp = /catch *\((_0x([a-z0-9]){4,6})\) *\{/; + const bodyParamNameRegExp: RegExp = /console\['\\x6c\\x6f\\x67'\]\((_0x([a-z0-9]){4,6})\);/; + + it('should obfuscate catch clause node', () => { + assert.match(obfuscatedCode, paramNameRegExp); + assert.match(obfuscatedCode, bodyParamNameRegExp); + }); + + it('catch clause arguments param name and param name in body should be same', () => { + const firstMatchArray: RegExpMatchArray|null = obfuscatedCode.match(paramNameRegExp); + const secondMatchArray: RegExpMatchArray|null = obfuscatedCode.match(bodyParamNameRegExp); + + const firstMatch: string|undefined = firstMatchArray ? firstMatchArray[1] : undefined; + const secondMatch: string|undefined = secondMatchArray ? secondMatchArray[1] : undefined; + + assert.isOk(firstMatch); + assert.isOk(secondMatch); + assert.equal(firstMatch, secondMatch); + }); + }); +}); diff --git a/test/index.spec.ts b/test/index.spec.ts index 0116f0b9..e041f342 100644 --- a/test/index.spec.ts +++ b/test/index.spec.ts @@ -14,7 +14,6 @@ import './unit-tests/OptionsNormalizer.spec'; import './unit-tests/SourceMapCorrector.spec'; import './unit-tests/Utils.spec'; import './unit-tests/cli/CLIUtils.spec'; -import './unit-tests/node-obfuscators/CatchClauseObfuscator.spec'; import './unit-tests/node-obfuscators/FunctionDeclarationObfuscator.spec'; import './unit-tests/node-obfuscators/FunctionObfuscator.spec'; import './unit-tests/stack-trace-analyzer/StackTraceAnalyzer.spec'; @@ -30,6 +29,7 @@ import './functional-tests/custom-nodes/domain-lock-nodes/DomainLockNode.spec'; import './functional-tests/custom-nodes/string-array-nodes/StringArrayCallsWrapper.spec'; import './functional-tests/custom-nodes/string-array-nodes/StringArrayRotateFunctionNode.spec'; import './functional-tests/custom-nodes/string-array-nodes/StringArrayNode.spec'; +import './functional-tests/node-obfuscators/CatchClauseObfuscator.spec'; import './functional-tests/node-obfuscators/FunctionObfuscator.spec'; import './functional-tests/node-obfuscators/LiteralObfuscator.spec'; import './functional-tests/node-obfuscators/MemberExpressionObfuscator.spec'; diff --git a/test/unit-tests/node-obfuscators/CatchClauseObfuscator.spec.ts b/test/unit-tests/node-obfuscators/CatchClauseObfuscator.spec.ts deleted file mode 100644 index a47129b7..00000000 --- a/test/unit-tests/node-obfuscators/CatchClauseObfuscator.spec.ts +++ /dev/null @@ -1,72 +0,0 @@ -import * as ESTree from 'estree'; - -import { ICustomNode } from '../../../src/interfaces/custom-nodes/ICustomNode'; - -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/Options'; - -const assert: Chai.AssertStatic = require('chai').assert; - -describe('CatchClauseObfuscator', () => { - describe('obfuscateNode (catchClauseNode: ICatchClauseNode): void', () => { - let catchClauseObfuscator: CatchClauseObfuscator, - catchClauseNode: ESTree.CatchClause; - - beforeEach(() => { - let expressionStatementNode: ESTree.ExpressionStatement = { - 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 = new CatchClauseObfuscator( - new Map(), - new Options(DEFAULT_PRESET) - ); - - catchClauseNode = NodeMocks.getCatchClauseNode([ - expressionStatementNode - ]); - - 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+$/ - ); - }); - }); -});