More UserTest tests + fixes of some bugs found by tests

This commit is contained in:
Jan Kaluza 2011-10-26 16:06:24 +02:00
parent 8e7146a496
commit 04cbb3a907
5 changed files with 141 additions and 5 deletions

View file

@ -73,7 +73,7 @@ void ServerStanzaChannel::finishSession(const JID& to, boost::shared_ptr<Element
}
(*i)->finishSession();
std::cout << "FINISH SESSION " << sessions[to.toBare().toString()].size() << "\n";
// std::cout << "FINISH SESSION " << sessions[to.toBare().toString()].size() << "\n";
if (last) {
break;
}

View file

@ -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<Swift::Presence> &response) {
dynamic_cast<Swift::ServerStanzaChannel *>(component->getStanzaChannel())->onPresenceReceived(response);
}
Swift::Stanza *BasicTest::getStanza(boost::shared_ptr<Swift::Element> element) {
Swift::Stanza *stanza = dynamic_cast<Swift::Stanza *>(element.get());
CPPUNIT_ASSERT(stanza);

View file

@ -96,6 +96,8 @@ class BasicTest : public Swift::XMPPParserClient {
void handleStreamEnd();
void injectPresence(boost::shared_ptr<Swift::Presence> &response);
Swift::Stanza *getStanza(boost::shared_ptr<Swift::Element> element);
protected:

View file

@ -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<Swift::DiscoInfo>());
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<Swift::DiscoInfo>());
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<Swift::Payload>(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<Swift::DiscoInfo>());
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<Swift::Payload>(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<Swift::Message *>(getStanza(received[0]));
CPPUNIT_ASSERT_EQUAL(std::string("Connection error"), m->getBody());
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;
}
void disconnectUser() {
if (disconnected)
return;
userManager->disconnectUser("user@localhost");
dynamic_cast<Swift::DummyTimerFactory *>(factories->getTimerFactory())->setTime(10);
loop->processEvents();

View file

@ -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<Swift::MUCPayload>() != NULL) {
password = presence->getPayload<Swift::MUCPayload>()->getPassword() ? *presence->getPayload<Swift::MUCPayload>()->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<Swift::ServerStanzaChannel *>(m_component->getStanzaChannel())->finishSession(m_jid, boost::shared_ptr<Swift::Element>(new Swift::StreamError(Swift::StreamError::UndefinedCondition, "test")));
dynamic_cast<Swift::ServerStanzaChannel *>(m_component->getStanzaChannel())->finishSession(m_jid, boost::shared_ptr<Swift::Element>(new Swift::StreamError(Swift::StreamError::UndefinedCondition, error)));
if (m_userManager->getUser(jid) != NULL) {
m_userManager->removeUser(this);
}