cyphesis/tests/ServerAccounttest.cpp

247 lines
6.6 KiB
C++
Raw Normal View History

2010-06-30 18:16:22 +05:30
// Cyphesis Online RPG Server and AI Engine
// Copyright (C) 2009 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
// $Id$
2011-02-15 09:30:46 +00:00
#ifdef NDEBUG
#undef NDEBUG
#endif
#ifndef DEBUG
#define DEBUG
#endif
2010-06-30 18:16:22 +05:30
#include "server/ServerAccount.h"
#include "server/CommServer.h"
#include "server/ServerRouting.h"
#include "server/CommClient.h"
#include "server/Connection.h"
#include "server/WorldRouter.h"
#include "server/Ruleset.h"
#include "server/Player.h"
#include "rulesets/Python_API.h"
#include "rulesets/Entity.h"
#include "rulesets/Character.h"
2011-05-05 18:00:14 +01:00
#include "rulesets/Domain.h"
#include "rulesets/World.h"
2010-06-30 18:16:22 +05:30
#include "common/Monitor.h"
#include "common/Connect.h"
2011-10-31 12:47:56 +01:00
#include "common/SystemTime.h"
2010-06-30 18:16:22 +05:30
#include "TestWorld.h"
#include <Atlas/Objects/SmartPtr.h>
#include <Atlas/Objects/Anonymous.h>
#include <Atlas/Objects/Operation.h>
#include <cassert>
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::Logout;
using Atlas::Objects::Operation::Connect;
class TestCommClient : public CommClient {
public:
2011-10-30 01:04:54 +02:00
TestCommClient(CommServer & cs) : CommClient(cs, "") { }
2010-06-30 18:16:22 +05:30
};
class TestServerAccount : public ServerAccount {
public:
TestServerAccount(Connection * conn, const std::string & username,
const std::string & passwd,
const std::string & id, long intId) :
ServerAccount(conn, username, passwd, id, intId) {
}
virtual int testCharacterError(const Operation & op,
const Atlas::Objects::Root & ent,
2010-06-30 18:16:22 +05:30
OpVector & res) const {
return characterError(op, ent, res);
}
Entity * testAddNewCharacter(const std::string & typestr,
const Atlas::Objects::Entity::RootEntity & ent,
const Atlas::Objects::Entity::RootEntity & arg)
{
return addNewCharacter(typestr, ent, arg);
}
};
2010-07-01 03:54:19 +05:30
void run_operation_checks(TestServerAccount * ac, Entity * chr, WorldRouter & world)
2010-06-30 18:16:22 +05:30
{
// Entity injection test
{
Anonymous ent;
// Add the test attributes
ent->setAttr("objtype", "obj");
ent->setAttr("name", "test_entity");
ent->setParents(std::list<std::string>(1,"thing"));
Create op;
OpVector res;
op->setArgs1(ent);
ac->operation(op, res);
2010-07-01 03:54:19 +05:30
Entity *reply = world.findByName("test_entity");
assert(reply != 0);
}
// Regular create op tests
2010-06-30 18:16:22 +05:30
{
// This is the only op we've overridden
Create op;
OpVector res;
ac->operation(op, res);
op->setArgs1(Root());
ac->operation(op, res);
Anonymous op_arg;
op->setArgs1(op_arg);
ac->operation(op, res);
op_arg->setParents(std::list<std::string>());
ac->operation(op, res);
op_arg->setParents(std::list<std::string>(1, "game_entity"));
ac->operation(op, res);
op_arg->setObjtype("obj");
ac->operation(op, res);
op_arg->setName("Bob");
ac->operation(op, res);
op_arg->setObjtype("class");
ac->operation(op, res);
op_arg->setId("game_entity");
ac->operation(op, res);
op_arg->setId("new_class");
ac->operation(op, res);
op_arg->setParents(std::list<std::string>(1, ""));
ac->operation(op, res);
op_arg->setParents(std::list<std::string>(1, "non_exist"));
ac->operation(op, res);
}
}
int main()
{
database_flag = false;
2011-05-05 18:00:14 +01:00
(void)new Domain;
std::string ruleset("3b9cb0f9-c428-40cd-b8d0-0f39a5664143");
init_python_api(ruleset);
2010-06-30 18:16:22 +05:30
2011-10-31 12:47:56 +01:00
SystemTime time;
WorldRouter world(time);
2010-06-30 18:16:22 +05:30
Entity & e = world.m_gameWorld;
Ruleset::init(ruleset);
2010-06-30 18:16:22 +05:30
ServerRouting server(world, "noruleset", "unittesting",
"1", 1, "2", 2);
2011-10-31 13:04:55 +00:00
CommServer commServer;
2010-06-30 18:16:22 +05:30
TestCommClient * tc = new TestCommClient(commServer);
2010-11-26 16:21:57 +00:00
Connection * c = new Connection(*tc, server, "addr", "3", 3);
2010-06-30 18:16:22 +05:30
TestServerAccount * ac = new TestServerAccount(c, "user", "password", "4", 4);
Entity * chr;
Player * p = new Player(0, "bob", "bobspass", "8", 8);
server.addAccount(p);
{
chr = new Entity("5", 5);
chr->m_location.m_loc = &e;
chr->m_location.m_loc->makeContainer();
assert(chr->m_location.m_loc->m_contains != 0);
chr->m_location.m_loc->m_contains->insert(chr);
ac->addCharacter(chr);
chr->destroy();
}
{
chr = new Character("6", 6);
chr->m_location.m_loc = &e;
chr->m_location.m_loc->makeContainer();
assert(chr->m_location.m_loc->m_contains != 0);
chr->m_location.m_loc->m_contains->insert(chr);
ac->addCharacter(chr);
chr->destroy();
}
{
chr = new Character("7", 7);
chr->m_location.m_loc = &e;
chr->m_location.m_loc->makeContainer();
assert(chr->m_location.m_loc->m_contains != 0);
chr->m_location.m_loc->m_contains->insert(chr);
ac->addCharacter(chr);
}
{
Anonymous new_char;
ac->testAddNewCharacter("game_entity", new_char, RootEntity());
}
ac->getType();
{
MapType emap;
ac->addToMessage(emap);
}
{
RootEntity ent;
ac->addToEntity(ent);
}
{
Create op;
Anonymous ent;
OpVector res;
ac->testCharacterError(op, ent, res);
ent->setParents(std::list<std::string>(1, "game_entity"));
ac->testCharacterError(op, ent, res);
ent->setParents(std::list<std::string>());
ac->testCharacterError(op, ent, res);
}
2010-07-01 03:54:19 +05:30
run_operation_checks(ac, chr, world);
2010-06-30 18:16:22 +05:30
ac->m_connection = 0;
2010-07-01 03:54:19 +05:30
run_operation_checks(ac, chr, world);
2010-06-30 18:16:22 +05:30
delete ac;
shutdown_python_api();
return 0;
}