MethodDefinitionObfuscator.spec.ts 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import { assert } from 'chai';
  2. import { IObfuscationResult } from '../../../../src/interfaces/IObfuscationResult';
  3. import { NO_CUSTOM_NODES_PRESET } from '../../../../src/preset-options/NoCustomNodesPreset';
  4. import { JavaScriptObfuscator } from '../../../../src/JavaScriptObfuscator';
  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. {
  16. ...NO_CUSTOM_NODES_PRESET
  17. }
  18. );
  19. assert.match(obfuscationResult.getObfuscatedCode(), /\['\\x62\\x61\\x72'\]\(\)\{\}/);
  20. });
  21. it('should replace method definition node `key` property with unicode array call', () => {
  22. let obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  23. code,
  24. {
  25. ...NO_CUSTOM_NODES_PRESET,
  26. stringArray: true,
  27. stringArrayThreshold: 1
  28. }
  29. );
  30. assert.match(obfuscationResult.getObfuscatedCode(), /var *_0x([a-z0-9]){4} *= *\['\\x62\\x61\\x72'\];/);
  31. assert.match(obfuscationResult.getObfuscatedCode(), /\[_0x([a-z0-9]){4}\('0x0'\)\]\(\)\{\}/);
  32. });
  33. it('should not obfuscate method definition node with `constructor` key', () => {
  34. let obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  35. code,
  36. {
  37. ...NO_CUSTOM_NODES_PRESET
  38. }
  39. );
  40. assert.match(obfuscationResult.getObfuscatedCode(), /constructor\(\)\{\}/);
  41. });
  42. });