OptionsNormalizer.spec.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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('selfDefendingRule', () => {
  149. before(() => {
  150. optionsPreset = getNormalizedOptions({
  151. ...DEFAULT_PRESET,
  152. selfDefending: true,
  153. compact: false
  154. });
  155. expectedOptionsPreset = {
  156. ...DEFAULT_PRESET,
  157. selfDefending: true,
  158. compact: true
  159. };
  160. });
  161. it('should normalize options preset', () => {
  162. assert.deepEqual(optionsPreset, expectedOptionsPreset);
  163. });
  164. });
  165. describe('sourceMapBaseUrlRule', () => {
  166. describe('variant #1: only source map base url', () => {
  167. before(() => {
  168. optionsPreset = getNormalizedOptions({
  169. ...DEFAULT_PRESET,
  170. sourceMapBaseUrl: 'http://localhost:9000',
  171. });
  172. expectedOptionsPreset = {
  173. ...DEFAULT_PRESET,
  174. sourceMapBaseUrl: ''
  175. };
  176. });
  177. it('should normalize options preset', () => {
  178. assert.deepEqual(optionsPreset, expectedOptionsPreset);
  179. });
  180. });
  181. describe('variant #2: source map base url with source map file name', () => {
  182. before(() => {
  183. optionsPreset = getNormalizedOptions({
  184. ...DEFAULT_PRESET,
  185. sourceMapBaseUrl: 'http://localhost:9000',
  186. sourceMapFileName: '/outputSourceMapName.map'
  187. });
  188. expectedOptionsPreset = {
  189. ...DEFAULT_PRESET,
  190. sourceMapBaseUrl: 'http://localhost:9000/',
  191. sourceMapFileName: 'outputSourceMapName.js.map'
  192. };
  193. });
  194. it('should normalize options preset', () => {
  195. assert.deepEqual(optionsPreset, expectedOptionsPreset);
  196. });
  197. });
  198. });
  199. describe('sourceMapFileNameRule', () => {
  200. before(() => {
  201. optionsPreset = getNormalizedOptions({
  202. ...DEFAULT_PRESET,
  203. sourceMapBaseUrl: 'http://localhost:9000',
  204. sourceMapFileName: '//outputSourceMapName'
  205. });
  206. expectedOptionsPreset = {
  207. ...DEFAULT_PRESET,
  208. sourceMapBaseUrl: 'http://localhost:9000/',
  209. sourceMapFileName: 'outputSourceMapName.js.map'
  210. };
  211. });
  212. it('should normalize options preset', () => {
  213. assert.deepEqual(optionsPreset, expectedOptionsPreset);
  214. });
  215. });
  216. describe('stringArrayRule', () => {
  217. before(() => {
  218. optionsPreset = getNormalizedOptions({
  219. ...DEFAULT_PRESET,
  220. stringArray: false,
  221. stringArrayEncoding: StringArrayEncoding.Rc4,
  222. stringArrayThreshold: 0.5,
  223. rotateStringArray: true
  224. });
  225. expectedOptionsPreset = {
  226. ...DEFAULT_PRESET,
  227. stringArray: false,
  228. stringArrayEncoding: false,
  229. stringArrayThreshold: 0,
  230. rotateStringArray: false
  231. };
  232. });
  233. it('should normalize options preset', () => {
  234. assert.deepEqual(optionsPreset, expectedOptionsPreset);
  235. });
  236. });
  237. describe('stringArrayEncodingRule', () => {
  238. before(() => {
  239. optionsPreset = getNormalizedOptions({
  240. ...DEFAULT_PRESET,
  241. stringArrayEncoding: true
  242. });
  243. expectedOptionsPreset = {
  244. ...DEFAULT_PRESET,
  245. stringArrayEncoding: StringArrayEncoding.Base64
  246. };
  247. });
  248. it('should normalize options preset', () => {
  249. assert.deepEqual(optionsPreset, expectedOptionsPreset);
  250. });
  251. });
  252. describe('stringArrayThresholdRule', () => {
  253. before(() => {
  254. optionsPreset = getNormalizedOptions({
  255. ...DEFAULT_PRESET,
  256. rotateStringArray: true,
  257. stringArray: true,
  258. stringArrayThreshold: 0
  259. });
  260. expectedOptionsPreset = {
  261. ...DEFAULT_PRESET,
  262. rotateStringArray: false,
  263. stringArray: false,
  264. stringArrayThreshold: 0
  265. };
  266. });
  267. it('should normalize options preset', () => {
  268. assert.deepEqual(optionsPreset, expectedOptionsPreset);
  269. });
  270. });
  271. });
  272. });