ddnet/src/test/json.cpp
heinrich5991 36694d3852 Add a way to call for external moderator help
This is done by HTTP POSTing to a location specified by
`sv_modhelp_url`. We also provide a `src/modhelp/server.py` which can
use theses POSTs to forward them to Discord servers.

The POST contains a JSON object payload, with the keys `"port"` which
contains the server port, `"player_id"` which contains the calling
player's client ID, `"player_name"` which contains the calling player's
nick and `"message"` which is the user-specified message.

Make JSON-escaping function public, add tests and fix bugs uncovered by
these tests.

Supersedes #1129.
2018-06-19 23:27:35 +02:00

23 lines
800 B
C++

#include <gtest/gtest.h>
#include <engine/shared/fetcher.h>
TEST(Json, Escape)
{
char aBuf[128];
char aSmall[2];
char aSix[6];
EXPECT_STREQ(EscapeJson(aBuf, sizeof(aBuf), ""), "");
EXPECT_STREQ(EscapeJson(aBuf, sizeof(aBuf), "a"), "a");
EXPECT_STREQ(EscapeJson(aBuf, sizeof(aBuf), "\n"), "\\n");
EXPECT_STREQ(EscapeJson(aBuf, sizeof(aBuf), "\\"), "\\\\"); // https://www.xkcd.com/1638/
EXPECT_STREQ(EscapeJson(aBuf, sizeof(aBuf), "\x1b"), "\\u001b"); // escape
EXPECT_STREQ(EscapeJson(aBuf, sizeof(aBuf), ""), "");
EXPECT_STREQ(EscapeJson(aBuf, sizeof(aBuf), "😂"), "😂");
// Truncations
EXPECT_STREQ(EscapeJson(aSmall, sizeof(aSmall), "\\"), "");
EXPECT_STREQ(EscapeJson(aSix, sizeof(aSix), "\x01"), "");
EXPECT_STREQ(EscapeJson(aSix, sizeof(aSix), "aaaaaa"), "aaaaa");
}