mirror of
https://github.com/javascript-obfuscator/javascript-obfuscator
synced 2026-08-09 12:42:29 +00:00
Added getRegExpMatch helper to tests
This commit is contained in:
+6
-8
@@ -4,6 +4,7 @@ import { IObfuscationResult } from '../../../../../src/interfaces/IObfuscationRe
|
||||
|
||||
import { NO_CUSTOM_NODES_PRESET } from '../../../../../src/options/presets/NoCustomNodes';
|
||||
|
||||
import { getRegExpMatch } from '../../../../helpers/getRegExpMatch';
|
||||
import { readFileAsString } from '../../../../helpers/readFileAsString';
|
||||
|
||||
import { JavaScriptObfuscator } from '../../../../../src/JavaScriptObfuscator';
|
||||
@@ -87,14 +88,13 @@ describe('BlockStatementControlFlowTransformer', function () {
|
||||
|
||||
describe('switch-case map', () => {
|
||||
const switchCaseMapVariableRegExp: RegExp = /var *_0x(?:[a-f0-9]){4,6} *= *_0x(?:[a-f0-9]){4,6}\['.*'\]\['split'\]\('\\x7c'\)/;
|
||||
const switchCaseMapStringRegExp: RegExp = /var *_0x(?:[a-f0-9]){4,6} *= *\{'.*' *: *'(.*)'\};/;
|
||||
const expectedSwitchCasesSequence: string[] = ['0', '1', '2', '3', '4'];
|
||||
|
||||
let switchCaseMap: string[];
|
||||
|
||||
before(() => {
|
||||
const switchCaseMapStringRegExp: RegExp = /var *_0x(?:[a-f0-9]){4,6} *= *\{'.*' *: *'(.*)'\};/;
|
||||
const switchCaseMapStringMatches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(switchCaseMapStringRegExp);
|
||||
const switchCaseMapMatch: string = switchCaseMapStringMatches[1];
|
||||
const switchCaseMapMatch: string = getRegExpMatch(obfuscatedCode, switchCaseMapStringRegExp);
|
||||
|
||||
switchCaseMap = switchCaseMapMatch.replace(/\\x7c/g, '|').split('|').sort();
|
||||
});
|
||||
@@ -157,13 +157,12 @@ describe('BlockStatementControlFlowTransformer', function () {
|
||||
|
||||
describe('block statement statements', () => {
|
||||
const switchCaseRegExp: RegExp = /switch *\(_0x([a-f0-9]){4,6}\[_0x([a-f0-9]){4,6}\+\+\]\) *\{/;
|
||||
const switchCaseLengthRegExp: RegExp = /case *'[0-5]': *console\['log'\]\(0x[0-6]\);/g;
|
||||
const expectedSwitchCaseLength: number = 5;
|
||||
|
||||
let switchCaseLength: number;
|
||||
|
||||
before(() => {
|
||||
const switchCaseLengthRegExp: RegExp = /case *'[0-5]': *console\['log'\]\(0x[0-6]\);/g;
|
||||
|
||||
switchCaseLength = obfuscatedCode.match(switchCaseLengthRegExp)!.length;
|
||||
});
|
||||
|
||||
@@ -178,14 +177,13 @@ describe('BlockStatementControlFlowTransformer', function () {
|
||||
|
||||
describe('switch-case map', () => {
|
||||
const switchCaseMapVariableRegExp: RegExp = /var *_0x(?:[a-f0-9]){4,6} *= *_0x(?:[a-f0-9]){4,6}\['.*'\]\['split'\]\('\\x7c'\)/;
|
||||
const switchCaseMapStringRegExp: RegExp = /var *_0x(?:[a-f0-9]){4,6} *= *\{'.*' *: *'(.*)'\};/;
|
||||
const expectedSwitchCasesSequence: string[] = ['0', '1', '2', '3', '4'];
|
||||
|
||||
let switchCaseMap: string[];
|
||||
|
||||
before(() => {
|
||||
const switchCaseMapStringRegExp: RegExp = /var *_0x(?:[a-f0-9]){4,6} *= *\{'.*' *: *'(.*)'\};/;
|
||||
const switchCaseMapStringMatches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(switchCaseMapStringRegExp);
|
||||
const switchCaseMapMatch: string = switchCaseMapStringMatches[1];
|
||||
const switchCaseMapMatch: string = getRegExpMatch(obfuscatedCode, switchCaseMapStringRegExp);
|
||||
|
||||
switchCaseMap = switchCaseMapMatch.replace(/\\x7c/g, '|').split('|').sort();
|
||||
});
|
||||
|
||||
+3
-6
@@ -4,6 +4,7 @@ import { IObfuscationResult } from '../../../../../src/interfaces/IObfuscationRe
|
||||
|
||||
import { NO_CUSTOM_NODES_PRESET } from '../../../../../src/options/presets/NoCustomNodes';
|
||||
|
||||
import { getRegExpMatch } from '../../../../helpers/getRegExpMatch';
|
||||
import { readFileAsString } from '../../../../helpers/readFileAsString';
|
||||
|
||||
import { JavaScriptObfuscator } from '../../../../../src/JavaScriptObfuscator';
|
||||
@@ -28,12 +29,8 @@ describe('CatchClauseTransformer', () => {
|
||||
);
|
||||
|
||||
obfuscatedCode = obfuscationResult.getObfuscatedCode();
|
||||
|
||||
const firstMatchArray: RegExpMatchArray | null = obfuscatedCode.match(paramNameRegExp);
|
||||
const secondMatchArray: RegExpMatchArray | null = obfuscatedCode.match(bodyParamNameRegExp);
|
||||
|
||||
firstMatch = firstMatchArray ? firstMatchArray[1] : undefined;
|
||||
secondMatch = secondMatchArray ? secondMatchArray[1] : undefined;
|
||||
firstMatch = getRegExpMatch(obfuscatedCode, paramNameRegExp);
|
||||
secondMatch = getRegExpMatch(obfuscatedCode, bodyParamNameRegExp);
|
||||
});
|
||||
|
||||
it('match #1: should transform catch clause node', () => {
|
||||
|
||||
+1
-1
@@ -4,10 +4,10 @@ import { IObfuscationResult } from '../../../../../src/interfaces/IObfuscationRe
|
||||
|
||||
import { NO_CUSTOM_NODES_PRESET } from '../../../../../src/options/presets/NoCustomNodes';
|
||||
|
||||
import { getRegExpMatch } from '../../../../helpers/getRegExpMatch';
|
||||
import { readFileAsString } from '../../../../helpers/readFileAsString';
|
||||
|
||||
import { JavaScriptObfuscator } from '../../../../../src/JavaScriptObfuscator';
|
||||
import { getRegExpMatch } from '../../../../helpers/getRegExpMatch';
|
||||
|
||||
describe('FunctionDeclarationTransformer', () => {
|
||||
describe('transformation of `functionDeclaration` node names', () => {
|
||||
|
||||
+15
-15
@@ -7,6 +7,7 @@ import { NO_CUSTOM_NODES_PRESET } from '../../../../../src/options/presets/NoCus
|
||||
import { readFileAsString } from '../../../../helpers/readFileAsString';
|
||||
|
||||
import { JavaScriptObfuscator } from '../../../../../src/JavaScriptObfuscator';
|
||||
import { getRegExpMatch } from '../../../../helpers/getRegExpMatch';
|
||||
|
||||
describe('FunctionTransformer', () => {
|
||||
describe('identifiers transformation inside `FunctionDeclaration` and `FunctionExpression` node body', () => {
|
||||
@@ -123,11 +124,10 @@ describe('FunctionTransformer', () => {
|
||||
);
|
||||
|
||||
obfuscatedCode = obfuscationResult.getObfuscatedCode();
|
||||
|
||||
variableDeclarationIdentifierName = obfuscatedCode.match(variableDeclarationRegExp)![1];
|
||||
functionParameterIdentifierName = obfuscatedCode.match(functionParameterRegExp)![1];
|
||||
functionDefaultParameterIdentifierName = obfuscatedCode.match(functionParameterRegExp)![2];
|
||||
functionBodyIdentifierName = obfuscatedCode.match(functionBodyRegExp)![1];
|
||||
variableDeclarationIdentifierName = getRegExpMatch(obfuscatedCode, variableDeclarationRegExp);
|
||||
functionParameterIdentifierName = getRegExpMatch(obfuscatedCode, functionParameterRegExp);
|
||||
functionDefaultParameterIdentifierName = getRegExpMatch(obfuscatedCode, functionParameterRegExp, 1);
|
||||
functionBodyIdentifierName = getRegExpMatch(obfuscatedCode, functionBodyRegExp);
|
||||
});
|
||||
|
||||
it('match #1: should transform function parameter assignment pattern identifier', () => {
|
||||
@@ -175,13 +175,13 @@ describe('FunctionTransformer', () => {
|
||||
|
||||
obfuscatedCode = obfuscationResult.getObfuscatedCode();
|
||||
|
||||
variableDeclarationIdentifierName = obfuscatedCode.match(variableDeclarationRegExp)![1];
|
||||
functionParameterIdentifierName = obfuscatedCode.match(functionParameterRegExp)![1];
|
||||
functionDefaultParameterIdentifierName1 = obfuscatedCode.match(functionParameterRegExp)![2];
|
||||
functionDefaultParameterIdentifierName2 = obfuscatedCode.match(functionParameterRegExp)![3];
|
||||
variableDeclarationIdentifierName = getRegExpMatch(obfuscatedCode, variableDeclarationRegExp);
|
||||
functionParameterIdentifierName = getRegExpMatch(obfuscatedCode, functionParameterRegExp);
|
||||
functionDefaultParameterIdentifierName1 = getRegExpMatch(obfuscatedCode, functionParameterRegExp, 1);
|
||||
functionDefaultParameterIdentifierName2 = getRegExpMatch(obfuscatedCode, functionParameterRegExp, 2);
|
||||
|
||||
functionBodyIdentifierName1 = obfuscatedCode.match(functionBodyRegExp)![1];
|
||||
functionBodyIdentifierName2 = obfuscatedCode.match(functionBodyRegExp)![2];
|
||||
functionBodyIdentifierName1 = getRegExpMatch(obfuscatedCode, functionBodyRegExp);
|
||||
functionBodyIdentifierName2 = getRegExpMatch(obfuscatedCode, functionBodyRegExp, 1);
|
||||
});
|
||||
|
||||
it('match #1: should transform function parameter assignment pattern identifier', () => {
|
||||
@@ -241,10 +241,10 @@ describe('FunctionTransformer', () => {
|
||||
);
|
||||
const obfuscatedCode: string = obfuscationResult.getObfuscatedCode();
|
||||
|
||||
arrayPatternIdentifierName1 = obfuscatedCode.match(functionParameterRegExp)![1];
|
||||
arrayPatternIdentifierName2 = obfuscatedCode.match(functionParameterRegExp)![2];
|
||||
functionBodyIdentifierName1 = obfuscatedCode.match(functionBodyRegExp)![1];
|
||||
functionBodyIdentifierName2 = obfuscatedCode.match(functionBodyRegExp)![2];
|
||||
arrayPatternIdentifierName1 = getRegExpMatch(obfuscatedCode, functionParameterRegExp);
|
||||
arrayPatternIdentifierName2 = getRegExpMatch(obfuscatedCode, functionParameterRegExp, 1);
|
||||
functionBodyIdentifierName1 = getRegExpMatch(obfuscatedCode, functionBodyRegExp);
|
||||
functionBodyIdentifierName2 = getRegExpMatch(obfuscatedCode, functionBodyRegExp, 1);
|
||||
});
|
||||
|
||||
it('equal #1: should keep same names for identifiers in function parameter array pattern and function body', () => {
|
||||
|
||||
+4
-7
@@ -4,6 +4,7 @@ import { IObfuscationResult } from '../../../../../src/interfaces/IObfuscationRe
|
||||
|
||||
import { NO_CUSTOM_NODES_PRESET } from '../../../../../src/options/presets/NoCustomNodes';
|
||||
|
||||
import { getRegExpMatch } from '../../../../helpers/getRegExpMatch';
|
||||
import { readFileAsString } from '../../../../helpers/readFileAsString';
|
||||
|
||||
import { JavaScriptObfuscator } from '../../../../../src/JavaScriptObfuscator';
|
||||
@@ -30,13 +31,9 @@ describe('LabeledStatementTransformer', () => {
|
||||
|
||||
obfuscatedCode = obfuscationResult.getObfuscatedCode();
|
||||
|
||||
const firstMatchArray: RegExpMatchArray|null = obfuscatedCode.match(labeledStatementRegExp);
|
||||
const secondMatchArray: RegExpMatchArray|null = obfuscatedCode.match(continueStatementRegExp);
|
||||
const thirdMatchArray: RegExpMatchArray|null = obfuscatedCode.match(breakStatementRegExp);
|
||||
|
||||
firstMatch = firstMatchArray ? firstMatchArray[1] : undefined;
|
||||
secondMatch = secondMatchArray ? secondMatchArray[1] : undefined;
|
||||
thirdMatch = thirdMatchArray ? thirdMatchArray[1] : undefined;
|
||||
firstMatch = getRegExpMatch(obfuscatedCode, labeledStatementRegExp);
|
||||
secondMatch = getRegExpMatch(obfuscatedCode, continueStatementRegExp);
|
||||
thirdMatch = getRegExpMatch(obfuscatedCode, breakStatementRegExp);
|
||||
});
|
||||
|
||||
it('should transform `labeledStatement` identifier', () => {
|
||||
|
||||
+15
-36
@@ -4,6 +4,7 @@ import { IObfuscationResult } from '../../../../../src/interfaces/IObfuscationRe
|
||||
|
||||
import { NO_CUSTOM_NODES_PRESET } from '../../../../../src/options/presets/NoCustomNodes';
|
||||
|
||||
import { getRegExpMatch } from '../../../../helpers/getRegExpMatch';
|
||||
import { readFileAsString } from '../../../../helpers/readFileAsString';
|
||||
|
||||
import { JavaScriptObfuscator } from '../../../../../src/JavaScriptObfuscator';
|
||||
@@ -157,22 +158,11 @@ describe('VariableDeclarationTransformer', () => {
|
||||
);
|
||||
const obfuscatedCode: string = obfuscationResult.getObfuscatedCode();
|
||||
|
||||
const functionParamIdentifierMatch: RegExpMatchArray|null = obfuscatedCode
|
||||
.match(functionParamIdentifierRegExp);
|
||||
const innerFunctionParamIdentifierMatch: RegExpMatchArray|null = obfuscatedCode
|
||||
.match(innerFunctionParamIdentifierRegExp);
|
||||
const constructorIdentifierMatch: RegExpMatchArray|null = obfuscatedCode
|
||||
.match(constructorIdentifierRegExp);
|
||||
const objectIdentifierMatch: RegExpMatchArray|null = obfuscatedCode
|
||||
.match(objectIdentifierRegExp);
|
||||
const variableDeclarationIdentifierMatch: RegExpMatchArray|null = obfuscatedCode
|
||||
.match(variableDeclarationIdentifierRegExp);
|
||||
|
||||
outerFunctionParamIdentifierName = (<RegExpMatchArray>functionParamIdentifierMatch)[1];
|
||||
innerFunctionParamIdentifierName = (<RegExpMatchArray>innerFunctionParamIdentifierMatch)[1];
|
||||
constructorIdentifierName = (<RegExpMatchArray>constructorIdentifierMatch)[1];
|
||||
objectIdentifierName = (<RegExpMatchArray>objectIdentifierMatch)[1];
|
||||
variableDeclarationIdentifierName = (<RegExpMatchArray>variableDeclarationIdentifierMatch)[1];
|
||||
outerFunctionParamIdentifierName = getRegExpMatch(obfuscatedCode, functionParamIdentifierRegExp);
|
||||
innerFunctionParamIdentifierName = getRegExpMatch(obfuscatedCode, innerFunctionParamIdentifierRegExp);
|
||||
constructorIdentifierName = getRegExpMatch(obfuscatedCode, constructorIdentifierRegExp);
|
||||
objectIdentifierName = getRegExpMatch(obfuscatedCode, objectIdentifierRegExp);
|
||||
variableDeclarationIdentifierName = getRegExpMatch(obfuscatedCode, variableDeclarationIdentifierRegExp);
|
||||
});
|
||||
|
||||
it('match #1: should\'t name variables inside inner function with names from outer function params', () => {
|
||||
@@ -223,22 +213,11 @@ describe('VariableDeclarationTransformer', () => {
|
||||
);
|
||||
const obfuscatedCode: string = obfuscationResult.getObfuscatedCode();
|
||||
|
||||
const catchClauseParamIdentifierMatch: RegExpMatchArray|null = obfuscatedCode
|
||||
.match(catchClauseParamIdentifierRegExp);
|
||||
const innerFunctionParamIdentifierMatch: RegExpMatchArray|null = obfuscatedCode
|
||||
.match(innerFunctionParamIdentifierRegExp);
|
||||
const constructorIdentifierMatch: RegExpMatchArray|null = obfuscatedCode
|
||||
.match(constructorIdentifierRegExp);
|
||||
const objectIdentifierMatch: RegExpMatchArray|null = obfuscatedCode
|
||||
.match(objectIdentifierRegExp);
|
||||
const variableDeclarationIdentifierMatch: RegExpMatchArray|null = obfuscatedCode
|
||||
.match(variableDeclarationIdentifierRegExp);
|
||||
|
||||
catchClauseParamIdentifierName = (<RegExpMatchArray>catchClauseParamIdentifierMatch)[1];
|
||||
innerFunctionParamIdentifierName = (<RegExpMatchArray>innerFunctionParamIdentifierMatch)[1];
|
||||
constructorIdentifierName = (<RegExpMatchArray>constructorIdentifierMatch)[1];
|
||||
objectIdentifierName = (<RegExpMatchArray>objectIdentifierMatch)[1];
|
||||
variableDeclarationIdentifierName = (<RegExpMatchArray>variableDeclarationIdentifierMatch)[1];
|
||||
catchClauseParamIdentifierName = getRegExpMatch(obfuscatedCode, catchClauseParamIdentifierRegExp);
|
||||
innerFunctionParamIdentifierName = getRegExpMatch(obfuscatedCode, innerFunctionParamIdentifierRegExp);
|
||||
constructorIdentifierName = getRegExpMatch(obfuscatedCode, constructorIdentifierRegExp);
|
||||
objectIdentifierName = getRegExpMatch(obfuscatedCode, objectIdentifierRegExp);
|
||||
variableDeclarationIdentifierName = getRegExpMatch(obfuscatedCode, variableDeclarationIdentifierRegExp);
|
||||
});
|
||||
|
||||
it('match #1: should\'t name variables inside inner function with names from catch clause param', () => {
|
||||
@@ -360,10 +339,10 @@ describe('VariableDeclarationTransformer', () => {
|
||||
|
||||
obfuscatedCode = obfuscationResult.getObfuscatedCode();
|
||||
|
||||
objectPatternIdentifierName1 = obfuscatedCode.match(objectPatternVariableDeclaratorRegExp)![1];
|
||||
objectPatternIdentifierName2 = obfuscatedCode.match(objectPatternVariableDeclaratorRegExp)![2];
|
||||
identifierName1 = obfuscatedCode.match(variableUsageRegExp)![1];
|
||||
identifierName2 = obfuscatedCode.match(variableUsageRegExp)![2];
|
||||
objectPatternIdentifierName1 = getRegExpMatch(obfuscatedCode, objectPatternVariableDeclaratorRegExp);
|
||||
objectPatternIdentifierName2 = getRegExpMatch(obfuscatedCode, objectPatternVariableDeclaratorRegExp, 1);
|
||||
identifierName1 = getRegExpMatch(obfuscatedCode, variableUsageRegExp);
|
||||
identifierName2 = getRegExpMatch(obfuscatedCode, variableUsageRegExp, 1);
|
||||
});
|
||||
|
||||
it('match #1: should transform array pattern variable declarator', () => {
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
/**
|
||||
* @param str
|
||||
* @param regExp
|
||||
* @param index
|
||||
* @param matchIndex
|
||||
* @return {string}
|
||||
*/
|
||||
export function getRegExpMatch (str: string, regExp: RegExp, index: number = 1): string {
|
||||
export function getRegExpMatch (str: string, regExp: RegExp, matchIndex: number = 0): string {
|
||||
const match: RegExpMatchArray | null = str.match(regExp);
|
||||
|
||||
if (!match) {
|
||||
throw new Error(`No matches were found for regular expression \`${regExp.toString()}\``);
|
||||
}
|
||||
|
||||
return (<RegExpMatchArray>match)[index];
|
||||
return (<RegExpMatchArray>match)[matchIndex + 1];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user