Unit tests can now test more resources interaction

This commit is contained in:
HanzZ 2012-09-22 18:09:25 +02:00
parent 3ff7f92490
commit 2f542b5077
5 changed files with 93 additions and 7 deletions

View file

@ -110,6 +110,10 @@ class User : public Swift::EntityCapsProvider {
return m_connected; return m_connected;
} }
int getResourceCount() {
return m_resources;
}
boost::signal<void ()> onReadyToConnect; boost::signal<void ()> onReadyToConnect;
boost::signal<void (Swift::Presence::ref presence)> onPresenceChanged; boost::signal<void (Swift::Presence::ref presence)> onPresenceChanged;
boost::signal<void (const Swift::JID &who, const std::string &room, const std::string &nickname, const std::string &password)> onRoomJoined; boost::signal<void (const Swift::JID &who, const std::string &room, const std::string &nickname, const std::string &password)> onRoomJoined;

View file

@ -41,7 +41,7 @@ using namespace Transport;
void BasicTest::setMeUp (void) { void BasicTest::setMeUp (void) {
streamEnded = false; streamEnded = false;
std::istringstream ifs("service.server_mode = 1\nservice.jid=localhost"); std::istringstream ifs("service.server_mode = 1\nservice.jid=localhost\nservice.more_resources=1\n");
cfg = new Config(); cfg = new Config();
cfg->load(ifs); cfg->load(ifs);
@ -86,6 +86,7 @@ void BasicTest::setMeUp (void) {
payloadSerializers->addSerializer(new Swift::GatewayPayloadSerializer()); payloadSerializers->addSerializer(new Swift::GatewayPayloadSerializer());
parser = new Swift::XMPPParser(this, payloadParserFactories, factories->getXMLParserFactory()); parser = new Swift::XMPPParser(this, payloadParserFactories, factories->getXMLParserFactory());
parser2 = new Swift::XMPPParser(this, payloadParserFactories, factories->getXMLParserFactory());
serverFromClientSession = boost::shared_ptr<Swift::ServerFromClientSession>(new Swift::ServerFromClientSession("id", factories->getConnectionFactory()->createConnection(), serverFromClientSession = boost::shared_ptr<Swift::ServerFromClientSession>(new Swift::ServerFromClientSession("id", factories->getConnectionFactory()->createConnection(),
payloadParserFactories, payloadSerializers, userRegistry, factories->getXMLParserFactory(), Swift::JID("user@localhost/resource"))); payloadParserFactories, payloadSerializers, userRegistry, factories->getXMLParserFactory(), Swift::JID("user@localhost/resource")));
@ -95,13 +96,19 @@ void BasicTest::setMeUp (void) {
dynamic_cast<Swift::ServerStanzaChannel *>(component->getStanzaChannel())->addSession(serverFromClientSession); dynamic_cast<Swift::ServerStanzaChannel *>(component->getStanzaChannel())->addSession(serverFromClientSession);
parser->parse("<stream:stream xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' to='localhost' version='1.0'>"); parser->parse("<stream:stream xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' to='localhost' version='1.0'>");
parser2->parse("<stream:stream xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' to='localhost' version='1.0'>");
received.clear(); received.clear();
received2.clear();
receivedData.clear(); receivedData.clear();
loop->processEvents(); loop->processEvents();
} }
void BasicTest::tearMeDown (void) { void BasicTest::tearMeDown (void) {
dynamic_cast<Swift::ServerStanzaChannel *>(component->getStanzaChannel())->removeSession(serverFromClientSession); dynamic_cast<Swift::ServerStanzaChannel *>(component->getStanzaChannel())->removeSession(serverFromClientSession);
if (serverFromClientSession2) {
dynamic_cast<Swift::ServerStanzaChannel *>(component->getStanzaChannel())->removeSession(serverFromClientSession2);
serverFromClientSession2.reset();
}
delete component; delete component;
delete userRegistry; delete userRegistry;
delete factories; delete factories;
@ -109,29 +116,48 @@ void BasicTest::tearMeDown (void) {
delete loop; delete loop;
delete cfg; delete cfg;
delete parser; delete parser;
delete parser2;
delete storage; delete storage;
delete userRegistration; delete userRegistration;
delete itemsResponder; delete itemsResponder;
received.clear(); received.clear();
received2.clear();
receivedData.clear(); receivedData.clear();
receivedData2.clear();
} }
void BasicTest::handleDataReceived(const Swift::SafeByteArray &data) { void BasicTest::handleDataReceived(const Swift::SafeByteArray &data) {
// std::cout << safeByteArrayToString(data) << "\n"; // std::cout << safeByteArrayToString(data) << "\n";
stream1_active = true;
receivedData += safeByteArrayToString(data) + "\n"; receivedData += safeByteArrayToString(data) + "\n";
parser->parse(safeByteArrayToString(data)); parser->parse(safeByteArrayToString(data));
} }
void BasicTest::handleDataReceived2(const Swift::SafeByteArray &data) {
// std::cout << safeByteArrayToString(data) << "\n";
stream1_active = false;
receivedData2 += safeByteArrayToString(data) + "\n";
parser2->parse(safeByteArrayToString(data));
}
void BasicTest::handleStreamStart(const Swift::ProtocolHeader&) { void BasicTest::handleStreamStart(const Swift::ProtocolHeader&) {
} }
void BasicTest::dumpReceived() { void BasicTest::dumpReceived() {
std::cout << "\nStream1:\n";
std::cout << receivedData << "\n"; std::cout << receivedData << "\n";
std::cout << "Stream2:\n";
std::cout << receivedData2 << "\n";
} }
void BasicTest::handleElement(boost::shared_ptr<Swift::Element> element) { void BasicTest::handleElement(boost::shared_ptr<Swift::Element> element) {
received.push_back(element); if (stream1_active) {
received.push_back(element);
}
else {
received2.push_back(element);
}
} }
void BasicTest::handleStreamEnd() { void BasicTest::handleStreamEnd() {
@ -159,6 +185,7 @@ Swift::Stanza *BasicTest::getStanza(boost::shared_ptr<Swift::Element> element) {
void BasicTest::connectUser() { void BasicTest::connectUser() {
CPPUNIT_ASSERT_EQUAL(0, userManager->getUserCount()); CPPUNIT_ASSERT_EQUAL(0, userManager->getUserCount());
userRegistry->isValidUserPassword(Swift::JID("user@localhost/resource"), serverFromClientSession.get(), Swift::createSafeByteArray("password")); userRegistry->isValidUserPassword(Swift::JID("user@localhost/resource"), serverFromClientSession.get(), Swift::createSafeByteArray("password"));
userRegistry->onPasswordValid(Swift::JID("user@localhost/resource"));
loop->processEvents(); loop->processEvents();
CPPUNIT_ASSERT_EQUAL(1, userManager->getUserCount()); CPPUNIT_ASSERT_EQUAL(1, userManager->getUserCount());
@ -173,9 +200,39 @@ void BasicTest::connectUser() {
user->setConnected(true); user->setConnected(true);
CPPUNIT_ASSERT(user->isConnected() == true); CPPUNIT_ASSERT(user->isConnected() == true);
CPPUNIT_ASSERT_EQUAL(1, (int) received.size()); CPPUNIT_ASSERT_EQUAL(2, (int) received.size());
CPPUNIT_ASSERT(getStanza(received[0])->getPayload<Swift::DiscoInfo>()); CPPUNIT_ASSERT(getStanza(received[0])->getPayload<Swift::DiscoInfo>());
received.clear(); received.clear();
receivedData.clear();
}
void BasicTest::connectSecondResource() {
serverFromClientSession2 = boost::shared_ptr<Swift::ServerFromClientSession>(new Swift::ServerFromClientSession("id", factories->getConnectionFactory()->createConnection(),
payloadParserFactories, payloadSerializers, userRegistry, factories->getXMLParserFactory(), Swift::JID("user@localhost/resource2")));
serverFromClientSession2->startSession();
serverFromClientSession2->onDataWritten.connect(boost::bind(&BasicTest::handleDataReceived2, this, _1));
dynamic_cast<Swift::ServerStanzaChannel *>(component->getStanzaChannel())->addSession(serverFromClientSession2);
userRegistry->isValidUserPassword(Swift::JID("user@localhost/resource2"), serverFromClientSession2.get(), Swift::createSafeByteArray("password"));
userRegistry->onPasswordValid(Swift::JID("user@localhost/resource2"));
loop->processEvents();
Swift::Presence::ref response = Swift::Presence::create();
response->setTo("localhost");
response->setFrom("user@localhost/resource2");
injectPresence(response);
loop->processEvents();
CPPUNIT_ASSERT_EQUAL(1, userManager->getUserCount());
User *user = userManager->getUser("user@localhost");
CPPUNIT_ASSERT(user);
CPPUNIT_ASSERT_EQUAL(2, user->getResourceCount());
CPPUNIT_ASSERT(getStanza(received2[1])->getPayload<Swift::DiscoInfo>());
} }
void BasicTest::disconnectUser() { void BasicTest::disconnectUser() {

View file

@ -205,6 +205,7 @@ class BasicTest : public Swift::XMPPParserClient {
void tearMeDown (void); void tearMeDown (void);
void handleDataReceived(const Swift::SafeByteArray &data); void handleDataReceived(const Swift::SafeByteArray &data);
void handleDataReceived2(const Swift::SafeByteArray &data);
void handleStreamStart(const Swift::ProtocolHeader&); void handleStreamStart(const Swift::ProtocolHeader&);
@ -228,6 +229,7 @@ class BasicTest : public Swift::XMPPParserClient {
} }
void connectUser(); void connectUser();
void connectSecondResource();
void disconnectUser(); void disconnectUser();
void add2Buddies(); void add2Buddies();
@ -237,9 +239,11 @@ class BasicTest : public Swift::XMPPParserClient {
bool streamEnded; bool streamEnded;
UserManager *userManager; UserManager *userManager;
boost::shared_ptr<Swift::ServerFromClientSession> serverFromClientSession; boost::shared_ptr<Swift::ServerFromClientSession> serverFromClientSession;
boost::shared_ptr<Swift::ServerFromClientSession> serverFromClientSession2;
Swift::FullPayloadSerializerCollection* payloadSerializers; Swift::FullPayloadSerializerCollection* payloadSerializers;
Swift::FullPayloadParserFactoryCollection* payloadParserFactories; Swift::FullPayloadParserFactoryCollection* payloadParserFactories;
Swift::XMPPParser *parser; Swift::XMPPParser *parser;
Swift::XMPPParser *parser2;
UserRegistry *userRegistry; UserRegistry *userRegistry;
Config *cfg; Config *cfg;
Swift::Server *server; Swift::Server *server;
@ -248,9 +252,12 @@ class BasicTest : public Swift::XMPPParserClient {
TestingFactory *factory; TestingFactory *factory;
Component *component; Component *component;
std::vector<boost::shared_ptr<Swift::Element> > received; std::vector<boost::shared_ptr<Swift::Element> > received;
std::vector<boost::shared_ptr<Swift::Element> > received2;
std::string receivedData; std::string receivedData;
std::string receivedData2;
StorageBackend *storage; StorageBackend *storage;
UserRegistration *userRegistration; UserRegistration *userRegistration;
DiscoItemsResponder *itemsResponder; DiscoItemsResponder *itemsResponder;
bool stream1_active;
}; };

View file

@ -23,6 +23,7 @@ using namespace Transport;
class UserManagerTest : public CPPUNIT_NS :: TestFixture, public BasicTest { class UserManagerTest : public CPPUNIT_NS :: TestFixture, public BasicTest {
CPPUNIT_TEST_SUITE(UserManagerTest); CPPUNIT_TEST_SUITE(UserManagerTest);
CPPUNIT_TEST(connectUser); CPPUNIT_TEST(connectUser);
CPPUNIT_TEST(connectTwoResources);
CPPUNIT_TEST(connectUserTransportDisabled); CPPUNIT_TEST(connectUserTransportDisabled);
CPPUNIT_TEST(connectUserRegistrationNeeded); CPPUNIT_TEST(connectUserRegistrationNeeded);
CPPUNIT_TEST(connectUserRegistrationNeededRegistered); CPPUNIT_TEST(connectUserRegistrationNeededRegistered);
@ -86,6 +87,22 @@ class UserManagerTest : public CPPUNIT_NS :: TestFixture, public BasicTest {
CPPUNIT_ASSERT_EQUAL(Swift::Presence::Unavailable, presence->getType()); CPPUNIT_ASSERT_EQUAL(Swift::Presence::Unavailable, presence->getType());
} }
void connectTwoResources() {
connectUser();
add2Buddies();
connectSecondResource();
// we should get presences
CPPUNIT_ASSERT_EQUAL(4, (int) received2.size());
CPPUNIT_ASSERT(dynamic_cast<Swift::Presence *>(getStanza(received2[2])));
CPPUNIT_ASSERT_EQUAL(Swift::StatusShow::Away, dynamic_cast<Swift::Presence *>(getStanza(received2[2]))->getShow());
CPPUNIT_ASSERT_EQUAL(std::string("status1"), dynamic_cast<Swift::Presence *>(getStanza(received2[2]))->getStatus());
CPPUNIT_ASSERT(dynamic_cast<Swift::Presence *>(getStanza(received2[3])));
CPPUNIT_ASSERT_EQUAL(Swift::StatusShow::Away, dynamic_cast<Swift::Presence *>(getStanza(received2[3]))->getShow());
CPPUNIT_ASSERT_EQUAL(std::string("status2"), dynamic_cast<Swift::Presence *>(getStanza(received2[3]))->getStatus());
}
}; };
CPPUNIT_TEST_SUITE_REGISTRATION (UserManagerTest); CPPUNIT_TEST_SUITE_REGISTRATION (UserManagerTest);

View file

@ -266,13 +266,14 @@ void User::handlePresence(Swift::Presence::ref presence) {
} }
else { else {
sendCurrentPresence(); sendCurrentPresence();
// This resource is new, so we have to send buddies presences
if (currentResourcesCount != m_resources) {
m_rosterManager->sendCurrentPresences(presence->getFrom());
}
} }
} }
// This resource is new, so we have to send buddies presences
if (presence->getType() != Swift::Presence::Unavailable && currentResourcesCount != m_resources) {
m_rosterManager->sendCurrentPresences(presence->getFrom());
}
m_resources = currentResourcesCount; m_resources = currentResourcesCount;