StringArrayRotateFunctionTemplate.spec.ts 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. import 'reflect-metadata';
  2. import { assert } from 'chai';
  3. import { IObfuscationResult } from '../../../../../../src/interfaces/source-code/IObfuscationResult';
  4. import { NO_ADDITIONAL_NODES_PRESET } from '../../../../../../src/options/presets/NoCustomNodes';
  5. import { JavaScriptObfuscator } from '../../../../../../src/JavaScriptObfuscatorFacade';
  6. import { readFileAsString } from '../../../../../helpers/readFileAsString';
  7. describe('StringArrayRotateFunctionTemplate', () => {
  8. describe('Computed member expressions as array method calls', () => {
  9. describe('Array push', () => {
  10. const arrayPushBaseRegExp: RegExp = /_0x([a-f0-9]){4,6}\.push/;
  11. const arrayPushComputedRegExp: RegExp = /_0x([a-f0-9]){4,6}\['push']/;
  12. let obfuscatedCode: string;
  13. beforeEach(() => {
  14. const code: string = readFileAsString(__dirname + '/fixtures/simple-input.js');
  15. const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  16. code,
  17. {
  18. ...NO_ADDITIONAL_NODES_PRESET,
  19. stringArray: true,
  20. stringArrayThreshold: 1,
  21. rotateStringArray: true
  22. }
  23. );
  24. obfuscatedCode = obfuscationResult.getObfuscatedCode();
  25. });
  26. it('Should use computed member expression in `array.push` method', () => {
  27. assert.match(obfuscatedCode, arrayPushComputedRegExp);
  28. });
  29. it('Should not use base member expression in `array.push` method', () => {
  30. assert.notMatch(obfuscatedCode, arrayPushBaseRegExp);
  31. });
  32. });
  33. describe('Array shift', () => {
  34. const arrayShiftBaseRegExp: RegExp = /_0x([a-f0-9]){4,6}\.shift/;
  35. const arrayShiftComputedRegExp: RegExp = /_0x([a-f0-9]){4,6}\['shift']/;
  36. let obfuscatedCode: string;
  37. beforeEach(() => {
  38. const code: string = readFileAsString(__dirname + '/fixtures/simple-input.js');
  39. const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  40. code,
  41. {
  42. ...NO_ADDITIONAL_NODES_PRESET,
  43. stringArray: true,
  44. stringArrayThreshold: 1,
  45. rotateStringArray: true
  46. }
  47. );
  48. obfuscatedCode = obfuscationResult.getObfuscatedCode();
  49. });
  50. it('Should use computed member expression in `array.shift` method', () => {
  51. assert.match(obfuscatedCode, arrayShiftComputedRegExp);
  52. });
  53. it('Should not use base member expression in `array.shift` method', () => {
  54. assert.notMatch(obfuscatedCode, arrayShiftBaseRegExp);
  55. });
  56. });
  57. });
  58. describe('Prevailing kind of variables', () => {
  59. describe('`var` kind', () => {
  60. let obfuscatedCode: string,
  61. stringArrayRotateFunctionTryCatchRegExp: RegExp = /try *{var *_0x([a-f0-9]){4,6}/;
  62. beforeEach(() => {
  63. const code: string = readFileAsString(__dirname + '/fixtures/prevailing-kind-of-variables-var.js');
  64. const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  65. code,
  66. {
  67. ...NO_ADDITIONAL_NODES_PRESET,
  68. stringArray: true,
  69. stringArrayThreshold: 1,
  70. rotateStringArray: true
  71. }
  72. );
  73. obfuscatedCode = obfuscationResult.getObfuscatedCode();
  74. });
  75. it('Should return correct kind of variables for string array rotate function', () => {
  76. assert.match(obfuscatedCode, stringArrayRotateFunctionTryCatchRegExp);
  77. });
  78. it('Should does not break on obfuscating', () => {
  79. assert.doesNotThrow(() => obfuscatedCode);
  80. });
  81. });
  82. describe('`const` kind', () => {
  83. let obfuscatedCode: string,
  84. stringArrayRotateFunctionTryCatchRegExp: RegExp = /try *{const *_0x([a-f0-9]){4,6}/;
  85. beforeEach(() => {
  86. const code: string = readFileAsString(__dirname + '/fixtures/prevailing-kind-of-variables-const.js');
  87. const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  88. code,
  89. {
  90. ...NO_ADDITIONAL_NODES_PRESET,
  91. stringArray: true,
  92. stringArrayThreshold: 1,
  93. rotateStringArray: true
  94. }
  95. );
  96. obfuscatedCode = obfuscationResult.getObfuscatedCode();
  97. });
  98. it('Should return correct kind of variables for string array rotate function', () => {
  99. assert.match(obfuscatedCode, stringArrayRotateFunctionTryCatchRegExp);
  100. });
  101. it('Should does not break on obfuscating', () => {
  102. assert.doesNotThrow(() => obfuscatedCode);
  103. });
  104. });
  105. describe('`let` kind', () => {
  106. let obfuscatedCode: string,
  107. stringArrayRotateFunctionTryCatchRegExp: RegExp = /try *{const *_0x([a-f0-9]){4,6}/;
  108. beforeEach(() => {
  109. const code: string = readFileAsString(__dirname + '/fixtures/prevailing-kind-of-variables-let.js');
  110. const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  111. code,
  112. {
  113. ...NO_ADDITIONAL_NODES_PRESET,
  114. stringArray: true,
  115. stringArrayThreshold: 1,
  116. rotateStringArray: true
  117. }
  118. );
  119. obfuscatedCode = obfuscationResult.getObfuscatedCode();
  120. });
  121. it('Should return correct kind of variables for string array rotate function', () => {
  122. assert.match(obfuscatedCode, stringArrayRotateFunctionTryCatchRegExp);
  123. });
  124. it('Should does not break on obfuscating', () => {
  125. assert.doesNotThrow(() => obfuscatedCode);
  126. });
  127. });
  128. });
  129. });