From cc532bd079607029b4ddfa6cf7c91d48c847cc1c Mon Sep 17 00:00:00 2001 From: sanex3339 Date: Thu, 15 Jun 2017 22:26:17 +0300 Subject: [PATCH] Added getRegExpMatch helper to tests --- ...ockStatementControlFlowTransformer.spec.ts | 14 +++-- .../CatchClauseTransformer.spec.ts | 9 ++-- .../FunctionDeclarationTransformer.spec.ts | 2 +- .../FunctionTransformer.spec.ts | 30 +++++------ .../LabeledStatementTransformer.spec.ts | 11 ++-- .../VariableDeclarationTransformer.spec.ts | 51 ++++++------------- test/helpers/getRegExpMatch.ts | 6 +-- 7 files changed, 47 insertions(+), 76 deletions(-) diff --git a/test/functional-tests/node-transformers/control-flow-transformers/block-statement-control-flow-transformer/BlockStatementControlFlowTransformer.spec.ts b/test/functional-tests/node-transformers/control-flow-transformers/block-statement-control-flow-transformer/BlockStatementControlFlowTransformer.spec.ts index 529c8f2b..f811d4ae 100644 --- a/test/functional-tests/node-transformers/control-flow-transformers/block-statement-control-flow-transformer/BlockStatementControlFlowTransformer.spec.ts +++ b/test/functional-tests/node-transformers/control-flow-transformers/block-statement-control-flow-transformer/BlockStatementControlFlowTransformer.spec.ts @@ -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 = 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 = obfuscatedCode.match(switchCaseMapStringRegExp); - const switchCaseMapMatch: string = switchCaseMapStringMatches[1]; + const switchCaseMapMatch: string = getRegExpMatch(obfuscatedCode, switchCaseMapStringRegExp); switchCaseMap = switchCaseMapMatch.replace(/\\x7c/g, '|').split('|').sort(); }); diff --git a/test/functional-tests/node-transformers/obfuscating-transformers/catch-clause-transformer/CatchClauseTransformer.spec.ts b/test/functional-tests/node-transformers/obfuscating-transformers/catch-clause-transformer/CatchClauseTransformer.spec.ts index 85b08401..6392a0cf 100644 --- a/test/functional-tests/node-transformers/obfuscating-transformers/catch-clause-transformer/CatchClauseTransformer.spec.ts +++ b/test/functional-tests/node-transformers/obfuscating-transformers/catch-clause-transformer/CatchClauseTransformer.spec.ts @@ -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', () => { diff --git a/test/functional-tests/node-transformers/obfuscating-transformers/function-declaration-transformer/FunctionDeclarationTransformer.spec.ts b/test/functional-tests/node-transformers/obfuscating-transformers/function-declaration-transformer/FunctionDeclarationTransformer.spec.ts index df05ca1c..0671413a 100644 --- a/test/functional-tests/node-transformers/obfuscating-transformers/function-declaration-transformer/FunctionDeclarationTransformer.spec.ts +++ b/test/functional-tests/node-transformers/obfuscating-transformers/function-declaration-transformer/FunctionDeclarationTransformer.spec.ts @@ -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', () => { diff --git a/test/functional-tests/node-transformers/obfuscating-transformers/function-transformer/FunctionTransformer.spec.ts b/test/functional-tests/node-transformers/obfuscating-transformers/function-transformer/FunctionTransformer.spec.ts index 857dc7c0..531db379 100644 --- a/test/functional-tests/node-transformers/obfuscating-transformers/function-transformer/FunctionTransformer.spec.ts +++ b/test/functional-tests/node-transformers/obfuscating-transformers/function-transformer/FunctionTransformer.spec.ts @@ -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', () => { diff --git a/test/functional-tests/node-transformers/obfuscating-transformers/labeled-statement-transformer/LabeledStatementTransformer.spec.ts b/test/functional-tests/node-transformers/obfuscating-transformers/labeled-statement-transformer/LabeledStatementTransformer.spec.ts index 14974c0b..659495ff 100644 --- a/test/functional-tests/node-transformers/obfuscating-transformers/labeled-statement-transformer/LabeledStatementTransformer.spec.ts +++ b/test/functional-tests/node-transformers/obfuscating-transformers/labeled-statement-transformer/LabeledStatementTransformer.spec.ts @@ -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', () => { diff --git a/test/functional-tests/node-transformers/obfuscating-transformers/variable-declaration-transformer/VariableDeclarationTransformer.spec.ts b/test/functional-tests/node-transformers/obfuscating-transformers/variable-declaration-transformer/VariableDeclarationTransformer.spec.ts index 27a7f967..c1e608bf 100644 --- a/test/functional-tests/node-transformers/obfuscating-transformers/variable-declaration-transformer/VariableDeclarationTransformer.spec.ts +++ b/test/functional-tests/node-transformers/obfuscating-transformers/variable-declaration-transformer/VariableDeclarationTransformer.spec.ts @@ -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 = (functionParamIdentifierMatch)[1]; - innerFunctionParamIdentifierName = (innerFunctionParamIdentifierMatch)[1]; - constructorIdentifierName = (constructorIdentifierMatch)[1]; - objectIdentifierName = (objectIdentifierMatch)[1]; - variableDeclarationIdentifierName = (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 = (catchClauseParamIdentifierMatch)[1]; - innerFunctionParamIdentifierName = (innerFunctionParamIdentifierMatch)[1]; - constructorIdentifierName = (constructorIdentifierMatch)[1]; - objectIdentifierName = (objectIdentifierMatch)[1]; - variableDeclarationIdentifierName = (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', () => { diff --git a/test/helpers/getRegExpMatch.ts b/test/helpers/getRegExpMatch.ts index c816aed5..4adeb008 100644 --- a/test/helpers/getRegExpMatch.ts +++ b/test/helpers/getRegExpMatch.ts @@ -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 (match)[index]; + return (match)[matchIndex + 1]; }