LiteralObfuscator.spec.ts 4.2 KB

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