ConsoleOutputDisableExpressionNode.spec.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import { assert } from 'chai';
  2. import { NO_ADDITIONAL_NODES_PRESET } from '../../../../src/options/presets/NoCustomNodes';
  3. import { readFileAsString } from '../../../helpers/readFileAsString';
  4. import { JavaScriptObfuscator } from '../../../../src/JavaScriptObfuscatorFacade';
  5. describe('ConsoleOutputDisableExpressionNode', () => {
  6. const consoleLogRegExp: RegExp = /_0x([a-f0-9]){4,6}\['console'\]\['log'\] *= *_0x([a-f0-9]){4,6};/u;
  7. const consoleErrorRegExp: RegExp = /_0x([a-f0-9]){4,6}\['console'\]\['error'\] *= *_0x([a-f0-9]){4,6};/u;
  8. const consoleWarnRegExp: RegExp = /_0x([a-f0-9]){4,6}\['console'\]\['warn'\] *= *_0x([a-f0-9]){4,6};/u;
  9. describe('`disableConsoleOutput` option is set', () => {
  10. let obfuscatedCode: string;
  11. before(() => {
  12. const code: string = readFileAsString(__dirname + '/fixtures/simple-input.js');
  13. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  14. code,
  15. {
  16. ...NO_ADDITIONAL_NODES_PRESET,
  17. disableConsoleOutput: true
  18. }
  19. ).getObfuscatedCode();
  20. });
  21. it('match #1: should correctly append custom node into the obfuscated code', () => {
  22. assert.match(obfuscatedCode, consoleLogRegExp);
  23. });
  24. it('match #2: should correctly append custom node into the obfuscated code', () => {
  25. assert.match(obfuscatedCode, consoleErrorRegExp);
  26. });
  27. it('match #3: should correctly append custom node into the obfuscated code', () => {
  28. assert.match(obfuscatedCode, consoleWarnRegExp);
  29. });
  30. });
  31. describe('`disableConsoleOutput` option isn\'t set', () => {
  32. let obfuscatedCode: string;
  33. before(() => {
  34. const code: string = readFileAsString(__dirname + '/fixtures/simple-input.js');
  35. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  36. code,
  37. {
  38. ...NO_ADDITIONAL_NODES_PRESET,
  39. disableConsoleOutput: false
  40. }
  41. ).getObfuscatedCode();
  42. });
  43. it('match #1: shouldn\'t append custom node into the obfuscated code', () => {
  44. assert.notMatch(obfuscatedCode, consoleLogRegExp);
  45. });
  46. it('match #2: shouldn\'t append custom node into the obfuscated code', () => {
  47. assert.notMatch(obfuscatedCode, consoleErrorRegExp);
  48. });
  49. it('match #3: shouldn\'t append custom node into the obfuscated code', () => {
  50. assert.notMatch(obfuscatedCode, consoleWarnRegExp);
  51. });
  52. });
  53. });