StringLiteralControlFlowReplacer.spec.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233
  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 obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  9. readFileAsString(__dirname + '/fixtures/input-1.js'),
  10. {
  11. ...NO_CUSTOM_NODES_PRESET,
  12. controlFlowFlattening: true,
  13. controlFlowFlatteningThreshold: 1
  14. }
  15. );
  16. const obfuscatedCode: string = obfuscationResult.getObfuscatedCode();
  17. const controlFlowStorageStringLiteralRegExp: RegExp = /var *_0x([a-f0-9]){4,6} *= *\{'\w{3}' *: *'test'\};/;
  18. const controlFlowStorageCallRegExp: RegExp = /var *_0x([a-f0-9]){4,6} *= *_0x([a-f0-9]){4,6}\['\w{3}'\];/;
  19. it('should add string literal node as property of control flow storage node', () => {
  20. assert.match(obfuscatedCode, controlFlowStorageStringLiteralRegExp);
  21. });
  22. it('should replace string literal node by call to control flow storage node', () => {
  23. assert.match(obfuscatedCode, controlFlowStorageCallRegExp);
  24. });
  25. });
  26. });