Files
BiLauncher/tests/unit/tst_authoffline.cpp
T

67 lines
2.3 KiB
C++
Raw Normal View History

2026-10-12 13:14:08 +03:00
#include "testenv.h"
#include "authservice.h"
#include "localization.h"
#include <QCryptographicHash>
#include <QRegularExpression>
#include <QUuid>
class TestAuthOffline : public QObject
{
Q_OBJECT
private slots:
void emptyNicknameFails()
{
const AuthResult result = AuthService::offline(QString());
QVERIFY(!result.ok);
QCOMPARE(result.error, Loc::text("auth.offline.error.noNickname"));
}
void uuidMatchesVanillaOfflineScheme()
{
// Так же UUID офлайн-игрока считает сервер в offline-mode:
// UUID v3 от MD5("OfflinePlayer:<ник>").
const AuthResult result = AuthService::offline(QStringLiteral("Notch"));
QVERIFY(result.ok);
QCOMPARE(result.playerName, QStringLiteral("Notch"));
QCOMPARE(result.uuid, QStringLiteral("b50ad385829d3141a2167e7d7539ba7f"));
QCOMPARE(result.accessToken, QStringLiteral("0"));
QCOMPARE(result.clientToken, QStringLiteral("0"));
QCOMPARE(result.userType, QStringLiteral("legacy"));
}
void uuidIsStableAndWellFormed_data()
{
QTest::addColumn<QString>("nick");
QTest::newRow("ascii") << "Steve";
QTest::newRow("digits") << "player_123";
QTest::newRow("cyrillic") << QStringLiteral("Игрок");
}
void uuidIsStableAndWellFormed()
{
QFETCH(QString, nick);
const AuthResult a = AuthService::offline(nick);
const AuthResult b = AuthService::offline(nick);
QCOMPARE(a.uuid, b.uuid);
static const QRegularExpression hex(QStringLiteral("^[0-9a-f]{32}$"));
QVERIFY(hex.match(a.uuid).hasMatch());
QCOMPARE(a.uuid.at(12), QChar(u'3')); // версия 3
QVERIFY(QStringLiteral("89ab").contains(a.uuid.at(16))); // вариант RFC 4122
QVERIFY(AuthService::offline(nick + u'x').uuid != a.uuid);
}
void clientTokenIsRandomUuid()
{
const QString a = AuthService::generateClientToken();
const QString b = AuthService::generateClientToken();
QVERIFY(a != b);
QVERIFY(!QUuid::fromString(a).isNull());
QVERIFY(!a.startsWith(u'{'));
}
};
BILAUNCHER_TEST_MAIN(TestAuthOffline)
#include "tst_authoffline.moc"