ConditionalCommentObfuscatingGuard.spec.ts 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import { assert } from 'chai';
  2. import { IObfuscationResult } from '../../../../../../src/interfaces/IObfuscationResult';
  3. import { JavaScriptObfuscator } from '../../../../../../src/JavaScriptObfuscatorFacade';
  4. import { NO_CUSTOM_NODES_PRESET } from '../../../../../../src/options/presets/NoCustomNodes';
  5. import { readFileAsString } from '../../../../../helpers/readFileAsString';
  6. describe('ConditionalCommentObfuscatingGuard', () => {
  7. describe('check (node: ESTree.Node): boolean', () => {
  8. describe('variant #1: `disable` conditional comment', () => {
  9. const obfuscatedVariableDeclarationRegExp: RegExp = /var *_0x([a-f0-9]){4,6} *= *0x1;/;
  10. const ignoredVariableDeclarationRegExp: RegExp = /var *bar *= *2;/;
  11. const consoleLogRegExp: RegExp = /console.log\(_0x([a-f0-9]){4,6}\);/;
  12. let obfuscatedCode: string;
  13. beforeEach(() => {
  14. const code: string = readFileAsString(__dirname + '/fixtures/simple.js');
  15. const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  16. code,
  17. {
  18. ...NO_CUSTOM_NODES_PRESET
  19. }
  20. );
  21. obfuscatedCode = obfuscationResult.getObfuscatedCode();
  22. });
  23. it('match #1: should obfuscate variable declaration before `disable` conditional comment', () => {
  24. assert.match(obfuscatedCode, obfuscatedVariableDeclarationRegExp);
  25. });
  26. it('match #2: should ignore variable declaration after `disable` conditional comment', () => {
  27. assert.match(obfuscatedCode, ignoredVariableDeclarationRegExp);
  28. });
  29. it('match #3: should obfuscate variable name in `console.log`', () => {
  30. assert.match(obfuscatedCode, consoleLogRegExp);
  31. });
  32. });
  33. describe('variant #2: `disable` and `enable` conditional comment', () => {
  34. const obfuscatedVariableDeclaration1RegExp: RegExp = /var *_0x([a-f0-9]){4,6} *= *0x1;/;
  35. const obfuscatedVariableDeclaration2RegExp: RegExp = /var *_0x([a-f0-9]){4,6} *= *0x3;/;
  36. const ignoredVariableDeclarationRegExp: RegExp = /var *bar *= *2;/;
  37. let obfuscatedCode: string;
  38. beforeEach(() => {
  39. const code: string = readFileAsString(__dirname + '/fixtures/disable-and-enable-comments.js');
  40. const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  41. code,
  42. {
  43. ...NO_CUSTOM_NODES_PRESET
  44. }
  45. );
  46. obfuscatedCode = obfuscationResult.getObfuscatedCode();
  47. });
  48. it('match #1: should obfuscate variable declaration before `disable` conditional comment', () => {
  49. assert.match(obfuscatedCode, obfuscatedVariableDeclaration1RegExp);
  50. });
  51. it('match #2: should ignore variable declaration after `disable` conditional comment', () => {
  52. assert.match(obfuscatedCode, ignoredVariableDeclarationRegExp);
  53. });
  54. it('match #3: should obfuscate variable declaration after `enable` conditional comment', () => {
  55. assert.match(obfuscatedCode, obfuscatedVariableDeclaration2RegExp);
  56. });
  57. });
  58. });
  59. });