OptionsNormalizer.spec.ts 11 KB

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