123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994 |
- import { assert } from 'chai';
- import { TypeFromEnum } from '@gradecam/tsenum';
- import { TInputOptions } from '../../../src/types/options/TInputOptions';
- import { TDictionary } from '../../../src/types/TDictionary';
- import { IObfuscatedCode } from '../../../src/interfaces/source-code/IObfuscatedCode';
- import { SourceMapMode } from '../../../src/enums/source-map/SourceMapMode';
- import { StringArrayEncoding } from '../../../src/enums/StringArrayEncoding';
- import { JavaScriptObfuscator } from '../../../src/JavaScriptObfuscatorFacade';
- import { NO_ADDITIONAL_NODES_PRESET } from '../../../src/options/presets/NoCustomNodes';
- import { IdentifierNamesGenerator } from '../../../src/enums/generators/identifier-names-generators/IdentifierNamesGenerator';
- 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);
- });
- });
- });
- 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('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,
- {
- identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator
- }
- ).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,
- rotateStringArray: true,
- stringArray: true,
- stringArrayEncoding: StringArrayEncoding.Rc4,
- stringArrayThreshold: 1,
- transformObjectKeys: true,
- unicodeEscapeSequence: false
- }
- ).getObfuscatedCode();
- result = eval(obfuscatedCode);
- });
- it('should correctly obfuscate 3.5k variables', () => {
- assert.equal(result, expectedValue);
- });
- });
- 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);
- });
- });
- });
- });
|