FunctionObfuscator.spec.ts 1.9 KB

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