123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650 |
- import * as fs from 'fs';
- import { assert } from 'chai';
- import { resolveSources } from 'source-map-resolve';
- import { TDictionary } from '../../../src/types/TDictionary';
- import { TIdentifierNamesCache } from '../../../src/types/TIdentifierNamesCache';
- import { TInputOptions } from '../../../src/types/options/TInputOptions';
- import { TOptionsPreset } from '../../../src/types/options/TOptionsPreset';
- import { TTypeFromEnum } from '../../../src/types/utils/TTypeFromEnum';
- import { IObfuscationResult } from '../../../src/interfaces/source-code/IObfuscationResult';
- import { ISourceMap } from '../../../src/interfaces/source-code/ISourceMap';
- import { SourceMapMode } from '../../../src/enums/source-map/SourceMapMode';
- import { SourceMapSourcesMode } from '../../../src/enums/source-map/SourceMapSourcesMode';
- import { StringArrayEncoding } from '../../../src/enums/node-transformers/string-array-transformers/StringArrayEncoding';
- import { StringArrayIndexesType } from '../../../src/enums/node-transformers/string-array-transformers/StringArrayIndexesType';
- 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 { getStringArrayRegExp } from '../../helpers/get-string-array-regexp';
- 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 obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
- code,
- {
- ...NO_ADDITIONAL_NODES_PRESET
- }
- );
- obfuscatedCode = obfuscationResult.getObfuscatedCode();
- sourceMap = obfuscationResult.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 code: string,
- obfuscatedCode: string,
- sourceMap: ISourceMap,
- resolvedSources: string;
- beforeEach((done) => {
- code = readFileAsString(__dirname + '/fixtures/simple-input-1.js');
- const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
- code,
- {
- ...NO_ADDITIONAL_NODES_PRESET,
- sourceMap: true
- }
- );
- obfuscatedCode = obfuscationResult.getObfuscatedCode();
- sourceMap = JSON.parse(obfuscationResult.getSourceMap());
- resolveSources(sourceMap, '/', fs.readFile, (error, result) => {
- resolvedSources = typeof result.sourcesContent[0] === 'string'
- ? result.sourcesContent[0]
- : '';
- done();
- });
- });
- it('should return correct obfuscated code', () => {
- assert.isOk(obfuscatedCode);
- });
- it('should return correct source map mappings', () => {
- assert.isOk(sourceMap.mappings);
- });
- it('should define placeholder `sources` field for source map', () => {
- assert.deepEqual(sourceMap.sources, ['sourceMap']);
- });
- it('should define `sourcesContent` field for source map', () => {
- assert.isOk(sourceMap.sourcesContent);
- });
- it('should resolve correct sources from source map', () => {
- assert.equal(resolvedSources, code);
- });
- });
- describe('`sourceMapMode` is `inline`', () => {
- const regExp: RegExp = /sourceMappingURL=data:application\/json;base64/;
- let code: string,
- obfuscatedCode: string,
- sourceMap: ISourceMap,
- resolvedSources: string;
- beforeEach((done) => {
- code = readFileAsString(__dirname + '/fixtures/simple-input-1.js');
- const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
- code,
- {
- ...NO_ADDITIONAL_NODES_PRESET,
- sourceMap: true,
- sourceMapMode: SourceMapMode.Inline
- }
- );
- obfuscatedCode = obfuscationResult.getObfuscatedCode();
- sourceMap = JSON.parse(obfuscationResult.getSourceMap());
- resolveSources(sourceMap, '/', fs.readFile, (error, result) => {
- resolvedSources = typeof result.sourcesContent[0] === 'string'
- ? result.sourcesContent[0]
- : '';
- done();
- });
- });
- 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 mappings', () => {
- assert.isOk(sourceMap.mappings);
- });
- it('should define placeholder `sources` field for source map', () => {
- assert.deepEqual(sourceMap.sources, ['sourceMap']);
- });
- it('should define `sourcesContent` field for source map', () => {
- assert.isOk(sourceMap.sourcesContent);
- });
- it('should resolve correct sources from source map', () => {
- console.log(resolvedSources);
- assert.equal(resolvedSources, code);
- });
- });
- describe('empty source code', () => {
- let obfuscatedCode: string,
- sourceMapNames: string[],
- sourceMapSources: string[],
- sourceMapMappings: string;
- beforeEach(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/empty-input.js');
- const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
- code,
- {
- sourceMap: true
- }
- );
- obfuscatedCode = obfuscationResult.getObfuscatedCode();
- const sourceMapObject: any = JSON.parse(obfuscationResult.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('`sourceMapSourceMode` is set', () => {
- describe('`sourcesContent` value', () => {
- let code: string,
- obfuscatedCode: string,
- sourceMap: ISourceMap,
- resolvedSources: string;
- beforeEach((done) => {
- code = readFileAsString(__dirname + '/fixtures/simple-input-1.js');
- const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
- code,
- {
- ...NO_ADDITIONAL_NODES_PRESET,
- sourceMap: true,
- sourceMapSourcesMode: SourceMapSourcesMode.SourcesContent
- }
- );
- obfuscatedCode = obfuscationResult.getObfuscatedCode();
- sourceMap = JSON.parse(obfuscationResult.getSourceMap());
- resolveSources(sourceMap, '/', fs.readFile, (error, result) => {
- resolvedSources = typeof result.sourcesContent[0] === 'string'
- ? result.sourcesContent[0]
- : '';
- done();
- });
- });
- it('should return correct obfuscated code', () => {
- assert.isOk(obfuscatedCode);
- });
- it('should return correct source map mappings', () => {
- assert.isOk(sourceMap.mappings);
- });
- it('should define `sources` field for source map', () => {
- assert.property(sourceMap, 'sources');
- });
- it('should define `sourcesContent` field for source map', () => {
- assert.property(sourceMap, 'sourcesContent');
- });
- it('should define placeholder `sources` field for source map', () => {
- assert.deepEqual(sourceMap.sources, ['sourceMap']);
- });
- it('should define `sourcesContent` value with source code', () => {
- assert.deepEqual(sourceMap.sourcesContent[0], code);
- });
- it('should resolve correct sources from source map', () => {
- assert.equal(resolvedSources, code);
- });
- });
- describe('`sources` value', () => {
- describe('`inputFileName` option is set', () => {
- let code: string,
- obfuscatedCode: string,
- sourceMap: ISourceMap;
- beforeEach(() => {
- code = readFileAsString(__dirname + '/fixtures/simple-input-1.js');
- const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
- code,
- {
- ...NO_ADDITIONAL_NODES_PRESET,
- inputFileName: 'someFile.js',
- sourceMap: true,
- sourceMapSourcesMode: SourceMapSourcesMode.Sources
- }
- );
- obfuscatedCode = obfuscationResult.getObfuscatedCode();
- sourceMap = JSON.parse(obfuscationResult.getSourceMap());
- });
- it('should return correct obfuscated code', () => {
- assert.isOk(obfuscatedCode);
- });
- it('should return correct source map mappings', () => {
- assert.isOk(sourceMap.mappings);
- });
- it('should define `sources` field for source map', () => {
- assert.property(sourceMap, 'sources');
- });
- it('should not define `sourcesContent` field for source map', () => {
- assert.notProperty(sourceMap, 'sourcesContent');
- });
- it('should define placeholder `sources` field for source map', () => {
- assert.deepEqual(sourceMap.sources, ['someFile.js']);
- });
- })
- describe('`inputFileName` option is not set', () => {
- let testFunc: () => IObfuscationResult;
- beforeEach(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/simple-input-1.js');
- testFunc = () => JavaScriptObfuscator.obfuscate(
- code,
- {
- ...NO_ADDITIONAL_NODES_PRESET,
- sourceMap: true,
- sourceMapSourcesMode: SourceMapSourcesMode.Sources
- }
- );
- });
- it('should throw an error', () => {
- assert.throws(testFunc, Error);
- });
- });
- });
- });
- });
- 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 = new RegExp(
- 'function foo_0x([a-f0-9]){4} *\\(\\) *{' +
- 'var _0x([a-f0-9]){4,6} *= *\\[\'abc\'];.*' +
- 'return foo_0x([a-f0-9]){4}\\(\\); *' +
- '}'
- );
- 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 = getStringArrayRegExp(['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 = new RegExp(
- 'function _0x(\\w){4} *\\(\\) *{' +
- 'var _0x([a-f0-9]){4,6} *= *\\[\'абц\'];.*' +
- 'return _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 = new RegExp(
- 'function _0x(\\w){4} *\\(\\) *{' +
- 'var _0x([a-f0-9]){4,6} *= *\\[\'.*\'];.*' +
- 'return _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/javascript-obfuscator/javascript-obfuscator/issues/710
- */
- describe('export * as', () => {
- const regExp: RegExp = /export *\* *as foo from *'bar';/;
- let obfuscatedCode: string;
- beforeEach(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/export-all-named-support.js');
- obfuscatedCode = JavaScriptObfuscator.obfuscate(
- code,
- {
- ...NO_ADDITIONAL_NODES_PRESET
- }
- ).getObfuscatedCode();
- });
- it('should support `export * as` syntax', () => {
- 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('Nullish coalescing support', () => {
- const regExp: RegExp = /\(foo *\?\? *bar\) *&& *baz;/;
- let obfuscatedCode: string;
- beforeEach(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/nullish-coalescing-support.js');
- obfuscatedCode = JavaScriptObfuscator.obfuscate(
- code,
- {
- ...NO_ADDITIONAL_NODES_PRESET
- }
- ).getObfuscatedCode();
- });
- it('should support nullish coalescing operator', () => {
- assert.match(obfuscatedCode, regExp);
- });
- });
- describe('Numeric separators support', () => {
- const regExp: RegExp = /const foo *= *0x64;/;
- let obfuscatedCode: string;
- beforeEach(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/numeric-separators-support.js');
- obfuscatedCode = JavaScriptObfuscator.obfuscate(
- code,
- {
- ...NO_ADDITIONAL_NODES_PRESET
- }
- ).getObfuscatedCode();
- });
- it('should support numeric separators', () => {
- assert.match(obfuscatedCode, regExp);
- });
- });
- describe('Top-level await support', () => {
- const regExp: RegExp = /await 0x1;/;
- let obfuscatedCode: string;
- beforeEach(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/top-level-await-support.js');
- obfuscatedCode = JavaScriptObfuscator.obfuscate(
- code,
- {
- ...NO_ADDITIONAL_NODES_PRESET
- }
- ).getObfuscatedCode();
- });
- it('should support top-level await', () => {
- assert.match(obfuscatedCode, regExp);
- });
- });
- describe('Class static block support', () => {
- const regExp: RegExp = new RegExp(
- 'let _0x(\\w){4,6} *= *0x1; *' +
- 'class _0x(\\w){4,6} *{ *' +
- 'static *\\[\'_0x(\\w){4,6}\']; *' +
- 'static *{ *' +
- 'let _0x(\\w){4,6} *= *0x2; *' +
- '_0x(\\w){4,6}\\[\'_0x(\\w){4,6}\'] *= *0x3; *' +
- '_0x(\\w){4,6} *= *0x4; *' +
- '_0x(\\w){4,6} *= *0x5; *' +
- '} *' +
- '}'
- );
- let obfuscatedCode: string;
- beforeEach(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/class-static-block-support-1.js');
- obfuscatedCode = JavaScriptObfuscator.obfuscate(
- code,
- {
- ...NO_ADDITIONAL_NODES_PRESET,
- renameGlobals: true,
- renameProperties: true
- }
- ).getObfuscatedCode();
- console.log(obfuscatedCode);
- });
- it('should support class static block await', () => {
- assert.match(obfuscatedCode, regExp);
- });
- });
- describe('Private identifiers support', () => {
- const regExp: RegExp = new RegExp(
- 'class Foo *{ *' +
- '#bar *= *0x1; *' +
- '\\[\'method\'] *\\(\\) *{ *' +
- 'this\.#bar *= *0x2;' +
- '} *' +
- '}'
- );
- let obfuscatedCode: string;
- beforeEach(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/private-identifier.js');
- obfuscatedCode = JavaScriptObfuscator.obfuscate(
- code,
- {
- ...NO_ADDITIONAL_NODES_PRESET,
- renameProperties: true
- }
- ).getObfuscatedCode();
- });
- it('should support private identifiers', () => {
- 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 #1: 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('identifier names cache generation', () => {
- describe('Variant #1: `identifierNamesCache` and `renameGlobal` options are enabled. Existing cache is passed', () => {
- const expectedIdentifierNamesCache: TIdentifierNamesCache = {
- globalIdentifiers: {
- foo: 'a',
- bar: 'b',
- baz: 'baz_value_from_cache'
- },
- propertyIdentifiers: {}
- };
- let identifierNamesCache: TIdentifierNamesCache;
- beforeEach(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/identifier-names-cache-1.js');
- identifierNamesCache = JavaScriptObfuscator.obfuscate(
- code,
- {
- ...NO_ADDITIONAL_NODES_PRESET,
- renameGlobals: true,
- identifierNamesCache: {
- globalIdentifiers: {
- baz: 'baz_value_from_cache'
- },
- propertyIdentifiers: {}
- },
- identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator
- }
- ).getIdentifierNamesCache();
- });
- it('Match #1: should correctly generate identifier names cache', () => {
- assert.deepEqual(identifierNamesCache, expectedIdentifierNamesCache);
- });
- });
- describe('Variant #2: `identifierNamesCache` and `renameGlobal` options are enabled', () => {
- const expectedIdentifierNamesCache: TIdentifierNamesCache = {
- globalIdentifiers: {
- foo: 'a',
- bar: 'b'
- },
- propertyIdentifiers: {}
- };
- let identifierNamesCache: TIdentifierNamesCache;
- beforeEach(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/identifier-names-cache-1.js');
- identifierNamesCache = JavaScriptObfuscator.obfuscate(
- code,
- {
- ...NO_ADDITIONAL_NODES_PRESET,
- renameGlobals: true,
- identifierNamesCache: {
- globalIdentifiers: {},
- propertyIdentifiers: {}
- },
- identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator
- }
- ).getIdentifierNamesCache();
- });
- it('Match #1: should correctly generate identifier names cache', () => {
- assert.deepEqual(identifierNamesCache, expectedIdentifierNamesCache);
- });
- });
- describe('Variant #3: `identifierNamesCache` and `renameGlobal` options are enabled. Source code without global variables', () => {
- const expectedIdentifierNamesCache: TIdentifierNamesCache = {
- globalIdentifiers: {},
- propertyIdentifiers: {}
- };
- let identifierNamesCache: TIdentifierNamesCache;
- beforeEach(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/identifier-names-cache-2.js');
- identifierNamesCache = JavaScriptObfuscator.obfuscate(
- code,
- {
- ...NO_ADDITIONAL_NODES_PRESET,
- renameGlobals: true,
- identifierNamesCache: {
- globalIdentifiers: {},
- propertyIdentifiers: {}
- },
- identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator
- }
- ).getIdentifierNamesCache();
- });
- it('Match #1: should correctly generate identifier names cache', () => {
- assert.deepEqual(identifierNamesCache, expectedIdentifierNamesCache);
- });
- });
- describe('Variant #4: `identifierNamesCache` option is disabled', () => {
- const expectedIdentifierNamesCache: TIdentifierNamesCache = null;
- let identifierNamesCache: TIdentifierNamesCache;
- beforeEach(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/identifier-names-cache-1.js');
- identifierNamesCache = JavaScriptObfuscator.obfuscate(
- code,
- {
- ...NO_ADDITIONAL_NODES_PRESET,
- renameGlobals: true,
- identifierNamesCache: null,
- identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator
- }
- ).getIdentifierNamesCache();
- });
- it('Match #1: should correctly generate identifier names cache', () => {
- assert.deepEqual(identifierNamesCache, expectedIdentifierNamesCache);
- });
- });
- });
- 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,
- stringArrayRotate: true,
- stringArray: true,
- stringArrayEncoding: [
- StringArrayEncoding.Base64,
- StringArrayEncoding.Rc4
- ],
- stringArrayIndexesType: [
- StringArrayIndexesType.HexadecimalNumber,
- StringArrayIndexesType.HexadecimalNumericString
- ],
- stringArrayIndexShift: true,
- stringArrayWrappersChainedCalls: true,
- stringArrayWrappersCount: 10,
- stringArrayWrappersParametersMaxCount: 5,
- 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 = 'Hello World';
- 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 collisionError: string | null = null;
- let obfuscateFunc: (identifierNamesGenerator: TTypeFromEnum<typeof IdentifierNamesGenerator>) => IObfuscationResult;
- before(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/custom-nodes-identifier-names-collision.js');
- obfuscateFunc = (identifierNamesGenerator: TTypeFromEnum<typeof IdentifierNamesGenerator>) => {
- return JavaScriptObfuscator.obfuscate(
- code,
- {
- identifierNamesGenerator,
- compact: false,
- renameGlobals: true,
- identifiersDictionary: ['foo', 'bar', 'baz', 'bark', 'hawk', 'foozmos', 'cow', 'chikago'],
- stringArray: true
- }
- );
- };
- [
- IdentifierNamesGenerator.DictionaryIdentifierNamesGenerator,
- IdentifierNamesGenerator.MangledIdentifierNamesGenerator
- ].forEach((identifierNamesGenerator: TTypeFromEnum<typeof IdentifierNamesGenerator>) => {
- for (let i = 0; i < samplesCount; i++) {
- const obfuscationResult = obfuscateFunc(identifierNamesGenerator);
- const obfuscatedCode = obfuscationResult.getObfuscatedCode();
- const seed = obfuscationResult.getOptions().seed;
- try {
- eval(obfuscatedCode);
- } catch (error) {
- collisionError = `Seed: ${seed}. Error: ${error.message}`;
- break;
- }
- }
- });
- });
- it('It does not create identifier names collision', () => {
- assert.isNull(collisionError);
- });
- });
- describe('Prevailing kind of variables', () => {
- const baseParams: TInputOptions = {
- compact: true,
- controlFlowFlattening: true,
- controlFlowFlatteningThreshold: 1,
- deadCodeInjection: true,
- deadCodeInjectionThreshold: 1,
- debugProtection: true,
- debugProtectionInterval: true,
- disableConsoleOutput: false,
- stringArrayRotate: 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<IObfuscationResult>;
- 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');
- });
- });
- });
- });
|