diff --git a/test/functional-tests/javascript-obfuscator/JavaScriptObfuscator.spec.ts b/test/functional-tests/javascript-obfuscator/JavaScriptObfuscator.spec.ts index 1d2ef3ee..d4fa2131 100644 --- a/test/functional-tests/javascript-obfuscator/JavaScriptObfuscator.spec.ts +++ b/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); diff --git a/test/functional-tests/javascript-obfuscator/fixtures/identifier-names-cache-1.js b/test/functional-tests/javascript-obfuscator/fixtures/identifier-names-cache-1.js new file mode 100644 index 00000000..34d562bb --- /dev/null +++ b/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(); \ No newline at end of file diff --git a/test/functional-tests/javascript-obfuscator/fixtures/identifier-names-cache-2.js b/test/functional-tests/javascript-obfuscator/fixtures/identifier-names-cache-2.js new file mode 100644 index 00000000..fdbc0c40 --- /dev/null +++ b/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; + } +})(); \ No newline at end of file diff --git a/test/functional-tests/node-transformers/rename-identifiers-transformers/scope-through-identifiers-transformer/variable-declaration/VariableDeclaration.spec.ts b/test/functional-tests/node-transformers/rename-identifiers-transformers/scope-through-identifiers-transformer/variable-declaration/VariableDeclaration.spec.ts index 494d4bef..b4cece50 100644 --- a/test/functional-tests/node-transformers/rename-identifiers-transformers/scope-through-identifiers-transformer/variable-declaration/VariableDeclaration.spec.ts +++ b/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; + 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: { + 'foo': 'foo_from_cache' + } } - } - ).getObfuscatedCode(); + ).getObfuscatedCode(); + }); + + it('should transform variable reference name', () => { + assert.match(obfuscatedCode, variableReferenceIdentifierRegExp); + }); }); - 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; + + 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: {} + } + ).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-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); + }); }); }); }); diff --git a/test/functional-tests/node-transformers/rename-identifiers-transformers/scope-through-identifiers-transformer/variable-declaration/fixtures/variable-reference-without-declaration.js b/test/functional-tests/node-transformers/rename-identifiers-transformers/scope-through-identifiers-transformer/variable-declaration/fixtures/variable-reference-without-declaration-global-scope.js similarity index 100% rename from test/functional-tests/node-transformers/rename-identifiers-transformers/scope-through-identifiers-transformer/variable-declaration/fixtures/variable-reference-without-declaration.js rename to test/functional-tests/node-transformers/rename-identifiers-transformers/scope-through-identifiers-transformer/variable-declaration/fixtures/variable-reference-without-declaration-global-scope.js diff --git a/test/functional-tests/node-transformers/rename-identifiers-transformers/scope-through-identifiers-transformer/variable-declaration/fixtures/variable-reference-without-declaration-local-scope.js b/test/functional-tests/node-transformers/rename-identifiers-transformers/scope-through-identifiers-transformer/variable-declaration/fixtures/variable-reference-without-declaration-local-scope.js new file mode 100644 index 00000000..9ffdf5bf --- /dev/null +++ b/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; + } +})(); \ No newline at end of file