StringArrayCallsWrapperCodeHelper.spec.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import { assert } from 'chai';
  2. import { IdentifierNamesGenerator } from '../../../../src/enums/generators/identifier-names-generators/IdentifierNamesGenerator';
  3. import { StringArrayEncoding } from '../../../../src/enums/StringArrayEncoding';
  4. import { NO_ADDITIONAL_NODES_PRESET } from '../../../../src/options/presets/NoCustomNodes';
  5. import { readFileAsString } from '../../../helpers/readFileAsString';
  6. import { JavaScriptObfuscator } from '../../../../src/JavaScriptObfuscatorFacade';
  7. describe('StringArrayCallsWrapperCodeHelper', () => {
  8. const regExp: RegExp = /_0x([a-f0-9]){4,6} *= *_0x([a-f0-9]){4,6} *- *0x0\;/;
  9. describe('`stringArray` 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. stringArray: true,
  18. stringArrayThreshold: 1
  19. }
  20. ).getObfuscatedCode();
  21. });
  22. it('should correctly append code helper into the obfuscated code', () => {
  23. assert.match(obfuscatedCode, regExp);
  24. });
  25. });
  26. describe('`stringArray` option isn\'t set', () => {
  27. let obfuscatedCode: string;
  28. before(() => {
  29. const code: string = readFileAsString(__dirname + '/fixtures/simple-input.js');
  30. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  31. code,
  32. {
  33. ...NO_ADDITIONAL_NODES_PRESET,
  34. stringArray: false
  35. }
  36. ).getObfuscatedCode();
  37. });
  38. it('shouldn\'t append code helper into the obfuscated code', () => {
  39. assert.notMatch(obfuscatedCode, regExp);
  40. });
  41. });
  42. describe('`stringArrayIntermediateVariablesCount` option is set', () => {
  43. const stringArrayCallRegExp: RegExp = new RegExp(
  44. 'return _0x([a-f0-9]){4,6};' +
  45. '};' +
  46. 'var _0x([a-f0-9]){4} *= *_0x([a-f0-9]){4};' +
  47. 'var _0x([a-f0-9]){4} *= *_0x([a-f0-9]){4};' +
  48. 'var _0x([a-f0-9]){4} *= *_0x([a-f0-9]){4};' +
  49. 'var test *= *_0x([a-f0-9]){4}\\(\'0x0\'\\);'
  50. );
  51. let obfuscatedCode: string;
  52. before(() => {
  53. const code: string = readFileAsString(__dirname + '/fixtures/simple-input.js');
  54. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  55. code,
  56. {
  57. ...NO_ADDITIONAL_NODES_PRESET,
  58. stringArray: true,
  59. stringArrayThreshold: 1,
  60. stringArrayIntermediateVariablesCount: 3
  61. }
  62. ).getObfuscatedCode();
  63. });
  64. it('should correctly append `StringArrayCallsWrapperIntermediateTemplate` template into the obfuscated code', () => {
  65. assert.match(obfuscatedCode, stringArrayCallRegExp);
  66. });
  67. });
  68. describe('Preserve string array name', () => {
  69. const callsWrapperRegExp: RegExp = new RegExp(`` +
  70. `var b *= *function *\\(c, *d\\) *{ *` +
  71. `c *= *c *- *0x0; *` +
  72. `var e *= *a\\[c]; *` +
  73. ``);
  74. let obfuscatedCode: string;
  75. before(() => {
  76. const code: string = readFileAsString(__dirname + '/fixtures/simple-input.js');
  77. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  78. code,
  79. {
  80. ...NO_ADDITIONAL_NODES_PRESET,
  81. identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator,
  82. stringArray: true,
  83. stringArrayThreshold: 1,
  84. stringArrayEncoding: [StringArrayEncoding.Base64]
  85. }
  86. ).getObfuscatedCode();
  87. });
  88. it('should preserve string array name', () => {
  89. assert.match(obfuscatedCode, callsWrapperRegExp);
  90. });
  91. });
  92. });