ObjectExpressionTransformer.spec.ts 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 { JavaScriptObfuscator } from '../../../../src/JavaScriptObfuscator';
  5. describe('ObjectExpressionTransformer', () => {
  6. it('should replace object expression node `key` property with literal value by unicode value', () => {
  7. let obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  8. `var test = { 'foo': 0 };`,
  9. {
  10. ...NO_CUSTOM_NODES_PRESET
  11. }
  12. );
  13. assert.match(obfuscationResult.getObfuscatedCode(), /var *test *= *\{'\\x66\\x6f\\x6f':0x0\};/);
  14. });
  15. it('should replace object expression node `key` property with identifier value by unicode value', () => {
  16. let obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  17. `var test = { foo: 0 };`,
  18. {
  19. ...NO_CUSTOM_NODES_PRESET
  20. }
  21. );
  22. assert.match(obfuscationResult.getObfuscatedCode(), /var *test *= *\{'\\x66\\x6f\\x6f':0x0\};/);
  23. });
  24. it('should correct convert shorthand ES6 object expression to non-shorthand object expression', () => {
  25. let obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  26. `
  27. (function () {
  28. let a = 0;
  29. let b = 0;
  30. var test = {a, b};
  31. })();
  32. `,
  33. {
  34. ...NO_CUSTOM_NODES_PRESET
  35. }
  36. );
  37. assert.match(
  38. obfuscationResult.getObfuscatedCode(),
  39. /var *_0x[a-z0-9]{4,6} *= *\{'\\x61': *_0x[a-z0-9]{4,6}\, *'\\x62': *_0x[a-z0-9]{4,6}\};/
  40. );
  41. });
  42. it('should correct convert shorthand ES6 object expression to non-shorthand object expression', () => {
  43. let obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  44. `
  45. (function () {
  46. let a = 0;
  47. let b = 0;
  48. var test = {a, b};
  49. })();
  50. `,
  51. {
  52. ...NO_CUSTOM_NODES_PRESET
  53. }
  54. );
  55. assert.match(
  56. obfuscationResult.getObfuscatedCode(),
  57. /var *_0x[a-z0-9]{4,6} *= *\{'\\x61': *_0x[a-z0-9]{4,6}\, *'\\x62': *_0x[a-z0-9]{4,6}\};/
  58. );
  59. });
  60. });