OptionsNormalizer.spec.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. import 'reflect-metadata';
  2. import { ServiceIdentifiers } from '../../../src/container/ServiceIdentifiers';
  3. import { assert } from 'chai';
  4. import { TInputOptions } from '../../../src/types/options/TInputOptions';
  5. import { IInversifyContainerFacade } from '../../../src/interfaces/container/IInversifyContainerFacade';
  6. import { IOptions } from '../../../src/interfaces/options/IOptions';
  7. import { IOptionsNormalizer } from '../../../src/interfaces/options/IOptionsNormalizer';
  8. import { StringArrayEncoding } from '../../../src/enums/StringArrayEncoding';
  9. import { DEFAULT_PRESET } from '../../../src/options/presets/Default';
  10. import { InversifyContainerFacade } from '../../../src/container/InversifyContainerFacade';
  11. /**
  12. * @param optionsPreset
  13. * @returns {IOptions}
  14. */
  15. function getNormalizedOptions (optionsPreset: TInputOptions): TInputOptions {
  16. const inversifyContainerFacade: IInversifyContainerFacade = new InversifyContainerFacade();
  17. inversifyContainerFacade.load('', optionsPreset);
  18. const options: IOptions = inversifyContainerFacade
  19. .get<IOptions>(ServiceIdentifiers.IOptions);
  20. const optionsNormalizer: IOptionsNormalizer = inversifyContainerFacade
  21. .get<IOptionsNormalizer>(ServiceIdentifiers.IOptionsNormalizer);
  22. return optionsNormalizer.normalize(options);
  23. }
  24. describe('OptionsNormalizer', () => {
  25. describe('normalize (options: IObfuscatorOptions): IObfuscatorOptions', () => {
  26. let optionsPreset: TInputOptions,
  27. expectedOptionsPreset: TInputOptions;
  28. describe('controlFlowFlatteningThresholdRule', () => {
  29. before(() => {
  30. optionsPreset = getNormalizedOptions({
  31. ...DEFAULT_PRESET,
  32. controlFlowFlattening: true,
  33. controlFlowFlatteningThreshold: 0
  34. });
  35. expectedOptionsPreset = {
  36. ...DEFAULT_PRESET,
  37. controlFlowFlattening: false,
  38. controlFlowFlatteningThreshold: 0
  39. };
  40. });
  41. it('should normalize options preset', () => {
  42. assert.deepEqual(optionsPreset, expectedOptionsPreset);
  43. });
  44. });
  45. describe('deadCodeInjectionRule', () => {
  46. before(() => {
  47. optionsPreset = getNormalizedOptions({
  48. ...DEFAULT_PRESET,
  49. deadCodeInjection: true,
  50. deadCodeInjectionThreshold: 0.4,
  51. stringArray: false,
  52. stringArrayThreshold: 0
  53. });
  54. expectedOptionsPreset = {
  55. ...DEFAULT_PRESET,
  56. deadCodeInjection: true,
  57. deadCodeInjectionThreshold: 0.4,
  58. stringArray: true,
  59. stringArrayThreshold: 0.75
  60. };
  61. });
  62. it('should normalize options preset', () => {
  63. assert.deepEqual(optionsPreset, expectedOptionsPreset);
  64. });
  65. });
  66. describe('deadCodeInjectionRule', () => {
  67. describe('`stringArrayThreshold` option is empty', () => {
  68. before(() => {
  69. optionsPreset = getNormalizedOptions({
  70. ...DEFAULT_PRESET,
  71. deadCodeInjection: true,
  72. deadCodeInjectionThreshold: 0.4,
  73. stringArray: false,
  74. stringArrayThreshold: 0
  75. });
  76. expectedOptionsPreset = {
  77. ...DEFAULT_PRESET,
  78. deadCodeInjection: true,
  79. deadCodeInjectionThreshold: 0.4,
  80. stringArray: true,
  81. stringArrayThreshold: 0.75
  82. };
  83. });
  84. it('should normalize options preset', () => {
  85. assert.deepEqual(optionsPreset, expectedOptionsPreset);
  86. });
  87. });
  88. describe('`stringArrayThreshold` option is not empty', () => {
  89. before(() => {
  90. optionsPreset = getNormalizedOptions({
  91. ...DEFAULT_PRESET,
  92. deadCodeInjection: true,
  93. deadCodeInjectionThreshold: 0.4,
  94. stringArray: false,
  95. stringArrayThreshold: 0.5
  96. });
  97. expectedOptionsPreset = {
  98. ...DEFAULT_PRESET,
  99. deadCodeInjection: true,
  100. deadCodeInjectionThreshold: 0.4,
  101. stringArray: true,
  102. stringArrayThreshold: 0.5
  103. };
  104. });
  105. it('should normalize options preset', () => {
  106. assert.deepEqual(optionsPreset, expectedOptionsPreset);
  107. });
  108. });
  109. });
  110. describe('deadCodeInjectionThresholdRule', () => {
  111. before(() => {
  112. optionsPreset = getNormalizedOptions({
  113. ...DEFAULT_PRESET,
  114. deadCodeInjection: true,
  115. deadCodeInjectionThreshold: 0
  116. });
  117. expectedOptionsPreset = {
  118. ...DEFAULT_PRESET,
  119. deadCodeInjection: false,
  120. deadCodeInjectionThreshold: 0
  121. };
  122. });
  123. it('should normalize options preset', () => {
  124. assert.deepEqual(optionsPreset, expectedOptionsPreset);
  125. });
  126. });
  127. describe('domainLockRule', () => {
  128. before(() => {
  129. optionsPreset = getNormalizedOptions({
  130. ...DEFAULT_PRESET,
  131. domainLock: [
  132. '//localhost:9000',
  133. 'https://google.ru/abc?cde=fgh'
  134. ]
  135. });
  136. expectedOptionsPreset = {
  137. ...DEFAULT_PRESET,
  138. domainLock: [
  139. 'localhost',
  140. 'google.ru'
  141. ]
  142. };
  143. });
  144. it('should normalize options preset', () => {
  145. assert.deepEqual(optionsPreset, expectedOptionsPreset);
  146. });
  147. });
  148. describe('identifiersPrefixRule', () => {
  149. describe('variant #1: string option value', () => {
  150. before(() => {
  151. optionsPreset = getNormalizedOptions({
  152. ...DEFAULT_PRESET,
  153. identifiersPrefix: 'foo',
  154. });
  155. expectedOptionsPreset = {
  156. ...DEFAULT_PRESET,
  157. identifiersPrefix: 'foo',
  158. };
  159. });
  160. it('should normalize options preset', () => {
  161. assert.deepEqual(optionsPreset, expectedOptionsPreset);
  162. });
  163. });
  164. describe('variant #2: empty string option value', () => {
  165. before(() => {
  166. optionsPreset = getNormalizedOptions({
  167. ...DEFAULT_PRESET,
  168. identifiersPrefix: '',
  169. });
  170. expectedOptionsPreset = {
  171. ...DEFAULT_PRESET,
  172. identifiersPrefix: false,
  173. };
  174. });
  175. it('should normalize options preset', () => {
  176. assert.deepEqual(optionsPreset, expectedOptionsPreset);
  177. });
  178. });
  179. describe('variant #3: option value is `true`', () => {
  180. before(() => {
  181. optionsPreset = getNormalizedOptions({
  182. ...DEFAULT_PRESET,
  183. identifiersPrefix: true,
  184. });
  185. expectedOptionsPreset = {
  186. ...DEFAULT_PRESET,
  187. identifiersPrefix: true,
  188. };
  189. });
  190. it('should normalize options preset', () => {
  191. assert.deepEqual(optionsPreset, expectedOptionsPreset);
  192. });
  193. });
  194. describe('variant #4: option value is `false`', () => {
  195. before(() => {
  196. optionsPreset = getNormalizedOptions({
  197. ...DEFAULT_PRESET,
  198. identifiersPrefix: false,
  199. });
  200. expectedOptionsPreset = {
  201. ...DEFAULT_PRESET,
  202. identifiersPrefix: false,
  203. };
  204. });
  205. it('should normalize options preset', () => {
  206. assert.deepEqual(optionsPreset, expectedOptionsPreset);
  207. });
  208. });
  209. describe('variant #5: option value is object', () => {
  210. before(() => {
  211. optionsPreset = getNormalizedOptions({
  212. ...DEFAULT_PRESET,
  213. identifiersPrefix: <any>{},
  214. });
  215. expectedOptionsPreset = {
  216. ...DEFAULT_PRESET,
  217. identifiersPrefix: false,
  218. };
  219. });
  220. it('should normalize options preset', () => {
  221. assert.deepEqual(optionsPreset, expectedOptionsPreset);
  222. });
  223. });
  224. describe('variant #6: option value is function', () => {
  225. before(() => {
  226. optionsPreset = getNormalizedOptions({
  227. ...DEFAULT_PRESET,
  228. identifiersPrefix: <any>{},
  229. });
  230. expectedOptionsPreset = {
  231. ...DEFAULT_PRESET,
  232. identifiersPrefix: false,
  233. };
  234. });
  235. it('should normalize options preset', () => {
  236. assert.deepEqual(optionsPreset, expectedOptionsPreset);
  237. });
  238. });
  239. describe('variant #7: option value is number', () => {
  240. before(() => {
  241. optionsPreset = getNormalizedOptions({
  242. ...DEFAULT_PRESET,
  243. identifiersPrefix: <any>123,
  244. });
  245. expectedOptionsPreset = {
  246. ...DEFAULT_PRESET,
  247. identifiersPrefix: '123',
  248. };
  249. });
  250. it('should normalize options preset', () => {
  251. assert.deepEqual(optionsPreset, expectedOptionsPreset);
  252. });
  253. });
  254. });
  255. describe('selfDefendingRule', () => {
  256. before(() => {
  257. optionsPreset = getNormalizedOptions({
  258. ...DEFAULT_PRESET,
  259. selfDefending: true,
  260. compact: false
  261. });
  262. expectedOptionsPreset = {
  263. ...DEFAULT_PRESET,
  264. selfDefending: true,
  265. compact: true
  266. };
  267. });
  268. it('should normalize options preset', () => {
  269. assert.deepEqual(optionsPreset, expectedOptionsPreset);
  270. });
  271. });
  272. describe('sourceMapBaseUrlRule', () => {
  273. describe('variant #1: only source map base url', () => {
  274. before(() => {
  275. optionsPreset = getNormalizedOptions({
  276. ...DEFAULT_PRESET,
  277. sourceMapBaseUrl: 'http://localhost:9000',
  278. });
  279. expectedOptionsPreset = {
  280. ...DEFAULT_PRESET,
  281. sourceMapBaseUrl: ''
  282. };
  283. });
  284. it('should normalize options preset', () => {
  285. assert.deepEqual(optionsPreset, expectedOptionsPreset);
  286. });
  287. });
  288. describe('variant #2: source map base url with source map file name', () => {
  289. before(() => {
  290. optionsPreset = getNormalizedOptions({
  291. ...DEFAULT_PRESET,
  292. sourceMapBaseUrl: 'http://localhost:9000',
  293. sourceMapFileName: '/outputSourceMapName.map'
  294. });
  295. expectedOptionsPreset = {
  296. ...DEFAULT_PRESET,
  297. sourceMapBaseUrl: 'http://localhost:9000/',
  298. sourceMapFileName: 'outputSourceMapName.js.map'
  299. };
  300. });
  301. it('should normalize options preset', () => {
  302. assert.deepEqual(optionsPreset, expectedOptionsPreset);
  303. });
  304. });
  305. });
  306. describe('sourceMapFileNameRule', () => {
  307. before(() => {
  308. optionsPreset = getNormalizedOptions({
  309. ...DEFAULT_PRESET,
  310. sourceMapBaseUrl: 'http://localhost:9000',
  311. sourceMapFileName: '//outputSourceMapName'
  312. });
  313. expectedOptionsPreset = {
  314. ...DEFAULT_PRESET,
  315. sourceMapBaseUrl: 'http://localhost:9000/',
  316. sourceMapFileName: 'outputSourceMapName.js.map'
  317. };
  318. });
  319. it('should normalize options preset', () => {
  320. assert.deepEqual(optionsPreset, expectedOptionsPreset);
  321. });
  322. });
  323. describe('stringArrayRule', () => {
  324. before(() => {
  325. optionsPreset = getNormalizedOptions({
  326. ...DEFAULT_PRESET,
  327. stringArray: false,
  328. stringArrayEncoding: StringArrayEncoding.Rc4,
  329. stringArrayThreshold: 0.5,
  330. rotateStringArray: true
  331. });
  332. expectedOptionsPreset = {
  333. ...DEFAULT_PRESET,
  334. stringArray: false,
  335. stringArrayEncoding: false,
  336. stringArrayThreshold: 0,
  337. rotateStringArray: false
  338. };
  339. });
  340. it('should normalize options preset', () => {
  341. assert.deepEqual(optionsPreset, expectedOptionsPreset);
  342. });
  343. });
  344. describe('stringArrayEncodingRule', () => {
  345. before(() => {
  346. optionsPreset = getNormalizedOptions({
  347. ...DEFAULT_PRESET,
  348. stringArrayEncoding: true
  349. });
  350. expectedOptionsPreset = {
  351. ...DEFAULT_PRESET,
  352. stringArrayEncoding: StringArrayEncoding.Base64
  353. };
  354. });
  355. it('should normalize options preset', () => {
  356. assert.deepEqual(optionsPreset, expectedOptionsPreset);
  357. });
  358. });
  359. describe('stringArrayThresholdRule', () => {
  360. before(() => {
  361. optionsPreset = getNormalizedOptions({
  362. ...DEFAULT_PRESET,
  363. rotateStringArray: true,
  364. stringArray: true,
  365. stringArrayThreshold: 0
  366. });
  367. expectedOptionsPreset = {
  368. ...DEFAULT_PRESET,
  369. rotateStringArray: false,
  370. stringArray: false,
  371. stringArrayThreshold: 0
  372. };
  373. });
  374. it('should normalize options preset', () => {
  375. assert.deepEqual(optionsPreset, expectedOptionsPreset);
  376. });
  377. });
  378. });
  379. });