JavaScriptObfuscator.spec.ts 2.0 KB

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