tst-sax.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /* iksemel (XML parser for Jabber)
  2. ** Copyright (C) 2000-2003 Gurer Ozen <madcat@e-kolay.net>
  3. ** This code is free software; you can redistribute it and/or
  4. ** modify it under the terms of GNU Lesser General Public License.
  5. */
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <stdarg.h>
  10. #include "iksemel.h"
  11. struct element_s {
  12. struct element_s *next;
  13. enum ikstype type;
  14. enum ikstagtype tag;
  15. char *name;
  16. int nr_atts;
  17. char *atts[10];
  18. char *vals[10];
  19. char *cdata;
  20. int len;
  21. };
  22. struct {
  23. char *doc;
  24. int len;
  25. struct element_s *elements;
  26. struct element_s *last_element;
  27. struct element_s *cur;
  28. int nr_tests;
  29. int nr_cur;
  30. int blocksize;
  31. } tester;
  32. void
  33. document (char *xml)
  34. {
  35. if (tester.elements) {
  36. struct element_s *tmp;
  37. for (; tester.elements; tester.elements = tmp) {
  38. tmp = tester.elements->next;
  39. free (tester.elements);
  40. }
  41. }
  42. tester.doc = xml;
  43. tester.len = strlen (xml);
  44. tester.elements = NULL;
  45. tester.last_element = NULL;
  46. tester.nr_tests++;
  47. }
  48. void
  49. element (enum ikstype type, ...)
  50. {
  51. struct element_s *el;
  52. va_list ap;
  53. char *tmp;
  54. el = malloc (sizeof (struct element_s));
  55. memset (el, 0, sizeof (struct element_s));
  56. el->type = type;
  57. va_start (ap, type);
  58. switch (type) {
  59. case IKS_TAG:
  60. el->tag = va_arg (ap, int);
  61. el->name = va_arg (ap, char*);
  62. if (IKS_CLOSE == el->tag) break;
  63. while (1) {
  64. tmp = va_arg (ap, char*);
  65. if (tmp) {
  66. el->atts[el->nr_atts] = tmp;
  67. el->vals[el->nr_atts] = va_arg (ap, char*);
  68. el->nr_atts++;
  69. } else {
  70. break;
  71. }
  72. }
  73. break;
  74. case IKS_CDATA:
  75. tmp = va_arg (ap, char*);
  76. el->cdata = tmp;
  77. el->len = strlen (tmp);
  78. break;
  79. case IKS_NONE:
  80. case IKS_ATTRIBUTE:
  81. puts ("invalid element() call");
  82. exit (1);
  83. }
  84. va_end (ap);
  85. if (NULL == tester.elements) tester.elements = el;
  86. if (tester.last_element) tester.last_element->next = el;
  87. tester.last_element = el;
  88. }
  89. #define PRINT_TEST printf ("Sax test %d, blocksize %d, element %d:\n", tester.nr_tests, tester.blocksize, tester.nr_cur)
  90. #define NEXT_ELEM { tester.cur = tester.cur->next; tester.nr_cur++; }
  91. void
  92. debug_tag (enum ikstagtype type, char *name, char **atts)
  93. {
  94. int i;
  95. PRINT_TEST;
  96. if (tester.cur && tester.cur->type == IKS_TAG) {
  97. switch (tester.cur->tag) {
  98. case IKS_OPEN:
  99. printf (" Expecting tag <%s>\n", tester.cur->name);
  100. break;
  101. case IKS_CLOSE:
  102. printf (" Expecting tag </%s>\n", tester.cur->name);
  103. break;
  104. case IKS_SINGLE:
  105. printf (" Expecting tag <%s/>\n", tester.cur->name);
  106. break;
  107. }
  108. for (i = 0; i < tester.cur->nr_atts; i++) {
  109. printf (" %s='%s'\n", tester.cur->atts[i], tester.cur->vals[i]);
  110. }
  111. } else {
  112. printf (" Not expecting a tag here.\n");
  113. }
  114. switch (type) {
  115. case IKS_OPEN:
  116. printf (" Got tag <%s>\n", name);
  117. break;
  118. case IKS_CLOSE:
  119. printf (" Got tag </%s>\n", name);
  120. break;
  121. case IKS_SINGLE:
  122. printf (" Got tag <%s/>\n", name);
  123. break;
  124. }
  125. i = 0;
  126. while (atts && atts[i]) {
  127. printf (" %s='%s'\n", atts[i], atts[i+1]);
  128. i += 2;
  129. }
  130. }
  131. #define TAG_FAIL { debug_tag (type,name,atts); exit (1); }
  132. int
  133. tagHook (void *udata, char *name, char **atts, int type)
  134. {
  135. int nr, i, flag;
  136. if (!tester.cur) TAG_FAIL;
  137. if (tester.cur->type != IKS_TAG) TAG_FAIL;
  138. if (tester.cur->tag != type) TAG_FAIL;
  139. if (iks_strcmp (tester.cur->name, name) != 0) TAG_FAIL;
  140. if (!atts && tester.cur->nr_atts > 0) TAG_FAIL;
  141. if (atts && tester.cur->nr_atts == 0) TAG_FAIL;
  142. nr = tester.cur->nr_atts;
  143. while (nr) {
  144. flag = 0;
  145. for (i = 0;atts&&atts[i]; i+= 2) {
  146. if (iks_strcmp (atts[i], tester.cur->atts[nr-1]) == 0 && iks_strcmp (atts[i+1], tester.cur->vals[nr-1]) == 0) {
  147. flag = 1;
  148. break;
  149. }
  150. }
  151. if (flag == 0) TAG_FAIL;
  152. nr--;
  153. }
  154. NEXT_ELEM;
  155. return IKS_OK;
  156. }
  157. void
  158. debug_cdata (char *data, size_t len, int pos)
  159. {
  160. int i;
  161. PRINT_TEST;
  162. if (tester.cur && tester.cur->type == IKS_CDATA)
  163. printf (" Expecting cdata [%s]\n", tester.cur->cdata);
  164. else
  165. printf (" Not expecting cdata here\n");
  166. printf (" Got cdata [");
  167. for (i = 0; i < len; i++) putchar (data[i]);
  168. printf ("] at the pos %d.\n", pos);
  169. }
  170. #define CDATA_FAIL { debug_cdata (data, len, pos); exit (1); }
  171. int
  172. cdataHook (void *udata, char *data, size_t len)
  173. {
  174. static int pos = 0;
  175. if (!tester.cur) CDATA_FAIL;
  176. if (tester.cur->type != IKS_CDATA) CDATA_FAIL;
  177. if (iks_strncmp (tester.cur->cdata + pos, data, len) != 0) CDATA_FAIL;
  178. pos += len;
  179. if (pos > tester.cur->len) CDATA_FAIL;
  180. if (pos == tester.cur->len) {
  181. pos = 0;
  182. NEXT_ELEM;
  183. }
  184. return IKS_OK;
  185. }
  186. void
  187. test_size (int blocksize)
  188. {
  189. enum ikserror err;
  190. iksparser *prs;
  191. int i, len;
  192. tester.cur = tester.elements;
  193. tester.nr_cur = 1;
  194. tester.blocksize = blocksize;
  195. len = tester.len;
  196. prs = iks_sax_new (NULL, tagHook, cdataHook);
  197. i = 0;
  198. if (0 == blocksize) blocksize = len;
  199. while (i < len) {
  200. if (i + blocksize > len) blocksize = len - i;
  201. err = iks_parse (prs, tester.doc + i, blocksize, 0);
  202. switch (err) {
  203. case IKS_OK:
  204. break;
  205. case IKS_NOMEM:
  206. exit (1);
  207. case IKS_BADXML:
  208. PRINT_TEST;
  209. printf ("Invalid xml at byte %ld in\n[%s]\n", iks_nr_bytes (prs), tester.doc);
  210. exit (1);
  211. case IKS_HOOK:
  212. exit (1);
  213. }
  214. i += blocksize;
  215. }
  216. if (tester.cur) exit (1);
  217. iks_parser_delete (prs);
  218. }
  219. void
  220. test (void)
  221. {
  222. int i;
  223. for (i = 0; i < tester.len; i++) {
  224. test_size (i);
  225. }
  226. }
  227. void
  228. test_bad (int badbyte)
  229. {
  230. iksparser *p;
  231. enum ikserror err;
  232. p = iks_sax_new (NULL, NULL, NULL);
  233. err = iks_parse (p, tester.doc, tester.len, 1);
  234. switch (err) {
  235. case IKS_OK:
  236. break;
  237. case IKS_NOMEM:
  238. exit (1);
  239. case IKS_BADXML:
  240. if (iks_nr_bytes (p) == badbyte) return;
  241. break;
  242. case IKS_HOOK:
  243. exit (1);
  244. }
  245. printf ("Sax test %d:\n", tester.nr_tests);
  246. printf ("Expected bad byte %d, got %ld in\n[%s]\n", badbyte, iks_nr_bytes (p), tester.doc);
  247. exit (1);
  248. }
  249. int
  250. main (int argc, char *argv[])
  251. {
  252. document ("<lonely/>");
  253. element (IKS_TAG, IKS_SINGLE, "lonely", 0);
  254. test ();
  255. document ("<?xml version='1.0'?><parent><child/><child/>child</parent>");
  256. element (IKS_TAG, IKS_OPEN, "parent", 0);
  257. element (IKS_TAG, IKS_SINGLE, "child", 0);
  258. element (IKS_TAG, IKS_SINGLE, "child", 0);
  259. element (IKS_CDATA, "child");
  260. element (IKS_TAG, IKS_CLOSE, "parent");
  261. test ();
  262. document ("<mytag abc='123' id=\"XC72\"></mytag>");
  263. element (IKS_TAG, IKS_OPEN, "mytag", "abc", "123", "id", "XC72", 0);
  264. element (IKS_TAG, IKS_CLOSE, "mytag");
  265. test ();
  266. document ("<body>I&apos;m fixing parser&amp;tester for &quot;&lt;&quot; and &quot;&gt;&quot; chars.</body>");
  267. element (IKS_TAG, IKS_OPEN, "body", 0);
  268. element (IKS_CDATA, "I'm fixing parser&tester for \"<\" and \">\" chars.");
  269. element (IKS_TAG, IKS_CLOSE, "body");
  270. test ();
  271. document ("<tag a='1' b='2' c='3' d='4' e='5' f='6' g='7' id='xyz9'><sub></sub></tag>");
  272. element (IKS_TAG, IKS_OPEN, "tag", "a", "1", "b", "2", "c", "3", "d", "4", "e", "5", "f", "6", "g", "7", "id", "xyz9", 0);
  273. element (IKS_TAG, IKS_OPEN, "sub", 0);
  274. element (IKS_TAG, IKS_CLOSE, "sub");
  275. element (IKS_TAG, IKS_CLOSE, "tag");
  276. test ();
  277. document ("<item url='http://jabber.org'><!-- little comment -->Jabber Site</item>");
  278. element (IKS_TAG, IKS_OPEN, "item", "url", "http://jabber.org", 0);
  279. element (IKS_CDATA, "Jabber Site");
  280. element (IKS_TAG, IKS_CLOSE, "item");
  281. test ();
  282. document ("<index><!-- <item> - tag has no childs --><item name='lala' page='42'/></index>");
  283. element (IKS_TAG, IKS_OPEN, "index", 0);
  284. element (IKS_TAG, IKS_SINGLE, "item", "name", "lala", "page", "42", 0);
  285. element (IKS_TAG, IKS_CLOSE, "index");
  286. test ();
  287. document ("<ka>1234<![CDATA[ <ka> lala ] ]] ]]] ]]>4321</ka>");
  288. element (IKS_TAG, IKS_OPEN, "ka", 0);
  289. element (IKS_CDATA, "1234 <ka> lala ] ]] ]]] 4321");
  290. element (IKS_TAG, IKS_CLOSE, "ka");
  291. test ();
  292. document ("<test><standalone be='happy'/>abcd<br/>&lt;escape&gt;</test>");
  293. element (IKS_TAG, IKS_OPEN, "test", 0);
  294. element (IKS_TAG, IKS_SINGLE, "standalone", "be", "happy", 0);
  295. element (IKS_CDATA, "abcd");
  296. element (IKS_TAG, IKS_SINGLE, "br", 0);
  297. element (IKS_CDATA, "<escape>");
  298. element (IKS_TAG, IKS_CLOSE, "test");
  299. test ();
  300. document ("<a><b>john&amp;mary<c><d e='f' g='123456' h='madcat' klm='nop'/></c></b></a>");
  301. element (IKS_TAG, IKS_OPEN, "a", 0);
  302. element (IKS_TAG, IKS_OPEN, "b", 0);
  303. element (IKS_CDATA, "john&mary");
  304. element (IKS_TAG, IKS_OPEN, "c", 0);
  305. element (IKS_TAG, IKS_SINGLE, "d", "e", "f", "g", "123456", "h", "madcat", "klm", "nop", 0);
  306. element (IKS_TAG, IKS_CLOSE, "c", 0);
  307. element (IKS_TAG, IKS_CLOSE, "b", 0);
  308. element (IKS_TAG, IKS_CLOSE, "a", 0);
  309. test ();
  310. document ("<test>\xFF</test>");
  311. test_bad (6);
  312. document ("<t\0></t>");
  313. tester.len = 8;
  314. test_bad (2);
  315. document ("<a><b/><c></c/></a>");
  316. test_bad (13);
  317. document ("<e><!-- -- --></e>");
  318. test_bad (10);
  319. document ("<g><test a='123'/ b='lala'></g>");
  320. test_bad (17);
  321. document ("<ha><!-- <lala> --><!- comment -></ha>");
  322. test_bad (22);
  323. document ("<lol>&lt;<&gt;</lol>");
  324. test_bad (16);
  325. document ("<a>\xC0\x80</a>");
  326. test_bad (3);
  327. document ("<\x8F\x85></\x8F\x85>");
  328. test_bad (1);
  329. document ("<utf8>\xC1\x80<br/>\xED\x95\x9C\xEA\xB5\xAD\xEC\x96\xB4<err>\xC1\x65</err></utf8>");
  330. test_bad (28);
  331. return 0;
  332. }