CatchClauseObfuscator.spec.ts 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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: ESTree.CatchClause): 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. describe('object pattern as parameter', () => {
  30. const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  31. `
  32. (function () {
  33. try {
  34. } catch ({ name }) {
  35. return name;
  36. }
  37. })();
  38. `,
  39. NO_CUSTOM_NODES_PRESET
  40. );
  41. const obfuscatedCode: string = obfuscationResult.getObfuscatedCode();
  42. it('shouldn\'t transform function parameter object pattern identifier', () => {
  43. const functionParameterMatch: RegExp = /\} *catch *\(\{ *name *\}\) *\{/;
  44. const functionBodyMatch: RegExp = /return *name;/;
  45. assert.match(obfuscatedCode, functionParameterMatch);
  46. assert.match(obfuscatedCode, functionBodyMatch);
  47. });
  48. });
  49. });