Validation.spec.ts 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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('IsIdentifierNamesCache', () => {
  6. describe('Variant #1: positive validation', () => {
  7. describe('Variant #1: object with existing identifier names cached', () => {
  8. let testFunc: () => string;
  9. beforeEach(() => {
  10. testFunc = () => JavaScriptObfuscator.obfuscate(
  11. '',
  12. {
  13. ...NO_ADDITIONAL_NODES_PRESET,
  14. identifierNamesCache: {
  15. globalIdentifiers: {
  16. foo: '_0x123456'
  17. },
  18. propertyIdentifiers: {
  19. bar: '_0x654321'
  20. }
  21. }
  22. }
  23. ).getObfuscatedCode();
  24. });
  25. it('should pass validation', () => {
  26. assert.doesNotThrow(testFunc);
  27. });
  28. });
  29. describe('Variant #2: object with empty identifier names cache', () => {
  30. let testFunc: () => string;
  31. beforeEach(() => {
  32. testFunc = () => JavaScriptObfuscator.obfuscate(
  33. '',
  34. {
  35. ...NO_ADDITIONAL_NODES_PRESET,
  36. identifierNamesCache: {
  37. globalIdentifiers: {
  38. foo: '_0x123456'
  39. },
  40. propertyIdentifiers: {}
  41. }
  42. }
  43. ).getObfuscatedCode();
  44. });
  45. it('should pass validation', () => {
  46. assert.doesNotThrow(testFunc);
  47. });
  48. });
  49. describe('Variant #3: object with empty identifier names caches', () => {
  50. let testFunc: () => string;
  51. beforeEach(() => {
  52. testFunc = () => JavaScriptObfuscator.obfuscate(
  53. '',
  54. {
  55. ...NO_ADDITIONAL_NODES_PRESET,
  56. identifierNamesCache: {
  57. globalIdentifiers: {},
  58. propertyIdentifiers: {}
  59. }
  60. }
  61. ).getObfuscatedCode();
  62. });
  63. it('should pass validation', () => {
  64. assert.doesNotThrow(testFunc);
  65. });
  66. });
  67. describe('Variant #4: `null` value', () => {
  68. let testFunc: () => string;
  69. beforeEach(() => {
  70. testFunc = () => JavaScriptObfuscator.obfuscate(
  71. '',
  72. {
  73. ...NO_ADDITIONAL_NODES_PRESET,
  74. identifierNamesCache: null
  75. }
  76. ).getObfuscatedCode();
  77. });
  78. it('should pass validation', () => {
  79. assert.doesNotThrow(testFunc);
  80. });
  81. });
  82. });
  83. describe('Variant #2: negative validation', () => {
  84. const expectedError: string = 'Passed value must be an identifier names cache object or `null` value';
  85. describe('Variant #1: string value', () => {
  86. let testFunc: () => string;
  87. beforeEach(() => {
  88. testFunc = () => JavaScriptObfuscator.obfuscate(
  89. '',
  90. {
  91. ...NO_ADDITIONAL_NODES_PRESET,
  92. identifierNamesCache: <any>'cache'
  93. }
  94. ).getObfuscatedCode();
  95. });
  96. it('should not pass validation', () => {
  97. assert.throws(testFunc, expectedError);
  98. });
  99. });
  100. describe('Variant #2: cache with number values inside single cache', () => {
  101. let testFunc: () => string;
  102. beforeEach(() => {
  103. testFunc = () => JavaScriptObfuscator.obfuscate(
  104. '',
  105. {
  106. ...NO_ADDITIONAL_NODES_PRESET,
  107. identifierNamesCache: {
  108. globalIdentifiers: {
  109. foo: <any>1,
  110. bar: <any>2,
  111. },
  112. propertyIdentifiers: {}
  113. }
  114. }
  115. ).getObfuscatedCode();
  116. });
  117. it('should not pass validation', () => {
  118. assert.throws(testFunc, expectedError);
  119. });
  120. });
  121. describe('Variant #3: cache with number values inside both caches', () => {
  122. let testFunc: () => string;
  123. beforeEach(() => {
  124. testFunc = () => JavaScriptObfuscator.obfuscate(
  125. '',
  126. {
  127. ...NO_ADDITIONAL_NODES_PRESET,
  128. identifierNamesCache: {
  129. globalIdentifiers: {
  130. foo: <any>1,
  131. bar: <any>2,
  132. },
  133. propertyIdentifiers: {
  134. baz: <any>3,
  135. bark: <any>4,
  136. }
  137. }
  138. }
  139. ).getObfuscatedCode();
  140. });
  141. it('should not pass validation', () => {
  142. assert.throws(testFunc, expectedError);
  143. });
  144. });
  145. describe('Variant #4: cache with mixed values', () => {
  146. let testFunc: () => string;
  147. beforeEach(() => {
  148. testFunc = () => JavaScriptObfuscator.obfuscate(
  149. '',
  150. {
  151. ...NO_ADDITIONAL_NODES_PRESET,
  152. identifierNamesCache: {
  153. globalIdentifiers: {
  154. foo: <any>1,
  155. bar: '_0x1234567',
  156. },
  157. propertyIdentifiers: {
  158. foo: '_0x123456'
  159. }
  160. }
  161. }
  162. ).getObfuscatedCode();
  163. });
  164. it('should not pass validation', () => {
  165. assert.throws(testFunc, expectedError);
  166. });
  167. });
  168. describe('Variant #4: cache with nullable dictionary fields', () => {
  169. let testFunc: () => string;
  170. beforeEach(() => {
  171. testFunc = () => JavaScriptObfuscator.obfuscate(
  172. '',
  173. {
  174. ...NO_ADDITIONAL_NODES_PRESET,
  175. identifierNamesCache: {
  176. globalIdentifiers: <any>null,
  177. propertyIdentifiers: <any>null
  178. }
  179. }
  180. ).getObfuscatedCode();
  181. });
  182. it('should not pass validation', () => {
  183. assert.throws(testFunc, expectedError);
  184. });
  185. });
  186. });
  187. });
  188. });