DebugProtectionFunctionIntervalTemplate.spec.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import { assert } from 'chai';
  2. import { readFileAsString } from '../../../../../helpers/readFileAsString';
  3. import { HIGH_OBFUSCATION_PRESET } from '../../../../../../src/options/presets/HighObfuscation';
  4. import { NO_ADDITIONAL_NODES_PRESET } from '../../../../../../src/options/presets/NoCustomNodes';
  5. import { JavaScriptObfuscator } from '../../../../../../src/JavaScriptObfuscatorFacade';
  6. describe('DebugProtectionFunctionIntervalTemplate', function () {
  7. const variableMatch: string = '_0x([a-f0-9]){4,6}';
  8. describe('Variant #1 - `high-obfuscation` preset interval', () => {
  9. const debugProtectionIntervalRegExp: RegExp = new RegExp(
  10. `${variableMatch}\\['setInterval'\\]\\( *` +
  11. `${variableMatch}, *` +
  12. '0xfa0 *' +
  13. `\\);`
  14. );
  15. let obfuscatedCode: string;
  16. beforeEach(() => {
  17. const code: string = readFileAsString(__dirname + '/fixtures/input.js');
  18. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  19. code,
  20. {
  21. ...NO_ADDITIONAL_NODES_PRESET,
  22. debugProtection: true,
  23. debugProtectionInterval: HIGH_OBFUSCATION_PRESET.debugProtectionInterval
  24. }
  25. ).getObfuscatedCode();
  26. console.log(obfuscatedCode);
  27. });
  28. it('Should add debug protection interval code with default interval value', () => {
  29. assert.match(obfuscatedCode, debugProtectionIntervalRegExp);
  30. });
  31. });
  32. describe('Variant #2 - custom interval', () => {
  33. const debugProtectionIntervalRegExp: RegExp = new RegExp(
  34. `${variableMatch}\\['setInterval'\\]\\( *` +
  35. `${variableMatch}, *` +
  36. '0x64 *' +
  37. `\\);`
  38. );
  39. let obfuscatedCode: string;
  40. beforeEach(() => {
  41. const code: string = readFileAsString(__dirname + '/fixtures/input.js');
  42. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  43. code,
  44. {
  45. ...NO_ADDITIONAL_NODES_PRESET,
  46. debugProtection: true,
  47. debugProtectionInterval: 100
  48. }
  49. ).getObfuscatedCode();
  50. });
  51. it('Should add debug protection interval code with default interval value', () => {
  52. assert.match(obfuscatedCode, debugProtectionIntervalRegExp);
  53. });
  54. });
  55. describe('Variant #3 - no interval', () => {
  56. const debugProtectionIntervalRegExp: RegExp = /setInterval/
  57. let obfuscatedCode: string;
  58. beforeEach(() => {
  59. const code: string = readFileAsString(__dirname + '/fixtures/input.js');
  60. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  61. code,
  62. {
  63. ...NO_ADDITIONAL_NODES_PRESET,
  64. debugProtection: true,
  65. debugProtectionInterval: 0
  66. }
  67. ).getObfuscatedCode();
  68. });
  69. it('Should not add debug protection interval code', () => {
  70. assert.notMatch(obfuscatedCode, debugProtectionIntervalRegExp);
  71. });
  72. });
  73. });