CatchClauseObfuscator.spec.ts 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import { IObfuscationResult } from '../../../../src/interfaces/IObfuscationResult';
  2. import { NO_CUSTOM_NODES_PRESET } from '../../../../src/preset-options/NoCustomNodesPreset';
  3. import { readFileAsString } from '../../../helpers/readFileAsString';
  4. import { JavaScriptObfuscator } from '../../../../src/JavaScriptObfuscator';
  5. const assert: Chai.AssertStatic = require('chai').assert;
  6. describe('CatchClauseObfuscator', () => {
  7. describe('changeControlFlow (catchClauseNode: ESTree.CatchClause): void', () => {
  8. const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  9. readFileAsString(
  10. './test/fixtures/node-transformers/node-obfuscators/catch-clause-obfuscator/catch-clause-obfuscator.js'
  11. ),
  12. Object.assign({}, NO_CUSTOM_NODES_PRESET)
  13. );
  14. const obfuscatedCode: string = obfuscationResult.getObfuscatedCode();
  15. const paramNameRegExp: RegExp = /catch *\((_0x([a-z0-9]){4,6})\) *\{/;
  16. const bodyParamNameRegExp: RegExp = /console\['\\x6c\\x6f\\x67'\]\((_0x([a-z0-9]){4,6})\);/;
  17. it('should obfuscate catch clause node', () => {
  18. assert.match(obfuscatedCode, paramNameRegExp);
  19. assert.match(obfuscatedCode, bodyParamNameRegExp);
  20. });
  21. it('catch clause arguments param name and param name in body should be same', () => {
  22. const firstMatchArray: RegExpMatchArray|null = obfuscatedCode.match(paramNameRegExp);
  23. const secondMatchArray: RegExpMatchArray|null = obfuscatedCode.match(bodyParamNameRegExp);
  24. const firstMatch: string|undefined = firstMatchArray ? firstMatchArray[1] : undefined;
  25. const secondMatch: string|undefined = secondMatchArray ? secondMatchArray[1] : undefined;
  26. assert.isOk(firstMatch);
  27. assert.isOk(secondMatch);
  28. assert.equal(firstMatch, secondMatch);
  29. });
  30. });
  31. });