Add tests for mixed encoding to the StringArrayTransformer.spec.ts

This commit is contained in:
sanex
2020-08-30 19:09:51 +03:00
parent 9685d0c22e
commit af03c13fdd
@@ -280,7 +280,119 @@ describe('StringArrayTransformer', () => {
});
});
describe('Variant #10: `stringArrayThreshold` option value', () => {
describe('Variant #10: none and rc4 encoding', () => {
describe('Variant #1: single string literal', () => {
const samplesCount: number = 100;
const expectedMatchesChance: number = 0.5;
const expectedMatchesDelta: number = 0.15;
const noneEncodingRegExp: RegExp = /var test *= *_0x([a-f0-9]){4}\('0x0'\);/;
const rc4EncodingRegExp: RegExp = /var test *= *_0x([a-f0-9]){4}\('0x0', *'.{4}'\);/;
let noneEncodingMatchesCount: number = 0;
let rc4EncodingMatchesCount: number = 0;
let obfuscatedCode: string;
let noneEncodingMatchesChance: number;
let rc4EncodingMatchesChance: number;
before(() => {
const code: string = readFileAsString(__dirname + '/fixtures/simple-input.js');
for (let i = 0; i < samplesCount; i++) {
obfuscatedCode = JavaScriptObfuscator.obfuscate(
code,
{
...NO_ADDITIONAL_NODES_PRESET,
stringArray: true,
stringArrayEncoding: [
StringArrayEncoding.None,
StringArrayEncoding.Rc4
],
stringArrayThreshold: 1
}
).getObfuscatedCode();
if (obfuscatedCode.match(noneEncodingRegExp)) {
noneEncodingMatchesCount = noneEncodingMatchesCount + 1;
}
if (obfuscatedCode.match(rc4EncodingRegExp)) {
rc4EncodingMatchesCount = rc4EncodingMatchesCount + 1;
}
noneEncodingMatchesChance = noneEncodingMatchesCount / samplesCount;
rc4EncodingMatchesChance = rc4EncodingMatchesCount / samplesCount;
}
});
it('should replace literal node value with value from string array without encoding', () => {
assert.closeTo(noneEncodingMatchesChance, expectedMatchesChance, expectedMatchesDelta);
});
it('should replace literal node value with value from string array encoded using rc4', () => {
assert.closeTo(rc4EncodingMatchesChance, expectedMatchesChance, expectedMatchesDelta);
});
});
});
describe('Variant #11: base64 and rc4 encoding', () => {
describe('Variant #1: single string literal', () => {
const samplesCount: number = 100;
const expectedMatchesChance: number = 0.5;
const expectedMatchesDelta: number = 0.15;
const base64EncodingRegExp: RegExp = /var test *= *_0x([a-f0-9]){4}\('0x0'\);/;
const rc4EncodingRegExp: RegExp = /var test *= *_0x([a-f0-9]){4}\('0x0', *'.{4}'\);/;
let base64EncodingMatchesCount: number = 0;
let rc4EncodingMatchesCount: number = 0;
let obfuscatedCode: string;
let base64EncodingMatchesChance: number;
let rc4EncodingMatchesChance: number;
before(() => {
const code: string = readFileAsString(__dirname + '/fixtures/simple-input.js');
for (let i = 0; i < samplesCount; i++) {
obfuscatedCode = JavaScriptObfuscator.obfuscate(
code,
{
...NO_ADDITIONAL_NODES_PRESET,
stringArray: true,
stringArrayEncoding: [
StringArrayEncoding.Base64,
StringArrayEncoding.Rc4
],
stringArrayThreshold: 1
}
).getObfuscatedCode();
if (obfuscatedCode.match(base64EncodingRegExp)) {
base64EncodingMatchesCount = base64EncodingMatchesCount + 1;
}
if (obfuscatedCode.match(rc4EncodingRegExp)) {
rc4EncodingMatchesCount = rc4EncodingMatchesCount + 1;
}
base64EncodingMatchesChance = base64EncodingMatchesCount / samplesCount;
rc4EncodingMatchesChance = rc4EncodingMatchesCount / samplesCount;
}
});
it('should replace literal node value with value from string array encoded using base64', () => {
assert.closeTo(base64EncodingMatchesChance, expectedMatchesChance, expectedMatchesDelta);
});
it('should replace literal node value with value from string array encoded using rc4', () => {
assert.closeTo(rc4EncodingMatchesChance, expectedMatchesChance, expectedMatchesDelta);
});
});
});
describe('Variant #12: `stringArrayThreshold` option value', () => {
const samples: number = 1000;
const stringArrayThreshold: number = 0.5;
const delta: number = 0.1;
@@ -323,7 +435,7 @@ describe('StringArrayTransformer', () => {
});
});
describe('Variant #11: string array calls wrapper name', () => {
describe('Variant #13: string array calls wrapper name', () => {
const regExp: RegExp = /console\[b\('0x0'\)]\('a'\);/;
let obfuscatedCode: string;
@@ -347,7 +459,7 @@ describe('StringArrayTransformer', () => {
});
});
describe('Variant #12: `reservedStrings` option is enabled', () => {
describe('Variant #14: `reservedStrings` option is enabled', () => {
describe('Variant #1: base `reservedStrings` values', () => {
describe('Variant #1: single reserved string value', () => {
const stringLiteralRegExp1: RegExp = /const foo *= *'foo';/;
@@ -519,7 +631,7 @@ describe('StringArrayTransformer', () => {
});
});
describe('Variant #13: object expression key literal', () => {
describe('Variant #15: object expression key literal', () => {
describe('Variant #1: base key literal', () => {
const stringArrayRegExp: RegExp = /^var _0x([a-f0-9]){4} *= *\['bar'];/;
const objectExpressionRegExp: RegExp = /var test *= *{'foo' *: *_0x([a-f0-9]){4}\('0x0'\)};/;
@@ -577,7 +689,7 @@ describe('StringArrayTransformer', () => {
});
});
describe('Variant #14: import declaration source literal', () => {
describe('Variant #16: import declaration source literal', () => {
const importDeclarationRegExp: RegExp = /import *{ *bar *} *from *'foo';/;
let obfuscatedCode: string;
@@ -600,7 +712,7 @@ describe('StringArrayTransformer', () => {
});
});
describe('Variant #15: export all declaration source literal', () => {
describe('Variant #17: export all declaration source literal', () => {
const exportAllDeclarationRegExp: RegExp = /export *\* *from *'foo';/;
let obfuscatedCode: string;
@@ -623,7 +735,7 @@ describe('StringArrayTransformer', () => {
});
});
describe('Variant #16: export named declaration source literal', () => {
describe('Variant #18: export named declaration source literal', () => {
const exportNamedDeclarationRegExp: RegExp = /export *{ *bar *} *from *'foo';/;
let obfuscatedCode: string;