Added test for dead code identifiers conflict

This commit is contained in:
sanex3339
2018-01-17 00:22:43 +03:00
parent 87a4638159
commit 4f4f95b73f
2 changed files with 66 additions and 0 deletions
@@ -4,6 +4,9 @@ import { IObfuscationResult } from '../../../../src/interfaces/IObfuscationResul
import { NO_ADDITIONAL_NODES_PRESET } from '../../../../src/options/presets/NoCustomNodes';
import { IdentifierNamesGenerator } from '../../../../src/enums/generators/identifier-names-generators/IdentifierNamesGenerator';
import { getRegExpMatch } from '../../../helpers/getRegExpMatch';
import { readFileAsString } from '../../../helpers/readFileAsString';
import { JavaScriptObfuscator } from '../../../../src/JavaScriptObfuscatorFacade';
@@ -436,5 +439,47 @@ describe('DeadCodeInjectionTransformer', () => {
assert.notInclude(obfuscatedCode, variableName);
});
});
describe('variant #10 - unique names for dead code identifiers', () => {
const deadCodeMatch: string = `` +
`if *\\(.*?\\) *{` +
`var *(\\w).*?;` +
`} *else *{` +
`return *(\\w).*?;` +
`}` +
``;
const deadCodeRegExp: RegExp = new RegExp(deadCodeMatch);
let returnIdentifierName: string | null,
variableDeclarationIdentifierName: string | null,
obfuscatedCode: string;
before(() => {
const code: string = readFileAsString(__dirname + '/fixtures/unique-names-for-dead-code-identifiers.js');
const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
code,
{
...NO_ADDITIONAL_NODES_PRESET,
deadCodeInjection: true,
deadCodeInjectionThreshold: 1,
identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator,
seed: 1
}
);
obfuscatedCode = obfuscationResult.getObfuscatedCode();
variableDeclarationIdentifierName = getRegExpMatch(obfuscatedCode, deadCodeRegExp, 0);
returnIdentifierName = getRegExpMatch(obfuscatedCode, deadCodeRegExp, 1);
});
it('should correctly add dead code', () => {
assert.match(obfuscatedCode, deadCodeRegExp);
});
it('should generate separate identifiers for common AST and dead code', () => {
assert.notEqual(returnIdentifierName, variableDeclarationIdentifierName);
});
});
});
});
@@ -0,0 +1,21 @@
(function(variable){
function foo () {
return variable.push(1);
}
function bar () {
var variable = 1;
}
function baz() {
var variable = 2;
}
function bark() {
var variable = 3;
}
function hawk() {
var variable = 4;
}
})([]);