1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-07-22 17:18:39 +00:00

Encode high codepoints as surrogates to safely transport wchar_t over network

fixes #7643
This commit is contained in:
sfan5 2021-01-29 15:24:07 +01:00
parent c834d2ab25
commit 674d67f312
5 changed files with 80 additions and 26 deletions

View file

@ -39,6 +39,7 @@ public:
void runTests(IGameDef *gamedef);
void testNetworkPacketSerialize();
void testHelpers();
void testConnectSendReceive();
};
@ -47,6 +48,7 @@ static TestConnection g_test_instance;
void TestConnection::runTests(IGameDef *gamedef)
{
TEST(testNetworkPacketSerialize);
TEST(testHelpers);
TEST(testConnectSendReceive);
}
@ -78,6 +80,39 @@ struct Handler : public con::PeerHandler
const char *name;
};
void TestConnection::testNetworkPacketSerialize()
{
const static u8 expected[] = {
0x00, 0x7b,
0x00, 0x02, 0xd8, 0x42, 0xdf, 0x9a
};
if (sizeof(wchar_t) == 2)
warningstream << __func__ << " may fail on this platform." << std::endl;
{
NetworkPacket pkt(123, 0);
// serializing wide strings should do surrogate encoding, we test that here
pkt << std::wstring(L"\U00020b9a");
SharedBuffer<u8> buf = pkt.oldForgePacket();
UASSERTEQ(int, buf.getSize(), sizeof(expected));
UASSERT(!memcmp(expected, &buf[0], buf.getSize()));
}
{
NetworkPacket pkt;
pkt.putRawPacket(expected, sizeof(expected), 0);
// same for decoding
std::wstring pkt_s;
pkt >> pkt_s;
UASSERT(pkt_s == L"\U00020b9a");
}
}
void TestConnection::testHelpers()
{
// Some constants for testing