You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

271 lines
8.7 KiB

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