Files
DarkflameServer/tests/dGameTests/dNetTests/CommonAuthPacketsTests.cpp
Aaron Kimbrell 05d18f56e4 feat(net): VERSION_CONFIRM carries the server's build identifier
After serviceType the reply now sends major, minor, patch, a flags byte
(build kind in bits 0-1, dirty in bit 2), the first 32 bits of the commit
hash and a u16 length-prefixed build string, instead of the stale fixed
ASCII "0.1.3". unknown stays "DLU3". The 1.10.64 client reads only
netVersion and serviceType and never checks the length, so the extra
bytes are ignored. The build string is optional on read.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-29 16:29:46 -05:00

608 lines
25 KiB
C++

#include "AuthPackets.h"
#include "BuildInfo.h"
#include "ClientPackets.h"
#include "CommonPackets.h"
#include "GameDependencies.h"
#include "MasterPackets.h"
#include "PacketTestUtils.h"
#include "Legacy/CommonAuthPacketsLegacy.h"
#include "eLoginResponse.h"
#include "eServerDisconnectIdentifiers.h"
#include "magic_enum.hpp"
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <functional>
#include <string>
#include <vector>
#include <gtest/gtest.h>
using namespace PacketTestUtils;
namespace {
SystemAddress TestAddress(uint16_t port = 1234) {
SystemAddress address;
address.binaryAddress = 0x0100007F;
address.port = port;
return address;
}
// Runs a handler on a copy of the packet (header included), the way the servers receive it.
Packet MakePacket(RakNet::BitStream& bitStream, const SystemAddress& sysAddr) {
Packet packet{};
packet.systemAddress = sysAddr;
packet.data = bitStream.GetData();
packet.length = bitStream.GetNumberOfBytesUsed();
packet.bitSize = bitStream.GetNumberOfBitsUsed();
return packet;
}
// Reads the header like the servers do and hands the rest to a dispatcher.
void Dispatch(RakNet::BitStream& packetBytes, const SystemAddress& sysAddr, const std::function<void(RakNet::BitStream&, const SystemAddress&, uint32_t)>& handler) {
RakNet::BitStream inStream(packetBytes.GetData(), packetBytes.GetNumberOfBytesUsed(), false);
LUBitStream header;
ASSERT_TRUE(header.ReadHeader(inStream));
handler(inStream, sysAddr, header.internalPacketID);
}
void ExpectSamePackets(const std::vector<CapturedPacket>& expected, const std::vector<CapturedPacket>& actual) {
ASSERT_FALSE(expected.empty());
ASSERT_EQ(expected.size(), actual.size());
for (size_t i = 0; i < expected.size(); i++) {
EXPECT_PACKET_EQ(FromCapture(expected[i]), FromCapture(actual[i]));
EXPECT_EQ(expected[i].sysAddr, actual[i].sysAddr);
EXPECT_EQ(expected[i].broadcast, actual[i].broadcast);
}
}
// Runs both, retrying if the second changed while they ran (stamps carry time(nullptr)).
void ExpectSameOutput(const std::function<void()>& legacy, const std::function<void()>& converted) {
for (int attempt = 0; attempt < 3; attempt++) {
const auto before = std::time(nullptr);
Game::randomEngine.seed(1234);
const auto expected = Capture(legacy);
Game::randomEngine.seed(1234);
const auto actual = Capture(converted);
if (before != std::time(nullptr)) continue;
ExpectSamePackets(expected, actual);
return;
}
FAIL() << "the clock kept changing seconds while comparing";
}
// VERSION_CONFIRM replies: the header, netVersion, unknown and serviceType (the 20 bytes the client reads up to)
// must match the legacy reply; the build identifier after them replaced the legacy's 8 fixed bytes.
constexpr size_t VERSION_CONFIRM_LEGACY_PREFIX = 20;
void ExpectSameVersionConfirmPrefix(const std::function<void()>& legacy, const std::function<void()>& converted) {
const auto expected = Capture(legacy);
const auto actual = Capture(converted);
ASSERT_FALSE(expected.empty());
ASSERT_EQ(expected.size(), actual.size());
RakNet::BitStream thisBuild;
CommonPackets::ServerVersionConfirm{}.WritePacket(thisBuild);
const auto build = FromBitStream(thisBuild);
const std::vector<uint8_t> expectedTail(build.bytes.begin() + VERSION_CONFIRM_LEGACY_PREFIX, build.bytes.end());
for (size_t i = 0; i < expected.size(); i++) {
ASSERT_GE(expected[i].bytes.size(), VERSION_CONFIRM_LEGACY_PREFIX);
ASSERT_GE(actual[i].bytes.size(), VERSION_CONFIRM_LEGACY_PREFIX);
const std::vector<uint8_t> expectedPrefix(expected[i].bytes.begin(), expected[i].bytes.begin() + VERSION_CONFIRM_LEGACY_PREFIX);
const std::vector<uint8_t> actualPrefix(actual[i].bytes.begin(), actual[i].bytes.begin() + VERSION_CONFIRM_LEGACY_PREFIX);
EXPECT_EQ(expectedPrefix, actualPrefix);
const std::vector<uint8_t> actualTail(actual[i].bytes.begin() + VERSION_CONFIRM_LEGACY_PREFIX, actual[i].bytes.end());
EXPECT_EQ(expectedTail, actualTail);
EXPECT_EQ(expected[i].sysAddr, actual[i].sysAddr);
EXPECT_EQ(expected[i].broadcast, actual[i].broadcast);
}
}
template<typename T>
T RoundTrip(const T& packet) {
RakNet::BitStream first;
packet.WritePacket(first);
T copy;
EXPECT_TRUE(copy.ReadHeader(first));
EXPECT_TRUE(copy.Deserialize(first));
EXPECT_EQ(first.GetNumberOfUnreadBits(), 0);
RakNet::BitStream second;
copy.WritePacket(second);
EXPECT_PACKET_EQ(FromBitStream(first), FromBitStream(second));
return copy;
}
template<typename T>
void ExpectTruncatedFails(const T& packet) {
RakNet::BitStream full;
packet.WritePacket(full);
const auto bytes = full.GetNumberOfBytesUsed();
for (uint32_t cut = 8; cut < bytes; cut++) {
RakNet::BitStream truncated(full.GetData(), cut, true);
T copy;
ASSERT_TRUE(copy.ReadHeader(truncated));
EXPECT_FALSE(copy.Deserialize(truncated)) << "cut at " << cut << " of " << bytes;
}
}
const std::vector<std::string> g_Strings = { "", "a", "Hello World", "user_name-01", std::string(40, 'x'), std::string(300, 'y') };
}
class CommonAuthPacketsTests : public GameDependenciesTest {
protected:
void SetUp() override { SetUpDependencies(); }
void TearDown() override { TearDownDependencies(); }
};
TEST_F(CommonAuthPacketsTests, VersionConfirmHandshakeMatchesLegacy) {
for (const uint32_t netVersion : { 0u, 171022u, 0xFFFFFFFFu }) {
for (const auto serviceType : { ServiceType::CLIENT, ServiceType::AUTH, static_cast<ServiceType>(0xFFFF) }) {
for (const uint16_t port : { uint16_t{ 0 }, uint16_t{ 1234 }, uint16_t{ 0xFFFF } }) {
CommonPackets::ClientVersionConfirm request;
request.netVersion = netVersion;
request.unknown = 0x12345678;
request.serviceType = serviceType;
request.processID = 4321;
request.port = port;
request.unknown2 = LUString("127.0.0.1");
RakNet::BitStream bytes;
request.WritePacket(bytes);
const auto sysAddr = TestAddress();
ExpectSameVersionConfirmPrefix(
[&] { auto packet = MakePacket(bytes, sysAddr); LegacyAuthPackets::HandleHandshake(Game::server, &packet); },
[&] { Dispatch(bytes, sysAddr, CommonPackets::Handle); });
const auto copy = RoundTrip(request);
EXPECT_EQ(copy.netVersion, netVersion);
EXPECT_EQ(copy.serviceType, serviceType);
EXPECT_EQ(copy.port, port);
EXPECT_EQ(copy.unknown2.string, "127.0.0.1");
}
}
}
}
TEST_F(CommonAuthPacketsTests, ServerVersionConfirmKeepsLegacyPrefix) {
for (const auto serviceType : { ServiceType::AUTH, ServiceType::WORLD, ServiceType::CHAT }) {
const auto sysAddr = TestAddress();
ExpectSameVersionConfirmPrefix(
[&] { LegacyAuthPackets::SendHandshake(Game::server, sysAddr, "ignored", 1, serviceType); },
[&] {
CommonPackets::ServerVersionConfirm response;
response.serviceType = static_cast<uint32_t>(serviceType);
response.Send(sysAddr);
});
}
}
TEST_F(CommonAuthPacketsTests, VersionConfirmGoldenBytes) {
CommonPackets::ServerVersionConfirm response;
response.serviceType = static_cast<uint32_t>(ServiceType::WORLD);
response.versionMajor = 3;
response.versionMinor = 0;
response.versionPatch = 1;
response.buildFlags = static_cast<uint8_t>(BuildInfo::eBuildKind::CI) | BuildInfo::DIRTY_FLAG;
response.commitPrefix = 0x1a2b3c4d;
response.buildString = "3.0.1-main+g1a2b3c4d";
RakNet::BitStream bytes;
response.WritePacket(bytes);
// 0x53 | COMMON u16 | VERSION_CONFIRM u32 | pad | 171022 | 861228100 ("DLU3") | WORLD u32
// | 3 0 1 | flags (CI | dirty) | commit 1a2b3c4d | u16 20 | "3.0.1-main+g1a2b3c4d"
EXPECT_PACKET_EQ(FromHex("53 00 00 00 00 00 00 00 0e 9c 02 00 44 4c 55 33 04 00 00 00 03 00 01 06 1a 2b 3c 4d 14 00"
" 33 2e 30 2e 31 2d 6d 61 69 6e 2b 67 31 61 32 62 33 63 34 64"), FromBitStream(bytes));
const auto copy = RoundTrip(response);
EXPECT_EQ(copy.commitPrefix, 0x1a2b3c4du);
EXPECT_EQ(copy.buildString, "3.0.1-main+g1a2b3c4d");
CommonPackets::ClientVersionConfirm request;
request.netVersion = 171022;
request.serviceType = ServiceType::CLIENT;
request.processID = 0x11223344;
request.port = 0x5566;
RakNet::BitStream requestBytes;
request.WritePacket(requestBytes);
std::string zeros;
for (int i = 0; i < 33; i++) zeros += " 00";
// ... | 171022 | unknown 0 | CLIENT u16 | pad u16 | process id | port | 33 byte string
EXPECT_PACKET_EQ(FromHex("53 00 00 00 00 00 00 00 0e 9c 02 00 00 00 00 00 05 00 00 00 44 33 22 11 66 55" + zeros), FromBitStream(requestBytes));
ExpectTruncatedFails(request);
}
TEST_F(CommonAuthPacketsTests, ServerVersionConfirmRoundTrip) {
CommonPackets::ServerVersionConfirm response;
response.netVersion = 0xFFFFFFFF;
response.serviceType = static_cast<uint32_t>(ServiceType::CHAT);
response.versionMajor = 0xFF;
response.versionMinor = 0x7F;
response.versionPatch = 0;
response.buildFlags = static_cast<uint8_t>(BuildInfo::eBuildKind::RELEASE);
response.commitPrefix = 0xDEADBEEF;
for (const auto& buildString : g_Strings) {
response.buildString = buildString;
const auto copy = RoundTrip(response);
EXPECT_EQ(copy.netVersion, response.netVersion);
EXPECT_EQ(copy.unknown, CommonPackets::ServerVersionConfirm::UNKNOWN_VALUE);
EXPECT_EQ(copy.serviceType, response.serviceType);
EXPECT_EQ(copy.versionMajor, response.versionMajor);
EXPECT_EQ(copy.versionMinor, response.versionMinor);
EXPECT_EQ(copy.versionPatch, response.versionPatch);
EXPECT_EQ(copy.buildFlags, response.buildFlags);
EXPECT_EQ(copy.commitPrefix, response.commitPrefix);
EXPECT_EQ(copy.buildString, buildString);
}
// The defaults describe this build.
const CommonPackets::ServerVersionConfirm current;
EXPECT_EQ(current.unknown, 861228100u);
EXPECT_EQ(current.versionMajor, BuildInfo::versionMajor);
EXPECT_EQ(current.versionMinor, BuildInfo::versionMinor);
EXPECT_EQ(current.versionPatch, BuildInfo::versionPatch);
EXPECT_EQ(current.buildFlags, BuildInfo::Flags());
EXPECT_EQ(current.commitPrefix, BuildInfo::CommitPrefix());
EXPECT_EQ(current.buildString, BuildInfo::buildString);
EXPECT_EQ(RoundTrip(current).buildString, BuildInfo::buildString);
}
TEST_F(CommonAuthPacketsTests, ServerVersionConfirmReadsWithoutBuildString) {
// A reply that stops after the commit bytes still reads; the build string is optional on read.
const auto fixed = FromHex("53 00 00 00 00 00 00 00 0e 9c 02 00 44 4c 55 33 04 00 00 00 03 00 01 06 1a 2b 3c 4d");
RakNet::BitStream bytes(const_cast<unsigned char*>(fixed.bytes.data()), fixed.bytes.size(), true);
CommonPackets::ServerVersionConfirm copy;
copy.buildString = "stale";
ASSERT_TRUE(copy.ReadHeader(bytes));
ASSERT_TRUE(copy.Deserialize(bytes));
EXPECT_EQ(copy.netVersion, 171022u);
EXPECT_EQ(copy.serviceType, static_cast<uint32_t>(ServiceType::WORLD));
EXPECT_EQ(copy.versionMajor, 3);
EXPECT_EQ(copy.versionPatch, 1);
EXPECT_EQ(copy.buildFlags, 6);
EXPECT_EQ(copy.commitPrefix, 0x1a2b3c4du);
EXPECT_TRUE(copy.buildString.empty());
// Cut inside the fixed bytes or inside the string: fails.
CommonPackets::ServerVersionConfirm full;
full.buildString = "3.0.0";
RakNet::BitStream fullBytes;
full.WritePacket(fullBytes);
for (const uint32_t cut : { 8u, 19u, 27u, 29u, 32u }) {
RakNet::BitStream truncated(fullBytes.GetData(), cut, true);
CommonPackets::ServerVersionConfirm partial;
ASSERT_TRUE(partial.ReadHeader(truncated));
EXPECT_FALSE(partial.Deserialize(truncated)) << "cut at " << cut;
}
}
TEST(BuildInfoTests, FlagsAndCommitPrefix) {
EXPECT_EQ(BuildInfo::Flags() & BuildInfo::BUILD_KIND_MASK, static_cast<uint8_t>(BuildInfo::buildKind));
EXPECT_EQ((BuildInfo::Flags() & BuildInfo::DIRTY_FLAG) != 0, BuildInfo::dirty);
EXPECT_EQ(BuildInfo::Flags() & ~(BuildInfo::BUILD_KIND_MASK | BuildInfo::DIRTY_FLAG), 0);
if (BuildInfo::commit.size() >= 8) {
char hex[9];
std::snprintf(hex, sizeof(hex), "%08x", BuildInfo::CommitPrefix());
EXPECT_EQ(std::string(hex), std::string(BuildInfo::commit.substr(0, 8)));
EXPECT_NE(BuildInfo::buildString.find("+g" + std::string(hex)), std::string_view::npos);
} else {
EXPECT_EQ(BuildInfo::CommitPrefix(), 0u);
}
const auto version = std::to_string(BuildInfo::versionMajor) + "." + std::to_string(BuildInfo::versionMinor) + "." + std::to_string(BuildInfo::versionPatch);
EXPECT_TRUE(BuildInfo::buildString.starts_with(version));
}
TEST_F(CommonAuthPacketsTests, DisconnectNotifyMatchesLegacy) {
for (const auto id : magic_enum::enum_values<eServerDisconnectIdentifiers>()) {
RakNet::BitStream expected;
LegacyAuthPackets::WriteDisconnectNotify(expected, id);
CommonPackets::DisconnectNotify notify;
notify.disconnectID = id;
RakNet::BitStream actual;
notify.WritePacket(actual);
EXPECT_PACKET_EQ(FromBitStream(expected), FromBitStream(actual));
EXPECT_EQ(RoundTrip(notify).disconnectID, id);
}
CommonPackets::DisconnectNotify kick;
kick.disconnectID = eServerDisconnectIdentifiers::KICK;
RakNet::BitStream bytes;
kick.WritePacket(bytes);
EXPECT_PACKET_EQ(FromHex("53 00 00 01 00 00 00 00 0b 00 00 00"), FromBitStream(bytes));
ExpectTruncatedFails(kick);
}
TEST_F(CommonAuthPacketsTests, GeneralNotifyGoldenAndRoundTrip) {
CommonPackets::GeneralNotify notify;
notify.notifyType = 0;
notify.showMessageBox = true;
RakNet::BitStream bytes;
notify.WritePacket(bytes);
EXPECT_PACKET_EQ(FromHex("53 00 00 02 00 00 00 00 00 00 00 00 01"), FromBitStream(bytes));
const auto copy = RoundTrip(notify);
EXPECT_EQ(copy.notifyType, 0);
EXPECT_TRUE(copy.showMessageBox);
ExpectTruncatedFails(notify);
}
TEST_F(CommonAuthPacketsTests, LoginResponseMatchesLegacy) {
// Event gating comes from the config; environment variables override it
setenv("EVENT_1", "Event One", 1);
setenv("EVENT_8", std::string(40, 'e').c_str(), 1);
setenv("VERSION_MINOR", "99", 1);
const std::vector<eLoginResponse> codes = { eLoginResponse::GENERAL_FAILED, eLoginResponse::BANNED, eLoginResponse::PERMISSIONS_NOT_HIGH_ENOUGH, eLoginResponse::INVALID_USER, eLoginResponse::WRONG_PASS, eLoginResponse::ACCOUNT_LOCKED };
for (const auto code : codes) {
for (const auto& text : g_Strings) {
for (const size_t stampCount : { size_t{ 0 }, size_t{ 1 }, size_t{ 5 } }) {
std::vector<Stamp> legacyStamps;
for (size_t i = 0; i < stampCount; i++) legacyStamps.emplace_back(static_cast<eStamps>(i), static_cast<uint32_t>(i * 3), 1000 + i);
auto stamps = legacyStamps;
const auto sysAddr = TestAddress();
ExpectSameOutput(
[&] { auto copy = legacyStamps; LegacyAuthPackets::SendLoginResponse(Game::server, sysAddr, code, text, text, 2001, "user", copy); },
[&] {
// The old function appended these two to every response; now the login steps stamp themselves
auto copy = stamps;
copy.emplace_back(eStamps::PASSPORT_AUTH_IM_LOGIN_START, 1);
copy.emplace_back(eStamps::PASSPORT_AUTH_WORLD_COMMUNICATION_FINISH, 1);
Stamps loginStamps(copy);
AuthPackets::SendLoginResponse(Game::server, sysAddr, code, text, text, 2001, "user", loginStamps);
});
}
}
}
unsetenv("EVENT_1");
unsetenv("EVENT_8");
unsetenv("VERSION_MINOR");
}
TEST_F(CommonAuthPacketsTests, LoginResponseRoundTrip) {
ClientPackets::LoginResponse response;
response.responseCode = eLoginResponse::SUCCESS;
response.events[0] = LUString("Talk_Like_A_Pirate");
response.versionMajor = 1;
response.versionCurrent = 10;
response.versionMinor = 64;
response.userKey = LUWString("0123456789abcdef0123456789abcdef");
response.worldServerIP = LUString("192.168.1.2");
response.worldServerPort = 2000;
response.errorMessage = "Something went wrong";
response.stamps.list = { Stamp(eStamps::PASSPORT_AUTH_START, 0, 5), Stamp(eStamps::NO_WORLD_SERVER, 1, 6) };
const auto copy = RoundTrip(response);
EXPECT_EQ(copy.responseCode, eLoginResponse::SUCCESS);
EXPECT_EQ(copy.events[0].string, "Talk_Like_A_Pirate");
EXPECT_EQ(copy.versionMinor, 64);
EXPECT_EQ(copy.worldServerIP.string, "192.168.1.2");
EXPECT_EQ(copy.worldServerPort, 2000);
EXPECT_EQ(copy.cdnTicket.string, ClientPackets::LoginResponse::DEFAULT_CDN_TICKET);
EXPECT_EQ(copy.localization.string, "US");
EXPECT_EQ(copy.errorMessage, "Something went wrong");
ASSERT_EQ(copy.stamps.list.size(), 2);
EXPECT_EQ(copy.stamps.list[1].type, eStamps::NO_WORLD_SERVER);
EXPECT_EQ(copy.stamps.list[1].timestamp, 6);
ExpectTruncatedFails(response);
response.errorMessage.clear();
response.stamps.list.clear();
EXPECT_TRUE(RoundTrip(response).errorMessage.empty());
}
TEST_F(CommonAuthPacketsTests, LoginRequestMatchesLegacy) {
for (const auto& username : g_Strings) {
AuthPackets::LoginRequest request;
request.username = LUWString(username);
request.password = LUWString(std::string("hunter2"), 41);
request.localeID = LanguageCodeID::en_US;
request.clientOS = ClientOS::WINDOWS;
request.memoryStats = LUWString(std::string("Memory"), 256);
request.videoCard = LUWString(std::string("Video card"), 128);
request.numberOfProcessors = 8;
request.processorType = 586;
request.processorLevel = 6;
request.processorRevision = 0x3a09;
request.osVersionInfoSize = 148;
request.majorVersion = 6;
request.minorVersion = 1;
request.buildNumber = 7601;
request.platformID = 2;
RakNet::BitStream bytes;
request.WritePacket(bytes);
const auto sysAddr = TestAddress();
// The test database knows no accounts, so both answer INVALID_USER. Everything but the stamps is the same.
Game::randomEngine.seed(1234);
const auto legacy = Capture([&] { auto packet = MakePacket(bytes, sysAddr); LegacyAuthPackets::HandleLoginRequest(Game::server, &packet); });
Game::randomEngine.seed(1234);
const auto before = static_cast<uint64_t>(std::time(nullptr));
const auto converted = Capture([&] { Dispatch(bytes, sysAddr, AuthPackets::Handle); });
const auto after = static_cast<uint64_t>(std::time(nullptr));
ASSERT_EQ(legacy.size(), 1);
ASSERT_EQ(converted.size(), 1);
EXPECT_EQ(legacy[0].sysAddr, converted[0].sysAddr);
const auto read = [](const CapturedPacket& captured) {
RakNet::BitStream bitStream(const_cast<uint8_t*>(captured.bytes.data()), captured.bytes.size(), true);
ClientPackets::LoginResponse response;
EXPECT_TRUE(response.ReadHeader(bitStream));
EXPECT_TRUE(response.Deserialize(bitStream));
return response;
};
auto legacyResponse = read(legacy[0]);
auto convertedResponse = read(converted[0]);
const auto stamps = convertedResponse.stamps.list;
legacyResponse.stamps.list.clear();
convertedResponse.stamps.list.clear();
RakNet::BitStream legacyBytes;
legacyResponse.WritePacket(legacyBytes);
RakNet::BitStream convertedBytes;
convertedResponse.WritePacket(convertedBytes);
EXPECT_PACKET_EQ(FromBitStream(legacyBytes), FromBitStream(convertedBytes));
EXPECT_EQ(convertedResponse.responseCode, eLoginResponse::INVALID_USER);
// The steps the login went through, in order, stamped as they happened
const std::vector<std::pair<eStamps, uint32_t>> expected = {
{ eStamps::PASSPORT_AUTH_START, 0 },
{ eStamps::PASSPORT_AUTH_CLIENT_OS, static_cast<uint32_t>(ClientOS::WINDOWS) },
{ eStamps::PASSPORT_AUTH_DB_SELECT_START, 0 },
{ eStamps::PASSPORT_AUTH_DB_SELECT_FINISH, 0 }, // not found
{ eStamps::PASSPORT_AUTH_ERROR, 1 },
};
ASSERT_EQ(stamps.size(), expected.size());
for (size_t i = 0; i < stamps.size(); i++) {
EXPECT_EQ(stamps[i].type, expected[i].first) << i;
EXPECT_EQ(stamps[i].value, expected[i].second) << i;
EXPECT_GE(stamps[i].timestamp, before);
EXPECT_LE(stamps[i].timestamp, after);
if (i > 0) EXPECT_GE(stamps[i].timestamp, stamps[i - 1].timestamp);
}
const auto copy = RoundTrip(request);
EXPECT_EQ(copy.username.GetAsString(), username.substr(0, 33));
EXPECT_EQ(copy.password.GetAsString(), "hunter2");
EXPECT_EQ(copy.localeID, LanguageCodeID::en_US);
EXPECT_EQ(copy.buildNumber, 7601);
EXPECT_EQ(copy.platformID, 2);
ExpectTruncatedFails(request);
}
}
TEST_F(CommonAuthPacketsTests, LoginStampsRecordStepsAsTheyHappen) {
const auto before = static_cast<uint64_t>(std::time(nullptr));
Stamps stamps;
EXPECT_TRUE(stamps.empty());
stamps.Log("nobody"); // nothing to log
stamps.Add(eStamps::PASSPORT_AUTH_START);
stamps.Add(eStamps::PASSPORT_AUTH_WORLD_PACKET_RECEIVED, 42);
stamps.Add(eStamps::NO_WORLD_SERVER, 1);
const auto after = static_cast<uint64_t>(std::time(nullptr));
stamps.Log("somebody");
const auto& list = stamps.list;
ASSERT_EQ(list.size(), 3);
EXPECT_EQ(list[0].type, eStamps::PASSPORT_AUTH_START);
EXPECT_EQ(list[0].value, 0);
EXPECT_EQ(list[1].type, eStamps::PASSPORT_AUTH_WORLD_PACKET_RECEIVED);
EXPECT_EQ(list[1].value, 42);
EXPECT_EQ(list[2].type, eStamps::NO_WORLD_SERVER);
for (const auto& stamp : list) {
EXPECT_GE(stamp.timestamp, before);
EXPECT_LE(stamp.timestamp, after);
}
// A failed response carries exactly the stamps of the steps that ran, nothing appended
const auto sent = Capture([&] { AuthPackets::SendLoginResponse(Game::server, TestAddress(), eLoginResponse::WRONG_PASS, "", "", 2001, "somebody", stamps); });
ASSERT_EQ(sent.size(), 1);
RakNet::BitStream bitStream(const_cast<uint8_t*>(sent[0].bytes.data()), sent[0].bytes.size(), true);
ClientPackets::LoginResponse response;
ASSERT_TRUE(response.ReadHeader(bitStream));
ASSERT_TRUE(response.Deserialize(bitStream));
ASSERT_EQ(response.stamps.list.size(), 3);
for (size_t i = 0; i < 3; i++) {
EXPECT_EQ(response.stamps.list[i].type, list[i].type);
EXPECT_EQ(response.stamps.list[i].value, list[i].value);
EXPECT_EQ(response.stamps.list[i].timestamp, list[i].timestamp);
}
}
TEST_F(CommonAuthPacketsTests, StampGoldenBytes) {
ClientPackets::LoginResponse response;
response.stamps.list = { Stamp(eStamps::PASSPORT_AUTH_CLIENT_OS, 1, 0x0102030405060708) };
RakNet::BitStream bytes;
response.WritePacket(bytes);
// ... | error length 0 | stamps size 16 * 1 + 4 | CLIENT_OS (20) | value 1 | timestamp
const auto all = FromBitStream(bytes);
const std::vector<uint8_t> tail(all.bytes.end() - 22, all.bytes.end());
EXPECT_PACKET_EQ(FromHex("00 00 14 00 00 00 14 00 00 00 01 00 00 00 08 07 06 05 04 03 02 01"), (PacketBytes{ tail, 22 * 8 }));
}
TEST_F(CommonAuthPacketsTests, StampsRoundTrip) {
for (const size_t count : { size_t{ 0 }, size_t{ 1 }, size_t{ 7 } }) {
Stamps stamps;
for (size_t i = 0; i < count; i++) stamps.list.emplace_back(static_cast<eStamps>(i), static_cast<uint32_t>(i + 100), 1790000000 + i);
RakNet::BitStream bytes;
stamps.Serialize(bytes);
EXPECT_EQ(bytes.GetNumberOfBytesUsed(), 4 + 16 * count);
Stamps copy;
ASSERT_TRUE(copy.Deserialize(bytes));
EXPECT_EQ(bytes.GetNumberOfUnreadBits(), 0);
ASSERT_EQ(copy.size(), count);
for (size_t i = 0; i < count; i++) {
EXPECT_EQ(copy.list[i].type, stamps.list[i].type);
EXPECT_EQ(copy.list[i].value, stamps.list[i].value);
EXPECT_EQ(copy.list[i].timestamp, stamps.list[i].timestamp);
}
// Every truncation fails
for (uint32_t cut = 0; cut < bytes.GetNumberOfBytesUsed(); cut++) {
RakNet::BitStream truncated(bytes.GetData(), cut, true);
Stamps partial;
EXPECT_FALSE(partial.Deserialize(truncated)) << cut;
}
}
// A size that is not 4 + 16 * n is rejected
RakNet::BitStream bad;
bad.Write<uint32_t>(4 + 15);
bad.Write<uint64_t>(0);
bad.Write<uint64_t>(0);
Stamps rejected;
EXPECT_FALSE(rejected.Deserialize(bad));
}
TEST_F(CommonAuthPacketsTests, ZoneTransferResponseCarriesStamps) {
const auto sendResponse = [](const Stamps& stamps) {
MasterPackets::RequestZoneTransferResponse response;
response.requestID = 99;
response.mythranShift = true;
response.zoneID = 1000;
response.zoneInstance = 7;
response.zoneClone = 0;
response.serverIP = LUString("127.0.0.1", 255);
response.serverPort = 2001;
response.stamps = stamps;
MasterPackets::SendTo(TestAddress(), response);
};
Stamps stamps;
stamps.list = { Stamp(eStamps::PASSPORT_AUTH_START, 0, 10), Stamp(eStamps::PASSPORT_AUTH_WORLD_SESSION_CONFIRM_TO_AUTH, 7, 11) };
const auto sent = Capture([&] { sendResponse(stamps); });
ASSERT_EQ(sent.size(), 1);
RakNet::BitStream bitStream(const_cast<uint8_t*>(sent[0].bytes.data()), sent[0].bytes.size(), true);
LUBitStream header;
ASSERT_TRUE(header.ReadHeader(bitStream));
EXPECT_EQ(header.connectionType, ServiceType::MASTER);
uint64_t requestID{};
uint8_t mythranShift{};
uint32_t zoneID{};
uint32_t zoneInstance{};
uint32_t zoneClone{};
uint16_t port{};
LUString ip(255);
ASSERT_TRUE(bitStream.Read(requestID));
ASSERT_TRUE(bitStream.Read(mythranShift));
ASSERT_TRUE(bitStream.Read(zoneID));
ASSERT_TRUE(bitStream.Read(zoneInstance));
ASSERT_TRUE(bitStream.Read(zoneClone));
ASSERT_TRUE(bitStream.Read(port));
ASSERT_TRUE(bitStream.Read(ip));
EXPECT_EQ(requestID, 99);
EXPECT_EQ(port, 2001);
EXPECT_EQ(ip.string, "127.0.0.1");
Stamps copy;
ASSERT_TRUE(copy.Deserialize(bitStream));
EXPECT_EQ(bitStream.GetNumberOfUnreadBits(), 0);
ASSERT_EQ(copy.size(), 2);
EXPECT_EQ(copy.list[1].type, eStamps::PASSPORT_AUTH_WORLD_SESSION_CONFIRM_TO_AUTH);
EXPECT_EQ(copy.list[1].value, 7);
EXPECT_EQ(copy.list[1].timestamp, 11);
// Without a login the list is empty: just its size field
const auto plain = Capture([&] { sendResponse({}); });
ASSERT_EQ(plain.size(), 1);
EXPECT_EQ(plain[0].bytes.size() + 32, sent[0].bytes.size());
const std::vector<uint8_t> tail(plain[0].bytes.end() - 4, plain[0].bytes.end());
EXPECT_EQ(tail, (std::vector<uint8_t>{ 4, 0, 0, 0 }));
}