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/mucroomlookuptask.h"
     18 #include "webrtc/base/faketaskrunner.h"
     19 #include "webrtc/base/gunit.h"
     20 #include "webrtc/base/sigslot.h"
     21 
     22 class MucRoomLookupListener : public sigslot::has_slots<> {
     23  public:
     24   MucRoomLookupListener() : error_count(0) {}
     25 
     26   void OnResult(buzz::MucRoomLookupTask* task,
     27                 const buzz::MucRoomInfo& room) {
     28     last_room = room;
     29   }
     30 
     31   void OnError(buzz::IqTask* task,
     32                const buzz::XmlElement* error) {
     33     ++error_count;
     34   }
     35 
     36   buzz::MucRoomInfo last_room;
     37   int error_count;
     38 };
     39 
     40 class MucRoomLookupTaskTest : public testing::Test {
     41  public:
     42   MucRoomLookupTaskTest() :
     43       lookup_server_jid("lookup (at) domain.com"),
     44       room_jid("muc-jid-ponies (at) domain.com"),
     45       room_name("ponies"),
     46       room_domain("domain.com"),
     47       room_full_name("ponies (at) domain.com"),
     48       hangout_id("some_hangout_id") {
     49   }
     50 
     51   virtual void SetUp() {
     52     runner = new rtc::FakeTaskRunner();
     53     xmpp_client = new buzz::FakeXmppClient(runner);
     54     listener = new MucRoomLookupListener();
     55   }
     56 
     57   virtual void TearDown() {
     58     delete listener;
     59     // delete xmpp_client;  Deleted by deleting runner.
     60     delete runner;
     61   }
     62 
     63   rtc::FakeTaskRunner* runner;
     64   buzz::FakeXmppClient* xmpp_client;
     65   MucRoomLookupListener* listener;
     66   buzz::Jid lookup_server_jid;
     67   buzz::Jid room_jid;
     68   std::string room_name;
     69   std::string room_domain;
     70   std::string room_full_name;
     71   std::string hangout_id;
     72 };
     73 
     74 TEST_F(MucRoomLookupTaskTest, TestLookupName) {
     75   ASSERT_EQ(0U, xmpp_client->sent_stanzas().size());
     76 
     77   buzz::MucRoomLookupTask* task =
     78       buzz::MucRoomLookupTask::CreateLookupTaskForRoomName(
     79           xmpp_client, lookup_server_jid, room_name, room_domain);
     80   task->SignalResult.connect(listener, &MucRoomLookupListener::OnResult);
     81   task->Start();
     82 
     83   std::string expected_iq =
     84       "<cli:iq type=\"set\" to=\"lookup (at) domain.com\" id=\"0\" "
     85         "xmlns:cli=\"jabber:client\">"
     86         "<query xmlns=\"jabber:iq:search\">"
     87           "<room-name>ponies</room-name>"
     88           "<room-domain>domain.com</room-domain>"
     89         "</query>"
     90       "</cli:iq>";
     91 
     92   ASSERT_EQ(1U, xmpp_client->sent_stanzas().size());
     93   EXPECT_EQ(expected_iq, xmpp_client->sent_stanzas()[0]->Str());
     94 
     95   EXPECT_EQ("", listener->last_room.name);
     96 
     97   std::string response_iq =
     98       "<iq xmlns='jabber:client' from='lookup (at) domain.com' id='0' type='result'>"
     99       "  <query xmlns='jabber:iq:search'>"
    100       "    <item jid='muc-jid-ponies (at) domain.com'>"
    101       "      <room-name>ponies</room-name>"
    102       "      <room-domain>domain.com</room-domain>"
    103       "    </item>"
    104       "  </query>"
    105       "</iq>";
    106 
    107   xmpp_client->HandleStanza(buzz::XmlElement::ForStr(response_iq));
    108 
    109   EXPECT_EQ(room_name, listener->last_room.name);
    110   EXPECT_EQ(room_domain, listener->last_room.domain);
    111   EXPECT_EQ(room_jid, listener->last_room.jid);
    112   EXPECT_EQ(room_full_name, listener->last_room.full_name());
    113   EXPECT_EQ(0, listener->error_count);
    114 }
    115 
    116 TEST_F(MucRoomLookupTaskTest, TestLookupHangoutId) {
    117   ASSERT_EQ(0U, xmpp_client->sent_stanzas().size());
    118 
    119   buzz::MucRoomLookupTask* task = buzz::MucRoomLookupTask::CreateLookupTaskForHangoutId(
    120       xmpp_client, lookup_server_jid, hangout_id);
    121   task->SignalResult.connect(listener, &MucRoomLookupListener::OnResult);
    122   task->Start();
    123 
    124   std::string expected_iq =
    125       "<cli:iq type=\"set\" to=\"lookup (at) domain.com\" id=\"0\" "
    126         "xmlns:cli=\"jabber:client\">"
    127         "<query xmlns=\"jabber:iq:search\">"
    128           "<hangout-id>some_hangout_id</hangout-id>"
    129         "</query>"
    130       "</cli:iq>";
    131 
    132   ASSERT_EQ(1U, xmpp_client->sent_stanzas().size());
    133   EXPECT_EQ(expected_iq, xmpp_client->sent_stanzas()[0]->Str());
    134 
    135   EXPECT_EQ("", listener->last_room.name);
    136 
    137   std::string response_iq =
    138       "<iq xmlns='jabber:client' from='lookup (at) domain.com' id='0' type='result'>"
    139       "  <query xmlns='jabber:iq:search'>"
    140       "    <item jid='muc-jid-ponies (at) domain.com'>"
    141       "      <room-name>some_hangout_id</room-name>"
    142       "      <room-domain>domain.com</room-domain>"
    143       "    </item>"
    144       "  </query>"
    145       "</iq>";
    146 
    147   xmpp_client->HandleStanza(buzz::XmlElement::ForStr(response_iq));
    148 
    149   EXPECT_EQ(hangout_id, listener->last_room.name);
    150   EXPECT_EQ(room_domain, listener->last_room.domain);
    151   EXPECT_EQ(room_jid, listener->last_room.jid);
    152   EXPECT_EQ(0, listener->error_count);
    153 }
    154 
    155 TEST_F(MucRoomLookupTaskTest, TestError) {
    156   buzz::MucRoomLookupTask* task = buzz::MucRoomLookupTask::CreateLookupTaskForRoomName(
    157       xmpp_client, lookup_server_jid, room_name, room_domain);
    158   task->SignalError.connect(listener, &MucRoomLookupListener::OnError);
    159   task->Start();
    160 
    161   std::string error_iq =
    162       "<iq xmlns='jabber:client' id='0' type='error'"
    163       "  from='lookup (at) domain.com'>"
    164       "</iq>";
    165 
    166   EXPECT_EQ(0, listener->error_count);
    167   xmpp_client->HandleStanza(buzz::XmlElement::ForStr(error_iq));
    168   EXPECT_EQ(1, listener->error_count);
    169 }
    170 
    171 TEST_F(MucRoomLookupTaskTest, TestBadJid) {
    172   buzz::MucRoomLookupTask* task = buzz::MucRoomLookupTask::CreateLookupTaskForRoomName(
    173       xmpp_client, lookup_server_jid, room_name, room_domain);
    174   task->SignalError.connect(listener, &MucRoomLookupListener::OnError);
    175   task->Start();
    176 
    177   std::string response_iq =
    178       "<iq xmlns='jabber:client' from='lookup (at) domain.com' id='0' type='result'>"
    179       "  <query xmlns='jabber:iq:search'>"
    180       "    <item/>"
    181       "  </query>"
    182       "</iq>";
    183 
    184   EXPECT_EQ(0, listener->error_count);
    185   xmpp_client->HandleStanza(buzz::XmlElement::ForStr(response_iq));
    186   EXPECT_EQ(1, listener->error_count);
    187 }
    188