StringArrayCallsWrapperCodeHelper.spec.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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('Preserve string array name', () => {
  43. const callsWrapperRegExp: RegExp = new RegExp(`` +
  44. `var b *= *function *\\(c, *d\\) *{ *` +
  45. `c *= *c *- *0x0; *` +
  46. `var e *= *a\\[c]; *` +
  47. ``);
  48. let obfuscatedCode: string;
  49. before(() => {
  50. const code: string = readFileAsString(__dirname + '/fixtures/simple-input.js');
  51. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  52. code,
  53. {
  54. ...NO_ADDITIONAL_NODES_PRESET,
  55. identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator,
  56. stringArray: true,
  57. stringArrayThreshold: 1,
  58. stringArrayEncoding: StringArrayEncoding.Base64
  59. }
  60. ).getObfuscatedCode();
  61. });
  62. it('should preserve string array name', () => {
  63. assert.match(obfuscatedCode, callsWrapperRegExp);
  64. });
  65. });
  66. });