LiteralObfuscator.spec.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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', () => {
  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 encoded using base64', () => {
  15. let obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  16. `var test = 'test';`,
  17. Object.assign({}, NO_CUSTOM_NODES_PRESET, {
  18. unicodeArray: true,
  19. unicodeArrayEncoding: 'base64',
  20. unicodeArrayThreshold: 1
  21. })
  22. );
  23. assert.match(
  24. obfuscationResult.getObfuscatedCode(),
  25. /^var *_0x([a-z0-9]){4} *= *\['\\x64\\x47\\x56\\x7a\\x64\\x41\\x3d\\x3d'\];/
  26. );
  27. assert.match(obfuscationResult.getObfuscatedCode(), /var *test *= *_0x([a-z0-9]){4}\('0x0'\);/);
  28. });
  29. it('should replace literal node value with unicode value encoded using rc4', () => {
  30. let obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  31. `var test = 'test';`,
  32. Object.assign({}, NO_CUSTOM_NODES_PRESET, {
  33. unicodeArray: true,
  34. unicodeArrayEncoding: 'rc4',
  35. unicodeArrayThreshold: 1
  36. })
  37. );
  38. assert.match(
  39. obfuscationResult.getObfuscatedCode(),
  40. /^var *_0x([a-z0-9]){4} *= *\['(\\x[a-f0-9]*){8}'\];/
  41. );
  42. assert.match(
  43. obfuscationResult.getObfuscatedCode(),
  44. /var *test *= *_0x([a-z0-9]){4}\('0x0', '(\\x[a-f0-9]*){4}'\);/
  45. );
  46. });
  47. });
  48. it('should obfuscate literal node with boolean value', () => {
  49. let obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  50. `var test = true;`,
  51. Object.assign({}, NO_CUSTOM_NODES_PRESET, {
  52. unicodeArray: true,
  53. unicodeArrayThreshold: 1
  54. })
  55. );
  56. assert.match(obfuscationResult.getObfuscatedCode(), /^var *test *= *!!\[\];$/);
  57. });
  58. it('should obfuscate literal node with number value', () => {
  59. let obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  60. `var test = 0;`,
  61. Object.assign({}, NO_CUSTOM_NODES_PRESET, {
  62. unicodeArray: true,
  63. unicodeArrayThreshold: 1
  64. })
  65. );
  66. assert.match(obfuscationResult.getObfuscatedCode(), /^var *test *= *0x0;$/);
  67. });
  68. });