Browse Source

Added more tests of identifier names conflicts when using `identifiersPrefix`

sanex3339 5 years ago
parent
commit
029d5953c2

+ 7 - 3
test/dev/dev.ts

@@ -7,15 +7,19 @@ import { NO_ADDITIONAL_NODES_PRESET } from '../../src/options/presets/NoCustomNo
 
     let obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
         `
-            const ab = 'abc';
+            function foo () {
+                const testA = 'abc';
+                const testB = 'abc';
+                const testC = 'abc';
+                const testD = 'abc';
+            }
         `,
         {
             ...NO_ADDITIONAL_NODES_PRESET,
             compact: false,
             identifierNamesGenerator: 'dictionary',
-            identifiersDictionary: ['a', 'b'],
+            identifiersDictionary: ['a', 'b', 'aa'],
             identifiersPrefix: 'a',
-            transformObjectKeys: true,
             stringArray: true,
             stringArrayThreshold: 1
         }

+ 86 - 0
test/functional-tests/generators/identifier-names-generators/dictionary-identifier-names-generator/DictionaryIdentifierNamesGenerator.spec.ts

@@ -5,6 +5,7 @@ import { IdentifierNamesGenerator } from '../../../../../src/enums/generators/id
 import { NO_ADDITIONAL_NODES_PRESET } from '../../../../../src/options/presets/NoCustomNodes';
 
 import { readFileAsString } from '../../../../helpers/readFileAsString';
+import { getRegExpMatch } from '../../../../helpers/getRegExpMatch';
 
 import { JavaScriptObfuscator } from '../../../../../src/JavaScriptObfuscatorFacade';
 
@@ -76,5 +77,90 @@ describe('DictionaryIdentifierNamesGenerator', () => {
                 });
             });
         });
+
+        describe('Variant #2: should not generate same prefixed name for identifier in code as prefixed name of string array', function () {
+            this.timeout(10000);
+
+            const samplesCount: number = 20;
+
+            describe('Variant #1: `renameGlobals` option is disabled', () => {
+                const stringArrayStorageRegExp: RegExp = /const ([abAB]{1,3}) *= *\['first', *'abc'];/;
+                const variableDeclarationIdentifierNameRegExp: RegExp = /const ([abAB]{1,3}){1,2} *= *[abAB]{1,3}\('0x0'\);/;
+
+                let isIdentifiersAreConflicted: boolean = false;
+
+                before(() => {
+                    const code: string = readFileAsString(__dirname + '/fixtures/string-array-storage-name-conflict-2.js');
+
+                    for (let i = 0; i < samplesCount; i++) {
+                        const obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
+                            code,
+                            {
+                                ...NO_ADDITIONAL_NODES_PRESET,
+                                identifierNamesGenerator: IdentifierNamesGenerator.DictionaryIdentifierNamesGenerator,
+                                identifiersDictionary: ['a', 'b', 'aa'],
+                                identifiersPrefix: 'a',
+                                transformObjectKeys: true,
+                                stringArray: true,
+                                stringArrayThreshold: 1
+                            }
+                        ).getObfuscatedCode();
+
+                        const stringArrayStorageName: string = getRegExpMatch(obfuscatedCode, stringArrayStorageRegExp);
+                        const variableDeclarationIdentifierName: string = getRegExpMatch(obfuscatedCode, variableDeclarationIdentifierNameRegExp);
+
+                        if (stringArrayStorageName === variableDeclarationIdentifierName) {
+                            isIdentifiersAreConflicted = true;
+
+                            break;
+                        }
+                    }
+                });
+
+                it('Should not generate same name for string array and last variable', () => {
+                    assert.equal(isIdentifiersAreConflicted, false);
+                });
+            });
+
+            describe('Variant #2: `renameGlobals` option is enabled', () => {
+                const stringArrayStorageRegExp: RegExp = /const ([abAB]{1,3}) *= *\['first', *'abc'];/;
+                const variableDeclarationIdentifierNameRegExp: RegExp = /const ([abAB]{1,3}){1,2} *= *[abAB]{1,3}\('0x0'\);/;
+
+                let isIdentifiersAreConflicted: boolean = false;
+
+                before(() => {
+                    const code: string = readFileAsString(__dirname + '/fixtures/string-array-storage-name-conflict-2.js');
+
+                    for (let i = 0; i < samplesCount; i++) {
+                        const obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
+                            code,
+                            {
+                                ...NO_ADDITIONAL_NODES_PRESET,
+                                identifierNamesGenerator: IdentifierNamesGenerator.DictionaryIdentifierNamesGenerator,
+                                identifiersDictionary: ['a', 'b', 'aa'],
+                                identifiersPrefix: 'a',
+                                renameGlobals: true,
+                                transformObjectKeys: true,
+                                stringArray: true,
+                                stringArrayThreshold: 1
+                            }
+                        ).getObfuscatedCode();
+
+                        const stringArrayStorageName: string = getRegExpMatch(obfuscatedCode, stringArrayStorageRegExp);
+                        const variableDeclarationIdentifierName: string = getRegExpMatch(obfuscatedCode, variableDeclarationIdentifierNameRegExp);
+
+                        if (stringArrayStorageName === variableDeclarationIdentifierName) {
+                            isIdentifiersAreConflicted = true;
+
+                            break;
+                        }
+                    }
+                });
+
+                it('Should not generate same name for string array and last variable', () => {
+                    assert.equal(isIdentifiersAreConflicted, false);
+                });
+            });
+        });
     });
 });

