FunctionDeclarationObfuscator.spec.ts 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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('FunctionDeclarationObfuscator', () => {
  6. describe('obfuscation of `functionDeclaration` node names', () => {
  7. const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  8. `
  9. function foo () {
  10. function bar () {
  11. }
  12. if (true) {
  13. bar();
  14. }
  15. }
  16. if (true) {
  17. foo();
  18. }
  19. `,
  20. {
  21. ...NO_CUSTOM_NODES_PRESET
  22. }
  23. );
  24. const obfuscatedCode: string = obfuscationResult.getObfuscatedCode();
  25. it('shouldn\'t obfuscate function name if `functionDeclaration` parent block scope is a `ProgramNode`', () => {
  26. const functionNameIdentifierMatch: RegExpMatchArray|null = obfuscatedCode
  27. .match(/function *foo *\(\) *\{/);
  28. const functionCallIdentifierMatch: RegExpMatchArray|null = obfuscatedCode
  29. .match(/foo *\( *\);/);
  30. const functionParamIdentifierName: string = (<RegExpMatchArray>functionNameIdentifierMatch)[1];
  31. const functionBodyIdentifierName: string = (<RegExpMatchArray>functionCallIdentifierMatch)[1];
  32. assert.equal(functionParamIdentifierName, functionBodyIdentifierName);
  33. });
  34. it('should obfuscate function name if `functionDeclaration` parent block scope is not a `ProgramNode`', () => {
  35. const functionNameIdentifierMatch: RegExpMatchArray|null = obfuscatedCode
  36. .match(/function *_0x[a-z0-9]{4,6} *\(\) *\{/);
  37. const functionCallIdentifierMatch: RegExpMatchArray|null = obfuscatedCode
  38. .match(/_0x[a-z0-9]{4,6} *\( *\);/);
  39. const functionParamIdentifierName: string = (<RegExpMatchArray>functionNameIdentifierMatch)[1];
  40. const functionBodyIdentifierName: string = (<RegExpMatchArray>functionCallIdentifierMatch)[1];
  41. assert.equal(functionParamIdentifierName, functionBodyIdentifierName);
  42. });
  43. });
  44. });