JavaScriptObfuscatorCLI.spec.ts 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import { JavaScriptObfuscator } from "../src/JavaScriptObfuscator";
  2. import { NO_CUSTOM_NODES_PRESET } from "../src/preset-options/NoCustomNodesPreset";
  3. let assert: any = require('chai').assert;
  4. describe('JavaScriptObfuscator', () => {
  5. describe('obfuscate (sourceCode: string, customOptions?: IOptionsPreset): string', () => {
  6. it('should obfuscate simple code with variable inside global scope', () => {
  7. assert.match(
  8. JavaScriptObfuscator.obfuscate(
  9. `var test = 1;`,
  10. Object.assign({}, NO_CUSTOM_NODES_PRESET)
  11. ),
  12. /^var *test *= *0x\d+;$/
  13. );
  14. });
  15. it('should obfuscate simple code with variable inside block-scope', () => {
  16. assert.match(
  17. JavaScriptObfuscator.obfuscate(
  18. `(function () {var test = 1;})()`,
  19. Object.assign({}, NO_CUSTOM_NODES_PRESET)
  20. ),
  21. /^\(function *\(\) *\{ *var *_0x[\w]+ *= *0x\d+; *\}(\(\)\)|\)\(\));?$/
  22. );
  23. });
  24. it('should obfuscate simple code with literal variable value', () => {
  25. let pattern: RegExp = /^var _0x(\w){4} *= *\['(\\[x|u]\d+)+'\]; *var *test *= *_0x(\w){4}\[0x0\];$/;
  26. assert.match(
  27. JavaScriptObfuscator.obfuscate(
  28. `var test = 'abc';`,
  29. Object.assign({}, NO_CUSTOM_NODES_PRESET, {
  30. unicodeArray: true,
  31. unicodeArrayThreshold: 1
  32. })
  33. ),
  34. pattern
  35. );
  36. assert.match(
  37. JavaScriptObfuscator.obfuscate(
  38. `var test = 'абц';`,
  39. Object.assign({}, NO_CUSTOM_NODES_PRESET, {
  40. unicodeArray: true,
  41. unicodeArrayThreshold: 1
  42. })
  43. ),
  44. pattern
  45. );
  46. });
  47. });
  48. });