Added ScopeThroughIdentifiersTransformer tests

This commit is contained in:
sanex
2021-05-30 03:42:41 +03:00
parent 128e91b612
commit b62f49369c
10 changed files with 282 additions and 0 deletions
@@ -0,0 +1,89 @@
import { assert } from 'chai';
import { NO_ADDITIONAL_NODES_PRESET } from '../../../../../../src/options/presets/NoCustomNodes';
import { readFileAsString } from '../../../../../helpers/readFileAsString';
import { JavaScriptObfuscator } from '../../../../../../src/JavaScriptObfuscatorFacade';
describe('ScopeThroughIdentifiersTransformer ClassDeclaration identifiers', () => {
describe('Variant #1: class declaration is exist', () => {
const classNameIdentifierRegExp: RegExp = /class *(_0x[a-f0-9]{4,6}) *\{/;
const classCallIdentifierRegExp: RegExp = /new *(_0x[a-f0-9]{4,6}) *\( *\);/;
let obfuscatedCode: string;
before(() => {
const code: string = readFileAsString(__dirname + '/fixtures/class-call-with-declaration.js');
obfuscatedCode = JavaScriptObfuscator.obfuscate(
code,
{
...NO_ADDITIONAL_NODES_PRESET,
renameGlobals: true,
identifierNamesCache: {
'Foo': 'Foo_from_cache'
}
}
).getObfuscatedCode();
});
it('should skip transformation of class name', () => {
assert.match(obfuscatedCode, classNameIdentifierRegExp);
});
it('should skip transformation of class name', () => {
assert.match(obfuscatedCode, classCallIdentifierRegExp);
});
});
describe('Variant #2: class declaration is missing', () => {
describe('Variant #1: transformation of class call identifier node name based on identifier names cache', () => {
const classCallIdentifierRegExp: RegExp = /new *Foo_from_cache *\( *\);/;
let obfuscatedCode: string;
before(() => {
const code: string = readFileAsString(__dirname + '/fixtures/class-call-without-declaration.js');
obfuscatedCode = JavaScriptObfuscator.obfuscate(
code,
{
...NO_ADDITIONAL_NODES_PRESET,
renameGlobals: true,
identifierNamesCache: {
'Foo': 'Foo_from_cache'
}
}
).getObfuscatedCode();
});
it('should transform class name', () => {
assert.match(obfuscatedCode, classCallIdentifierRegExp);
});
});
describe('Variant #2: ignore transformation of class call identifier node name when no identifier names cache value is available', () => {
const classCallIdentifierRegExp: RegExp = /new *Foo *\( *\);/;
let obfuscatedCode: string;
before(() => {
const code: string = readFileAsString(__dirname + '/fixtures/class-call-without-declaration.js');
obfuscatedCode = JavaScriptObfuscator.obfuscate(
code,
{
...NO_ADDITIONAL_NODES_PRESET,
renameGlobals: true,
identifierNamesCache: {}
}
).getObfuscatedCode();
});
it('should not transform class name', () => {
assert.match(obfuscatedCode, classCallIdentifierRegExp);
});
});
});
});
@@ -0,0 +1,89 @@
import { assert } from 'chai';
import { NO_ADDITIONAL_NODES_PRESET } from '../../../../../../src/options/presets/NoCustomNodes';
import { readFileAsString } from '../../../../../helpers/readFileAsString';
import { JavaScriptObfuscator } from '../../../../../../src/JavaScriptObfuscatorFacade';
describe('ScopeThroughIdentifiersTransformer FunctionDeclaration identifiers', () => {
describe('Variant #1: function declaration is exist', () => {
const functionNameIdentifierRegExp: RegExp = /function *(_0x[a-f0-9]{4,6}) *\(\) *\{/;
const functionCallIdentifierRegExp: RegExp = /(_0x[a-f0-9]{4,6}) *\( *\);/;
let obfuscatedCode: string;
before(() => {
const code: string = readFileAsString(__dirname + '/fixtures/function-call-with-declaration.js');
obfuscatedCode = JavaScriptObfuscator.obfuscate(
code,
{
...NO_ADDITIONAL_NODES_PRESET,
renameGlobals: true,
identifierNamesCache: {
'foo': 'foo_from_cache'
}
}
).getObfuscatedCode();
});
it('should skip transformation of function name', () => {
assert.match(obfuscatedCode, functionNameIdentifierRegExp);
});
it('should skip transformation of function name', () => {
assert.match(obfuscatedCode, functionCallIdentifierRegExp);
});
});
describe('Variant #2: function declaration is missing', () => {
describe('Variant #1: transformation of function call identifier node name based on identifier names cache', () => {
const functionCallIdentifierRegExp: RegExp = /foo_from_cache *\( *\);/;
let obfuscatedCode: string;
before(() => {
const code: string = readFileAsString(__dirname + '/fixtures/function-call-without-declaration.js');
obfuscatedCode = JavaScriptObfuscator.obfuscate(
code,
{
...NO_ADDITIONAL_NODES_PRESET,
renameGlobals: true,
identifierNamesCache: {
'foo': 'foo_from_cache'
}
}
).getObfuscatedCode();
});
it('should transform function name', () => {
assert.match(obfuscatedCode, functionCallIdentifierRegExp);
});
});
describe('Variant #2: ignore transformation of function call identifier node name when no identifier names cache value is available', () => {
const functionCallIdentifierRegExp: RegExp = /foo *\( *\);/;
let obfuscatedCode: string;
before(() => {
const code: string = readFileAsString(__dirname + '/fixtures/function-call-without-declaration.js');
obfuscatedCode = JavaScriptObfuscator.obfuscate(
code,
{
...NO_ADDITIONAL_NODES_PRESET,
renameGlobals: true,
identifierNamesCache: {}
}
).getObfuscatedCode();
});
it('should not transform function name', () => {
assert.match(obfuscatedCode, functionCallIdentifierRegExp);
});
});
});
});
@@ -0,0 +1,89 @@
import { assert } from 'chai';
import { NO_ADDITIONAL_NODES_PRESET } from '../../../../../../src/options/presets/NoCustomNodes';
import { readFileAsString } from '../../../../../helpers/readFileAsString';
import { JavaScriptObfuscator } from '../../../../../../src/JavaScriptObfuscatorFacade';
describe('ScopeThroughIdentifiersTransformer VariableDeclaration identifiers', () => {
describe('Variant #1: variable declaration is exist', () => {
const variableIdentifierRegExp: RegExp = /var *(_0x[a-f0-9]{4,6}) *= *0x1/;
const variableReferenceIdentifierRegExp: RegExp = /(_0x[a-f0-9]{4,6});/;
let obfuscatedCode: string;
before(() => {
const code: string = readFileAsString(__dirname + '/fixtures/variable-reference-with-declaration.js');
obfuscatedCode = JavaScriptObfuscator.obfuscate(
code,
{
...NO_ADDITIONAL_NODES_PRESET,
renameGlobals: true,
identifierNamesCache: {
'foo': 'foo_from_cache'
}
}
).getObfuscatedCode();
});
it('should skip transformation of variable declaration name', () => {
assert.match(obfuscatedCode, variableIdentifierRegExp);
});
it('should skip transformation of variable reference name', () => {
assert.match(obfuscatedCode, variableReferenceIdentifierRegExp);
});
});
describe('Variant #2: variable declaration is missing', () => {
describe('Variant #1: transformation of variable reference identifier node name based on identifier names cache', () => {
const variableReferenceIdentifierRegExp: RegExp = /foo_from_cache;/;
let obfuscatedCode: string;
before(() => {
const code: string = readFileAsString(__dirname + '/fixtures/variable-reference-without-declaration.js');
obfuscatedCode = JavaScriptObfuscator.obfuscate(
code,
{
...NO_ADDITIONAL_NODES_PRESET,
renameGlobals: true,
identifierNamesCache: {
'foo': 'foo_from_cache'
}
}
).getObfuscatedCode();
});
it('should transform variable reference name', () => {
assert.match(obfuscatedCode, variableReferenceIdentifierRegExp);
});
});
describe('Variant #2: ignore transformation of variable reference identifier node name when no identifier names cache value is available', () => {
const variableReferenceIdentifierRegExp: RegExp = /foo;/;
let obfuscatedCode: string;
before(() => {
const code: string = readFileAsString(__dirname + '/fixtures/variable-reference-without-declaration.js');
obfuscatedCode = JavaScriptObfuscator.obfuscate(
code,
{
...NO_ADDITIONAL_NODES_PRESET,
renameGlobals: true,
identifierNamesCache: {}
}
).getObfuscatedCode();
});
it('should not transform variable reference name', () => {
assert.match(obfuscatedCode, variableReferenceIdentifierRegExp);
});
});
});
});
+3
View File
@@ -118,6 +118,9 @@ import './functional-tests/node-transformers/rename-identifiers-transformers/sco
import './functional-tests/node-transformers/rename-identifiers-transformers/scope-identifiers-transformer/function/Function.spec';
import './functional-tests/node-transformers/rename-identifiers-transformers/scope-identifiers-transformer/import-declaration/ImportDeclaration.spec';
import './functional-tests/node-transformers/rename-identifiers-transformers/scope-identifiers-transformer/variable-declaration/VariableDeclaration.spec';
import './functional-tests/node-transformers/rename-identifiers-transformers/scope-through-identifiers-transformer/class-declaration/ClassDeclaration.spec';
import './functional-tests/node-transformers/rename-identifiers-transformers/scope-through-identifiers-transformer/function-declaration/FunctionDeclaration.spec';
import './functional-tests/node-transformers/rename-identifiers-transformers/scope-through-identifiers-transformer/variable-declaration/VariableDeclaration.spec';
import './functional-tests/node-transformers/rename-properties-transformers/rename-properties-transformer/RenamePropertiesTransformer.spec';
import './functional-tests/node-transformers/simplifying-transformers/block-statement-simplify-transformer/BlockStatementSimplifyTransformer.spec';
import './functional-tests/node-transformers/simplifying-transformers/expression-statements-merge-transformer/ExpressionStatementsMergeTransformer.spec';