Files
@ 1a11965611f7
Branch filter:
Location: libtransport.git/src/tests/basictest.h
1a11965611f7
7.5 KiB
text/plain
ConversationManager tests
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 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 | /**
* libtransport -- C++ library for easy XMPP Transports development
*
* Copyright (C) 2011, Jan Kaluza <hanzz.k@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
*/
#pragma once
#include <vector>
#include "Swiften/Swiften.h"
#include "Swiften/Queries/SetResponder.h"
#include "transport/conversation.h"
#include "transport/conversationmanager.h"
#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/userregistration.h"
#include "transport/discoitemsresponder.h"
#include "transport/localbuddy.h"
#include "transport/storagebackend.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) {
onMessageToSend(this, message);
}
boost::signal<void (TestingConversation *, boost::shared_ptr<Swift::Message> &)> onMessageToSend;
};
class TestingFactory : public Factory {
public:
TestingFactory() {
}
// Creates new conversation (NetworkConversation in this case)
Conversation *createConversation(ConversationManager *conversationManager, const std::string &legacyName) {
TestingConversation *nc = new TestingConversation(conversationManager, legacyName);
nc->onMessageToSend.connect(boost::bind(&TestingFactory::handleMessageToSend, this, _1, _2));
return nc;
}
void handleMessageToSend(TestingConversation *_conv, boost::shared_ptr<Swift::Message> &_msg) {
onMessageToSend(_conv, _msg);
}
// Creates new LocalBuddy
Buddy *createBuddy(RosterManager *rosterManager, const BuddyInfo &buddyInfo) {
LocalBuddy *buddy = new LocalBuddy(rosterManager, buddyInfo.id, buddyInfo.legacyName, buddyInfo.alias, buddyInfo.groups, (BuddyFlag) buddyInfo.flags);
if (!buddy->isValid()) {
delete buddy;
return NULL;
}
buddy->setSubscription(Buddy::Ask);
if (buddyInfo.settings.find("icon_hash") != buddyInfo.settings.end())
buddy->setIconHash(buddyInfo.settings.find("icon_hash")->second.s);
return buddy;
}
boost::signal<void (TestingConversation *, boost::shared_ptr<Swift::Message> &)> onMessageToSend;
};
class TestingStorageBackend : public StorageBackend {
public:
bool connected;
std::map<std::string, UserInfo> users;
std::map<std::string, bool> online_users;
std::map<int, std::map<std::string, std::string> > settings;
long buddyid;
TestingStorageBackend() {
buddyid = 0;
connected = false;
}
/// connect
virtual bool connect() {
connected = true;
return true;
}
/// createDatabase
virtual bool createDatabase() {return true;}
/// setUser
virtual void setUser(const UserInfo &user) {
users[user.jid] = user;
}
/// getuser
virtual bool getUser(const std::string &barejid, UserInfo &user) {
if (users.find(barejid) == users.end()) {
return false;
}
user = users[barejid];
return true;
}
std::string findUserByID(long id) {
for (std::map<std::string, UserInfo>::const_iterator it = users.begin(); it != users.end(); it++) {
if (it->second.id == id) {
return it->first;
}
}
return "";
}
/// setUserOnline
virtual void setUserOnline(long id, bool online) {
std::string user = findUserByID(id);
if (user.empty()) {
return;
}
online_users[user] = online;
}
/// removeUser
virtual bool removeUser(long id) {
std::string user = findUserByID(id);
if (user.empty()) {
return false;
}
users.erase(user);
return true;
}
/// getBuddies
virtual bool getBuddies(long id, std::list<BuddyInfo> &roster) {
return true;
}
/// getOnlineUsers
virtual bool getOnlineUsers(std::vector<std::string> &users) {
return true;
}
virtual long addBuddy(long userId, const BuddyInfo &buddyInfo) {
return buddyid++;
}
virtual void updateBuddy(long userId, const BuddyInfo &buddyInfo) {
}
virtual void removeBuddy(long id) {
}
virtual void getBuddySetting(long userId, long buddyId, const std::string &variable, int &type, std::string &value) {}
virtual void updateBuddySetting(long userId, long buddyId, const std::string &variable, int type, const std::string &value) {}
virtual void getUserSetting(long userId, const std::string &variable, int &type, std::string &value) {
if (settings[userId].find(variable) == settings[userId].end()) {
settings[userId][variable] = value;
return;
}
value = settings[userId][variable];
}
virtual void updateUserSetting(long userId, const std::string &variable, const std::string &value) {
settings[userId][variable] = value;
}
virtual void beginTransaction() {}
virtual void commitTransaction() {}
};
class BasicTest : public Swift::XMPPParserClient {
public:
void setMeUp (void);
void tearMeDown (void);
void handleDataReceived(const Swift::SafeByteArray &data);
void handleStreamStart(const Swift::ProtocolHeader&);
void handleElement(boost::shared_ptr<Swift::Element> element);
void handleStreamEnd();
void injectPresence(boost::shared_ptr<Swift::Presence> &response);
void injectIQ(boost::shared_ptr<Swift::IQ> iq);
void injectMessage(boost::shared_ptr<Swift::Message> msg);
void dumpReceived();
void addUser() {
UserInfo user;
user.id = 1;
user.jid = "user@localhost";
user.uin = "legacyname";
user.password = "password";
storage->setUser(user);
}
void connectUser();
void disconnectUser();
void add2Buddies();
Swift::Stanza *getStanza(boost::shared_ptr<Swift::Element> element);
protected:
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;
std::string receivedData;
StorageBackend *storage;
UserRegistration *userRegistration;
DiscoItemsResponder *itemsResponder;
};
|