cyphesis/tests/AccountConnectionIntegration.cpp

398 lines
11 KiB
C++
Raw Permalink Normal View History

2012-01-12 00:37:43 +00:00
// Cyphesis Online RPG Server and AI Engine
// Copyright (C) 2011 Alistair Riddoch
//
// 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 02110-1301 USA
#ifdef NDEBUG
#undef NDEBUG
#endif
#ifndef DEBUG
#define DEBUG
#endif
2012-12-04 21:04:33 +00:00
#include "TestBase.h"
#include "TestWorld.h"
#include "null_stream.h"
#include "server/Connection.h"
#include "server/Lobby.h"
2012-01-12 00:37:43 +00:00
#include "server/Player.h"
#include "server/ServerAccount.h"
#include "server/ServerRouting.h"
2012-01-12 00:37:43 +00:00
#include "server/SystemAccount.h"
2012-01-16 17:15:55 +00:00
#include "common/id.h"
#include "common/TypeNode.h"
#include "common/CommSocket.h"
#include "DatabaseNull.h"
#include "rules/simulation/Entity.h"
2012-01-16 17:15:55 +00:00
#include <Atlas/Objects/Anonymous.h>
#include <Atlas/Objects/Operation.h>
2012-01-12 00:37:43 +00:00
#include <cassert>
#include <server/Persistence.h>
2012-01-12 00:37:43 +00:00
2012-01-12 18:55:59 +00:00
using Atlas::Message::Element;
using Atlas::Message::ListType;
using Atlas::Message::MapType;
using Atlas::Objects::Root;
using Atlas::Objects::Entity::RootEntity;
using Atlas::Objects::Entity::Anonymous;
using Atlas::Objects::Operation::Create;
using Atlas::Objects::Operation::Get;
using Atlas::Objects::Operation::Imaginary;
using Atlas::Objects::Operation::Logout;
using Atlas::Objects::Operation::Look;
using Atlas::Objects::Operation::Set;
using Atlas::Objects::Operation::Talk;
using Atlas::Objects::Operation::Move;
using Atlas::Objects::smart_dynamic_cast;
2012-01-12 00:37:43 +00:00
2012-01-16 17:15:55 +00:00
class SpawningTestWorld : public TestWorld {
public:
SpawningTestWorld(Ref<Entity> gw) : TestWorld(gw) { }
2012-01-16 17:15:55 +00:00
2018-07-24 21:18:50 +02:00
Ref<LocatedEntity> addEntity(const Ref<LocatedEntity>& ent) override{
2018-08-17 22:04:07 +02:00
ent->m_location.m_parent = m_gw;
2012-01-16 17:15:55 +00:00
ent->m_location.m_pos = WFMath::Point<3>(0,0,0);
m_eobjects[ent->getIntId()] = ent;
return ent;
}
2018-07-24 21:18:50 +02:00
Ref<LocatedEntity> addNewEntity(const std::string & t,
const Atlas::Objects::Entity::RootEntity &) override {
2012-01-16 17:15:55 +00:00
std::string id;
long intId = newId(id);
2018-10-27 13:13:45 +02:00
Ref<Entity> e = new Entity(id, intId);
2012-01-16 17:15:55 +00:00
e->setType(new TypeNode(t));
return addEntity(e);
}
};
class TestCommSocket : public CommSocket
{
2012-01-12 00:37:43 +00:00
public:
TestCommSocket() : CommSocket(*(boost::asio::io_service*)0)
{
}
virtual void disconnect()
{
}
virtual int flush()
{
return 0;
}
2012-01-12 00:37:43 +00:00
};
2012-12-04 21:04:33 +00:00
class AccountConnectionintegration : public Cyphesis::TestBase {
protected:
DatabaseNull m_database;
Persistence* m_persistence;
Ref<Entity> m_tlve;
BaseWorld * m_world;
ServerRouting * m_server;
Connection * m_connection;
public:
2012-12-04 21:04:33 +00:00
AccountConnectionintegration();
2012-12-04 21:04:33 +00:00
void setup();
void teardown();
2012-12-04 21:04:33 +00:00
void test_account_creation();
Connection * connection() const { return m_connection; }
ServerRouting * server() const { return m_server; }
};
2012-01-12 00:37:43 +00:00
2012-12-04 21:04:33 +00:00
AccountConnectionintegration::AccountConnectionintegration()
{
ADD_TEST(AccountConnectionintegration::test_account_creation);
}
2012-12-04 21:04:33 +00:00
void AccountConnectionintegration::setup()
2012-01-12 00:37:43 +00:00
{
m_persistence = new Persistence(m_database);
2012-12-04 21:04:33 +00:00
m_tlve = new Entity("0", 0);
m_world = new SpawningTestWorld(m_tlve);
2012-12-04 21:04:33 +00:00
m_server = new ServerRouting(*m_world,
"testrules",
"testname",
"1", 1, "2", 2);
m_connection = new Connection(*new TestCommSocket(),
2012-12-04 21:04:33 +00:00
*m_server,
"test_addr",
"3", 3);
}
2012-12-04 21:04:33 +00:00
void AccountConnectionintegration::teardown()
{
delete m_connection;
delete m_server;
delete m_world;
delete m_persistence;
2012-12-04 21:04:33 +00:00
}
static OpVector test_sent_ops;
void AccountConnectionintegration::test_account_creation()
{
// Basic player account creation
{
2012-12-04 21:04:33 +00:00
ASSERT_NOT_NULL(m_connection);
ASSERT_TRUE(m_connection->objects().empty());
Create op;
Anonymous create_arg;
2017-07-24 13:39:22 +02:00
create_arg->setParent("player");
create_arg->setAttr("username", "39d409ec");
create_arg->setAttr("password", "6a6e71bab281");
op->setArgs1(create_arg);
2012-12-04 21:04:33 +00:00
ASSERT_TRUE(test_sent_ops.empty());
// Send the operation to create the account
2012-12-04 21:04:33 +00:00
m_connection->externalOperation(op, *m_connection);
// There should be a response op
2012-12-04 21:04:33 +00:00
ASSERT_TRUE(!test_sent_ops.empty());
ASSERT_EQUAL(test_sent_ops.size(), 1u);
// and the account creation should have created an object bound
// to this connection.
2012-12-04 21:04:33 +00:00
ASSERT_TRUE(!m_connection->objects().empty());
// Check the response is an info indicating successful account
// creation.
const Operation & reply = test_sent_ops.front();
2012-12-04 21:04:33 +00:00
ASSERT_EQUAL(reply->getClassNo(), Atlas::Objects::Operation::INFO_NO);
// The Info response should have an argument describing the created
// account
const std::vector<Root> & reply_args = reply->getArgs();
2012-12-04 21:04:33 +00:00
ASSERT_TRUE(!reply_args.empty());
RootEntity account = smart_dynamic_cast<RootEntity>(reply_args.front());
2012-12-04 21:04:33 +00:00
ASSERT_TRUE(account.isValid());
// The account ID should be provided
2012-12-04 21:04:33 +00:00
ASSERT_TRUE(!account->isDefaultId());
2012-01-16 17:15:55 +00:00
const std::string account_id = account->getId();
2012-12-04 21:04:33 +00:00
ASSERT_TRUE(!account_id.empty());
// Check the account has been registered in the server object
2012-12-04 21:04:33 +00:00
Router * account_router_ptr = m_server->getObject(account_id);
ASSERT_NOT_NULL(account_router_ptr);
// Check the account has been logged into the lobby
2018-10-27 13:13:45 +02:00
const auto & lobby_dict = m_server->m_lobby.getAccounts();
auto I = lobby_dict.find(account_id);
2012-12-04 21:04:33 +00:00
ASSERT_TRUE(I != lobby_dict.end());
2018-10-27 13:13:45 +02:00
auto account_ptr = I->second;
2012-12-04 21:04:33 +00:00
ASSERT_EQUAL(account_router_ptr, account_ptr);
2012-01-12 20:45:31 +00:00
// Basic login as now been established by account creation
// Set up some other account details
create_arg->setAttr("username", "89cae312");
create_arg->setAttr("password", "d730b8bd2d6c");
// and try an additional account creation, which should fail.
// Multiple logins are ok, but there is no reason to allow multiple
// account creations.
test_sent_ops.clear();
2012-12-04 21:04:33 +00:00
m_connection->externalOperation(op, *m_connection);
ASSERT_TRUE(!test_sent_ops.empty());
ASSERT_EQUAL(test_sent_ops.size(), 1u);
2012-01-16 11:37:59 +00:00
const Operation & error_reply = test_sent_ops.front();
2012-12-04 21:04:33 +00:00
ASSERT_EQUAL(error_reply->getClassNo(),
Atlas::Objects::Operation::ERROR_NO);
2012-01-16 17:15:55 +00:00
2012-01-12 20:45:31 +00:00
// TODO Character creation etc?
// TODO Lobby interaction?
// TODO Logout ?
}
2012-01-12 00:37:43 +00:00
}
2012-12-04 21:04:33 +00:00
int main()
{
AccountConnectionintegration t;
return t.run();
}
2012-01-12 00:37:43 +00:00
// stubs
2012-09-11 22:53:15 +01:00
2018-11-03 16:43:33 +01:00
#include "rules/simulation/ExternalMind.h"
#include "rules/simulation/ExternalProperty.h"
2012-01-12 00:37:43 +00:00
#include "server/Juncture.h"
#include "server/Persistence.h"
#include "server/Ruleset.h"
#include "server/PossessionAuthenticator.h"
2012-01-12 00:37:43 +00:00
#include "common/const.h"
2012-01-12 00:37:43 +00:00
#include "common/globals.h"
#include "common/log.h"
#include "common/Monitors.h"
2012-01-12 00:37:43 +00:00
#include "common/PropertyManager.h"
#include "common/Variable.h"
2012-01-12 00:37:43 +00:00
#include <cstdlib>
#include "stubs/server/stubBuildid.h"
2012-01-12 00:37:43 +00:00
namespace consts {
const char * version = "test_build";
}
// globals - why do we have these again?
2013-11-15 00:16:17 +01:00
const char * const CYPHESIS = "cyphesisAccountConnectionintegration";
int timeoffset = 0;
std::string instance(CYPHESIS);
int CommSocket::flush()
{
return 0;
}
2018-10-27 13:13:45 +02:00
#define STUB_ExternalMind_connectionId
const std::string & ExternalMind::connectionId()
{
assert(m_link != 0);
return m_link->getId();
}
2018-10-27 13:13:45 +02:00
#define STUB_ExternalMind_linkUp
void ExternalMind::linkUp(Link * c)
{
m_link = c;
}
2018-11-03 16:43:33 +01:00
#include "stubs/rules/simulation/stubExternalMind.h"
#include "stubs/rules/simulation/stubExternalProperty.h"
#include "stubs/rules/simulation/stubMindsProperty.h"
#include "stubs/rules/simulation/stubAdminMind.h"
2012-01-12 00:37:43 +00:00
2018-10-27 13:13:45 +02:00
#include "stubs/server/stubJuncture.h"
2018-10-27 13:13:45 +02:00
#define STUB_Link_send
2016-12-19 22:01:23 +01:00
void Link::send(const OpVector& opVector) const
{
for (const auto& op : opVector) {
test_sent_ops.push_back(op);
}
}
void Link::send(const Operation & op) const
{
test_sent_ops.push_back(op);
}
2018-10-27 13:13:45 +02:00
#include "stubs/common/stubLink.h"
2018-05-10 00:21:30 +02:00
#include "server/PropertyRuleHandler.h"
#include "server/ArchetypeRuleHandler.h"
#include "server/EntityRuleHandler.h"
#include "server/OpRuleHandler.h"
#include "stubs/server/stubRuleset.h"
#include "stubs/common/stubDatabase.h"
#include "stubs/server/stubPossessionAuthenticator.h"
#include "stubs/server/stubPersistence.h"
2012-01-12 00:37:43 +00:00
void log(LogLevel lvl, const std::string & msg)
{
std::cout << msg << std::endl;
2012-01-12 00:37:43 +00:00
}
void logEvent(LogEvent lev, const std::string & msg)
{
}
2018-11-03 16:43:33 +01:00
#include "stubs/rules/simulation/stubThing.h"
#include "stubs/rules/simulation/stubEntity.h"
#include "stubs/rules/stubLocatedEntity.h"
2015-04-09 14:50:45 +02:00
#include "stubs/common/stubVariable.h"
#include "stubs/common/stubMonitors.h"
2015-05-23 16:47:45 +02:00
#include "stubs/server/stubExternalMindsManager.h"
#include "stubs/server/stubExternalMindsConnection.h"
#include "stubs/common/stubOperationsDispatcher.h"
2018-11-03 16:43:33 +01:00
#include "stubs/rules/stubLocation.h"
#include "stubs/common/stubPropertyManager.h"
2012-01-12 00:37:43 +00:00
long integerId(const std::string & id)
{
long intId = strtol(id.c_str(), 0, 10);
if (intId == 0 && id != "0") {
intId = -1L;
}
return intId;
}
static long idGenerator = 500;
2012-01-12 00:37:43 +00:00
#include <cstdio>
long newId(std::string & id)
{
static char buf[32];
long new_id = ++idGenerator;
sprintf(buf, "%ld", new_id);
id = buf;
assert(!id.empty());
return new_id;
}
void encrypt_password(const std::string & pwd, std::string & hash)
{
}
int check_password(const std::string & pwd, const std::string & hash)
{
return 0;
}
bool_config_register::bool_config_register(bool & var,
const char * section,
const char * setting,
const char * help)
{
}
2012-10-25 10:37:45 -04:00
void hash_password(const std::string & pwd, const std::string & salt,
std::string & hash)
{
}
#include <common/Shaker.h>
Shaker::Shaker()
{
}
std::string Shaker::generateSalt(size_t length)
{
return "";
}
2018-02-06 10:26:54 +01:00
std::string assets_directory = "";