Files
@ ca227ccd2638
Branch filter:
Location: libtransport.git/src/tests/component.cpp
ca227ccd2638
6.2 KiB
text/x-c++hdr
Better PM
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 | #include "transport/userregistry.h"
#include "transport/config.h"
#include "transport/transport.h"
#include "transport/conversation.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 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);
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->onUserPresenceReceived.connect(boost::bind(&ComponentTest::handleUserPresenceReceived, this, _1));
component->onUserDiscoInfoReceived.connect(boost::bind(&ComponentTest::handleUserDiscoInfoReceived, this, _1, _2));
component->start();
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(&ComponentTest::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 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));
}
void handleStreamStart(const Swift::ProtocolHeader&) {
}
void handleElement(boost::shared_ptr<Swift::Element> element) {
received.push_back(element);
}
void handleStreamEnd() {
}
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;
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 (ComponentTest);
|