CatchClauseObfuscator.spec.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import * as ESTree from 'estree';
  2. import { ICustomNode } from '../../../src/interfaces/custom-nodes/ICustomNode';
  3. import { DEFAULT_PRESET } from '../../../src/preset-options/DefaultPreset';
  4. import { NodeType } from '../../../src/enums/NodeType';
  5. import { CatchClauseObfuscator } from '../../../src/node-obfuscators/CatchClauseObfuscator';
  6. import { NodeMocks } from '../../mocks/NodeMocks';
  7. import { Options } from '../../../src/options/Options';
  8. const assert: Chai.AssertStatic = require('chai').assert;
  9. describe('CatchClauseObfuscator', () => {
  10. describe('obfuscateNode (catchClauseNode: ICatchClauseNode): void', () => {
  11. let catchClauseObfuscator: CatchClauseObfuscator,
  12. catchClauseNode: ESTree.CatchClause;
  13. beforeEach(() => {
  14. let expressionStatementNode: ESTree.ExpressionStatement = {
  15. type: NodeType.ExpressionStatement,
  16. expression: {
  17. type: NodeType.CallExpression,
  18. callee: {
  19. type: NodeType.MemberExpression,
  20. computed: false,
  21. object: {
  22. type: NodeType.Identifier,
  23. name: 'console'
  24. },
  25. property: {
  26. type: NodeType.Identifier,
  27. name: 'log'
  28. }
  29. },
  30. arguments: [
  31. {
  32. type: NodeType.Identifier,
  33. 'name': 'err'
  34. }
  35. ]
  36. }
  37. };
  38. catchClauseObfuscator = new CatchClauseObfuscator(
  39. new Map<string, ICustomNode>(),
  40. new Options(DEFAULT_PRESET)
  41. );
  42. catchClauseNode = NodeMocks.getCatchClauseNode([
  43. expressionStatementNode
  44. ]);
  45. catchClauseObfuscator.obfuscateNode(catchClauseNode);
  46. });
  47. it('should obfuscate catch clause param name', () => {
  48. assert.match(
  49. (<any>catchClauseNode.body.body[0]).expression.arguments[0].name,
  50. /^_0x\w+$/
  51. );
  52. });
  53. it('should obfuscate catch clause param calls in catch clause node body', () => {
  54. assert.match(
  55. (<ESTree.Identifier>catchClauseNode.param).name,
  56. /^_0x\w+$/
  57. );
  58. });
  59. });
  60. });