FunctionDeclarationObfuscator.spec.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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('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. Object.assign({}, NO_CUSTOM_NODES_PRESET)
  21. );
  22. const obfuscatedCode: string = obfuscationResult.getObfuscatedCode();
  23. it('shouldn\'t obfuscate function name if `functionDeclaration` parent block scope is a `ProgramNode`', () => {
  24. const functionNameIdentifierMatch: RegExpMatchArray|null = obfuscatedCode
  25. .match(/function *foo *\(\) *\{/);
  26. const functionCallIdentifierMatch: RegExpMatchArray|null = obfuscatedCode
  27. .match(/foo *\( *\);/);
  28. const functionParamIdentifierName: string = (<RegExpMatchArray>functionNameIdentifierMatch)[1];
  29. const functionBodyIdentifierName: string = (<RegExpMatchArray>functionCallIdentifierMatch)[1];
  30. assert.equal(functionParamIdentifierName, functionBodyIdentifierName);
  31. });
  32. it('should obfuscate function name if `functionDeclaration` parent block scope is not a `ProgramNode`', () => {
  33. const functionNameIdentifierMatch: RegExpMatchArray|null = obfuscatedCode
  34. .match(/function *_0x[a-z0-9]{4,6} *\(\) *\{/);
  35. const functionCallIdentifierMatch: RegExpMatchArray|null = obfuscatedCode
  36. .match(/_0x[a-z0-9]{4,6} *\( *\);/);
  37. const functionParamIdentifierName: string = (<RegExpMatchArray>functionNameIdentifierMatch)[1];
  38. const functionBodyIdentifierName: string = (<RegExpMatchArray>functionCallIdentifierMatch)[1];
  39. assert.equal(functionParamIdentifierName, functionBodyIdentifierName);
  40. });
  41. });
  42. });