FunctionObfuscator.spec.ts 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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('FunctionObfuscator', () => {
  6. describe('identifiers obfuscation inside `FunctionDeclaration` and `FunctionExpression` node body', () => {
  7. const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  8. `
  9. (function () {
  10. var test = function (test) {
  11. console.log(test);
  12. if (true) {
  13. var test = 5
  14. }
  15. variable = 6;
  16. return test;
  17. }
  18. })();
  19. `,
  20. {
  21. ...NO_CUSTOM_NODES_PRESET
  22. }
  23. );
  24. const obfuscatedCode: string = obfuscationResult.getObfuscatedCode();
  25. it('should correct obfuscate both function parameter identifier and function body identifier with same name', () => {
  26. const functionParamIdentifierMatch: RegExpMatchArray|null = obfuscatedCode
  27. .match(/var _0x[a-z0-9]{4,6} *= *function *\((_0x[a-z0-9]{4,6})\) *\{/);
  28. const functionBodyIdentifierMatch: RegExpMatchArray|null = obfuscatedCode
  29. .match(/console\['\\x6c\\x6f\\x67'\]\((_0x[a-z0-9]{4,6})\)/);
  30. const functionParamIdentifierName: string = (<RegExpMatchArray>functionParamIdentifierMatch)[1];
  31. const functionBodyIdentifierName: string = (<RegExpMatchArray>functionBodyIdentifierMatch)[1];
  32. assert.equal(functionParamIdentifierName, functionBodyIdentifierName);
  33. });
  34. it('shouldn\'t obfuscate other variables in function body', () => {
  35. assert.equal(/variable *= *0x6;/.test(obfuscatedCode), true);
  36. });
  37. });
  38. });