Files
@ d4769080ca30
Branch filter:
Location: libtransport.git/src/tests/userregistry.cpp
d4769080ca30
8.8 KiB
text/x-c++hdr
More stats
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 | #include "transport/userregistry.h"
#include "transport/config.h"
#include <cppunit/TestFixture.h>
#include <cppunit/extensions/HelperMacros.h>
#include <Swiften/EventLoop/DummyEventLoop.h>
#include <Swiften/Server/Server.h>
#include <Swiften/Network/DummyNetworkFactories.h>
#include <Swiften/Network/DummyConnectionServer.h>
using namespace Transport;
class UserRegistryTest : public CPPUNIT_NS :: TestFixture {
CPPUNIT_TEST_SUITE(UserRegistryTest);
CPPUNIT_TEST(login);
CPPUNIT_TEST(loginInvalidPassword);
CPPUNIT_TEST(loginTwoClientsValidPasswords);
CPPUNIT_TEST(loginTwoClientsDifferentPasswords);
CPPUNIT_TEST(loginDisconnect);
CPPUNIT_TEST(removeLater);
CPPUNIT_TEST_SUITE_END();
public:
void setUp (void) {
state1 = Init;
state2 = Init;
std::istringstream ifs;
cfg = new Config();
cfg->load(ifs);
loop = new Swift::DummyEventLoop();
factories = new Swift::DummyNetworkFactories(loop);
userRegistry = new UserRegistry(cfg, factories);
userRegistry->onConnectUser.connect(bind(&UserRegistryTest::handleConnectUser, this, _1));
userRegistry->onDisconnectUser.connect(bind(&UserRegistryTest::handleDisconnectUser, this, _1));
server = new Swift::Server(loop, factories, userRegistry, "localhost", 5222);
server->start();
connectionServer = server->getConnectionServer();
client1 = factories->getConnectionFactory()->createConnection();
dynamic_cast<Swift::DummyConnectionServer *>(connectionServer.get())->acceptConnection(client1);
dynamic_cast<Swift::DummyConnection *>(client1.get())->onDataSent.connect(boost::bind(&UserRegistryTest::handleDataReceived, this, _1, client1));
client2 = factories->getConnectionFactory()->createConnection();
dynamic_cast<Swift::DummyConnectionServer *>(connectionServer.get())->acceptConnection(client2);
dynamic_cast<Swift::DummyConnection *>(client2.get())->onDataSent.connect(boost::bind(&UserRegistryTest::handleDataReceived, this, _1, client2));
loop->processEvents();
}
void tearDown (void) {
delete server;
dynamic_cast<Swift::DummyConnection *>(client1.get())->onDataSent.disconnect(boost::bind(&UserRegistryTest::handleDataReceived, this, _1, client1));
client1.reset();
dynamic_cast<Swift::DummyConnection *>(client2.get())->onDataSent.disconnect(boost::bind(&UserRegistryTest::handleDataReceived, this, _1, client2));
client2.reset();
connectionServer.reset();
delete userRegistry;
delete factories;
delete loop;
delete cfg;
received1.clear();
received2.clear();
}
void send(boost::shared_ptr<Swift::Connection> conn, const std::string &data) {
dynamic_cast<Swift::DummyConnection *>(conn.get())->receive(Swift::createSafeByteArray(data));
loop->processEvents();
}
void sendCredentials(boost::shared_ptr<Swift::Connection> conn, const std::string &username, const std::string &password, const std::string &b64) {
std::vector<std::string> &received = conn == client1 ? received1 : received2;
send(conn, "<stream:stream xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' to='localhost' version='1.0'>");
CPPUNIT_ASSERT_EQUAL(2, (int) received.size());
CPPUNIT_ASSERT(received[0].find("<?xml version=\"1.0\"?>") == 0);
CPPUNIT_ASSERT(received[1].find("PLAIN") != std::string::npos);
received.clear();
// username:test
send(conn, "<auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl' mechanism='PLAIN'>" + b64 + "</auth>");
if (conn == client1)
CPPUNIT_ASSERT_EQUAL(Connecting, state1);
// else
// CPPUNIT_ASSERT_EQUAL(Connecting, state2);
CPPUNIT_ASSERT_EQUAL(password, userRegistry->getUserPassword(username));
CPPUNIT_ASSERT_EQUAL(std::string(""), userRegistry->getUserPassword("unknown@localhost"));
}
void bindSession(boost::shared_ptr<Swift::Connection> conn) {
std::vector<std::string> &received = conn == client1 ? received1 : received2;
send(conn, "<stream:stream xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' to='localhost' version='1.0'>");
CPPUNIT_ASSERT_EQUAL(2, (int) received.size());
CPPUNIT_ASSERT(received[0].find("<?xml version=\"1.0\"?>") == 0);
CPPUNIT_ASSERT(received[1].find("urn:ietf:params:xml:ns:xmpp-bind") != std::string::npos);
CPPUNIT_ASSERT(received[1].find("urn:ietf:params:xml:ns:xmpp-session") != std::string::npos);
}
void handleDataReceived(const Swift::SafeByteArray &data, boost::shared_ptr<Swift::Connection> conn) {
if (conn == client1) {
received1.push_back(safeByteArrayToString(data));
// std::cout << received1.back() << "\n";
}
else {
received2.push_back(safeByteArrayToString(data));
// std::cout << received2.back() << "\n";
}
}
void handleConnectUser(const Swift::JID &user) {
state1 = Connecting;
}
void handleDisconnectUser(const Swift::JID &user) {
state1 = Disconnected;
}
void login() {
sendCredentials(client1, "username@localhost", "test", "AHVzZXJuYW1lAHRlc3Q=");
userRegistry->onPasswordValid("username@localhost");
loop->processEvents();
CPPUNIT_ASSERT_EQUAL(std::string(""), userRegistry->getUserPassword("username@localhost"));
CPPUNIT_ASSERT_EQUAL(1, (int) received1.size());
CPPUNIT_ASSERT_EQUAL(std::string("<success xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\"></success>"), received1[0]);
received1.clear();
bindSession(client1);
}
void loginInvalidPassword() {
sendCredentials(client1, "username@localhost", "test", "AHVzZXJuYW1lAHRlc3Q=");
userRegistry->onPasswordInvalid("username@localhost");
loop->processEvents();
CPPUNIT_ASSERT_EQUAL(std::string(""), userRegistry->getUserPassword("username@localhost"));
CPPUNIT_ASSERT_EQUAL(2, (int) received1.size());
CPPUNIT_ASSERT_EQUAL(std::string("<failure xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\"/>"), received1[0]);
CPPUNIT_ASSERT_EQUAL(std::string("</stream:stream>"), received1[1]);
}
void loginTwoClientsValidPasswords() {
sendCredentials(client1, "username@localhost", "test", "AHVzZXJuYW1lAHRlc3Q=");
sendCredentials(client2, "username@localhost", "test", "AHVzZXJuYW1lAHRlc3Q=");
CPPUNIT_ASSERT_EQUAL(2, (int) received1.size());
CPPUNIT_ASSERT_EQUAL(0, (int) received2.size());
CPPUNIT_ASSERT_EQUAL(std::string("<failure xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\"/>"), received1[0]);
CPPUNIT_ASSERT_EQUAL(std::string("</stream:stream>"), received1[1]);
userRegistry->onPasswordValid("username@localhost");
loop->processEvents();
CPPUNIT_ASSERT_EQUAL(std::string(""), userRegistry->getUserPassword("username@localhost"));
CPPUNIT_ASSERT_EQUAL(1, (int) received2.size());
CPPUNIT_ASSERT_EQUAL(std::string("<success xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\"></success>"), received2[0]);
}
void loginTwoClientsDifferentPasswords() {
// first is valid, second invalid.
sendCredentials(client1, "username@localhost", "test", "AHVzZXJuYW1lAHRlc3Q=");
sendCredentials(client2, "username@localhost", "test2", "AHVzZXJuYW1lAHRlc3Qy");
CPPUNIT_ASSERT_EQUAL(2, (int) received1.size());
CPPUNIT_ASSERT_EQUAL(0, (int) received2.size());
CPPUNIT_ASSERT_EQUAL(std::string("<failure xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\"/>"), received1[0]);
CPPUNIT_ASSERT_EQUAL(std::string("</stream:stream>"), received1[1]);
userRegistry->onPasswordValid("username@localhost");
loop->processEvents();
CPPUNIT_ASSERT_EQUAL(std::string(""), userRegistry->getUserPassword("username@localhost"));
CPPUNIT_ASSERT_EQUAL(1, (int) received2.size());
CPPUNIT_ASSERT_EQUAL(std::string("<success xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\"></success>"), received2[0]);
}
void loginDisconnect() {
sendCredentials(client1, "username@localhost", "test", "AHVzZXJuYW1lAHRlc3Q=");
client1->onDisconnected(boost::optional<Swift::Connection::Error>());
loop->processEvents();
CPPUNIT_ASSERT_EQUAL(Disconnected, state1);
}
void removeLater() {
sendCredentials(client1, "username@localhost", "test", "AHVzZXJuYW1lAHRlc3Q=");
userRegistry->removeLater("username@localhost");
loop->processEvents();
CPPUNIT_ASSERT_EQUAL(0, (int) received1.size());
dynamic_cast<Swift::DummyTimerFactory *>(factories->getTimerFactory())->setTime(10);
loop->processEvents();
CPPUNIT_ASSERT_EQUAL(2, (int) received1.size());
CPPUNIT_ASSERT_EQUAL(std::string("<failure xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\"/>"), received1[0]);
CPPUNIT_ASSERT_EQUAL(std::string("</stream:stream>"), received1[1]);
}
private:
typedef enum {
Init,
Connecting,
Disconnected,
} State;
UserRegistry *userRegistry;
Config *cfg;
Swift::Server *server;
Swift::DummyNetworkFactories *factories;
Swift::DummyEventLoop *loop;
boost::shared_ptr<Swift::ConnectionServer> connectionServer;
boost::shared_ptr<Swift::Connection> client1;
boost::shared_ptr<Swift::Connection> client2;
std::vector<std::string> received1;
std::vector<std::string> received2;
State state1;
State state2;
};
CPPUNIT_TEST_SUITE_REGISTRATION (UserRegistryTest);
|