1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180 |
- import { assert } from 'chai';
- import { TypeFromEnum } from '@gradecam/tsenum';
- import { TDictionary } from '../../../src/types/TDictionary';
- import { TInputOptions } from '../../../src/types/options/TInputOptions';
- import { TOptionsPreset } from '../../../src/types/options/TOptionsPreset';
- import { IObfuscatedCode } from '../../../src/interfaces/source-code/IObfuscatedCode';
- import { SourceMapMode } from '../../../src/enums/source-map/SourceMapMode';
- import { StringArrayEncoding } from '../../../src/enums/node-transformers/string-array-transformers/StringArrayEncoding';
- import { StringArrayWrappersType } from '../../../src/enums/node-transformers/string-array-transformers/StringArrayWrappersType';
- import { JavaScriptObfuscator } from '../../../src/JavaScriptObfuscatorFacade';
- import { HIGH_OBFUSCATION_PRESET } from '../../../src/options/presets/HighObfuscation';
- import { NO_ADDITIONAL_NODES_PRESET } from '../../../src/options/presets/NoCustomNodes';
- import { IdentifierNamesGenerator } from '../../../src/enums/generators/identifier-names-generators/IdentifierNamesGenerator';
- import { OptionsPreset } from '../../../src/enums/options/presets/OptionsPreset';
- import { buildLargeCode } from '../../helpers/buildLargeCode';
- import { getRegExpMatch } from '../../helpers/getRegExpMatch';
- import { readFileAsString } from '../../helpers/readFileAsString';
- describe('JavaScriptObfuscator', () => {
- describe('obfuscate', () => {
- describe('correct source code', () => {
- let obfuscatedCode: string,
- sourceMap: string;
- beforeEach(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/simple-input-1.js');
- const obfuscatedCodeObject: IObfuscatedCode = JavaScriptObfuscator.obfuscate(
- code,
- {
- ...NO_ADDITIONAL_NODES_PRESET
- }
- );
- obfuscatedCode = obfuscatedCodeObject.getObfuscatedCode();
- sourceMap = obfuscatedCodeObject.getSourceMap();
- });
- it('should return correct obfuscated code', () => {
- assert.isOk(obfuscatedCode);
- });
- it('should return empty source map', () => {
- assert.isNotOk(sourceMap);
- });
- });
- describe('Empty or invalid source code', () => {
- describe('empty source code', () => {
- let obfuscatedCode: string;
- beforeEach(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/empty-input.js');
- obfuscatedCode = JavaScriptObfuscator.obfuscate(
- code,
- ).getObfuscatedCode();
- });
- it('should return an empty obfuscated code', () => {
- assert.isNotOk(obfuscatedCode);
- });
- });
- describe('empty source code with comments', () => {
- let obfuscatedCode: string;
- beforeEach(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/comments-only.js');
- obfuscatedCode = JavaScriptObfuscator.obfuscate(
- code,
- {
- controlFlowFlattening: true,
- deadCodeInjection: true
- }
- ).getObfuscatedCode();
- });
- it('should return an empty obfuscated code', () => {
- assert.isNotOk(obfuscatedCode);
- });
- });
- describe('invalid source code type', () => {
- let obfuscatedCode: string;
- beforeEach(() => {
- obfuscatedCode = JavaScriptObfuscator.obfuscate(
- 1 as unknown as string
- ).getObfuscatedCode();
- });
- it('should return an empty obfuscated code', () => {
- assert.isNotOk(obfuscatedCode);
- });
- });
- });
- describe('`sourceMap` option is `true`', () => {
- describe('`sourceMapMode` is `separate`', () => {
- let obfuscatedCode: string,
- sourceMap: string;
- beforeEach(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/simple-input-1.js');
- const obfuscatedCodeObject: IObfuscatedCode = JavaScriptObfuscator.obfuscate(
- code,
- {
- ...NO_ADDITIONAL_NODES_PRESET,
- sourceMap: true
- }
- );
- obfuscatedCode = obfuscatedCodeObject.getObfuscatedCode();
- sourceMap = JSON.parse(obfuscatedCodeObject.getSourceMap()).mappings;
- });
- it('should return correct obfuscated code', () => {
- assert.isOk(obfuscatedCode);
- });
- it('should return correct source map', () => {
- assert.isOk(sourceMap);
- });
- });
- describe('`sourceMapMode` is `inline`', () => {
- const regExp: RegExp = /sourceMappingURL=data:application\/json;base64/;
- let obfuscatedCode: string,
- sourceMap: string;
- beforeEach(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/simple-input-1.js');
- const obfuscatedCodeObject: IObfuscatedCode = JavaScriptObfuscator.obfuscate(
- code,
- {
- ...NO_ADDITIONAL_NODES_PRESET,
- sourceMap: true,
- sourceMapMode: SourceMapMode.Inline
- }
- );
- obfuscatedCode = obfuscatedCodeObject.getObfuscatedCode();
- sourceMap = JSON.parse(obfuscatedCodeObject.getSourceMap()).mappings;
- });
- it('should return correct obfuscated code', () => {
- assert.isOk(obfuscatedCode);
- });
- it('should return obfuscated code with inline source map as Base64 string', () => {
- assert.match(obfuscatedCode, regExp);
- });
- it('should return correct source map', () => {
- assert.isOk(sourceMap);
- });
- });
- describe('empty source code', () => {
- let obfuscatedCode: string,
- sourceMapNames: string[],
- sourceMapSources: string[],
- sourceMapMappings: string;
- beforeEach(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/empty-input.js');
- const obfuscatedCodeObject: IObfuscatedCode = JavaScriptObfuscator.obfuscate(
- code,
- {
- sourceMap: true
- }
- );
- obfuscatedCode = obfuscatedCodeObject.getObfuscatedCode();
- const sourceMapObject: any = JSON.parse(obfuscatedCodeObject.getSourceMap());
- sourceMapNames = sourceMapObject.names;
- sourceMapSources = sourceMapObject.sources;
- sourceMapMappings = sourceMapObject.mappings;
- });
- it('should return empty obfuscated code', () => {
- assert.isNotOk(obfuscatedCode);
- });
- it('should return empty source map property `names`', () => {
- assert.deepEqual(sourceMapNames, []);
- });
- it('should return empty source map property `sources`', () => {
- assert.deepEqual(sourceMapSources, []);
- });
- it('should return empty source map property `mappings`', () => {
- assert.isNotOk(sourceMapMappings);
- });
- });
- });
- describe('variable inside global scope', () => {
- describe('Variant #1: without `renameGlobals` option', () => {
- const regExp: RegExp = /^var test *= *0x\d+;$/;
- let obfuscatedCode: string;
- beforeEach(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/simple-input-1.js');
- obfuscatedCode = JavaScriptObfuscator.obfuscate(
- code,
- {
- ...NO_ADDITIONAL_NODES_PRESET
- }
- ).getObfuscatedCode();
- });
- it('should return correct obfuscated code', () => {
- assert.match(obfuscatedCode, regExp);
- });
- });
- describe('Variant #2: with `renameGlobals` option', () => {
- const regExp: RegExp = /^var _0x(\w){4,6} *= *0x\d+;$/;
- let obfuscatedCode: string;
- beforeEach(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/simple-input-1.js');
- obfuscatedCode = JavaScriptObfuscator.obfuscate(
- code,
- {
- ...NO_ADDITIONAL_NODES_PRESET,
- renameGlobals: true
- }
- ).getObfuscatedCode();
- });
- it('should return correct obfuscated code', () => {
- assert.match(obfuscatedCode, regExp);
- });
- });
- describe('Variant #3: with `renameGlobals` and `identifiersPrefix` options', () => {
- const regExp: RegExp = /^var foo_0x(\w){4,6} *= *0x\d+;$/;
- let obfuscatedCode: string;
- beforeEach(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/simple-input-1.js');
- obfuscatedCode = JavaScriptObfuscator.obfuscate(
- code,
- {
- ...NO_ADDITIONAL_NODES_PRESET,
- renameGlobals: true,
- identifiersPrefix: 'foo'
- }
- ).getObfuscatedCode();
- });
- it('should return correct obfuscated code', () => {
- assert.match(obfuscatedCode, regExp);
- });
- });
- describe('Variant #4: with `stringArray`, `renameGlobals` and `identifiersPrefix` options', () => {
- const stringArrayRegExp: RegExp = /^var foo_0x(\w){4} *= *\['abc'\];/;
- const stringArrayCallRegExp: RegExp = /var foo_0x(\w){4,6} *= *foo_0x(\w){4}\('0x0'\);$/;
- let obfuscatedCode: string;
- beforeEach(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/simple-input-2.js');
- obfuscatedCode = JavaScriptObfuscator.obfuscate(
- code,
- {
- ...NO_ADDITIONAL_NODES_PRESET,
- renameGlobals: true,
- identifiersPrefix: 'foo',
- stringArray: true,
- stringArrayThreshold: 1
- }
- ).getObfuscatedCode();
- });
- it('match #1: should return correct obfuscated code', () => {
- assert.match(obfuscatedCode, stringArrayRegExp);
- });
- it('match #2: should return correct obfuscated code', () => {
- assert.match(obfuscatedCode, stringArrayCallRegExp);
- });
- });
- });
- describe('variable inside block scope', () => {
- const regExp: RegExp = /^\(function *\(\) *\{ *var _0x[\w]+ *= *0x\d+; *\}(\(\)\)|\)\(\));?$/;
- let obfuscatedCode: string;
- beforeEach(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/block-scope.js');
- obfuscatedCode = JavaScriptObfuscator.obfuscate(
- code,
- {
- ...NO_ADDITIONAL_NODES_PRESET
- }
- ).getObfuscatedCode();
- });
- it('should return correct obfuscated code', () => {
- assert.match(obfuscatedCode, regExp);
- });
- });
- describe('variables inside global and block scopes', () => {
- describe('Variant #1: with `renameGlobals` and `identifiersPrefix` options', () => {
- const variableDeclaration1: RegExp = /var foo_0x(\w){4,6} *= *0x1;/;
- const variableDeclaration2: RegExp = /var foo_0x(\w){4,6} *= *0x2;/;
- const variableDeclaration3: RegExp = /var _0x(\w){4,6} *= *foo_0x(\w){4,6} *\+ *foo_0x(\w){4,6}/;
- const functionDeclaration: RegExp = /var foo_0x(\w){4,6} *= *function/;
- let obfuscatedCode: string;
- beforeEach(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/identifiers-prefix.js');
- obfuscatedCode = JavaScriptObfuscator.obfuscate(
- code,
- {
- ...NO_ADDITIONAL_NODES_PRESET,
- renameGlobals: true,
- identifiersPrefix: 'foo'
- }
- ).getObfuscatedCode();
- });
- it('match #1: should return correct obfuscated code', () => {
- assert.match(obfuscatedCode, variableDeclaration1);
- });
- it('match #2: should return correct obfuscated code', () => {
- assert.match(obfuscatedCode, variableDeclaration2);
- });
- it('match #3: should return correct obfuscated code', () => {
- assert.match(obfuscatedCode, variableDeclaration3);
- });
- it('match #4: should return correct obfuscated code', () => {
- assert.match(obfuscatedCode, functionDeclaration);
- });
- });
- });
- describe('latin literal variable value', () => {
- const stringArrayLatinRegExp: RegExp = /^var _0x(\w){4} *= *\['abc'\];/;
- const stringArrayCallRegExp: RegExp = /var test *= *_0x(\w){4}\('0x0'\);$/;
- let obfuscatedCode: string;
- beforeEach(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/simple-input-2.js');
- obfuscatedCode = JavaScriptObfuscator.obfuscate(
- code,
- {
- ...NO_ADDITIONAL_NODES_PRESET,
- stringArray: true,
- stringArrayThreshold: 1
- }
- ).getObfuscatedCode();
- });
- it('match #1: should return correct obfuscated code', () => {
- assert.match(obfuscatedCode, stringArrayLatinRegExp);
- });
- it('match #2: should return correct obfuscated code', () => {
- assert.match(obfuscatedCode, stringArrayCallRegExp);
- });
- });
- describe('cyrillic literal variable value', () => {
- const stringArrayCyrillicRegExp: RegExp = /^var _0x(\w){4} *= *\['абц'\];/;
- const stringArrayCallRegExp: RegExp = /var test *= *_0x(\w){4}\('0x0'\);$/;
- let obfuscatedCode: string;
- beforeEach(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/simple-input-cyrillic.js');
- obfuscatedCode = JavaScriptObfuscator.obfuscate(
- code,
- {
- ...NO_ADDITIONAL_NODES_PRESET,
- stringArray: true,
- stringArrayThreshold: 1
- }
- ).getObfuscatedCode();
- });
- it('match #1: should return correct obfuscated code', () => {
- assert.match(obfuscatedCode, stringArrayCyrillicRegExp);
- });
- it('match #2: should return correct obfuscated code', () => {
- assert.match(obfuscatedCode, stringArrayCallRegExp);
- });
- });
- describe('seed', function () {
- this.timeout(60000);
- describe('same seed on each run', () => {
- const code: string = readFileAsString('./test/fixtures/sample.js');
- const samples: number = 100;
- let obfuscatedCode1: string,
- obfuscatedCode2: string,
- seed: number = 12345,
- equalsCount: number = 0;
- beforeEach(() => {
- for (let i: number = 0; i < samples; i++) {
- if (i % 20 === 0) {
- seed++;
- }
- obfuscatedCode1 = JavaScriptObfuscator.obfuscate(
- code,
- {
- seed: seed
- }
- ).getObfuscatedCode();
- obfuscatedCode2 = JavaScriptObfuscator.obfuscate(
- code,
- {
- seed: seed
- }
- ).getObfuscatedCode();
- if (obfuscatedCode1 === obfuscatedCode2) {
- equalsCount++;
- }
- }
- });
- it('should return same code every time with same `seed`', () => {
- assert.equal(equalsCount, samples);
- });
- });
- describe('Variant #1: different seed on each run', () => {
- const code: string = readFileAsString('./test/fixtures/sample.js');
- let obfuscatedCode1: string,
- obfuscatedCode2: string;
- beforeEach(() => {
- obfuscatedCode1 = JavaScriptObfuscator.obfuscate(
- code,
- {
- seed: 12345
- }
- ).getObfuscatedCode();
- obfuscatedCode2 = JavaScriptObfuscator.obfuscate(
- code,
- {
- seed: 12346
- }
- ).getObfuscatedCode();
- });
- it('should return different obfuscated code with different `seed` option value', () => {
- assert.notEqual(obfuscatedCode1, obfuscatedCode2);
- });
- });
- describe('Variant #2: different seed on each run', () => {
- const code: string = readFileAsString('./test/fixtures/sample.js');
- let obfuscatedCode1: string,
- obfuscatedCode2: string;
- beforeEach(() => {
- obfuscatedCode1 = JavaScriptObfuscator.obfuscate(
- code,
- {
- seed: 0
- }
- ).getObfuscatedCode();
- obfuscatedCode2 = JavaScriptObfuscator.obfuscate(
- code,
- {
- seed: 0
- }
- ).getObfuscatedCode();
- });
- it('should return different obfuscated code with different `seed` option value', () => {
- assert.notEqual(obfuscatedCode1, obfuscatedCode2);
- });
- });
- describe('Variant #3: same seed for different source code', () => {
- const code1: string = readFileAsString(__dirname + '/fixtures/simple-input-cyrillic.js');
- const code2: string = readFileAsString(__dirname + '/fixtures/simple-input-2.js');
- const regExp: RegExp = /var (_0x(\w){4}) *= *\['.*'\];/;
- let match1: string,
- match2: string;
- beforeEach(() => {
- const obfuscatedCode1: string = JavaScriptObfuscator.obfuscate(
- code1,
- {
- seed: 123,
- stringArrayThreshold: 1
- }
- ).getObfuscatedCode();
- const obfuscatedCode2: string = JavaScriptObfuscator.obfuscate(
- code2,
- {
- seed: 123,
- stringArrayThreshold: 1
- }
- ).getObfuscatedCode();
- match1 = getRegExpMatch(obfuscatedCode1, regExp);
- match2 = getRegExpMatch(obfuscatedCode2, regExp);
- });
- it('should return different String Array names for different source code with same seed', () => {
- assert.notEqual(match1, match2);
- });
- });
- });
- /**
- * https://github.com/estools/escodegen/pull/408
- */
- describe('`ObjectPattern` with single `RestElement`', () => {
- const regExp: RegExp = /const {\.\.\.foo} *= *{};/;
- let obfuscatedCode: string;
- beforeEach(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/object-pattern-single-rest-element.js');
- obfuscatedCode = JavaScriptObfuscator.obfuscate(
- code,
- {
- ...NO_ADDITIONAL_NODES_PRESET
- }
- ).getObfuscatedCode();
- });
- it('should not break on `ObjectPattern` with single `RestElement`', () => {
- assert.match(obfuscatedCode, regExp);
- });
- });
- /**
- * https://github.com/estools/escodegen/pull/415
- */
- describe('Precedence of `SequenceExpression` in computed property', () => {
- const regExp: RegExp = /class Foo *{ *\[\(bar, *baz\)]\(\) *{ *} * *}/;
- let obfuscatedCode: string;
- beforeEach(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/precedence-of-sequence-expression-in-computed-property.js');
- obfuscatedCode = JavaScriptObfuscator.obfuscate(
- code,
- {
- ...NO_ADDITIONAL_NODES_PRESET
- }
- ).getObfuscatedCode();
- });
- it('should generate a valid js code', () => {
- assert.match(obfuscatedCode, regExp);
- });
- });
- describe('new.target MetaProperty', () => {
- const regExp: RegExp = /new\.target *=== *Foo/;
- let obfuscatedCode: string;
- beforeEach(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/new-target.js');
- obfuscatedCode = JavaScriptObfuscator.obfuscate(
- code,
- {
- ...NO_ADDITIONAL_NODES_PRESET
- }
- ).getObfuscatedCode();
- });
- it('should keep new.target MetaProperty', () => {
- assert.match(obfuscatedCode, regExp);
- });
- });
- describe('import.meta support', () => {
- const regExp: RegExp = /console\['log']\(import\.meta\['url']\);/;
- let obfuscatedCode: string;
- beforeEach(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/import-meta.js');
- obfuscatedCode = JavaScriptObfuscator.obfuscate(
- code,
- {
- ...NO_ADDITIONAL_NODES_PRESET
- }
- ).getObfuscatedCode();
- });
- it('should support `import.meta`', () => {
- assert.match(obfuscatedCode, regExp);
- });
- });
- /**
- * https://github.com/estools/escodegen/pull/407
- */
- describe('valid exponentiation operator precedence', () => {
- const regExp: RegExp = /var foo *= *\( *0x1 *- *0x2 *\) *\*\* *0x2;/;
- let obfuscatedCode: string;
- beforeEach(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/exponentiation-operator-precedence.js');
- obfuscatedCode = JavaScriptObfuscator.obfuscate(
- code,
- {
- ...NO_ADDITIONAL_NODES_PRESET
- }
- ).getObfuscatedCode();
- });
- it('should support exponentiation operator', () => {
- assert.match(obfuscatedCode, regExp);
- });
- });
- describe('BigInt support', () => {
- const regExp: RegExp = /return 0x20000000000001n *\+ *0xan *\+ *0xan;/;
- let obfuscatedCode: string;
- beforeEach(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/bigint-support.js');
- obfuscatedCode = JavaScriptObfuscator.obfuscate(
- code,
- {
- ...NO_ADDITIONAL_NODES_PRESET
- }
- ).getObfuscatedCode();
- });
- it('should support BigInt', () => {
- assert.match(obfuscatedCode, regExp);
- });
- });
- describe('Optional chaining support', () => {
- const regExp: RegExp = new RegExp(
- 'const _0x(\\w){4,6} *= *{ *' +
- '\'bar\': *\\(\\) *=> *{} *' +
- '}; *' +
- '_0x(\\w){4,6}\\?\\.\\[\'bar\']\\?\\.\\(\\);'
- );
- let obfuscatedCode: string;
- beforeEach(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/optional-chaining-support.js');
- obfuscatedCode = JavaScriptObfuscator.obfuscate(
- code,
- {
- ...NO_ADDITIONAL_NODES_PRESET
- }
- ).getObfuscatedCode();
- });
- it('should support optional chaining', () => {
- assert.match(obfuscatedCode, regExp);
- });
- });
- describe('mangled identifier names generator', () => {
- const regExp: RegExp = /var c *= *0x1/;
- let obfuscatedCode: string;
- beforeEach(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/mangle.js');
- obfuscatedCode = JavaScriptObfuscator.obfuscate(
- code,
- {
- ...NO_ADDITIONAL_NODES_PRESET,
- identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator,
- stringArray: true,
- stringArrayThreshold: 1
- }
- ).getObfuscatedCode();
- });
- it('should mangle obfuscated code', () => {
- assert.match(obfuscatedCode, regExp);
- });
- });
- describe('mangled shuffled identifier names generator', () => {
- const regExp: RegExp = /var [a-zA-Z] *= *0x1/;
- let obfuscatedCode: string;
- beforeEach(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/mangle.js');
- obfuscatedCode = JavaScriptObfuscator.obfuscate(
- code,
- {
- identifierNamesGenerator: IdentifierNamesGenerator.MangledShuffledIdentifierNamesGenerator
- }
- ).getObfuscatedCode();
- });
- it('should mangle obfuscated code', () => {
- assert.match(obfuscatedCode, regExp);
- });
- });
- describe('dictionary identifier names generator', () => {
- const regExp1: RegExp = /var [abc] *= *0x1; *var [abc] *= *0x2; *var [abc] *= *0x3;/;
- const regExp2: RegExp = /var [ABC] *= *0x4; *var [ABC] *= *0x5; *var [ABC] *= *0x6;/;
- let obfuscatedCode: string;
- beforeEach(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/dictionary-identifiers.js');
- obfuscatedCode = JavaScriptObfuscator.obfuscate(
- code,
- {
- ...NO_ADDITIONAL_NODES_PRESET,
- identifierNamesGenerator: IdentifierNamesGenerator.DictionaryIdentifierNamesGenerator,
- identifiersDictionary: ['a', 'b', 'c']
- }
- ).getObfuscatedCode();
- });
- it('Match #1: should generate identifier based on the dictionary', () => {
- assert.match(obfuscatedCode, regExp1);
- });
- it('Match #2: should generate identifier based on the dictionary', () => {
- assert.match(obfuscatedCode, regExp2);
- });
- });
- describe('parse module', () => {
- describe('Variant #1: import', () => {
- const importRegExp: RegExp = /import *{foo} *from *'.\/foo';/;
- const variableDeclarationRegExp: RegExp = /var test *= *0x1/;
- let obfuscatedCode: string;
- beforeEach(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/parse-module-1.js');
- obfuscatedCode = JavaScriptObfuscator.obfuscate(code).getObfuscatedCode();
- });
- it('Match #!: should correctly obfuscate a import', () => {
- assert.match(obfuscatedCode, importRegExp);
- });
- it('Match #2: should correctly obfuscate a module', () => {
- assert.match(obfuscatedCode, variableDeclarationRegExp);
- });
- });
- describe('Variant #2: export', () => {
- const regExp: RegExp = /export *const foo *= *0x1;/;
- let obfuscatedCode: string;
- beforeEach(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/parse-module-2.js');
- obfuscatedCode = JavaScriptObfuscator.obfuscate(code).getObfuscatedCode();
- });
- it('should correctly obfuscate a module', () => {
- assert.match(obfuscatedCode, regExp);
- });
- });
- });
- describe('3.5k variables', function () {
- this.timeout(200000);
- const expectedValue: number = 3500;
- let result: number;
- beforeEach(() => {
- const code: string = buildLargeCode(expectedValue);
- const obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
- code,
- {
- compact: true,
- controlFlowFlattening: true,
- controlFlowFlatteningThreshold: 1,
- deadCodeInjection: true,
- deadCodeInjectionThreshold: 1,
- disableConsoleOutput: false,
- numbersToExpressions: true,
- simplify: true,
- renameProperties: true,
- rotateStringArray: true,
- stringArray: true,
- stringArrayEncoding: [
- StringArrayEncoding.Base64,
- StringArrayEncoding.Rc4
- ],
- stringArrayWrappersChainedCalls: true,
- stringArrayWrappersCount: 10,
- stringArrayWrappersType: StringArrayWrappersType.Function,
- stringArrayThreshold: 1,
- transformObjectKeys: true,
- unicodeEscapeSequence: false
- }
- ).getObfuscatedCode();
- result = eval(obfuscatedCode);
- });
- it('should correctly obfuscate 3.5k variables', () => {
- assert.equal(result, expectedValue);
- });
- });
- describe('Eval `Hello World`', function () {
- this.timeout(20000);
- const samplesCount: number = 100;
- const expectedEvaluationResult: string = 'aaabbbcccdddeee';
- let isEvaluationSuccessful: boolean = true;
- before(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/eval-hello-world.js');
- for (let i = 0; i < samplesCount; i++) {
- const obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
- code,
- {
- ...NO_ADDITIONAL_NODES_PRESET,
- compact: false,
- controlFlowFlattening: true,
- controlFlowFlatteningThreshold: 1,
- deadCodeInjection: true,
- deadCodeInjectionThreshold: 1,
- disableConsoleOutput: true,
- identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator,
- renameProperties: true,
- simplify: false,
- stringArray: true,
- stringArrayThreshold: 1,
- stringArrayWrappersChainedCalls: true,
- stringArrayWrappersCount: 1,
- stringArrayWrappersType: StringArrayWrappersType.Variable
- }
- ).getObfuscatedCode();
- const evaluationResult: string = eval(obfuscatedCode);
- if (evaluationResult !== expectedEvaluationResult) {
- isEvaluationSuccessful = false;
- break;
- }
- }
- });
- it('should correctly evaluate obfuscated code', () => {
- assert.equal(isEvaluationSuccessful, true);
- });
- });
- describe('Identifier names collision between base code and appended string array nodes', function () {
- this.timeout(10000);
- const samplesCount: number = 30;
- let areCollisionsExists: boolean = false;
- let obfuscateFunc: (identifierNamesGenerator: TypeFromEnum<typeof IdentifierNamesGenerator>) => string;
- before(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/custom-nodes-identifier-names-collision.js');
- obfuscateFunc = (identifierNamesGenerator: TypeFromEnum<typeof IdentifierNamesGenerator>) => {
- const obfuscatedCode = JavaScriptObfuscator.obfuscate(
- code,
- {
- identifierNamesGenerator,
- compact: false,
- renameGlobals: true,
- identifiersDictionary: ['foo', 'bar', 'baz', 'bark', 'hawk', 'foozmos', 'cow', 'chikago'],
- stringArray: true
- }
- ).getObfuscatedCode();
- return obfuscatedCode;
- };
- [
- IdentifierNamesGenerator.DictionaryIdentifierNamesGenerator,
- IdentifierNamesGenerator.MangledIdentifierNamesGenerator
- ].forEach((identifierNamesGenerator: TypeFromEnum<typeof IdentifierNamesGenerator>) => {
- for (let i = 0; i < samplesCount; i++) {
- try {
- eval(obfuscateFunc(identifierNamesGenerator));
- } catch {
- areCollisionsExists = true;
- break;
- }
- }
- });
- });
- it('It does not create identifier names collision', () => {
- assert.equal(areCollisionsExists, false);
- });
- });
- describe('Prevailing kind of variables', () => {
- const baseParams: TInputOptions = {
- compact: true,
- controlFlowFlattening: true,
- controlFlowFlatteningThreshold: 1,
- deadCodeInjection: true,
- deadCodeInjectionThreshold: 1,
- debugProtection: true,
- debugProtectionInterval: true,
- disableConsoleOutput: false,
- rotateStringArray: true,
- selfDefending: true,
- stringArray: true,
- stringArrayThreshold: 1,
- transformObjectKeys: true,
- unicodeEscapeSequence: false
- };
- describe('`var` kind', function () {
- let obfuscatedCode: string;
- before(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/prevailing-kind-of-variables-var.js');
- obfuscatedCode = JavaScriptObfuscator.obfuscate(
- code,
- {
- ...NO_ADDITIONAL_NODES_PRESET,
- ...baseParams,
- stringArrayEncoding: [StringArrayEncoding.Rc4]
- }
- ).getObfuscatedCode();
- });
- it('does not break on run', () => {
- assert.doesNotThrow(() => eval(obfuscatedCode));
- });
- });
- describe('`const` kind', function () {
- describe('Variant #1: StringArrayEncoding: rc4', () => {
- let obfuscatedCode: string;
- before(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/prevailing-kind-of-variables-const.js');
- obfuscatedCode = JavaScriptObfuscator.obfuscate(
- code,
- {
- ...NO_ADDITIONAL_NODES_PRESET,
- ...baseParams,
- stringArrayEncoding: [StringArrayEncoding.Rc4]
- }
- ).getObfuscatedCode();
- });
- it('does not break on run', () => {
- assert.doesNotThrow(() => eval(obfuscatedCode));
- });
- });
- describe('Variant #2: StringArrayEncoding: base64', () => {
- let obfuscatedCode: string;
- before(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/prevailing-kind-of-variables-const.js');
- obfuscatedCode = JavaScriptObfuscator.obfuscate(
- code,
- {
- ...NO_ADDITIONAL_NODES_PRESET,
- ...baseParams,
- stringArrayEncoding: [StringArrayEncoding.Rc4]
- }
- ).getObfuscatedCode();
- });
- it('does not break on run', () => {
- assert.doesNotThrow(() => eval(obfuscatedCode));
- });
- });
- });
- describe('`let` kind', function () {
- describe('Variant #1: StringArrayEncoding: rc4', () => {
- let obfuscatedCode: string;
- before(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/prevailing-kind-of-variables-let.js');
- obfuscatedCode = JavaScriptObfuscator.obfuscate(
- code,
- {
- ...NO_ADDITIONAL_NODES_PRESET,
- ...baseParams,
- stringArrayEncoding: [StringArrayEncoding.Rc4]
- }
- ).getObfuscatedCode();
- });
- it('does not break on run', () => {
- assert.doesNotThrow(() => eval(obfuscatedCode));
- });
- });
- describe('Variant #2: StringArrayEncoding: base64', () => {
- let obfuscatedCode: string;
- before(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/prevailing-kind-of-variables-let.js');
- obfuscatedCode = JavaScriptObfuscator.obfuscate(
- code,
- {
- ...NO_ADDITIONAL_NODES_PRESET,
- ...baseParams,
- stringArrayEncoding: [StringArrayEncoding.Base64]
- }
- ).getObfuscatedCode();
- });
- it('does not break on run', () => {
- assert.doesNotThrow(() => eval(obfuscatedCode));
- });
- });
- });
- });
- });
- describe('obfuscateMultiple', () => {
- describe('multiple source codes', () => {
- const regExp1: RegExp = /var a0_0x(\w){4,6} *= *0x1;/;
- const regExp2: RegExp = /var a1_0x(\w){4,6} *= *'abc';/;
- let obfuscatedCode1: string;
- let obfuscatedCode2: string;
- beforeEach(() => {
- const sourceCode1: string = readFileAsString(__dirname + '/fixtures/simple-input-1.js');
- const sourceCode2: string = readFileAsString(__dirname + '/fixtures/simple-input-2.js');
- const obfuscationResultsObject = JavaScriptObfuscator.obfuscateMultiple(
- {
- sourceCode1,
- sourceCode2
- },
- {
- ...NO_ADDITIONAL_NODES_PRESET,
- renameGlobals: true
- }
- );
- obfuscatedCode1 = obfuscationResultsObject.sourceCode1.getObfuscatedCode();
- obfuscatedCode2 = obfuscationResultsObject.sourceCode2.getObfuscatedCode();
- });
- it('Match #1: should return correct obfuscated code', () => {
- assert.match(obfuscatedCode1, regExp1);
- });
- it('Match #2: should return correct obfuscated code', () => {
- assert.match(obfuscatedCode2, regExp2);
- });
- });
- describe('invalid source codes object', () => {
- let testFunc: () => TDictionary<IObfuscatedCode>;
- beforeEach(() => {
- testFunc = () => JavaScriptObfuscator.obfuscateMultiple(
- 'foo' as any,
- {
- ...NO_ADDITIONAL_NODES_PRESET,
- renameGlobals: true
- }
- );
- });
- it('Should throw an error if source codes object is not a plain object', () => {
- assert.throw(testFunc, Error);
- });
- });
- });
- describe('getOptionsByPreset', () => {
- describe('Variant #1: base behaviour', () => {
- const optionsPresetName: TOptionsPreset = OptionsPreset.HighObfuscation;
- let options: TInputOptions;
- before(() => {
- options = JavaScriptObfuscator.getOptionsByPreset(optionsPresetName);
- });
- it('Should return options for passed options preset name', () => {
- assert.deepEqual(options, HIGH_OBFUSCATION_PRESET);
- });
- });
- describe('Variant #2: unknown options preset name', () => {
- const optionsPresetName: TOptionsPreset = 'foobar' as TOptionsPreset;
- let testFunc: () => TInputOptions;
- before(() => {
- testFunc = () => JavaScriptObfuscator.getOptionsByPreset(optionsPresetName);
- });
- it('Should throws an error when unknown option preset is passed', () => {
- assert.throws(testFunc, 'Options for preset name `foobar` are not found');
- });
- });
- });
- });
|