Validation.spec.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import { assert } from 'chai';
  2. import { JavaScriptObfuscator } from '../../../../src/JavaScriptObfuscatorFacade';
  3. import { NO_ADDITIONAL_NODES_PRESET } from '../../../../src/options/presets/NoCustomNodes';
  4. describe('`identifierNamesCache` validation', () => {
  5. describe('IsPrimitiveDictionary', () => {
  6. describe('Variant #1: positive validation', () => {
  7. describe('Variant #1: object with existing identifier names cache', () => {
  8. let testFunc: () => string;
  9. beforeEach(() => {
  10. testFunc = () => JavaScriptObfuscator.obfuscate(
  11. '',
  12. {
  13. ...NO_ADDITIONAL_NODES_PRESET,
  14. identifierNamesCache: {
  15. foo: '_0x123456'
  16. }
  17. }
  18. ).getObfuscatedCode();
  19. });
  20. it('should pass validation', () => {
  21. assert.doesNotThrow(testFunc);
  22. });
  23. });
  24. describe('Variant #2: object with empty identifier names cache', () => {
  25. let testFunc: () => string;
  26. beforeEach(() => {
  27. testFunc = () => JavaScriptObfuscator.obfuscate(
  28. '',
  29. {
  30. ...NO_ADDITIONAL_NODES_PRESET,
  31. identifierNamesCache: {}
  32. }
  33. ).getObfuscatedCode();
  34. });
  35. it('should pass validation', () => {
  36. assert.doesNotThrow(testFunc);
  37. });
  38. });
  39. describe('Variant #3: `null` value', () => {
  40. let testFunc: () => string;
  41. beforeEach(() => {
  42. testFunc = () => JavaScriptObfuscator.obfuscate(
  43. '',
  44. {
  45. ...NO_ADDITIONAL_NODES_PRESET,
  46. identifierNamesCache: null
  47. }
  48. ).getObfuscatedCode();
  49. });
  50. it('should pass validation', () => {
  51. assert.doesNotThrow(testFunc);
  52. });
  53. });
  54. });
  55. describe('Variant #2: negative validation', () => {
  56. const expectedError: string = 'Passed value must be a dictionary with `string` values or `null` value';
  57. describe('Variant #1: string value', () => {
  58. let testFunc: () => string;
  59. beforeEach(() => {
  60. testFunc = () => JavaScriptObfuscator.obfuscate(
  61. '',
  62. {
  63. ...NO_ADDITIONAL_NODES_PRESET,
  64. identifierNamesCache: <any>'cache'
  65. }
  66. ).getObfuscatedCode();
  67. });
  68. it('should not pass validation', () => {
  69. assert.throws(testFunc, expectedError);
  70. });
  71. });
  72. describe('Variant #2: object with number values', () => {
  73. let testFunc: () => string;
  74. beforeEach(() => {
  75. testFunc = () => JavaScriptObfuscator.obfuscate(
  76. '',
  77. {
  78. ...NO_ADDITIONAL_NODES_PRESET,
  79. identifierNamesCache: {
  80. foo: <any>1,
  81. bar: <any>2,
  82. }
  83. }
  84. ).getObfuscatedCode();
  85. });
  86. it('should not pass validation', () => {
  87. assert.throws(testFunc, expectedError);
  88. });
  89. });
  90. describe('Variant #3: object with mixed values', () => {
  91. let testFunc: () => string;
  92. beforeEach(() => {
  93. testFunc = () => JavaScriptObfuscator.obfuscate(
  94. '',
  95. {
  96. ...NO_ADDITIONAL_NODES_PRESET,
  97. identifierNamesCache: {
  98. foo: <any>1,
  99. bar: '_0x1234567',
  100. }
  101. }
  102. ).getObfuscatedCode();
  103. });
  104. it('should not pass validation', () => {
  105. assert.throws(testFunc, expectedError);
  106. });
  107. });
  108. });
  109. });
  110. });