StringArrayRotateFunctionCodeHelper.spec.ts 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import { assert } from 'chai';
  2. import { IdentifierNamesGenerator } from '../../../../src/enums/generators/identifier-names-generators/IdentifierNamesGenerator';
  3. import { NO_ADDITIONAL_NODES_PRESET } from '../../../../src/options/presets/NoCustomNodes';
  4. import { readFileAsString } from '../../../helpers/readFileAsString';
  5. import { JavaScriptObfuscator } from '../../../../src/JavaScriptObfuscatorFacade';
  6. describe('StringArrayRotateFunctionCodeHelper', () => {
  7. describe('Base behaviour', () => {
  8. const regExp: RegExp = /while *\(!!\[]\) *\{/;
  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. rotateStringArray: true,
  18. stringArray: true,
  19. stringArrayThreshold: 1
  20. }
  21. ).getObfuscatedCode();
  22. });
  23. it('should correctly append code helper into the obfuscated code', () => {
  24. assert.match(obfuscatedCode, regExp);
  25. });
  26. });
  27. describe('`stringArray` option isn\'t set', () => {
  28. let obfuscatedCode: string;
  29. before(() => {
  30. const code: string = readFileAsString(__dirname + '/fixtures/simple-input.js');
  31. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  32. code,
  33. {
  34. ...NO_ADDITIONAL_NODES_PRESET,
  35. rotateStringArray: false,
  36. stringArray: true,
  37. stringArrayThreshold: 1
  38. }
  39. ).getObfuscatedCode();
  40. });
  41. it('shouldn\'t append code helper into the obfuscated code', () => {
  42. assert.notMatch(obfuscatedCode, regExp);
  43. });
  44. });
  45. });
  46. describe('Preserve string array name', () => {
  47. const arrayRotateRegExp: RegExp = /c\['push']\(c\['shift']\(\)\);/;
  48. const comparisonRegExp: RegExp = /if *\(e *=== *d\) *{/;
  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. rotateStringArray: true,
  58. stringArray: true,
  59. stringArrayThreshold: 1
  60. }
  61. ).getObfuscatedCode();
  62. });
  63. it('should preserve string array name', () => {
  64. assert.match(obfuscatedCode, arrayRotateRegExp);
  65. });
  66. it('generate valid identifier names', () => {
  67. assert.match(obfuscatedCode, comparisonRegExp);
  68. });
  69. });
  70. });