JavaScriptObfuscator.spec.ts 2.1 KB

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