mirror of
https://github.com/javascript-obfuscator/javascript-obfuscator
synced 2026-08-09 12:42:29 +00:00
Added test for dead code identifiers conflict
This commit is contained in:
+45
@@ -4,6 +4,9 @@ import { IObfuscationResult } from '../../../../src/interfaces/IObfuscationResul
|
|||||||
|
|
||||||
import { NO_ADDITIONAL_NODES_PRESET } from '../../../../src/options/presets/NoCustomNodes';
|
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 { readFileAsString } from '../../../helpers/readFileAsString';
|
||||||
|
|
||||||
import { JavaScriptObfuscator } from '../../../../src/JavaScriptObfuscatorFacade';
|
import { JavaScriptObfuscator } from '../../../../src/JavaScriptObfuscatorFacade';
|
||||||
@@ -436,5 +439,47 @@ describe('DeadCodeInjectionTransformer', () => {
|
|||||||
assert.notInclude(obfuscatedCode, variableName);
|
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);
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
+21
@@ -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;
|
||||||
|
}
|
||||||
|
})([]);
|
||||||
Reference in New Issue
Block a user