|
@@ -1,26 +1,25 @@
|
|
|
import { assert } from 'chai';
|
|
|
|
|
|
-import { Chance } from 'chance';
|
|
|
+import { IObfuscationResult } from '../../../src/interfaces/IObfuscationResult';
|
|
|
|
|
|
-import { IObfuscationResult } from '../../src/interfaces/IObfuscationResult';
|
|
|
+import { JavaScriptObfuscator } from '../../../src/JavaScriptObfuscator';
|
|
|
|
|
|
-import { JavaScriptObfuscator } from '../../src/JavaScriptObfuscator';
|
|
|
+import { NO_CUSTOM_NODES_PRESET } from '../../../src/options/presets/NoCustomNodes';
|
|
|
|
|
|
-import { NO_CUSTOM_NODES_PRESET } from '../../src/options/presets/NoCustomNodes';
|
|
|
+import { readFileAsString } from '../../helpers/readFileAsString';
|
|
|
|
|
|
-import { readFileAsString } from '../helpers/readFileAsString';
|
|
|
-import { RandomGeneratorUtils } from '../../src/utils/RandomGeneratorUtils';
|
|
|
+import { RandomGeneratorUtils } from '../../../src/utils/RandomGeneratorUtils';
|
|
|
|
|
|
describe('JavaScriptObfuscator', () => {
|
|
|
describe('obfuscate (sourceCode: string, customOptions?: IObfuscatorOptions): IObfuscationResult', () => {
|
|
|
beforeEach(() => {
|
|
|
- RandomGeneratorUtils.setRandomGenerator(new Chance());
|
|
|
+ RandomGeneratorUtils.initializeRandomGenerator(0);
|
|
|
});
|
|
|
|
|
|
describe('if `sourceMap` option is `false`', () => {
|
|
|
it('should returns object with obfuscated code and empty source map', () => {
|
|
|
let obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
|
|
|
- `var test = 1;`,
|
|
|
+ readFileAsString(__dirname + '/fixtures/simple-input-1.js'),
|
|
|
{
|
|
|
...NO_CUSTOM_NODES_PRESET
|
|
|
}
|
|
@@ -34,7 +33,7 @@ describe('JavaScriptObfuscator', () => {
|
|
|
describe('if `sourceMap` option is `true`', () => {
|
|
|
it('should returns object with obfuscated code and source map', () => {
|
|
|
let obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
|
|
|
- `var test = 1;`,
|
|
|
+ readFileAsString(__dirname + '/fixtures/simple-input-1.js'),
|
|
|
{
|
|
|
...NO_CUSTOM_NODES_PRESET,
|
|
|
sourceMap: true
|
|
@@ -47,7 +46,7 @@ describe('JavaScriptObfuscator', () => {
|
|
|
|
|
|
it('should returns object with obfuscated code with inline source map as Base64 string', () => {
|
|
|
let obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
|
|
|
- `var test = 1;`,
|
|
|
+ readFileAsString(__dirname + '/fixtures/simple-input-1.js'),
|
|
|
{
|
|
|
...NO_CUSTOM_NODES_PRESET,
|
|
|
sourceMap: true,
|
|
@@ -65,7 +64,7 @@ describe('JavaScriptObfuscator', () => {
|
|
|
|
|
|
it('should returns object with empty obfuscated code and source map with empty data if source code is empty', () => {
|
|
|
let obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
|
|
|
- '',
|
|
|
+ readFileAsString(__dirname + '/fixtures/empty-input.js'),
|
|
|
{
|
|
|
sourceMap: true
|
|
|
}
|
|
@@ -81,7 +80,7 @@ describe('JavaScriptObfuscator', () => {
|
|
|
it('should returns an empty string if source code is empty', () => {
|
|
|
assert.isNotOk(
|
|
|
JavaScriptObfuscator.obfuscate(
|
|
|
- ''
|
|
|
+ readFileAsString(__dirname + '/fixtures/empty-input.js'),
|
|
|
).getObfuscatedCode()
|
|
|
);
|
|
|
});
|
|
@@ -89,7 +88,7 @@ describe('JavaScriptObfuscator', () => {
|
|
|
it('should returns an empty string if source code contains only comments', () => {
|
|
|
assert.isNotOk(
|
|
|
JavaScriptObfuscator.obfuscate(
|
|
|
- '// comment'
|
|
|
+ readFileAsString(__dirname + '/fixtures/comments-only.js'),
|
|
|
).getObfuscatedCode()
|
|
|
);
|
|
|
});
|
|
@@ -97,7 +96,7 @@ describe('JavaScriptObfuscator', () => {
|
|
|
it('should obfuscate simple code with variable inside global scope', () => {
|
|
|
assert.match(
|
|
|
JavaScriptObfuscator.obfuscate(
|
|
|
- `var test = 1;`,
|
|
|
+ readFileAsString(__dirname + '/fixtures/simple-input-1.js'),
|
|
|
{
|
|
|
...NO_CUSTOM_NODES_PRESET
|
|
|
}
|
|
@@ -109,7 +108,7 @@ describe('JavaScriptObfuscator', () => {
|
|
|
it('should obfuscate simple code with variable inside block-scope', () => {
|
|
|
assert.match(
|
|
|
JavaScriptObfuscator.obfuscate(
|
|
|
- `(function () {var test = 1;})()`,
|
|
|
+ readFileAsString(__dirname + '/fixtures/block-scope.js'),
|
|
|
{
|
|
|
...NO_CUSTOM_NODES_PRESET
|
|
|
}
|
|
@@ -122,7 +121,7 @@ describe('JavaScriptObfuscator', () => {
|
|
|
let pattern1: RegExp = /^var _0x(\w){4} *= *\['(\\[x|u]\d+)+'\];/,
|
|
|
pattern2: RegExp = /var *test *= *_0x(\w){4}\('0x0'\);$/,
|
|
|
obfuscatedCode1: string = JavaScriptObfuscator.obfuscate(
|
|
|
- `var test = 'abc';`,
|
|
|
+ readFileAsString(__dirname + '/fixtures/simple-input-2.js'),
|
|
|
{
|
|
|
...NO_CUSTOM_NODES_PRESET,
|
|
|
stringArray: true,
|
|
@@ -130,7 +129,7 @@ describe('JavaScriptObfuscator', () => {
|
|
|
}
|
|
|
).getObfuscatedCode(),
|
|
|
obfuscatedCode2: string = JavaScriptObfuscator.obfuscate(
|
|
|
- `var test = 'абц';`,
|
|
|
+ readFileAsString(__dirname + '/fixtures/simple-input-cyrillic.js'),
|
|
|
{
|
|
|
...NO_CUSTOM_NODES_PRESET,
|
|
|
stringArray: true,
|
|
@@ -145,21 +144,33 @@ describe('JavaScriptObfuscator', () => {
|
|
|
assert.match(obfuscatedCode2, pattern2);
|
|
|
});
|
|
|
|
|
|
- it('should returns same code every time with same `seed`', () => {
|
|
|
+ it('should returns same code every time with same `seed`', function () {
|
|
|
+ this.timeout(15000);
|
|
|
+
|
|
|
const code: string = readFileAsString('./test/fixtures/sample.js');
|
|
|
- const seed: number = 12345;
|
|
|
+ const samples: number = 100;
|
|
|
|
|
|
- const obfuscationResult1: IObfuscationResult = JavaScriptObfuscator.obfuscate(
|
|
|
- code, { seed: seed }
|
|
|
- );
|
|
|
- RandomGeneratorUtils.randomVariableNameSet.clear();
|
|
|
+ let seed: number = 12345,
|
|
|
+ equalsCount: number = 0;
|
|
|
|
|
|
- const obfuscationResult2: IObfuscationResult = JavaScriptObfuscator.obfuscate(
|
|
|
- code, { seed: seed }
|
|
|
- );
|
|
|
- RandomGeneratorUtils.randomVariableNameSet.clear();
|
|
|
+ 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 }
|
|
|
+ );
|
|
|
+
|
|
|
+ if (obfuscationResult1.getObfuscatedCode() === obfuscationResult2.getObfuscatedCode()) {
|
|
|
+ equalsCount++;
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- assert.equal(obfuscationResult1.getObfuscatedCode(), obfuscationResult2.getObfuscatedCode());
|
|
|
+ assert.equal(equalsCount, samples);
|
|
|
});
|
|
|
|
|
|
it('should returns different code with different `seed` option value', () => {
|
|
@@ -184,7 +195,7 @@ describe('JavaScriptObfuscator', () => {
|
|
|
});
|
|
|
|
|
|
afterEach(() => {
|
|
|
- RandomGeneratorUtils.setRandomGenerator(new Chance());
|
|
|
+ RandomGeneratorUtils.initializeRandomGenerator(0);
|
|
|
});
|
|
|
});
|
|
|
});
|