LiteralObfuscator.spec.ts 2.7 KB

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