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/jid.h" | |
19 #include "webrtc/libjingle/xmpp/pubsubclient.h" | |
20 #include "webrtc/base/faketaskrunner.h" | |
21 #include "webrtc/base/gunit.h" | |
22 #include "webrtc/base/sigslot.h" | |
23 | |
24 struct HandledPubSubItem { | |
25 std::string itemid; | |
26 std::string payload; | |
27 }; | |
28 | |
29 class TestPubSubItemsListener : public sigslot::has_slots<> { | |
30 public: | |
31 TestPubSubItemsListener() : error_count(0) {} | |
32 | |
33 void OnItems(buzz::PubSubClient*, | |
34 const std::vector<buzz::PubSubItem>& items) { | |
35 for (std::vector<buzz::PubSubItem>::const_iterator item = items.begin(); | |
36 item != items.end(); ++item) { | |
37 HandledPubSubItem handled_item; | |
38 handled_item.itemid = item->itemid; | |
39 if (item->elem->FirstElement() != NULL) { | |
40 handled_item.payload = item->elem->FirstElement()->Str(); | |
41 } | |
42 this->items.push_back(handled_item); | |
43 } | |
44 } | |
45 | |
46 void OnRequestError(buzz::PubSubClient* client, | |
47 const buzz::XmlElement* stanza) { | |
48 error_count++; | |
49 } | |
50 | |
51 void OnPublishResult(buzz::PubSubClient* client, | |
52 const std::string& task_id, | |
53 const buzz::XmlElement* item) { | |
54 result_task_id = task_id; | |
55 } | |
56 | |
57 void OnPublishError(buzz::PubSubClient* client, | |
58 const std::string& task_id, | |
59 const buzz::XmlElement* item, | |
60 const buzz::XmlElement* stanza) { | |
61 error_count++; | |
62 error_task_id = task_id; | |
63 } | |
64 | |
65 void OnRetractResult(buzz::PubSubClient* client, | |
66 const std::string& task_id) { | |
67 result_task_id = task_id; | |
68 } | |
69 | |
70 void OnRetractError(buzz::PubSubClient* client, | |
71 const std::string& task_id, | |
72 const buzz::XmlElement* stanza) { | |
73 error_count++; | |
74 error_task_id = task_id; | |
75 } | |
76 | |
77 std::vector<HandledPubSubItem> items; | |
78 int error_count; | |
79 std::string error_task_id; | |
80 std::string result_task_id; | |
81 }; | |
82 | |
83 class PubSubClientTest : public testing::Test { | |
84 public: | |
85 PubSubClientTest() : | |
86 pubsubjid("room@domain.com"), | |
87 node("topic"), | |
88 itemid("key") { | |
89 runner.reset(new rtc::FakeTaskRunner()); | |
90 xmpp_client = new buzz::FakeXmppClient(runner.get()); | |
91 client.reset(new buzz::PubSubClient(xmpp_client, pubsubjid, node)); | |
92 listener.reset(new TestPubSubItemsListener()); | |
93 client->SignalItems.connect( | |
94 listener.get(), &TestPubSubItemsListener::OnItems); | |
95 client->SignalRequestError.connect( | |
96 listener.get(), &TestPubSubItemsListener::OnRequestError); | |
97 client->SignalPublishResult.connect( | |
98 listener.get(), &TestPubSubItemsListener::OnPublishResult); | |
99 client->SignalPublishError.connect( | |
100 listener.get(), &TestPubSubItemsListener::OnPublishError); | |
101 client->SignalRetractResult.connect( | |
102 listener.get(), &TestPubSubItemsListener::OnRetractResult); | |
103 client->SignalRetractError.connect( | |
104 listener.get(), &TestPubSubItemsListener::OnRetractError); | |
105 } | |
106 | |
107 std::unique_ptr<rtc::FakeTaskRunner> runner; | |
108 // xmpp_client deleted by deleting runner. | |
109 buzz::FakeXmppClient* xmpp_client; | |
110 std::unique_ptr<buzz::PubSubClient> client; | |
111 std::unique_ptr<TestPubSubItemsListener> listener; | |
112 buzz::Jid pubsubjid; | |
113 std::string node; | |
114 std::string itemid; | |
115 }; | |
116 | |
117 TEST_F(PubSubClientTest, TestRequest) { | |
118 client->RequestItems(); | |
119 | |
120 std::string expected_iq = | |
121 "<cli:iq type=\"get\" to=\"room@domain.com\" id=\"0\" " | |
122 "xmlns:cli=\"jabber:client\">" | |
123 "<pub:pubsub xmlns:pub=\"http://jabber.org/protocol/pubsub\">" | |
124 "<pub:items node=\"topic\"/>" | |
125 "</pub:pubsub>" | |
126 "</cli:iq>"; | |
127 | |
128 ASSERT_EQ(1U, xmpp_client->sent_stanzas().size()); | |
129 EXPECT_EQ(expected_iq, xmpp_client->sent_stanzas()[0]->Str()); | |
130 | |
131 std::string result_iq = | |
132 "<iq xmlns='jabber:client' id='0' type='result' from='room@domain.com'>" | |
133 " <pubsub xmlns='http://jabber.org/protocol/pubsub'>" | |
134 " <items node='topic'>" | |
135 " <item id='key0'>" | |
136 " <value0a/>" | |
137 " </item>" | |
138 " <item id='key1'>" | |
139 " <value1a/>" | |
140 " </item>" | |
141 " </items>" | |
142 " </pubsub>" | |
143 "</iq>"; | |
144 | |
145 xmpp_client->HandleStanza(buzz::XmlElement::ForStr(result_iq)); | |
146 ASSERT_EQ(2U, listener->items.size()); | |
147 EXPECT_EQ("key0", listener->items[0].itemid); | |
148 EXPECT_EQ("<pub:value0a xmlns:pub=\"http://jabber.org/protocol/pubsub\"/>", | |
149 listener->items[0].payload); | |
150 EXPECT_EQ("key1", listener->items[1].itemid); | |
151 EXPECT_EQ("<pub:value1a xmlns:pub=\"http://jabber.org/protocol/pubsub\"/>", | |
152 listener->items[1].payload); | |
153 | |
154 std::string items_message = | |
155 "<message xmlns='jabber:client' from='room@domain.com'>" | |
156 " <event xmlns='http://jabber.org/protocol/pubsub#event'>" | |
157 " <items node='topic'>" | |
158 " <item id='key0'>" | |
159 " <value0b/>" | |
160 " </item>" | |
161 " <item id='key1'>" | |
162 " <value1b/>" | |
163 " </item>" | |
164 " </items>" | |
165 " </event>" | |
166 "</message>"; | |
167 | |
168 xmpp_client->HandleStanza(buzz::XmlElement::ForStr(items_message)); | |
169 ASSERT_EQ(4U, listener->items.size()); | |
170 EXPECT_EQ("key0", listener->items[2].itemid); | |
171 EXPECT_EQ("<eve:value0b" | |
172 " xmlns:eve=\"http://jabber.org/protocol/pubsub#event\"/>", | |
173 listener->items[2].payload); | |
174 EXPECT_EQ("key1", listener->items[3].itemid); | |
175 EXPECT_EQ("<eve:value1b" | |
176 " xmlns:eve=\"http://jabber.org/protocol/pubsub#event\"/>", | |
177 listener->items[3].payload); | |
178 } | |
179 | |
180 TEST_F(PubSubClientTest, TestRequestError) { | |
181 std::string result_iq = | |
182 "<iq xmlns='jabber:client' id='0' type='error' from='room@domain.com'>" | |
183 " <error type='auth'>" | |
184 " <forbidden xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>" | |
185 " </error>" | |
186 "</iq>"; | |
187 | |
188 client->RequestItems(); | |
189 xmpp_client->HandleStanza(buzz::XmlElement::ForStr(result_iq)); | |
190 EXPECT_EQ(1, listener->error_count); | |
191 } | |
192 | |
193 TEST_F(PubSubClientTest, TestPublish) { | |
194 buzz::XmlElement* payload = | |
195 new buzz::XmlElement(buzz::QName(buzz::NS_PUBSUB, "value")); | |
196 | |
197 std::string task_id; | |
198 client->PublishItem(itemid, payload, &task_id); | |
199 | |
200 std::string expected_iq = | |
201 "<cli:iq type=\"set\" to=\"room@domain.com\" id=\"0\" " | |
202 "xmlns:cli=\"jabber:client\">" | |
203 "<pubsub xmlns=\"http://jabber.org/protocol/pubsub\">" | |
204 "<publish node=\"topic\">" | |
205 "<item id=\"key\">" | |
206 "<value/>" | |
207 "</item>" | |
208 "</publish>" | |
209 "</pubsub>" | |
210 "</cli:iq>"; | |
211 | |
212 ASSERT_EQ(1U, xmpp_client->sent_stanzas().size()); | |
213 EXPECT_EQ(expected_iq, xmpp_client->sent_stanzas()[0]->Str()); | |
214 | |
215 std::string result_iq = | |
216 "<iq xmlns='jabber:client' id='0' type='result' from='room@domain.com'/>"; | |
217 | |
218 xmpp_client->HandleStanza(buzz::XmlElement::ForStr(result_iq)); | |
219 EXPECT_EQ(task_id, listener->result_task_id); | |
220 } | |
221 | |
222 TEST_F(PubSubClientTest, TestPublishError) { | |
223 buzz::XmlElement* payload = | |
224 new buzz::XmlElement(buzz::QName(buzz::NS_PUBSUB, "value")); | |
225 | |
226 std::string task_id; | |
227 client->PublishItem(itemid, payload, &task_id); | |
228 | |
229 std::string result_iq = | |
230 "<iq xmlns='jabber:client' id='0' type='error' from='room@domain.com'>" | |
231 " <error type='auth'>" | |
232 " <forbidden xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>" | |
233 " </error>" | |
234 "</iq>"; | |
235 | |
236 xmpp_client->HandleStanza(buzz::XmlElement::ForStr(result_iq)); | |
237 EXPECT_EQ(1, listener->error_count); | |
238 EXPECT_EQ(task_id, listener->error_task_id); | |
239 } | |
240 | |
241 TEST_F(PubSubClientTest, TestRetract) { | |
242 std::string task_id; | |
243 client->RetractItem(itemid, &task_id); | |
244 | |
245 std::string expected_iq = | |
246 "<cli:iq type=\"set\" to=\"room@domain.com\" id=\"0\" " | |
247 "xmlns:cli=\"jabber:client\">" | |
248 "<pubsub xmlns=\"http://jabber.org/protocol/pubsub\">" | |
249 "<retract node=\"topic\" notify=\"true\">" | |
250 "<item id=\"key\"/>" | |
251 "</retract>" | |
252 "</pubsub>" | |
253 "</cli:iq>"; | |
254 | |
255 ASSERT_EQ(1U, xmpp_client->sent_stanzas().size()); | |
256 EXPECT_EQ(expected_iq, xmpp_client->sent_stanzas()[0]->Str()); | |
257 | |
258 std::string result_iq = | |
259 "<iq xmlns='jabber:client' id='0' type='result' from='room@domain.com'/>"; | |
260 | |
261 xmpp_client->HandleStanza(buzz::XmlElement::ForStr(result_iq)); | |
262 EXPECT_EQ(task_id, listener->result_task_id); | |
263 } | |
264 | |
265 TEST_F(PubSubClientTest, TestRetractError) { | |
266 std::string task_id; | |
267 client->RetractItem(itemid, &task_id); | |
268 | |
269 std::string result_iq = | |
270 "<iq xmlns='jabber:client' id='0' type='error' from='room@domain.com'>" | |
271 " <error type='auth'>" | |
272 " <forbidden xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>" | |
273 " </error>" | |
274 "</iq>"; | |
275 | |
276 xmpp_client->HandleStanza(buzz::XmlElement::ForStr(result_iq)); | |
277 EXPECT_EQ(1, listener->error_count); | |
278 EXPECT_EQ(task_id, listener->error_task_id); | |
279 } | |
OLD | NEW |