|
|
@@ -0,0 +1,104 @@
|
|
|
+#include <gtest/gtest.h>
|
|
|
+
|
|
|
+#include <nlohmann/json.hpp>
|
|
|
+
|
|
|
+#include "utf8_text.hpp"
|
|
|
+#include "functions/mime_base64.hpp"
|
|
|
+
|
|
|
+using svapi::isValidUtf8;
|
|
|
+
|
|
|
+namespace {
|
|
|
+
|
|
|
+/// nlohmann's OWN verdict on whether a byte string can be serialised.
|
|
|
+///
|
|
|
+/// The point of the cross-check: `isValidUtf8` exists to predict this, so the
|
|
|
+/// tests compare it against the real thing rather than against my reading of
|
|
|
+/// RFC 3629. If the two ever disagree, the 500 this guard removes would simply
|
|
|
+/// come back from a different line.
|
|
|
+bool nlohmannCanSerialise(const std::string& bytes) {
|
|
|
+ nlohmann::json j;
|
|
|
+ j["body"] = bytes;
|
|
|
+ try {
|
|
|
+ (void)j.dump();
|
|
|
+ return true;
|
|
|
+ } catch (const std::exception&) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+/// Every awkward case worth naming, good and bad together.
|
|
|
+const std::vector<std::pair<const char*, std::string>>& corpus() {
|
|
|
+ static const std::vector<std::pair<const char*, std::string>> c = {
|
|
|
+ // --- must be accepted
|
|
|
+ {"empty", ""},
|
|
|
+ {"ascii", "hello world"},
|
|
|
+ {"a NUL byte is legal UTF-8", std::string("a\0b", 3)},
|
|
|
+ {"hungarian", "\xc3\xa1rv\xc3\xadzt\xc5\xb1r\xc5\x91"},
|
|
|
+ {"two-byte lowest", "\xc2\x80"},
|
|
|
+ {"three-byte lowest", "\xe0\xa0\x80"},
|
|
|
+ {"four-byte lowest", "\xf0\x90\x80\x80"},
|
|
|
+ {"largest code point U+10FFFF", "\xf4\x8f\xbf\xbf"},
|
|
|
+ {"just below the surrogates", "\xed\x9f\xbf"},
|
|
|
+ {"just above the surrogates", "\xee\x80\x80"},
|
|
|
+ // --- must be refused
|
|
|
+ {"a PNG header", std::string("\x89PNG\r\n\x1a\n", 8)},
|
|
|
+ {"a lone continuation byte", "\x80"},
|
|
|
+ {"an overlong two-byte NUL", "\xc0\x80"},
|
|
|
+ {"an overlong C1", "\xc1\xbf"},
|
|
|
+ {"an overlong three-byte", "\xe0\x80\x80"},
|
|
|
+ {"an overlong four-byte", "\xf0\x80\x80\x80"},
|
|
|
+ {"a UTF-16 surrogate half", "\xed\xa0\x80"},
|
|
|
+ {"past U+10FFFF", "\xf4\x90\x80\x80"},
|
|
|
+ {"an F5 lead byte", "\xf5\x80\x80\x80"},
|
|
|
+ {"an FF byte", "\xff"},
|
|
|
+ {"a truncated two-byte", "\xc3"},
|
|
|
+ {"a truncated three-byte", "\xe0\xa0"},
|
|
|
+ {"a truncated four-byte", "\xf0\x90\x80"},
|
|
|
+ {"a good prefix then a bad byte", "ok then \xff"},
|
|
|
+ };
|
|
|
+ return c;
|
|
|
+}
|
|
|
+
|
|
|
+} // namespace
|
|
|
+
|
|
|
+TEST(Utf8Text, AgreesWithNlohmannOnEveryAwkwardCase) {
|
|
|
+ // The cross-check, and the only test here that could catch my own
|
|
|
+ // misreading of the spec: two independent implementations, compared.
|
|
|
+ for (const auto& [name, bytes] : corpus())
|
|
|
+ EXPECT_EQ(isValidUtf8(bytes), nlohmannCanSerialise(bytes))
|
|
|
+ << name << " (" << bytes.size() << " bytes)";
|
|
|
+}
|
|
|
+
|
|
|
+TEST(Utf8Text, AgreesWithNlohmannOnEverySingleByte) {
|
|
|
+ // Exhaustive over one byte: a lead-byte table is easy to get wrong in one
|
|
|
+ // row, and a hand-picked list would not notice.
|
|
|
+ for (int b = 0; b <= 0xFF; ++b) {
|
|
|
+ const std::string one(1, static_cast<char>(b));
|
|
|
+ EXPECT_EQ(isValidUtf8(one), nlohmannCanSerialise(one)) << "byte 0x" << std::hex << b;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+TEST(Utf8Text, AgreesWithNlohmannOnEveryTwoByteSequence) {
|
|
|
+ // Exhaustive over two bytes: 65536 cases, which covers the whole overlong
|
|
|
+ // and truncated-sequence space for the two-byte forms.
|
|
|
+ for (int a = 0; a <= 0xFF; ++a)
|
|
|
+ for (int b = 0; b <= 0xFF; ++b) {
|
|
|
+ std::string two;
|
|
|
+ two.push_back(static_cast<char>(a));
|
|
|
+ two.push_back(static_cast<char>(b));
|
|
|
+ ASSERT_EQ(isValidUtf8(two), nlohmannCanSerialise(two))
|
|
|
+ << "bytes 0x" << std::hex << a << " 0x" << b;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+TEST(Utf8Text, ABinaryBodyRoundTripsThroughBase64) {
|
|
|
+ // The other half of the fix: what cannot go in a JSON string goes in as
|
|
|
+ // base64, and must come back byte-identical.
|
|
|
+ const std::string png("\x89PNG\r\n\x1a\n\x00\x01\xfe\xff", 12);
|
|
|
+ ASSERT_FALSE(isValidUtf8(png));
|
|
|
+ const std::string encoded = svapi::functions::mimeBase64Encode(png);
|
|
|
+ // The encoded form must itself be safe to serialise, or nothing was gained.
|
|
|
+ EXPECT_TRUE(isValidUtf8(encoded));
|
|
|
+ EXPECT_TRUE(nlohmannCanSerialise(encoded));
|
|
|
+ EXPECT_EQ(svapi::functions::mimeBase64Decode(encoded), png);
|
|
|
+}
|