LiteralObfuscator.spec.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 escape sequence', () => {
  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 escape sequence from unicode array', () => {
  15. let obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  16. `var test = 'test';`,
  17. Object.assign({}, NO_CUSTOM_NODES_PRESET, {
  18. stringArray: true,
  19. stringArrayThreshold: 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('should replace literal node value with raw value from unicode array if `unicodeEscapeSequence` is disabled', () => {
  29. let obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  30. `var test = 'test';`,
  31. Object.assign({}, NO_CUSTOM_NODES_PRESET, {
  32. stringArray: true,
  33. stringArrayThreshold: 1,
  34. unicodeEscapeSequence: false
  35. })
  36. );
  37. assert.match(
  38. obfuscationResult.getObfuscatedCode(),
  39. /^var *_0x([a-z0-9]){4} *= *\['test'\];/
  40. );
  41. assert.match(obfuscationResult.getObfuscatedCode(), /var *test *= *_0x([a-z0-9]){4}\('0x0'\);/);
  42. });
  43. it('shouldn\'t replace short literal node value with unicode array value', () => {
  44. let obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  45. `var test = 'te';`,
  46. Object.assign({}, NO_CUSTOM_NODES_PRESET, {
  47. stringArray: true,
  48. stringArrayThreshold: 1
  49. })
  50. );
  51. assert.match(obfuscationResult.getObfuscatedCode(), /var *test *= *'\\x74\\x65';/);
  52. });
  53. it('should replace literal node value with unicode array value encoded using base64', () => {
  54. let obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  55. `var test = 'test';`,
  56. Object.assign({}, NO_CUSTOM_NODES_PRESET, {
  57. stringArray: true,
  58. stringArrayEncoding: 'base64',
  59. stringArrayThreshold: 1
  60. })
  61. );
  62. assert.match(
  63. obfuscationResult.getObfuscatedCode(),
  64. /^var *_0x([a-z0-9]){4} *= *\['\\x64\\x47\\x56\\x7a\\x64\\x41\\x3d\\x3d'\];/
  65. );
  66. assert.match(obfuscationResult.getObfuscatedCode(), /var *test *= *_0x([a-z0-9]){4}\('0x0'\);/);
  67. });
  68. it('should replace literal node value with unicode array value encoded using rc4', () => {
  69. let obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  70. `var test = 'test';`,
  71. Object.assign({}, NO_CUSTOM_NODES_PRESET, {
  72. stringArray: true,
  73. stringArrayEncoding: 'rc4',
  74. stringArrayThreshold: 1
  75. })
  76. );
  77. assert.match(
  78. obfuscationResult.getObfuscatedCode(),
  79. /var *test *= *_0x([a-z0-9]){4}\('0x0', '(\\x[a-f0-9]*){4}'\);/
  80. );
  81. });
  82. });
  83. it('should obfuscate literal node with boolean value', () => {
  84. let obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  85. `var test = true;`,
  86. Object.assign({}, NO_CUSTOM_NODES_PRESET, {
  87. stringArray: true,
  88. stringArrayThreshold: 1
  89. })
  90. );
  91. assert.match(obfuscationResult.getObfuscatedCode(), /^var *test *= *!!\[\];$/);
  92. });
  93. it('should obfuscate literal node with number value', () => {
  94. let obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  95. `var test = 0;`,
  96. Object.assign({}, NO_CUSTOM_NODES_PRESET, {
  97. stringArray: true,
  98. stringArrayThreshold: 1
  99. })
  100. );
  101. assert.match(obfuscationResult.getObfuscatedCode(), /^var *test *= *0x0;$/);
  102. });
  103. });