StringArrayCallsWrapperCodeHelper.spec.ts 3.0 KB

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