2
0

check-qjson.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508
  1. /*
  2. * Copyright IBM, Corp. 2009
  3. * Copyright (c) 2013, 2015 Red Hat Inc.
  4. *
  5. * Authors:
  6. * Anthony Liguori <aliguori@us.ibm.com>
  7. * Markus Armbruster <armbru@redhat.com>
  8. *
  9. * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
  10. * See the COPYING.LIB file in the top-level directory.
  11. *
  12. */
  13. #include "qemu/osdep.h"
  14. #include "qapi/error.h"
  15. #include "qapi/qmp/qbool.h"
  16. #include "qapi/qmp/qjson.h"
  17. #include "qapi/qmp/qlit.h"
  18. #include "qapi/qmp/qnull.h"
  19. #include "qapi/qmp/qnum.h"
  20. #include "qapi/qmp/qstring.h"
  21. #include "qemu/unicode.h"
  22. #include "qemu-common.h"
  23. static QString *from_json_str(const char *jstr, bool single, Error **errp)
  24. {
  25. char quote = single ? '\'' : '"';
  26. char *qjstr = g_strdup_printf("%c%s%c", quote, jstr, quote);
  27. QString *ret = qobject_to(QString, qobject_from_json(qjstr, errp));
  28. g_free(qjstr);
  29. return ret;
  30. }
  31. static char *to_json_str(QString *str)
  32. {
  33. QString *json = qobject_to_json(QOBJECT(str));
  34. char *jstr;
  35. if (!json) {
  36. return NULL;
  37. }
  38. /* peel off double quotes */
  39. jstr = g_strndup(qstring_get_str(json) + 1,
  40. qstring_get_length(json) - 2);
  41. qobject_unref(json);
  42. return jstr;
  43. }
  44. static void escaped_string(void)
  45. {
  46. struct {
  47. /* Content of JSON string to parse with qobject_from_json() */
  48. const char *json_in;
  49. /* Expected parse output; to unparse with qobject_to_json() */
  50. const char *utf8_out;
  51. int skip;
  52. } test_cases[] = {
  53. { "\\b\\f\\n\\r\\t\\\\\\\"", "\b\f\n\r\t\\\"" },
  54. { "\\/\\'", "/'", .skip = 1 },
  55. { "single byte utf-8 \\u0020", "single byte utf-8 ", .skip = 1 },
  56. { "double byte utf-8 \\u00A2", "double byte utf-8 \xc2\xa2" },
  57. { "triple byte utf-8 \\u20AC", "triple byte utf-8 \xe2\x82\xac" },
  58. { "quadruple byte utf-8 \\uD834\\uDD1E", /* U+1D11E */
  59. "quadruple byte utf-8 \xF0\x9D\x84\x9E" },
  60. { "\\", NULL },
  61. { "\\z", NULL },
  62. { "\\ux", NULL },
  63. { "\\u1x", NULL },
  64. { "\\u12x", NULL },
  65. { "\\u123x", NULL },
  66. { "\\u12345", "\341\210\2645" },
  67. { "\\u0000x", "\xC0\x80x" },
  68. { "unpaired leading surrogate \\uD800", NULL },
  69. { "unpaired leading surrogate \\uD800\\uCAFE", NULL },
  70. { "unpaired leading surrogate \\uD800\\uD801\\uDC02", NULL },
  71. { "unpaired trailing surrogate \\uDC00", NULL },
  72. { "backward surrogate pair \\uDC00\\uD800", NULL },
  73. { "noncharacter U+FDD0 \\uFDD0", NULL },
  74. { "noncharacter U+FDEF \\uFDEF", NULL },
  75. { "noncharacter U+1FFFE \\uD87F\\uDFFE", NULL },
  76. { "noncharacter U+10FFFF \\uDC3F\\uDFFF", NULL },
  77. {}
  78. };
  79. int i, j;
  80. QString *cstr;
  81. char *jstr;
  82. for (i = 0; test_cases[i].json_in; i++) {
  83. for (j = 0; j < 2; j++) {
  84. if (test_cases[i].utf8_out) {
  85. cstr = from_json_str(test_cases[i].json_in, j, &error_abort);
  86. g_assert_cmpstr(qstring_get_try_str(cstr),
  87. ==, test_cases[i].utf8_out);
  88. if (!test_cases[i].skip) {
  89. jstr = to_json_str(cstr);
  90. g_assert_cmpstr(jstr, ==, test_cases[i].json_in);
  91. g_free(jstr);
  92. }
  93. qobject_unref(cstr);
  94. } else {
  95. cstr = from_json_str(test_cases[i].json_in, j, NULL);
  96. g_assert(!cstr);
  97. }
  98. }
  99. }
  100. }
  101. static void string_with_quotes(void)
  102. {
  103. const char *test_cases[] = {
  104. "\"the bee's knees\"",
  105. "'double quote \"'",
  106. NULL
  107. };
  108. int i;
  109. QString *str;
  110. char *cstr;
  111. for (i = 0; test_cases[i]; i++) {
  112. str = qobject_to(QString,
  113. qobject_from_json(test_cases[i], &error_abort));
  114. g_assert(str);
  115. cstr = g_strndup(test_cases[i] + 1, strlen(test_cases[i]) - 2);
  116. g_assert_cmpstr(qstring_get_str(str), ==, cstr);
  117. g_free(cstr);
  118. qobject_unref(str);
  119. }
  120. }
  121. static void utf8_string(void)
  122. {
  123. /*
  124. * Most test cases are scraped from Markus Kuhn's UTF-8 decoder
  125. * capability and stress test at
  126. * http://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt
  127. */
  128. static const struct {
  129. /* Content of JSON string to parse with qobject_from_json() */
  130. const char *json_in;
  131. /* Expected parse output */
  132. const char *utf8_out;
  133. /* Expected unparse output, defaults to @json_in */
  134. const char *json_out;
  135. } test_cases[] = {
  136. /* 0 Control characters */
  137. {
  138. /*
  139. * Note: \x00 is impossible, other representations of
  140. * U+0000 are covered under 4.3
  141. */
  142. "\x01\x02\x03\x04\x05\x06\x07"
  143. "\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F"
  144. "\x10\x11\x12\x13\x14\x15\x16\x17"
  145. "\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F",
  146. NULL,
  147. "\\u0001\\u0002\\u0003\\u0004\\u0005\\u0006\\u0007"
  148. "\\b\\t\\n\\u000B\\f\\r\\u000E\\u000F"
  149. "\\u0010\\u0011\\u0012\\u0013\\u0014\\u0015\\u0016\\u0017"
  150. "\\u0018\\u0019\\u001A\\u001B\\u001C\\u001D\\u001E\\u001F",
  151. },
  152. /* 1 Some correct UTF-8 text */
  153. {
  154. /* a bit of German */
  155. "Falsches \xC3\x9C" "ben von Xylophonmusik qu\xC3\xA4lt"
  156. " jeden gr\xC3\xB6\xC3\x9F" "eren Zwerg.",
  157. "Falsches \xC3\x9C" "ben von Xylophonmusik qu\xC3\xA4lt"
  158. " jeden gr\xC3\xB6\xC3\x9F" "eren Zwerg.",
  159. "Falsches \\u00DCben von Xylophonmusik qu\\u00E4lt"
  160. " jeden gr\\u00F6\\u00DFeren Zwerg.",
  161. },
  162. {
  163. /* a bit of Greek */
  164. "\xCE\xBA\xE1\xBD\xB9\xCF\x83\xCE\xBC\xCE\xB5",
  165. "\xCE\xBA\xE1\xBD\xB9\xCF\x83\xCE\xBC\xCE\xB5",
  166. "\\u03BA\\u1F79\\u03C3\\u03BC\\u03B5",
  167. },
  168. /* '%' character when not interpolating */
  169. {
  170. "100%",
  171. "100%",
  172. },
  173. /* 2 Boundary condition test cases */
  174. /* 2.1 First possible sequence of a certain length */
  175. /*
  176. * 2.1.1 1 byte U+0020
  177. * Control characters are already covered by their own test
  178. * case under 0. Test the first 1 byte non-control character
  179. * here.
  180. */
  181. {
  182. " ",
  183. " ",
  184. },
  185. /* 2.1.2 2 bytes U+0080 */
  186. {
  187. "\xC2\x80",
  188. "\xC2\x80",
  189. "\\u0080",
  190. },
  191. /* 2.1.3 3 bytes U+0800 */
  192. {
  193. "\xE0\xA0\x80",
  194. "\xE0\xA0\x80",
  195. "\\u0800",
  196. },
  197. /* 2.1.4 4 bytes U+10000 */
  198. {
  199. "\xF0\x90\x80\x80",
  200. "\xF0\x90\x80\x80",
  201. "\\uD800\\uDC00",
  202. },
  203. /* 2.1.5 5 bytes U+200000 */
  204. {
  205. "\xF8\x88\x80\x80\x80",
  206. NULL,
  207. "\\uFFFD",
  208. },
  209. /* 2.1.6 6 bytes U+4000000 */
  210. {
  211. "\xFC\x84\x80\x80\x80\x80",
  212. NULL,
  213. "\\uFFFD",
  214. },
  215. /* 2.2 Last possible sequence of a certain length */
  216. /* 2.2.1 1 byte U+007F */
  217. {
  218. "\x7F",
  219. "\x7F",
  220. "\\u007F",
  221. },
  222. /* 2.2.2 2 bytes U+07FF */
  223. {
  224. "\xDF\xBF",
  225. "\xDF\xBF",
  226. "\\u07FF",
  227. },
  228. /*
  229. * 2.2.3 3 bytes U+FFFC
  230. * The last possible sequence is actually U+FFFF. But that's
  231. * a noncharacter, and already covered by its own test case
  232. * under 5.3. Same for U+FFFE. U+FFFD is the last character
  233. * in the BMP, and covered under 2.3. Because of U+FFFD's
  234. * special role as replacement character, it's worth testing
  235. * U+FFFC here.
  236. */
  237. {
  238. "\xEF\xBF\xBC",
  239. "\xEF\xBF\xBC",
  240. "\\uFFFC",
  241. },
  242. /* 2.2.4 4 bytes U+1FFFFF */
  243. {
  244. "\xF7\xBF\xBF\xBF",
  245. NULL,
  246. "\\uFFFD",
  247. },
  248. /* 2.2.5 5 bytes U+3FFFFFF */
  249. {
  250. "\xFB\xBF\xBF\xBF\xBF",
  251. NULL,
  252. "\\uFFFD",
  253. },
  254. /* 2.2.6 6 bytes U+7FFFFFFF */
  255. {
  256. "\xFD\xBF\xBF\xBF\xBF\xBF",
  257. NULL,
  258. "\\uFFFD",
  259. },
  260. /* 2.3 Other boundary conditions */
  261. {
  262. /* last one before surrogate range: U+D7FF */
  263. "\xED\x9F\xBF",
  264. "\xED\x9F\xBF",
  265. "\\uD7FF",
  266. },
  267. {
  268. /* first one after surrogate range: U+E000 */
  269. "\xEE\x80\x80",
  270. "\xEE\x80\x80",
  271. "\\uE000",
  272. },
  273. {
  274. /* last one in BMP: U+FFFD */
  275. "\xEF\xBF\xBD",
  276. "\xEF\xBF\xBD",
  277. "\\uFFFD",
  278. },
  279. {
  280. /* last one in last plane: U+10FFFD */
  281. "\xF4\x8F\xBF\xBD",
  282. "\xF4\x8F\xBF\xBD",
  283. "\\uDBFF\\uDFFD"
  284. },
  285. {
  286. /* first one beyond Unicode range: U+110000 */
  287. "\xF4\x90\x80\x80",
  288. NULL,
  289. "\\uFFFD",
  290. },
  291. /* 3 Malformed sequences */
  292. /* 3.1 Unexpected continuation bytes */
  293. /* 3.1.1 First continuation byte */
  294. {
  295. "\x80",
  296. NULL,
  297. "\\uFFFD",
  298. },
  299. /* 3.1.2 Last continuation byte */
  300. {
  301. "\xBF",
  302. NULL,
  303. "\\uFFFD",
  304. },
  305. /* 3.1.3 2 continuation bytes */
  306. {
  307. "\x80\xBF",
  308. NULL,
  309. "\\uFFFD\\uFFFD",
  310. },
  311. /* 3.1.4 3 continuation bytes */
  312. {
  313. "\x80\xBF\x80",
  314. NULL,
  315. "\\uFFFD\\uFFFD\\uFFFD",
  316. },
  317. /* 3.1.5 4 continuation bytes */
  318. {
  319. "\x80\xBF\x80\xBF",
  320. NULL,
  321. "\\uFFFD\\uFFFD\\uFFFD\\uFFFD",
  322. },
  323. /* 3.1.6 5 continuation bytes */
  324. {
  325. "\x80\xBF\x80\xBF\x80",
  326. NULL,
  327. "\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD",
  328. },
  329. /* 3.1.7 6 continuation bytes */
  330. {
  331. "\x80\xBF\x80\xBF\x80\xBF",
  332. NULL,
  333. "\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD",
  334. },
  335. /* 3.1.8 7 continuation bytes */
  336. {
  337. "\x80\xBF\x80\xBF\x80\xBF\x80",
  338. NULL,
  339. "\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD",
  340. },
  341. /* 3.1.9 Sequence of all 64 possible continuation bytes */
  342. {
  343. "\x80\x81\x82\x83\x84\x85\x86\x87"
  344. "\x88\x89\x8A\x8B\x8C\x8D\x8E\x8F"
  345. "\x90\x91\x92\x93\x94\x95\x96\x97"
  346. "\x98\x99\x9A\x9B\x9C\x9D\x9E\x9F"
  347. "\xA0\xA1\xA2\xA3\xA4\xA5\xA6\xA7"
  348. "\xA8\xA9\xAA\xAB\xAC\xAD\xAE\xAF"
  349. "\xB0\xB1\xB2\xB3\xB4\xB5\xB6\xB7"
  350. "\xB8\xB9\xBA\xBB\xBC\xBD\xBE\xBF",
  351. NULL,
  352. "\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD"
  353. "\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD"
  354. "\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD"
  355. "\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD"
  356. "\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD"
  357. "\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD"
  358. "\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD"
  359. "\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD",
  360. },
  361. /* 3.2 Lonely start characters */
  362. /* 3.2.1 All 32 first bytes of 2-byte sequences, followed by space */
  363. {
  364. "\xC0 \xC1 \xC2 \xC3 \xC4 \xC5 \xC6 \xC7 "
  365. "\xC8 \xC9 \xCA \xCB \xCC \xCD \xCE \xCF "
  366. "\xD0 \xD1 \xD2 \xD3 \xD4 \xD5 \xD6 \xD7 "
  367. "\xD8 \xD9 \xDA \xDB \xDC \xDD \xDE \xDF ",
  368. NULL,
  369. "\\uFFFD \\uFFFD \\uFFFD \\uFFFD \\uFFFD \\uFFFD \\uFFFD \\uFFFD "
  370. "\\uFFFD \\uFFFD \\uFFFD \\uFFFD \\uFFFD \\uFFFD \\uFFFD \\uFFFD "
  371. "\\uFFFD \\uFFFD \\uFFFD \\uFFFD \\uFFFD \\uFFFD \\uFFFD \\uFFFD "
  372. "\\uFFFD \\uFFFD \\uFFFD \\uFFFD \\uFFFD \\uFFFD \\uFFFD \\uFFFD ",
  373. },
  374. /* 3.2.2 All 16 first bytes of 3-byte sequences, followed by space */
  375. {
  376. "\xE0 \xE1 \xE2 \xE3 \xE4 \xE5 \xE6 \xE7 "
  377. "\xE8 \xE9 \xEA \xEB \xEC \xED \xEE \xEF ",
  378. NULL,
  379. "\\uFFFD \\uFFFD \\uFFFD \\uFFFD \\uFFFD \\uFFFD \\uFFFD \\uFFFD "
  380. "\\uFFFD \\uFFFD \\uFFFD \\uFFFD \\uFFFD \\uFFFD \\uFFFD \\uFFFD ",
  381. },
  382. /* 3.2.3 All 8 first bytes of 4-byte sequences, followed by space */
  383. {
  384. "\xF0 \xF1 \xF2 \xF3 \xF4 \xF5 \xF6 \xF7 ",
  385. NULL,
  386. "\\uFFFD \\uFFFD \\uFFFD \\uFFFD \\uFFFD \\uFFFD \\uFFFD \\uFFFD ",
  387. },
  388. /* 3.2.4 All 4 first bytes of 5-byte sequences, followed by space */
  389. {
  390. "\xF8 \xF9 \xFA \xFB ",
  391. NULL,
  392. "\\uFFFD \\uFFFD \\uFFFD \\uFFFD ",
  393. },
  394. /* 3.2.5 All 2 first bytes of 6-byte sequences, followed by space */
  395. {
  396. "\xFC \xFD ",
  397. NULL,
  398. "\\uFFFD \\uFFFD ",
  399. },
  400. /* 3.3 Sequences with last continuation byte missing */
  401. /* 3.3.1 2-byte sequence with last byte missing (U+0000) */
  402. {
  403. "\xC0",
  404. NULL,
  405. "\\uFFFD",
  406. },
  407. /* 3.3.2 3-byte sequence with last byte missing (U+0000) */
  408. {
  409. "\xE0\x80",
  410. NULL,
  411. "\\uFFFD",
  412. },
  413. /* 3.3.3 4-byte sequence with last byte missing (U+0000) */
  414. {
  415. "\xF0\x80\x80",
  416. NULL,
  417. "\\uFFFD",
  418. },
  419. /* 3.3.4 5-byte sequence with last byte missing (U+0000) */
  420. {
  421. "\xF8\x80\x80\x80",
  422. NULL,
  423. "\\uFFFD",
  424. },
  425. /* 3.3.5 6-byte sequence with last byte missing (U+0000) */
  426. {
  427. "\xFC\x80\x80\x80\x80",
  428. NULL,
  429. "\\uFFFD",
  430. },
  431. /* 3.3.6 2-byte sequence with last byte missing (U+07FF) */
  432. {
  433. "\xDF",
  434. NULL,
  435. "\\uFFFD",
  436. },
  437. /* 3.3.7 3-byte sequence with last byte missing (U+FFFF) */
  438. {
  439. "\xEF\xBF",
  440. NULL,
  441. "\\uFFFD",
  442. },
  443. /* 3.3.8 4-byte sequence with last byte missing (U+1FFFFF) */
  444. {
  445. "\xF7\xBF\xBF",
  446. NULL,
  447. "\\uFFFD",
  448. },
  449. /* 3.3.9 5-byte sequence with last byte missing (U+3FFFFFF) */
  450. {
  451. "\xFB\xBF\xBF\xBF",
  452. NULL,
  453. "\\uFFFD",
  454. },
  455. /* 3.3.10 6-byte sequence with last byte missing (U+7FFFFFFF) */
  456. {
  457. "\xFD\xBF\xBF\xBF\xBF",
  458. NULL,
  459. "\\uFFFD",
  460. },
  461. /* 3.4 Concatenation of incomplete sequences */
  462. {
  463. "\xC0\xE0\x80\xF0\x80\x80\xF8\x80\x80\x80\xFC\x80\x80\x80\x80"
  464. "\xDF\xEF\xBF\xF7\xBF\xBF\xFB\xBF\xBF\xBF\xFD\xBF\xBF\xBF\xBF",
  465. NULL,
  466. "\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD"
  467. "\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD",
  468. },
  469. /* 3.5 Impossible bytes */
  470. {
  471. "\xFE",
  472. NULL,
  473. "\\uFFFD",
  474. },
  475. {
  476. "\xFF",
  477. NULL,
  478. "\\uFFFD",
  479. },
  480. {
  481. "\xFE\xFE\xFF\xFF",
  482. NULL,
  483. "\\uFFFD\\uFFFD\\uFFFD\\uFFFD",
  484. },
  485. /* 4 Overlong sequences */
  486. /* 4.1 Overlong '/' */
  487. {
  488. "\xC0\xAF",
  489. NULL,
  490. "\\uFFFD",
  491. },
  492. {
  493. "\xE0\x80\xAF",
  494. NULL,
  495. "\\uFFFD",
  496. },
  497. {
  498. "\xF0\x80\x80\xAF",
  499. NULL,
  500. "\\uFFFD",
  501. },
  502. {
  503. "\xF8\x80\x80\x80\xAF",
  504. NULL,
  505. "\\uFFFD",
  506. },
  507. {
  508. "\xFC\x80\x80\x80\x80\xAF",
  509. NULL,
  510. "\\uFFFD",
  511. },
  512. /*
  513. * 4.2 Maximum overlong sequences
  514. * Highest Unicode value that is still resulting in an
  515. * overlong sequence if represented with the given number of
  516. * bytes. This is a boundary test for safe UTF-8 decoders.
  517. */
  518. {
  519. /* \U+007F */
  520. "\xC1\xBF",
  521. NULL,
  522. "\\uFFFD",
  523. },
  524. {
  525. /* \U+07FF */
  526. "\xE0\x9F\xBF",
  527. NULL,
  528. "\\uFFFD",
  529. },
  530. {
  531. /*
  532. * \U+FFFC
  533. * The actual maximum would be U+FFFF, but that's a
  534. * noncharacter. Testing U+FFFC seems more useful. See
  535. * also 2.2.3
  536. */
  537. "\xF0\x8F\xBF\xBC",
  538. NULL,
  539. "\\uFFFD",
  540. },
  541. {
  542. /* \U+1FFFFF */
  543. "\xF8\x87\xBF\xBF\xBF",
  544. NULL,
  545. "\\uFFFD",
  546. },
  547. {
  548. /* \U+3FFFFFF */
  549. "\xFC\x83\xBF\xBF\xBF\xBF",
  550. NULL,
  551. "\\uFFFD",
  552. },
  553. /* 4.3 Overlong representation of the NUL character */
  554. {
  555. /* \U+0000 */
  556. "\xC0\x80",
  557. "\xC0\x80",
  558. "\\u0000",
  559. },
  560. {
  561. /* \U+0000 */
  562. "\xE0\x80\x80",
  563. NULL,
  564. "\\uFFFD",
  565. },
  566. {
  567. /* \U+0000 */
  568. "\xF0\x80\x80\x80",
  569. NULL,
  570. "\\uFFFD",
  571. },
  572. {
  573. /* \U+0000 */
  574. "\xF8\x80\x80\x80\x80",
  575. NULL,
  576. "\\uFFFD",
  577. },
  578. {
  579. /* \U+0000 */
  580. "\xFC\x80\x80\x80\x80\x80",
  581. NULL,
  582. "\\uFFFD",
  583. },
  584. /* 5 Illegal code positions */
  585. /* 5.1 Single UTF-16 surrogates */
  586. {
  587. /* \U+D800 */
  588. "\xED\xA0\x80",
  589. NULL,
  590. "\\uFFFD",
  591. },
  592. {
  593. /* \U+DB7F */
  594. "\xED\xAD\xBF",
  595. NULL,
  596. "\\uFFFD",
  597. },
  598. {
  599. /* \U+DB80 */
  600. "\xED\xAE\x80",
  601. NULL,
  602. "\\uFFFD",
  603. },
  604. {
  605. /* \U+DBFF */
  606. "\xED\xAF\xBF",
  607. NULL,
  608. "\\uFFFD",
  609. },
  610. {
  611. /* \U+DC00 */
  612. "\xED\xB0\x80",
  613. NULL,
  614. "\\uFFFD",
  615. },
  616. {
  617. /* \U+DF80 */
  618. "\xED\xBE\x80",
  619. NULL,
  620. "\\uFFFD",
  621. },
  622. {
  623. /* \U+DFFF */
  624. "\xED\xBF\xBF",
  625. NULL,
  626. "\\uFFFD",
  627. },
  628. /* 5.2 Paired UTF-16 surrogates */
  629. {
  630. /* \U+D800\U+DC00 */
  631. "\xED\xA0\x80\xED\xB0\x80",
  632. NULL,
  633. "\\uFFFD\\uFFFD",
  634. },
  635. {
  636. /* \U+D800\U+DFFF */
  637. "\xED\xA0\x80\xED\xBF\xBF",
  638. NULL,
  639. "\\uFFFD\\uFFFD",
  640. },
  641. {
  642. /* \U+DB7F\U+DC00 */
  643. "\xED\xAD\xBF\xED\xB0\x80",
  644. NULL,
  645. "\\uFFFD\\uFFFD",
  646. },
  647. {
  648. /* \U+DB7F\U+DFFF */
  649. "\xED\xAD\xBF\xED\xBF\xBF",
  650. NULL,
  651. "\\uFFFD\\uFFFD",
  652. },
  653. {
  654. /* \U+DB80\U+DC00 */
  655. "\xED\xAE\x80\xED\xB0\x80",
  656. NULL,
  657. "\\uFFFD\\uFFFD",
  658. },
  659. {
  660. /* \U+DB80\U+DFFF */
  661. "\xED\xAE\x80\xED\xBF\xBF",
  662. NULL,
  663. "\\uFFFD\\uFFFD",
  664. },
  665. {
  666. /* \U+DBFF\U+DC00 */
  667. "\xED\xAF\xBF\xED\xB0\x80",
  668. NULL,
  669. "\\uFFFD\\uFFFD",
  670. },
  671. {
  672. /* \U+DBFF\U+DFFF */
  673. "\xED\xAF\xBF\xED\xBF\xBF",
  674. NULL,
  675. "\\uFFFD\\uFFFD",
  676. },
  677. /* 5.3 Other illegal code positions */
  678. /* BMP noncharacters */
  679. {
  680. /* \U+FFFE */
  681. "\xEF\xBF\xBE",
  682. NULL,
  683. "\\uFFFD",
  684. },
  685. {
  686. /* \U+FFFF */
  687. "\xEF\xBF\xBF",
  688. NULL,
  689. "\\uFFFD",
  690. },
  691. {
  692. /* U+FDD0 */
  693. "\xEF\xB7\x90",
  694. NULL,
  695. "\\uFFFD",
  696. },
  697. {
  698. /* U+FDEF */
  699. "\xEF\xB7\xAF",
  700. NULL,
  701. "\\uFFFD",
  702. },
  703. /* Plane 1 .. 16 noncharacters */
  704. {
  705. /* U+1FFFE U+1FFFF U+2FFFE U+2FFFF ... U+10FFFE U+10FFFF */
  706. "\xF0\x9F\xBF\xBE\xF0\x9F\xBF\xBF"
  707. "\xF0\xAF\xBF\xBE\xF0\xAF\xBF\xBF"
  708. "\xF0\xBF\xBF\xBE\xF0\xBF\xBF\xBF"
  709. "\xF1\x8F\xBF\xBE\xF1\x8F\xBF\xBF"
  710. "\xF1\x9F\xBF\xBE\xF1\x9F\xBF\xBF"
  711. "\xF1\xAF\xBF\xBE\xF1\xAF\xBF\xBF"
  712. "\xF1\xBF\xBF\xBE\xF1\xBF\xBF\xBF"
  713. "\xF2\x8F\xBF\xBE\xF2\x8F\xBF\xBF"
  714. "\xF2\x9F\xBF\xBE\xF2\x9F\xBF\xBF"
  715. "\xF2\xAF\xBF\xBE\xF2\xAF\xBF\xBF"
  716. "\xF2\xBF\xBF\xBE\xF2\xBF\xBF\xBF"
  717. "\xF3\x8F\xBF\xBE\xF3\x8F\xBF\xBF"
  718. "\xF3\x9F\xBF\xBE\xF3\x9F\xBF\xBF"
  719. "\xF3\xAF\xBF\xBE\xF3\xAF\xBF\xBF"
  720. "\xF3\xBF\xBF\xBE\xF3\xBF\xBF\xBF"
  721. "\xF4\x8F\xBF\xBE\xF4\x8F\xBF\xBF",
  722. NULL,
  723. "\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD"
  724. "\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD"
  725. "\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD"
  726. "\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD\\uFFFD",
  727. },
  728. {}
  729. };
  730. int i, j;
  731. QString *str;
  732. const char *json_in, *utf8_out, *utf8_in, *json_out, *tail;
  733. char *end, *in, *jstr;
  734. for (i = 0; test_cases[i].json_in; i++) {
  735. for (j = 0; j < 2; j++) {
  736. json_in = test_cases[i].json_in;
  737. utf8_out = test_cases[i].utf8_out;
  738. utf8_in = test_cases[i].utf8_out ?: test_cases[i].json_in;
  739. json_out = test_cases[i].json_out ?: test_cases[i].json_in;
  740. /* Parse @json_in, expect @utf8_out */
  741. if (utf8_out) {
  742. str = from_json_str(json_in, j, &error_abort);
  743. g_assert_cmpstr(qstring_get_try_str(str), ==, utf8_out);
  744. qobject_unref(str);
  745. } else {
  746. str = from_json_str(json_in, j, NULL);
  747. g_assert(!str);
  748. /*
  749. * Failure may be due to any sequence, but *all* sequences
  750. * are expected to fail. Test each one in isolation.
  751. */
  752. for (tail = json_in; *tail; tail = end) {
  753. mod_utf8_codepoint(tail, 6, &end);
  754. if (*end == ' ') {
  755. end++;
  756. }
  757. in = g_strndup(tail, end - tail);
  758. str = from_json_str(in, j, NULL);
  759. g_assert(!str);
  760. g_free(in);
  761. }
  762. }
  763. /* Unparse @utf8_in, expect @json_out */
  764. str = qstring_from_str(utf8_in);
  765. jstr = to_json_str(str);
  766. g_assert_cmpstr(jstr, ==, json_out);
  767. qobject_unref(str);
  768. g_free(jstr);
  769. /* Parse @json_out right back, unless it has replacements */
  770. if (!strstr(json_out, "\\uFFFD")) {
  771. str = from_json_str(json_out, j, &error_abort);
  772. g_assert_cmpstr(qstring_get_try_str(str), ==, utf8_in);
  773. qobject_unref(str);
  774. }
  775. }
  776. }
  777. }
  778. static void simple_number(void)
  779. {
  780. int i;
  781. struct {
  782. const char *encoded;
  783. int64_t decoded;
  784. int skip;
  785. } test_cases[] = {
  786. { "0", 0 },
  787. { "1234", 1234 },
  788. { "1", 1 },
  789. { "-32", -32 },
  790. { "-0", 0, .skip = 1 },
  791. { },
  792. };
  793. for (i = 0; test_cases[i].encoded; i++) {
  794. QNum *qnum;
  795. int64_t val;
  796. qnum = qobject_to(QNum,
  797. qobject_from_json(test_cases[i].encoded,
  798. &error_abort));
  799. g_assert(qnum);
  800. g_assert(qnum_get_try_int(qnum, &val));
  801. g_assert_cmpint(val, ==, test_cases[i].decoded);
  802. if (test_cases[i].skip == 0) {
  803. QString *str;
  804. str = qobject_to_json(QOBJECT(qnum));
  805. g_assert(strcmp(qstring_get_str(str), test_cases[i].encoded) == 0);
  806. qobject_unref(str);
  807. }
  808. qobject_unref(qnum);
  809. }
  810. }
  811. static void large_number(void)
  812. {
  813. const char *maxu64 = "18446744073709551615"; /* 2^64-1 */
  814. const char *gtu64 = "18446744073709551616"; /* 2^64 */
  815. const char *lti64 = "-9223372036854775809"; /* -2^63 - 1 */
  816. QNum *qnum;
  817. QString *str;
  818. uint64_t val;
  819. int64_t ival;
  820. qnum = qobject_to(QNum, qobject_from_json(maxu64, &error_abort));
  821. g_assert(qnum);
  822. g_assert_cmpuint(qnum_get_uint(qnum), ==, 18446744073709551615U);
  823. g_assert(!qnum_get_try_int(qnum, &ival));
  824. str = qobject_to_json(QOBJECT(qnum));
  825. g_assert_cmpstr(qstring_get_str(str), ==, maxu64);
  826. qobject_unref(str);
  827. qobject_unref(qnum);
  828. qnum = qobject_to(QNum, qobject_from_json(gtu64, &error_abort));
  829. g_assert(qnum);
  830. g_assert_cmpfloat(qnum_get_double(qnum), ==, 18446744073709552e3);
  831. g_assert(!qnum_get_try_uint(qnum, &val));
  832. g_assert(!qnum_get_try_int(qnum, &ival));
  833. str = qobject_to_json(QOBJECT(qnum));
  834. g_assert_cmpstr(qstring_get_str(str), ==, gtu64);
  835. qobject_unref(str);
  836. qobject_unref(qnum);
  837. qnum = qobject_to(QNum, qobject_from_json(lti64, &error_abort));
  838. g_assert(qnum);
  839. g_assert_cmpfloat(qnum_get_double(qnum), ==, -92233720368547758e2);
  840. g_assert(!qnum_get_try_uint(qnum, &val));
  841. g_assert(!qnum_get_try_int(qnum, &ival));
  842. str = qobject_to_json(QOBJECT(qnum));
  843. g_assert_cmpstr(qstring_get_str(str), ==, "-9223372036854775808");
  844. qobject_unref(str);
  845. qobject_unref(qnum);
  846. }
  847. static void float_number(void)
  848. {
  849. int i;
  850. struct {
  851. const char *encoded;
  852. double decoded;
  853. int skip;
  854. } test_cases[] = {
  855. { "32.43", 32.43 },
  856. { "0.222", 0.222 },
  857. { "-32.12313", -32.12313 },
  858. { "-32.20e-10", -32.20e-10, .skip = 1 },
  859. { },
  860. };
  861. for (i = 0; test_cases[i].encoded; i++) {
  862. QObject *obj;
  863. QNum *qnum;
  864. obj = qobject_from_json(test_cases[i].encoded, &error_abort);
  865. qnum = qobject_to(QNum, obj);
  866. g_assert(qnum);
  867. g_assert(qnum_get_double(qnum) == test_cases[i].decoded);
  868. if (test_cases[i].skip == 0) {
  869. QString *str;
  870. str = qobject_to_json(obj);
  871. g_assert(strcmp(qstring_get_str(str), test_cases[i].encoded) == 0);
  872. qobject_unref(str);
  873. }
  874. qobject_unref(qnum);
  875. }
  876. }
  877. static void keyword_literal(void)
  878. {
  879. QObject *obj;
  880. QBool *qbool;
  881. QNull *null;
  882. QString *str;
  883. obj = qobject_from_json("true", &error_abort);
  884. qbool = qobject_to(QBool, obj);
  885. g_assert(qbool);
  886. g_assert(qbool_get_bool(qbool) == true);
  887. str = qobject_to_json(obj);
  888. g_assert(strcmp(qstring_get_str(str), "true") == 0);
  889. qobject_unref(str);
  890. qobject_unref(qbool);
  891. obj = qobject_from_json("false", &error_abort);
  892. qbool = qobject_to(QBool, obj);
  893. g_assert(qbool);
  894. g_assert(qbool_get_bool(qbool) == false);
  895. str = qobject_to_json(obj);
  896. g_assert(strcmp(qstring_get_str(str), "false") == 0);
  897. qobject_unref(str);
  898. qobject_unref(qbool);
  899. obj = qobject_from_json("null", &error_abort);
  900. g_assert(obj != NULL);
  901. g_assert(qobject_type(obj) == QTYPE_QNULL);
  902. null = qnull();
  903. g_assert(QOBJECT(null) == obj);
  904. qobject_unref(obj);
  905. qobject_unref(null);
  906. }
  907. static void interpolation_valid(void)
  908. {
  909. long long value_lld = 0x123456789abcdefLL;
  910. int64_t value_d64 = value_lld;
  911. long value_ld = (long)value_lld;
  912. int value_d = (int)value_lld;
  913. unsigned long long value_llu = 0xfedcba9876543210ULL;
  914. uint64_t value_u64 = value_llu;
  915. unsigned long value_lu = (unsigned long)value_llu;
  916. unsigned value_u = (unsigned)value_llu;
  917. double value_f = 2.323423423;
  918. const char *value_s = "hello world";
  919. QObject *value_p = QOBJECT(qnull());
  920. QBool *qbool;
  921. QNum *qnum;
  922. QString *qstr;
  923. QObject *qobj;
  924. /* bool */
  925. qbool = qobject_to(QBool, qobject_from_jsonf_nofail("%i", false));
  926. g_assert(qbool);
  927. g_assert(qbool_get_bool(qbool) == false);
  928. qobject_unref(qbool);
  929. /* Test that non-zero values other than 1 get collapsed to true */
  930. qbool = qobject_to(QBool, qobject_from_jsonf_nofail("%i", 2));
  931. g_assert(qbool);
  932. g_assert(qbool_get_bool(qbool) == true);
  933. qobject_unref(qbool);
  934. /* number */
  935. qnum = qobject_to(QNum, qobject_from_jsonf_nofail("%d", value_d));
  936. g_assert_cmpint(qnum_get_int(qnum), ==, value_d);
  937. qobject_unref(qnum);
  938. qnum = qobject_to(QNum, qobject_from_jsonf_nofail("%ld", value_ld));
  939. g_assert_cmpint(qnum_get_int(qnum), ==, value_ld);
  940. qobject_unref(qnum);
  941. qnum = qobject_to(QNum, qobject_from_jsonf_nofail("%lld", value_lld));
  942. g_assert_cmpint(qnum_get_int(qnum), ==, value_lld);
  943. qobject_unref(qnum);
  944. qnum = qobject_to(QNum, qobject_from_jsonf_nofail("%" PRId64, value_d64));
  945. g_assert_cmpint(qnum_get_int(qnum), ==, value_lld);
  946. qobject_unref(qnum);
  947. qnum = qobject_to(QNum, qobject_from_jsonf_nofail("%u", value_u));
  948. g_assert_cmpuint(qnum_get_uint(qnum), ==, value_u);
  949. qobject_unref(qnum);
  950. qnum = qobject_to(QNum, qobject_from_jsonf_nofail("%lu", value_lu));
  951. g_assert_cmpuint(qnum_get_uint(qnum), ==, value_lu);
  952. qobject_unref(qnum);
  953. qnum = qobject_to(QNum, qobject_from_jsonf_nofail("%llu", value_llu));
  954. g_assert_cmpuint(qnum_get_uint(qnum), ==, value_llu);
  955. qobject_unref(qnum);
  956. qnum = qobject_to(QNum, qobject_from_jsonf_nofail("%" PRIu64, value_u64));
  957. g_assert_cmpuint(qnum_get_uint(qnum), ==, value_llu);
  958. qobject_unref(qnum);
  959. qnum = qobject_to(QNum, qobject_from_jsonf_nofail("%f", value_f));
  960. g_assert(qnum_get_double(qnum) == value_f);
  961. qobject_unref(qnum);
  962. /* string */
  963. qstr = qobject_to(QString,
  964. qobject_from_jsonf_nofail("%s", value_s));
  965. g_assert_cmpstr(qstring_get_try_str(qstr), ==, value_s);
  966. qobject_unref(qstr);
  967. /* object */
  968. qobj = qobject_from_jsonf_nofail("%p", value_p);
  969. g_assert(qobj == value_p);
  970. }
  971. static void interpolation_unknown(void)
  972. {
  973. if (g_test_subprocess()) {
  974. qobject_from_jsonf_nofail("%x", 666);
  975. }
  976. g_test_trap_subprocess(NULL, 0, 0);
  977. g_test_trap_assert_failed();
  978. g_test_trap_assert_stderr("*Unexpected error*"
  979. "invalid interpolation '%x'*");
  980. }
  981. static void interpolation_string(void)
  982. {
  983. if (g_test_subprocess()) {
  984. qobject_from_jsonf_nofail("['%s', %s]", "eins", "zwei");
  985. }
  986. g_test_trap_subprocess(NULL, 0, 0);
  987. g_test_trap_assert_failed();
  988. g_test_trap_assert_stderr("*Unexpected error*"
  989. "can't interpolate into string*");
  990. }
  991. static void simple_dict(void)
  992. {
  993. int i;
  994. struct {
  995. const char *encoded;
  996. QLitObject decoded;
  997. } test_cases[] = {
  998. {
  999. .encoded = "{\"foo\": 42, \"bar\": \"hello world\"}",
  1000. .decoded = QLIT_QDICT(((QLitDictEntry[]){
  1001. { "foo", QLIT_QNUM(42) },
  1002. { "bar", QLIT_QSTR("hello world") },
  1003. { }
  1004. })),
  1005. }, {
  1006. .encoded = "{}",
  1007. .decoded = QLIT_QDICT(((QLitDictEntry[]){
  1008. { }
  1009. })),
  1010. }, {
  1011. .encoded = "{\"foo\": 43}",
  1012. .decoded = QLIT_QDICT(((QLitDictEntry[]){
  1013. { "foo", QLIT_QNUM(43) },
  1014. { }
  1015. })),
  1016. },
  1017. { }
  1018. };
  1019. for (i = 0; test_cases[i].encoded; i++) {
  1020. QObject *obj;
  1021. QString *str;
  1022. obj = qobject_from_json(test_cases[i].encoded, &error_abort);
  1023. g_assert(qlit_equal_qobject(&test_cases[i].decoded, obj));
  1024. str = qobject_to_json(obj);
  1025. qobject_unref(obj);
  1026. obj = qobject_from_json(qstring_get_str(str), &error_abort);
  1027. g_assert(qlit_equal_qobject(&test_cases[i].decoded, obj));
  1028. qobject_unref(obj);
  1029. qobject_unref(str);
  1030. }
  1031. }
  1032. /*
  1033. * this generates json of the form:
  1034. * a(0,m) = [0, 1, ..., m-1]
  1035. * a(n,m) = {
  1036. * 'key0': a(0,m),
  1037. * 'key1': a(1,m),
  1038. * ...
  1039. * 'key(n-1)': a(n-1,m)
  1040. * }
  1041. */
  1042. static void gen_test_json(GString *gstr, int nest_level_max,
  1043. int elem_count)
  1044. {
  1045. int i;
  1046. g_assert(gstr);
  1047. if (nest_level_max == 0) {
  1048. g_string_append(gstr, "[");
  1049. for (i = 0; i < elem_count; i++) {
  1050. g_string_append_printf(gstr, "%d", i);
  1051. if (i < elem_count - 1) {
  1052. g_string_append_printf(gstr, ", ");
  1053. }
  1054. }
  1055. g_string_append(gstr, "]");
  1056. return;
  1057. }
  1058. g_string_append(gstr, "{");
  1059. for (i = 0; i < nest_level_max; i++) {
  1060. g_string_append_printf(gstr, "'key%d': ", i);
  1061. gen_test_json(gstr, i, elem_count);
  1062. if (i < nest_level_max - 1) {
  1063. g_string_append(gstr, ",");
  1064. }
  1065. }
  1066. g_string_append(gstr, "}");
  1067. }
  1068. static void large_dict(void)
  1069. {
  1070. GString *gstr = g_string_new("");
  1071. QObject *obj;
  1072. gen_test_json(gstr, 10, 100);
  1073. obj = qobject_from_json(gstr->str, &error_abort);
  1074. g_assert(obj != NULL);
  1075. qobject_unref(obj);
  1076. g_string_free(gstr, true);
  1077. }
  1078. static void simple_list(void)
  1079. {
  1080. int i;
  1081. struct {
  1082. const char *encoded;
  1083. QLitObject decoded;
  1084. } test_cases[] = {
  1085. {
  1086. .encoded = "[43,42]",
  1087. .decoded = QLIT_QLIST(((QLitObject[]){
  1088. QLIT_QNUM(43),
  1089. QLIT_QNUM(42),
  1090. { }
  1091. })),
  1092. },
  1093. {
  1094. .encoded = "[43]",
  1095. .decoded = QLIT_QLIST(((QLitObject[]){
  1096. QLIT_QNUM(43),
  1097. { }
  1098. })),
  1099. },
  1100. {
  1101. .encoded = "[]",
  1102. .decoded = QLIT_QLIST(((QLitObject[]){
  1103. { }
  1104. })),
  1105. },
  1106. {
  1107. .encoded = "[{}]",
  1108. .decoded = QLIT_QLIST(((QLitObject[]){
  1109. QLIT_QDICT(((QLitDictEntry[]){
  1110. {},
  1111. })),
  1112. {},
  1113. })),
  1114. },
  1115. { }
  1116. };
  1117. for (i = 0; test_cases[i].encoded; i++) {
  1118. QObject *obj;
  1119. QString *str;
  1120. obj = qobject_from_json(test_cases[i].encoded, &error_abort);
  1121. g_assert(qlit_equal_qobject(&test_cases[i].decoded, obj));
  1122. str = qobject_to_json(obj);
  1123. qobject_unref(obj);
  1124. obj = qobject_from_json(qstring_get_str(str), &error_abort);
  1125. g_assert(qlit_equal_qobject(&test_cases[i].decoded, obj));
  1126. qobject_unref(obj);
  1127. qobject_unref(str);
  1128. }
  1129. }
  1130. static void simple_whitespace(void)
  1131. {
  1132. int i;
  1133. struct {
  1134. const char *encoded;
  1135. QLitObject decoded;
  1136. } test_cases[] = {
  1137. {
  1138. .encoded = " [ 43 , 42 ]",
  1139. .decoded = QLIT_QLIST(((QLitObject[]){
  1140. QLIT_QNUM(43),
  1141. QLIT_QNUM(42),
  1142. { }
  1143. })),
  1144. },
  1145. {
  1146. .encoded = "\t[ 43 , { 'h' : 'b' },\r\n\t[ ], 42 ]\n",
  1147. .decoded = QLIT_QLIST(((QLitObject[]){
  1148. QLIT_QNUM(43),
  1149. QLIT_QDICT(((QLitDictEntry[]){
  1150. { "h", QLIT_QSTR("b") },
  1151. { }})),
  1152. QLIT_QLIST(((QLitObject[]){
  1153. { }})),
  1154. QLIT_QNUM(42),
  1155. { }
  1156. })),
  1157. },
  1158. {
  1159. .encoded = " [ 43 , { 'h' : 'b' , 'a' : 32 }, [ ], 42 ]",
  1160. .decoded = QLIT_QLIST(((QLitObject[]){
  1161. QLIT_QNUM(43),
  1162. QLIT_QDICT(((QLitDictEntry[]){
  1163. { "h", QLIT_QSTR("b") },
  1164. { "a", QLIT_QNUM(32) },
  1165. { }})),
  1166. QLIT_QLIST(((QLitObject[]){
  1167. { }})),
  1168. QLIT_QNUM(42),
  1169. { }
  1170. })),
  1171. },
  1172. { }
  1173. };
  1174. for (i = 0; test_cases[i].encoded; i++) {
  1175. QObject *obj;
  1176. QString *str;
  1177. obj = qobject_from_json(test_cases[i].encoded, &error_abort);
  1178. g_assert(qlit_equal_qobject(&test_cases[i].decoded, obj));
  1179. str = qobject_to_json(obj);
  1180. qobject_unref(obj);
  1181. obj = qobject_from_json(qstring_get_str(str), &error_abort);
  1182. g_assert(qlit_equal_qobject(&test_cases[i].decoded, obj));
  1183. qobject_unref(obj);
  1184. qobject_unref(str);
  1185. }
  1186. }
  1187. static void simple_interpolation(void)
  1188. {
  1189. QObject *embedded_obj;
  1190. QObject *obj;
  1191. QLitObject decoded = QLIT_QLIST(((QLitObject[]){
  1192. QLIT_QNUM(1),
  1193. QLIT_QSTR("100%"),
  1194. QLIT_QLIST(((QLitObject[]){
  1195. QLIT_QNUM(32),
  1196. QLIT_QNUM(42),
  1197. {}})),
  1198. {}}));
  1199. embedded_obj = qobject_from_json("[32, 42]", &error_abort);
  1200. g_assert(embedded_obj != NULL);
  1201. obj = qobject_from_jsonf_nofail("[%d, '100%%', %p]", 1, embedded_obj);
  1202. g_assert(qlit_equal_qobject(&decoded, obj));
  1203. qobject_unref(obj);
  1204. }
  1205. static void empty_input(void)
  1206. {
  1207. Error *err = NULL;
  1208. QObject *obj;
  1209. obj = qobject_from_json("", &err);
  1210. error_free_or_abort(&err);
  1211. g_assert(obj == NULL);
  1212. }
  1213. static void blank_input(void)
  1214. {
  1215. Error *err = NULL;
  1216. QObject *obj;
  1217. obj = qobject_from_json("\n ", &err);
  1218. error_free_or_abort(&err);
  1219. g_assert(obj == NULL);
  1220. }
  1221. static void junk_input(void)
  1222. {
  1223. /* Note: junk within strings is covered elsewhere */
  1224. Error *err = NULL;
  1225. QObject *obj;
  1226. obj = qobject_from_json("@", &err);
  1227. error_free_or_abort(&err);
  1228. g_assert(obj == NULL);
  1229. obj = qobject_from_json("{\x01", &err);
  1230. error_free_or_abort(&err);
  1231. g_assert(obj == NULL);
  1232. obj = qobject_from_json("[0\xFF]", &err);
  1233. error_free_or_abort(&err);
  1234. g_assert(obj == NULL);
  1235. obj = qobject_from_json("00", &err);
  1236. error_free_or_abort(&err);
  1237. g_assert(obj == NULL);
  1238. obj = qobject_from_json("[1e", &err);
  1239. error_free_or_abort(&err);
  1240. g_assert(obj == NULL);
  1241. obj = qobject_from_json("truer", &err);
  1242. error_free_or_abort(&err);
  1243. g_assert(obj == NULL);
  1244. }
  1245. static void unterminated_string(void)
  1246. {
  1247. Error *err = NULL;
  1248. QObject *obj = qobject_from_json("\"abc", &err);
  1249. error_free_or_abort(&err);
  1250. g_assert(obj == NULL);
  1251. }
  1252. static void unterminated_sq_string(void)
  1253. {
  1254. Error *err = NULL;
  1255. QObject *obj = qobject_from_json("'abc", &err);
  1256. error_free_or_abort(&err);
  1257. g_assert(obj == NULL);
  1258. }
  1259. static void unterminated_escape(void)
  1260. {
  1261. Error *err = NULL;
  1262. QObject *obj = qobject_from_json("\"abc\\\"", &err);
  1263. error_free_or_abort(&err);
  1264. g_assert(obj == NULL);
  1265. }
  1266. static void unterminated_array(void)
  1267. {
  1268. Error *err = NULL;
  1269. QObject *obj = qobject_from_json("[32", &err);
  1270. error_free_or_abort(&err);
  1271. g_assert(obj == NULL);
  1272. }
  1273. static void unterminated_array_comma(void)
  1274. {
  1275. Error *err = NULL;
  1276. QObject *obj = qobject_from_json("[32,", &err);
  1277. error_free_or_abort(&err);
  1278. g_assert(obj == NULL);
  1279. }
  1280. static void invalid_array_comma(void)
  1281. {
  1282. Error *err = NULL;
  1283. QObject *obj = qobject_from_json("[32,}", &err);
  1284. error_free_or_abort(&err);
  1285. g_assert(obj == NULL);
  1286. }
  1287. static void unterminated_dict(void)
  1288. {
  1289. Error *err = NULL;
  1290. QObject *obj = qobject_from_json("{'abc':32", &err);
  1291. error_free_or_abort(&err);
  1292. g_assert(obj == NULL);
  1293. }
  1294. static void unterminated_dict_comma(void)
  1295. {
  1296. Error *err = NULL;
  1297. QObject *obj = qobject_from_json("{'abc':32,", &err);
  1298. error_free_or_abort(&err);
  1299. g_assert(obj == NULL);
  1300. }
  1301. static void invalid_dict_comma(void)
  1302. {
  1303. Error *err = NULL;
  1304. QObject *obj = qobject_from_json("{'abc':32,}", &err);
  1305. error_free_or_abort(&err);
  1306. g_assert(obj == NULL);
  1307. }
  1308. static void unterminated_literal(void)
  1309. {
  1310. Error *err = NULL;
  1311. QObject *obj = qobject_from_json("nul", &err);
  1312. error_free_or_abort(&err);
  1313. g_assert(obj == NULL);
  1314. }
  1315. static char *make_nest(char *buf, size_t cnt)
  1316. {
  1317. memset(buf, '[', cnt - 1);
  1318. buf[cnt - 1] = '{';
  1319. buf[cnt] = '}';
  1320. memset(buf + cnt + 1, ']', cnt - 1);
  1321. buf[2 * cnt] = 0;
  1322. return buf;
  1323. }
  1324. static void limits_nesting(void)
  1325. {
  1326. Error *err = NULL;
  1327. enum { max_nesting = 1024 }; /* see qobject/json-streamer.c */
  1328. char buf[2 * (max_nesting + 1) + 1];
  1329. QObject *obj;
  1330. obj = qobject_from_json(make_nest(buf, max_nesting), &error_abort);
  1331. g_assert(obj != NULL);
  1332. qobject_unref(obj);
  1333. obj = qobject_from_json(make_nest(buf, max_nesting + 1), &err);
  1334. error_free_or_abort(&err);
  1335. g_assert(obj == NULL);
  1336. }
  1337. static void multiple_values(void)
  1338. {
  1339. Error *err = NULL;
  1340. QObject *obj;
  1341. obj = qobject_from_json("false true", &err);
  1342. error_free_or_abort(&err);
  1343. g_assert(obj == NULL);
  1344. obj = qobject_from_json("} true", &err);
  1345. error_free_or_abort(&err);
  1346. g_assert(obj == NULL);
  1347. }
  1348. int main(int argc, char **argv)
  1349. {
  1350. g_test_init(&argc, &argv, NULL);
  1351. g_test_add_func("/literals/string/escaped", escaped_string);
  1352. g_test_add_func("/literals/string/quotes", string_with_quotes);
  1353. g_test_add_func("/literals/string/utf8", utf8_string);
  1354. g_test_add_func("/literals/number/simple", simple_number);
  1355. g_test_add_func("/literals/number/large", large_number);
  1356. g_test_add_func("/literals/number/float", float_number);
  1357. g_test_add_func("/literals/keyword", keyword_literal);
  1358. g_test_add_func("/literals/interpolation/valid", interpolation_valid);
  1359. g_test_add_func("/literals/interpolation/unkown", interpolation_unknown);
  1360. g_test_add_func("/literals/interpolation/string", interpolation_string);
  1361. g_test_add_func("/dicts/simple_dict", simple_dict);
  1362. g_test_add_func("/dicts/large_dict", large_dict);
  1363. g_test_add_func("/lists/simple_list", simple_list);
  1364. g_test_add_func("/mixed/simple_whitespace", simple_whitespace);
  1365. g_test_add_func("/mixed/interpolation", simple_interpolation);
  1366. g_test_add_func("/errors/empty", empty_input);
  1367. g_test_add_func("/errors/blank", blank_input);
  1368. g_test_add_func("/errors/junk", junk_input);
  1369. g_test_add_func("/errors/unterminated/string", unterminated_string);
  1370. g_test_add_func("/errors/unterminated/escape", unterminated_escape);
  1371. g_test_add_func("/errors/unterminated/sq_string", unterminated_sq_string);
  1372. g_test_add_func("/errors/unterminated/array", unterminated_array);
  1373. g_test_add_func("/errors/unterminated/array_comma", unterminated_array_comma);
  1374. g_test_add_func("/errors/unterminated/dict", unterminated_dict);
  1375. g_test_add_func("/errors/unterminated/dict_comma", unterminated_dict_comma);
  1376. g_test_add_func("/errors/invalid_array_comma", invalid_array_comma);
  1377. g_test_add_func("/errors/invalid_dict_comma", invalid_dict_comma);
  1378. g_test_add_func("/errors/unterminated/literal", unterminated_literal);
  1379. g_test_add_func("/errors/limits/nesting", limits_nesting);
  1380. g_test_add_func("/errors/multiple_values", multiple_values);
  1381. return g_test_run();
  1382. }