Browse Source

CatchClauseObfuscator.spec refactoring

sanex3339 8 years ago
parent
commit
f78c7fe4d0

+ 5 - 0
test/fixtures/node-obfuscators/catch-clause-obfuscator/catch-clause-obfuscator.js

@@ -0,0 +1,5 @@
+try {
+
+} catch (error) {
+    console.log(error);
+}

+ 38 - 0
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);
+        });
+    });
+});

+ 1 - 1
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';

+ 0 - 72
test/unit-tests/node-obfuscators/CatchClauseObfuscator.spec.ts

@@ -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<string, ICustomNode>(),
-                new Options(DEFAULT_PRESET)
-            );
-
-            catchClauseNode = NodeMocks.getCatchClauseNode([
-                expressionStatementNode
-            ]);
-
-            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(
-                (<ESTree.Identifier>catchClauseNode.param).name,
-                /^_0x\w+$/
-            );
-        });
-    });
-});