CatchClauseObfuscator.spec.ts 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738
  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('obfuscateNode (catchClauseNode: ICatchClauseNode): void', () => {
  8. const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  9. readFileAsString('./test/fixtures/node-obfuscators/catch-clause-obfuscator/catch-clause-obfuscator.js'),
  10. Object.assign({}, NO_CUSTOM_NODES_PRESET)
  11. );
  12. const obfuscatedCode: string = obfuscationResult.getObfuscatedCode();
  13. const paramNameRegExp: RegExp = /catch *\((_0x([a-z0-9]){4,6})\) *\{/;
  14. const bodyParamNameRegExp: RegExp = /console\['\\x6c\\x6f\\x67'\]\((_0x([a-z0-9]){4,6})\);/;
  15. it('should obfuscate catch clause node', () => {
  16. assert.match(obfuscatedCode, paramNameRegExp);
  17. assert.match(obfuscatedCode, bodyParamNameRegExp);
  18. });
  19. it('catch clause arguments param name and param name in body should be same', () => {
  20. const firstMatchArray: RegExpMatchArray|null = obfuscatedCode.match(paramNameRegExp);
  21. const secondMatchArray: RegExpMatchArray|null = obfuscatedCode.match(bodyParamNameRegExp);
  22. const firstMatch: string|undefined = firstMatchArray ? firstMatchArray[1] : undefined;
  23. const secondMatch: string|undefined = secondMatchArray ? secondMatchArray[1] : undefined;
  24. assert.isOk(firstMatch);
  25. assert.isOk(secondMatch);
  26. assert.equal(firstMatch, secondMatch);
  27. });
  28. });
  29. });