2010-11-26 11:22:47 +00:00
|
|
|
// Cyphesis Online RPG Server and AI Engine
|
|
|
|
|
// Copyright (C) 2010 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
|
|
|
|
|
|
|
|
|
|
|
2011-02-15 09:30:46 +00:00
|
|
|
#ifdef NDEBUG
|
|
|
|
|
#undef NDEBUG
|
|
|
|
|
#endif
|
|
|
|
|
#ifndef DEBUG
|
|
|
|
|
#define DEBUG
|
|
|
|
|
#endif
|
|
|
|
|
|
2012-12-26 00:34:31 +00:00
|
|
|
#include "TestBase.h"
|
|
|
|
|
|
2013-07-26 20:14:33 +02:00
|
|
|
#include "server/PossessionAuthenticator.h"
|
2010-08-08 18:27:25 +05:30
|
|
|
|
2013-07-26 20:14:33 +02:00
|
|
|
#include "server/PendingPossession.h"
|
2010-11-26 11:22:47 +00:00
|
|
|
|
2018-11-03 16:43:33 +01:00
|
|
|
#include "rules/simulation/BaseWorld.h"
|
2010-08-08 18:59:42 +05:30
|
|
|
#include "common/id.h"
|
|
|
|
|
#include "common/log.h"
|
|
|
|
|
|
2018-10-31 21:38:35 +01:00
|
|
|
#include "rules/simulation/Entity.h"
|
2010-08-08 18:59:42 +05:30
|
|
|
|
2010-08-13 05:35:31 +05:30
|
|
|
#include <Atlas/Objects/Operation.h>
|
|
|
|
|
|
2010-08-08 18:59:42 +05:30
|
|
|
#include <cstdlib>
|
|
|
|
|
|
2010-08-08 18:27:25 +05:30
|
|
|
#include <cassert>
|
|
|
|
|
|
2010-08-13 05:35:31 +05:30
|
|
|
using Atlas::Objects::Entity::RootEntity;
|
|
|
|
|
|
|
|
|
|
int stub_baseworld_receieved_op = -1;
|
|
|
|
|
int stub_connection_send_op = -1;
|
|
|
|
|
int stub_connection_send_count = 0;
|
|
|
|
|
|
2018-07-24 21:18:50 +02:00
|
|
|
#include "TestWorld.h"
|
2010-08-13 05:35:31 +05:30
|
|
|
|
2018-07-24 21:18:50 +02:00
|
|
|
class MyTestWorld : public TestWorld {
|
|
|
|
|
public:
|
|
|
|
|
explicit MyTestWorld() {
|
2010-08-13 05:35:31 +05:30
|
|
|
}
|
|
|
|
|
|
2013-01-17 10:17:07 +00:00
|
|
|
LocatedEntity * test_addEntity(LocatedEntity * ent, long intId) {
|
2010-08-13 05:35:31 +05:30
|
|
|
m_eobjects[intId] = ent;
|
2018-08-05 14:31:04 +02:00
|
|
|
return ent;
|
2010-08-13 05:35:31 +05:30
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2012-12-26 00:34:31 +00:00
|
|
|
class TeleportAuthenticatortest : public Cyphesis::TestBase
|
2010-08-08 18:27:25 +05:30
|
|
|
{
|
2012-12-26 00:34:31 +00:00
|
|
|
private:
|
2018-07-24 21:18:50 +02:00
|
|
|
MyTestWorld * m_world;
|
2018-08-04 01:12:45 +02:00
|
|
|
PossessionAuthenticator* m_possessionAuthenticator;
|
2012-12-26 00:34:31 +00:00
|
|
|
public:
|
|
|
|
|
TeleportAuthenticatortest();
|
2010-08-08 18:27:25 +05:30
|
|
|
|
2012-12-26 00:34:31 +00:00
|
|
|
void setup();
|
|
|
|
|
void teardown();
|
2010-08-08 18:27:25 +05:30
|
|
|
|
2012-12-26 00:34:31 +00:00
|
|
|
void test_sequence();
|
2013-07-26 20:14:33 +02:00
|
|
|
void test_authenticatePossession();
|
|
|
|
|
void test_authenticatePossession_nonexist();
|
|
|
|
|
void test_removePossession_id();
|
|
|
|
|
void test_removePossession_iterator();
|
2012-12-26 00:34:31 +00:00
|
|
|
};
|
2010-08-08 18:27:25 +05:30
|
|
|
|
2012-12-26 00:34:31 +00:00
|
|
|
TeleportAuthenticatortest::TeleportAuthenticatortest()
|
|
|
|
|
{
|
|
|
|
|
ADD_TEST(TeleportAuthenticatortest::test_sequence);
|
2013-07-26 20:14:33 +02:00
|
|
|
ADD_TEST(TeleportAuthenticatortest::test_authenticatePossession);
|
|
|
|
|
ADD_TEST(TeleportAuthenticatortest::test_authenticatePossession_nonexist);
|
|
|
|
|
ADD_TEST(TeleportAuthenticatortest::test_removePossession_id);
|
|
|
|
|
ADD_TEST(TeleportAuthenticatortest::test_removePossession_iterator);
|
2012-12-26 00:34:31 +00:00
|
|
|
}
|
2010-08-08 18:27:25 +05:30
|
|
|
|
2012-12-26 00:34:31 +00:00
|
|
|
void TeleportAuthenticatortest::setup()
|
|
|
|
|
{
|
2018-07-24 21:18:50 +02:00
|
|
|
m_world = new MyTestWorld;
|
2018-08-04 01:12:45 +02:00
|
|
|
m_possessionAuthenticator = new PossessionAuthenticator();
|
2012-12-26 00:34:31 +00:00
|
|
|
}
|
2010-08-08 18:27:25 +05:30
|
|
|
|
2012-12-26 00:34:31 +00:00
|
|
|
void TeleportAuthenticatortest::teardown()
|
|
|
|
|
{
|
2018-08-04 01:12:45 +02:00
|
|
|
delete m_possessionAuthenticator;
|
2012-12-26 00:34:31 +00:00
|
|
|
delete m_world;
|
|
|
|
|
}
|
2010-08-08 18:27:25 +05:30
|
|
|
|
2012-12-26 00:34:31 +00:00
|
|
|
void TeleportAuthenticatortest::test_sequence()
|
|
|
|
|
{
|
|
|
|
|
// Check for correct singleton instancing
|
2018-08-04 01:12:45 +02:00
|
|
|
|
2012-12-26 00:34:31 +00:00
|
|
|
// Test isPending() function
|
2018-08-04 01:12:45 +02:00
|
|
|
assert(!PossessionAuthenticator::instance().isPending("test_non_existent_entity_id"));
|
2010-08-08 18:27:25 +05:30
|
|
|
|
2012-12-26 00:34:31 +00:00
|
|
|
// Test adding of teleport entries
|
2018-08-04 01:12:45 +02:00
|
|
|
assert(PossessionAuthenticator::instance().addPossession("test_entity_id", "test_possess_key") == 0);
|
|
|
|
|
assert(PossessionAuthenticator::instance().isPending("test_entity_id"));
|
|
|
|
|
assert(PossessionAuthenticator::instance().addPossession("test_entity_id", "test_possess_key") == -1);
|
2010-08-08 18:27:25 +05:30
|
|
|
|
2012-12-26 00:34:31 +00:00
|
|
|
// Test removal of teleport entries
|
2018-08-04 01:12:45 +02:00
|
|
|
assert(PossessionAuthenticator::instance().removePossession("test_non_existent_entity_id") == -1);
|
|
|
|
|
assert(PossessionAuthenticator::instance().removePossession("test_entity_id") == 0);
|
|
|
|
|
assert(!PossessionAuthenticator::instance().isPending("test_entity_id"));
|
2010-08-08 18:27:25 +05:30
|
|
|
|
2012-12-26 00:34:31 +00:00
|
|
|
}
|
|
|
|
|
|
2013-07-26 20:14:33 +02:00
|
|
|
void TeleportAuthenticatortest::test_authenticatePossession()
|
2012-12-26 00:34:31 +00:00
|
|
|
{
|
2018-08-05 14:31:04 +02:00
|
|
|
Ref<Entity> ent(new Entity("100", 100));
|
2018-08-05 20:47:09 +02:00
|
|
|
m_world->test_addEntity(ent.get(), 100);
|
2010-11-25 19:31:04 +00:00
|
|
|
|
2018-08-04 01:12:45 +02:00
|
|
|
PossessionAuthenticator::instance().addPossession("100", "test_possess_key");
|
2010-11-25 19:31:04 +00:00
|
|
|
|
2012-12-26 00:34:31 +00:00
|
|
|
// Test non-existent ID authentication request
|
2018-08-04 01:12:45 +02:00
|
|
|
assert(PossessionAuthenticator::instance().authenticatePossession(
|
2018-01-30 16:54:49 +01:00
|
|
|
"101", "test_possess_key") == nullptr);
|
2010-11-25 19:31:04 +00:00
|
|
|
|
2012-12-26 00:34:31 +00:00
|
|
|
// Test incorrect possess key authentication request
|
2018-08-04 01:12:45 +02:00
|
|
|
assert(PossessionAuthenticator::instance().authenticatePossession("100",
|
2018-01-30 16:54:49 +01:00
|
|
|
"test_wrong_possess_key") == nullptr);
|
2012-12-26 00:34:31 +00:00
|
|
|
|
|
|
|
|
// Test valid authentication request
|
2018-08-04 01:12:45 +02:00
|
|
|
assert(PossessionAuthenticator::instance().authenticatePossession("100",
|
2018-01-30 16:54:49 +01:00
|
|
|
"test_possess_key") != nullptr);
|
2012-12-26 00:34:31 +00:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-26 20:14:33 +02:00
|
|
|
void TeleportAuthenticatortest::test_authenticatePossession_nonexist()
|
2012-12-26 00:34:31 +00:00
|
|
|
{
|
2018-08-05 14:31:04 +02:00
|
|
|
Ref<Entity> ent(new Entity("100", 100));
|
2018-08-05 20:47:09 +02:00
|
|
|
m_world->test_addEntity(ent.get(), 100);
|
2012-12-26 00:34:31 +00:00
|
|
|
|
2018-08-04 01:12:45 +02:00
|
|
|
PossessionAuthenticator::instance().addPossession("101", "test_possess_key");
|
2012-12-26 00:34:31 +00:00
|
|
|
|
|
|
|
|
// Test ID authentication request, that we added, for a non existant
|
|
|
|
|
// entity
|
2018-08-04 01:12:45 +02:00
|
|
|
assert(PossessionAuthenticator::instance().authenticatePossession(
|
2018-01-30 16:54:49 +01:00
|
|
|
"101", "test_possess_key") == nullptr);
|
2012-12-26 00:34:31 +00:00
|
|
|
}
|
|
|
|
|
|
2013-07-26 20:14:33 +02:00
|
|
|
void TeleportAuthenticatortest::test_removePossession_id()
|
2012-12-26 00:34:31 +00:00
|
|
|
{
|
2018-08-04 01:12:45 +02:00
|
|
|
PossessionAuthenticator * ta = PossessionAuthenticator::instancePtr();
|
2013-02-09 00:21:17 +00:00
|
|
|
|
2013-07-26 20:14:33 +02:00
|
|
|
ta->m_possessions.insert(std::make_pair("1",
|
|
|
|
|
new PendingPossession("1", "e146db28-1058-46e6-a9b3-601ab6ef07a7")));
|
2013-02-09 00:21:17 +00:00
|
|
|
|
2013-07-26 20:14:33 +02:00
|
|
|
ASSERT_TRUE(ta->m_possessions.find("1") != ta->m_possessions.end());
|
2013-02-09 00:21:17 +00:00
|
|
|
|
2013-07-26 20:14:33 +02:00
|
|
|
int ret = ta->removePossession("1");
|
2013-02-09 00:21:17 +00:00
|
|
|
ASSERT_EQUAL(ret, 0);
|
|
|
|
|
|
2013-07-26 20:14:33 +02:00
|
|
|
ASSERT_TRUE(ta->m_possessions.find("1") == ta->m_possessions.end());
|
2013-02-09 00:21:17 +00:00
|
|
|
}
|
|
|
|
|
|
2013-07-26 20:14:33 +02:00
|
|
|
void TeleportAuthenticatortest::test_removePossession_iterator()
|
2013-02-09 00:21:17 +00:00
|
|
|
{
|
2018-08-04 01:12:45 +02:00
|
|
|
PossessionAuthenticator * ta = PossessionAuthenticator::instancePtr();
|
2013-02-09 00:21:17 +00:00
|
|
|
|
2013-07-26 20:14:33 +02:00
|
|
|
ta->m_possessions.insert(std::make_pair("2",
|
|
|
|
|
new PendingPossession("2", "b769b7a4-32d3-477d-9803-a53fd9ad49c7")));
|
2013-02-09 00:21:17 +00:00
|
|
|
|
2013-07-26 20:14:33 +02:00
|
|
|
ASSERT_TRUE(ta->m_possessions.find("2") != ta->m_possessions.end());
|
2013-02-09 00:21:17 +00:00
|
|
|
|
2013-07-26 20:14:33 +02:00
|
|
|
PendingPossessionsMap::iterator I = ta->m_possessions.find("2");
|
2013-02-09 00:21:17 +00:00
|
|
|
|
2013-07-26 20:14:33 +02:00
|
|
|
ta->removePossession(I);
|
2013-02-09 00:21:17 +00:00
|
|
|
|
2013-07-26 20:14:33 +02:00
|
|
|
ASSERT_TRUE(ta->m_possessions.find("2") == ta->m_possessions.end());
|
2012-12-26 00:34:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main()
|
|
|
|
|
{
|
|
|
|
|
TeleportAuthenticatortest t;
|
|
|
|
|
|
|
|
|
|
return t.run();
|
2010-08-08 18:27:25 +05:30
|
|
|
}
|
2010-08-08 18:59:42 +05:30
|
|
|
|
|
|
|
|
// Stubs
|
|
|
|
|
|
|
|
|
|
long integerId(const std::string & id)
|
|
|
|
|
{
|
|
|
|
|
long intId = strtol(id.c_str(), 0, 10);
|
|
|
|
|
if (intId == 0 && id != "0") {
|
|
|
|
|
intId = -1L;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return intId;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void log(LogLevel lvl, const std::string & msg)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-25 00:16:09 +02:00
|
|
|
#ifndef STUB_BaseWorld_getEntity
|
|
|
|
|
#define STUB_BaseWorld_getEntity
|
2018-08-05 20:47:09 +02:00
|
|
|
Ref<LocatedEntity> BaseWorld::getEntity(const std::string & id) const
|
2010-08-08 18:59:42 +05:30
|
|
|
{
|
2018-07-25 00:16:09 +02:00
|
|
|
return getEntity(integerId(id));
|
2010-08-08 18:59:42 +05:30
|
|
|
}
|
|
|
|
|
|
2018-08-05 20:47:09 +02:00
|
|
|
Ref<LocatedEntity> BaseWorld::getEntity(long id) const
|
2010-08-08 18:59:42 +05:30
|
|
|
{
|
2018-07-25 00:16:09 +02:00
|
|
|
auto I = m_eobjects.find(id);
|
2010-08-08 18:59:42 +05:30
|
|
|
if (I != m_eobjects.end()) {
|
2018-07-25 00:16:09 +02:00
|
|
|
assert(I->second);
|
2010-08-08 18:59:42 +05:30
|
|
|
return I->second;
|
|
|
|
|
} else {
|
2018-07-25 00:16:09 +02:00
|
|
|
return nullptr;
|
2010-08-08 18:59:42 +05:30
|
|
|
}
|
|
|
|
|
}
|
2018-07-25 00:16:09 +02:00
|
|
|
#endif //STUB_BaseWorld_getEntity
|
2010-08-08 18:59:42 +05:30
|
|
|
|
2018-11-03 16:43:33 +01:00
|
|
|
#include "stubs/rules/simulation/stubBaseWorld.h"
|
2013-07-26 20:14:33 +02:00
|
|
|
PendingPossession::PendingPossession(const std::string &id, const std::string &key)
|
2010-08-08 18:59:42 +05:30
|
|
|
: m_entity_id(id),
|
|
|
|
|
m_possess_key(key),
|
|
|
|
|
m_valid(false)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-26 20:14:33 +02:00
|
|
|
bool PendingPossession::validate(const std::string &entity_id,
|
2010-11-25 19:22:30 +00:00
|
|
|
const std::string &possess_key) const
|
2010-08-08 18:59:42 +05:30
|
|
|
{
|
|
|
|
|
if(m_entity_id == entity_id && m_possess_key == possess_key) {
|
|
|
|
|
return true;
|
|
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-26 20:14:33 +02:00
|
|
|
void PendingPossession::setValidated()
|
2010-08-08 18:59:42 +05:30
|
|
|
{
|
|
|
|
|
m_valid = true;
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-03 16:43:33 +01:00
|
|
|
#include "stubs/rules/simulation/stubEntity.h"
|
|
|
|
|
#include "stubs/rules/stubLocatedEntity.h"
|
|
|
|
|
#include "stubs/rules/stubLocation.h"
|
2014-09-29 19:24:05 +02:00
|
|
|
#include "stubs/common/stubRouter.h"
|
2010-08-13 05:35:31 +05:30
|
|
|
|