1/* 2 * libjingle 3 * Copyright 2011, Google Inc. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, 9 * this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright notice, 11 * this list of conditions and the following disclaimer in the documentation 12 * and/or other materials provided with the distribution. 13 * 3. The name of the author may not be used to endorse or promote products 14 * derived from this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28#include <string> 29#include <vector> 30 31#include "webrtc/libjingle/xmllite/xmlelement.h" 32#include "talk/xmpp/constants.h" 33#include "talk/xmpp/fakexmppclient.h" 34#include "talk/xmpp/mucroomdiscoverytask.h" 35#include "webrtc/base/faketaskrunner.h" 36#include "webrtc/base/gunit.h" 37#include "webrtc/base/sigslot.h" 38 39class MucRoomDiscoveryListener : public sigslot::has_slots<> { 40 public: 41 MucRoomDiscoveryListener() : error_count(0) {} 42 43 void OnResult(buzz::MucRoomDiscoveryTask* task, 44 bool exists, 45 const std::string& name, 46 const std::string& conversation_id, 47 const std::set<std::string>& features, 48 const std::map<std::string, std::string>& extended_info) { 49 last_exists = exists; 50 last_name = name; 51 last_conversation_id = conversation_id; 52 last_features = features; 53 last_extended_info = extended_info; 54 } 55 56 void OnError(buzz::IqTask* task, 57 const buzz::XmlElement* error) { 58 ++error_count; 59 } 60 61 bool last_exists; 62 std::string last_name; 63 std::string last_conversation_id; 64 std::set<std::string> last_features; 65 std::map<std::string, std::string> last_extended_info; 66 int error_count; 67}; 68 69class MucRoomDiscoveryTaskTest : public testing::Test { 70 public: 71 MucRoomDiscoveryTaskTest() : 72 room_jid("muc-jid-ponies@domain.com"), 73 room_name("ponies"), 74 conversation_id("test_conversation_id") { 75 } 76 77 virtual void SetUp() { 78 runner = new rtc::FakeTaskRunner(); 79 xmpp_client = new buzz::FakeXmppClient(runner); 80 listener = new MucRoomDiscoveryListener(); 81 } 82 83 virtual void TearDown() { 84 delete listener; 85 // delete xmpp_client; Deleted by deleting runner. 86 delete runner; 87 } 88 89 rtc::FakeTaskRunner* runner; 90 buzz::FakeXmppClient* xmpp_client; 91 MucRoomDiscoveryListener* listener; 92 buzz::Jid room_jid; 93 std::string room_name; 94 std::string conversation_id; 95}; 96 97TEST_F(MucRoomDiscoveryTaskTest, TestDiscovery) { 98 ASSERT_EQ(0U, xmpp_client->sent_stanzas().size()); 99 100 buzz::MucRoomDiscoveryTask* task = new buzz::MucRoomDiscoveryTask( 101 xmpp_client, room_jid); 102 task->SignalResult.connect(listener, &MucRoomDiscoveryListener::OnResult); 103 task->Start(); 104 105 std::string expected_iq = 106 "<cli:iq type=\"get\" to=\"muc-jid-ponies@domain.com\" id=\"0\" " 107 "xmlns:cli=\"jabber:client\">" 108 "<info:query xmlns:info=\"http://jabber.org/protocol/disco#info\"/>" 109 "</cli:iq>"; 110 111 ASSERT_EQ(1U, xmpp_client->sent_stanzas().size()); 112 EXPECT_EQ(expected_iq, xmpp_client->sent_stanzas()[0]->Str()); 113 114 EXPECT_EQ("", listener->last_name); 115 EXPECT_EQ("", listener->last_conversation_id); 116 117 std::string response_iq = 118 "<iq xmlns='jabber:client'" 119 " from='muc-jid-ponies@domain.com' id='0' type='result'>" 120 " <info:query xmlns:info='http://jabber.org/protocol/disco#info'>" 121 " <info:identity name='ponies'>" 122 " <han:conversation-id xmlns:han='google:muc#hangout'>" 123 "test_conversation_id</han:conversation-id>" 124 " </info:identity>" 125 " <info:feature var='feature1'/>" 126 " <info:feature var='feature2'/>" 127 " <data:x xmlns:data='jabber:x:data'>" 128 " <data:field var='var1' data:value='value1' />" 129 " <data:field var='var2' data:value='value2' />" 130 " </data:x>" 131 " </info:query>" 132 "</iq>"; 133 134 xmpp_client->HandleStanza(buzz::XmlElement::ForStr(response_iq)); 135 136 EXPECT_EQ(true, listener->last_exists); 137 EXPECT_EQ(room_name, listener->last_name); 138 EXPECT_EQ(conversation_id, listener->last_conversation_id); 139 EXPECT_EQ(2U, listener->last_features.size()); 140 EXPECT_EQ(1U, listener->last_features.count("feature1")); 141 EXPECT_EQ(2U, listener->last_extended_info.size()); 142 EXPECT_EQ("value1", listener->last_extended_info["var1"]); 143 EXPECT_EQ(0, listener->error_count); 144} 145 146TEST_F(MucRoomDiscoveryTaskTest, TestMissingName) { 147 buzz::MucRoomDiscoveryTask* task = new buzz::MucRoomDiscoveryTask( 148 xmpp_client, room_jid); 149 task->SignalError.connect(listener, &MucRoomDiscoveryListener::OnError); 150 task->Start(); 151 152 std::string error_iq = 153 "<iq xmlns='jabber:client'" 154 " from='muc-jid-ponies@domain.com' id='0' type='result'>" 155 " <info:query xmlns:info='http://jabber.org/protocol/disco#info'>" 156 " <info:identity />" 157 " </info:query>" 158 "</iq>"; 159 EXPECT_EQ(0, listener->error_count); 160 xmpp_client->HandleStanza(buzz::XmlElement::ForStr(error_iq)); 161 EXPECT_EQ(0, listener->error_count); 162} 163