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