tst-filter.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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 <locale.h>
  10. #include <assert.h>
  11. #include "iksemel.h"
  12. struct {
  13. iksfilter *f;
  14. char *xml;
  15. int nr;
  16. iksFilterHook *hook[20];
  17. int nr_hook;
  18. iksFilterHook *call[20];
  19. int nr_call;
  20. } tester;
  21. void
  22. document (char *xml)
  23. {
  24. tester.nr++;
  25. tester.xml = xml;
  26. tester.nr_hook = 0;
  27. }
  28. void
  29. hook (iksFilterHook *hk)
  30. {
  31. tester.hook[tester.nr_hook++] = hk;
  32. }
  33. void
  34. debug (void)
  35. {
  36. int i;
  37. printf ("Filter test %d:\n", tester.nr);
  38. if (tester.nr_hook) {
  39. puts ("Expected hook order:");
  40. for (i = 0; i < tester.nr_hook; i++) {
  41. printf (" ");
  42. tester.hook[i] (NULL, NULL);
  43. }
  44. } else {
  45. puts("No hooks expected.");
  46. }
  47. if (tester.nr_call) {
  48. puts ("Hook order:");
  49. for (i = 0; i < tester.nr_call; i++) {
  50. printf (" ");
  51. tester.call[i] (NULL, NULL);
  52. }
  53. } else {
  54. puts("No hooks called.");
  55. }
  56. exit (1);
  57. }
  58. void
  59. test (void)
  60. {
  61. iksparser *prs;
  62. iks *x;
  63. int i;
  64. tester.nr_call = 0;
  65. prs = iks_dom_new (&x);
  66. iks_parse (prs, tester.xml, strlen (tester.xml), 1);
  67. iks_parser_delete (prs);
  68. iks_filter_packet (tester.f, iks_packet (x));
  69. iks_delete (x);
  70. if (tester.nr_call != tester.nr_hook) debug ();
  71. for (i = 0; i < tester.nr_hook; i++) {
  72. if (tester.call[i] != tester.hook[i]) debug ();
  73. }
  74. }
  75. #define DEBUG(x) if (NULL == pak) { puts ( (x) ); return IKS_FILTER_PASS; }
  76. int
  77. on_msg (void *user_data, ikspak *pak)
  78. {
  79. DEBUG ("on_msg");
  80. assert (IKS_PAK_MESSAGE == pak->type);
  81. tester.call[tester.nr_call++] = on_msg;
  82. return IKS_FILTER_PASS;
  83. }
  84. int
  85. on_iq (void *user_data, ikspak *pak)
  86. {
  87. DEBUG ("on_iq");
  88. assert (IKS_PAK_IQ == pak->type);
  89. tester.call[tester.nr_call++] = on_iq;
  90. return IKS_FILTER_PASS;
  91. }
  92. int
  93. on_iq_result (void *user_data, ikspak *pak)
  94. {
  95. DEBUG ("on_iq_result");
  96. assert (IKS_PAK_IQ == pak->type);
  97. assert (IKS_TYPE_RESULT == pak->subtype);
  98. tester.call[tester.nr_call++] = on_iq_result;
  99. return IKS_FILTER_PASS;
  100. }
  101. int
  102. on_iq_result_id_auth (void *user_data, ikspak *pak)
  103. {
  104. DEBUG ("on_iq_result_id_auth");
  105. assert (IKS_PAK_IQ == pak->type);
  106. assert (IKS_TYPE_RESULT == pak->subtype);
  107. assert (iks_strcmp (pak->id, "auth") == 0);
  108. tester.call[tester.nr_call++] = on_iq_result_id_auth;
  109. return IKS_FILTER_PASS;
  110. }
  111. int
  112. on_id_auth (void *user_data, ikspak *pak)
  113. {
  114. DEBUG ("on_id_auth");
  115. assert (iks_strcmp (pak->id, "auth") == 0);
  116. tester.call[tester.nr_call++] = on_id_auth;
  117. return IKS_FILTER_PASS;
  118. }
  119. int
  120. on_from_patrician (void *user_data, ikspak *pak)
  121. {
  122. DEBUG ("on_from_patrician");
  123. assert (iks_strcmp (pak->from->partial, "patrician@morpork.gov") == 0);
  124. tester.call[tester.nr_call++] = on_from_patrician;
  125. return IKS_FILTER_PASS;
  126. }
  127. int
  128. on_msg_chat_from_patrician (void *user_data, ikspak *pak)
  129. {
  130. DEBUG ("on_msg_chat_from_patrician");
  131. assert (pak->type == IKS_PAK_MESSAGE);
  132. assert (pak->subtype == IKS_TYPE_CHAT);
  133. assert (iks_strcmp (pak->from->partial, "patrician@morpork.gov") == 0);
  134. tester.call[tester.nr_call++] = on_msg_chat_from_patrician;
  135. return IKS_FILTER_PASS;
  136. }
  137. int
  138. on_id_albatros (void *user_data, ikspak *pak)
  139. {
  140. DEBUG ("on_id_albatros");
  141. assert (iks_strcmp (pak->id, "albatros") == 0);
  142. tester.call[tester.nr_call++] = on_id_albatros;
  143. return IKS_FILTER_PASS;
  144. }
  145. int
  146. on_from_dean (void *user_data, ikspak *pak)
  147. {
  148. DEBUG ("on_from_dean");
  149. assert (iks_strcmp (pak->from->partial, "dean@unseen.edu") == 0);
  150. tester.call[tester.nr_call++] = on_from_dean;
  151. return IKS_FILTER_PASS;
  152. }
  153. int
  154. main (int argc, char *argv[])
  155. {
  156. tester.f = iks_filter_new ();
  157. iks_filter_add_rule (tester.f, on_msg, 0,
  158. IKS_RULE_TYPE, IKS_PAK_MESSAGE,
  159. IKS_RULE_DONE);
  160. iks_filter_add_rule (tester.f, on_iq, 0,
  161. IKS_RULE_TYPE, IKS_PAK_IQ,
  162. IKS_RULE_DONE);
  163. iks_filter_add_rule (tester.f, on_iq_result, 0,
  164. IKS_RULE_TYPE, IKS_PAK_IQ,
  165. IKS_RULE_SUBTYPE, IKS_TYPE_RESULT,
  166. IKS_RULE_DONE);
  167. iks_filter_add_rule (tester.f, on_iq_result_id_auth, 0,
  168. IKS_RULE_TYPE, IKS_PAK_IQ,
  169. IKS_RULE_SUBTYPE, IKS_TYPE_RESULT,
  170. IKS_RULE_ID, "auth",
  171. IKS_RULE_DONE);
  172. iks_filter_add_rule (tester.f, on_id_auth, 0,
  173. IKS_RULE_ID, "auth",
  174. IKS_RULE_DONE);
  175. iks_filter_add_rule (tester.f, on_from_dean, 0,
  176. IKS_RULE_FROM_PARTIAL, "dean@unseen.edu",
  177. IKS_RULE_DONE);
  178. iks_filter_add_rule (tester.f, on_from_patrician, 0,
  179. IKS_RULE_FROM_PARTIAL, "patrician@morpork.gov",
  180. IKS_RULE_DONE);
  181. iks_filter_add_rule (tester.f, on_msg_chat_from_patrician, 0,
  182. IKS_RULE_TYPE, IKS_PAK_MESSAGE,
  183. IKS_RULE_SUBTYPE, IKS_TYPE_CHAT,
  184. IKS_RULE_FROM_PARTIAL, "patrician@morpork.gov",
  185. IKS_RULE_DONE);
  186. iks_filter_add_rule (tester.f, on_id_albatros, 0,
  187. IKS_RULE_ID, "albatros",
  188. IKS_RULE_DONE);
  189. document ("<message from='dean@unseen.edu' id='1234'><body>Born to Rune.</body></message>");
  190. hook (on_from_dean);
  191. hook (on_msg);
  192. test ();
  193. document ("<presence from='librarian@unseen.edu' show='away'/>");
  194. test ();
  195. document ("<message from='rincewind@unseen.edu' type='chat' id='albatros'><body>yaaargh</body></message>");
  196. hook (on_id_albatros);
  197. hook (on_msg);
  198. test ();
  199. document ("<iq type='get' from='rincewind@unseen.edu'><query xmlns='jabber:time'/></iq>");
  200. hook (on_iq);
  201. test ();
  202. document ("<message from='patrician@morpork.gov'><body>so you admit it?</body></message>");
  203. hook (on_from_patrician);
  204. hook (on_msg);
  205. test ();
  206. document ("<iq type='result' from='rincewind@unseen.edu'><query xmlns='jabber:version'><name>cabbar</name><version>1.0</version></query></iq>");
  207. hook (on_iq_result);
  208. hook (on_iq);
  209. test ();
  210. document ("<presence from='dean@unseen.edu/psi' type='unavailable'/>");
  211. hook (on_from_dean);
  212. test ();
  213. document ("<message from='patrician@morpork.gov' type='chat' id='albatros'><body>hmm</body></message>");
  214. hook (on_id_albatros);
  215. hook (on_msg_chat_from_patrician);
  216. hook (on_from_patrician);
  217. hook (on_msg);
  218. test ();
  219. document ("<iq type='result' id='auth'/>");
  220. hook (on_iq_result_id_auth);
  221. hook (on_id_auth);
  222. hook (on_iq_result);
  223. hook (on_iq);
  224. test ();
  225. iks_filter_delete (tester.f);
  226. return 0;
  227. }