/telepathy-gabble-0.16.1/lib/ext/wocky/tests/wocky-test-helper.c

# · C · 310 lines · 237 code · 61 blank · 12 comment · 11 complexity · 5e9cb2f4c86f93f4071f955ed45da2ab MD5 · raw file

  1. #ifdef HAVE_CONFIG_H
  2. #include "config.h"
  3. #endif
  4. #include <wocky/wocky.h>
  5. #include "wocky-test-helper.h"
  6. #include "wocky-test-stream.h"
  7. #define TIMEOUT 10
  8. gboolean
  9. test_timeout_cb (gpointer data)
  10. {
  11. g_test_message ("Timeout reached :(");
  12. g_assert_not_reached ();
  13. return FALSE;
  14. }
  15. static test_data_t *
  16. setup_test_full (guint timeout,
  17. const gchar *in_jid,
  18. const gchar *out_jid)
  19. {
  20. test_data_t *data;
  21. data = g_new0 (test_data_t, 1);
  22. data->loop = g_main_loop_new (NULL, FALSE);
  23. data->stream = g_object_new (WOCKY_TYPE_TEST_STREAM, NULL);
  24. data->in = wocky_xmpp_connection_new (data->stream->stream0);
  25. data->out = wocky_xmpp_connection_new (data->stream->stream1);
  26. data->session_in = wocky_session_new_with_connection (data->in, in_jid);
  27. data->session_out = wocky_session_new_with_connection (data->out, out_jid);
  28. data->sched_in = wocky_session_get_porter (data->session_in);
  29. data->sched_out = wocky_session_get_porter (data->session_out);
  30. data->expected_stanzas = g_queue_new ();
  31. data->cancellable = g_cancellable_new ();
  32. data->timeout_id = g_timeout_add_seconds (timeout, test_timeout_cb, NULL);
  33. return data;
  34. }
  35. test_data_t *
  36. setup_test_with_jids (const gchar *in_jid,
  37. const gchar *out_jid)
  38. {
  39. return setup_test_full (TIMEOUT, in_jid, out_jid);
  40. }
  41. test_data_t *
  42. setup_test_with_timeout (guint timeout)
  43. {
  44. return setup_test_full (timeout, "example.com", "example.com");
  45. }
  46. test_data_t *
  47. setup_test (void)
  48. {
  49. return setup_test_full (TIMEOUT, "example.com", "example.com");
  50. }
  51. void
  52. teardown_test (test_data_t *data)
  53. {
  54. g_main_loop_unref (data->loop);
  55. g_object_unref (data->stream);
  56. g_object_unref (data->in);
  57. g_object_unref (data->out);
  58. if (data->session_in != NULL)
  59. g_object_unref (data->session_in);
  60. if (data->session_out != NULL)
  61. g_object_unref (data->session_out);
  62. g_object_unref (data->cancellable);
  63. g_source_remove (data->timeout_id);
  64. /* All the stanzas should have been received */
  65. g_assert (g_queue_get_length (data->expected_stanzas) == 0);
  66. g_queue_free (data->expected_stanzas);
  67. g_free (data);
  68. }
  69. void
  70. test_wait_pending (test_data_t *test)
  71. {
  72. while (test->outstanding > 0)
  73. g_main_loop_run (test->loop);
  74. }
  75. static void
  76. send_received_open_cb (GObject *source, GAsyncResult *res, gpointer user_data)
  77. {
  78. WockyXmppConnection *conn = WOCKY_XMPP_CONNECTION (source);
  79. test_data_t *d = (test_data_t *) user_data;
  80. g_assert (wocky_xmpp_connection_recv_open_finish (conn, res,
  81. NULL, NULL, NULL, NULL, NULL, NULL));
  82. d->outstanding--;
  83. g_main_loop_quit (d->loop);
  84. }
  85. static void
  86. send_open_cb (GObject *source, GAsyncResult *res, gpointer user_data)
  87. {
  88. test_data_t *data = (test_data_t *) user_data;
  89. WockyXmppConnection *conn;
  90. g_assert (wocky_xmpp_connection_send_open_finish (
  91. WOCKY_XMPP_CONNECTION (source), res, NULL));
  92. /* The other connection has to receive the opening */
  93. if (WOCKY_XMPP_CONNECTION (source) == data->in)
  94. conn = data->out;
  95. else
  96. conn = data->in;
  97. wocky_xmpp_connection_recv_open_async (conn,
  98. NULL, send_received_open_cb, user_data);
  99. }
  100. static void
  101. open_connection (test_data_t *test,
  102. WockyXmppConnection *connection)
  103. {
  104. wocky_xmpp_connection_send_open_async (connection,
  105. NULL, NULL, NULL, NULL, NULL,
  106. NULL, send_open_cb, test);
  107. test->outstanding++;
  108. test_wait_pending (test);
  109. }
  110. /* Open XMPP the 'in' connection */
  111. void
  112. test_open_connection (test_data_t *test)
  113. {
  114. open_connection (test, test->in);
  115. }
  116. /* Open both XMPP connections */
  117. void
  118. test_open_both_connections (test_data_t *test)
  119. {
  120. open_connection (test, test->in);
  121. open_connection (test, test->out);
  122. }
  123. static void
  124. wait_close_cb (GObject *source, GAsyncResult *res,
  125. gpointer user_data)
  126. {
  127. test_data_t *data = (test_data_t *) user_data;
  128. WockyStanza *s;
  129. GError *error = NULL;
  130. s = wocky_xmpp_connection_recv_stanza_finish (WOCKY_XMPP_CONNECTION (source),
  131. res, &error);
  132. g_assert (s == NULL);
  133. /* connection has been disconnected */
  134. g_assert_error (error, WOCKY_XMPP_CONNECTION_ERROR,
  135. WOCKY_XMPP_CONNECTION_ERROR_CLOSED);
  136. g_error_free (error);
  137. data->outstanding--;
  138. g_main_loop_quit (data->loop);
  139. }
  140. static void
  141. close_sent_cb (GObject *source, GAsyncResult *res,
  142. gpointer user_data)
  143. {
  144. test_data_t *data = (test_data_t *) user_data;
  145. g_assert (wocky_xmpp_connection_send_close_finish (
  146. WOCKY_XMPP_CONNECTION (source), res, NULL));
  147. data->outstanding--;
  148. g_main_loop_quit (data->loop);
  149. }
  150. /* Close XMPP connections on both sides */
  151. void
  152. test_close_connection (test_data_t *test)
  153. {
  154. wocky_xmpp_connection_recv_stanza_async (test->out, NULL, wait_close_cb,
  155. test);
  156. test->outstanding++;
  157. /* Close the connection */
  158. wocky_xmpp_connection_send_close_async (
  159. WOCKY_XMPP_CONNECTION (test->in),
  160. NULL, close_sent_cb, test);
  161. test->outstanding++;
  162. test_wait_pending (test);
  163. }
  164. static void
  165. sched_close_cb (GObject *source,
  166. GAsyncResult *res,
  167. gpointer user_data)
  168. {
  169. test_data_t *test = (test_data_t *) user_data;
  170. GError *error = NULL;
  171. gboolean ret = wocky_porter_close_finish (
  172. WOCKY_PORTER (source), res, &error);
  173. g_assert_no_error (error);
  174. g_assert (ret);
  175. test->outstanding--;
  176. g_main_loop_quit (test->loop);
  177. }
  178. static void
  179. wait_sched_close_cb (GObject *source,
  180. GAsyncResult *res,
  181. gpointer user_data)
  182. {
  183. test_data_t *test = (test_data_t *) user_data;
  184. WockyXmppConnection *connection = WOCKY_XMPP_CONNECTION (source);
  185. WockyStanza *s;
  186. GError *error = NULL;
  187. s = wocky_xmpp_connection_recv_stanza_finish (connection, res, &error);
  188. g_assert (s == NULL);
  189. /* connection has been disconnected */
  190. g_assert_error (error, WOCKY_XMPP_CONNECTION_ERROR,
  191. WOCKY_XMPP_CONNECTION_ERROR_CLOSED);
  192. g_error_free (error);
  193. /* close on our side */
  194. wocky_xmpp_connection_send_close_async (connection, NULL,
  195. close_sent_cb, test);
  196. /* Don't decrement test->outstanding as we are waiting for another
  197. * callback */
  198. g_main_loop_quit (test->loop);
  199. }
  200. void
  201. test_close_porter (test_data_t *test)
  202. {
  203. /* close connections */
  204. wocky_xmpp_connection_recv_stanza_async (test->in, NULL,
  205. wait_sched_close_cb, test);
  206. wocky_porter_close_async (test->sched_out, NULL, sched_close_cb,
  207. test);
  208. test->outstanding += 2;
  209. test_wait_pending (test);
  210. }
  211. void
  212. test_expected_stanza_received (test_data_t *test,
  213. WockyStanza *stanza)
  214. {
  215. WockyStanza *expected;
  216. expected = g_queue_pop_head (test->expected_stanzas);
  217. g_assert (expected != NULL);
  218. test_assert_nodes_equal (wocky_stanza_get_top_node (stanza),
  219. wocky_stanza_get_top_node (expected));
  220. g_object_unref (expected);
  221. test->outstanding--;
  222. g_main_loop_quit (test->loop);
  223. }
  224. void
  225. test_close_both_porters (test_data_t *test)
  226. {
  227. wocky_porter_close_async (test->sched_out, NULL, sched_close_cb,
  228. test);
  229. wocky_porter_close_async (test->sched_in, NULL, sched_close_cb,
  230. test);
  231. test->outstanding += 2;
  232. test_wait_pending (test);
  233. }
  234. void
  235. test_init (int argc,
  236. char **argv)
  237. {
  238. #if !GLIB_CHECK_VERSION (2, 31, 0)
  239. g_thread_init (NULL);
  240. #endif
  241. g_test_init (&argc, &argv, NULL);
  242. g_type_init ();
  243. wocky_init ();
  244. }
  245. void
  246. test_deinit (void)
  247. {
  248. wocky_deinit ();
  249. }