Added more tests and fixes of #217

This commit is contained in:
sanex3339
2018-03-11 01:56:16 +03:00
parent a5a4239463
commit 10144ee6e6
9 changed files with 104 additions and 14 deletions
+1 -1
View File
File diff suppressed because one or more lines are too long
@@ -146,13 +146,14 @@ export class ClassDeclarationTransformer extends AbstractNodeTransformer {
estraverse.replace(blockScopeNode, {
enter: (node: ESTree.Node, parentNode: ESTree.Node | null): void => {
if (parentNode && NodeGuards.isReplaceableIdentifierNode(node, parentNode)) {
if (parentNode && !node.obfuscatedNode && NodeGuards.isReplaceableIdentifierNode(node, parentNode)) {
const newIdentifier: ESTree.Identifier = this.identifierObfuscatingReplacer
.replace(node.name, nodeIdentifier);
const newIdentifierName: string = newIdentifier.name;
if (node.name !== newIdentifierName) {
node.name = newIdentifierName;
node.obfuscatedNode = true;
} else {
storedReplaceableIdentifiers.push(node);
}
+3 -3
View File
@@ -6,10 +6,10 @@ import { NO_ADDITIONAL_NODES_PRESET } from '../../src/options/presets/NoCustomNo
let obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
`
(function() {
(function() {
function test() {
function func1() {}
let a, b, c, d, e;
class inner {}
let a, b, c, d;
}
})();
`,
@@ -8,6 +8,7 @@ import { getRegExpMatch } from '../../../../helpers/getRegExpMatch';
import { readFileAsString } from '../../../../helpers/readFileAsString';
import { JavaScriptObfuscator } from '../../../../../src/JavaScriptObfuscatorFacade';
import { IdentifierNamesGenerator } from '../../../../../src/enums/generators/identifier-names-generators/IdentifierNamesGenerator';
describe('ClassDeclarationTransformer', () => {
describe('transformation of `classDeclaration` node names', () => {
@@ -94,5 +95,33 @@ describe('ClassDeclarationTransformer', () => {
});
});
});
describe('Variant #3: already renamed identifiers shouldn\'t be renamed twice', () => {
const classDeclarationRegExp: RegExp = /class *d *{/;
const variableDeclarationsRegExp: RegExp = /let *e, *f, *g, *h;/;
let obfuscatedCode: string;
before(() => {
const code: string = readFileAsString(__dirname + '/fixtures/prevent-renaming-of-renamed-identifiers.js');
const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
code,
{
...NO_ADDITIONAL_NODES_PRESET,
identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator
}
);
obfuscatedCode = obfuscationResult.getObfuscatedCode();
});
it('Match #1: shouldn\'t rename twice class declaration name', () => {
assert.match(obfuscatedCode, classDeclarationRegExp);
});
it('Match #2: should correctly rename variable declarations', () => {
assert.match(obfuscatedCode, variableDeclarationsRegExp);
});
});
});
});
@@ -0,0 +1,6 @@
(function() {
function test() {
class inner {}
let a, b, c, d;
}
})();
@@ -8,6 +8,7 @@ import { getRegExpMatch } from '../../../../helpers/getRegExpMatch';
import { readFileAsString } from '../../../../helpers/readFileAsString';
import { JavaScriptObfuscator } from '../../../../../src/JavaScriptObfuscatorFacade';
import { IdentifierNamesGenerator } from '../../../../../src/enums/generators/identifier-names-generators/IdentifierNamesGenerator';
describe('FunctionDeclarationTransformer', () => {
describe('transformation of `functionDeclaration` node names', () => {
@@ -148,5 +149,33 @@ describe('FunctionDeclarationTransformer', () => {
assert.equal(functionNameIdentifier, functionCallIdentifier);
});
});
describe('Variant #5: already renamed identifiers shouldn\'t be renamed twice', () => {
const functionDeclarationRegExp: RegExp = /function *d\(\) *{/;
const variableDeclarationsRegExp: RegExp = /let *e, *f, *g, *h;/;
let obfuscatedCode: string;
before(() => {
const code: string = readFileAsString(__dirname + '/fixtures/prevent-renaming-of-renamed-identifiers.js');
const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
code,
{
...NO_ADDITIONAL_NODES_PRESET,
identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator
}
);
obfuscatedCode = obfuscationResult.getObfuscatedCode();
});
it('Match #1: shouldn\'t rename twice function declaration name', () => {
assert.match(obfuscatedCode, functionDeclarationRegExp);
});
it('Match #2: should correctly rename variable declarations', () => {
assert.match(obfuscatedCode, variableDeclarationsRegExp);
});
});
});
});
@@ -0,0 +1,6 @@
(function() {
function test() {
function inner() {}
let a, b, c, d;
}
})();
@@ -438,9 +438,12 @@ describe('VariableDeclarationTransformer', () => {
});
});
describe('Variant #13: renamed identifiers shouldn\'t be renamed twice', () => {
const functionRegExp: RegExp = /function *d\(\) *{/;
const variableDeclarationsRegExp: RegExp = /let *e, *f, *g, *h;/;
describe('Variant #13: already renamed identifiers shouldn\'t be renamed twice', () => {
const variableDeclarationRegExp: RegExp = /var *d *= *0x1;/;
const functionDeclarationRegExp1: RegExp = /function *e *\(\) *{}/;
const functionDeclarationRegExp2: RegExp = /function *f *\(\) *{}/;
const functionDeclarationRegExp3: RegExp = /function *g *\(\) *{}/;
const functionDeclarationRegExp4: RegExp = /function *h *\(\) *{}/;
let obfuscatedCode: string;
@@ -457,12 +460,24 @@ describe('VariableDeclarationTransformer', () => {
obfuscatedCode = obfuscationResult.getObfuscatedCode();
});
it('Match #1: shouldn\'t rename twice function declaration name', () => {
assert.match(obfuscatedCode, functionRegExp);
it('Match #1: shouldn\'t rename twice variable declaration name', () => {
assert.match(obfuscatedCode, variableDeclarationRegExp);
});
it('Match #2: should correctly rename variable declarations', () => {
assert.match(obfuscatedCode, variableDeclarationsRegExp);
it('Match #2: should correctly rename function declaration name', () => {
assert.match(obfuscatedCode, functionDeclarationRegExp1);
});
it('Match #2: should correctly rename function declaration name', () => {
assert.match(obfuscatedCode, functionDeclarationRegExp2);
});
it('Match #2: should correctly rename function declaration name', () => {
assert.match(obfuscatedCode, functionDeclarationRegExp3);
});
it('Match #2: should correctly rename function declaration name', () => {
assert.match(obfuscatedCode, functionDeclarationRegExp4);
});
});
});
@@ -1,6 +1,10 @@
(function() {
function test() {
function inner() {}
let a, b, c, d;
var inner = 1;
function a () {}
function b () {}
function c () {}
function d () {}
}
})();