Merge branch 'master' of https://github.com/hanzz/libtransport
This commit is contained in:
commit
13e8c5e499
5 changed files with 257 additions and 6 deletions
|
@ -7,10 +7,22 @@ if( SWIFTEN_LIBRARY AND SWIFTEN_INCLUDE_DIR )
|
|||
if (SWIFTEN_CONFIG_EXECUTABLE)
|
||||
execute_process(
|
||||
COMMAND ${SWIFTEN_CONFIG_EXECUTABLE} --libs
|
||||
OUTPUT_VARIABLE SWIFTEN_LIBRARY)
|
||||
string(REGEX REPLACE "[\r\n]" " " SWIFTEN_LIBRARY ${SWIFTEN_LIBRARY})
|
||||
string(REGEX REPLACE " +$" "" SWIFTEN_LIBRARY ${SWIFTEN_LIBRARY})
|
||||
string(REGEX REPLACE " " ";" SWIFTEN_LIBRARY ${SWIFTEN_LIBRARY})
|
||||
OUTPUT_VARIABLE SWIFTEN_LIB)
|
||||
string(REGEX REPLACE "[\r\n]" " " SWIFTEN_LIB ${SWIFTEN_LIB})
|
||||
string(REGEX REPLACE " +$" "" SWIFTEN_LIB ${SWIFTEN_LIB})
|
||||
string(REGEX REPLACE " " ";" SWIFTEN_LIB ${SWIFTEN_LIB})
|
||||
set(SWIFTEN_LIBRARY "")
|
||||
foreach(f ${SWIFTEN_LIB})
|
||||
STRING(SUBSTRING ${f} 0 2 f_out)
|
||||
STRING(COMPARE EQUAL ${f_out} "/l" IS_PATH)
|
||||
if(${IS_PATH})
|
||||
message(${f})
|
||||
string(REGEX REPLACE "/libpath:" "" f_replaced ${f})
|
||||
link_directories(${f_replaced})
|
||||
else()
|
||||
list(APPEND SWIFTEN_LIBRARY ${f})
|
||||
endif()
|
||||
endforeach(f)
|
||||
else()
|
||||
message( FATAL_ERROR "Could NOT find swiften-config" )
|
||||
endif()
|
||||
|
|
|
@ -149,9 +149,14 @@ static void start_all_instances(ManagerConfig *config) {
|
|||
std::cerr << "Can't load config file " << itr->path().string() << ". Skipping...\n";
|
||||
}
|
||||
|
||||
if (!isRunning(CONFIG_STRING(&cfg, "service.pidfile"))) {
|
||||
int pid = isRunning(CONFIG_STRING(&cfg, "service.pidfile"));
|
||||
if (pid == 0) {
|
||||
std::cout << "Starting " << itr->path() << ": OK\n";
|
||||
exec_(spectrum2_binary, itr->path().string());
|
||||
}
|
||||
else {
|
||||
std::cout << "Starting " << itr->path() << ": Already started (PID=" << pid << ")\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -185,8 +190,12 @@ static void stop_all_instances(ManagerConfig *config) {
|
|||
|
||||
int pid = isRunning(CONFIG_STRING(&cfg, "service.pidfile"));
|
||||
if (pid) {
|
||||
std::cout << "Stopping " << itr->path() << ": OK\n";
|
||||
kill(pid, SIGTERM);
|
||||
}
|
||||
else {
|
||||
std::cout << "Stopping " << itr->path() << ": Not running\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -55,10 +55,13 @@ class TestingFactory : public Factory {
|
|||
class ComponentTest : public CPPUNIT_NS :: TestFixture, public Swift::XMPPParserClient {
|
||||
CPPUNIT_TEST_SUITE(ComponentTest);
|
||||
CPPUNIT_TEST(handlePresenceWithNode);
|
||||
CPPUNIT_TEST(handlePresenceWithoutNode);
|
||||
CPPUNIT_TEST_SUITE_END();
|
||||
|
||||
public:
|
||||
void setUp (void) {
|
||||
onUserPresenceReceived = false;
|
||||
onUserDiscoInfoReceived = false;
|
||||
std::istringstream ifs("service.server_mode = 1\n");
|
||||
cfg = new Config();
|
||||
cfg->load(ifs);
|
||||
|
@ -71,6 +74,8 @@ class ComponentTest : public CPPUNIT_NS :: TestFixture, public Swift::XMPPParser
|
|||
userRegistry = new UserRegistry(cfg, factories);
|
||||
|
||||
component = new Component(loop, factories, cfg, factory, userRegistry);
|
||||
component->onUserPresenceReceived.connect(boost::bind(&ComponentTest::handleUserPresenceReceived, this, _1));
|
||||
component->onUserDiscoInfoReceived.connect(boost::bind(&ComponentTest::handleUserDiscoInfoReceived, this, _1, _2));
|
||||
component->start();
|
||||
|
||||
payloadSerializers = new Swift::FullPayloadSerializerCollection();
|
||||
|
@ -101,6 +106,14 @@ class ComponentTest : public CPPUNIT_NS :: TestFixture, public Swift::XMPPParser
|
|||
received.clear();
|
||||
}
|
||||
|
||||
void handleUserDiscoInfoReceived(const Swift::JID& jid, boost::shared_ptr<Swift::DiscoInfo> info) {
|
||||
onUserDiscoInfoReceived = true;
|
||||
}
|
||||
|
||||
void handleUserPresenceReceived(Swift::Presence::ref presence) {
|
||||
onUserPresenceReceived = true;
|
||||
}
|
||||
|
||||
void handleDataReceived(const Swift::SafeByteArray &data) {
|
||||
parser->parse(safeByteArrayToString(data));
|
||||
}
|
||||
|
@ -118,15 +131,35 @@ class ComponentTest : public CPPUNIT_NS :: TestFixture, public Swift::XMPPParser
|
|||
}
|
||||
|
||||
void handlePresenceWithNode() {
|
||||
Swift::Presence::ref response = Swift::Presence::create();
|
||||
response->setTo("somebody@localhost");
|
||||
response->setFrom("user@localhost/resource");
|
||||
dynamic_cast<Swift::ServerStanzaChannel *>(component->getStanzaChannel())->onPresenceReceived(response);
|
||||
|
||||
loop->processEvents();
|
||||
CPPUNIT_ASSERT_EQUAL(0, (int) received.size());
|
||||
}
|
||||
|
||||
void handlePresenceWithoutNode() {
|
||||
Swift::Presence::ref response = Swift::Presence::create();
|
||||
response->setTo("localhost");
|
||||
response->setFrom("user@localhost/resource");
|
||||
dynamic_cast<Swift::ServerStanzaChannel *>(component->getStanzaChannel())->onPresenceReceived(response);
|
||||
|
||||
loop->processEvents();
|
||||
CPPUNIT_ASSERT(getStanza(received[0])->getPayload<Swift::DiscoInfo>());
|
||||
CPPUNIT_ASSERT(onUserPresenceReceived);
|
||||
}
|
||||
|
||||
Swift::Stanza *getStanza(boost::shared_ptr<Swift::Element> element) {
|
||||
Swift::Stanza *stanza = dynamic_cast<Swift::Stanza *>(element.get());
|
||||
CPPUNIT_ASSERT(stanza);
|
||||
return stanza;
|
||||
}
|
||||
|
||||
private:
|
||||
bool onUserPresenceReceived;
|
||||
bool onUserDiscoInfoReceived;
|
||||
boost::shared_ptr<Swift::ServerFromClientSession> serverFromClientSession;
|
||||
Swift::FullPayloadSerializerCollection* payloadSerializers;
|
||||
Swift::FullPayloadParserFactoryCollection* payloadParserFactories;
|
||||
|
|
198
src/tests/usermanager.cpp
Normal file
198
src/tests/usermanager.cpp
Normal file
|
@ -0,0 +1,198 @@
|
|||
#include "transport/userregistry.h"
|
||||
#include "transport/config.h"
|
||||
#include "transport/storagebackend.h"
|
||||
#include "transport/user.h"
|
||||
#include "transport/transport.h"
|
||||
#include "transport/conversation.h"
|
||||
#include "transport/usermanager.h"
|
||||
#include "transport/localbuddy.h"
|
||||
#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"
|
||||
|
||||
using namespace Transport;
|
||||
|
||||
class TestingConversation : public Conversation {
|
||||
public:
|
||||
TestingConversation(ConversationManager *conversationManager, const std::string &legacyName, bool muc = false) : Conversation(conversationManager, legacyName, muc) {
|
||||
}
|
||||
|
||||
// Called when there's new message to legacy network from XMPP network
|
||||
void sendMessage(boost::shared_ptr<Swift::Message> &message) {
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
class TestingFactory : public Factory {
|
||||
public:
|
||||
TestingFactory() {
|
||||
}
|
||||
|
||||
// Creates new conversation (NetworkConversation in this case)
|
||||
Conversation *createConversation(ConversationManager *conversationManager, const std::string &legacyName) {
|
||||
Conversation *nc = new TestingConversation(conversationManager, legacyName);
|
||||
return nc;
|
||||
}
|
||||
|
||||
// Creates new LocalBuddy
|
||||
Buddy *createBuddy(RosterManager *rosterManager, const BuddyInfo &buddyInfo) {
|
||||
LocalBuddy *buddy = new LocalBuddy(rosterManager, buddyInfo.id);
|
||||
buddy->setAlias(buddyInfo.alias);
|
||||
buddy->setName(buddyInfo.legacyName);
|
||||
buddy->setSubscription(buddyInfo.subscription);
|
||||
buddy->setGroups(buddyInfo.groups);
|
||||
buddy->setFlags((BuddyFlag) buddyInfo.flags);
|
||||
if (buddyInfo.settings.find("icon_hash") != buddyInfo.settings.end())
|
||||
buddy->setIconHash(buddyInfo.settings.find("icon_hash")->second.s);
|
||||
return buddy;
|
||||
}
|
||||
};
|
||||
|
||||
class UserManagerTest : public CPPUNIT_NS :: TestFixture, public Swift::XMPPParserClient {
|
||||
CPPUNIT_TEST_SUITE(UserManagerTest);
|
||||
CPPUNIT_TEST(connectUser);
|
||||
CPPUNIT_TEST(handleProbePresence);
|
||||
CPPUNIT_TEST(disconnectUser);
|
||||
CPPUNIT_TEST_SUITE_END();
|
||||
|
||||
public:
|
||||
void setUp (void) {
|
||||
streamEnded = false;
|
||||
std::istringstream ifs("service.server_mode = 1\n");
|
||||
cfg = new Config();
|
||||
cfg->load(ifs);
|
||||
|
||||
factory = new TestingFactory();
|
||||
|
||||
loop = new Swift::DummyEventLoop();
|
||||
factories = new Swift::DummyNetworkFactories(loop);
|
||||
|
||||
userRegistry = new UserRegistry(cfg, factories);
|
||||
|
||||
component = new Component(loop, factories, cfg, factory, userRegistry);
|
||||
component->start();
|
||||
|
||||
userManager = new UserManager(component, userRegistry);
|
||||
|
||||
payloadSerializers = new Swift::FullPayloadSerializerCollection();
|
||||
payloadParserFactories = new Swift::FullPayloadParserFactoryCollection();
|
||||
parser = new Swift::XMPPParser(this, payloadParserFactories, factories->getXMLParserFactory());
|
||||
|
||||
serverFromClientSession = boost::shared_ptr<Swift::ServerFromClientSession>(new Swift::ServerFromClientSession("id", factories->getConnectionFactory()->createConnection(),
|
||||
payloadParserFactories, payloadSerializers, userRegistry, factories->getXMLParserFactory(), Swift::JID("user@localhost/resource")));
|
||||
serverFromClientSession->startSession();
|
||||
|
||||
serverFromClientSession->onDataWritten.connect(boost::bind(&UserManagerTest::handleDataReceived, this, _1));
|
||||
|
||||
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'>");
|
||||
received.clear();
|
||||
loop->processEvents();
|
||||
}
|
||||
|
||||
void tearDown (void) {
|
||||
dynamic_cast<Swift::ServerStanzaChannel *>(component->getStanzaChannel())->removeSession(serverFromClientSession);
|
||||
delete component;
|
||||
delete userRegistry;
|
||||
delete factories;
|
||||
delete factory;
|
||||
delete loop;
|
||||
delete cfg;
|
||||
delete parser;
|
||||
received.clear();
|
||||
}
|
||||
|
||||
void handleDataReceived(const Swift::SafeByteArray &data) {
|
||||
parser->parse(safeByteArrayToString(data));
|
||||
}
|
||||
|
||||
void handleStreamStart(const Swift::ProtocolHeader&) {
|
||||
|
||||
}
|
||||
|
||||
void handleElement(boost::shared_ptr<Swift::Element> element) {
|
||||
received.push_back(element);
|
||||
}
|
||||
|
||||
void handleStreamEnd() {
|
||||
streamEnded = true;
|
||||
}
|
||||
|
||||
void connectUser() {
|
||||
CPPUNIT_ASSERT_EQUAL(0, userManager->getUserCount());
|
||||
userRegistry->isValidUserPassword(Swift::JID("user@localhost/resource"), serverFromClientSession.get(), Swift::createSafeByteArray("password"));
|
||||
loop->processEvents();
|
||||
CPPUNIT_ASSERT_EQUAL(1, userManager->getUserCount());
|
||||
|
||||
User *user = userManager->getUser("user@localhost");
|
||||
CPPUNIT_ASSERT(user);
|
||||
|
||||
UserInfo userInfo = user->getUserInfo();
|
||||
CPPUNIT_ASSERT_EQUAL(std::string("password"), userInfo.password);
|
||||
CPPUNIT_ASSERT(user->isReadyToConnect() == true);
|
||||
CPPUNIT_ASSERT(user->isConnected() == false);
|
||||
|
||||
user->setConnected(true);
|
||||
CPPUNIT_ASSERT(user->isConnected() == true);
|
||||
|
||||
CPPUNIT_ASSERT_EQUAL(1, (int) received.size());
|
||||
CPPUNIT_ASSERT(getStanza(received[0])->getPayload<Swift::DiscoInfo>());
|
||||
}
|
||||
|
||||
void disconnectUser() {
|
||||
connectUser();
|
||||
received.clear();
|
||||
|
||||
userManager->disconnectUser("user@localhost");
|
||||
dynamic_cast<Swift::DummyTimerFactory *>(factories->getTimerFactory())->setTime(10);
|
||||
loop->processEvents();
|
||||
|
||||
CPPUNIT_ASSERT_EQUAL(0, userManager->getUserCount());
|
||||
CPPUNIT_ASSERT_EQUAL(1, (int) received.size());
|
||||
CPPUNIT_ASSERT(dynamic_cast<Swift::Presence *>(getStanza(received[0])));
|
||||
}
|
||||
|
||||
void handleProbePresence() {
|
||||
Swift::Presence::ref response = Swift::Presence::create();
|
||||
response->setTo("localhost");
|
||||
response->setFrom("user@localhost/resource");
|
||||
response->setType(Swift::Presence::Probe);
|
||||
dynamic_cast<Swift::ServerStanzaChannel *>(component->getStanzaChannel())->onPresenceReceived(response);
|
||||
loop->processEvents();
|
||||
|
||||
CPPUNIT_ASSERT_EQUAL(2, (int) received.size());
|
||||
CPPUNIT_ASSERT(getStanza(received[0])->getPayload<Swift::DiscoInfo>());
|
||||
CPPUNIT_ASSERT(dynamic_cast<Swift::Presence *>(getStanza(received[1])));
|
||||
}
|
||||
|
||||
Swift::Stanza *getStanza(boost::shared_ptr<Swift::Element> element) {
|
||||
Swift::Stanza *stanza = dynamic_cast<Swift::Stanza *>(element.get());
|
||||
CPPUNIT_ASSERT(stanza);
|
||||
return stanza;
|
||||
}
|
||||
|
||||
private:
|
||||
bool streamEnded;
|
||||
UserManager *userManager;
|
||||
boost::shared_ptr<Swift::ServerFromClientSession> serverFromClientSession;
|
||||
Swift::FullPayloadSerializerCollection* payloadSerializers;
|
||||
Swift::FullPayloadParserFactoryCollection* payloadParserFactories;
|
||||
Swift::XMPPParser *parser;
|
||||
UserRegistry *userRegistry;
|
||||
Config *cfg;
|
||||
Swift::Server *server;
|
||||
Swift::DummyNetworkFactories *factories;
|
||||
Swift::DummyEventLoop *loop;
|
||||
TestingFactory *factory;
|
||||
Component *component;
|
||||
std::vector<boost::shared_ptr<Swift::Element> > received;
|
||||
};
|
||||
|
||||
CPPUNIT_TEST_SUITE_REGISTRATION (UserManagerTest);
|
|
@ -251,7 +251,6 @@ void Component::handlePresence(Swift::Presence::ref presence) {
|
|||
}
|
||||
|
||||
// check if we have this client's capabilities and ask for them
|
||||
// bool haveFeatures = false;
|
||||
if (presence->getType() != Swift::Presence::Unavailable) {
|
||||
boost::shared_ptr<CapsInfo> capsInfo = presence->getPayload<CapsInfo>();
|
||||
if (capsInfo && capsInfo->getHash() == "sha-1") {
|
||||
|
|
Loading…
Add table
Reference in a new issue