MemberExpressionObfuscator.spec.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import { IObfuscationResult } from '../../../src/interfaces/IObfuscationResult';
  2. import { NO_CUSTOM_NODES_PRESET } from '../../../src/preset-options/NoCustomNodesPreset';
  3. import { JavaScriptObfuscator } from '../../../src/JavaScriptObfuscator';
  4. const assert: Chai.AssertStatic = require('chai').assert;
  5. describe('MemberExpressionObfuscator', () => {
  6. describe('obfuscation of member expression node with dot notation', () => {
  7. it('should replace member expression dot notation call by square brackets call with unicode literal value', () => {
  8. let obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  9. `var test = console.log;`,
  10. Object.assign({}, NO_CUSTOM_NODES_PRESET)
  11. );
  12. assert.match(obfuscationResult.getObfuscatedCode(), /var *test *= *console\['\\x6c\\x6f\\x67'\];/);
  13. });
  14. it('should replace member expression dot notation call by square brackets call to unicode array', () => {
  15. let obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  16. `var test = console.log;`,
  17. Object.assign({}, NO_CUSTOM_NODES_PRESET, {
  18. stringArray: true,
  19. stringArrayThreshold: 1
  20. })
  21. );
  22. assert.match(obfuscationResult.getObfuscatedCode(), /var *_0x([a-z0-9]){4} *= *\['\\x6c\\x6f\\x67'\];/);
  23. assert.match(obfuscationResult.getObfuscatedCode(), /var *test *= *console\[_0x([a-z0-9]){4}\('0x0'\)\];/);
  24. });
  25. });
  26. describe('obfuscation of member expression node without dot notation', () => {
  27. it('should replace member expression square brackets call by square brackets call to unicode array', () => {
  28. let obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  29. `var test = console['log'];`,
  30. Object.assign({}, NO_CUSTOM_NODES_PRESET, {
  31. stringArray: true,
  32. stringArrayThreshold: 1
  33. })
  34. );
  35. assert.match(obfuscationResult.getObfuscatedCode(), /var *_0x([a-z0-9]){4} *= *\['\\x6c\\x6f\\x67'\];/);
  36. assert.match(obfuscationResult.getObfuscatedCode(), /var *test *= *console\[_0x([a-z0-9]){4}\('0x0'\)\];/);
  37. });
  38. it('should ignore square brackets call with identifier value', () => {
  39. let obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  40. `
  41. var identifier = 'log';
  42. var test = console[identifier];
  43. `,
  44. Object.assign({}, NO_CUSTOM_NODES_PRESET)
  45. );
  46. assert.match(obfuscationResult.getObfuscatedCode(), /var *test *= *console\[identifier\];/);
  47. });
  48. });
  49. });