Validation.spec.ts 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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: object with some empty identifier names cache', () => {
  68. let testFunc: () => string;
  69. beforeEach(() => {
  70. testFunc = () => JavaScriptObfuscator.obfuscate(
  71. '',
  72. {
  73. ...NO_ADDITIONAL_NODES_PRESET,
  74. identifierNamesCache: {
  75. globalIdentifiers: {
  76. foo: '_0x123456'
  77. }
  78. }
  79. }
  80. ).getObfuscatedCode();
  81. });
  82. it('should pass validation', () => {
  83. assert.doesNotThrow(testFunc);
  84. });
  85. });
  86. describe('Variant #5: empty object', () => {
  87. let testFunc: () => string;
  88. beforeEach(() => {
  89. testFunc = () => JavaScriptObfuscator.obfuscate(
  90. '',
  91. {
  92. ...NO_ADDITIONAL_NODES_PRESET,
  93. identifierNamesCache: {}
  94. }
  95. ).getObfuscatedCode();
  96. });
  97. it('should pass validation', () => {
  98. assert.doesNotThrow(testFunc);
  99. });
  100. });
  101. describe('Variant #5: `null` value', () => {
  102. let testFunc: () => string;
  103. beforeEach(() => {
  104. testFunc = () => JavaScriptObfuscator.obfuscate(
  105. '',
  106. {
  107. ...NO_ADDITIONAL_NODES_PRESET,
  108. identifierNamesCache: null
  109. }
  110. ).getObfuscatedCode();
  111. });
  112. it('should pass validation', () => {
  113. assert.doesNotThrow(testFunc);
  114. });
  115. });
  116. });
  117. describe('Variant #2: negative validation', () => {
  118. const expectedError: string = 'Passed value must be an identifier names cache object or `null` value';
  119. describe('Variant #1: string value', () => {
  120. let testFunc: () => string;
  121. beforeEach(() => {
  122. testFunc = () => JavaScriptObfuscator.obfuscate(
  123. '',
  124. {
  125. ...NO_ADDITIONAL_NODES_PRESET,
  126. identifierNamesCache: <any>'cache'
  127. }
  128. ).getObfuscatedCode();
  129. });
  130. it('should not pass validation', () => {
  131. assert.throws(testFunc, expectedError);
  132. });
  133. });
  134. describe('Variant #2: cache with number values inside single cache', () => {
  135. let testFunc: () => string;
  136. beforeEach(() => {
  137. testFunc = () => JavaScriptObfuscator.obfuscate(
  138. '',
  139. {
  140. ...NO_ADDITIONAL_NODES_PRESET,
  141. identifierNamesCache: {
  142. globalIdentifiers: {
  143. foo: <any>1,
  144. bar: <any>2,
  145. },
  146. propertyIdentifiers: {}
  147. }
  148. }
  149. ).getObfuscatedCode();
  150. });
  151. it('should not pass validation', () => {
  152. assert.throws(testFunc, expectedError);
  153. });
  154. });
  155. describe('Variant #3: cache with number values inside both caches', () => {
  156. let testFunc: () => string;
  157. beforeEach(() => {
  158. testFunc = () => JavaScriptObfuscator.obfuscate(
  159. '',
  160. {
  161. ...NO_ADDITIONAL_NODES_PRESET,
  162. identifierNamesCache: {
  163. globalIdentifiers: {
  164. foo: <any>1,
  165. bar: <any>2,
  166. },
  167. propertyIdentifiers: {
  168. baz: <any>3,
  169. bark: <any>4,
  170. }
  171. }
  172. }
  173. ).getObfuscatedCode();
  174. });
  175. it('should not pass validation', () => {
  176. assert.throws(testFunc, expectedError);
  177. });
  178. });
  179. describe('Variant #4: cache with mixed values', () => {
  180. let testFunc: () => string;
  181. beforeEach(() => {
  182. testFunc = () => JavaScriptObfuscator.obfuscate(
  183. '',
  184. {
  185. ...NO_ADDITIONAL_NODES_PRESET,
  186. identifierNamesCache: {
  187. globalIdentifiers: {
  188. foo: <any>1,
  189. bar: '_0x1234567',
  190. },
  191. propertyIdentifiers: {
  192. foo: '_0x123456'
  193. }
  194. }
  195. }
  196. ).getObfuscatedCode();
  197. });
  198. it('should not pass validation', () => {
  199. assert.throws(testFunc, expectedError);
  200. });
  201. });
  202. describe('Variant #4: cache with nullable dictionary fields', () => {
  203. let testFunc: () => string;
  204. beforeEach(() => {
  205. testFunc = () => JavaScriptObfuscator.obfuscate(
  206. '',
  207. {
  208. ...NO_ADDITIONAL_NODES_PRESET,
  209. identifierNamesCache: {
  210. globalIdentifiers: <any>null,
  211. propertyIdentifiers: <any>null
  212. }
  213. }
  214. ).getObfuscatedCode();
  215. });
  216. it('should not pass validation', () => {
  217. assert.throws(testFunc, expectedError);
  218. });
  219. });
  220. });
  221. });
  222. });