BinaryExpressionControlFlowReplacer.spec.ts 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import { assert } from 'chai';
  2. import { IObfuscationResult } from '../../../../../src/interfaces/IObfuscationResult';
  3. import { NO_CUSTOM_NODES_PRESET } from '../../../../../src/options/presets/NoCustomNodes';
  4. import { readFileAsString } from '../../../../helpers/readFileAsString';
  5. import { JavaScriptObfuscator } from '../../../../../src/JavaScriptObfuscator';
  6. describe('BinaryExpressionControlFlowReplacer', () => {
  7. describe('replace (binaryExpressionNode: ESTree.BinaryExpression,parentNode: ESTree.Node,controlFlowStorage: IStorage <ICustomNode>)', () => {
  8. describe('variant #1 - single binary expression', () => {
  9. const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  10. readFileAsString(
  11. './test/fixtures/node-transformers/node-control-flow-transformers/control-flow-replacers/binary-expression-control-flow-replacer-1.js'
  12. ),
  13. {
  14. ...NO_CUSTOM_NODES_PRESET,
  15. controlFlowFlattening: true,
  16. controlFlowFlatteningThreshold: 1
  17. }
  18. );
  19. const obfuscatedCode: string = obfuscationResult.getObfuscatedCode();
  20. const controlFlowStorageCallRegExp: RegExp = /var *_0x([a-z0-9]){4,6} *= *_0x([a-z0-9]){4,6}\['(\\x[a-f0-9]*){3}'\]\(0x1, *0x2\);/;
  21. it('should replace binary expression node by call to control flow storage node', () => {
  22. assert.match(obfuscatedCode, controlFlowStorageCallRegExp);
  23. });
  24. });
  25. describe('variant #2 - multiple binary expressions with threshold = 1', () => {
  26. it('should replace binary expression node by call to control flow storage node', function () {
  27. this.timeout(4000);
  28. const samplesCount: number = 200;
  29. const expectedValue: number = 0.5;
  30. const delta: number = 0.06;
  31. let equalsValue: number = 0;
  32. for (let i = 0; i < samplesCount; i++) {
  33. const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  34. readFileAsString(
  35. './test/fixtures/node-transformers/node-control-flow-transformers/control-flow-replacers/binary-expression-control-flow-replacer-2.js'
  36. ),
  37. {
  38. ...NO_CUSTOM_NODES_PRESET,
  39. controlFlowFlattening: true,
  40. controlFlowFlatteningThreshold: 1
  41. }
  42. );
  43. const obfuscatedCode: string = obfuscationResult.getObfuscatedCode();
  44. const controlFlowStorageCallRegExp1: RegExp = /var *_0x([a-z0-9]){4,6} *= *(_0x([a-z0-9]){4,6}\['(\\x[a-f0-9]*){3}'\])\(0x1, *0x2\);/;
  45. const controlFlowStorageCallRegExp2: RegExp = /var *_0x([a-z0-9]){4,6} *= *(_0x([a-z0-9]){4,6}\['(\\x[a-f0-9]*){3}'\])\(0x2, *0x3\);/;
  46. const firstMatchArray: RegExpMatchArray | null = obfuscatedCode.match(controlFlowStorageCallRegExp1);
  47. const secondMatchArray: RegExpMatchArray | null = obfuscatedCode.match(controlFlowStorageCallRegExp2);
  48. const firstMatch: string | undefined = firstMatchArray ? firstMatchArray[2] : undefined;
  49. const secondMatch: string | undefined = secondMatchArray ? secondMatchArray[2] : undefined;
  50. assert.match(obfuscatedCode, controlFlowStorageCallRegExp1);
  51. assert.match(obfuscatedCode, controlFlowStorageCallRegExp2);
  52. assert.isOk(firstMatch);
  53. assert.isOk(secondMatch);
  54. if (firstMatch === secondMatch) {
  55. equalsValue++;
  56. }
  57. }
  58. assert.closeTo(equalsValue / samplesCount, expectedValue, delta);
  59. });
  60. });
  61. });
  62. });