OptionsNormalizer.spec.ts 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. import { assert } from 'chai';
  2. import { TInputOptions } from '../../../../src/types/options/TInputOptions';
  3. import { IOptions } from '../../../../src/interfaces/options/IOptions';
  4. import { DEFAULT_PRESET } from '../../../../src/options/presets/Default';
  5. import { Options } from '../../../../src/options/Options';
  6. import { OptionsNormalizer } from '../../../../src/options/OptionsNormalizer';
  7. /**
  8. * @param optionsPreset
  9. * @returns {IOptions}
  10. */
  11. function getNormalizedOptions (optionsPreset: TInputOptions): TInputOptions {
  12. const options: IOptions = new Options(optionsPreset);
  13. return OptionsNormalizer.normalizeOptions(options);
  14. }
  15. describe('OptionsNormalizer', () => {
  16. describe('normalizeOptions (options: IObfuscatorOptions): IObfuscatorOptions', () => {
  17. let optionsPreset: TInputOptions,
  18. expectedOptionsPreset: TInputOptions;
  19. it('should normalize options preset: controlFlowFlatteningThresholdRule', () => {
  20. optionsPreset = {
  21. ...DEFAULT_PRESET,
  22. controlFlowFlattening: true,
  23. controlFlowFlatteningThreshold: 0
  24. };
  25. expectedOptionsPreset = {
  26. ...DEFAULT_PRESET,
  27. controlFlowFlattening: false,
  28. controlFlowFlatteningThreshold: 0
  29. };
  30. assert.deepEqual(getNormalizedOptions(optionsPreset), expectedOptionsPreset);
  31. });
  32. it('should normalize options preset: deadCodeInjectionRule', () => {
  33. optionsPreset = {
  34. ...DEFAULT_PRESET,
  35. deadCodeInjection: true,
  36. deadCodeInjectionThreshold: 0.4,
  37. stringArray: false,
  38. stringArrayThreshold: 0
  39. };
  40. expectedOptionsPreset = {
  41. ...DEFAULT_PRESET,
  42. deadCodeInjection: true,
  43. deadCodeInjectionThreshold: 0.4,
  44. stringArray: true,
  45. stringArrayThreshold: 0.75
  46. };
  47. assert.deepEqual(getNormalizedOptions(optionsPreset), expectedOptionsPreset);
  48. });
  49. it('should normalize options preset: deadCodeInjectionRule. `stringArrayThreshold` option is not empty', () => {
  50. optionsPreset = {
  51. ...DEFAULT_PRESET,
  52. deadCodeInjection: true,
  53. deadCodeInjectionThreshold: 0.4,
  54. stringArray: false,
  55. stringArrayThreshold: 0.5
  56. };
  57. expectedOptionsPreset = {
  58. ...DEFAULT_PRESET,
  59. deadCodeInjection: true,
  60. deadCodeInjectionThreshold: 0.4,
  61. stringArray: true,
  62. stringArrayThreshold: 0.5
  63. };
  64. assert.deepEqual(getNormalizedOptions(optionsPreset), expectedOptionsPreset);
  65. });
  66. it('should normalize options preset: deadCodeInjectionThresholdRule', () => {
  67. optionsPreset = {
  68. ...DEFAULT_PRESET,
  69. deadCodeInjection: true,
  70. deadCodeInjectionThreshold: 0
  71. };
  72. expectedOptionsPreset = {
  73. ...DEFAULT_PRESET,
  74. deadCodeInjection: false,
  75. deadCodeInjectionThreshold: 0
  76. };
  77. assert.deepEqual(getNormalizedOptions(optionsPreset), expectedOptionsPreset);
  78. });
  79. it('should normalize options preset: domainLockRule', () => {
  80. optionsPreset = {
  81. ...DEFAULT_PRESET,
  82. domainLock: ['//localhost:9000', 'https://google.ru/abc?cde=fgh']
  83. };
  84. expectedOptionsPreset = {
  85. ...DEFAULT_PRESET,
  86. domainLock: ['localhost', 'google.ru']
  87. };
  88. assert.deepEqual(getNormalizedOptions(optionsPreset), expectedOptionsPreset);
  89. });
  90. it('should normalize options preset: selfDefendingRule', () => {
  91. optionsPreset = {
  92. ...DEFAULT_PRESET,
  93. selfDefending: true,
  94. compact: false
  95. };
  96. expectedOptionsPreset = {
  97. ...DEFAULT_PRESET,
  98. selfDefending: true,
  99. compact: true
  100. };
  101. assert.deepEqual(getNormalizedOptions(optionsPreset), expectedOptionsPreset);
  102. });
  103. it('should normalize options preset: sourceMapBaseUrlRule #1', () => {
  104. optionsPreset = {
  105. ...DEFAULT_PRESET,
  106. sourceMapBaseUrl: 'http://localhost:9000',
  107. };
  108. expectedOptionsPreset = {
  109. ...DEFAULT_PRESET,
  110. sourceMapBaseUrl: ''
  111. };
  112. assert.deepEqual(getNormalizedOptions(optionsPreset), expectedOptionsPreset);
  113. });
  114. it('should normalize options preset: sourceMapBaseUrlRule #2', () => {
  115. optionsPreset = {
  116. ...DEFAULT_PRESET,
  117. sourceMapBaseUrl: 'http://localhost:9000',
  118. sourceMapFileName: '/outputSourceMapName.map'
  119. };
  120. expectedOptionsPreset = {
  121. ...DEFAULT_PRESET,
  122. sourceMapBaseUrl: 'http://localhost:9000/',
  123. sourceMapFileName: 'outputSourceMapName.js.map'
  124. };
  125. assert.deepEqual(getNormalizedOptions(optionsPreset), expectedOptionsPreset);
  126. });
  127. it('should normalize options preset: sourceMapFileNameRule', () => {
  128. optionsPreset = {
  129. ...DEFAULT_PRESET,
  130. sourceMapBaseUrl: 'http://localhost:9000',
  131. sourceMapFileName: '//outputSourceMapName'
  132. };
  133. expectedOptionsPreset = {
  134. ...DEFAULT_PRESET,
  135. sourceMapBaseUrl: 'http://localhost:9000/',
  136. sourceMapFileName: 'outputSourceMapName.js.map'
  137. };
  138. assert.deepEqual(getNormalizedOptions(optionsPreset), expectedOptionsPreset);
  139. });
  140. it('should normalize options preset: stringArrayRule', () => {
  141. optionsPreset = {
  142. ...DEFAULT_PRESET,
  143. stringArray: false,
  144. stringArrayEncoding: 'rc4',
  145. stringArrayThreshold: 0.5,
  146. rotateStringArray: true
  147. };
  148. expectedOptionsPreset = {
  149. ...DEFAULT_PRESET,
  150. stringArray: false,
  151. stringArrayEncoding: false,
  152. stringArrayThreshold: 0,
  153. rotateStringArray: false
  154. };
  155. assert.deepEqual(getNormalizedOptions(optionsPreset), expectedOptionsPreset);
  156. });
  157. it('should normalize options preset: stringArrayEncodingRule', () => {
  158. optionsPreset = {
  159. ...DEFAULT_PRESET,
  160. stringArrayEncoding: true
  161. };
  162. expectedOptionsPreset = {
  163. ...DEFAULT_PRESET,
  164. stringArrayEncoding: 'base64'
  165. };
  166. assert.deepEqual(getNormalizedOptions(optionsPreset), expectedOptionsPreset);
  167. });
  168. it('should normalize options preset: stringArrayThresholdRule', () => {
  169. optionsPreset = {
  170. ...DEFAULT_PRESET,
  171. rotateStringArray: true,
  172. stringArray: true,
  173. stringArrayThreshold: 0
  174. };
  175. expectedOptionsPreset = {
  176. ...DEFAULT_PRESET,
  177. rotateStringArray: false,
  178. stringArray: false,
  179. stringArrayThreshold: 0
  180. };
  181. assert.deepEqual(getNormalizedOptions(optionsPreset), expectedOptionsPreset);
  182. });
  183. });
  184. });