Home | History | Annotate | Download | only in xmpp
      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 <string>
     12 #include <vector>
     13 
     14 #include "webrtc/libjingle/xmllite/xmlelement.h"
     15 #include "webrtc/libjingle/xmpp/constants.h"
     16 #include "webrtc/libjingle/xmpp/fakexmppclient.h"
     17 #include "webrtc/libjingle/xmpp/mucroomdiscoverytask.h"
     18 #include "webrtc/base/faketaskrunner.h"
     19 #include "webrtc/base/gunit.h"
     20 #include "webrtc/base/sigslot.h"
     21 
     22 class MucRoomDiscoveryListener : public sigslot::has_slots<> {
     23  public:
     24   MucRoomDiscoveryListener() : error_count(0) {}
     25 
     26   void OnResult(buzz::MucRoomDiscoveryTask* task,
     27                 bool exists,
     28                 const std::string& name,
     29                 const std::string& conversation_id,
     30                 const std::set<std::string>& features,
     31                 const std::map<std::string, std::string>& extended_info) {
     32     last_exists = exists;
     33     last_name = name;
     34     last_conversation_id = conversation_id;
     35     last_features = features;
     36     last_extended_info = extended_info;
     37   }
     38 
     39   void OnError(buzz::IqTask* task,
     40                const buzz::XmlElement* error) {
     41     ++error_count;
     42   }
     43 
     44   bool last_exists;
     45   std::string last_name;
     46   std::string last_conversation_id;
     47   std::set<std::string> last_features;
     48   std::map<std::string, std::string> last_extended_info;
     49   int error_count;
     50 };
     51 
     52 class MucRoomDiscoveryTaskTest : public testing::Test {
     53  public:
     54   MucRoomDiscoveryTaskTest() :
     55       room_jid("muc-jid-ponies (at) domain.com"),
     56       room_name("ponies"),
     57       conversation_id("test_conversation_id") {
     58   }
     59 
     60   virtual void SetUp() {
     61     runner = new rtc::FakeTaskRunner();
     62     xmpp_client = new buzz::FakeXmppClient(runner);
     63     listener = new MucRoomDiscoveryListener();
     64   }
     65 
     66   virtual void TearDown() {
     67     delete listener;
     68     // delete xmpp_client;  Deleted by deleting runner.
     69     delete runner;
     70   }
     71 
     72   rtc::FakeTaskRunner* runner;
     73   buzz::FakeXmppClient* xmpp_client;
     74   MucRoomDiscoveryListener* listener;
     75   buzz::Jid room_jid;
     76   std::string room_name;
     77   std::string conversation_id;
     78 };
     79 
     80 TEST_F(MucRoomDiscoveryTaskTest, TestDiscovery) {
     81   ASSERT_EQ(0U, xmpp_client->sent_stanzas().size());
     82 
     83   buzz::MucRoomDiscoveryTask* task = new buzz::MucRoomDiscoveryTask(
     84       xmpp_client, room_jid);
     85   task->SignalResult.connect(listener, &MucRoomDiscoveryListener::OnResult);
     86   task->Start();
     87 
     88   std::string expected_iq =
     89       "<cli:iq type=\"get\" to=\"muc-jid-ponies (at) domain.com\" id=\"0\" "
     90         "xmlns:cli=\"jabber:client\">"
     91         "<info:query xmlns:info=\"http://jabber.org/protocol/disco#info\"/>"
     92       "</cli:iq>";
     93 
     94   ASSERT_EQ(1U, xmpp_client->sent_stanzas().size());
     95   EXPECT_EQ(expected_iq, xmpp_client->sent_stanzas()[0]->Str());
     96 
     97   EXPECT_EQ("", listener->last_name);
     98   EXPECT_EQ("", listener->last_conversation_id);
     99 
    100   std::string response_iq =
    101       "<iq xmlns='jabber:client'"
    102       "    from='muc-jid-ponies (at) domain.com' id='0' type='result'>"
    103       "  <info:query xmlns:info='http://jabber.org/protocol/disco#info'>"
    104       "    <info:identity name='ponies'>"
    105       "      <han:conversation-id xmlns:han='google:muc#hangout'>"
    106       "test_conversation_id</han:conversation-id>"
    107       "    </info:identity>"
    108       "    <info:feature var='feature1'/>"
    109       "    <info:feature var='feature2'/>"
    110       "    <data:x xmlns:data='jabber:x:data'>"
    111       "      <data:field var='var1' data:value='value1' />"
    112       "      <data:field var='var2' data:value='value2' />"
    113       "    </data:x>"
    114       "  </info:query>"
    115       "</iq>";
    116 
    117   xmpp_client->HandleStanza(buzz::XmlElement::ForStr(response_iq));
    118 
    119   EXPECT_EQ(true, listener->last_exists);
    120   EXPECT_EQ(room_name, listener->last_name);
    121   EXPECT_EQ(conversation_id, listener->last_conversation_id);
    122   EXPECT_EQ(2U, listener->last_features.size());
    123   EXPECT_EQ(1U, listener->last_features.count("feature1"));
    124   EXPECT_EQ(2U, listener->last_extended_info.size());
    125   EXPECT_EQ("value1", listener->last_extended_info["var1"]);
    126   EXPECT_EQ(0, listener->error_count);
    127 }
    128 
    129 TEST_F(MucRoomDiscoveryTaskTest, TestMissingName) {
    130   buzz::MucRoomDiscoveryTask* task = new buzz::MucRoomDiscoveryTask(
    131       xmpp_client, room_jid);
    132   task->SignalError.connect(listener, &MucRoomDiscoveryListener::OnError);
    133   task->Start();
    134 
    135   std::string error_iq =
    136       "<iq xmlns='jabber:client'"
    137       "    from='muc-jid-ponies (at) domain.com' id='0' type='result'>"
    138       "  <info:query xmlns:info='http://jabber.org/protocol/disco#info'>"
    139       "    <info:identity />"
    140       "  </info:query>"
    141       "</iq>";
    142   EXPECT_EQ(0, listener->error_count);
    143   xmpp_client->HandleStanza(buzz::XmlElement::ForStr(error_iq));
    144   EXPECT_EQ(0, listener->error_count);
    145 }
    146