123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501 |
- import { assert } from 'chai';
- import { IObfuscationResult } from '../../../src/interfaces/IObfuscationResult';
- import { JavaScriptObfuscator } from '../../../src/JavaScriptObfuscator';
- import { NO_CUSTOM_NODES_PRESET } from '../../../src/options/presets/NoCustomNodes';
- import { buildLargeCode } from '../../helpers/buildLargeCode';
- import { getRegExpMatch } from '../../helpers/getRegExpMatch';
- import { readFileAsString } from '../../helpers/readFileAsString';
- describe('JavaScriptObfuscator', () => {
- describe('obfuscate (sourceCode: string, customOptions?: IObfuscatorOptions): IObfuscationResult', () => {
- 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_CUSTOM_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 source code', () => {
- let obfuscatedCode: string;
- beforeEach(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/empty-input.js');
- const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
- code,
- );
- obfuscatedCode = obfuscationResult.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');
- const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
- code,
- );
- obfuscatedCode = obfuscationResult.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 obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
- code,
- {
- ...NO_CUSTOM_NODES_PRESET,
- sourceMap: true
- }
- );
- obfuscatedCode = obfuscationResult.getObfuscatedCode();
- sourceMap = JSON.parse(obfuscationResult.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 obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
- code,
- {
- ...NO_CUSTOM_NODES_PRESET,
- sourceMap: true,
- sourceMapMode: 'inline'
- }
- );
- obfuscatedCode = obfuscationResult.getObfuscatedCode();
- sourceMap = JSON.parse(obfuscationResult.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 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('variable inside global scope', () => {
- const regExp: RegExp = /^var *test *= *0x\d+;$/;
- let obfuscatedCode: string;
- beforeEach(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/simple-input-1.js');
- const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
- code,
- {
- ...NO_CUSTOM_NODES_PRESET
- }
- );
- obfuscatedCode = obfuscationResult.getObfuscatedCode();
- });
- it('should return correct obfuscated code', () => {
- assert.match(obfuscatedCode, regExp);
- });
- });
- describe('variable inside global scope', () => {
- const regExp: RegExp = /^\(function *\(\) *\{ *var *_0x[\w]+ *= *0x\d+; *\}(\(\)\)|\)\(\));?$/;
- let obfuscatedCode: string;
- beforeEach(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/block-scope.js');
- const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
- code,
- {
- ...NO_CUSTOM_NODES_PRESET
- }
- );
- obfuscatedCode = obfuscationResult.getObfuscatedCode();
- });
- it('should return correct obfuscated code', () => {
- assert.match(obfuscatedCode, regExp);
- });
- });
- 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');
- const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
- code,
- {
- ...NO_CUSTOM_NODES_PRESET,
- stringArray: true,
- stringArrayThreshold: 1
- }
- );
- obfuscatedCode = obfuscationResult.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');
- const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
- code,
- {
- ...NO_CUSTOM_NODES_PRESET,
- stringArray: true,
- stringArrayThreshold: 1
- }
- );
- obfuscatedCode = obfuscationResult.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++;
- }
- const obfuscationResult1: IObfuscationResult = JavaScriptObfuscator.obfuscate(
- code,
- {
- seed: seed
- }
- );
- const obfuscationResult2: IObfuscationResult = JavaScriptObfuscator.obfuscate(
- code,
- {
- seed: seed
- }
- );
- obfuscatedCode1 = obfuscationResult1.getObfuscatedCode();
- obfuscatedCode2 = obfuscationResult2.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(() => {
- const obfuscationResult1: IObfuscationResult = JavaScriptObfuscator.obfuscate(
- code,
- {
- seed: 12345
- }
- );
- const obfuscationResult2: IObfuscationResult = JavaScriptObfuscator.obfuscate(
- code,
- {
- seed: 12346
- }
- );
- obfuscatedCode1 = obfuscationResult1.getObfuscatedCode();
- obfuscatedCode2 = obfuscationResult2.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(() => {
- const obfuscationResult1: IObfuscationResult = JavaScriptObfuscator.obfuscate(
- code,
- {
- seed: 0
- }
- );
- const obfuscationResult2: IObfuscationResult = JavaScriptObfuscator.obfuscate(
- code,
- {
- seed: 0
- }
- );
- obfuscatedCode1 = obfuscationResult1.getObfuscatedCode();
- obfuscatedCode2 = obfuscationResult2.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 obfuscationResult1: IObfuscationResult = JavaScriptObfuscator.obfuscate(
- code1,
- {
- seed: 123,
- stringArrayThreshold: 1
- }
- );
- const obfuscationResult2: IObfuscationResult = JavaScriptObfuscator.obfuscate(
- code2,
- {
- seed: 123,
- stringArrayThreshold: 1
- }
- );
- const obfuscatedCode1: string = obfuscationResult1.getObfuscatedCode();
- const obfuscatedCode2: string = obfuscationResult2.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');
- const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
- code,
- {
- ...NO_CUSTOM_NODES_PRESET
- }
- );
- obfuscatedCode = obfuscationResult.getObfuscatedCode();
- });
- it('should keep new.target MetaProperty', () => {
- assert.match(obfuscatedCode, regExp);
- });
- });
- describe('mangle', () => {
- const regExp: RegExp = /var *a *= *0x1/;
- let obfuscatedCode: string;
- beforeEach(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/mangle.js');
- const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
- code,
- {
- mangle: true
- }
- );
- obfuscatedCode = obfuscationResult.getObfuscatedCode();
- });
- it('should mangle obfuscated code', () => {
- 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 obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
- code,
- {
- compact: true,
- controlFlowFlattening: true,
- controlFlowFlatteningThreshold: 1,
- deadCodeInjection: true,
- deadCodeInjectionThreshold: 1,
- disableConsoleOutput: false,
- rotateStringArray: true,
- stringArray: true,
- stringArrayEncoding: 'rc4',
- stringArrayThreshold: 1,
- unicodeEscapeSequence: false
- }
- );
- const obfuscatedCode: string = obfuscationResult.getObfuscatedCode();
- result = eval(obfuscatedCode);
- });
- it('should correctly obfuscate 3.5k variables', () => {
- assert.equal(result, expectedValue);
- });
- });
- });
- });
|