JavaScriptObfuscator.spec.ts 2.0 KB

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