OptionsNormalizer.spec.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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: deadCodeInjectionThresholdRule', () => {
  33. optionsPreset = {
  34. ...DEFAULT_PRESET,
  35. deadCodeInjection: true,
  36. deadCodeInjectionThreshold: 0
  37. };
  38. expectedOptionsPreset = {
  39. ...DEFAULT_PRESET,
  40. deadCodeInjection: false,
  41. deadCodeInjectionThreshold: 0
  42. };
  43. assert.deepEqual(getNormalizedOptions(optionsPreset), expectedOptionsPreset);
  44. });
  45. it('should normalize options preset: domainLockRule', () => {
  46. optionsPreset = {
  47. ...DEFAULT_PRESET,
  48. domainLock: ['//localhost:9000', 'https://google.ru/abc?cde=fgh']
  49. };
  50. expectedOptionsPreset = {
  51. ...DEFAULT_PRESET,
  52. domainLock: ['localhost', 'google.ru']
  53. };
  54. assert.deepEqual(getNormalizedOptions(optionsPreset), expectedOptionsPreset);
  55. });
  56. it('should normalize options preset: selfDefendingRule', () => {
  57. optionsPreset = {
  58. ...DEFAULT_PRESET,
  59. selfDefending: true,
  60. compact: false
  61. };
  62. expectedOptionsPreset = {
  63. ...DEFAULT_PRESET,
  64. selfDefending: true,
  65. compact: true
  66. };
  67. assert.deepEqual(getNormalizedOptions(optionsPreset), expectedOptionsPreset);
  68. });
  69. it('should normalize options preset: sourceMapBaseUrlRule #1', () => {
  70. optionsPreset = {
  71. ...DEFAULT_PRESET,
  72. sourceMapBaseUrl: 'http://localhost:9000',
  73. };
  74. expectedOptionsPreset = {
  75. ...DEFAULT_PRESET,
  76. sourceMapBaseUrl: ''
  77. };
  78. assert.deepEqual(getNormalizedOptions(optionsPreset), expectedOptionsPreset);
  79. });
  80. it('should normalize options preset: sourceMapBaseUrlRule #2', () => {
  81. optionsPreset = {
  82. ...DEFAULT_PRESET,
  83. sourceMapBaseUrl: 'http://localhost:9000',
  84. sourceMapFileName: '/outputSourceMapName.map'
  85. };
  86. expectedOptionsPreset = {
  87. ...DEFAULT_PRESET,
  88. sourceMapBaseUrl: 'http://localhost:9000/',
  89. sourceMapFileName: 'outputSourceMapName.js.map'
  90. };
  91. assert.deepEqual(getNormalizedOptions(optionsPreset), expectedOptionsPreset);
  92. });
  93. it('should normalize options preset: sourceMapFileNameRule', () => {
  94. optionsPreset = {
  95. ...DEFAULT_PRESET,
  96. sourceMapBaseUrl: 'http://localhost:9000',
  97. sourceMapFileName: '//outputSourceMapName'
  98. };
  99. expectedOptionsPreset = {
  100. ...DEFAULT_PRESET,
  101. sourceMapBaseUrl: 'http://localhost:9000/',
  102. sourceMapFileName: 'outputSourceMapName.js.map'
  103. };
  104. assert.deepEqual(getNormalizedOptions(optionsPreset), expectedOptionsPreset);
  105. });
  106. it('should normalize options preset: stringArrayRule', () => {
  107. optionsPreset = {
  108. ...DEFAULT_PRESET,
  109. stringArray: false,
  110. stringArrayEncoding: 'rc4',
  111. stringArrayThreshold: 0.5,
  112. rotateStringArray: true
  113. };
  114. expectedOptionsPreset = {
  115. ...DEFAULT_PRESET,
  116. stringArray: false,
  117. stringArrayEncoding: false,
  118. stringArrayThreshold: 0,
  119. rotateStringArray: false
  120. };
  121. assert.deepEqual(getNormalizedOptions(optionsPreset), expectedOptionsPreset);
  122. });
  123. it('should normalize options preset: stringArrayEncodingRule', () => {
  124. optionsPreset = {
  125. ...DEFAULT_PRESET,
  126. stringArrayEncoding: true
  127. };
  128. expectedOptionsPreset = {
  129. ...DEFAULT_PRESET,
  130. stringArrayEncoding: 'base64'
  131. };
  132. assert.deepEqual(getNormalizedOptions(optionsPreset), expectedOptionsPreset);
  133. });
  134. it('should normalize options preset: stringArrayThresholdRule', () => {
  135. optionsPreset = {
  136. ...DEFAULT_PRESET,
  137. rotateStringArray: true,
  138. stringArray: true,
  139. stringArrayThreshold: 0
  140. };
  141. expectedOptionsPreset = {
  142. ...DEFAULT_PRESET,
  143. rotateStringArray: false,
  144. stringArray: false,
  145. stringArrayThreshold: 0
  146. };
  147. assert.deepEqual(getNormalizedOptions(optionsPreset), expectedOptionsPreset);
  148. });
  149. });
  150. });