summaryrefslogtreecommitdiff
path: root/tests/wocky-xmpp-readwrite-test.c
blob: 2a70eadb6d82edbd71ebe138f1af78939fb16805 (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
#include <stdio.h>
#include <unistd.h>
#include <string.h>

#include <glib.h>

#include <wocky/wocky-xmpp-reader.h>
#include <wocky/wocky-xmpp-writer.h>
#include <wocky/wocky-utils.h>

#include "wocky-test-helper.h"

#define TO "example.net"
#define FROM "julliet@example.com"
#define VERSION "1.0"
#define LANG "en"
#define DUMMY_NS "urn:wocky:test:blah:blah:blah"

static WockyStanza *
create_stanza (void)
{
  WockyStanza *stanza;
  WockyNode *html;
  WockyNode *head;

  stanza = wocky_stanza_build (WOCKY_STANZA_TYPE_MESSAGE,
    WOCKY_STANZA_SUB_TYPE_CHAT, "juliet@example.com", "romeo@example.net",
      '(', "html", ':', "http://www.w3.org/1999/xhtml",
        '(', "body",
          '$', "Art thou not Romeo, and a Montague?",
        ')',
      ')',
    NULL);

  html = wocky_node_get_child (wocky_stanza_get_top_node (stanza), "html");
  head = wocky_node_add_child (html, "head");
  wocky_node_set_attribute_ns (head, "rev", "0xbad1dea", DUMMY_NS);

  return stanza;
}

static void
test_readwrite (void)
{
  WockyXmppReader *reader;
  WockyXmppWriter *writer;
  WockyStanza *received = NULL, *sent;
  const guint8 *data;
  gsize length;
  gchar *to, *from, *version, *lang;
  int i;

  writer = wocky_xmpp_writer_new ();
  reader = wocky_xmpp_reader_new ();

  g_assert (wocky_xmpp_reader_get_state (reader)
    == WOCKY_XMPP_READER_STATE_INITIAL);

  wocky_xmpp_writer_stream_open (writer, TO, FROM, VERSION, LANG, NULL,
      &data, &length);
  wocky_xmpp_reader_push (reader, data, length);

  g_assert (wocky_xmpp_reader_get_state (reader)
    == WOCKY_XMPP_READER_STATE_OPENED);

  g_object_get (reader,
      "to", &to,
      "from", &from,
      "version", &version,
      "lang", &lang,
      NULL);

  g_assert (!wocky_strdiff (to, TO));
  g_assert (!wocky_strdiff (from, FROM));
  g_assert (!wocky_strdiff (version, VERSION));
  g_assert (!wocky_strdiff (lang, LANG));

  g_free (to);
  g_free (from);
  g_free (version);
  g_free (lang);

  sent = create_stanza ();

  for (i = 0; i < 3 ; i++)
    {
      WockyNode *html;
      WockyNode *head;
      const gchar *attr_recv = NULL;
      const gchar *attr_send = NULL;
      const gchar *attr_none = NULL;

      g_assert (wocky_xmpp_reader_get_state (reader)
        == WOCKY_XMPP_READER_STATE_OPENED);

      wocky_xmpp_writer_write_stanza (writer, sent, &data, &length);
      wocky_xmpp_reader_push (reader, data, length);

      received = wocky_xmpp_reader_pop_stanza (reader);

      g_assert (received != NULL);
      test_assert_stanzas_equal (sent, received);

      html = wocky_node_get_child (wocky_stanza_get_top_node (received),
          "html");
      head = wocky_node_get_child (html, "head");
      attr_recv = wocky_node_get_attribute_ns (head, "rev", DUMMY_NS);
      attr_none = wocky_node_get_attribute_ns (head, "rev",
        DUMMY_NS ":x");

      html = wocky_node_get_child (wocky_stanza_get_top_node (sent),
          "html");
      head = wocky_node_get_child (html, "head");
      attr_send = wocky_node_get_attribute_ns (head, "rev", DUMMY_NS);

      g_assert (attr_none == NULL);
      g_assert (attr_recv != NULL);
      g_assert (attr_send != NULL);
      g_assert (!strcmp (attr_send, attr_recv));


      g_object_unref (received);

      /* No more stanzas in the queue */
      received = wocky_xmpp_reader_pop_stanza (reader);
      g_assert (received == NULL);
    }

  wocky_xmpp_writer_write_stanza (writer, sent, &data, &length);
  wocky_xmpp_reader_push (reader, data, length);

  wocky_xmpp_writer_stream_close (writer, &data, &length);
  wocky_xmpp_reader_push (reader, data, length);

  /*  Stream state should stay open until we popped the last stanza */
  g_assert (wocky_xmpp_reader_get_state (reader)
     == WOCKY_XMPP_READER_STATE_OPENED);

  received = wocky_xmpp_reader_pop_stanza (reader);
  g_assert (received != NULL);
  test_assert_stanzas_equal (sent, received);

  g_object_unref (received);

  /* Last stanza pop, stream should be closed */
  g_assert (wocky_xmpp_reader_get_state (reader)
    == WOCKY_XMPP_READER_STATE_CLOSED);

  /* No more stanzas in the queue */
  received = wocky_xmpp_reader_pop_stanza (reader);
  g_assert (received == NULL);

  g_object_unref (sent);
  g_object_unref (reader);
  g_object_unref (writer);
}

static void
test_readwrite_nostream (void)
{
  WockyXmppReader *reader;
  WockyXmppWriter *writer;
  WockyStanza *received = NULL, *sent;
  const guint8 *data;
  gsize length;
  int i;

  writer = wocky_xmpp_writer_new_no_stream ();
  reader = wocky_xmpp_reader_new_no_stream ();

  sent = create_stanza ();


  for (i = 0 ; i < 3 ; i++)
    {
      g_assert (wocky_xmpp_reader_get_state (reader)
        == WOCKY_XMPP_READER_STATE_OPENED);

      wocky_xmpp_writer_write_stanza (writer, sent, &data, &length);
      wocky_xmpp_reader_push (reader, data, length);

      g_assert (wocky_xmpp_reader_get_state (reader)
        == WOCKY_XMPP_READER_STATE_OPENED);

      received = wocky_xmpp_reader_pop_stanza (reader);

      g_assert (received != NULL);
      test_assert_stanzas_equal (sent, received);

      g_assert (wocky_xmpp_reader_get_state (reader) ==
        WOCKY_XMPP_READER_STATE_CLOSED);

      wocky_xmpp_reader_reset (reader);

      g_object_unref (received);
    }

  g_object_unref (sent);
  g_object_unref (reader);
  g_object_unref (writer);
}

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

  test_init (argc, argv);

  g_test_add_func ("/xmpp-readwrite/readwrite", test_readwrite);
  g_test_add_func ("/xmpp-readwrite/readwrite-nostream",
    test_readwrite_nostream);

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