2011-10-26 00:16:03 +02:00
|
|
|
#include "basictest.h"
|
2015-11-16 10:49:10 +01:00
|
|
|
#include "XMPPFrontend.h"
|
|
|
|
#include "XMPPUserRegistration.h"
|
|
|
|
#include "XMPPUserManager.h"
|
2011-10-26 00:16:03 +02:00
|
|
|
#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"
|
|
|
|
|
2012-08-09 11:21:01 +02:00
|
|
|
#include "Swiften/Serializer/GenericPayloadSerializer.h"
|
|
|
|
|
2015-11-16 10:49:10 +01:00
|
|
|
#include "storageparser.h"
|
2012-08-09 11:21:01 +02:00
|
|
|
#include "Swiften/Parser/PayloadParsers/AttentionParser.h"
|
|
|
|
#include "Swiften/Serializer/PayloadSerializers/AttentionSerializer.h"
|
|
|
|
#include "Swiften/Parser/PayloadParsers/XHTMLIMParser.h"
|
|
|
|
#include "Swiften/Serializer/PayloadSerializers/XHTMLIMSerializer.h"
|
|
|
|
#include "Swiften/Parser/PayloadParsers/StatsParser.h"
|
|
|
|
#include "Swiften/Serializer/PayloadSerializers/StatsSerializer.h"
|
|
|
|
#include "Swiften/Parser/PayloadParsers/GatewayPayloadParser.h"
|
|
|
|
#include "Swiften/Serializer/PayloadSerializers/GatewayPayloadSerializer.h"
|
|
|
|
#include "Swiften/Serializer/PayloadSerializers/SpectrumErrorSerializer.h"
|
|
|
|
#include "Swiften/Parser/PayloadParsers/MUCPayloadParser.h"
|
2015-11-16 10:49:10 +01:00
|
|
|
#include "BlockParser.h"
|
|
|
|
#include "BlockSerializer.h"
|
2012-08-09 11:21:01 +02:00
|
|
|
#include "Swiften/Parser/PayloadParsers/InvisibleParser.h"
|
|
|
|
#include "Swiften/Serializer/PayloadSerializers/InvisibleSerializer.h"
|
|
|
|
|
2011-10-26 00:16:03 +02:00
|
|
|
using namespace Transport;
|
|
|
|
|
|
|
|
void BasicTest::setMeUp (void) {
|
|
|
|
streamEnded = false;
|
2016-03-04 14:06:07 +01:00
|
|
|
std::istringstream ifs("service.server_mode = 1\nservice.jid=localhost\nservice.more_resources=1\nservice.admin_jid=me@localhost\n");
|
2011-10-26 00:16:03 +02:00
|
|
|
cfg = new Config();
|
|
|
|
cfg->load(ifs);
|
|
|
|
|
|
|
|
factory = new TestingFactory();
|
|
|
|
|
2012-08-14 12:53:14 +02:00
|
|
|
storage = new TestingStorageBackend();
|
|
|
|
|
2011-10-26 00:16:03 +02:00
|
|
|
loop = new Swift::DummyEventLoop();
|
|
|
|
factories = new Swift::DummyNetworkFactories(loop);
|
|
|
|
|
|
|
|
userRegistry = new UserRegistry(cfg, factories);
|
|
|
|
|
2015-11-16 10:49:10 +01:00
|
|
|
frontend = new Transport::XMPPFrontend();
|
2011-10-26 00:16:03 +02:00
|
|
|
|
2015-11-16 10:49:10 +01:00
|
|
|
component = new Component(frontend, loop, factories, cfg, factory, userRegistry);
|
|
|
|
component->start();
|
2012-10-18 09:04:20 +02:00
|
|
|
|
2015-11-16 10:49:10 +01:00
|
|
|
userManager = frontend->createUserManager(component, userRegistry, storage);
|
2012-08-14 12:53:14 +02:00
|
|
|
|
2016-02-07 20:53:35 +01:00
|
|
|
itemsResponder = static_cast<XMPPUserManager *>(userManager)->getDiscoItemsResponder();
|
2011-10-26 00:16:03 +02:00
|
|
|
|
|
|
|
payloadSerializers = new Swift::FullPayloadSerializerCollection();
|
|
|
|
payloadParserFactories = new Swift::FullPayloadParserFactoryCollection();
|
2012-08-09 11:21:01 +02:00
|
|
|
|
2016-02-23 12:37:10 +01:00
|
|
|
parserFactories.push_back(new Swift::GenericPayloadParserFactory<StorageParser>("private", "jabber:iq:private"));
|
|
|
|
parserFactories.push_back(new Swift::GenericPayloadParserFactory<Swift::AttentionParser>("attention", "urn:xmpp:attention:0"));
|
|
|
|
parserFactories.push_back(new Swift::GenericPayloadParserFactory<Swift::XHTMLIMParser>("html", "http://jabber.org/protocol/xhtml-im"));
|
|
|
|
parserFactories.push_back(new Swift::GenericPayloadParserFactory<Transport::BlockParser>("block", "urn:xmpp:block:0"));
|
|
|
|
parserFactories.push_back(new Swift::GenericPayloadParserFactory<Swift::InvisibleParser>("invisible", "urn:xmpp:invisible:0"));
|
|
|
|
parserFactories.push_back(new Swift::GenericPayloadParserFactory<Swift::StatsParser>("query", "http://jabber.org/protocol/stats"));
|
|
|
|
parserFactories.push_back(new Swift::GenericPayloadParserFactory<Swift::GatewayPayloadParser>("query", "jabber:iq:gateway"));
|
|
|
|
parserFactories.push_back(new Swift::GenericPayloadParserFactory<Swift::MUCPayloadParser>("x", "http://jabber.org/protocol/muc"));
|
|
|
|
|
|
|
|
BOOST_FOREACH(Swift::PayloadParserFactory *factory, parserFactories) {
|
|
|
|
payloadParserFactories->addFactory(factory);
|
|
|
|
}
|
|
|
|
|
|
|
|
_payloadSerializers.push_back(new Swift::AttentionSerializer());
|
|
|
|
_payloadSerializers.push_back(new Swift::XHTMLIMSerializer());
|
|
|
|
_payloadSerializers.push_back(new Transport::BlockSerializer());
|
|
|
|
_payloadSerializers.push_back(new Swift::InvisibleSerializer());
|
|
|
|
_payloadSerializers.push_back(new Swift::StatsSerializer());
|
|
|
|
_payloadSerializers.push_back(new Swift::SpectrumErrorSerializer());
|
|
|
|
_payloadSerializers.push_back(new Swift::GatewayPayloadSerializer());
|
|
|
|
|
|
|
|
BOOST_FOREACH(Swift::PayloadSerializer *serializer, _payloadSerializers) {
|
|
|
|
payloadSerializers->addSerializer(serializer);
|
|
|
|
}
|
2012-08-09 11:21:01 +02:00
|
|
|
|
2011-10-26 00:16:03 +02:00
|
|
|
parser = new Swift::XMPPParser(this, payloadParserFactories, factories->getXMLParserFactory());
|
2012-09-22 18:09:25 +02:00
|
|
|
parser2 = new Swift::XMPPParser(this, payloadParserFactories, factories->getXMLParserFactory());
|
2011-10-26 00:16:03 +02:00
|
|
|
|
2016-09-12 18:20:58 +02:00
|
|
|
serverFromClientSession = SWIFTEN_SHRPTR_NAMESPACE::shared_ptr<Swift::ServerFromClientSession>(new Swift::ServerFromClientSession("id", factories->getConnectionFactory()->createConnection(),
|
2011-10-26 00:16:03 +02:00
|
|
|
payloadParserFactories, payloadSerializers, userRegistry, factories->getXMLParserFactory(), Swift::JID("user@localhost/resource")));
|
|
|
|
serverFromClientSession->startSession();
|
|
|
|
|
|
|
|
serverFromClientSession->onDataWritten.connect(boost::bind(&BasicTest::handleDataReceived, this, _1));
|
|
|
|
|
2015-11-16 10:49:10 +01:00
|
|
|
dynamic_cast<Swift::ServerStanzaChannel *>(static_cast<XMPPFrontend *>(component->getFrontend())->getStanzaChannel())->addSession(serverFromClientSession);
|
2011-10-26 00:16:03 +02:00
|
|
|
parser->parse("<stream:stream xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' to='localhost' version='1.0'>");
|
2012-09-22 18:09:25 +02:00
|
|
|
parser2->parse("<stream:stream xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' to='localhost' version='1.0'>");
|
2011-10-26 00:16:03 +02:00
|
|
|
received.clear();
|
2012-09-22 18:09:25 +02:00
|
|
|
received2.clear();
|
2012-08-09 11:21:01 +02:00
|
|
|
receivedData.clear();
|
2011-10-26 00:16:03 +02:00
|
|
|
loop->processEvents();
|
|
|
|
}
|
|
|
|
|
|
|
|
void BasicTest::tearMeDown (void) {
|
2015-11-16 10:49:10 +01:00
|
|
|
dynamic_cast<Swift::ServerStanzaChannel *>(static_cast<XMPPFrontend *>(component->getFrontend())->getStanzaChannel())->removeSession(serverFromClientSession);
|
2012-09-22 18:09:25 +02:00
|
|
|
if (serverFromClientSession2) {
|
2015-11-16 10:49:10 +01:00
|
|
|
dynamic_cast<Swift::ServerStanzaChannel *>(static_cast<XMPPFrontend *>(component->getFrontend())->getStanzaChannel())->removeSession(serverFromClientSession2);
|
2012-09-22 18:09:25 +02:00
|
|
|
serverFromClientSession2.reset();
|
|
|
|
}
|
2016-02-23 12:37:10 +01:00
|
|
|
delete userManager;
|
2011-10-26 00:16:03 +02:00
|
|
|
delete component;
|
2015-11-16 10:49:10 +01:00
|
|
|
delete frontend;
|
2011-10-26 00:16:03 +02:00
|
|
|
delete userRegistry;
|
|
|
|
delete factories;
|
|
|
|
delete factory;
|
|
|
|
delete loop;
|
|
|
|
delete cfg;
|
|
|
|
delete parser;
|
2012-09-22 18:09:25 +02:00
|
|
|
delete parser2;
|
2012-08-14 12:53:14 +02:00
|
|
|
delete storage;
|
2015-11-16 10:49:10 +01:00
|
|
|
// delete userRegistration;
|
2011-10-26 00:16:03 +02:00
|
|
|
received.clear();
|
2012-09-22 18:09:25 +02:00
|
|
|
received2.clear();
|
2012-08-09 11:21:01 +02:00
|
|
|
receivedData.clear();
|
2012-09-22 18:09:25 +02:00
|
|
|
receivedData2.clear();
|
2016-02-23 12:37:10 +01:00
|
|
|
|
|
|
|
delete payloadParserFactories;
|
|
|
|
delete payloadSerializers;
|
|
|
|
|
|
|
|
|
|
|
|
BOOST_FOREACH(Swift::PayloadParserFactory *factory, parserFactories) {
|
|
|
|
delete factory;
|
|
|
|
}
|
|
|
|
parserFactories.clear();
|
|
|
|
|
|
|
|
BOOST_FOREACH(Swift::PayloadSerializer *serializer, _payloadSerializers) {
|
|
|
|
delete serializer;
|
|
|
|
}
|
|
|
|
_payloadSerializers.clear();
|
2011-10-26 00:16:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void BasicTest::handleDataReceived(const Swift::SafeByteArray &data) {
|
2011-10-26 16:06:24 +02:00
|
|
|
// std::cout << safeByteArrayToString(data) << "\n";
|
2012-09-22 18:09:25 +02:00
|
|
|
stream1_active = true;
|
2012-08-09 11:21:01 +02:00
|
|
|
receivedData += safeByteArrayToString(data) + "\n";
|
2011-10-26 16:06:24 +02:00
|
|
|
parser->parse(safeByteArrayToString(data));
|
2011-10-26 00:16:03 +02:00
|
|
|
}
|
|
|
|
|
2012-09-22 18:09:25 +02:00
|
|
|
void BasicTest::handleDataReceived2(const Swift::SafeByteArray &data) {
|
|
|
|
// std::cout << safeByteArrayToString(data) << "\n";
|
|
|
|
stream1_active = false;
|
|
|
|
receivedData2 += safeByteArrayToString(data) + "\n";
|
|
|
|
parser2->parse(safeByteArrayToString(data));
|
|
|
|
}
|
|
|
|
|
2011-10-26 00:16:03 +02:00
|
|
|
void BasicTest::handleStreamStart(const Swift::ProtocolHeader&) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-08-09 11:21:01 +02:00
|
|
|
void BasicTest::dumpReceived() {
|
2012-09-22 18:09:25 +02:00
|
|
|
std::cout << "\nStream1:\n";
|
2012-08-09 11:21:01 +02:00
|
|
|
std::cout << receivedData << "\n";
|
2012-09-22 18:09:25 +02:00
|
|
|
std::cout << "Stream2:\n";
|
|
|
|
std::cout << receivedData2 << "\n";
|
2012-08-09 11:21:01 +02:00
|
|
|
}
|
2015-10-15 15:41:16 +03:00
|
|
|
#if HAVE_SWIFTEN_3
|
2016-09-12 18:20:58 +02:00
|
|
|
void BasicTest::handleElement(SWIFTEN_SHRPTR_NAMESPACE::shared_ptr<Swift::ToplevelElement> element) {
|
2015-10-15 15:41:16 +03:00
|
|
|
#else
|
2016-09-12 18:20:58 +02:00
|
|
|
void BasicTest::handleElement(SWIFTEN_SHRPTR_NAMESPACE::shared_ptr<Swift::Element> element) {
|
2015-10-15 15:41:16 +03:00
|
|
|
#endif
|
2012-09-22 18:09:25 +02:00
|
|
|
if (stream1_active) {
|
|
|
|
received.push_back(element);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
received2.push_back(element);
|
|
|
|
}
|
2011-10-26 00:16:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void BasicTest::handleStreamEnd() {
|
|
|
|
streamEnded = true;
|
|
|
|
}
|
|
|
|
|
2016-09-12 18:20:58 +02:00
|
|
|
void BasicTest::injectPresence(SWIFTEN_SHRPTR_NAMESPACE::shared_ptr<Swift::Presence> &response) {
|
2015-11-16 10:49:10 +01:00
|
|
|
dynamic_cast<Swift::ServerStanzaChannel *>(static_cast<XMPPFrontend *>(component->getFrontend())->getStanzaChannel())->onPresenceReceived(response);
|
2011-10-26 16:06:24 +02:00
|
|
|
}
|
|
|
|
|
2016-09-12 18:20:58 +02:00
|
|
|
void BasicTest::injectIQ(SWIFTEN_SHRPTR_NAMESPACE::shared_ptr<Swift::IQ> iq) {
|
2015-11-16 10:49:10 +01:00
|
|
|
dynamic_cast<Swift::ServerStanzaChannel *>(static_cast<XMPPFrontend *>(component->getFrontend())->getStanzaChannel())->onIQReceived(iq);
|
2011-10-26 17:24:33 +02:00
|
|
|
}
|
|
|
|
|
2016-09-12 18:20:58 +02:00
|
|
|
void BasicTest::injectMessage(SWIFTEN_SHRPTR_NAMESPACE::shared_ptr<Swift::Message> msg) {
|
2015-11-16 10:49:10 +01:00
|
|
|
dynamic_cast<Swift::ServerStanzaChannel *>(static_cast<XMPPFrontend *>(component->getFrontend())->getStanzaChannel())->onMessageReceived(msg);
|
2012-08-30 12:37:14 +02:00
|
|
|
}
|
|
|
|
|
2016-09-12 18:20:58 +02:00
|
|
|
Swift::Stanza *BasicTest::getStanza(SWIFTEN_SHRPTR_NAMESPACE::shared_ptr<Swift::Element> element) {
|
2011-10-26 00:16:03 +02:00
|
|
|
Swift::Stanza *stanza = dynamic_cast<Swift::Stanza *>(element.get());
|
|
|
|
CPPUNIT_ASSERT(stanza);
|
|
|
|
return stanza;
|
|
|
|
}
|
|
|
|
|
2012-08-28 13:12:46 +02:00
|
|
|
void BasicTest::connectUser() {
|
|
|
|
CPPUNIT_ASSERT_EQUAL(0, userManager->getUserCount());
|
|
|
|
userRegistry->isValidUserPassword(Swift::JID("user@localhost/resource"), serverFromClientSession.get(), Swift::createSafeByteArray("password"));
|
2012-09-22 18:09:25 +02:00
|
|
|
userRegistry->onPasswordValid(Swift::JID("user@localhost/resource"));
|
2012-08-28 13:12:46 +02:00
|
|
|
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);
|
|
|
|
|
2012-09-22 18:09:25 +02:00
|
|
|
CPPUNIT_ASSERT_EQUAL(2, (int) received.size());
|
2012-08-28 13:12:46 +02:00
|
|
|
CPPUNIT_ASSERT(getStanza(received[0])->getPayload<Swift::DiscoInfo>());
|
2012-10-23 18:27:04 +02:00
|
|
|
|
2012-08-28 13:12:46 +02:00
|
|
|
received.clear();
|
2012-09-22 18:09:25 +02:00
|
|
|
receivedData.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void BasicTest::connectSecondResource() {
|
2016-09-12 18:20:58 +02:00
|
|
|
serverFromClientSession2 = SWIFTEN_SHRPTR_NAMESPACE::shared_ptr<Swift::ServerFromClientSession>(new Swift::ServerFromClientSession("id", factories->getConnectionFactory()->createConnection(),
|
2012-09-22 18:09:25 +02:00
|
|
|
payloadParserFactories, payloadSerializers, userRegistry, factories->getXMLParserFactory(), Swift::JID("user@localhost/resource2")));
|
|
|
|
serverFromClientSession2->startSession();
|
|
|
|
|
|
|
|
serverFromClientSession2->onDataWritten.connect(boost::bind(&BasicTest::handleDataReceived2, this, _1));
|
|
|
|
|
2015-11-16 10:49:10 +01:00
|
|
|
dynamic_cast<Swift::ServerStanzaChannel *>(static_cast<XMPPFrontend *>(component->getFrontend())->getStanzaChannel())->addSession(serverFromClientSession2);
|
2012-09-22 18:09:25 +02:00
|
|
|
|
|
|
|
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>());
|
2012-08-28 13:12:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void BasicTest::disconnectUser() {
|
2012-12-21 09:26:34 +01:00
|
|
|
User *user = userManager->getUser("user@localhost");
|
|
|
|
if (user) {
|
|
|
|
user->addUserSetting("stay_connected", "0");
|
|
|
|
}
|
2013-02-22 09:45:31 +01:00
|
|
|
else {
|
|
|
|
return;
|
|
|
|
}
|
2012-10-23 18:27:04 +02:00
|
|
|
received.clear();
|
2012-08-28 13:12:46 +02:00
|
|
|
userManager->disconnectUser("user@localhost");
|
2012-12-21 09:26:34 +01:00
|
|
|
dynamic_cast<Swift::DummyTimerFactory *>(factories->getTimerFactory())->setTime(100);
|
2012-08-28 13:12:46 +02:00
|
|
|
loop->processEvents();
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT_EQUAL(0, userManager->getUserCount());
|
2016-02-19 10:24:13 +01:00
|
|
|
|
|
|
|
// When user has been in a room, unavailable presence can be sent from that room.
|
|
|
|
if (received.size() == 2) {
|
|
|
|
CPPUNIT_ASSERT(dynamic_cast<Swift::Presence *>(getStanza(received[0])));
|
|
|
|
CPPUNIT_ASSERT(dynamic_cast<Swift::Presence *>(getStanza(received[1])));
|
|
|
|
return;
|
|
|
|
}
|
2012-08-28 13:12:46 +02:00
|
|
|
CPPUNIT_ASSERT_EQUAL(1, (int) received.size());
|
|
|
|
CPPUNIT_ASSERT(dynamic_cast<Swift::Presence *>(getStanza(received[0])));
|
|
|
|
}
|
|
|
|
|
|
|
|
void BasicTest::add2Buddies() {
|
|
|
|
User *user = userManager->getUser("user@localhost");
|
|
|
|
CPPUNIT_ASSERT(user);
|
|
|
|
|
|
|
|
std::vector<std::string> grp;
|
|
|
|
grp.push_back("group1");
|
2016-01-22 15:17:51 +01:00
|
|
|
LocalBuddy *buddy = new LocalBuddy(user->getRosterManager(), -1, "BuddY1", "Buddy 1", grp, BUDDY_JID_ESCAPING);
|
2012-08-28 13:12:46 +02:00
|
|
|
user->getRosterManager()->setBuddy(buddy);
|
2013-01-12 10:09:35 +01:00
|
|
|
buddy->setStatus(Swift::StatusShow(Swift::StatusShow::Away), "status1");
|
2012-08-28 13:12:46 +02:00
|
|
|
|
|
|
|
std::vector<std::string> grp2;
|
|
|
|
grp2.push_back("group2");
|
|
|
|
buddy = new LocalBuddy(user->getRosterManager(), -1, "buddy2", "Buddy 2", grp2, BUDDY_JID_ESCAPING);
|
|
|
|
user->getRosterManager()->setBuddy(buddy);
|
2013-01-12 10:09:35 +01:00
|
|
|
buddy->setStatus(Swift::StatusShow(Swift::StatusShow::Away), "status2");
|
2012-08-28 13:12:46 +02:00
|
|
|
}
|
|
|
|
|