StringArrayCodeHelperGroup.spec.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import { assert } from 'chai';
  2. import { StringArrayEncoding } from '../../../../../src/enums/node-transformers/string-array-transformers/StringArrayEncoding';
  3. import { IdentifierNamesGenerator } from '../../../../../src/enums/generators/identifier-names-generators/IdentifierNamesGenerator';
  4. import { NO_ADDITIONAL_NODES_PRESET } from '../../../../../src/options/presets/NoCustomNodes';
  5. import { getRegExpMatch } from '../../../../helpers/getRegExpMatch';
  6. import { readFileAsString } from '../../../../helpers/readFileAsString';
  7. import { JavaScriptObfuscator } from '../../../../../src/JavaScriptObfuscatorFacade';
  8. describe('StringArrayCodeHelperGroup', () => {
  9. const regExp: RegExp = new RegExp(
  10. 'function *\\w *\\(\\w, *\\w\\) *{.*return \\w;}.*' +
  11. 'function *\\w *\\(\\w, *\\w\\) *{.*return \\w;}.*' +
  12. 'function *\\w *\\(\\w, *\\w\\) *{.*return \\w;}'
  13. );
  14. const stringArrayCallsWrapperRegExp: RegExp = new RegExp(
  15. `function *(\\w) *\\(\\w, *\\w\\) *{.*return \\w;}.*`
  16. );
  17. describe('StringArrayCallsWrapper code helper names', () => {
  18. const stringArrayCallsWrapperNames: Set<string> = new Set();
  19. const samplesCount: number = 30;
  20. let obfuscatedCode: string;
  21. const expectedUniqStringArrayCallsWrapperNamesCount: number = 3;
  22. before(() => {
  23. const code: string = readFileAsString(__dirname + '/fixtures/simple-input.js');
  24. for (let i = 0; i < samplesCount; i++) {
  25. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  26. code,
  27. {
  28. ...NO_ADDITIONAL_NODES_PRESET,
  29. identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator,
  30. stringArray: true,
  31. stringArrayThreshold: 1,
  32. stringArrayEncoding: [
  33. StringArrayEncoding.None,
  34. StringArrayEncoding.Base64,
  35. StringArrayEncoding.Rc4
  36. ]
  37. }
  38. ).getObfuscatedCode();
  39. const callsWrapperName: string = getRegExpMatch(obfuscatedCode, stringArrayCallsWrapperRegExp);
  40. stringArrayCallsWrapperNames.add(callsWrapperName);
  41. }
  42. });
  43. it('should correct place all StringArrayCallsWrapper code helpers', () => {
  44. assert.match(obfuscatedCode, regExp);
  45. });
  46. it('should place multiple StringArrayCallsWrapper code helper names in the random order', () => {
  47. assert.equal(stringArrayCallsWrapperNames.size, expectedUniqStringArrayCallsWrapperNamesCount);
  48. });
  49. });
  50. });