From eb233be65a6a7189278a259115ad5ef207b3c2be Mon Sep 17 00:00:00 2001 From: sanex3339 Date: Wed, 12 Feb 2020 22:51:59 +0300 Subject: [PATCH] Added basic tests for mangled lexical scope identifiers generation --- .../MangledIdentifierNamesGenerator.spec.ts | 150 ++++++++++++++++++ .../lexical-block-scope-identifiers-1.js | 3 + 2 files changed, 153 insertions(+) create mode 100644 test/functional-tests/generators/identifier-names-generators/mangled-identifier-names-generator/fixtures/lexical-block-scope-identifiers-1.js 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 c9fb6ffa..49a2e438 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 @@ -150,4 +150,154 @@ describe('MangledIdentifierNamesGenerator', () => { }); }); }); + + describe('generateForLexicalScope', () => { + describe('Variant #1: Should generate different names set for different lexical scopes', () => { + describe('Variant #1: `renameGlobals` option is disabled', () => { + const variableIdentifierRegExp: RegExp = /var foo *= *'abc';/; + const functionDeclarationRegExp1: RegExp = /function bar *\(a, *b\) *{}/; + const functionDeclarationRegExp2: RegExp = /function baz *\(a, *b\) *{}/; + + let obfuscatedCode: string; + + before(() => { + const code: string = readFileAsString(__dirname + '/fixtures/lexical-block-scope-identifiers-1.js'); + + obfuscatedCode = JavaScriptObfuscator.obfuscate( + code, + { + ...NO_ADDITIONAL_NODES_PRESET, + identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator + } + ).getObfuscatedCode(); + }); + + it('Match #1: should keep identifier name for variable declaration', () => { + assert.match(obfuscatedCode, variableIdentifierRegExp); + }); + + it('Match #2: should generate valid identifier names for function', () => { + assert.match(obfuscatedCode, functionDeclarationRegExp1); + }); + + it('Match #3: should generate valid identifier names for function', () => { + assert.match(obfuscatedCode, functionDeclarationRegExp2); + }); + }); + + describe('Variant #2: `renameGlobals` option is enabled', () => { + const variableIdentifierRegExp: RegExp = /var a *= *'abc';/; + const functionDeclarationRegExp1: RegExp = /function b *\(d, *e\) *{}/; + const functionDeclarationRegExp2: RegExp = /function c *\(d, *e\) *{}/; + + let obfuscatedCode: string; + + before(() => { + const code: string = readFileAsString(__dirname + '/fixtures/lexical-block-scope-identifiers-1.js'); + + obfuscatedCode = JavaScriptObfuscator.obfuscate( + code, + { + ...NO_ADDITIONAL_NODES_PRESET, + identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator, + renameGlobals: true + } + ).getObfuscatedCode(); + }); + + it('Match #1: should generate valid identifier name for variable declaration', () => { + assert.match(obfuscatedCode, variableIdentifierRegExp); + }); + + it('Match #2: should generate valid identifier names for function', () => { + assert.match(obfuscatedCode, functionDeclarationRegExp1); + }); + + it('Match #3: should generate valid identifier names for function', () => { + assert.match(obfuscatedCode, functionDeclarationRegExp2); + }); + }); + }); + + describe('Variant #2: Should generate different names set for different lexical scopes when string array is enabled', () => { + describe('Variant #1: `renameGlobals` option is disabled', () => { + const stringArrayIdentifierRegExp: RegExp = /var a *= *\['abc'];/; + const variableIdentifierRegExp: RegExp = /var foo *= *b\('0x0'\);/; + const functionDeclarationRegExp1: RegExp = /function bar *\(c, *d\) *{}/; + const functionDeclarationRegExp2: RegExp = /function baz *\(c, *d\) *{}/; + + let obfuscatedCode: string; + + before(() => { + const code: string = readFileAsString(__dirname + '/fixtures/lexical-block-scope-identifiers-1.js'); + + obfuscatedCode = JavaScriptObfuscator.obfuscate( + code, + { + ...NO_ADDITIONAL_NODES_PRESET, + identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator, + stringArray: true, + stringArrayThreshold: 1 + } + ).getObfuscatedCode(); + }); + + it('Match #1: should generate valid identifier names for string array', () => { + assert.match(obfuscatedCode, stringArrayIdentifierRegExp); + }); + + it('Match #2: should keep identifier name for variable declaration', () => { + assert.match(obfuscatedCode, variableIdentifierRegExp); + }); + + it('Match #3: should generate valid identifier names for function', () => { + assert.match(obfuscatedCode, functionDeclarationRegExp1); + }); + + it('Match #4: should generate valid identifier names for function', () => { + assert.match(obfuscatedCode, functionDeclarationRegExp2); + }); + }); + + describe('Variant #2: `renameGlobals` option is enabled', () => { + const stringArrayIdentifierRegExp: RegExp = /var a *= *\['abc'];/; + const variableIdentifierRegExp: RegExp = /var c *= *b\('0x0'\);/; + const functionDeclarationRegExp1: RegExp = /function d *\(f, *g\) *{}/; + const functionDeclarationRegExp2: RegExp = /function e *\(f, *g\) *{}/; + + let obfuscatedCode: string; + + before(() => { + const code: string = readFileAsString(__dirname + '/fixtures/lexical-block-scope-identifiers-1.js'); + + obfuscatedCode = JavaScriptObfuscator.obfuscate( + code, + { + ...NO_ADDITIONAL_NODES_PRESET, + identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator, + renameGlobals: true, + stringArray: true, + stringArrayThreshold: 1 + } + ).getObfuscatedCode(); + }); + + it('Match #1: should generate valid identifier names for string array', () => { + assert.match(obfuscatedCode, stringArrayIdentifierRegExp); + }); + + it('Match #2: should generate valid identifier name for variable declaration', () => { + assert.match(obfuscatedCode, variableIdentifierRegExp); + }); + + it('Match #3: should generate valid identifier names for function', () => { + assert.match(obfuscatedCode, functionDeclarationRegExp1); + }); + + it('Match #4: should generate valid identifier names for function', () => { + assert.match(obfuscatedCode, functionDeclarationRegExp2); + }); + }); + }); + }); }); diff --git a/test/functional-tests/generators/identifier-names-generators/mangled-identifier-names-generator/fixtures/lexical-block-scope-identifiers-1.js b/test/functional-tests/generators/identifier-names-generators/mangled-identifier-names-generator/fixtures/lexical-block-scope-identifiers-1.js new file mode 100644 index 00000000..fb355040 --- /dev/null +++ b/test/functional-tests/generators/identifier-names-generators/mangled-identifier-names-generator/fixtures/lexical-block-scope-identifiers-1.js @@ -0,0 +1,3 @@ +var foo = 'abc'; +function bar (bar1, bar2) {} +function baz (baz1, baz2) {} \ No newline at end of file