StringLiteralControlFlowReplacer.spec.ts 1.7 KB

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