From 029d5953c22323d582386f809c2be065843ba12b Mon Sep 17 00:00:00 2001 From: sanex3339 Date: Sun, 9 Feb 2020 17:14:53 +0300 Subject: [PATCH] Added more tests of identifier names conflicts when using `identifiersPrefix` --- test/dev/dev.ts | 10 ++- ...DictionaryIdentifierNamesGenerator.spec.ts | 86 +++++++++++++++++++ .../string-array-storage-name-conflict-2.js | 5 ++ .../MangledIdentifierNamesGenerator.spec.ts | 65 ++++++++++++++ .../string-array-storage-name-conflict-2.js | 63 ++++++++++++++ 5 files changed, 226 insertions(+), 3 deletions(-) create mode 100644 test/functional-tests/generators/identifier-names-generators/dictionary-identifier-names-generator/fixtures/string-array-storage-name-conflict-2.js create mode 100644 test/functional-tests/generators/identifier-names-generators/mangled-identifier-names-generator/fixtures/string-array-storage-name-conflict-2.js diff --git a/test/dev/dev.ts b/test/dev/dev.ts index 12b641d0..08959c4e 100644 --- a/test/dev/dev.ts +++ b/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 } diff --git a/test/functional-tests/generators/identifier-names-generators/dictionary-identifier-names-generator/DictionaryIdentifierNamesGenerator.spec.ts b/test/functional-tests/generators/identifier-names-generators/dictionary-identifier-names-generator/DictionaryIdentifierNamesGenerator.spec.ts index da5abb8b..579b3e9d 100644 --- a/test/functional-tests/generators/identifier-names-generators/dictionary-identifier-names-generator/DictionaryIdentifierNamesGenerator.spec.ts +++ b/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); + }); + }); + }); }); }); diff --git a/test/functional-tests/generators/identifier-names-generators/dictionary-identifier-names-generator/fixtures/string-array-storage-name-conflict-2.js b/test/functional-tests/generators/identifier-names-generators/dictionary-identifier-names-generator/fixtures/string-array-storage-name-conflict-2.js new file mode 100644 index 00000000..743b63e1 --- /dev/null +++ b/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'; +} \ No newline at end of file diff --git a/test/functional-tests/generators/identifier-names-generators/mangled-identifier-names-generator/MangledIdentifierNamesGenerator.spec.ts b/test/functional-tests/generators/identifier-names-generators/mangled-identifier-names-generator/MangledIdentifierNamesGenerator.spec.ts index 9aec988a..7a9c1e4b 100644 --- a/test/functional-tests/generators/identifier-names-generators/mangled-identifier-names-generator/MangledIdentifierNamesGenerator.spec.ts +++ b/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); + }); + }); + }); }); }); diff --git a/test/functional-tests/generators/identifier-names-generators/mangled-identifier-names-generator/fixtures/string-array-storage-name-conflict-2.js b/test/functional-tests/generators/identifier-names-generators/mangled-identifier-names-generator/fixtures/string-array-storage-name-conflict-2.js new file mode 100644 index 00000000..8e623766 --- /dev/null +++ b/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'; +} \ No newline at end of file