ConsoleOutputDisableExpressionNode.spec.ts 2.8 KB

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