summaryrefslogtreecommitdiff
path: root/tests/wocky-pep-service-test.c
blob: fb47fe2b7624fe0d3ce85f0a22664360924a2b2c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdio.h>
#include <unistd.h>
#include <string.h>

#include <glib.h>

#include <wocky/wocky.h>

#include "wocky-test-stream.h"
#include "wocky-test-helper.h"

#define TEST_NODE1 "http://test.com/node1"
#define TEST_NODE2 "http://test.com/node2"

/* Test to instantiate a WockyPepService object */
static void
test_instantiation (void)
{
  WockyPepService *pep;

  pep = wocky_pep_service_new ("http://test.com/badger", FALSE);
  g_assert (pep != NULL);

  g_object_unref (pep);
}

/* Test that the 'changed' signal is properly fired */
gboolean event_received;

static void
test_changed_signal_cb (WockyPepService *pep,
    WockyBareContact *contact,
    WockyStanza *stanza,
    WockyNode *item,
    test_data_t *test)
{
  const gchar *id = wocky_node_get_attribute (
      wocky_stanza_get_top_node (stanza), "id");

  g_assert_cmpstr (wocky_bare_contact_get_jid (contact), ==,
        "alice@example.org");

  /* the id happens to hold the number of <item> children; we expect to get the
   * first one if there is more than one. */
  if (wocky_strdiff (id, "0"))
    {
      g_assert (item != NULL);
      g_assert_cmpstr (item->name, ==, "item");
      g_assert_cmpstr (wocky_node_get_attribute (item, "id"), ==, "1");
    }
  else
    {
      g_assert (item == NULL);
    }

  test->outstanding--;
  g_main_loop_quit (test->loop);
  event_received = TRUE;
}

static void
send_pep_event (WockyPorter *porter,
    const gchar *node,
    guint n_items)
{
  WockyStanza *stanza;
  WockyNode *items;
  gchar *n_items_str = g_strdup_printf ("%d", n_items);
  guint i;

  stanza = wocky_stanza_build (WOCKY_STANZA_TYPE_MESSAGE,
      WOCKY_STANZA_SUB_TYPE_NONE,
      "alice@example.org", NULL,
      /* This is a hint for test_changed_signal_cb. */
      '@', "id", n_items_str,
      '(', "event",
        ':', WOCKY_XMPP_NS_PUBSUB_EVENT,
        '(', "items",
          '@', "node", node,
          '*', &items,
        ')',
      ')',
      NULL);

  for (i = 1; i <= n_items; i++)
    {
      gchar *i_str = g_strdup_printf ("%d", i);
      wocky_node_add_build (items,
          '(', "item",
          '@', "id", i_str,
            '(', "payload", ')',
          ')', NULL);
      g_free (i_str);
    }

  wocky_porter_send (porter, stanza);
  g_object_unref (stanza);
  g_free (n_items_str);
}

static void
test_changed_signal (void)
{
  test_data_t *test = setup_test ();
  WockyPepService *pep;

  pep = wocky_pep_service_new (TEST_NODE1, FALSE);
  test_open_both_connections (test);

  g_signal_connect (pep, "changed", G_CALLBACK (test_changed_signal_cb), test);

  wocky_pep_service_start (pep, test->session_out);
  wocky_porter_start (test->sched_out);
  wocky_porter_start (test->sched_in);

  /* send events on the right node */
  event_received = FALSE;
  send_pep_event (test->sched_in, TEST_NODE1, 0);
  send_pep_event (test->sched_in, TEST_NODE1, 1);
  send_pep_event (test->sched_in, TEST_NODE1, 2);

  test->outstanding += 3;
  test_wait_pending (test);
  g_assert (event_received);
  event_received = FALSE;

  /* send event on the wrong node */
  send_pep_event (test->sched_in, TEST_NODE2, 1);

  g_object_unref (pep);

  /* send event to the right node after the PEP service has been destroyed */
  send_pep_event (test->sched_in, TEST_NODE1, 1);

  test_close_both_porters (test);
  teardown_test (test);
  g_assert (!event_received);
}

/* Test wocky_pep_service_get_async */
static void
test_send_query_cb (GObject *source_object,
    GAsyncResult *res,
    gpointer user_data)
{
  test_data_t *test = (test_data_t *) user_data;
  WockyStanza *reply;
  WockyNode *item;
  WockyStanzaType type;
  WockyStanzaSubType sub_type;

  reply = wocky_pep_service_get_finish (WOCKY_PEP_SERVICE (source_object), res,
      &item, NULL);
  g_assert (reply != NULL);

  wocky_stanza_get_type_info (reply, &type, &sub_type);
  g_assert (type == WOCKY_STANZA_TYPE_IQ);
  g_assert (sub_type == WOCKY_STANZA_SUB_TYPE_RESULT);

  g_assert (item != NULL);
  g_assert_cmpstr (item->name, ==, "item");
  g_assert_cmpstr (wocky_node_get_attribute (item, "id"), ==, "1");

  g_object_unref (reply);

  test->outstanding--;
  g_main_loop_quit (test->loop);
}

