mirror of
https://github.com/javascript-obfuscator/javascript-obfuscator
synced 2026-08-09 12:42:29 +00:00
CatchClause obfuscator ObjectPattern test
Tests refactoring
This commit is contained in:
+18
@@ -37,4 +37,22 @@ describe('CatchClauseTransformer', () => {
|
||||
assert.equal(firstMatch, secondMatch);
|
||||
});
|
||||
});
|
||||
|
||||
describe('object pattern as parameter', () => {
|
||||
const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
|
||||
readFileAsString(__dirname + '/fixtures/object-pattern-as-parameter.js'),
|
||||
{
|
||||
...NO_CUSTOM_NODES_PRESET
|
||||
}
|
||||
);
|
||||
const obfuscatedCode: string = obfuscationResult.getObfuscatedCode();
|
||||
|
||||
it('shouldn\'t transform function parameter object pattern identifier', () => {
|
||||
const functionParameterMatch: RegExp = /\} *catch *\(\{ *name *\}\) *\{/;
|
||||
const functionBodyMatch: RegExp = /return *name;/;
|
||||
|
||||
assert.match(obfuscatedCode, functionParameterMatch);
|
||||
assert.match(obfuscatedCode, functionBodyMatch);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
(function () {
|
||||
try {
|
||||
|
||||
} catch ({ name }) {
|
||||
return name;
|
||||
}
|
||||
})();
|
||||
+2
-2
@@ -35,9 +35,9 @@ describe('FunctionTransformer', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('object pattern as argument', () => {
|
||||
describe('object pattern as parameter', () => {
|
||||
const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
|
||||
readFileAsString(__dirname + '/fixtures/object-pattern-as-argument.js'),
|
||||
readFileAsString(__dirname + '/fixtures/object-pattern-as-parameter.js'),
|
||||
{
|
||||
...NO_CUSTOM_NODES_PRESET
|
||||
}
|
||||
|
||||
+73
-30
@@ -77,48 +77,91 @@ describe('VariableDeclarationTransformer', () => {
|
||||
});
|
||||
|
||||
describe(`variable calls before variable declaration when function param has the same name as variables name`, () => {
|
||||
let obfuscationResult: IObfuscationResult,
|
||||
functionParamIdentifierName: string|null,
|
||||
innerFunctionParamIdentifierName: string|null,
|
||||
constructorIdentifierName: string|null,
|
||||
objectIdentifierName: string|null,
|
||||
variableDeclarationIdentifierName: string|null;
|
||||
const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
|
||||
readFileAsString(__dirname + '/fixtures/variable-call-before-variable-declaration-2.js'),
|
||||
{
|
||||
...NO_CUSTOM_NODES_PRESET
|
||||
}
|
||||
);
|
||||
const obfuscatedCode: string = obfuscationResult.getObfuscatedCode();
|
||||
|
||||
beforeEach(() => {
|
||||
obfuscationResult = JavaScriptObfuscator.obfuscate(
|
||||
readFileAsString(__dirname + '/fixtures/variable-call-before-variable-declaration-2.js'),
|
||||
{
|
||||
...NO_CUSTOM_NODES_PRESET
|
||||
}
|
||||
);
|
||||
const functionParamIdentifierMatch: RegExpMatchArray|null = obfuscatedCode
|
||||
.match(/function *_0x[a-f0-9]{4,6} *\((_0x[a-f0-9]{4,6})\,(_0x[a-f0-9]{4,6})\) *\{/);
|
||||
const innerFunctionParamIdentifierMatch: RegExpMatchArray|null = obfuscatedCode
|
||||
.match(/function _0x[a-f0-9]{4,6} *\((_0x[a-f0-9]{4,6})\) *\{/);
|
||||
const constructorIdentifierMatch: RegExpMatchArray|null = obfuscatedCode
|
||||
.match(/console\['\\x6c\\x6f\\x67'\]\((_0x[a-f0-9]{4,6})\)/);
|
||||
const objectIdentifierMatch: RegExpMatchArray|null = obfuscatedCode
|
||||
.match(/return\{'\\x74':(_0x[a-f0-9]{4,6})\}/);
|
||||
const variableDeclarationIdentifierMatch: RegExpMatchArray|null = obfuscatedCode
|
||||
.match(/var *(_0x[a-f0-9]{4,6});/);
|
||||
|
||||
const outerFunctionParamIdentifierName: string|null = (<RegExpMatchArray>functionParamIdentifierMatch)[1];
|
||||
const innerFunctionParamIdentifierName: string|null = (<RegExpMatchArray>innerFunctionParamIdentifierMatch)[1];
|
||||
const constructorIdentifierName: string|null = (<RegExpMatchArray>constructorIdentifierMatch)[1];
|
||||
const objectIdentifierName: string|null = (<RegExpMatchArray>objectIdentifierMatch)[1];
|
||||
const variableDeclarationIdentifierName: string|null = (<RegExpMatchArray>variableDeclarationIdentifierMatch)[1];
|
||||
|
||||
it('should\'t name variables inside inner function with names from outer function params', () => {
|
||||
assert.notEqual(outerFunctionParamIdentifierName, constructorIdentifierName);
|
||||
assert.notEqual(outerFunctionParamIdentifierName, innerFunctionParamIdentifierName);
|
||||
});
|
||||
|
||||
it('should correct transform variables inside function body', () => {
|
||||
const obfuscatedCode: string = obfuscationResult.getObfuscatedCode();
|
||||
const functionParamIdentifierMatch: RegExpMatchArray|null = obfuscatedCode
|
||||
.match(/function *_0x[a-f0-9]{4,6} *\((_0x[a-f0-9]{4,6})\,(_0x[a-f0-9]{4,6})\) *\{/);
|
||||
const innerFunctionParamIdentifierMatch: RegExpMatchArray|null = obfuscatedCode
|
||||
.match(/function _0x[a-f0-9]{4,6} *\((_0x[a-f0-9]{4,6})\) *\{/);
|
||||
const constructorIdentifierMatch: RegExpMatchArray|null = obfuscatedCode
|
||||
.match(/console\['\\x6c\\x6f\\x67'\]\((_0x[a-f0-9]{4,6})\)/);
|
||||
const objectIdentifierMatch: RegExpMatchArray|null = obfuscatedCode
|
||||
.match(/return\{'\\x74':(_0x[a-f0-9]{4,6})\}/);
|
||||
const variableDeclarationIdentifierMatch: RegExpMatchArray|null = obfuscatedCode
|
||||
.match(/var *(_0x[a-f0-9]{4,6});/);
|
||||
it('should correct transform variables inside outer function body', () => {
|
||||
assert.equal(outerFunctionParamIdentifierName, objectIdentifierName);
|
||||
assert.equal(outerFunctionParamIdentifierName, variableDeclarationIdentifierName);
|
||||
});
|
||||
|
||||
functionParamIdentifierName = (<RegExpMatchArray>functionParamIdentifierMatch)[1];
|
||||
innerFunctionParamIdentifierName = (<RegExpMatchArray>innerFunctionParamIdentifierMatch)[1];
|
||||
constructorIdentifierName = (<RegExpMatchArray>constructorIdentifierMatch)[1];
|
||||
objectIdentifierName = (<RegExpMatchArray>objectIdentifierMatch)[1];
|
||||
variableDeclarationIdentifierName = (<RegExpMatchArray>variableDeclarationIdentifierMatch)[1];
|
||||
it('should correct transform variables inside inner function body', () => {
|
||||
assert.equal(innerFunctionParamIdentifierName, constructorIdentifierName);
|
||||
});
|
||||
|
||||
it('should keep equal names after transformation for variables with same names', () => {
|
||||
assert.equal(variableDeclarationIdentifierName, objectIdentifierName);
|
||||
});
|
||||
});
|
||||
|
||||
describe(`variable calls before variable declaration when catch clause param has the same name as variables name`, () => {
|
||||
const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
|
||||
readFileAsString(__dirname + '/fixtures/variable-call-before-variable-declaration-3.js'),
|
||||
{
|
||||
...NO_CUSTOM_NODES_PRESET
|
||||
}
|
||||
);
|
||||
const obfuscatedCode: string = obfuscationResult.getObfuscatedCode();
|
||||
|
||||
const catchClauseParamIdentifierMatch: RegExpMatchArray|null = obfuscatedCode
|
||||
.match(/catch *\((_0x[a-f0-9]{4,6})\) *\{/);
|
||||
const innerFunctionParamIdentifierMatch: RegExpMatchArray|null = obfuscatedCode
|
||||
.match(/function _0x[a-f0-9]{4,6} *\((_0x[a-f0-9]{4,6})\) *\{/);
|
||||
const constructorIdentifierMatch: RegExpMatchArray|null = obfuscatedCode
|
||||
.match(/console\['\\x6c\\x6f\\x67'\]\((_0x[a-f0-9]{4,6})\)/);
|
||||
const objectIdentifierMatch: RegExpMatchArray|null = obfuscatedCode
|
||||
.match(/return\{'\\x74':(_0x[a-f0-9]{4,6})\}/);
|
||||
const variableDeclarationIdentifierMatch: RegExpMatchArray|null = obfuscatedCode
|
||||
.match(/var *(_0x[a-f0-9]{4,6});/);
|
||||
|
||||
const functionParamIdentifierName: string|null = (<RegExpMatchArray>catchClauseParamIdentifierMatch)[1];
|
||||
const innerFunctionParamIdentifierName: string|null = (<RegExpMatchArray>innerFunctionParamIdentifierMatch)[1];
|
||||
const constructorIdentifierName: string|null = (<RegExpMatchArray>constructorIdentifierMatch)[1];
|
||||
const objectIdentifierName: string|null = (<RegExpMatchArray>objectIdentifierMatch)[1];
|
||||
const variableDeclarationIdentifierName: string|null = (<RegExpMatchArray>variableDeclarationIdentifierMatch)[1];
|
||||
|
||||
it('should\'t name variables inside inner function with names from catch clause param', () => {
|
||||
assert.notEqual(functionParamIdentifierName, constructorIdentifierName);
|
||||
assert.notEqual(functionParamIdentifierName, innerFunctionParamIdentifierName);
|
||||
});
|
||||
|
||||
it('should correct transform variables inside catch clause body', () => {
|
||||
assert.equal(functionParamIdentifierName, objectIdentifierName);
|
||||
assert.equal(functionParamIdentifierName, variableDeclarationIdentifierName);
|
||||
});
|
||||
|
||||
it('should correct transform variables inside inner function body', () => {
|
||||
assert.equal(innerFunctionParamIdentifierName, constructorIdentifierName);
|
||||
});
|
||||
|
||||
it('should keep equal names after transformation for variables with same names', () => {
|
||||
assert.equal(variableDeclarationIdentifierName, objectIdentifierName);
|
||||
});
|
||||
});
|
||||
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
(function () {
|
||||
try {
|
||||
|
||||
} catch (t) {
|
||||
return function () {
|
||||
function baz (t) {
|
||||
console.log(t);
|
||||
}
|
||||
|
||||
return {t: t};
|
||||
var t;
|
||||
}();
|
||||
}
|
||||
})();
|
||||
Reference in New Issue
Block a user