2011-10-26 00:32:15 +02:00
|
|
|
#include <cppunit/TestFixture.h>
|
|
|
|
#include <cppunit/extensions/HelperMacros.h>
|
|
|
|
#include <Swiften/Swiften.h>
|
|
|
|
#include <Swiften/EventLoop/DummyEventLoop.h>
|
|
|
|
#include <Swiften/Server/Server.h>
|
|
|
|
#include <Swiften/Network/DummyNetworkFactories.h>
|
|
|
|
#include <Swiften/Network/DummyConnectionServer.h>
|
|
|
|
#include "Swiften/Server/ServerStanzaChannel.h"
|
|
|
|
#include "Swiften/Server/ServerFromClientSession.h"
|
|
|
|
#include "Swiften/Parser/PayloadParsers/FullPayloadParserFactoryCollection.h"
|
|
|
|
#include "basictest.h"
|
|
|
|
|
|
|
|
using namespace Transport;
|
|
|
|
|
2016-02-15 09:57:25 +01:00
|
|
|
#if !HAVE_SWIFTEN_3
|
2016-02-15 12:21:04 +01:00
|
|
|
#define get_value_or(X) substr()
|
2016-02-15 09:57:25 +01:00
|
|
|
#endif
|
|
|
|
|
2011-10-26 00:32:15 +02:00
|
|
|
class UserTest : public CPPUNIT_NS :: TestFixture, public BasicTest {
|
|
|
|
CPPUNIT_TEST_SUITE(UserTest);
|
|
|
|
CPPUNIT_TEST(sendCurrentPresence);
|
2011-10-26 16:06:24 +02:00
|
|
|
CPPUNIT_TEST(handlePresence);
|
|
|
|
CPPUNIT_TEST(handlePresenceJoinRoom);
|
2012-09-22 20:19:19 +02:00
|
|
|
CPPUNIT_TEST(handlePresenceJoinRoomTwoResources);
|
2016-02-19 10:24:13 +01:00
|
|
|
// CPPUNIT_TEST(handlePresenceLeaveRoom); // tested as part of other tests
|
2012-09-22 20:19:19 +02:00
|
|
|
CPPUNIT_TEST(handlePresenceLeaveRoomTwoResources);
|
2012-09-25 12:48:30 +02:00
|
|
|
CPPUNIT_TEST(handlePresenceLeaveRoomTwoResourcesOneDisconnects);
|
2012-12-21 09:26:34 +01:00
|
|
|
CPPUNIT_TEST(handlePresenceLeaveRoomBouncer);
|
|
|
|
CPPUNIT_TEST(handlePresenceLeaveRoomTwoResourcesBouncer);
|
|
|
|
CPPUNIT_TEST(handlePresenceLeaveRoomTwoResourcesOneDisconnectsBouncer);
|
2016-02-08 20:49:22 +01:00
|
|
|
CPPUNIT_TEST(handlePresenceLeaveRoomTwoResourcesAnotherOneDisconnects);
|
2012-08-16 13:18:40 +02:00
|
|
|
CPPUNIT_TEST(leaveJoinedRoom);
|
2016-02-19 15:14:49 +01:00
|
|
|
CPPUNIT_TEST(doNotLeaveNormalChat);
|
2012-10-08 10:51:27 +02:00
|
|
|
CPPUNIT_TEST(joinRoomBeforeConnected);
|
2011-10-26 16:06:24 +02:00
|
|
|
CPPUNIT_TEST(handleDisconnected);
|
2012-09-05 10:06:51 +02:00
|
|
|
CPPUNIT_TEST(handleDisconnectedReconnect);
|
2012-10-24 10:53:26 +02:00
|
|
|
CPPUNIT_TEST(joinRoomHandleDisconnectedRejoin);
|
2016-01-20 07:36:05 +01:00
|
|
|
CPPUNIT_TEST(joinRoomAfterFlagNotAuthorized);
|
2016-02-04 06:55:14 +01:00
|
|
|
CPPUNIT_TEST(requestVCard);
|
2011-10-26 00:32:15 +02:00
|
|
|
CPPUNIT_TEST_SUITE_END();
|
|
|
|
|
|
|
|
public:
|
2011-10-26 16:06:24 +02:00
|
|
|
std::string room;
|
|
|
|
std::string roomNickname;
|
|
|
|
std::string roomPassword;
|
2016-02-04 06:55:14 +01:00
|
|
|
std::string photo;
|
2011-10-26 16:06:24 +02:00
|
|
|
bool readyToConnect;
|
|
|
|
bool disconnected;
|
|
|
|
Swift::Presence::ref changedPresence;
|
|
|
|
|
2011-10-26 00:32:15 +02:00
|
|
|
void setUp (void) {
|
2011-10-26 16:06:24 +02:00
|
|
|
disconnected = false;
|
|
|
|
readyToConnect = false;
|
|
|
|
changedPresence = Swift::Presence::ref();
|
|
|
|
room = "";
|
|
|
|
roomNickname = "";
|
|
|
|
roomPassword = "";
|
2016-02-04 06:55:14 +01:00
|
|
|
photo = "";
|
2011-10-26 16:06:24 +02:00
|
|
|
|
2011-10-26 00:32:15 +02:00
|
|
|
setMeUp();
|
2011-10-26 16:06:24 +02:00
|
|
|
userManager->onUserCreated.connect(boost::bind(&UserTest::handleUserCreated, this, _1));
|
2011-10-26 00:32:15 +02:00
|
|
|
connectUser();
|
|
|
|
received.clear();
|
2016-02-04 06:55:14 +01:00
|
|
|
|
|
|
|
frontend->onVCardUpdated.connect(boost::bind(&UserTest::handleVCardUpdated, this, _1, _2));
|
2011-10-26 00:32:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void tearDown (void) {
|
2011-10-26 16:06:24 +02:00
|
|
|
received.clear();
|
2012-08-28 13:12:46 +02:00
|
|
|
if (!disconnected) {
|
|
|
|
disconnectUser();
|
|
|
|
}
|
2012-12-21 09:26:34 +01:00
|
|
|
userManager->removeAllUsers();
|
2011-10-26 00:32:15 +02:00
|
|
|
tearMeDown();
|
|
|
|
}
|
|
|
|
|
2016-09-12 18:20:58 +02:00
|
|
|
void handleVCardUpdated(User *user, SWIFTEN_SHRPTR_NAMESPACE::shared_ptr<Swift::VCard> v) {
|
2016-02-04 06:55:14 +01:00
|
|
|
photo = Swift::byteArrayToString(v->getPhoto());
|
|
|
|
}
|
|
|
|
|
2011-10-26 16:06:24 +02:00
|
|
|
void handleUserCreated(User *user) {
|
|
|
|
user->onReadyToConnect.connect(boost::bind(&UserTest::handleUserReadyToConnect, this, user));
|
|
|
|
user->onPresenceChanged.connect(boost::bind(&UserTest::handleUserPresenceChanged, this, user, _1));
|
2012-08-09 11:21:01 +02:00
|
|
|
user->onRoomJoined.connect(boost::bind(&UserTest::handleRoomJoined, this, user, _1, _2, _3, _4));
|
2011-10-26 16:06:24 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-08-09 11:21:01 +02:00
|
|
|
void handleRoomJoined(User *user, const std::string &jid, const std::string &r, const std::string &nickname, const std::string &password) {
|
2011-10-26 16:06:24 +02:00
|
|
|
room = r;
|
|
|
|
roomNickname = nickname;
|
|
|
|
roomPassword = password;
|
|
|
|
}
|
|
|
|
|
|
|
|
void handleRoomLeft(User *user, const std::string &r) {
|
|
|
|
room = r;
|
|
|
|
}
|
|
|
|
|
2011-10-26 00:32:15 +02:00
|
|
|
|
|
|
|
void sendCurrentPresence() {
|
|
|
|
User *user = userManager->getUser("user@localhost");
|
|
|
|
user->sendCurrentPresence();
|
|
|
|
|
|
|
|
// We're not forwarding current presence in server-mode
|
|
|
|
CPPUNIT_ASSERT_EQUAL(0, (int) received.size());
|
|
|
|
}
|
|
|
|
|
2011-10-26 16:06:24 +02:00
|
|
|
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<Swift::DiscoInfo>());
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT(changedPresence);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(Swift::StatusShow::Away, changedPresence->getShow());
|
|
|
|
}
|
|
|
|
|
|
|
|
void handlePresenceJoinRoom() {
|
2012-08-16 12:52:53 +02:00
|
|
|
User *user = userManager->getUser("user@localhost");
|
|
|
|
|
2011-10-26 16:06:24 +02:00
|
|
|
Swift::Presence::ref response = Swift::Presence::create();
|
2013-01-25 08:05:46 +01:00
|
|
|
response->setTo("room@localhost/hanzz");
|
2011-10-26 16:06:24 +02:00
|
|
|
response->setFrom("user@localhost/resource");
|
|
|
|
|
|
|
|
Swift::MUCPayload *payload = new Swift::MUCPayload();
|
|
|
|
payload->setPassword("password");
|
2016-09-12 18:20:58 +02:00
|
|
|
response->addPayload(SWIFTEN_SHRPTR_NAMESPACE::shared_ptr<Swift::Payload>(payload));
|
2011-10-26 16:06:24 +02:00
|
|
|
injectPresence(response);
|
|
|
|
loop->processEvents();
|
|
|
|
|
2013-01-25 08:05:46 +01:00
|
|
|
CPPUNIT_ASSERT_EQUAL(0, (int) received.size());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("room"), room);
|
2011-10-26 16:06:24 +02:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("hanzz"), roomNickname);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("password"), roomPassword);
|
2012-08-16 12:52:53 +02:00
|
|
|
|
|
|
|
room = "";
|
|
|
|
roomNickname = "";
|
|
|
|
roomPassword = "";
|
|
|
|
|
|
|
|
// simulate that backend joined the room
|
2013-01-25 08:05:46 +01:00
|
|
|
TestingConversation *conv = new TestingConversation(user->getConversationManager(), "room", true);
|
2012-09-22 20:19:19 +02:00
|
|
|
conv->addJID("user@localhost/resource");
|
2016-01-20 07:36:05 +01:00
|
|
|
conv->setNickname("hanzz");
|
2012-08-30 12:37:14 +02:00
|
|
|
user->getConversationManager()->addConversation(conv);
|
2012-08-16 12:52:53 +02:00
|
|
|
|
|
|
|
received.clear();
|
|
|
|
injectPresence(response);
|
|
|
|
loop->processEvents();
|
|
|
|
|
|
|
|
// no presence received in server mode, just disco#info
|
2013-01-25 08:05:46 +01:00
|
|
|
CPPUNIT_ASSERT_EQUAL(0, (int) received.size());
|
2012-08-16 12:52:53 +02:00
|
|
|
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string(""), room);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string(""), roomNickname);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string(""), roomPassword);
|
2011-10-26 16:06:24 +02:00
|
|
|
}
|
|
|
|
|
2012-09-22 20:19:19 +02:00
|
|
|
void handlePresenceJoinRoomTwoResources() {
|
|
|
|
handlePresenceJoinRoom();
|
2012-09-23 09:48:03 +02:00
|
|
|
User *user = userManager->getUser("user@localhost");
|
|
|
|
|
|
|
|
// Add 1 participant
|
2013-01-25 08:05:46 +01:00
|
|
|
Conversation *conv = user->getConversationManager()->getConversation("room");
|
2013-01-24 08:35:24 +01:00
|
|
|
conv->handleParticipantChanged("anotheruser", Conversation::PARTICIPANT_FLAG_NONE, Swift::StatusShow::Away, "my status message");
|
2012-09-23 09:48:03 +02:00
|
|
|
|
|
|
|
// Connect 2nd resource
|
2012-09-22 20:19:19 +02:00
|
|
|
connectSecondResource();
|
2012-09-23 09:48:03 +02:00
|
|
|
received2.clear();
|
2012-09-22 20:19:19 +02:00
|
|
|
Swift::Presence::ref response = Swift::Presence::create();
|
2013-01-25 08:05:46 +01:00
|
|
|
response->setTo("room@localhost/hanzz");
|
2012-09-22 20:19:19 +02:00
|
|
|
response->setFrom("user@localhost/resource2");
|
|
|
|
|
|
|
|
Swift::MUCPayload *payload = new Swift::MUCPayload();
|
|
|
|
payload->setPassword("password");
|
2016-09-12 18:20:58 +02:00
|
|
|
response->addPayload(SWIFTEN_SHRPTR_NAMESPACE::shared_ptr<Swift::Payload>(payload));
|
2012-09-22 20:19:19 +02:00
|
|
|
injectPresence(response);
|
|
|
|
loop->processEvents();
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string(""), room);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string(""), roomNickname);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string(""), roomPassword);
|
2012-09-23 09:48:03 +02:00
|
|
|
|
2016-02-18 07:34:59 +01:00
|
|
|
CPPUNIT_ASSERT_EQUAL(2, (int) received2.size());
|
|
|
|
CPPUNIT_ASSERT(dynamic_cast<Swift::Presence *>(getStanza(received2[1])));
|
|
|
|
CPPUNIT_ASSERT_EQUAL(Swift::StatusShow::Away, dynamic_cast<Swift::Presence *>(getStanza(received2[1]))->getShow());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("user@localhost/resource2"), dynamic_cast<Swift::Presence *>(getStanza(received2[1]))->getTo().toString());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("room@localhost/anotheruser"), dynamic_cast<Swift::Presence *>(getStanza(received2[1]))->getFrom().toString());
|
|
|
|
CPPUNIT_ASSERT(getStanza(received2[1])->getPayload<Swift::MUCUserPayload>());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(Swift::MUCOccupant::Member, *getStanza(received2[1])->getPayload<Swift::MUCUserPayload>()->getItems()[0].affiliation);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(Swift::MUCOccupant::Participant, *getStanza(received2[1])->getPayload<Swift::MUCUserPayload>()->getItems()[0].role);
|
2012-09-22 20:19:19 +02:00
|
|
|
}
|
|
|
|
|
2011-10-26 16:06:24 +02:00
|
|
|
void handlePresenceLeaveRoom() {
|
2013-01-25 13:26:49 +01:00
|
|
|
received.clear();
|
2011-10-26 16:06:24 +02:00
|
|
|
Swift::Presence::ref response = Swift::Presence::create();
|
2013-01-25 08:05:46 +01:00
|
|
|
response->setTo("room@localhost/hanzz");
|
2011-10-26 16:06:24 +02:00
|
|
|
response->setFrom("user@localhost/resource");
|
|
|
|
response->setType(Swift::Presence::Unavailable);
|
|
|
|
|
|
|
|
Swift::MUCPayload *payload = new Swift::MUCPayload();
|
|
|
|
payload->setPassword("password");
|
2016-09-12 18:20:58 +02:00
|
|
|
response->addPayload(SWIFTEN_SHRPTR_NAMESPACE::shared_ptr<Swift::Payload>(payload));
|
2011-10-26 16:06:24 +02:00
|
|
|
injectPresence(response);
|
|
|
|
loop->processEvents();
|
|
|
|
|
2016-02-19 10:24:13 +01:00
|
|
|
CPPUNIT_ASSERT_EQUAL(1, (int) received.size());
|
2011-10-26 16:06:24 +02:00
|
|
|
|
2013-01-25 13:26:49 +01:00
|
|
|
// CPPUNIT_ASSERT_EQUAL(std::string("room"), room);
|
|
|
|
// CPPUNIT_ASSERT_EQUAL(std::string(""), roomNickname);
|
|
|
|
// CPPUNIT_ASSERT_EQUAL(std::string(""), roomPassword);
|
2011-10-26 16:06:24 +02:00
|
|
|
}
|
|
|
|
|
2012-09-22 20:19:19 +02:00
|
|
|
void handlePresenceLeaveRoomTwoResources() {
|
|
|
|
handlePresenceJoinRoomTwoResources();
|
|
|
|
received.clear();
|
|
|
|
|
|
|
|
// User is still connected from resource2, so he should not leave the room
|
|
|
|
Swift::Presence::ref response = Swift::Presence::create();
|
2013-01-25 08:05:46 +01:00
|
|
|
response->setTo("room@localhost/hanzz");
|
2012-09-22 20:19:19 +02:00
|
|
|
response->setFrom("user@localhost/resource");
|
|
|
|
response->setType(Swift::Presence::Unavailable);
|
|
|
|
|
|
|
|
Swift::MUCPayload *payload = new Swift::MUCPayload();
|
|
|
|
payload->setPassword("password");
|
2016-09-12 18:20:58 +02:00
|
|
|
response->addPayload(SWIFTEN_SHRPTR_NAMESPACE::shared_ptr<Swift::Payload>(payload));
|
2012-09-22 20:19:19 +02:00
|
|
|
injectPresence(response);
|
|
|
|
loop->processEvents();
|
|
|
|
|
2016-02-19 10:24:13 +01:00
|
|
|
CPPUNIT_ASSERT_EQUAL(1, (int) received.size());
|
2012-09-22 20:19:19 +02:00
|
|
|
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string(""), room);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string(""), roomNickname);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string(""), roomPassword);
|
|
|
|
|
|
|
|
// disconnect also from resource
|
|
|
|
// User is still connected from resource2, so he should not leave the room
|
|
|
|
response = Swift::Presence::create();
|
2013-01-25 08:05:46 +01:00
|
|
|
response->setTo("room@localhost/hanzz");
|
2012-09-22 20:19:19 +02:00
|
|
|
response->setFrom("user@localhost/resource2");
|
|
|
|
response->setType(Swift::Presence::Unavailable);
|
|
|
|
|
|
|
|
payload = new Swift::MUCPayload();
|
|
|
|
payload->setPassword("password");
|
2016-09-12 18:20:58 +02:00
|
|
|
response->addPayload(SWIFTEN_SHRPTR_NAMESPACE::shared_ptr<Swift::Payload>(payload));
|
2012-09-22 20:19:19 +02:00
|
|
|
injectPresence(response);
|
|
|
|
loop->processEvents();
|
|
|
|
|
2016-02-19 10:24:13 +01:00
|
|
|
CPPUNIT_ASSERT_EQUAL(1, (int) received.size());
|
2012-09-22 20:19:19 +02:00
|
|
|
|
2013-01-25 08:05:46 +01:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("room"), room);
|
2012-09-22 20:19:19 +02:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string(""), roomNickname);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string(""), roomPassword);
|
|
|
|
}
|
|
|
|
|
2012-09-25 12:48:30 +02:00
|
|
|
void handlePresenceLeaveRoomTwoResourcesOneDisconnects() {
|
|
|
|
handlePresenceJoinRoomTwoResources();
|
|
|
|
received.clear();
|
|
|
|
User *user = userManager->getUser("user@localhost");
|
|
|
|
|
|
|
|
// User is still connected from resource2, so he should not leave the room
|
|
|
|
Swift::Presence::ref response = Swift::Presence::create();
|
|
|
|
response->setTo("localhost/hanzz");
|
|
|
|
response->setFrom("user@localhost/resource");
|
|
|
|
response->setType(Swift::Presence::Unavailable);
|
|
|
|
injectPresence(response);
|
|
|
|
loop->processEvents();
|
|
|
|
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string(""), room);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string(""), roomNickname);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string(""), roomPassword);
|
|
|
|
|
2013-01-25 08:05:46 +01:00
|
|
|
Conversation *conv = user->getConversationManager()->getConversation("room");
|
2012-09-25 12:48:30 +02:00
|
|
|
CPPUNIT_ASSERT_EQUAL(1, (int) conv->getJIDs().size());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(Swift::JID("user@localhost/resource2"), conv->getJIDs().front());
|
|
|
|
}
|
|
|
|
|
2016-02-08 20:49:22 +01:00
|
|
|
void handlePresenceLeaveRoomTwoResourcesAnotherOneDisconnects() {
|
|
|
|
handlePresenceJoinRoom();
|
|
|
|
User *user = userManager->getUser("user@localhost");
|
|
|
|
|
|
|
|
// Add 1 participant
|
|
|
|
Conversation *conv = user->getConversationManager()->getConversation("room");
|
|
|
|
conv->handleParticipantChanged("anotheruser", Conversation::PARTICIPANT_FLAG_NONE, Swift::StatusShow::Away, "my status message");
|
|
|
|
|
|
|
|
// Connect 2nd resource
|
|
|
|
connectSecondResource();
|
|
|
|
received2.clear();
|
|
|
|
received.clear();
|
|
|
|
|
|
|
|
// User is still connected from resource2, but not in room, so we should leave the room
|
|
|
|
Swift::Presence::ref response = Swift::Presence::create();
|
|
|
|
response->setTo("localhost/hanzz");
|
|
|
|
response->setFrom("user@localhost/resource");
|
|
|
|
response->setType(Swift::Presence::Unavailable);
|
|
|
|
injectPresence(response);
|
|
|
|
loop->processEvents();
|
|
|
|
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("room"), room);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string(""), roomNickname);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string(""), roomPassword);
|
|
|
|
|
|
|
|
conv = user->getConversationManager()->getConversation("room");
|
|
|
|
CPPUNIT_ASSERT(!conv);
|
|
|
|
}
|
|
|
|
|
2012-12-21 09:26:34 +01:00
|
|
|
void handlePresenceLeaveRoomBouncer() {
|
|
|
|
User *user = userManager->getUser("user@localhost");
|
|
|
|
user->addUserSetting("stay_connected", "1");
|
|
|
|
Swift::Presence::ref response = Swift::Presence::create();
|
2013-01-25 08:05:46 +01:00
|
|
|
response->setTo("room@localhost/hanzz");
|
2012-12-21 09:26:34 +01:00
|
|
|
response->setFrom("user@localhost/resource");
|
|
|
|
response->setType(Swift::Presence::Unavailable);
|
|
|
|
|
|
|
|
Swift::MUCPayload *payload = new Swift::MUCPayload();
|
|
|
|
payload->setPassword("password");
|
2016-09-12 18:20:58 +02:00
|
|
|
response->addPayload(SWIFTEN_SHRPTR_NAMESPACE::shared_ptr<Swift::Payload>(payload));
|
2012-12-21 09:26:34 +01:00
|
|
|
injectPresence(response);
|
|
|
|
loop->processEvents();
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT_EQUAL(0, (int) received.size());
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string(""), room);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string(""), roomNickname);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string(""), roomPassword);
|
|
|
|
}
|
|
|
|
|
|
|
|
void handlePresenceLeaveRoomTwoResourcesBouncer() {
|
|
|
|
User *user = userManager->getUser("user@localhost");
|
|
|
|
user->addUserSetting("stay_connected", "1");
|
|
|
|
handlePresenceJoinRoomTwoResources();
|
|
|
|
received.clear();
|
|
|
|
|
|
|
|
// User is still connected from resource2, so he should not leave the room
|
|
|
|
Swift::Presence::ref response = Swift::Presence::create();
|
2013-01-25 08:05:46 +01:00
|
|
|
response->setTo("room@localhost/hanzz");
|
2012-12-21 09:26:34 +01:00
|
|
|
response->setFrom("user@localhost/resource");
|
|
|
|
response->setType(Swift::Presence::Unavailable);
|
|
|
|
|
|
|
|
Swift::MUCPayload *payload = new Swift::MUCPayload();
|
|
|
|
payload->setPassword("password");
|
2016-09-12 18:20:58 +02:00
|
|
|
response->addPayload(SWIFTEN_SHRPTR_NAMESPACE::shared_ptr<Swift::Payload>(payload));
|
2012-12-21 09:26:34 +01:00
|
|
|
injectPresence(response);
|
|
|
|
loop->processEvents();
|
|
|
|
|
2016-02-19 10:24:13 +01:00
|
|
|
CPPUNIT_ASSERT_EQUAL(1, (int) received.size());
|
2012-12-21 09:26:34 +01:00
|
|
|
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string(""), room);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string(""), roomNickname);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string(""), roomPassword);
|
|
|
|
|
|
|
|
room = "something";
|
|
|
|
// disconnect also from resource
|
|
|
|
// User is still connected from resource2, so he should not leave the room
|
|
|
|
response = Swift::Presence::create();
|
2013-01-25 08:05:46 +01:00
|
|
|
response->setTo("room@localhost/hanzz");
|
2012-12-21 09:26:34 +01:00
|
|
|
response->setFrom("user@localhost/resource2");
|
|
|
|
response->setType(Swift::Presence::Unavailable);
|
|
|
|
|
|
|
|
payload = new Swift::MUCPayload();
|
|
|
|
payload->setPassword("password");
|
2016-09-12 18:20:58 +02:00
|
|
|
response->addPayload(SWIFTEN_SHRPTR_NAMESPACE::shared_ptr<Swift::Payload>(payload));
|
2012-12-21 09:26:34 +01:00
|
|
|
injectPresence(response);
|
|
|
|
loop->processEvents();
|
|
|
|
|
2016-02-19 10:24:13 +01:00
|
|
|
CPPUNIT_ASSERT_EQUAL(1, (int) received.size());
|
2012-12-21 09:26:34 +01:00
|
|
|
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("something"), room);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string(""), roomNickname);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string(""), roomPassword);
|
|
|
|
}
|
|
|
|
|
|
|
|
void handlePresenceLeaveRoomTwoResourcesOneDisconnectsBouncer() {
|
|
|
|
room = "something";
|
|
|
|
handlePresenceJoinRoomTwoResources();
|
|
|
|
received.clear();
|
|
|
|
User *user = userManager->getUser("user@localhost");
|
|
|
|
|
|
|
|
// User is still connected from resource2, so he should not leave the room
|
|
|
|
Swift::Presence::ref response = Swift::Presence::create();
|
|
|
|
response->setTo("localhost/hanzz");
|
|
|
|
response->setFrom("user@localhost/resource");
|
|
|
|
response->setType(Swift::Presence::Unavailable);
|
|
|
|
injectPresence(response);
|
|
|
|
loop->processEvents();
|
|
|
|
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string(""), room);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string(""), roomNickname);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string(""), roomPassword);
|
|
|
|
|
2013-01-25 08:05:46 +01:00
|
|
|
Conversation *conv = user->getConversationManager()->getConversation("room");
|
2012-12-21 09:26:34 +01:00
|
|
|
CPPUNIT_ASSERT_EQUAL(1, (int) conv->getJIDs().size());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(Swift::JID("user@localhost/resource2"), conv->getJIDs().front());
|
|
|
|
}
|
|
|
|
|
2012-08-16 13:18:40 +02:00
|
|
|
void leaveJoinedRoom() {
|
|
|
|
User *user = userManager->getUser("user@localhost");
|
|
|
|
handlePresenceJoinRoom();
|
|
|
|
|
2013-01-25 08:05:46 +01:00
|
|
|
CPPUNIT_ASSERT(user->getConversationManager()->getConversation("room"));
|
2012-08-16 13:18:40 +02:00
|
|
|
|
|
|
|
received.clear();
|
|
|
|
handlePresenceLeaveRoom();
|
|
|
|
|
2013-01-25 08:05:46 +01:00
|
|
|
CPPUNIT_ASSERT(!user->getConversationManager()->getConversation("room"));
|
2012-08-16 13:18:40 +02:00
|
|
|
}
|
|
|
|
|
2016-02-19 15:14:49 +01:00
|
|
|
void doNotLeaveNormalChat() {
|
|
|
|
User *user = userManager->getUser("user@localhost");
|
|
|
|
|
|
|
|
TestingConversation *conv = new TestingConversation(user->getConversationManager(), "buddy1@test");
|
|
|
|
user->getConversationManager()->addConversation(conv);
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string(""), room);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string(""), roomNickname);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string(""), roomPassword);
|
|
|
|
|
|
|
|
user->getConversationManager()->removeJID("user@localhost/resource");
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string(""), room);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string(""), roomNickname);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string(""), roomPassword);
|
|
|
|
Conversation *cv = user->getConversationManager()->getConversation("buddy1@test");
|
|
|
|
CPPUNIT_ASSERT(cv);
|
|
|
|
}
|
|
|
|
|
2011-10-26 16:06:24 +02:00
|
|
|
void handleDisconnected() {
|
|
|
|
User *user = userManager->getUser("user@localhost");
|
2012-08-09 11:21:01 +02:00
|
|
|
user->handleDisconnected("Connection error", Swift::SpectrumErrorPayload::CONNECTION_ERROR_AUTHENTICATION_FAILED);
|
2011-10-26 16:06:24 +02:00
|
|
|
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<Swift::Message *>(getStanza(received[0]));
|
2016-02-15 12:21:04 +01:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("Connection error"), m->getBody().get_value_or(""));
|
2011-10-26 16:06:24 +02:00
|
|
|
|
|
|
|
CPPUNIT_ASSERT(dynamic_cast<Swift::StreamError *>(received[1].get()));
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("Connection error"), dynamic_cast<Swift::StreamError *>(received[1].get())->getText());
|
|
|
|
|
|
|
|
disconnected = true;
|
|
|
|
}
|
|
|
|
|
2012-09-05 10:06:51 +02:00
|
|
|
void handleDisconnectedReconnect() {
|
2012-09-16 14:56:07 +02:00
|
|
|
readyToConnect = false;
|
2012-09-05 10:06:51 +02:00
|
|
|
User *user = userManager->getUser("user@localhost");
|
|
|
|
user->handleDisconnected("Connection error");
|
|
|
|
loop->processEvents();
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT(!streamEnded);
|
|
|
|
user = userManager->getUser("user@localhost");
|
|
|
|
CPPUNIT_ASSERT(user);
|
2012-09-16 14:56:07 +02:00
|
|
|
CPPUNIT_ASSERT(readyToConnect);
|
2012-10-24 10:53:26 +02:00
|
|
|
|
|
|
|
Swift::Presence::ref response = Swift::Presence::create();
|
|
|
|
response->setTo("localhost");
|
|
|
|
response->setFrom("user@localhost/resource");
|
|
|
|
injectPresence(response);
|
|
|
|
loop->processEvents();
|
2012-09-05 10:06:51 +02:00
|
|
|
}
|
|
|
|
|
2012-10-08 10:51:27 +02:00
|
|
|
void joinRoomBeforeConnected() {
|
|
|
|
User *user = userManager->getUser("user@localhost");
|
|
|
|
user->setConnected(false);
|
|
|
|
|
|
|
|
Swift::Presence::ref response = Swift::Presence::create();
|
2013-01-25 08:05:46 +01:00
|
|
|
response->setTo("room@localhost/hanzz");
|
2012-10-08 10:51:27 +02:00
|
|
|
response->setFrom("user@localhost/resource");
|
|
|
|
|
|
|
|
Swift::MUCPayload *payload = new Swift::MUCPayload();
|
|
|
|
payload->setPassword("password");
|
2016-09-12 18:20:58 +02:00
|
|
|
response->addPayload(SWIFTEN_SHRPTR_NAMESPACE::shared_ptr<Swift::Payload>(payload));
|
2012-10-08 10:51:27 +02:00
|
|
|
injectPresence(response);
|
|
|
|
loop->processEvents();
|
|
|
|
|
2013-01-25 08:05:46 +01:00
|
|
|
CPPUNIT_ASSERT_EQUAL(0, (int) received.size());
|
2012-10-08 10:51:27 +02:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string(""), room);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string(""), roomNickname);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string(""), roomPassword);
|
|
|
|
|
|
|
|
user->setConnected(true);
|
2013-01-25 08:05:46 +01:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("room"), room);
|
2012-10-08 10:51:27 +02:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("hanzz"), roomNickname);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("password"), roomPassword);
|
2012-10-24 10:53:26 +02:00
|
|
|
}
|
2012-10-08 10:51:27 +02:00
|
|
|
|
2012-10-24 10:53:26 +02:00
|
|
|
void joinRoomHandleDisconnectedRejoin() {
|
|
|
|
User *user = userManager->getUser("user@localhost");
|
|
|
|
handlePresenceJoinRoom();
|
|
|
|
handleDisconnectedReconnect();
|
2012-10-08 10:51:27 +02:00
|
|
|
room = "";
|
|
|
|
roomNickname = "";
|
|
|
|
roomPassword = "";
|
2012-10-24 10:53:26 +02:00
|
|
|
received.clear();
|
2012-10-08 10:51:27 +02:00
|
|
|
user->setConnected(true);
|
2012-10-24 10:53:26 +02:00
|
|
|
|
2013-01-25 08:05:46 +01:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("room"), room);
|
2012-10-24 10:53:26 +02:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("hanzz"), roomNickname);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("password"), roomPassword);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(0, (int) received.size());
|
2012-10-08 10:51:27 +02:00
|
|
|
}
|
|
|
|
|
2016-01-20 07:36:05 +01:00
|
|
|
void joinRoomAfterFlagNotAuthorized() {
|
|
|
|
User *user = userManager->getUser("user@localhost");
|
|
|
|
handlePresenceJoinRoom();
|
|
|
|
|
|
|
|
Conversation *conv = user->getConversationManager()->getConversation("room");
|
|
|
|
conv->handleParticipantChanged("hanzz", Conversation::PARTICIPANT_FLAG_NOT_AUTHORIZED, Swift::StatusShow::Away, "my status message");
|
|
|
|
loop->processEvents();
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT_EQUAL(1, (int) received.size());
|
|
|
|
CPPUNIT_ASSERT(dynamic_cast<Swift::Presence *>(getStanza(received[0])));
|
|
|
|
CPPUNIT_ASSERT_EQUAL(Swift::Presence::Error, dynamic_cast<Swift::Presence *>(getStanza(received[0]))->getType());
|
|
|
|
CPPUNIT_ASSERT(getStanza(received[0])->getPayload<Swift::ErrorPayload>());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(Swift::ErrorPayload::NotAuthorized, getStanza(received[0])->getPayload<Swift::ErrorPayload>()->getCondition());
|
|
|
|
|
|
|
|
received.clear();
|
|
|
|
handlePresenceJoinRoom();
|
|
|
|
}
|
|
|
|
|
2016-02-04 06:55:14 +01:00
|
|
|
void requestVCard() {
|
|
|
|
User *user = userManager->getUser("user@localhost");
|
|
|
|
user->setStorageBackend(storage);
|
|
|
|
|
|
|
|
Swift::Presence::ref response = Swift::Presence::create();
|
|
|
|
response->setTo("localhost");
|
|
|
|
response->setFrom("user@localhost/resource");
|
2016-09-12 18:20:58 +02:00
|
|
|
response->addPayload(SWIFTEN_SHRPTR_NAMESPACE::shared_ptr<Swift::Payload>(new Swift::VCardUpdate("hash")));
|
2016-02-04 06:55:14 +01:00
|
|
|
|
|
|
|
injectPresence(response);
|
|
|
|
loop->processEvents();
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT_EQUAL(2, (int) received.size());
|
|
|
|
Swift::VCard::ref payload1 = getStanza(received[1])->getPayload<Swift::VCard>();
|
|
|
|
CPPUNIT_ASSERT(payload1);
|
|
|
|
|
2016-09-12 18:20:58 +02:00
|
|
|
SWIFTEN_SHRPTR_NAMESPACE::shared_ptr<Swift::VCard> vcard(new Swift::VCard());
|
2016-02-04 06:55:14 +01:00
|
|
|
vcard->setPhoto(Swift::createByteArray("photo"));
|
|
|
|
injectIQ(Swift::IQ::createResult(getStanza(received[1])->getFrom(), getStanza(received[1])->getTo(), getStanza(received[1])->getID(), vcard));
|
|
|
|
loop->processEvents();
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT_EQUAL(2, (int) received.size());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("photo"), photo);
|
|
|
|
|
|
|
|
received.clear();
|
|
|
|
injectPresence(response);
|
|
|
|
loop->processEvents();
|
|
|
|
CPPUNIT_ASSERT_EQUAL(1, (int) received.size());
|
|
|
|
}
|
|
|
|
|
2011-10-26 00:32:15 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE_REGISTRATION (UserTest);
|