CatchClauseTransformer.spec.ts 1.9 KB

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