OptionsNormalizer.spec.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import { IInputOptions } from '../../src/interfaces/options/IInputOptions';
  2. import { IOptions } from '../../src/interfaces/options/IOptions';
  3. import { DEFAULT_PRESET } from '../../src/preset-options/DefaultPreset';
  4. import { Options } from '../../src/options/Options';
  5. import { OptionsNormalizer } from '../../src/options/OptionsNormalizer';
  6. const assert: Chai.AssertStatic = require('chai').assert;
  7. /**
  8. * @param optionsPreset
  9. * @returns {IOptions}
  10. */
  11. function getNormalizedOptions (optionsPreset: IInputOptions): IInputOptions {
  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: IInputOptions,
  18. expectedOptionsPreset: IInputOptions;
  19. it('should normalize options preset: domainLockRule', () => {
  20. optionsPreset = Object.assign({}, DEFAULT_PRESET, {
  21. domainLock: ['//localhost:9000', 'https://google.ru/abc?cde=fgh']
  22. });
  23. expectedOptionsPreset = Object.assign({}, DEFAULT_PRESET, {
  24. domainLock: ['localhost', 'google.ru']
  25. });
  26. assert.deepEqual(getNormalizedOptions(optionsPreset), expectedOptionsPreset);
  27. });
  28. it('should normalize options preset: selfDefendingRule', () => {
  29. optionsPreset = Object.assign({}, DEFAULT_PRESET, {
  30. selfDefending: true,
  31. compact: false
  32. });
  33. expectedOptionsPreset = Object.assign({}, DEFAULT_PRESET, {
  34. selfDefending: true,
  35. compact: true
  36. });
  37. assert.deepEqual(getNormalizedOptions(optionsPreset), expectedOptionsPreset);
  38. });
  39. it('should normalize options preset: sourceMapBaseUrlRule #1', () => {
  40. optionsPreset = Object.assign({}, DEFAULT_PRESET, {
  41. sourceMapBaseUrl: 'http://localhost:9000',
  42. });
  43. expectedOptionsPreset = Object.assign({}, DEFAULT_PRESET, {
  44. sourceMapBaseUrl: '',
  45. });
  46. assert.deepEqual(getNormalizedOptions(optionsPreset), expectedOptionsPreset);
  47. });
  48. it('should normalize options preset: sourceMapBaseUrlRule #2', () => {
  49. optionsPreset = Object.assign({}, DEFAULT_PRESET, {
  50. sourceMapBaseUrl: 'http://localhost:9000',
  51. sourceMapFileName: '/outputSourceMapName.map'
  52. });
  53. expectedOptionsPreset = Object.assign({}, DEFAULT_PRESET, {
  54. sourceMapBaseUrl: 'http://localhost:9000/',
  55. sourceMapFileName: 'outputSourceMapName.js.map'
  56. });
  57. assert.deepEqual(getNormalizedOptions(optionsPreset), expectedOptionsPreset);
  58. });
  59. it('should normalize options preset: sourceMapFileNameRule', () => {
  60. optionsPreset = Object.assign({}, DEFAULT_PRESET, {
  61. sourceMapBaseUrl: 'http://localhost:9000',
  62. sourceMapFileName: '//outputSourceMapName',
  63. });
  64. expectedOptionsPreset = Object.assign({}, DEFAULT_PRESET, {
  65. sourceMapBaseUrl: 'http://localhost:9000/',
  66. sourceMapFileName: 'outputSourceMapName.js.map',
  67. });
  68. assert.deepEqual(getNormalizedOptions(optionsPreset), expectedOptionsPreset);
  69. });
  70. it('should normalize options preset: stringArrayRule', () => {
  71. optionsPreset = Object.assign({}, DEFAULT_PRESET, {
  72. stringArray: false,
  73. stringArrayEncoding: 'rc4',
  74. stringArrayThreshold: 0.5,
  75. rotateStringArray: true
  76. });
  77. expectedOptionsPreset = Object.assign({}, DEFAULT_PRESET, {
  78. stringArray: false,
  79. stringArrayEncoding: false,
  80. stringArrayThreshold: 0,
  81. rotateStringArray: false
  82. });
  83. assert.deepEqual(getNormalizedOptions(optionsPreset), expectedOptionsPreset);
  84. });
  85. it('should normalize options preset: stringArrayEncodingRule', () => {
  86. optionsPreset = Object.assign({}, DEFAULT_PRESET, {
  87. stringArrayEncoding: true
  88. });
  89. expectedOptionsPreset = Object.assign({}, DEFAULT_PRESET, {
  90. stringArrayEncoding: 'base64'
  91. });
  92. assert.deepEqual(getNormalizedOptions(optionsPreset), expectedOptionsPreset);
  93. });
  94. it('should normalize options preset: stringArrayThresholdRule', () => {
  95. optionsPreset = Object.assign({}, DEFAULT_PRESET, {
  96. rotateStringArray: true,
  97. stringArray: true,
  98. stringArrayThreshold: 0
  99. });
  100. expectedOptionsPreset = Object.assign({}, DEFAULT_PRESET, {
  101. rotateStringArray: false,
  102. stringArray: false,
  103. stringArrayThreshold: 0
  104. });
  105. assert.deepEqual(getNormalizedOptions(optionsPreset), expectedOptionsPreset);
  106. });
  107. });
  108. });