summaryrefslogtreecommitdiff
path: root/xmpp/pubsubclient_unittest.cc
blob: 135dc37e46ae93d6d799e282d338f480e369f5f8 (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
// Copyright 2011 Google Inc. All Rights Reserved


#include <string>

#include "talk/xmllite/qname.h"
#include "talk/xmllite/xmlelement.h"
#include "talk/xmpp/constants.h"
#include "talk/xmpp/fakexmppclient.h"
#include "talk/xmpp/jid.h"
#include "talk/xmpp/pubsubclient.h"
#include "webrtc/base/faketaskrunner.h"
#include "webrtc/base/gunit.h"
#include "webrtc/base/sigslot.h"

struct HandledPubSubItem {
  std::string itemid;
  std::string payload;
};

class TestPubSubItemsListener : public sigslot::has_slots<> {
 public:
  TestPubSubItemsListener() : error_count(0) {}

  void OnItems(buzz::PubSubClient*,
               const std::vector<buzz::PubSubItem>& items) {
    for (std::vector<buzz::PubSubItem>::const_iterator item = items.begin();
         item != items.end(); ++item) {
      HandledPubSubItem handled_item;
      handled_item.itemid = item->itemid;
      if (item->elem->FirstElement() != NULL) {
        handled_item.payload = item->elem->FirstElement()->Str();
      }
      this->items.push_back(handled_item);
    }
  }

  void OnRequestError(buzz::PubSubClient* client,
                      const buzz::XmlElement* stanza) {
    error_count++;
  }

  void OnPublishResult(buzz::PubSubClient* client,
                       const std::string& task_id,
                       const buzz::XmlElement* item) {
    result_task_id = task_id;
  }

  void OnPublishError(buzz::PubSubClient* client,
                      const std::string& task_id,
                      const buzz::XmlElement* item,
                      const buzz::XmlElement* stanza) {
    error_count++;
    error_task_id = task_id;
  }

  void OnRetractResult(buzz::PubSubClient* client,
                       const std::string& task_id) {
    result_task_id = task_id;
  }

  void OnRetractError(buzz::PubSubClient* client,
                      const std::string& task_id,
                      const buzz::XmlElement* stanza) {
    error_count++;
    error_task_id = task_id;
  }

  std::vector<HandledPubSubItem> items;
  int error_count;
  std::string error_task_id;
  std::string result_task_id;
};

class PubSubClientTest : public testing::Test {
 public:
  PubSubClientTest() :
      pubsubjid("room@domain.com"),
      node("topic"),
      itemid("key") {
    runner.reset(new rtc::FakeTaskRunner());
    xmpp_client = new buzz::FakeXmppClient(runner.get());
    client.reset(new buzz::PubSubClient(xmpp_client, pubsubjid, node));
    listener.reset(new TestPubSubItemsListener());
    client->SignalItems.connect(
        listener.get(), &TestPubSubItemsListener::OnItems);
    client->SignalRequestError.connect(
        listener.get(), &TestPubSubItemsListener::OnRequestError);
    client->SignalPublishResult.connect(
        listener.get(), &TestPubSubItemsListener::OnPublishResult);
    client->SignalPublishError.connect(
        listener.get(), &TestPubSubItemsListener::OnPublishError);
    client->SignalRetractResult.connect(
        listener.get(), &TestPubSubItemsListener::OnRetractResult);
    client->SignalRetractError.connect(
        listener.get(), &TestPubSubItemsListener::OnRetractError);
  }

  rtc::scoped_ptr<rtc::FakeTaskRunner> runner;
  // xmpp_client deleted by deleting runner.
  buzz::FakeXmppClient* xmpp_client;
  rtc::scoped_ptr<buzz::PubSubClient> client;
  rtc::scoped_ptr<TestPubSubItemsListener> listener;
  buzz::Jid pubsubjid;
  std::string node;
  std::string itemid;
};

TEST_F(PubSubClientTest, TestRequest) {
  client->RequestItems();

  std::string expected_iq =
      "<cli:iq type=\"get\" to=\"room@domain.com\" id=\"0\" "
        "xmlns:cli=\"jabber:client\">"
        "<pub:pubsub xmlns:pub=\"http://jabber.org/protocol/pubsub\">"
          "<pub:items node=\"topic\"/>"
        "</pub:pubsub>"
      "</cli:iq>";

  ASSERT_EQ(1U, xmpp_client->sent_stanzas().size());
  EXPECT_EQ(expected_iq, xmpp_client->sent_stanzas()[0]->Str());

  std::string result_iq =
      "<iq xmlns='jabber:client' id='0' type='result' from='room@domain.com'>"
      "  <pubsub xmlns='http://jabber.org/protocol/pubsub'>"
      "    <items node='topic'>"
      "      <item id='key0'>"
      "        <value0a/>"
      "      </item>"
      "      <item id='key1'>"
      "        <value1a/>"
      "      </item>"
      "    </items>"
      "  </pubsub>"
      "</iq>";

  xmpp_client->HandleStanza(buzz::XmlElement::ForStr(result_iq));
  ASSERT_EQ(2U, listener->items.size());
  EXPECT_EQ("key0", listener->items[0].itemid);
  EXPECT_EQ("<pub:value0a xmlns:pub=\"http://jabber.org/protocol/pubsub\"/>",
            listener->items[0].payload);
  EXPECT_EQ("key1", listener->items[1].itemid);
  EXPECT_EQ("<pub:value1a xmlns:pub=\"http://jabber.org/protocol/pubsub\"/>",
            listener->items[1].payload);

  std::string items_message =
      "<message xmlns='jabber:client' from='room@domain.com'>"
      "  <event xmlns='http://jabber.org/protocol/pubsub#event'>"
      "    <items node='topic'>"
      "      <item id='key0'>"
      "        <value0b/>"
      "      </item>"
      "      <item id='key1'>"
      "        <value1b/>"
      "      </item>"
      "    </items>"
      "  </event>"
      "</message>";

  xmpp_client->HandleStanza(buzz::XmlElement::ForStr(items_message));
  ASSERT_EQ(4U, listener->items.size());
  EXPECT_EQ("key0", listener->items[2].itemid);
  EXPECT_EQ("<eve:value0b"
            " xmlns:eve=\"http://jabber.org/protocol/pubsub#event\"/>",
            listener->items[2].payload);
  EXPECT_EQ("key1", listener->items[3].itemid);
  EXPECT_EQ("<eve:value1b"
            " xmlns:eve=\"http://jabber.org/protocol/pubsub#event\"/>",
            listener->items[3].payload);
}

TEST_F(PubSubClientTest, TestRequestError) {
  std::string result_iq =
      "<iq xmlns='jabber:client' id='0' type='error' from='room@domain.com'>"
      "  <error type='auth'>"
      "    <forbidden xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>"
      "  </error>"
      "</iq>";

  client->RequestItems();
  xmpp_client->HandleStanza(buzz::XmlElement::ForStr(result_iq));
  EXPECT_EQ(1, listener->error_count);
}

TEST_F(PubSubClientTest, TestPublish) {
  buzz::XmlElement* payload =
      new buzz::XmlElement(buzz::QName(buzz::NS_PUBSUB, "value"));

  std::string task_id;
  client->PublishItem(itemid, payload, &task_id);

  std::string expected_iq =
      "<cli:iq type=\"set\" to=\"room@domain.com\" id=\"0\" "
        "xmlns:cli=\"jabber:client\">"
        "<pubsub xmlns=\"http://jabber.org/protocol/pubsub\">"
          "<publish node=\"topic\">"
            "<item id=\"key\">"
              "<value/>"
            "</item>"
          "</publish>"
        "</pubsub>"
      "</cli:iq>";

  ASSERT_EQ(1U, xmpp_client->sent_stanzas().size());
  EXPECT_EQ(expected_iq, xmpp_client->sent_stanzas()[0]->Str());

  std::string result_iq =
      "<iq xmlns='jabber:client' id='0' type='result' from='room@domain.com'/>";

  xmpp_client->HandleStanza(buzz::XmlElement::ForStr(result_iq));
  EXPECT_EQ(task_id, listener->result_task_id);
}

TEST_F(PubSubClientTest, TestPublishError) {
  buzz::XmlElement* payload =
      new buzz::XmlElement(buzz::QName(buzz::NS_PUBSUB, "value"));

  std::string task_id;
  client->PublishItem(itemid, payload, &task_id);

  std::string result_iq =
      "<iq xmlns='jabber:client' id='0' type='error' from='room@domain.com'>"
      "  <error type='auth'>"
      "    <forbidden xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>"
      "  </error>"
      "</iq>";

  xmpp_client->HandleStanza(buzz::XmlElement::ForStr(result_iq));
  EXPECT_EQ(1, listener->error_count);
  EXPECT_EQ(task_id, listener->error_task_id);
}

TEST_F(PubSubClientTest, TestRetract) {
  std::string task_id;
  client->RetractItem(itemid, &task_id);

  std::string expected_iq =
      "<cli:iq type=\"set\" to=\"room@domain.com\" id=\"0\" "
        "xmlns:cli=\"jabber:client\">"
        "<pubsub xmlns=\"http://jabber.org/protocol/pubsub\">"
          "<retract node=\"topic\" notify=\"true\">"
            "<item id=\"key\"/>"
          "</retract>"
        "</pubsub>"
      "</cli:iq>";

  ASSERT_EQ(1U, xmpp_client->sent_stanzas().size());
  EXPECT_EQ(expected_iq, xmpp_client->sent_stanzas()[0]->Str());

  std::string result_iq =
      "<iq xmlns='jabber:client' id='0' type='result' from='room@domain.com'/>";

  xmpp_client->HandleStanza(buzz::XmlElement::ForStr(result_iq));
  EXPECT_EQ(task_id, listener->result_task_id);
}

TEST_F(PubSubClientTest, TestRetractError) {
  std::string task_id;
  client->RetractItem(itemid, &task_id);

  std::string result_iq =
      "<iq xmlns='jabber:client' id='0' type='error' from='room@domain.com'>"
      "  <error type='auth'>"
      "    <forbidden xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>"
      "  </error>"
      "</iq>";

  xmpp_client->HandleStanza(buzz::XmlElement::ForStr(result_iq));
  EXPECT_EQ(1, listener->error_count);
  EXPECT_EQ(task_id, listener->error_task_id);
}