mirror of
https://github.com/javascript-obfuscator/javascript-obfuscator
synced 2026-08-09 12:42:29 +00:00
WIP: added random string array scope calls wrappers parameters
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
import { NO_ADDITIONAL_NODES_PRESET } from '../../src/options/presets/NoCustomNodes';
|
||||
import { StringArrayIndexesType } from '../../src/enums/node-transformers/string-array-transformers/StringArrayIndexesType';
|
||||
import { StringArrayEncoding } from '../../src/enums/node-transformers/string-array-transformers/StringArrayEncoding';
|
||||
|
||||
(function () {
|
||||
const JavaScriptObfuscator: any = require('../../index');
|
||||
@@ -35,6 +36,9 @@ import { StringArrayIndexesType } from '../../src/enums/node-transformers/string
|
||||
rotateStringArray: true,
|
||||
shuffleStringArray: true,
|
||||
stringArray: true,
|
||||
stringArrayEncoding: [
|
||||
StringArrayEncoding.Rc4
|
||||
],
|
||||
stringArrayIndexesType: [
|
||||
StringArrayIndexesType.HexadecimalNumericString,
|
||||
StringArrayIndexesType.HexadecimalNumber
|
||||
|
||||
@@ -44,6 +44,7 @@ import './unit-tests/utils/CryptUtilsSwappedAlphabet.spec';
|
||||
import './unit-tests/utils/EscapeSequenceEncoder.spec';
|
||||
import './unit-tests/utils/LevelledTopologicalSorter.spec';
|
||||
import './unit-tests/utils/NumberUtils.spec';
|
||||
import './unit-tests/utils/RandomGenerator.spec';
|
||||
import './unit-tests/utils/StringUtils.spec';
|
||||
import './unit-tests/utils/Utils.spec';
|
||||
|
||||
|
||||
@@ -66,6 +66,61 @@ describe('ArrayUtils', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('fillWithRange', () => {
|
||||
const valueFunction: (index: number) => string = (index: number) => `foo${index}`;
|
||||
|
||||
describe('range length more than 0', () => {
|
||||
const rangeLength: number = 5;
|
||||
const expectedArray: string[] = [
|
||||
'foo0',
|
||||
'foo1',
|
||||
'foo2',
|
||||
'foo3',
|
||||
'foo4',
|
||||
];
|
||||
|
||||
let array: string[];
|
||||
|
||||
before(() => {
|
||||
array = arrayUtils.fillWithRange(rangeLength, valueFunction);
|
||||
});
|
||||
|
||||
it('should return array with range of strings', () => {
|
||||
assert.deepEqual(array, expectedArray);
|
||||
});
|
||||
});
|
||||
|
||||
describe('range length is 0', () => {
|
||||
const rangeLength: number = 0;
|
||||
const expectedArray: string[] = [];
|
||||
|
||||
let array: string[];
|
||||
|
||||
before(() => {
|
||||
array = arrayUtils.fillWithRange(rangeLength, valueFunction);
|
||||
});
|
||||
|
||||
it('should return empty array', () => {
|
||||
assert.deepEqual(array, expectedArray);
|
||||
});
|
||||
});
|
||||
|
||||
describe('range length less than 0', () => {
|
||||
const rangeLength: number = -5;
|
||||
const expectedArray: string[] = [];
|
||||
|
||||
let array: string[];
|
||||
|
||||
before(() => {
|
||||
array = arrayUtils.fillWithRange(rangeLength, valueFunction);
|
||||
});
|
||||
|
||||
it('should return empty array', () => {
|
||||
assert.deepEqual(array, expectedArray);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('findMostOccurringElement', () => {
|
||||
describe('empty array', () => {
|
||||
const array: string[] = [];
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
import 'reflect-metadata';
|
||||
|
||||
import { assert } from 'chai';
|
||||
|
||||
import { ServiceIdentifiers } from '../../../src/container/ServiceIdentifiers';
|
||||
|
||||
import { IInversifyContainerFacade } from '../../../src/interfaces/container/IInversifyContainerFacade';
|
||||
import { IRandomGenerator } from '../../../src/interfaces/utils/IRandomGenerator';
|
||||
|
||||
import { InversifyContainerFacade } from '../../../src/container/InversifyContainerFacade';
|
||||
|
||||
describe('RandomGenerator', () => {
|
||||
let randomGenerator: IRandomGenerator;
|
||||
|
||||
before(() => {
|
||||
const inversifyContainerFacade: IInversifyContainerFacade = new InversifyContainerFacade();
|
||||
|
||||
inversifyContainerFacade.load('', '', {});
|
||||
randomGenerator = inversifyContainerFacade.get<IRandomGenerator>(ServiceIdentifiers.IRandomGenerator);
|
||||
});
|
||||
|
||||
describe('getRandomIntegerExcluding', () => {
|
||||
describe('Variant #1: avoid excluded values', () => {
|
||||
const samplesCount: number = 500;
|
||||
|
||||
const minValue: number = 5;
|
||||
const maxValue: number = 10;
|
||||
const valuesToExclude: number[] = [6, 9];
|
||||
|
||||
const expectedRandomIntegerValues: number[] = [5, 7, 8, 10];
|
||||
|
||||
let isRandomIntegerInAllowedValuesRange: boolean = true;
|
||||
|
||||
before(() => {
|
||||
for (let i = 0; i < samplesCount; i++) {
|
||||
const randomInteger = randomGenerator.getRandomIntegerExcluding(minValue, maxValue, valuesToExclude);
|
||||
|
||||
if (!expectedRandomIntegerValues.includes(randomInteger)) {
|
||||
isRandomIntegerInAllowedValuesRange = false;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
it('should return a random integer in allowed values range', () => {
|
||||
assert.isTrue(isRandomIntegerInAllowedValuesRange);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Variant #2: values boundaries', () => {
|
||||
const samplesCount: number = 500;
|
||||
|
||||
const minValue: number = 5;
|
||||
const maxValue: number = 10;
|
||||
const valuesToExclude: number[] = [6, 9];
|
||||
|
||||
const delta: number = 0.15;
|
||||
|
||||
const expectedValueChance: number = 0.2
|
||||
|
||||
let minValuesCount: number = 0;
|
||||
let maxValuesCount: number = 0;
|
||||
|
||||
let minValueChance: number;
|
||||
let maxValueChance: number;
|
||||
|
||||
before(() => {
|
||||
for (let i = 0; i < samplesCount; i++) {
|
||||
const randomInteger: number = randomGenerator.getRandomIntegerExcluding(minValue, maxValue, valuesToExclude);
|
||||
|
||||
if (randomInteger === minValue) {
|
||||
minValuesCount += 1;
|
||||
}
|
||||
|
||||
if (randomInteger === maxValue) {
|
||||
maxValuesCount += 1;
|
||||
}
|
||||
|
||||
minValueChance = minValuesCount / samplesCount;
|
||||
maxValueChance = maxValuesCount / samplesCount;
|
||||
}
|
||||
});
|
||||
|
||||
it('should generate min values', () => {
|
||||
assert.closeTo(minValueChance, expectedValueChance, delta);
|
||||
});
|
||||
|
||||
it('should generate max values', () => {
|
||||
assert.closeTo(maxValueChance, expectedValueChance, delta);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user