FunctionObfuscator.spec.ts 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. 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. Object.assign({}, NO_CUSTOM_NODES_PRESET)
  21. );
  22. const obfuscatedCode: string = obfuscationResult.getObfuscatedCode();
  23. it('should correct obfuscate both function parameter identifier and function body identifier with same name', () => {
  24. const functionParamIdentifierMatch: RegExpMatchArray|null = obfuscatedCode
  25. .match(/var _0x[a-z0-9]{4,6} *= *function *\((_0x[a-z0-9]{4,6})\) *\{/);
  26. const functionBodyIdentifierMatch: RegExpMatchArray|null = obfuscatedCode
  27. .match(/console\['\\x6c\\x6f\\x67'\]\((_0x[a-z0-9]{4,6})\)/);
  28. const functionParamIdentifierName: string = (<RegExpMatchArray>functionParamIdentifierMatch)[1];
  29. const functionBodyIdentifierName: string = (<RegExpMatchArray>functionBodyIdentifierMatch)[1];
  30. assert.equal(functionParamIdentifierName, functionBodyIdentifierName);
  31. });
  32. it('shouldn\'t obfuscate other variables in function body', () => {
  33. assert.equal(/variable *= *0x6;/.test(obfuscatedCode), true);
  34. });
  35. });
  36. });