LiteralObfuscator.spec.ts 2.5 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('LiteralObfuscator', () => {
  6. describe('obfuscation of literal node with string value', () => {
  7. it('should replace literal node value with unicode value without encoding to base64', () => {
  8. let obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  9. `var test = 'test';`,
  10. Object.assign({}, NO_CUSTOM_NODES_PRESET)
  11. );
  12. assert.match(obfuscationResult.getObfuscatedCode(), /^var *test *= *'\\x74\\x65\\x73\\x74';$/);
  13. });
  14. it('should replace literal node value with unicode value with encoding to base64', () => {
  15. let obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  16. `var test = 'test';`,
  17. Object.assign({}, NO_CUSTOM_NODES_PRESET, {
  18. encodeUnicodeLiterals: true,
  19. unicodeArray: true,
  20. unicodeArrayThreshold: 1
  21. })
  22. );
  23. assert.match(obfuscationResult.getObfuscatedCode(), /^var *_0x([a-z0-9]){4} *= *\['\\x64\\x47\\x56\\x7a\\x64\\x41\\x3d\\x3d'\];/);
  24. assert.match(obfuscationResult.getObfuscatedCode(), /var *test *= *_0x([a-z0-9]){4}\('0x0'\);/);
  25. });
  26. });
  27. it('should obfuscate literal node with boolean value', () => {
  28. let obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  29. `var test = true;`,
  30. Object.assign({}, NO_CUSTOM_NODES_PRESET, {
  31. encodeUnicodeLiterals: true,
  32. unicodeArray: true,
  33. unicodeArrayThreshold: 1
  34. })
  35. );
  36. assert.match(obfuscationResult.getObfuscatedCode(), /^var *test *= *!!\[\];$/);
  37. });
  38. it('should obfuscate literal node with number value', () => {
  39. let obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  40. `var test = 0;`,
  41. Object.assign({}, NO_CUSTOM_NODES_PRESET, {
  42. encodeUnicodeLiterals: true,
  43. unicodeArray: true,
  44. unicodeArrayThreshold: 1
  45. })
  46. );
  47. assert.match(obfuscationResult.getObfuscatedCode(), /^var *test *= *0x0;$/);
  48. });
  49. });