[Server] Share one in-memory database interface across the server tests (#7862)

This commit is contained in:
BruebachL 2026-11-10 12:16:35 +01:00 • committed by GitHub
parent 3a1238a467
commit 9315a4aa6d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 200 additions and 170 deletions

View file

@ -0,0 +1,83 @@
#include "libcockatrice/server/dev/servatrice_in_memory_database_interface.h"
Servatrice_InMemoryDatabaseInterface::Servatrice_InMemoryDatabaseInterface(QObject *parent)
: Server_DatabaseInterface(parent)
{
}
void Servatrice_InMemoryDatabaseInterface::addUser(const ServerInfo_User &user)
{
users.insert(QString::fromStdString(user.name()), user);
}
void Servatrice_InMemoryDatabaseInterface::setAuthenticationResult(AuthenticationResult result)
{
authenticationResult = result;
}
void Servatrice_InMemoryDatabaseInterface::setActiveUserCount(int count)
{
activeUserCount = count;
}
const QList<Servatrice_InMemoryDatabaseInterface::LogEntry> &Servatrice_InMemoryDatabaseInterface::getLogEntries() const
{
return logEntries;
}
AuthenticationResult Servatrice_InMemoryDatabaseInterface::checkUserPassword(Server_ProtocolHandler *,
const QString &,
const QString &,
const QString &,
QString &,
int &,
bool)
{
return authenticationResult;
}
ServerInfo_User Servatrice_InMemoryDatabaseInterface::getUserData(const QString &name, bool withId)
{
const auto it = users.constFind(name);
if (it == users.constEnd()) {
return {};
}
ServerInfo_User user = *it;
if (!withId) {
user.clear_id();
}
return user;
}
int Servatrice_InMemoryDatabaseInterface::getNextGameId()
{
return nextGameId++;
}
int Servatrice_InMemoryDatabaseInterface::getNextReplayId()
{
return nextReplayId++;
}
int Servatrice_InMemoryDatabaseInterface::getActiveUserCount(QString)
{
return activeUserCount;
}
void Servatrice_InMemoryDatabaseInterface::logMessage(const int,
const QString &senderName,
const QString &,
const QString &logMessage,
LogMessage_TargetType targetType,
const int targetId,
const QString &targetName,
const QString &channel)
{
logEntries.append(LogEntry{.senderName = senderName,
.message = logMessage,
.targetType = targetType,
.targetId = targetId,
.targetName = targetName,
.channel = channel});
}

View file

@ -0,0 +1,72 @@
#ifndef SERVATRICE_IN_MEMORY_DATABASE_INTERFACE_H
#define SERVATRICE_IN_MEMORY_DATABASE_INTERFACE_H
#include "server_database_interface.h"
#include <QHash>
#include <QList>
#include <QString>
/**
* @brief In-memory Server_DatabaseInterface for tests and the dev server.
*
* Holds the little state a server needs to come up without a SQL database:
* registered users, the authentication outcome, the logged messages, the active
* user count and the game/replay id allocators. Calls that need persistence keep
* the interface's fail-closed defaults.
*/
class Servatrice_InMemoryDatabaseInterface : public Server_DatabaseInterface
{
public:
/** @brief One recorded logMessage() call. */
struct LogEntry
{
QString senderName;
QString message;
LogMessage_TargetType targetType;
int targetId;
QString targetName;
QString channel;
};
explicit Servatrice_InMemoryDatabaseInterface(QObject *parent = nullptr);
/** @brief Registers the user getUserData() returns; the id is only exposed to withId lookups. */
void addUser(const ServerInfo_User &user);
/** @brief Sets the result every checkUserPassword() call returns. */
void setAuthenticationResult(AuthenticationResult result);
/** @brief Sets the value getActiveUserCount() reports. */
void setActiveUserCount(int count);
const QList<LogEntry> &getLogEntries() const;
AuthenticationResult checkUserPassword(Server_ProtocolHandler * /* handler */,
const QString & /* user */,
const QString & /* password */,
const QString & /* clientId */,
QString & /* reasonStr */,
int & /* secondsLeft */,
bool /* passwordNeedsHash */) override;
ServerInfo_User getUserData(const QString &name, bool withId = false) override;
int getNextGameId() override;
int getNextReplayId() override;
int getActiveUserCount(QString connectionType = QString()) override;
void logMessage(const int /* senderId */,
const QString &senderName,
const QString & /* senderIp */,
const QString &logMessage,
LogMessage_TargetType targetType,
const int targetId,
const QString &targetName,
const QString &channel = QString()) override;
private:
QHash<QString, ServerInfo_User> users;
AuthenticationResult authenticationResult = NotLoggedIn;
int activeUserCount = 0;
int nextGameId = 1;
int nextReplayId = 1;
QList<LogEntry> logEntries;
};
#endif // SERVATRICE_IN_MEMORY_DATABASE_INTERFACE_H