cyphesis/tests/PeerTest.cpp

624 lines
15 KiB
C++
Raw Permalink Normal View History

2010-03-16 01:03:50 +00:00
// Cyphesis Online RPG Server and AI Engine
2010-03-18 01:51:46 +00:00
// Copyright (C) 2010 Alistair Riddoch
2010-03-16 01:03:50 +00:00
//
// 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
2011-02-15 09:30:46 +00:00
#ifdef NDEBUG
#undef NDEBUG
#endif
#ifndef DEBUG
#define DEBUG
#endif
2012-08-01 20:31:29 +01:00
#include "null_stream.h"
2010-03-16 01:03:50 +00:00
#include "server/Peer.h"
#include "server/CommAsioClient_impl.h"
2010-11-25 17:39:38 +00:00
#include "server/CommPeer.h"
2012-11-23 16:11:21 +00:00
#include "rulesets/ExternalMind.h"
2010-11-25 17:12:08 +00:00
2010-11-25 12:16:46 +00:00
#include "common/BaseWorld.h"
#include "common/CommSocket.h"
2010-11-25 17:12:08 +00:00
#include "rulesets/Character.h"
2010-11-25 16:48:51 +00:00
2010-12-03 21:46:22 +00:00
#include <Atlas/Objects/Operation.h>
2010-03-16 01:03:50 +00:00
#include <Atlas/Objects/SmartPtr.h>
#include <cassert>
#include <string>
class TestCommSocket : public CommSocket
{
2012-08-01 20:31:29 +01:00
public:
TestCommSocket() : CommSocket(*(boost::asio::io_service*)0)
{
}
virtual void disconnect()
{
}
virtual int flush()
{
return 0;
}
2012-08-01 20:31:29 +01:00
};
2010-11-25 12:16:46 +00:00
class TestWorld : public BaseWorld {
public:
explicit TestWorld() : BaseWorld(*(LocatedEntity*)0) {
2010-11-25 12:16:46 +00:00
}
virtual bool idle() { return false; }
virtual LocatedEntity * addEntity(LocatedEntity * ent) {
2010-11-25 12:16:46 +00:00
return 0;
}
LocatedEntity * test_addEntity(LocatedEntity * ent, long intId) {
2010-11-25 12:16:46 +00:00
m_eobjects[intId] = ent;
return 0;
}
2010-11-25 17:39:38 +00:00
void test_delEntity(long intId) {
m_eobjects.erase(intId);
}
virtual LocatedEntity * addNewEntity(const std::string &,
2010-11-25 12:16:46 +00:00
const Atlas::Objects::Entity::RootEntity &) {
return 0;
}
void delEntity(LocatedEntity * obj) {}
2010-11-25 12:16:46 +00:00
int createSpawnPoint(const Atlas::Message::MapType & data,
LocatedEntity *) { return 0; }
int removeSpawnPoint(LocatedEntity *) {return 0; }
2010-11-25 12:16:46 +00:00
int getSpawnList(Atlas::Message::ListType & data) { return 0; }
LocatedEntity * spawnNewEntity(const std::string & name,
const std::string & type,
const Atlas::Objects::Entity::RootEntity & desc) {
2010-11-25 12:16:46 +00:00
return addNewEntity(type, desc);
}
virtual int moveToSpawn(const std::string & name,
Location& location){return 0;}
virtual Task * newTask(const std::string &, LocatedEntity &) { return 0; }
2010-11-25 12:16:46 +00:00
virtual Task * activateTask(const std::string &, const std::string &,
LocatedEntity *, LocatedEntity &) { return 0; }
virtual ArithmeticScript * newArithmetic(const std::string &,
LocatedEntity *) {
2010-11-25 12:16:46 +00:00
return 0;
}
virtual void message(const Operation & op,
LocatedEntity & ent) {
2010-11-25 12:16:46 +00:00
// stub_baseworld_receieved_op = op->getClassNo();
}
virtual void messageToClients(const Atlas::Objects::Operation::RootOperation &) {}
virtual LocatedEntity * findByName(const std::string & name) { return 0; }
virtual LocatedEntity * findByType(const std::string & type) { return 0; }
virtual void addPerceptive(LocatedEntity *) { }
2010-11-25 12:16:46 +00:00
};
class TestPeer : public Peer {
public:
2012-08-01 20:31:29 +01:00
TestPeer(CommSocket & client, ServerRouting & svr,
const std::string & addr, const std::string & id, long iid)
2012-07-19 22:58:30 +01:00
: Peer(client, svr, addr, 6767, id, iid)
{
}
const std::string& getAccountType() const {
return m_accountType;
}
};
Atlas::Objects::Operation::RootOperation stub_CommClient_sent_op(0);
2010-11-25 12:16:46 +00:00
int main()
{
TestWorld world;
2010-03-16 01:03:50 +00:00
{
2012-08-01 20:31:29 +01:00
Peer * p = new Peer(*(CommSocket*)0, *(ServerRouting*)0, "addr", 6767, "1", 1);
2010-03-16 01:03:50 +00:00
delete p;
}
{
2012-08-01 20:31:29 +01:00
Peer * p = new Peer(*(CommSocket*)0, *(ServerRouting*)0, "addr", 6767, "1", 1);
2010-03-16 01:03:50 +00:00
Atlas::Objects::Operation::RootOperation op;
OpVector res;
p->operation(op, res);
delete p;
}
2010-08-10 19:21:24 +05:30
{
// Test the setting of authentiaction states
2012-08-01 20:31:29 +01:00
Peer * p = new Peer(*(CommSocket*)0, *(ServerRouting*)0, "addr", 6767, "1", 1);
2010-08-10 19:21:24 +05:30
assert(p->getAuthState() == PEER_INIT);
p->setAuthState(PEER_AUTHENTICATED);
assert(p->getAuthState() == PEER_AUTHENTICATED);
2010-08-19 02:11:06 +05:30
delete p;
2010-08-10 19:21:24 +05:30
}
{
2012-08-01 20:31:29 +01:00
Peer *p = new Peer(*(CommSocket*)0, *(ServerRouting*)0, "addr", 6767, "1", 1);
Atlas::Objects::Operation::Info op;
OpVector res;
p->operation(op, res);
}
2010-11-25 16:11:07 +00:00
// Authenticating (no args)
{
2012-08-01 20:31:29 +01:00
Peer *p = new Peer(*(CommSocket*)0, *(ServerRouting*)0, "addr", 6767, "1", 1);
2010-11-25 16:11:07 +00:00
p->setAuthState(PEER_AUTHENTICATING);
Atlas::Objects::Operation::Info op;
OpVector res;
p->operation(op, res);
}
// Authenticating (empty arg)
{
2012-08-01 20:31:29 +01:00
Peer *p = new Peer(*(CommSocket*)0, *(ServerRouting*)0, "addr", 6767, "1", 1);
2010-11-25 16:11:07 +00:00
p->setAuthState(PEER_AUTHENTICATING);
Atlas::Objects::Operation::Info op;
Atlas::Objects::Root arg;
op->setArgs1(arg);
OpVector res;
p->operation(op, res);
}
// Authenticating (full arg)
{
2012-08-01 20:31:29 +01:00
TestPeer *p = new TestPeer(*(CommSocket*)0, *(ServerRouting*)0, "addr", "1", 1);
2010-11-25 16:11:07 +00:00
p->setAuthState(PEER_AUTHENTICATING);
Atlas::Objects::Operation::Info op;
Atlas::Objects::Root arg;
arg->setId("2");
2017-07-24 13:39:22 +02:00
arg->setParent("server");
2010-11-25 16:11:07 +00:00
op->setArgs1(arg);
OpVector res;
p->operation(op, res);
assert(p->getAccountType() == "server");
2010-11-25 16:11:07 +00:00
}
// Authenticated (no args)
{
2012-08-01 20:31:29 +01:00
Peer *p = new Peer(*(CommSocket*)0, *(ServerRouting*)0, "addr", 6767, "1", 1);
2010-11-25 16:11:07 +00:00
p->setAuthState(PEER_AUTHENTICATED);
Atlas::Objects::Operation::Info op;
OpVector res;
p->operation(op, res);
}
{
2012-08-01 20:31:29 +01:00
Peer *p = new Peer(*(CommSocket*)0, *(ServerRouting*)0, "addr", 6767, "1", 1);
2010-11-25 16:11:07 +00:00
Atlas::Objects::Operation::Error op;
OpVector res;
p->operation(op, res);
}
2010-11-25 16:48:51 +00:00
// Not authenticated
{
2012-08-01 20:31:29 +01:00
Peer *p = new Peer(*(CommSocket*)0, *(ServerRouting*)0, "addr", 6767, "1", 1);
2010-11-25 16:48:51 +00:00
Entity e("3", 3);
int ret = p->teleportEntity(&e);
assert(ret == -1);
}
// Authenticated
{
TestCommSocket client;
2012-07-19 22:58:30 +01:00
Peer *p = new Peer(client, *(ServerRouting*)0, "addr", 6767, "1", 1);
2010-11-25 16:48:51 +00:00
p->setAuthState(PEER_AUTHENTICATED);
Entity e("3", 3);
int ret = p->teleportEntity(&e);
assert(ret == 0);
assert(stub_CommClient_sent_op.isValid());
assert(stub_CommClient_sent_op->getArgs().size() == 1);
2010-11-25 16:48:51 +00:00
}
2010-11-25 17:12:08 +00:00
// Re-teleport same entity
{
TestCommSocket client;
2012-07-19 22:58:30 +01:00
Peer *p = new Peer(client, *(ServerRouting*)0, "addr", 6767, "1", 1);
2010-11-25 17:12:08 +00:00
p->setAuthState(PEER_AUTHENTICATED);
Entity e("3", 3);
int ret = p->teleportEntity(&e);
assert(ret == 0);
assert(stub_CommClient_sent_op.isValid());
assert(stub_CommClient_sent_op->getArgs().size() == 1);
2010-11-25 17:12:08 +00:00
ret = p->teleportEntity(&e);
assert(ret != 0);
}
// Character (no mind)
{
TestCommSocket client;
2012-07-19 22:58:30 +01:00
Peer *p = new Peer(client, *(ServerRouting*)0, "addr", 6767, "1", 1);
2010-11-25 17:12:08 +00:00
p->setAuthState(PEER_AUTHENTICATED);
Character e("3", 3);
int ret = p->teleportEntity(&e);
assert(ret == 0);
assert(stub_CommClient_sent_op.isValid());
assert(stub_CommClient_sent_op->getArgs().size() == 1);
2010-11-25 17:12:08 +00:00
}
// Character (externl mind, unconnected)
{
TestCommSocket client;
2012-07-19 22:58:30 +01:00
Peer *p = new Peer(client, *(ServerRouting*)0, "addr", 6767, "1", 1);
2010-11-25 17:12:08 +00:00
p->setAuthState(PEER_AUTHENTICATED);
Character e("3", 3);
e.m_externalMind = new ExternalMind(e);
int ret = p->teleportEntity(&e);
assert(ret == 0);
assert(stub_CommClient_sent_op.isValid());
assert(stub_CommClient_sent_op->getArgs().size() == 1);
2010-11-25 17:12:08 +00:00
}
// Character (externl mind, connected)
{
TestCommSocket client;
2012-07-19 22:58:30 +01:00
Peer *p = new Peer(client, *(ServerRouting*)0, "addr", 6767, "1", 1);
2010-11-25 17:12:08 +00:00
p->setAuthState(PEER_AUTHENTICATED);
Character e("3", 3);
ExternalMind * mind = new ExternalMind(e);
mind->linkUp((Link*)23);
2010-11-25 17:12:08 +00:00
e.m_externalMind = mind;
int ret = p->teleportEntity(&e);
assert(ret == 0);
assert(stub_CommClient_sent_op.isValid());
assert(stub_CommClient_sent_op->getArgs().size() == 2);
2010-11-25 17:12:08 +00:00
}
2010-11-25 17:39:38 +00:00
// No arg
{
2012-08-01 20:31:29 +01:00
Peer *p = new Peer(*(CommSocket*)0, *(ServerRouting*)0, "addr", 6767, "1", 1);
2010-11-25 17:39:38 +00:00
Atlas::Objects::Operation::Info op;
OpVector res;
p->peerTeleportResponse(op, res);
}
// Empty arg, no refno
{
2012-08-01 20:31:29 +01:00
Peer *p = new Peer(*(CommSocket*)0, *(ServerRouting*)0, "addr", 6767, "1", 1);
2010-11-25 17:39:38 +00:00
Atlas::Objects::Operation::Info op;
OpVector res;
Atlas::Objects::Root arg;
op->setArgs1(arg);
p->peerTeleportResponse(op, res);
}
// Empty arg, made up refno, not CommPeer
{
TestCommSocket client;
Peer *p = new Peer(client, *(ServerRouting*)0, "addr", 6767, "1", 1);
2010-11-25 17:39:38 +00:00
Atlas::Objects::Operation::Info op;
OpVector res;
Atlas::Objects::Root arg;
op->setArgs1(arg);
op->setRefno(23);
p->peerTeleportResponse(op, res);
}
// Empty arg, made up refno
{
TestCommSocket client;
Peer *p = new Peer(client, *(ServerRouting*)0, "addr", 6767, "1", 1);
2010-11-25 17:39:38 +00:00
Atlas::Objects::Operation::Info op;
OpVector res;
Atlas::Objects::Root arg;
op->setArgs1(arg);
op->setRefno(23);
p->peerTeleportResponse(op, res);
}
// Empty arg, refno that matches earlier teleport, not in world
{
TestCommSocket client;
Peer *p = new Peer(client, *(ServerRouting*)0, "addr", 6767, "1", 1);
2010-11-25 17:39:38 +00:00
p->setAuthState(PEER_AUTHENTICATED);
Entity e("23", 23);
int ret = p->teleportEntity(&e);
assert(ret == 0);
Atlas::Objects::Operation::Info op;
OpVector res;
Atlas::Objects::Root arg;
op->setArgs1(arg);
op->setRefno(23);
p->peerTeleportResponse(op, res);
}
// Empty arg, refno that matches earlier teleport, in world
{
TestCommSocket client;
Peer *p = new Peer(client, *(ServerRouting*)0, "addr", 6767, "1", 1);
2010-11-25 17:39:38 +00:00
p->setAuthState(PEER_AUTHENTICATED);
Entity e("23", 23);
int ret = p->teleportEntity(&e);
assert(ret == 0);
world.test_addEntity(&e, 23);
Atlas::Objects::Operation::Info op;
OpVector res;
Atlas::Objects::Root arg;
op->setArgs1(arg);
op->setRefno(23);
p->peerTeleportResponse(op, res);
world.test_delEntity(23);
}
// Empty arg, refno that matches earlier teleport, with mind
{
TestCommSocket client;
Peer *p = new Peer(client, *(ServerRouting*)0, "addr", 6767, "1", 1);
2010-11-25 17:39:38 +00:00
p->setAuthState(PEER_AUTHENTICATED);
Character e("23", 23);
ExternalMind * mind = new ExternalMind(e);
mind->linkUp((Link*)23);
2010-11-25 17:39:38 +00:00
e.m_externalMind = mind;
int ret = p->teleportEntity(&e);
assert(ret == 0);
world.test_addEntity(&e, 23);
Atlas::Objects::Operation::Info op;
OpVector res;
Atlas::Objects::Root arg;
op->setArgs1(arg);
op->setRefno(23);
p->peerTeleportResponse(op, res);
world.test_delEntity(23);
}
// No teleports to clear
2010-11-25 16:11:07 +00:00
{
2012-08-01 20:31:29 +01:00
Peer *p = new Peer(*(CommSocket*)0, *(ServerRouting*)0, "addr", 6767, "1", 1);
2010-11-25 16:11:07 +00:00
p->cleanTeleports();
}
2010-11-25 17:39:38 +00:00
// One teleport to clear
{
TestCommSocket client;
2012-07-19 22:58:30 +01:00
Peer *p = new Peer(client, *(ServerRouting*)0, "addr", 6767, "1", 1);
2010-11-25 17:39:38 +00:00
p->setAuthState(PEER_AUTHENTICATED);
Entity e("23", 23);
int ret = p->teleportEntity(&e);
assert(ret == 0);
p->cleanTeleports();
}
2010-03-16 01:03:50 +00:00
return 0;
}
2010-11-25 12:16:46 +00:00
// stubs
#include "server/TeleportState.h"
#include "rulesets/Character.h"
#include "rulesets/Script.h"
#include "common/id.h"
#include "common/log.h"
#include "common/TypeNode.h"
#include <Atlas/Negotiate.h>
#include <cstdlib>
using Atlas::Message::MapType;
TeleportState::TeleportState(boost::posix_time::ptime time) : m_state(TELEPORT_NONE),
2010-11-25 14:29:44 +00:00
m_teleportTime(time)
2010-11-25 12:16:46 +00:00
{
}
void TeleportState::setRequested()
{
m_state = TELEPORT_REQUESTED;
}
void TeleportState::setCreated()
{
m_state = TELEPORT_CREATED;
}
void TeleportState::setKey(const std::string & key)
{
m_possessKey = key;
}
CommPeer::CommPeer(const std::string & name,
boost::asio::io_service& io_service) :
CommAsioClient<boost::asio::ip::tcp>(name, io_service), m_auth_timer(io_service)
2010-11-25 17:39:38 +00:00
{
}
2010-11-25 12:16:46 +00:00
CommPeer::~CommPeer()
{
}
CommSocket::CommSocket(boost::asio::io_service& io_service) : m_io_service(io_service) { }
2010-11-25 12:16:46 +00:00
CommSocket::~CommSocket()
{
}
2012-08-01 20:31:29 +01:00
int CommSocket::flush()
2010-11-25 12:16:46 +00:00
{
2012-08-01 20:31:29 +01:00
return 0;
2010-11-25 12:16:46 +00:00
}
ExternalMind::ExternalMind(LocatedEntity & e) : Router(e.getId(), e.getIntId()),
m_link(0), m_entity(e)
2010-11-25 12:16:46 +00:00
{
}
void ExternalMind::externalOperation(const Operation &, Link &)
{
}
2010-11-25 12:16:46 +00:00
void ExternalMind::operation(const Operation & op, OpVector & res)
{
}
void ExternalMind::linkUp(Link * c)
2010-11-25 17:12:08 +00:00
{
m_link = c;
2010-11-25 17:12:08 +00:00
}
#include "stubs/rulesets/stubCharacter.h"
#include "stubs/rulesets/stubThing.h"
#include "stubs/rulesets/stubEntity.h"
#include "stubs/rulesets/stubLocatedEntity.h"
#include "stubs/common/stubRouter.h"
2010-11-25 12:16:46 +00:00
2012-08-01 20:31:29 +01:00
Link::Link(CommSocket & socket, const std::string & id, long iid) :
Router(id, iid), m_encoder(0), m_commSocket(socket)
{
}
Link::~Link()
{
}
void Link::send(const Operation & op) const
{
stub_CommClient_sent_op = op;
2012-08-01 20:31:29 +01:00
}
void Link::disconnect()
{
}
#include "stubs/rulesets/stubScript.h"
#include "stubs/common/stubTypeNode.h"
#include "stubs/modules/stubLocation.h"
2010-11-25 12:16:46 +00:00
BaseWorld * BaseWorld::m_instance = 0;
BaseWorld::BaseWorld(LocatedEntity & gw) : m_gameWorld(gw)
2010-11-25 12:16:46 +00:00
{
m_instance = this;
}
BaseWorld::~BaseWorld()
{
m_instance = 0;
}
LocatedEntity * BaseWorld::getEntity(const std::string & id) const
2010-11-25 12:16:46 +00:00
{
long intId = integerId(id);
EntityDict::const_iterator I = m_eobjects.find(intId);
if (I != m_eobjects.end()) {
assert(I->second != 0);
return I->second;
} else {
return 0;
}
}
LocatedEntity * BaseWorld::getEntity(long id) const
2010-11-25 12:16:46 +00:00
{
EntityDict::const_iterator I = m_eobjects.find(id);
if (I != m_eobjects.end()) {
assert(I->second != 0);
return I->second;
} else {
return 0;
}
}
void log(LogLevel lvl, const std::string & msg)
{
}
2010-11-30 22:05:38 +00:00
void logEvent(LogEvent lev, const std::string & msg)
{
}
2010-11-25 12:16:46 +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;
}