StringLiteralControlFlowReplacer.spec.ts 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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('StringLiteralControlFlowReplacer', () => {
  6. describe('replace', () => {
  7. const variableMatch: string = '_0x([a-f0-9]){4,6}';
  8. const controlFlowStorageStringLiteralRegExp: RegExp = new RegExp(
  9. `var ${variableMatch} *= *\\{'\\w{5}' *: *'test'\\};`
  10. );
  11. const controlFlowStorageCallRegExp: RegExp = new RegExp(
  12. `var ${variableMatch} *= *${variableMatch}\\['\\w{5}'\\];`
  13. );
  14. let obfuscatedCode: string;
  15. before(() => {
  16. const code: string = readFileAsString(__dirname + '/fixtures/input-1.js');
  17. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  18. code,
  19. {
  20. ...NO_ADDITIONAL_NODES_PRESET,
  21. controlFlowFlattening: true,
  22. controlFlowFlatteningThreshold: 1
  23. }
  24. ).getObfuscatedCode();
  25. });
  26. it('should add string literal node as property of control flow storage node', () => {
  27. assert.match(obfuscatedCode, controlFlowStorageStringLiteralRegExp);
  28. });
  29. it('should replace string literal node with call to control flow storage node', () => {
  30. assert.match(obfuscatedCode, controlFlowStorageCallRegExp);
  31. });
  32. });
  33. });