소스 검색

Added more tests and fixes of #217

sanex3339 7 년 전
부모
커밋
10144ee6e6

파일 크기가 너무 크기때문에 변경 상태를 표시하지 않습니다.
+ 0 - 0
dist/index.js


+ 2 - 1
src/node-transformers/obfuscating-transformers/ClassDeclarationTransformer.ts

@@ -146,13 +146,14 @@ export class ClassDeclarationTransformer extends AbstractNodeTransformer {
 
         estraverse.replace(blockScopeNode, {
             enter: (node: ESTree.Node, parentNode: ESTree.Node | null): void => {
-                if (parentNode && NodeGuards.isReplaceableIdentifierNode(node, parentNode)) {
+                if (parentNode && !node.obfuscatedNode && NodeGuards.isReplaceableIdentifierNode(node, parentNode)) {
                     const newIdentifier: ESTree.Identifier = this.identifierObfuscatingReplacer
                         .replace(node.name, nodeIdentifier);
                     const newIdentifierName: string = newIdentifier.name;
 
                     if (node.name !== newIdentifierName) {
                         node.name = newIdentifierName;
+                        node.obfuscatedNode = true;
                     } else {
                         storedReplaceableIdentifiers.push(node);
                     }

+ 3 - 3
test/dev/dev.ts

@@ -6,10 +6,10 @@ import { NO_ADDITIONAL_NODES_PRESET } from '../../src/options/presets/NoCustomNo
 
     let obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
         `
-        (function() {        
+        (function() {
             function test() {
-                function func1() {}
-                let a, b, c, d, e;
+                class inner {}
+                let a, b, c, d;
             }
         })();
         `,

+ 29 - 0
test/functional-tests/node-transformers/obfuscating-transformers/class-declaration-transformer/ClassDeclarationTransformer.spec.ts

@@ -8,6 +8,7 @@ import { getRegExpMatch } from '../../../../helpers/getRegExpMatch';
 import { readFileAsString } from '../../../../helpers/readFileAsString';
 
 import { JavaScriptObfuscator } from '../../../../../src/JavaScriptObfuscatorFacade';
+import { IdentifierNamesGenerator } from '../../../../../src/enums/generators/identifier-names-generators/IdentifierNamesGenerator';
 
 describe('ClassDeclarationTransformer', () => {
     describe('transformation of `classDeclaration` node names', () => {
@@ -94,5 +95,33 @@ describe('ClassDeclarationTransformer', () => {
                 });
             });
         });
+
+        describe('Variant #3: already renamed identifiers shouldn\'t be renamed twice', () => {
+            const classDeclarationRegExp: RegExp = /class *d *{/;
+            const variableDeclarationsRegExp: RegExp = /let *e, *f, *g, *h;/;
+
+            let obfuscatedCode: string;
+
+            before(() => {
+                const code: string = readFileAsString(__dirname + '/fixtures/prevent-renaming-of-renamed-identifiers.js');
+                const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
+                    code,
+                    {
+                        ...NO_ADDITIONAL_NODES_PRESET,
+                        identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator
+                    }
+                );
+
+                obfuscatedCode = obfuscationResult.getObfuscatedCode();
+            });
+
+            it('Match #1: shouldn\'t rename twice class declaration name', () => {
+                assert.match(obfuscatedCode, classDeclarationRegExp);
+            });
+
+            it('Match #2: should correctly rename variable declarations', () => {
+                assert.match(obfuscatedCode, variableDeclarationsRegExp);
+            });
+        });
     });
 });

+ 6 - 0
test/functional-tests/node-transformers/obfuscating-transformers/class-declaration-transformer/fixtures/prevent-renaming-of-renamed-identifiers.js

@@ -0,0 +1,6 @@
+(function() {
+    function test() {
+        class inner {}
+        let a, b, c, d;
+    }
+})();

+ 29 - 0
test/functional-tests/node-transformers/obfuscating-transformers/function-declaration-transformer/FunctionDeclarationTransformer.spec.ts

@@ -8,6 +8,7 @@ import { getRegExpMatch } from '../../../../helpers/getRegExpMatch';
 import { readFileAsString } from '../../../../helpers/readFileAsString';
 
 import { JavaScriptObfuscator } from '../../../../../src/JavaScriptObfuscatorFacade';
+import { IdentifierNamesGenerator } from '../../../../../src/enums/generators/identifier-names-generators/IdentifierNamesGenerator';
 
 describe('FunctionDeclarationTransformer', () => {
     describe('transformation of `functionDeclaration` node names', () => {
@@ -148,5 +149,33 @@ describe('FunctionDeclarationTransformer', () => {
                 assert.equal(functionNameIdentifier, functionCallIdentifier);
             });
         });
+
+        describe('Variant #5: already renamed identifiers shouldn\'t be renamed twice', () => {
+            const functionDeclarationRegExp: RegExp = /function *d\(\) *{/;
+            const variableDeclarationsRegExp: RegExp = /let *e, *f, *g, *h;/;
+
+            let obfuscatedCode: string;
+
+            before(() => {
+                const code: string = readFileAsString(__dirname + '/fixtures/prevent-renaming-of-renamed-identifiers.js');
+                const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
+                    code,
+                    {
+                        ...NO_ADDITIONAL_NODES_PRESET,
+                        identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator
+                    }
+                );
+
+                obfuscatedCode = obfuscationResult.getObfuscatedCode();
+            });
+
+            it('Match #1: shouldn\'t rename twice function declaration name', () => {
+                assert.match(obfuscatedCode, functionDeclarationRegExp);
+            });
+
+            it('Match #2: should correctly rename variable declarations', () => {
+                assert.match(obfuscatedCode, variableDeclarationsRegExp);
+            });
+        });
     });
 });

