mirror of
https://github.com/DarkflameUniverse/DarkflameServer.git
synced 2026-10-02 10:53:44 +00:00
GameDependenciesTest deleted the logger, config and managers but left the pointers set, so a later test without the fixture (packet capture) logged through a freed logger and crashed when the whole suite ran. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
94 lines
2.9 KiB
C++
94 lines
2.9 KiB
C++
#ifndef __GAMEDEPENDENCIES__H__
|
|
#define __GAMEDEPENDENCIES__H__
|
|
|
|
#include "Game.h"
|
|
#include "Logger.h"
|
|
#include "dServer.h"
|
|
#include "CDClientManager.h"
|
|
#include "EntityInfo.h"
|
|
#include "EntityManager.h"
|
|
#include "dConfig.h"
|
|
#include "dZoneManager.h"
|
|
#include "GameDatabase/TestSQL/TestSQLDatabase.h"
|
|
#include "Database.h"
|
|
#include <gtest/gtest.h>
|
|
#include <vector>
|
|
|
|
class dZoneManager;
|
|
class AssetManager;
|
|
|
|
// A packet captured by dServerMock::Send. The bytes are copied, so the capture stays valid
|
|
// after the sender's local BitStream goes out of scope.
|
|
struct CapturedPacket {
|
|
std::vector<uint8_t> bytes;
|
|
uint32_t bits = 0;
|
|
SystemAddress sysAddr = UNASSIGNED_SYSTEM_ADDRESS;
|
|
bool broadcast = false;
|
|
};
|
|
|
|
class dServerMock : public dServer {
|
|
std::vector<CapturedPacket> sentPackets;
|
|
RakNet::BitStream mostRecent;
|
|
public:
|
|
dServerMock() {};
|
|
~dServerMock() {};
|
|
|
|
// Returns a copy of the most recently sent packet, with its read pointer at the start, or nullptr if nothing was sent.
|
|
RakNet::BitStream* GetMostRecentBitStream() { return sentPackets.empty() ? nullptr : &mostRecent; };
|
|
const std::vector<CapturedPacket>& GetSentPackets() const { return sentPackets; }
|
|
void ClearSentPackets() { sentPackets.clear(); mostRecent.Reset(); }
|
|
|
|
void Send(RakNet::BitStream& bitStream, const SystemAddress& sysAddr, bool broadcast) override {
|
|
CapturedPacket& packet = sentPackets.emplace_back();
|
|
packet.bits = bitStream.GetNumberOfBitsUsed();
|
|
packet.bytes.assign(bitStream.GetData(), bitStream.GetData() + bitStream.GetNumberOfBytesUsed());
|
|
packet.sysAddr = sysAddr;
|
|
packet.broadcast = broadcast;
|
|
|
|
mostRecent.Reset();
|
|
mostRecent.WriteBits(packet.bytes.data(), packet.bits, false);
|
|
};
|
|
void SetZoneId(unsigned int zoneId) { mZoneID = zoneId; }
|
|
};
|
|
|
|
class GameDependenciesTest : public ::testing::Test {
|
|
protected:
|
|
void SetUpDependencies() {
|
|
info.pos = NiPoint3Constant::ZERO;
|
|
info.rot = QuatUtils::IDENTITY;
|
|
info.scale = 1.0f;
|
|
info.spawner = nullptr;
|
|
info.lot = 999;
|
|
Game::logger = new Logger("./testing.log", true, true);
|
|
Game::server = new dServerMock();
|
|
Game::config = new dConfig("worldconfig.ini");
|
|
Game::entityManager = new EntityManager();
|
|
Game::zoneManager = new dZoneManager();
|
|
Game::zoneManager->LoadZone(LWOZONEID(1, 0, 0));
|
|
Database::_setDatabase(new TestSQLDatabase()); // this new is managed by the Database
|
|
|
|
// Create a CDClientManager instance and load from defaults
|
|
CDClientManager::LoadValuesFromDefaults();
|
|
}
|
|
|
|
void TearDownDependencies() {
|
|
if (Game::server) delete Game::server;
|
|
// Cleared too: tests without these fixtures (packet capture) run after them and check them
|
|
delete Game::entityManager;
|
|
Game::entityManager = nullptr;
|
|
delete Game::zoneManager;
|
|
Game::zoneManager = nullptr;
|
|
if (Game::logger) {
|
|
Game::logger->Flush();
|
|
delete Game::logger;
|
|
Game::logger = nullptr;
|
|
}
|
|
delete Game::config;
|
|
Game::config = nullptr;
|
|
}
|
|
|
|
EntityInfo info{};
|
|
};
|
|
|
|
#endif //!__GAMEDEPENDENCIES__H__
|