OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright 2011 The WebRTC Project Authors. All rights reserved. |
| 3 * |
| 4 * Use of this source code is governed by a BSD-style license |
| 5 * that can be found in the LICENSE file in the root of the source |
| 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ |
| 10 |
| 11 #include <memory> |
| 12 #include <string> |
| 13 |
| 14 #include "webrtc/libjingle/xmllite/qname.h" |
| 15 #include "webrtc/libjingle/xmllite/xmlelement.h" |
| 16 #include "webrtc/libjingle/xmpp/constants.h" |
| 17 #include "webrtc/libjingle/xmpp/fakexmppclient.h" |
| 18 #include "webrtc/libjingle/xmpp/iqtask.h" |
| 19 #include "webrtc/libjingle/xmpp/jid.h" |
| 20 #include "webrtc/libjingle/xmpp/pubsubtasks.h" |
| 21 #include "webrtc/base/faketaskrunner.h" |
| 22 #include "webrtc/base/gunit.h" |
| 23 #include "webrtc/base/sigslot.h" |
| 24 |
| 25 struct HandledPubSubItem { |
| 26 std::string itemid; |
| 27 std::string payload; |
| 28 }; |
| 29 |
| 30 class TestPubSubTasksListener : public sigslot::has_slots<> { |
| 31 public: |
| 32 TestPubSubTasksListener() : result_count(0), error_count(0) {} |
| 33 |
| 34 void OnReceiveUpdate(buzz::PubSubReceiveTask* task, |
| 35 const std::vector<buzz::PubSubItem>& items) { |
| 36 OnItems(items); |
| 37 } |
| 38 |
| 39 void OnRequestResult(buzz::PubSubRequestTask* task, |
| 40 const std::vector<buzz::PubSubItem>& items) { |
| 41 OnItems(items); |
| 42 } |
| 43 |
| 44 void OnItems(const std::vector<buzz::PubSubItem>& items) { |
| 45 for (std::vector<buzz::PubSubItem>::const_iterator item = items.begin(); |
| 46 item != items.end(); ++item) { |
| 47 HandledPubSubItem handled_item; |
| 48 handled_item.itemid = item->itemid; |
| 49 if (item->elem->FirstElement() != NULL) { |
| 50 handled_item.payload = item->elem->FirstElement()->Str(); |
| 51 } |
| 52 this->items.push_back(handled_item); |
| 53 } |
| 54 } |
| 55 |
| 56 void OnPublishResult(buzz::PubSubPublishTask* task) { |
| 57 ++result_count; |
| 58 } |
| 59 |
| 60 void OnRetractResult(buzz::PubSubRetractTask* task) { |
| 61 ++result_count; |
| 62 } |
| 63 |
| 64 void OnError(buzz::IqTask* task, const buzz::XmlElement* stanza) { |
| 65 ++error_count; |
| 66 } |
| 67 |
| 68 std::vector<HandledPubSubItem> items; |
| 69 int result_count; |
| 70 int error_count; |
| 71 }; |
| 72 |
| 73 class PubSubTasksTest : public testing::Test { |
| 74 public: |
| 75 PubSubTasksTest() : |
| 76 pubsubjid("room@domain.com"), |
| 77 node("topic"), |
| 78 itemid("key") { |
| 79 runner.reset(new rtc::FakeTaskRunner()); |
| 80 client = new buzz::FakeXmppClient(runner.get()); |
| 81 listener.reset(new TestPubSubTasksListener()); |
| 82 } |
| 83 |
| 84 std::unique_ptr<rtc::FakeTaskRunner> runner; |
| 85 // Client deleted by deleting runner. |
| 86 buzz::FakeXmppClient* client; |
| 87 std::unique_ptr<TestPubSubTasksListener> listener; |
| 88 buzz::Jid pubsubjid; |
| 89 std::string node; |
| 90 std::string itemid; |
| 91 }; |
| 92 |
| 93 TEST_F(PubSubTasksTest, TestRequest) { |
| 94 buzz::PubSubRequestTask* task = |
| 95 new buzz::PubSubRequestTask(client, pubsubjid, node); |
| 96 task->SignalResult.connect( |
| 97 listener.get(), &TestPubSubTasksListener::OnRequestResult); |
| 98 task->Start(); |
| 99 |
| 100 std::string expected_iq = |
| 101 "<cli:iq type=\"get\" to=\"room@domain.com\" id=\"0\" " |
| 102 "xmlns:cli=\"jabber:client\">" |
| 103 "<pub:pubsub xmlns:pub=\"http://jabber.org/protocol/pubsub\">" |
| 104 "<pub:items node=\"topic\"/>" |
| 105 "</pub:pubsub>" |
| 106 "</cli:iq>"; |
| 107 |
| 108 ASSERT_EQ(1U, client->sent_stanzas().size()); |
| 109 EXPECT_EQ(expected_iq, client->sent_stanzas()[0]->Str()); |
| 110 |
| 111 std::string result_iq = |
| 112 "<iq xmlns='jabber:client' id='0' type='result' from='room@domain.com'>" |
| 113 " <pubsub xmlns='http://jabber.org/protocol/pubsub'>" |
| 114 " <items node='topic'>" |
| 115 " <item id='key0'>" |
| 116 " <value0/>" |
| 117 " </item>" |
| 118 " <item id='key1'>" |
| 119 " <value1/>" |
| 120 " </item>" |
| 121 " </items>" |
| 122 " </pubsub>" |
| 123 "</iq>"; |
| 124 |
| 125 client->HandleStanza(buzz::XmlElement::ForStr(result_iq)); |
| 126 |
| 127 ASSERT_EQ(2U, listener->items.size()); |
| 128 EXPECT_EQ("key0", listener->items[0].itemid); |
| 129 EXPECT_EQ("<pub:value0 xmlns:pub=\"http://jabber.org/protocol/pubsub\"/>", |
| 130 listener->items[0].payload); |
| 131 EXPECT_EQ("key1", listener->items[1].itemid); |
| 132 EXPECT_EQ("<pub:value1 xmlns:pub=\"http://jabber.org/protocol/pubsub\"/>", |
| 133 listener->items[1].payload); |
| 134 } |
| 135 |
| 136 TEST_F(PubSubTasksTest, TestRequestError) { |
| 137 std::string result_iq = |
| 138 "<iq xmlns='jabber:client' id='0' type='error' from='room@domain.com'>" |
| 139 " <error type='auth'>" |
| 140 " <forbidden xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>" |
| 141 " </error>" |
| 142 "</iq>"; |
| 143 |
| 144 buzz::PubSubRequestTask* task = |
| 145 new buzz::PubSubRequestTask(client, pubsubjid, node); |
| 146 task->SignalResult.connect( |
| 147 listener.get(), &TestPubSubTasksListener::OnRequestResult); |
| 148 task->SignalError.connect( |
| 149 listener.get(), &TestPubSubTasksListener::OnError); |
| 150 task->Start(); |
| 151 client->HandleStanza(buzz::XmlElement::ForStr(result_iq)); |
| 152 |
| 153 EXPECT_EQ(0, listener->result_count); |
| 154 EXPECT_EQ(1, listener->error_count); |
| 155 } |
| 156 |
| 157 TEST_F(PubSubTasksTest, TestReceive) { |
| 158 std::string items_message = |
| 159 "<message xmlns='jabber:client' from='room@domain.com'>" |
| 160 " <event xmlns='http://jabber.org/protocol/pubsub#event'>" |
| 161 " <items node='topic'>" |
| 162 " <item id='key0'>" |
| 163 " <value0/>" |
| 164 " </item>" |
| 165 " <item id='key1'>" |
| 166 " <value1/>" |
| 167 " </item>" |
| 168 " </items>" |
| 169 " </event>" |
| 170 "</message>"; |
| 171 |
| 172 buzz::PubSubReceiveTask* task = |
| 173 new buzz::PubSubReceiveTask(client, pubsubjid, node); |
| 174 task->SignalUpdate.connect( |
| 175 listener.get(), &TestPubSubTasksListener::OnReceiveUpdate); |
| 176 task->Start(); |
| 177 client->HandleStanza(buzz::XmlElement::ForStr(items_message)); |
| 178 |
| 179 ASSERT_EQ(2U, listener->items.size()); |
| 180 EXPECT_EQ("key0", listener->items[0].itemid); |
| 181 EXPECT_EQ( |
| 182 "<eve:value0 xmlns:eve=\"http://jabber.org/protocol/pubsub#event\"/>", |
| 183 listener->items[0].payload); |
| 184 EXPECT_EQ("key1", listener->items[1].itemid); |
| 185 EXPECT_EQ( |
| 186 "<eve:value1 xmlns:eve=\"http://jabber.org/protocol/pubsub#event\"/>", |
| 187 listener->items[1].payload); |
| 188 } |
| 189 |
| 190 TEST_F(PubSubTasksTest, TestPublish) { |
| 191 buzz::XmlElement* payload = |
| 192 new buzz::XmlElement(buzz::QName(buzz::NS_PUBSUB, "value")); |
| 193 std::string expected_iq = |
| 194 "<cli:iq type=\"set\" to=\"room@domain.com\" id=\"0\" " |
| 195 "xmlns:cli=\"jabber:client\">" |
| 196 "<pubsub xmlns=\"http://jabber.org/protocol/pubsub\">" |
| 197 "<publish node=\"topic\">" |
| 198 "<item id=\"key\">" |
| 199 "<value/>" |
| 200 "</item>" |
| 201 "</publish>" |
| 202 "</pubsub>" |
| 203 "</cli:iq>"; |
| 204 |
| 205 std::vector<buzz::XmlElement*> children; |
| 206 children.push_back(payload); |
| 207 buzz::PubSubPublishTask* task = |
| 208 new buzz::PubSubPublishTask(client, pubsubjid, node, itemid, children); |
| 209 task->SignalResult.connect( |
| 210 listener.get(), &TestPubSubTasksListener::OnPublishResult); |
| 211 task->Start(); |
| 212 |
| 213 ASSERT_EQ(1U, client->sent_stanzas().size()); |
| 214 EXPECT_EQ(expected_iq, client->sent_stanzas()[0]->Str()); |
| 215 |
| 216 std::string result_iq = |
| 217 "<iq xmlns='jabber:client' id='0' type='result' from='room@domain.com'/>"; |
| 218 |
| 219 client->HandleStanza(buzz::XmlElement::ForStr(result_iq)); |
| 220 |
| 221 EXPECT_EQ(1, listener->result_count); |
| 222 EXPECT_EQ(0, listener->error_count); |
| 223 } |
| 224 |
| 225 TEST_F(PubSubTasksTest, TestPublishError) { |
| 226 buzz::XmlElement* payload = |
| 227 new buzz::XmlElement(buzz::QName(buzz::NS_PUBSUB, "value")); |
| 228 |
| 229 std::vector<buzz::XmlElement*> children; |
| 230 children.push_back(payload); |
| 231 buzz::PubSubPublishTask* task = |
| 232 new buzz::PubSubPublishTask(client, pubsubjid, node, itemid, children); |
| 233 task->SignalResult.connect( |
| 234 listener.get(), &TestPubSubTasksListener::OnPublishResult); |
| 235 task->SignalError.connect( |
| 236 listener.get(), &TestPubSubTasksListener::OnError); |
| 237 task->Start(); |
| 238 |
| 239 std::string result_iq = |
| 240 "<iq xmlns='jabber:client' id='0' type='error' from='room@domain.com'>" |
| 241 " <error type='auth'>" |
| 242 " <forbidden xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>" |
| 243 " </error>" |
| 244 "</iq>"; |
| 245 |
| 246 client->HandleStanza(buzz::XmlElement::ForStr(result_iq)); |
| 247 |
| 248 EXPECT_EQ(0, listener->result_count); |
| 249 EXPECT_EQ(1, listener->error_count); |
| 250 } |
| 251 |
| 252 TEST_F(PubSubTasksTest, TestRetract) { |
| 253 buzz::PubSubRetractTask* task = |
| 254 new buzz::PubSubRetractTask(client, pubsubjid, node, itemid); |
| 255 task->SignalResult.connect( |
| 256 listener.get(), &TestPubSubTasksListener::OnRetractResult); |
| 257 task->SignalError.connect( |
| 258 listener.get(), &TestPubSubTasksListener::OnError); |
| 259 task->Start(); |
| 260 |
| 261 std::string expected_iq = |
| 262 "<cli:iq type=\"set\" to=\"room@domain.com\" id=\"0\" " |
| 263 "xmlns:cli=\"jabber:client\">" |
| 264 "<pubsub xmlns=\"http://jabber.org/protocol/pubsub\">" |
| 265 "<retract node=\"topic\" notify=\"true\">" |
| 266 "<item id=\"key\"/>" |
| 267 "</retract>" |
| 268 "</pubsub>" |
| 269 "</cli:iq>"; |
| 270 |
| 271 ASSERT_EQ(1U, client->sent_stanzas().size()); |
| 272 EXPECT_EQ(expected_iq, client->sent_stanzas()[0]->Str()); |
| 273 |
| 274 std::string result_iq = |
| 275 "<iq xmlns='jabber:client' id='0' type='result' from='room@domain.com'/>"; |
| 276 |
| 277 client->HandleStanza(buzz::XmlElement::ForStr(result_iq)); |
| 278 |
| 279 EXPECT_EQ(1, listener->result_count); |
| 280 EXPECT_EQ(0, listener->error_count); |
| 281 } |
OLD | NEW |