+ 6 - 0
test/functional-tests/node-transformers/obfuscating-transformers/function-declaration-transformer/fixtures/prevent-renaming-of-renamed-identifiers.js

@@ -0,0 +1,6 @@
+(function() {
+    function test() {
+        function inner() {}
+        let a, b, c, d;
+    }
+})();

+ 22 - 7
test/functional-tests/node-transformers/obfuscating-transformers/variable-declaration-transformer/VariableDeclarationTransformer.spec.ts

@@ -438,9 +438,12 @@ describe('VariableDeclarationTransformer', () => {
         });
     });
 
-    describe('Variant #13: renamed identifiers shouldn\'t be renamed twice', () => {
-        const functionRegExp: RegExp = /function *d\(\) *{/;
-        const variableDeclarationsRegExp: RegExp = /let *e, *f, *g, *h;/;
+    describe('Variant #13: already renamed identifiers shouldn\'t be renamed twice', () => {
+        const variableDeclarationRegExp: RegExp = /var *d *= *0x1;/;
+        const functionDeclarationRegExp1: RegExp = /function *e *\(\) *{}/;
+        const functionDeclarationRegExp2: RegExp = /function *f *\(\) *{}/;
+        const functionDeclarationRegExp3: RegExp = /function *g *\(\) *{}/;
+        const functionDeclarationRegExp4: RegExp = /function *h *\(\) *{}/;
 
         let obfuscatedCode: string;
 
@@ -457,12 +460,24 @@ describe('VariableDeclarationTransformer', () => {
             obfuscatedCode = obfuscationResult.getObfuscatedCode();
         });
 
-        it('Match #1: shouldn\'t rename twice function declaration name', () => {
-            assert.match(obfuscatedCode, functionRegExp);
+        it('Match #1: shouldn\'t rename twice variable declaration name', () => {
+            assert.match(obfuscatedCode, variableDeclarationRegExp);
+        });
+
+        it('Match #2: should correctly rename function declaration name', () => {
+            assert.match(obfuscatedCode, functionDeclarationRegExp1);
+        });
+
+        it('Match #2: should correctly rename function declaration name', () => {
+            assert.match(obfuscatedCode, functionDeclarationRegExp2);
+        });
+
+        it('Match #2: should correctly rename function declaration name', () => {
+            assert.match(obfuscatedCode, functionDeclarationRegExp3);
         });
 
-        it('Match #2: should correctly rename variable declarations', () => {
-            assert.match(obfuscatedCode, variableDeclarationsRegExp);
+        it('Match #2: should correctly rename function declaration name', () => {
+            assert.match(obfuscatedCode, functionDeclarationRegExp4);
         });
     });
 });

+ 6 - 2
test/functional-tests/node-transformers/obfuscating-transformers/variable-declaration-transformer/fixtures/prevent-renaming-of-renamed-identifiers.js

@@ -1,6 +1,10 @@
 (function() {
     function test() {
-        function inner() {}
-        let a, b, c, d;
+        var inner = 1;
+
+        function a () {}
+        function b () {}
+        function c () {}
+        function d () {}
     }
 })();

이 변경점에서 너무 많은 파일들이 변경되어 몇몇 파일들은 표시되지 않았습니다.