summaryrefslogtreecommitdiff
path: root/src/unittest
diff options
context:
space:
mode:
authorsfan5 <sfan5@live.de>2021-01-29 15:24:07 +0100
committersfan5 <sfan5@live.de>2021-02-02 20:46:08 +0100
commit674d67f312c815e7f10dc00705e352bc392fc2af (patch)
tree7d6d0567b2f557d1487ce90384121d8ff5e31e1c /src/unittest
parentc834d2ab25694ef2d67dc24f85f304269d202c8e (diff)
downloadminetest-674d67f312c815e7f10dc00705e352bc392fc2af.tar.gz
minetest-674d67f312c815e7f10dc00705e352bc392fc2af.tar.bz2
minetest-674d67f312c815e7f10dc00705e352bc392fc2af.zip
Encode high codepoints as surrogates to safely transport wchar_t over network
fixes #7643
Diffstat (limited to 'src/unittest')
-rw-r--r--src/unittest/test_connection.cpp35
1 files changed, 35 insertions, 0 deletions
diff --git a/src/unittest/test_connection.cpp b/src/unittest/test_connection.cpp
index c5e4085e1..c3aacc536 100644
--- a/src/unittest/test_connection.cpp
+++ b/src/unittest/test_connection.cpp
@@ -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