StringArrayRotateFunctionCodeHelper.spec.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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('Comparison expression', () => {
  47. describe('Should add comparison expression to the code helper', () => {
  48. const comparisonExpressionRegExp: RegExp = /var _0x([a-f0-9]){4,6} *= *-?parseInt\(_0x([a-f0-9]){4,6}\(0x.\)\)/;
  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. rotateStringArray: true,
  57. stringArray: true,
  58. stringArrayThreshold: 1
  59. }
  60. ).getObfuscatedCode();
  61. });
  62. it('should add comparison expression to the code', () => {
  63. assert.match(obfuscatedCode, comparisonExpressionRegExp);
  64. });
  65. });
  66. });
  67. describe('Preserve string array name', () => {
  68. const arrayRotateRegExp: RegExp = /e\['push']\(e\['shift']\(\)\);/;
  69. const comparisonRegExp: RegExp = /if *\(f *=== *d\) *{/;
  70. let obfuscatedCode: string;
  71. before(() => {
  72. const code: string = readFileAsString(__dirname + '/fixtures/simple-input.js');
  73. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  74. code,
  75. {
  76. ...NO_ADDITIONAL_NODES_PRESET,
  77. identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator,
  78. rotateStringArray: true,
  79. stringArray: true,
  80. stringArrayThreshold: 1
  81. }
  82. ).getObfuscatedCode();
  83. });
  84. it('should preserve string array name', () => {
  85. assert.match(obfuscatedCode, arrayRotateRegExp);
  86. });
  87. it('generate valid identifier names', () => {
  88. assert.match(obfuscatedCode, comparisonRegExp);
  89. });
  90. });
  91. });