diff --git a/include/Swiften/Server/ServerStanzaChannel.cpp b/include/Swiften/Server/ServerStanzaChannel.cpp index a0f56543..bb3fbf6e 100644 --- a/include/Swiften/Server/ServerStanzaChannel.cpp +++ b/include/Swiften/Server/ServerStanzaChannel.cpp @@ -73,7 +73,7 @@ void ServerStanzaChannel::finishSession(const JID& to, boost::shared_ptrfinishSession(); - std::cout << "FINISH SESSION " << sessions[to.toBare().toString()].size() << "\n"; +// std::cout << "FINISH SESSION " << sessions[to.toBare().toString()].size() << "\n"; if (last) { break; } diff --git a/src/tests/basictest.cpp b/src/tests/basictest.cpp index 9de4b33a..73524063 100644 --- a/src/tests/basictest.cpp +++ b/src/tests/basictest.cpp @@ -22,7 +22,7 @@ using namespace Transport; void BasicTest::setMeUp (void) { streamEnded = false; - std::istringstream ifs("service.server_mode = 1\n"); + std::istringstream ifs("service.server_mode = 1\nservice.jid=localhost"); cfg = new Config(); cfg->load(ifs); @@ -67,7 +67,8 @@ void BasicTest::tearMeDown (void) { } void BasicTest::handleDataReceived(const Swift::SafeByteArray &data) { -parser->parse(safeByteArrayToString(data)); +// std::cout << safeByteArrayToString(data) << "\n"; + parser->parse(safeByteArrayToString(data)); } void BasicTest::handleStreamStart(const Swift::ProtocolHeader&) { @@ -82,6 +83,10 @@ void BasicTest::handleStreamEnd() { streamEnded = true; } +void BasicTest::injectPresence(boost::shared_ptr &response) { + dynamic_cast(component->getStanzaChannel())->onPresenceReceived(response); +} + Swift::Stanza *BasicTest::getStanza(boost::shared_ptr element) { Swift::Stanza *stanza = dynamic_cast(element.get()); CPPUNIT_ASSERT(stanza); diff --git a/src/tests/basictest.h b/src/tests/basictest.h index 6e093e33..525592ec 100644 --- a/src/tests/basictest.h +++ b/src/tests/basictest.h @@ -96,6 +96,8 @@ class BasicTest : public Swift::XMPPParserClient { void handleStreamEnd(); + void injectPresence(boost::shared_ptr &response); + Swift::Stanza *getStanza(boost::shared_ptr element); protected: diff --git a/src/tests/user.cpp b/src/tests/user.cpp index 864cbeba..a35ac423 100644 --- a/src/tests/user.cpp +++ b/src/tests/user.cpp @@ -23,20 +23,65 @@ using namespace Transport; class UserTest : public CPPUNIT_NS :: TestFixture, public BasicTest { CPPUNIT_TEST_SUITE(UserTest); CPPUNIT_TEST(sendCurrentPresence); + CPPUNIT_TEST(handlePresence); + CPPUNIT_TEST(handlePresenceJoinRoom); + CPPUNIT_TEST(handlePresenceLeaveRoom); + CPPUNIT_TEST(handleDisconnected); CPPUNIT_TEST_SUITE_END(); public: + std::string room; + std::string roomNickname; + std::string roomPassword; + bool readyToConnect; + bool disconnected; + Swift::Presence::ref changedPresence; + void setUp (void) { + disconnected = false; + readyToConnect = false; + changedPresence = Swift::Presence::ref(); + room = ""; + roomNickname = ""; + roomPassword = ""; + setMeUp(); + userManager->onUserCreated.connect(boost::bind(&UserTest::handleUserCreated, this, _1)); connectUser(); received.clear(); } void tearDown (void) { + received.clear(); disconnectUser(); tearMeDown(); } + void handleUserCreated(User *user) { + user->onReadyToConnect.connect(boost::bind(&UserTest::handleUserReadyToConnect, this, user)); + user->onPresenceChanged.connect(boost::bind(&UserTest::handleUserPresenceChanged, this, user, _1)); + user->onRoomJoined.connect(boost::bind(&UserTest::handleRoomJoined, this, user, _1, _2, _3)); + user->onRoomLeft.connect(boost::bind(&UserTest::handleRoomLeft, this, user, _1)); + } + + void handleUserReadyToConnect(User *user) { + readyToConnect = true; + } + + void handleUserPresenceChanged(User *user, Swift::Presence::ref presence) { + changedPresence = presence; + } + + void handleRoomJoined(User *user, const std::string &r, const std::string &nickname, const std::string &password) { + room = r; + roomNickname = nickname; + roomPassword = password; + } + + void handleRoomLeft(User *user, const std::string &r) { + room = r; + } + void connectUser() { CPPUNIT_ASSERT_EQUAL(0, userManager->getUserCount()); userRegistry->isValidUserPassword(Swift::JID("user@localhost/resource"), serverFromClientSession.get(), Swift::createSafeByteArray("password")); @@ -51,11 +96,14 @@ class UserTest : public CPPUNIT_NS :: TestFixture, public BasicTest { CPPUNIT_ASSERT(user->isReadyToConnect() == true); CPPUNIT_ASSERT(user->isConnected() == false); + CPPUNIT_ASSERT_EQUAL(true, readyToConnect); + user->setConnected(true); CPPUNIT_ASSERT(user->isConnected() == true); CPPUNIT_ASSERT_EQUAL(1, (int) received.size()); CPPUNIT_ASSERT(getStanza(received[0])->getPayload()); + received.clear(); } void sendCurrentPresence() { @@ -66,7 +114,84 @@ class UserTest : public CPPUNIT_NS :: TestFixture, public BasicTest { CPPUNIT_ASSERT_EQUAL(0, (int) received.size()); } + void handlePresence() { + Swift::Presence::ref response = Swift::Presence::create(); + response->setTo("localhost"); + response->setFrom("user@localhost/resource"); + response->setShow(Swift::StatusShow::Away); + + injectPresence(response); + loop->processEvents(); + + // no presence received in server mode, just disco#info + CPPUNIT_ASSERT_EQUAL(1, (int) received.size()); + CPPUNIT_ASSERT(getStanza(received[0])->getPayload()); + + CPPUNIT_ASSERT(changedPresence); + CPPUNIT_ASSERT_EQUAL(Swift::StatusShow::Away, changedPresence->getShow()); + } + + void handlePresenceJoinRoom() { + Swift::Presence::ref response = Swift::Presence::create(); + response->setTo("#room@localhost/hanzz"); + response->setFrom("user@localhost/resource"); + + Swift::MUCPayload *payload = new Swift::MUCPayload(); + payload->setPassword("password"); + response->addPayload(boost::shared_ptr(payload)); + injectPresence(response); + loop->processEvents(); + + // no presence received in server mode, just disco#info + CPPUNIT_ASSERT_EQUAL(1, (int) received.size()); + CPPUNIT_ASSERT(getStanza(received[0])->getPayload()); + + CPPUNIT_ASSERT_EQUAL(std::string("#room"), room); + CPPUNIT_ASSERT_EQUAL(std::string("hanzz"), roomNickname); + CPPUNIT_ASSERT_EQUAL(std::string("password"), roomPassword); + } + + void handlePresenceLeaveRoom() { + Swift::Presence::ref response = Swift::Presence::create(); + response->setTo("#room@localhost/hanzz"); + response->setFrom("user@localhost/resource"); + response->setType(Swift::Presence::Unavailable); + + Swift::MUCPayload *payload = new Swift::MUCPayload(); + payload->setPassword("password"); + response->addPayload(boost::shared_ptr(payload)); + injectPresence(response); + loop->processEvents(); + + CPPUNIT_ASSERT_EQUAL(0, (int) received.size()); + + CPPUNIT_ASSERT_EQUAL(std::string("#room"), room); + CPPUNIT_ASSERT_EQUAL(std::string(""), roomNickname); + CPPUNIT_ASSERT_EQUAL(std::string(""), roomPassword); + } + + void handleDisconnected() { + User *user = userManager->getUser("user@localhost"); + user->handleDisconnected("Connection error"); + loop->processEvents(); + + CPPUNIT_ASSERT(streamEnded); + user = userManager->getUser("user@localhost"); + CPPUNIT_ASSERT(!user); + + CPPUNIT_ASSERT_EQUAL(2, (int) received.size()); + Swift::Message *m = dynamic_cast(getStanza(received[0])); + CPPUNIT_ASSERT_EQUAL(std::string("Connection error"), m->getBody()); + + CPPUNIT_ASSERT(dynamic_cast(received[1].get())); + CPPUNIT_ASSERT_EQUAL(std::string("Connection error"), dynamic_cast(received[1].get())->getText()); + + disconnected = true; + } + void disconnectUser() { + if (disconnected) + return; userManager->disconnectUser("user@localhost"); dynamic_cast(factories->getTimerFactory())->setTime(10); loop->processEvents(); diff --git a/src/user.cpp b/src/user.cpp index 53056d40..c66da595 100644 --- a/src/user.cpp +++ b/src/user.cpp @@ -206,7 +206,11 @@ void User::handlePresence(Swift::Presence::ref presence) { } LOG4CXX_INFO(logger, m_jid.toString() << ": Going to join room " << presence->getTo().getNode() << " as " << presence->getTo().getResource()); std::string room = Buddy::JIDToLegacyName(presence->getTo()); - onRoomJoined(room, presence->getTo().getResource(), ""); + std::string password = ""; + if (presence->getPayload() != NULL) { + password = presence->getPayload()->getPassword() ? *presence->getPayload()->getPassword() : ""; + } + onRoomJoined(room, presence->getTo().getResource(), password); } return; } @@ -300,7 +304,7 @@ void User::handleDisconnected(const std::string &error) { // We can't be sure finishSession sends unavailable presence everytime, so check if user gets removed // in finishSession(...) call and if not, remove it here. std::string jid = m_jid.toBare().toString(); - dynamic_cast(m_component->getStanzaChannel())->finishSession(m_jid, boost::shared_ptr(new Swift::StreamError(Swift::StreamError::UndefinedCondition, "test"))); + dynamic_cast(m_component->getStanzaChannel())->finishSession(m_jid, boost::shared_ptr(new Swift::StreamError(Swift::StreamError::UndefinedCondition, error))); if (m_userManager->getUser(jid) != NULL) { m_userManager->removeUser(this); }