ソースを参照

Added more tests for identifier names cache code generation

sanex 4 年 前
コミット
e7d9c71964

+ 109 - 1
test/functional-tests/javascript-obfuscator/JavaScriptObfuscator.spec.ts

@@ -23,6 +23,7 @@ import { OptionsPreset } from '../../../src/enums/options/presets/OptionsPreset'
 import { buildLargeCode } from '../../helpers/buildLargeCode';
 import { getRegExpMatch } from '../../helpers/getRegExpMatch';
 import { readFileAsString } from '../../helpers/readFileAsString';
+import { TIdentifierNamesCache } from '../../../src/types/storages/TIdentifierNamesCache';
 
 describe('JavaScriptObfuscator', () => {
     describe('obfuscate', () => {
@@ -886,7 +887,7 @@ describe('JavaScriptObfuscator', () => {
                     obfuscatedCode = JavaScriptObfuscator.obfuscate(code).getObfuscatedCode();
                 });
 
-                it('Match #!: should correctly obfuscate a import', () => {
+                it('Match #1: should correctly obfuscate a import', () => {
                     assert.match(obfuscatedCode, importRegExp);
                 });
 
@@ -912,6 +913,113 @@ describe('JavaScriptObfuscator', () => {
             });
         });
 
+        describe('identifier names cache generation', () => {
+            describe('Variant #1: `identifierNamesCache` and `renameGlobal` options are enabled. Existing cache is passed', () => {
+                const expectedIdentifierNamesCache: TIdentifierNamesCache = {
+                    foo: 'a',
+                    bar: 'b',
+                    baz: 'baz_value_from_cache'
+                };
+
+                let identifierNamesCache: TIdentifierNamesCache;
+
+                beforeEach(() => {
+                    const code: string = readFileAsString(__dirname + '/fixtures/identifier-names-cache-1.js');
+
+                    identifierNamesCache = JavaScriptObfuscator.obfuscate(
+                        code,
+                        {
+                            ...NO_ADDITIONAL_NODES_PRESET,
+                            renameGlobals: true,
+                            identifierNamesCache: {
+                                baz: 'baz_value_from_cache'
+                            },
+                            identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator
+                        }
+                    ).getIdentifierNamesCache();
+                });
+
+                it('Match #1: should correctly generate identifier names cache', () => {
+                    assert.deepEqual(identifierNamesCache, expectedIdentifierNamesCache);
+                });
+            });
+
+            describe('Variant #2: `identifierNamesCache` and `renameGlobal` options are enabled', () => {
+                const expectedIdentifierNamesCache: TIdentifierNamesCache = {
+                    foo: 'a',
+                    bar: 'b'
+                };
+
+                let identifierNamesCache: TIdentifierNamesCache;
+
+                beforeEach(() => {
+                    const code: string = readFileAsString(__dirname + '/fixtures/identifier-names-cache-1.js');
+
+                    identifierNamesCache = JavaScriptObfuscator.obfuscate(
+                        code,
+                        {
+                            ...NO_ADDITIONAL_NODES_PRESET,
+                            renameGlobals: true,
+                            identifierNamesCache: {},
+                            identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator
+                        }
+                    ).getIdentifierNamesCache();
+                });
+
+                it('Match #1: should correctly generate identifier names cache', () => {
+                    assert.deepEqual(identifierNamesCache, expectedIdentifierNamesCache);
+                });
+            });
+
+            describe('Variant #3: `identifierNamesCache` and `renameGlobal` options are enabled. Source code without global variables', () => {
+                const expectedIdentifierNamesCache: TIdentifierNamesCache = {};
+
+                let identifierNamesCache: TIdentifierNamesCache;
+
+                beforeEach(() => {
+                    const code: string = readFileAsString(__dirname + '/fixtures/identifier-names-cache-2.js');
+
+                    identifierNamesCache = JavaScriptObfuscator.obfuscate(
+                        code,
+                        {
+                            ...NO_ADDITIONAL_NODES_PRESET,
+                            renameGlobals: true,
+                            identifierNamesCache: {},
+                            identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator
+                        }
+                    ).getIdentifierNamesCache();
+                });
+
+                it('Match #1: should correctly generate identifier names cache', () => {
+                    assert.deepEqual(identifierNamesCache, expectedIdentifierNamesCache);
+                });
+            });
+
+            describe('Variant #4: `identifierNamesCache` option is disabled', () => {
+                const expectedIdentifierNamesCache: TIdentifierNamesCache = null;
+
+                let identifierNamesCache: TIdentifierNamesCache;
+
+                beforeEach(() => {
+                    const code: string = readFileAsString(__dirname + '/fixtures/identifier-names-cache-1.js');
+
+                    identifierNamesCache = JavaScriptObfuscator.obfuscate(
+                        code,
+                        {
+                            ...NO_ADDITIONAL_NODES_PRESET,
+                            renameGlobals: true,
+                            identifierNamesCache: null,
+                            identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator
+                        }
+                    ).getIdentifierNamesCache();
+                });
+
+                it('Match #1: should correctly generate identifier names cache', () => {
+                    assert.deepEqual(identifierNamesCache, expectedIdentifierNamesCache);
+                });
+            });
+        });
+
         describe('3.5k variables', function () {
             this.timeout(200000);
 

+ 9 - 0
test/functional-tests/javascript-obfuscator/fixtures/identifier-names-cache-1.js

@@ -0,0 +1,9 @@
+function foo(arg) {
+    console.log(arg)
+}
+
+function bar() {
+    var bark = 2;
+}
+
+baz();

+ 9 - 0
test/functional-tests/javascript-obfuscator/fixtures/identifier-names-cache-2.js

@@ -0,0 +1,9 @@
+(function () {
+    function foo(arg) {
+        console.log(arg)
+    }
+
+    function bar() {
+        var bark = 2;
+    }
+})();

+ 135 - 31
test/functional-tests/node-transformers/rename-identifiers-transformers/scope-through-identifiers-transformer/variable-declaration/VariableDeclaration.spec.ts

@@ -38,51 +38,155 @@ describe('ScopeThroughIdentifiersTransformer VariableDeclaration identifiers', (
     });
 
     describe('Variant #2: variable declaration is missing', () => {
-        describe('Variant #1: transformation of variable reference identifier node name based on identifier names cache', () => {
-            const variableReferenceIdentifierRegExp: RegExp = /foo_from_cache;/;
+        describe('Variant #1: global variable reference scope', () => {
+            describe('Variant #1: identifier names cache value is exists', () => {
+                const variableReferenceIdentifierRegExp: RegExp = /foo_from_cache;/;
+
+                let obfuscatedCode: string;
+
+                before(() => {
+                    const code: string = readFileAsString(__dirname + '/fixtures/variable-reference-without-declaration-global-scope.js');
+
+                    obfuscatedCode = JavaScriptObfuscator.obfuscate(
+                        code,
+                        {
+                            ...NO_ADDITIONAL_NODES_PRESET,
+                            renameGlobals: true,
+                            identifierNamesCache: {
+                                'foo': 'foo_from_cache'
+                            }
+                        }
+                    ).getObfuscatedCode();
+                });
+
+                it('should transform variable reference name', () => {
+                    assert.match(obfuscatedCode, variableReferenceIdentifierRegExp);
+                });
+            });
+
+            describe('Variant #2: ignore transformation of variable reference identifier node name when no identifier names cache value is available', () => {
+                const variableReferenceIdentifierRegExp: RegExp = /foo;/;
 
-            let obfuscatedCode: string;
+                let obfuscatedCode: string;
 
-            before(() => {
-                const code: string = readFileAsString(__dirname + '/fixtures/variable-reference-without-declaration.js');
+                before(() => {
+                    const code: string = readFileAsString(__dirname + '/fixtures/variable-reference-without-declaration-global-scope.js');
 
-                obfuscatedCode = JavaScriptObfuscator.obfuscate(
-                    code,
-                    {
-                        ...NO_ADDITIONAL_NODES_PRESET,
-                        renameGlobals: true,
-                        identifierNamesCache: {
-                            'foo': 'foo_from_cache'
+                    obfuscatedCode = JavaScriptObfuscator.obfuscate(
+                        code,
+                        {
+                            ...NO_ADDITIONAL_NODES_PRESET,
+                            renameGlobals: true,
+                            identifierNamesCache: {}
                         }
-                    }
-                ).getObfuscatedCode();
+                    ).getObfuscatedCode();
+                });
+
+                it('should not transform variable reference name', () => {
+                    assert.match(obfuscatedCode, variableReferenceIdentifierRegExp);
+                });
             });
 
-            it('should transform variable reference name', () => {
-                assert.match(obfuscatedCode, variableReferenceIdentifierRegExp);
+            describe('Variant #3: ignore transformation of variable reference identifier node name when this name is reserved', () => {
+                const variableReferenceIdentifierRegExp: RegExp = /foo;/;
+
+                let obfuscatedCode: string;
+
+                before(() => {
+                    const code: string = readFileAsString(__dirname + '/fixtures/variable-reference-without-declaration-global-scope.js');
+
+                    obfuscatedCode = JavaScriptObfuscator.obfuscate(
+                        code,
+                        {
+                            ...NO_ADDITIONAL_NODES_PRESET,
+                            renameGlobals: true,
+                            identifierNamesCache: {
+                                'foo': 'foo_from_cache'
+                            },
+                            reservedNames: ['^foo$']
+                        }
+                    ).getObfuscatedCode();
+                });
+
+                it('should not transform variable reference name', () => {
+                    assert.match(obfuscatedCode, variableReferenceIdentifierRegExp);
+                });
             });
         });
 
-        describe('Variant #2: ignore transformation of variable reference identifier node name when no identifier names cache value is available', () => {
-            const variableReferenceIdentifierRegExp: RegExp = /foo;/;
+        describe('Variant #2: local variable reference scope', () => {
+            describe('Variant #1: identifier names cache value is exists', () => {
+                const variableReferenceIdentifierRegExp: RegExp = /foo_from_cache;/;
 
-            let obfuscatedCode: string;
+                let obfuscatedCode: string;
 
-            before(() => {
-                const code: string = readFileAsString(__dirname + '/fixtures/variable-reference-without-declaration.js');
+                before(() => {
+                    const code: string = readFileAsString(__dirname + '/fixtures/variable-reference-without-declaration-local-scope.js');
 
-                obfuscatedCode = JavaScriptObfuscator.obfuscate(
-                    code,
-                    {
-                        ...NO_ADDITIONAL_NODES_PRESET,
-                        renameGlobals: true,
-                        identifierNamesCache: {}
-                    }
-                ).getObfuscatedCode();
+                    obfuscatedCode = JavaScriptObfuscator.obfuscate(
+                        code,
+                        {
+                            ...NO_ADDITIONAL_NODES_PRESET,
+                            renameGlobals: true,
+                            identifierNamesCache: {
+                                'foo': 'foo_from_cache'
+                            }
+                        }
+                    ).getObfuscatedCode();
+                });
+
+                it('should transform variable reference name', () => {
+                    assert.match(obfuscatedCode, variableReferenceIdentifierRegExp);
+                });
             });
 
-            it('should not transform variable reference name', () => {
-                assert.match(obfuscatedCode, variableReferenceIdentifierRegExp);
+            describe('Variant #2: ignore transformation of variable reference identifier node name when no identifier names cache value is available', () => {
+                const variableReferenceIdentifierRegExp: RegExp = /foo;/;
+
+                let obfuscatedCode: string;
+
+                before(() => {
+                    const code: string = readFileAsString(__dirname + '/fixtures/variable-reference-without-declaration-local-scope.js');
+
+                    obfuscatedCode = JavaScriptObfuscator.obfuscate(
+                        code,
+                        {
+                            ...NO_ADDITIONAL_NODES_PRESET,
+                            renameGlobals: true,
+                            identifierNamesCache: {}
+                        }
+                    ).getObfuscatedCode();
+                });
+
+                it('should not transform variable reference name', () => {
+                    assert.match(obfuscatedCode, variableReferenceIdentifierRegExp);
+                });
+            });
+
+            describe('Variant #3 ignore transformation of variable reference identifier node name when this name is reserved', () => {
+                const variableReferenceIdentifierRegExp: RegExp = /foo;/;
+
+                let obfuscatedCode: string;
+
+                before(() => {
+                    const code: string = readFileAsString(__dirname + '/fixtures/variable-reference-without-declaration-local-scope.js');
+
+                    obfuscatedCode = JavaScriptObfuscator.obfuscate(
+                        code,
+                        {
+                            ...NO_ADDITIONAL_NODES_PRESET,
+                            renameGlobals: true,
+                            identifierNamesCache: {
+                                'foo': 'foo_from_cache'
+                            },
+                            reservedNames: ['^foo$']
+                        }
+                    ).getObfuscatedCode();
+                });
+
+                it('should not transform variable reference name', () => {
+                    assert.match(obfuscatedCode, variableReferenceIdentifierRegExp);
+                });
             });
         });
     });

+ 0 - 0
test/functional-tests/node-transformers/rename-identifiers-transformers/scope-through-identifiers-transformer/variable-declaration/fixtures/variable-reference-without-declaration.js → test/functional-tests/node-transformers/rename-identifiers-transformers/scope-through-identifiers-transformer/variable-declaration/fixtures/variable-reference-without-declaration-global-scope.js


+ 5 - 0
test/functional-tests/node-transformers/rename-identifiers-transformers/scope-through-identifiers-transformer/variable-declaration/fixtures/variable-reference-without-declaration-local-scope.js

@@ -0,0 +1,5 @@
+(function () {
+    function bar() {
+        foo;
+    }
+})();