BinaryExpressionControlFlowReplacer.spec.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import { assert } from 'chai';
  2. import { IObfuscationResult } from '../../../../../src/interfaces/IObfuscationResult';
  3. import { NO_CUSTOM_NODES_PRESET } from '../../../../../src/preset-options/NoCustomNodesPreset';
  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', () => {
  27. const samplesCount: number = 100;
  28. const expectedValue: number = 0.5;
  29. const delta: number = 0.08;
  30. let equalsValue: number = 0;
  31. for (let i = 0; i < samplesCount; i++) {
  32. const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  33. readFileAsString(
  34. './test/fixtures/node-transformers/node-control-flow-transformers/control-flow-replacers/binary-expression-control-flow-replacer-2.js'
  35. ),
  36. {
  37. ...NO_CUSTOM_NODES_PRESET,
  38. controlFlowFlattening: true,
  39. controlFlowFlatteningThreshold: 1
  40. }
  41. );
  42. const obfuscatedCode: string = obfuscationResult.getObfuscatedCode();
  43. const controlFlowStorageCallRegExp1: RegExp = /var *_0x([a-z0-9]){4,6} *= *(_0x([a-z0-9]){4,6}\['(\\x[a-f0-9]*){3}'\])\(0x1, *0x2\);/;
  44. const controlFlowStorageCallRegExp2: RegExp = /var *_0x([a-z0-9]){4,6} *= *(_0x([a-z0-9]){4,6}\['(\\x[a-f0-9]*){3}'\])\(0x2, *0x3\);/;
  45. const firstMatchArray: RegExpMatchArray | null = obfuscatedCode.match(controlFlowStorageCallRegExp1);
  46. const secondMatchArray: RegExpMatchArray | null = obfuscatedCode.match(controlFlowStorageCallRegExp2);
  47. const firstMatch: string | undefined = firstMatchArray ? firstMatchArray[2] : undefined;
  48. const secondMatch: string | undefined = secondMatchArray ? secondMatchArray[2] : undefined;
  49. assert.match(obfuscatedCode, controlFlowStorageCallRegExp1);
  50. assert.match(obfuscatedCode, controlFlowStorageCallRegExp2);
  51. assert.isOk(firstMatch);
  52. assert.isOk(secondMatch);
  53. if (firstMatch === secondMatch) {
  54. equalsValue++;
  55. }
  56. }
  57. assert.closeTo(equalsValue / samplesCount, expectedValue, delta);
  58. });
  59. });
  60. });
  61. });