BinaryExpressionControlFlowReplacer.spec.ts 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import { assert } from 'chai';
  2. import { NO_ADDITIONAL_NODES_PRESET } from '../../../../../../src/options/presets/NoCustomNodes';
  3. import { readFileAsString } from '../../../../../helpers/readFileAsString';
  4. import { JavaScriptObfuscator } from '../../../../../../src/JavaScriptObfuscatorFacade';
  5. describe('BinaryExpressionControlFlowReplacer', function () {
  6. this.timeout(100000);
  7. describe('replace', () => {
  8. describe('Variant #1 - single binary expression', () => {
  9. const controlFlowStorageCallRegExp: RegExp = /var _0x([a-f0-9]){4,6} *= *_0x([a-f0-9]){4,6}\['\w{5}'\]\(0x1, *0x2\);/;
  10. let obfuscatedCode: string;
  11. before(() => {
  12. const code: string = readFileAsString(__dirname + '/fixtures/input-1.js');
  13. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  14. code,
  15. {
  16. ...NO_ADDITIONAL_NODES_PRESET,
  17. controlFlowFlattening: true,
  18. controlFlowFlatteningThreshold: 1
  19. }
  20. ).getObfuscatedCode();
  21. });
  22. it('should replace binary expression node with call to control flow storage node', () => {
  23. assert.match(obfuscatedCode, controlFlowStorageCallRegExp);
  24. });
  25. });
  26. describe('Variant #2 - multiple binary expressions with threshold = 1', () => {
  27. const expectedMatchErrorsCount: number = 0;
  28. const expectedChance: number = 0.5;
  29. const samplesCount: number = 1000;
  30. const delta: number = 0.1;
  31. const controlFlowStorageCallRegExp1: RegExp = /var _0x(?:[a-f0-9]){4,6} *= *(_0x([a-f0-9]){4,6}\['\w{5}'\])\(0x1, *0x2\);/;
  32. const controlFlowStorageCallRegExp2: RegExp = /var _0x(?:[a-f0-9]){4,6} *= *(_0x([a-f0-9]){4,6}\['\w{5}'\])\(0x2, *0x3\);/;
  33. let matchErrorsCount: number = 0,
  34. usingExistingIdentifierChance: number;
  35. before(() => {
  36. const code: string = readFileAsString(__dirname + '/fixtures/input-2.js');
  37. let obfuscatedCode: string,
  38. firstMatchArray: RegExpMatchArray | null,
  39. secondMatchArray: RegExpMatchArray | null,
  40. firstMatch: string | undefined,
  41. secondMatch: string | undefined,
  42. equalsValue: number = 0;
  43. for (let i = 0; i < samplesCount; i++) {
  44. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  45. code,
  46. {
  47. ...NO_ADDITIONAL_NODES_PRESET,
  48. controlFlowFlattening: true,
  49. controlFlowFlatteningThreshold: 1
  50. }
  51. ).getObfuscatedCode();
  52. firstMatchArray = obfuscatedCode.match(controlFlowStorageCallRegExp1);
  53. secondMatchArray = obfuscatedCode.match(controlFlowStorageCallRegExp2);
  54. if (!firstMatchArray || !secondMatchArray) {
  55. matchErrorsCount++;
  56. continue;
  57. }
  58. firstMatch = firstMatchArray ? firstMatchArray[1] : undefined;
  59. secondMatch = secondMatchArray ? secondMatchArray[1] : undefined;
  60. if (firstMatch === secondMatch) {
  61. equalsValue++;
  62. }
  63. }
  64. usingExistingIdentifierChance = equalsValue / samplesCount;
  65. });
  66. it('should replace binary expression node with call to control flow storage node', () => {
  67. assert.equal(matchErrorsCount, expectedMatchErrorsCount);
  68. });
  69. it('should use existing identifier for control flow storage with expected chance', () => {
  70. assert.closeTo(usingExistingIdentifierChance, expectedChance, delta);
  71. });
  72. });
  73. });
  74. });