MethodDefinitionObfuscator.spec.ts 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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('MethodDefinitionObfuscator', () => {
  6. let code: string = `
  7. class Foo {
  8. constructor () {}
  9. bar () {}
  10. }
  11. `;
  12. it('should replace method definition node `key` property with unicode value', () => {
  13. let obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  14. code,
  15. Object.assign({}, NO_CUSTOM_NODES_PRESET)
  16. );
  17. assert.match(obfuscationResult.getObfuscatedCode(), /\['\\x62\\x61\\x72'\]\(\)\{\}/);
  18. });
  19. it('should replace method definition node `key` property with unicode array call', () => {
  20. let obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  21. code,
  22. Object.assign({}, NO_CUSTOM_NODES_PRESET, {
  23. unicodeArray: true,
  24. unicodeArrayThreshold: 1
  25. })
  26. );
  27. assert.match(obfuscationResult.getObfuscatedCode(), /var *_0x([a-z0-9]){4} *= *\['\\x62\\x61\\x72'\];/);
  28. assert.match(obfuscationResult.getObfuscatedCode(), /\[_0x([a-z0-9]){4}\[0x0\]\]\(\)\{\}/);
  29. });
  30. it('should not obfuscate method definition node with `constructor` key', () => {
  31. let obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  32. code,
  33. Object.assign({}, NO_CUSTOM_NODES_PRESET)
  34. );
  35. assert.match(obfuscationResult.getObfuscatedCode(), /constructor\(\)\{\}/);
  36. });
  37. });