OptionsNormalizer.spec.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. describe('controlFlowFlatteningThresholdRule', () => {
  20. before(() => {
  21. optionsPreset = getNormalizedOptions({
  22. ...DEFAULT_PRESET,
  23. controlFlowFlattening: true,
  24. controlFlowFlatteningThreshold: 0
  25. });
  26. expectedOptionsPreset = {
  27. ...DEFAULT_PRESET,
  28. controlFlowFlattening: false,
  29. controlFlowFlatteningThreshold: 0
  30. };
  31. });
  32. it('should normalize options preset', () => {
  33. assert.deepEqual(optionsPreset, expectedOptionsPreset);
  34. });
  35. });
  36. describe('deadCodeInjectionRule', () => {
  37. before(() => {
  38. optionsPreset = getNormalizedOptions({
  39. ...DEFAULT_PRESET,
  40. deadCodeInjection: true,
  41. deadCodeInjectionThreshold: 0.4,
  42. stringArray: false,
  43. stringArrayThreshold: 0
  44. });
  45. expectedOptionsPreset = {
  46. ...DEFAULT_PRESET,
  47. deadCodeInjection: true,
  48. deadCodeInjectionThreshold: 0.4,
  49. stringArray: true,
  50. stringArrayThreshold: 0.75
  51. };
  52. });
  53. it('should normalize options preset', () => {
  54. assert.deepEqual(optionsPreset, expectedOptionsPreset);
  55. });
  56. });
  57. describe('deadCodeInjectionRule', () => {
  58. describe('`stringArrayThreshold` option is empty', () => {
  59. before(() => {
  60. optionsPreset = getNormalizedOptions({
  61. ...DEFAULT_PRESET,
  62. deadCodeInjection: true,
  63. deadCodeInjectionThreshold: 0.4,
  64. stringArray: false,
  65. stringArrayThreshold: 0
  66. });
  67. expectedOptionsPreset = {
  68. ...DEFAULT_PRESET,
  69. deadCodeInjection: true,
  70. deadCodeInjectionThreshold: 0.4,
  71. stringArray: true,
  72. stringArrayThreshold: 0.75
  73. };
  74. });
  75. it('should normalize options preset', () => {
  76. assert.deepEqual(optionsPreset, expectedOptionsPreset);
  77. });
  78. });
  79. describe('`stringArrayThreshold` option is not empty', () => {
  80. before(() => {
  81. optionsPreset = getNormalizedOptions({
  82. ...DEFAULT_PRESET,
  83. deadCodeInjection: true,
  84. deadCodeInjectionThreshold: 0.4,
  85. stringArray: false,
  86. stringArrayThreshold: 0.5
  87. });
  88. expectedOptionsPreset = {
  89. ...DEFAULT_PRESET,
  90. deadCodeInjection: true,
  91. deadCodeInjectionThreshold: 0.4,
  92. stringArray: true,
  93. stringArrayThreshold: 0.5
  94. };
  95. });
  96. it('should normalize options preset', () => {
  97. assert.deepEqual(optionsPreset, expectedOptionsPreset);
  98. });
  99. });
  100. });
  101. describe('deadCodeInjectionThresholdRule', () => {
  102. before(() => {
  103. optionsPreset = getNormalizedOptions({
  104. ...DEFAULT_PRESET,
  105. deadCodeInjection: true,
  106. deadCodeInjectionThreshold: 0
  107. });
  108. expectedOptionsPreset = {
  109. ...DEFAULT_PRESET,
  110. deadCodeInjection: false,
  111. deadCodeInjectionThreshold: 0
  112. };
  113. });
  114. it('should normalize options preset', () => {
  115. assert.deepEqual(optionsPreset, expectedOptionsPreset);
  116. });
  117. });
  118. describe('domainLockRule', () => {
  119. before(() => {
  120. optionsPreset = getNormalizedOptions({
  121. ...DEFAULT_PRESET,
  122. domainLock: [
  123. '//localhost:9000',
  124. 'https://google.ru/abc?cde=fgh'
  125. ]
  126. });
  127. expectedOptionsPreset = {
  128. ...DEFAULT_PRESET,
  129. domainLock: [
  130. 'localhost',
  131. 'google.ru'
  132. ]
  133. };
  134. });
  135. it('should normalize options preset', () => {
  136. assert.deepEqual(optionsPreset, expectedOptionsPreset);
  137. });
  138. });
  139. describe('selfDefendingRule', () => {
  140. before(() => {
  141. optionsPreset = getNormalizedOptions({
  142. ...DEFAULT_PRESET,
  143. selfDefending: true,
  144. compact: false
  145. });
  146. expectedOptionsPreset = {
  147. ...DEFAULT_PRESET,
  148. selfDefending: true,
  149. compact: true
  150. };
  151. });
  152. it('should normalize options preset', () => {
  153. assert.deepEqual(optionsPreset, expectedOptionsPreset);
  154. });
  155. });
  156. describe('sourceMapBaseUrlRule', () => {
  157. describe('variant #1: only source map base url', () => {
  158. before(() => {
  159. optionsPreset = getNormalizedOptions({
  160. ...DEFAULT_PRESET,
  161. sourceMapBaseUrl: 'http://localhost:9000',
  162. });
  163. expectedOptionsPreset = {
  164. ...DEFAULT_PRESET,
  165. sourceMapBaseUrl: ''
  166. };
  167. });
  168. it('should normalize options preset', () => {
  169. assert.deepEqual(optionsPreset, expectedOptionsPreset);
  170. });
  171. });
  172. describe('variant #2: source map base url with source map file name', () => {
  173. before(() => {
  174. optionsPreset = getNormalizedOptions({
  175. ...DEFAULT_PRESET,
  176. sourceMapBaseUrl: 'http://localhost:9000',
  177. sourceMapFileName: '/outputSourceMapName.map'
  178. });
  179. expectedOptionsPreset = {
  180. ...DEFAULT_PRESET,
  181. sourceMapBaseUrl: 'http://localhost:9000/',
  182. sourceMapFileName: 'outputSourceMapName.js.map'
  183. };
  184. });
  185. it('should normalize options preset', () => {
  186. assert.deepEqual(optionsPreset, expectedOptionsPreset);
  187. });
  188. });
  189. });
  190. describe('sourceMapFileNameRule', () => {
  191. before(() => {
  192. optionsPreset = getNormalizedOptions({
  193. ...DEFAULT_PRESET,
  194. sourceMapBaseUrl: 'http://localhost:9000',
  195. sourceMapFileName: '//outputSourceMapName'
  196. });
  197. expectedOptionsPreset = {
  198. ...DEFAULT_PRESET,
  199. sourceMapBaseUrl: 'http://localhost:9000/',
  200. sourceMapFileName: 'outputSourceMapName.js.map'
  201. };
  202. });
  203. it('should normalize options preset', () => {
  204. assert.deepEqual(optionsPreset, expectedOptionsPreset);
  205. });
  206. });
  207. describe('stringArrayRule', () => {
  208. before(() => {
  209. optionsPreset = getNormalizedOptions({
  210. ...DEFAULT_PRESET,
  211. stringArray: false,
  212. stringArrayEncoding: 'rc4',
  213. stringArrayThreshold: 0.5,
  214. rotateStringArray: true
  215. });
  216. expectedOptionsPreset = {
  217. ...DEFAULT_PRESET,
  218. stringArray: false,
  219. stringArrayEncoding: false,
  220. stringArrayThreshold: 0,
  221. rotateStringArray: false
  222. };
  223. });
  224. it('should normalize options preset', () => {
  225. assert.deepEqual(optionsPreset, expectedOptionsPreset);
  226. });
  227. });
  228. describe('stringArrayEncodingRule', () => {
  229. before(() => {
  230. optionsPreset = getNormalizedOptions({
  231. ...DEFAULT_PRESET,
  232. stringArrayEncoding: true
  233. });
  234. expectedOptionsPreset = {
  235. ...DEFAULT_PRESET,
  236. stringArrayEncoding: 'base64'
  237. };
  238. });
  239. it('should normalize options preset', () => {
  240. assert.deepEqual(optionsPreset, expectedOptionsPreset);
  241. });
  242. });
  243. describe('stringArrayThresholdRule', () => {
  244. before(() => {
  245. optionsPreset = getNormalizedOptions({
  246. ...DEFAULT_PRESET,
  247. rotateStringArray: true,
  248. stringArray: true,
  249. stringArrayThreshold: 0
  250. });
  251. expectedOptionsPreset = {
  252. ...DEFAULT_PRESET,
  253. rotateStringArray: false,
  254. stringArray: false,
  255. stringArrayThreshold: 0
  256. };
  257. });
  258. it('should normalize options preset', () => {
  259. assert.deepEqual(optionsPreset, expectedOptionsPreset);
  260. });
  261. });
  262. });
  263. });