static gboolean
test_send_query_stanza_received_cb (WockyPorter *porter,
    WockyStanza *stanza,
    gpointer user_data)
{
  test_data_t *test = (test_data_t *) user_data;
  WockyStanza *reply;

  reply = wocky_stanza_build_iq_result (stanza,
      '(', "pubsub",
        ':', WOCKY_XMPP_NS_PUBSUB,
        '(', "items",
          '@', "node", "node1",
          '(', "item",
            '@', "id", "1",
            '(', "payload", ')',
          ')',
        ')',
      ')',
      NULL);

  wocky_porter_send (porter, reply);
  g_object_unref (reply);

  test->outstanding--;
  g_main_loop_quit (test->loop);
  return TRUE;
}

static void
test_send_query_failed_cb (GObject *source_object,
    GAsyncResult *res,
    gpointer user_data)
{
  test_data_t *test = (test_data_t *) user_data;
  WockyStanza *reply;
  GError *error = NULL;

  reply = wocky_pep_service_get_finish (WOCKY_PEP_SERVICE (source_object), res,
      NULL, &error);
  g_assert (reply == NULL);
  g_assert_error (error, WOCKY_XMPP_CONNECTION_ERROR,
      WOCKY_XMPP_CONNECTION_ERROR_CLOSED);
  g_clear_error (&error);

  test->outstanding--;
  g_main_loop_quit (test->loop);
}

static void
test_get (void)
{
  test_data_t *test = setup_test ();
  WockyPepService *pep;
  WockyContactFactory *contact_factory;
  WockyBareContact *contact;
  guint handler_id;

  pep = wocky_pep_service_new (TEST_NODE1, FALSE);
  test_open_both_connections (test);

  wocky_pep_service_start (pep, test->session_in);
  wocky_porter_start (test->sched_out);
  wocky_porter_start (test->sched_in);

  handler_id = wocky_porter_register_handler_from_anyone (test->sched_out,
      WOCKY_STANZA_TYPE_IQ, WOCKY_STANZA_SUB_TYPE_GET,
      WOCKY_PORTER_HANDLER_PRIORITY_MAX,
      test_send_query_stanza_received_cb, test, NULL);

  contact_factory = wocky_session_get_contact_factory (test->session_in);
  contact = wocky_contact_factory_ensure_bare_contact (contact_factory,
      "juliet@example.org");

  wocky_pep_service_get_async (pep, contact, NULL, test_send_query_cb, test);

  test->outstanding += 2;
  test_wait_pending (test);

  /* Regression test for a bug where wocky_pep_service_get_async's callback
   * would crash if sending the IQ failed.
   */
  wocky_porter_unregister_handler (test->sched_out, handler_id);
  wocky_pep_service_get_async (pep, contact, NULL, test_send_query_failed_cb,
      test);
  test->outstanding += 1;
  test_close_both_porters (test);

  g_object_unref (contact);
  g_object_unref (pep);
  teardown_test (test);
}

/* Test wocky_pep_service_make_publish_stanza */
static void
test_make_publish_stanza (void)
{
  WockyPepService *pep;
  WockyStanza *stanza;
  WockyNode *item = NULL, *n;
  WockyStanzaType type;
  WockyStanzaSubType sub_type;

  pep = wocky_pep_service_new (TEST_NODE1, FALSE);

  stanza = wocky_pep_service_make_publish_stanza (pep, &item);

  g_assert (stanza != NULL);

  wocky_stanza_get_type_info (stanza, &type, &sub_type);
  g_assert (type == WOCKY_STANZA_TYPE_IQ);
  g_assert (sub_type == WOCKY_STANZA_SUB_TYPE_SET);

  n = wocky_node_get_child_ns (wocky_stanza_get_top_node (stanza),
      "pubsub", WOCKY_XMPP_NS_PUBSUB);
  g_assert (n != NULL);

  n = wocky_node_get_child (n, "publish");
  g_assert (n != NULL);
  g_assert (!wocky_strdiff (wocky_node_get_attribute (n, "node"),
        TEST_NODE1));

  n = wocky_node_get_child (n, "item");
  g_assert (n != NULL);
  g_assert (n == item);

  g_object_unref (stanza);
  g_object_unref (pep);
}

int
main (int argc, char **argv)
{
  int result;

  test_init (argc, argv);

  g_test_add_func ("/pep-service/instantiation", test_instantiation);
  g_test_add_func ("/pep-service/changed-signal", test_changed_signal);
  g_test_add_func ("/pep-service/get", test_get);
  g_test_add_func ("/pep-service/make-publish-stanza",
      test_make_publish_stanza);

  result = g_test_run ();
  test_deinit ();
  return result;
}