From da535d6bf47bd732a0529f6f5e371cff4dc73ce8 Mon Sep 17 00:00:00 2001 From: HanzZ Date: Thu, 20 Oct 2011 16:26:20 +0200 Subject: [PATCH 1/4] Show some output from spectrum2_manager --- spectrum_manager/src/main.cpp | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/spectrum_manager/src/main.cpp b/spectrum_manager/src/main.cpp index 6a4d1efe..6c74057a 100644 --- a/spectrum_manager/src/main.cpp +++ b/spectrum_manager/src/main.cpp @@ -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"; + } } } } From ab886a7ae7d2613c7ec5a0bfa3453674166ce7af Mon Sep 17 00:00:00 2001 From: HanzZ Date: Thu, 20 Oct 2011 18:31:34 +0200 Subject: [PATCH 2/4] Split library path and libraries for swiften --- cmake_modules/SwiftenConfig.cmake | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/cmake_modules/SwiftenConfig.cmake b/cmake_modules/SwiftenConfig.cmake index 69aeb3d8..9181b09a 100644 --- a/cmake_modules/SwiftenConfig.cmake +++ b/cmake_modules/SwiftenConfig.cmake @@ -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() From 225a246407f7a6ef39da2c5191194138c1d97423 Mon Sep 17 00:00:00 2001 From: HanzZ Date: Thu, 20 Oct 2011 19:42:39 +0200 Subject: [PATCH 3/4] More tests --- src/tests/component.cpp | 33 ++++++++ src/tests/usermanager.cpp | 166 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 199 insertions(+) create mode 100644 src/tests/usermanager.cpp diff --git a/src/tests/component.cpp b/src/tests/component.cpp index ac02a5b0..8d4b3be9 100644 --- a/src/tests/component.cpp +++ b/src/tests/component.cpp @@ -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 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(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(component->getStanzaChannel())->onPresenceReceived(response); loop->processEvents(); + CPPUNIT_ASSERT(getStanza(received[0])->getPayload()); + CPPUNIT_ASSERT(onUserPresenceReceived); + } + + Swift::Stanza *getStanza(boost::shared_ptr element) { + Swift::Stanza *stanza = dynamic_cast(element.get()); + CPPUNIT_ASSERT(stanza); + return stanza; } private: + bool onUserPresenceReceived; + bool onUserDiscoInfoReceived; boost::shared_ptr serverFromClientSession; Swift::FullPayloadSerializerCollection* payloadSerializers; Swift::FullPayloadParserFactoryCollection* payloadParserFactories; diff --git a/src/tests/usermanager.cpp b/src/tests/usermanager.cpp new file mode 100644 index 00000000..bd559a54 --- /dev/null +++ b/src/tests/usermanager.cpp @@ -0,0 +1,166 @@ +#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 +#include +#include +#include +#include +#include +#include +#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 &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_SUITE_END(); + + public: + void setUp (void) { + 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(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(component->getStanzaChannel())->addSession(serverFromClientSession); + parser->parse(""); + received.clear(); + loop->processEvents(); + } + + void tearDown (void) { + dynamic_cast(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 element) { + received.push_back(element); + } + + void handleStreamEnd() { + + } + + 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); + } + + + Swift::Stanza *getStanza(boost::shared_ptr element) { + Swift::Stanza *stanza = dynamic_cast(element.get()); + CPPUNIT_ASSERT(stanza); + return stanza; + } + + private: + UserManager *userManager; + boost::shared_ptr 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 > received; +}; + +CPPUNIT_TEST_SUITE_REGISTRATION (UserManagerTest); From 25642926dd9ddcb1c2a2ec6e397e07bdc19a5773 Mon Sep 17 00:00:00 2001 From: HanzZ Date: Thu, 20 Oct 2011 20:52:42 +0200 Subject: [PATCH 4/4] more tests --- src/tests/usermanager.cpp | 34 +++++++++++++++++++++++++++++++++- src/transport.cpp | 1 - 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/tests/usermanager.cpp b/src/tests/usermanager.cpp index bd559a54..d0084ddf 100644 --- a/src/tests/usermanager.cpp +++ b/src/tests/usermanager.cpp @@ -58,10 +58,13 @@ class TestingFactory : public Factory { 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); @@ -119,7 +122,7 @@ class UserManagerTest : public CPPUNIT_NS :: TestFixture, public Swift::XMPPPars } void handleStreamEnd() { - + streamEnded = true; } void connectUser() { @@ -138,8 +141,36 @@ class UserManagerTest : public CPPUNIT_NS :: TestFixture, public Swift::XMPPPars user->setConnected(true); CPPUNIT_ASSERT(user->isConnected() == true); + + CPPUNIT_ASSERT_EQUAL(1, (int) received.size()); + CPPUNIT_ASSERT(getStanza(received[0])->getPayload()); } + void disconnectUser() { + connectUser(); + received.clear(); + + userManager->disconnectUser("user@localhost"); + dynamic_cast(factories->getTimerFactory())->setTime(10); + loop->processEvents(); + + CPPUNIT_ASSERT_EQUAL(0, userManager->getUserCount()); + CPPUNIT_ASSERT_EQUAL(1, (int) received.size()); + CPPUNIT_ASSERT(dynamic_cast(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(component->getStanzaChannel())->onPresenceReceived(response); + loop->processEvents(); + + CPPUNIT_ASSERT_EQUAL(2, (int) received.size()); + CPPUNIT_ASSERT(getStanza(received[0])->getPayload()); + CPPUNIT_ASSERT(dynamic_cast(getStanza(received[1]))); + } Swift::Stanza *getStanza(boost::shared_ptr element) { Swift::Stanza *stanza = dynamic_cast(element.get()); @@ -148,6 +179,7 @@ class UserManagerTest : public CPPUNIT_NS :: TestFixture, public Swift::XMPPPars } private: + bool streamEnded; UserManager *userManager; boost::shared_ptr serverFromClientSession; Swift::FullPayloadSerializerCollection* payloadSerializers; diff --git a/src/transport.cpp b/src/transport.cpp index cb2cdbb6..e9d87cb5 100644 --- a/src/transport.cpp +++ b/src/transport.cpp @@ -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 = presence->getPayload(); if (capsInfo && capsInfo->getHash() == "sha-1") {