/* * libjingle * Copyright 2004, Google Inc. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. The name of the author may not be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include #include #include #include "webrtc/libjingle/xmllite/xmlelement.h" #include "talk/xmpp/constants.h" #include "talk/xmpp/rostermodule.h" #include "talk/xmpp/util_unittest.h" #include "talk/xmpp/xmppengine.h" #include "webrtc/base/gunit.h" #include "webrtc/base/scoped_ptr.h" #define TEST_OK(x) EXPECT_EQ((x),XMPP_RETURN_OK) #define TEST_BADARGUMENT(x) EXPECT_EQ((x),XMPP_RETURN_BADARGUMENT) namespace buzz { class RosterModuleTest; static void WriteString(std::ostream& os, const std::string& str) { os<jid().Str()); os<<" available:"<available(); os<<" presence_show:"; WritePresenceShow(os, presence->presence_show()); os<<" priority:"<priority(); os<<" status:"; WriteString(os, presence->status()); os<<"]"<raw_xml()->Str(); } static void WriteContact(std::ostream& os, const XmppRosterContact* contact) { if (contact == NULL) { os<<"NULL"; return; } os<<"[Contact jid:"; WriteString(os, contact->jid().Str()); os<<" name:"; WriteString(os, contact->name()); os<<" subscription_state:"; WriteSubscriptionState(os, contact->subscription_state()); os<<" groups:["; for(size_t i=0; i < contact->GetGroupCount(); ++i) { os<<(i==0?"":", "); WriteString(os, contact->GetGroup(i)); } os<<"]]"<raw_xml()->Str(); } //! This session handler saves all calls to a string. These are events and //! data delivered form the engine to application code. class XmppTestRosterHandler : public XmppRosterHandler { public: XmppTestRosterHandler() {} virtual ~XmppTestRosterHandler() {} virtual void SubscriptionRequest(XmppRosterModule*, const Jid& requesting_jid, XmppSubscriptionRequestType type, const XmlElement* raw_xml) { ss_<<"[SubscriptionRequest Jid:" << requesting_jid.Str()<<" type:"; WriteSubscriptionRequestType(ss_, type); ss_<<"]"<Str(); } //! Some type of presence error has occured virtual void SubscriptionError(XmppRosterModule*, const Jid& from, const XmlElement* raw_xml) { ss_<<"[SubscriptionError from:"<Str(); } virtual void RosterError(XmppRosterModule*, const XmlElement* raw_xml) { ss_<<"[RosterError]"<Str(); } //! New presence information has come in //! The user is notified with the presence object directly. This info is also //! added to the store accessable from the engine. virtual void IncomingPresenceChanged(XmppRosterModule*, const XmppPresence* presence) { ss_<<"[IncomingPresenceChanged presence:"; WritePresence(ss_, presence); ss_<<"]"; } //! A contact has changed //! This indicates that the data for a contact may have changed. No //! contacts have been added or removed. virtual void ContactChanged(XmppRosterModule* roster, const XmppRosterContact* old_contact, size_t index) { ss_<<"[ContactChanged old_contact:"; WriteContact(ss_, old_contact); ss_<<" index:"<GetRosterContact(index)); ss_<<"]"; } //! A set of contacts have been added //! These contacts may have been added in response to the original roster //! request or due to a "roster push" from the server. virtual void ContactsAdded(XmppRosterModule* roster, size_t index, size_t number) { ss_<<"[ContactsAdded index:"<GetRosterContact(index+i)); } ss_<<"]"; } //! A contact has been removed //! This contact has been removed form the list. virtual void ContactRemoved(XmppRosterModule*, const XmppRosterContact* removed_contact, size_t index) { ss_<<"[ContactRemoved old_contact:"; WriteContact(ss_, removed_contact); ss_<<" index:"<AddAttr(QN_STATUS, STR_PSTN_CONFERENCE_STATUS_CONNECTING); XmlElement presence_xml(QN_PRESENCE); presence_xml.AddElement(status); rtc::scoped_ptr presence(XmppPresence::Create()); presence->set_raw_xml(&presence_xml); EXPECT_EQ(presence->connection_status(), XMPP_CONNECTION_STATUS_CONNECTING); } TEST_F(RosterModuleTest, TestOutgoingPresence) { std::stringstream dump; rtc::scoped_ptr engine(XmppEngine::Create()); XmppTestHandler handler(engine.get()); XmppTestRosterHandler roster_handler; rtc::scoped_ptr roster(XmppRosterModule::Create()); roster->set_roster_handler(&roster_handler); // Configure the roster module roster->RegisterEngine(engine.get()); // Set up callbacks engine->SetOutputHandler(&handler); engine->AddStanzaHandler(&handler); engine->SetSessionHandler(&handler); // Set up minimal login info engine->SetUser(Jid("david@my-server")); // engine->SetPassword("david"); // Do the whole login handshake RunLogin(this, engine.get(), &handler); EXPECT_EQ("", handler.OutputActivity()); // Set some presence and broadcast it TEST_OK(roster->outgoing_presence()-> set_available(XMPP_PRESENCE_AVAILABLE)); TEST_OK(roster->outgoing_presence()->set_priority(-37)); TEST_OK(roster->outgoing_presence()->set_presence_show(XMPP_PRESENCE_DND)); TEST_OK(roster->outgoing_presence()-> set_status("I'm off to the races!<>&")); TEST_OK(roster->BroadcastPresence()); EXPECT_EQ(roster_handler.StrClear(), ""); EXPECT_EQ(handler.OutputActivity(), "" "-37" "dnd" "I'm off to the races!<>&" ""); EXPECT_EQ(handler.SessionActivity(), ""); // Try some more TEST_OK(roster->outgoing_presence()-> set_available(XMPP_PRESENCE_UNAVAILABLE)); TEST_OK(roster->outgoing_presence()->set_priority(0)); TEST_OK(roster->outgoing_presence()->set_presence_show(XMPP_PRESENCE_XA)); TEST_OK(roster->outgoing_presence()->set_status("Gone fishin'")); TEST_OK(roster->BroadcastPresence()); EXPECT_EQ(roster_handler.StrClear(), ""); EXPECT_EQ(handler.OutputActivity(), "" "xa" "Gone fishin'" ""); EXPECT_EQ(handler.SessionActivity(), ""); // Okay -- we are back on TEST_OK(roster->outgoing_presence()-> set_available(XMPP_PRESENCE_AVAILABLE)); TEST_BADARGUMENT(roster->outgoing_presence()->set_priority(128)); TEST_OK(roster->outgoing_presence()-> set_presence_show(XMPP_PRESENCE_DEFAULT)); TEST_OK(roster->outgoing_presence()->set_status("Cookin' wit gas")); TEST_OK(roster->BroadcastPresence()); EXPECT_EQ(roster_handler.StrClear(), ""); EXPECT_EQ(handler.OutputActivity(), "" "Cookin' wit gas" ""); EXPECT_EQ(handler.SessionActivity(), ""); // Set it via XML XmlElement presence_input(QN_PRESENCE); presence_input.AddAttr(QN_TYPE, "unavailable"); presence_input.AddElement(new XmlElement(QN_PRIORITY)); presence_input.AddText("42", 1); presence_input.AddElement(new XmlElement(QN_STATUS)); presence_input.AddAttr(QN_XML_LANG, "es", 1); presence_input.AddText("Hola Amigos!", 1); presence_input.AddElement(new XmlElement(QN_STATUS)); presence_input.AddText("Hey there, friend!", 1); TEST_OK(roster->outgoing_presence()->set_raw_xml(&presence_input)); TEST_OK(roster->BroadcastPresence()); WritePresence(dump, roster->outgoing_presence()); EXPECT_EQ(dump.str(), "[Presence jid: available:0 presence_show:[default] " "priority:42 status:Hey there, friend!]" "" "42" "Hola Amigos!" "Hey there, friend!" ""); dump.str(""); EXPECT_EQ(roster_handler.StrClear(), ""); EXPECT_EQ(handler.OutputActivity(), "" "42" "Hola Amigos!" "Hey there, friend!" ""); EXPECT_EQ(handler.SessionActivity(), ""); // Construct a directed presence rtc::scoped_ptr directed_presence(XmppPresence::Create()); TEST_OK(directed_presence->set_available(XMPP_PRESENCE_AVAILABLE)); TEST_OK(directed_presence->set_priority(120)); TEST_OK(directed_presence->set_status("*very* available")); TEST_OK(roster->SendDirectedPresence(directed_presence.get(), Jid("myhoney@honey.net"))); EXPECT_EQ(roster_handler.StrClear(), ""); EXPECT_EQ(handler.OutputActivity(), "" "120" "*very* available" ""); EXPECT_EQ(handler.SessionActivity(), ""); } TEST_F(RosterModuleTest, TestIncomingPresence) { rtc::scoped_ptr engine(XmppEngine::Create()); XmppTestHandler handler(engine.get()); XmppTestRosterHandler roster_handler; rtc::scoped_ptr roster(XmppRosterModule::Create()); roster->set_roster_handler(&roster_handler); // Configure the roster module roster->RegisterEngine(engine.get()); // Set up callbacks engine->SetOutputHandler(&handler); engine->AddStanzaHandler(&handler); engine->SetSessionHandler(&handler); // Set up minimal login info engine->SetUser(Jid("david@my-server")); // engine->SetPassword("david"); // Do the whole login handshake RunLogin(this, engine.get(), &handler); EXPECT_EQ("", handler.OutputActivity()); // Load up with a bunch of data std::string input; input = "" "" "-10" "xa" "Off bowling" "" "" "20" "Looking for toes..." "" "" "10" "Throwing rocks" ""; TEST_OK(engine->HandleInput(input.c_str(), input.length())); EXPECT_EQ(roster_handler.StrClear(), "[IncomingPresenceChanged " "presence:[Presence jid:maude@example.net/studio available:1 " "presence_show:[default] priority:0 status:]" "]" "[IncomingPresenceChanged " "presence:[Presence jid:walter@example.net/home available:1 " "presence_show:xa priority:-10 status:Off bowling]" "" "-10" "xa" "Off bowling" "]" "[IncomingPresenceChanged " "presence:[Presence jid:walter@example.net/alley available:1 " "presence_show:[default] " "priority:20 status:Looking for toes...]" "" "20" "Looking for toes..." "]" "[IncomingPresenceChanged " "presence:[Presence jid:donny@example.net/alley available:1 " "presence_show:[default] priority:10 status:Throwing rocks]" "" "10" "Throwing rocks" "]"); EXPECT_EQ(handler.OutputActivity(), ""); handler.SessionActivity(); // Ignore the session output // Now look at the data structure we've built EXPECT_EQ(roster->GetIncomingPresenceCount(), static_cast(4)); EXPECT_EQ(roster->GetIncomingPresenceForJidCount(Jid("maude@example.net")), static_cast(1)); EXPECT_EQ(roster->GetIncomingPresenceForJidCount(Jid("walter@example.net")), static_cast(2)); const XmppPresence * presence; presence = roster->GetIncomingPresenceForJid(Jid("walter@example.net"), 1); std::stringstream dump; WritePresence(dump, presence); EXPECT_EQ(dump.str(), "[Presence jid:walter@example.net/alley available:1 " "presence_show:[default] priority:20 status:Looking for toes...]" "" "20" "Looking for toes..." ""); dump.str(""); // Maude took off... input = "" "Stealing my rug back" "-10" ""; TEST_OK(engine->HandleInput(input.c_str(), input.length())); EXPECT_EQ(roster_handler.StrClear(), "[IncomingPresenceChanged " "presence:[Presence jid:maude@example.net/studio available:0 " "presence_show:[default] priority:-10 " "status:Stealing my rug back]" "" "Stealing my rug back" "-10" "]"); EXPECT_EQ(handler.OutputActivity(), ""); handler.SessionActivity(); // Ignore the session output } TEST_F(RosterModuleTest, TestPresenceSubscription) { rtc::scoped_ptr engine(XmppEngine::Create()); XmppTestHandler handler(engine.get()); XmppTestRosterHandler roster_handler; rtc::scoped_ptr roster(XmppRosterModule::Create()); roster->set_roster_handler(&roster_handler); // Configure the roster module roster->RegisterEngine(engine.get()); // Set up callbacks engine->SetOutputHandler(&handler); engine->AddStanzaHandler(&handler); engine->SetSessionHandler(&handler); // Set up minimal login info engine->SetUser(Jid("david@my-server")); // engine->SetPassword("david"); // Do the whole login handshake RunLogin(this, engine.get(), &handler); EXPECT_EQ("", handler.OutputActivity()); // Test incoming requests std::string input; input = "" "" "" ""; TEST_OK(engine->HandleInput(input.c_str(), input.length())); EXPECT_EQ(roster_handler.StrClear(), "[SubscriptionRequest Jid:maude@example.net type:subscribe]" "" "[SubscriptionRequest Jid:maude@example.net type:unsubscribe]" "" "[SubscriptionRequest Jid:maude@example.net type:subscribed]" "" "[SubscriptionRequest Jid:maude@example.net type:unsubscribe]" ""); EXPECT_EQ(handler.OutputActivity(), ""); handler.SessionActivity(); // Ignore the session output TEST_OK(roster->RequestSubscription(Jid("maude@example.net"))); TEST_OK(roster->CancelSubscription(Jid("maude@example.net"))); TEST_OK(roster->ApproveSubscriber(Jid("maude@example.net"))); TEST_OK(roster->CancelSubscriber(Jid("maude@example.net"))); EXPECT_EQ(roster_handler.StrClear(), ""); EXPECT_EQ(handler.OutputActivity(), "" "" "" ""); EXPECT_EQ(handler.SessionActivity(), ""); } TEST_F(RosterModuleTest, TestRosterReceive) { rtc::scoped_ptr engine(XmppEngine::Create()); XmppTestHandler handler(engine.get()); XmppTestRosterHandler roster_handler; rtc::scoped_ptr roster(XmppRosterModule::Create()); roster->set_roster_handler(&roster_handler); // Configure the roster module roster->RegisterEngine(engine.get()); // Set up callbacks engine->SetOutputHandler(&handler); engine->AddStanzaHandler(&handler); engine->SetSessionHandler(&handler); // Set up minimal login info engine->SetUser(Jid("david@my-server")); // engine->SetPassword("david"); // Do the whole login handshake RunLogin(this, engine.get(), &handler); EXPECT_EQ("", handler.OutputActivity()); // Request a roster update TEST_OK(roster->RequestRosterUpdate()); EXPECT_EQ(roster_handler.StrClear(),""); EXPECT_EQ(handler.OutputActivity(), "" "" ""); EXPECT_EQ(handler.SessionActivity(), ""); // Prime the roster with a starting set std::string input = "" "" "" "Business Partners" "" "" "Friends" "Bowling Team" "Bowling League" "" "" "Friends" "Bowling Team" "Bowling League" "" "" "Business Partners" "" "" "Bowling League" "" "" ""; TEST_OK(engine->HandleInput(input.c_str(), input.length())); EXPECT_EQ(roster_handler.StrClear(), "[ContactsAdded index:0 number:5 " "0:[Contact jid:maude@example.net name:Maude Lebowski " "subscription_state:none_asked " "groups:[Business Partners]]" "" "Business Partners" " " "1:[Contact jid:walter@example.net name:Walter Sobchak " "subscription_state:both " "groups:[Friends, Bowling Team, Bowling League]]" "" "Friends" "Bowling Team" "Bowling League" " " "2:[Contact jid:donny@example.net name:Donny " "subscription_state:both " "groups:[Friends, Bowling Team, Bowling League]]" "" "Friends" "Bowling Team" "Bowling League" " " "3:[Contact jid:jeffrey@example.net name:The Big Lebowski " "subscription_state:to " "groups:[Business Partners]]" "" "Business Partners" " " "4:[Contact jid:jesus@example.net name:Jesus Quintana " "subscription_state:from groups:[Bowling League]]" "" "Bowling League" "]"); EXPECT_EQ(handler.OutputActivity(), ""); EXPECT_EQ(handler.SessionActivity(), ""); // Request that someone be added rtc::scoped_ptr contact(XmppRosterContact::Create()); TEST_OK(contact->set_jid(Jid("brandt@example.net"))); TEST_OK(contact->set_name("Brandt")); TEST_OK(contact->AddGroup("Business Partners")); TEST_OK(contact->AddGroup("Watchers")); TEST_OK(contact->AddGroup("Friends")); TEST_OK(contact->RemoveGroup("Friends")); // Maybe not... TEST_OK(roster->RequestRosterChange(contact.get())); EXPECT_EQ(roster_handler.StrClear(), ""); EXPECT_EQ(handler.OutputActivity(), "" "" "" "Business Partners" "Watchers" "" "" ""); EXPECT_EQ(handler.SessionActivity(), ""); // Get the push from the server input = "" "" "" "" "Business Partners" "Watchers" "" "" ""; TEST_OK(engine->HandleInput(input.c_str(), input.length())); EXPECT_EQ(roster_handler.StrClear(), "[ContactsAdded index:5 number:1 " "5:[Contact jid:brandt@example.net name:Brandt " "subscription_state:none " "groups:[Business Partners, Watchers]]" "" "Business Partners" "Watchers" "]"); EXPECT_EQ(handler.OutputActivity(), ""); EXPECT_EQ(handler.SessionActivity(), ""); // Get a contact update input = "" "" "" "Friends" "Bowling Team" "Bowling League" "Not wrong, just an..." "" "" ""; TEST_OK(engine->HandleInput(input.c_str(), input.length())); EXPECT_EQ(roster_handler.StrClear(), "[ContactChanged " "old_contact:[Contact jid:walter@example.net name:Walter Sobchak " "subscription_state:both " "groups:[Friends, Bowling Team, Bowling League]]" "" "Friends" "Bowling Team" "Bowling League" " " "index:1 " "new_contact:[Contact jid:walter@example.net name:Walter Sobchak " "subscription_state:both " "groups:[Friends, Bowling Team, Bowling League, " "Not wrong, just an...]]" "" "Friends" "Bowling Team" "Bowling League" "Not wrong, just an..." "]"); EXPECT_EQ(handler.OutputActivity(), ""); EXPECT_EQ(handler.SessionActivity(), ""); // Remove a contact TEST_OK(roster->RequestRosterRemove(Jid("jesus@example.net"))); EXPECT_EQ(roster_handler.StrClear(), ""); EXPECT_EQ(handler.OutputActivity(), "" "" ""); EXPECT_EQ(handler.SessionActivity(), ""); // Response from the server input = "" "" "" "" "" "" ""; TEST_OK(engine->HandleInput(input.c_str(), input.length())); EXPECT_EQ(roster_handler.StrClear(), "[ContactRemoved " "old_contact:[Contact jid:jesus@example.net name:Jesus Quintana " "subscription_state:from groups:[Bowling League]]" "" "Bowling League" " index:4]"); EXPECT_EQ(handler.OutputActivity(), ""); EXPECT_EQ(handler.SessionActivity(), ""); } }