ObjectExpressionObfuscator.spec.ts 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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('ObjectExpressionObfuscator', () => {
  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. Object.assign({}, NO_CUSTOM_NODES_PRESET)
  10. );
  11. assert.match(obfuscationResult.getObfuscatedCode(), /var *test *= *\{'\\x66\\x6f\\x6f':0x0\};/);
  12. });
  13. it('should replace object expression node `key` property with identifier value by unicode value', () => {
  14. let obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  15. `var test = { foo: 0 };`,
  16. Object.assign({}, NO_CUSTOM_NODES_PRESET)
  17. );
  18. assert.match(obfuscationResult.getObfuscatedCode(), /var *test *= *\{'\\x66\\x6f\\x6f':0x0\};/);
  19. });
  20. it('should correct convert shorthand ES6 object expression to non-shorthand object expression', () => {
  21. let obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  22. `
  23. (function () {
  24. let a = 0;
  25. let b = 0;
  26. var test = {a, b};
  27. })();
  28. `,
  29. Object.assign({}, NO_CUSTOM_NODES_PRESET)
  30. );
  31. assert.match(
  32. obfuscationResult.getObfuscatedCode(),
  33. /var *_0x[a-z0-9]{4,6} *= *\{'\\x61': *_0x[a-z0-9]{4,6}\, *'\\x62': *_0x[a-z0-9]{4,6}\};/
  34. );
  35. });
  36. });