Added basic tests for mangled lexical scope identifiers generation

This commit is contained in:
sanex3339
2020-02-12 22:51:59 +03:00
parent 7ff6721093
commit eb233be65a
2 changed files with 153 additions and 0 deletions
@@ -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);
});
});
});
});
});
@@ -0,0 +1,3 @@
var foo = 'abc';
function bar (bar1, bar2) {}
function baz (baz1, baz2) {}