ConsoleOutputDisableExpressionCodeHelper.spec.ts 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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('ConsoleOutputDisableExpressionCodeHelper', () => {
  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. const consoleTableRegExp: RegExp = /_0x([a-f0-9]){4,6}\['console'\]\['table'\] *= *_0x([a-f0-9]){4,6};/u;
  10. describe('`disableConsoleOutput` option is set', () => {
  11. let obfuscatedCode: string;
  12. before(() => {
  13. const code: string = readFileAsString(__dirname + '/fixtures/simple-input.js');
  14. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  15. code,
  16. {
  17. ...NO_ADDITIONAL_NODES_PRESET,
  18. disableConsoleOutput: true
  19. }
  20. ).getObfuscatedCode();
  21. });
  22. it('match #1: should correctly append code helper into the obfuscated code', () => {
  23. assert.match(obfuscatedCode, consoleLogRegExp);
  24. });
  25. it('match #2: should correctly append code helper into the obfuscated code', () => {
  26. assert.match(obfuscatedCode, consoleErrorRegExp);
  27. });
  28. it('match #3: should correctly append code helper into the obfuscated code', () => {
  29. assert.match(obfuscatedCode, consoleWarnRegExp);
  30. });
  31. it('match #4: should correctly append code helper into the obfuscated code', () => {
  32. assert.match(obfuscatedCode, consoleTableRegExp);
  33. });
  34. });
  35. describe('`disableConsoleOutput` option isn\'t set', () => {
  36. let obfuscatedCode: string;
  37. before(() => {
  38. const code: string = readFileAsString(__dirname + '/fixtures/simple-input.js');
  39. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  40. code,
  41. {
  42. ...NO_ADDITIONAL_NODES_PRESET,
  43. disableConsoleOutput: false
  44. }
  45. ).getObfuscatedCode();
  46. });
  47. it('match #1: shouldn\'t append code helper into the obfuscated code', () => {
  48. assert.notMatch(obfuscatedCode, consoleLogRegExp);
  49. });
  50. it('match #2: shouldn\'t append code helper into the obfuscated code', () => {
  51. assert.notMatch(obfuscatedCode, consoleErrorRegExp);
  52. });
  53. it('match #3: shouldn\'t append code helper into the obfuscated code', () => {
  54. assert.notMatch(obfuscatedCode, consoleWarnRegExp);
  55. });
  56. });
  57. });