+ 5 - 0
test/functional-tests/generators/identifier-names-generators/dictionary-identifier-names-generator/fixtures/string-array-storage-name-conflict-2.js

@@ -0,0 +1,5 @@
+function foo () {
+    const testA = 'first';
+    const testB = 'abc';
+    const testC = 'abc';
+}

+ 65 - 0
test/functional-tests/generators/identifier-names-generators/mangled-identifier-names-generator/MangledIdentifierNamesGenerator.spec.ts

@@ -74,5 +74,70 @@ describe('MangledIdentifierNamesGenerator', () => {
                 });
             });
         });
+
+        describe('Variant #2: should not generate same prefixed name for identifier in code as prefixed name of string array', () => {
+            describe('Variant #1: `renameGlobals` option is disabled', () => {
+                const stringArrayStorageRegExp: RegExp = /const aa *= *\['abc', *'last'];/;
+                const lastVariableDeclarationIdentifierNameRegExp: RegExp = /const ac *= *ab\('0x1'\);/;
+
+                let obfuscatedCode: string;
+
+                before(() => {
+                    const code: string = readFileAsString(__dirname + '/fixtures/string-array-storage-name-conflict-2.js');
+
+                    obfuscatedCode = JavaScriptObfuscator.obfuscate(
+                        code,
+                        {
+                            ...NO_ADDITIONAL_NODES_PRESET,
+                            identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator,
+                            identifiersPrefix: 'a',
+                            transformObjectKeys: true,
+                            stringArray: true,
+                            stringArrayThreshold: 1
+                        }
+                    ).getObfuscatedCode();
+                });
+
+                it('Match #1: should generate correct identifier for string array', () => {
+                    assert.match(obfuscatedCode, stringArrayStorageRegExp);
+                });
+
+                it('Match #2: should keep identifier name for last variable declaration', () => {
+                    assert.match(obfuscatedCode, lastVariableDeclarationIdentifierNameRegExp);
+                });
+            });
+
+            describe('Variant #2: `renameGlobals` option is enabled', () => {
+                const stringArrayStorageRegExp: RegExp = /const aa *= *\['abc', *'last'];/;
+                const lastVariableDeclarationIdentifierNameRegExp: RegExp = /const ae *= *ab\('0x1'\);/;
+
+                let obfuscatedCode: string;
+
+                before(() => {
+                    const code: string = readFileAsString(__dirname + '/fixtures/string-array-storage-name-conflict-2.js');
+
+                    obfuscatedCode = JavaScriptObfuscator.obfuscate(
+                        code,
+                        {
+                            ...NO_ADDITIONAL_NODES_PRESET,
+                            identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator,
+                            identifiersPrefix: 'a',
+                            renameGlobals: true,
+                            transformObjectKeys: true,
+                            stringArray: true,
+                            stringArrayThreshold: 1
+                        }
+                    ).getObfuscatedCode();
+                });
+
+                it('Match #1: should generate correct identifier for string array', () => {
+                    assert.match(obfuscatedCode, stringArrayStorageRegExp);
+                });
+
+                it('Match #2: should keep identifier name for last variable declaration', () => {
+                    assert.match(obfuscatedCode, lastVariableDeclarationIdentifierNameRegExp);
+                });
+            });
+        });
     });
 });

+ 63 - 0
test/functional-tests/generators/identifier-names-generators/mangled-identifier-names-generator/fixtures/string-array-storage-name-conflict-2.js

@@ -0,0 +1,63 @@
+function foo () {
+    const testA = 'abc';
+    const testB = 'abc';
+    const testC = 'abc';
+    const testD = 'abc';
+    const testE = 'abc';
+    const testF = 'abc';
+    const testG = 'abc';
+    const testH = 'abc';
+    const testI = 'abc';
+    const testJ = 'abc';
+    const testK = 'abc';
+    const testL = 'abc';
+    const testM = 'abc';
+    const testN = 'abc';
+    const testO = 'abc';
+    const testP = 'abc';
+    const testQ = 'abc';
+    const testR = 'abc';
+    const testS = 'abc';
+    const testT = 'abc';
+    const testU = 'abc';
+    const testV = 'abc';
+    const testW = 'abc';
+    const testX = 'abc';
+    const testY = 'abc';
+    const testZ = 'abc';
+    const test1 = 'abc';
+    const test2 = 'abc';
+    const test3 = 'abc';
+    const test4 = 'abc';
+    const test5 = 'abc';
+    const test6 = 'abc';
+    const test7 = 'abc';
+    const test8 = 'abc';
+    const test9 = 'abc';
+    const test10 = 'abc';
+    const test11 = 'abc';
+    const test12 = 'abc';
+    const test13 = 'abc';
+    const test14 = 'abc';
+    const test15 = 'abc';
+    const test16 = 'abc';
+    const test17 = 'abc';
+    const test18 = 'abc';
+    const test19 = 'abc';
+    const test20 = 'abc';
+    const test21 = 'abc';
+    const test22 = 'abc';
+    const test23 = 'abc';
+    const test24 = 'abc';
+    const test25 = 'abc';
+    const test26 = 'abc';
+    const test27 = 'abc';
+    const test28 = 'abc';
+    const test29 = 'abc';
+    const test30 = 'abc';
+    const test31 = 'abc';
+    const test32 = 'abc';
+    const test33 = 'abc';
+    const test34 = 'abc';
+    const test35 = 'last';
+}