LiteralObfuscator.spec.ts 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 *test *= *_0x([a-z0-9]){4}\('0x0', '(\\x[a-f0-9]*){4}'\);/
  41. );
  42. });
  43. });
  44. it('should obfuscate literal node with boolean value', () => {
  45. let obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  46. `var test = true;`,
  47. Object.assign({}, NO_CUSTOM_NODES_PRESET, {
  48. unicodeArray: true,
  49. unicodeArrayThreshold: 1
  50. })
  51. );
  52. assert.match(obfuscationResult.getObfuscatedCode(), /^var *test *= *!!\[\];$/);
  53. });
  54. it('should obfuscate literal node with number value', () => {
  55. let obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  56. `var test = 0;`,
  57. Object.assign({}, NO_CUSTOM_NODES_PRESET, {
  58. unicodeArray: true,
  59. unicodeArrayThreshold: 1
  60. })
  61. );
  62. assert.match(obfuscationResult.getObfuscatedCode(), /^var *test *= *0x0;$/);
  63. });
  64. });