2022-01-20 18:27:56 +00:00
|
|
|
// This file Copyright (C) 2013-2022 Mnemosyne LLC.
|
2022-08-08 18:05:39 +00:00
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
|
2022-01-20 18:27:56 +00:00
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
#define LIBTRANSMISSION_VARIANT_MODULE
|
|
|
|
|
2023-07-08 15:24:03 +00:00
|
|
|
#include <cstdint> // int64_t
|
2023-04-27 14:10:33 +00:00
|
|
|
#include <locale>
|
|
|
|
#include <optional>
|
2023-07-08 15:24:03 +00:00
|
|
|
#include <stdexcept> // std::runtime_error
|
2021-10-20 02:30:50 +00:00
|
|
|
#include <string>
|
|
|
|
#include <string_view>
|
|
|
|
|
2023-07-08 15:24:03 +00:00
|
|
|
#include <libtransmission/quark.h>
|
2023-01-02 16:23:51 +00:00
|
|
|
#include <libtransmission/variant.h>
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
2021-10-15 13:28:47 +00:00
|
|
|
using namespace std::literals;
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
class JSONTest : public ::testing::TestWithParam<char const*>
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
void SetUp() override
|
|
|
|
{
|
|
|
|
auto const* locale_str = GetParam();
|
2023-04-27 14:10:33 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
old_locale_ = std::locale::global(std::locale{ {}, new std::numpunct_byname<char>{ locale_str } });
|
|
|
|
}
|
|
|
|
catch (std::runtime_error const&)
|
2020-08-11 18:11:55 +00:00
|
|
|
{
|
|
|
|
GTEST_SKIP();
|
|
|
|
}
|
|
|
|
}
|
2023-04-27 14:10:33 +00:00
|
|
|
|
|
|
|
void TearDown() override
|
|
|
|
{
|
|
|
|
if (old_locale_)
|
|
|
|
{
|
2023-06-22 22:58:16 +00:00
|
|
|
std::locale::global(*old_locale_);
|
2023-04-27 14:10:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::optional<std::locale> old_locale_;
|
2020-08-11 18:11:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
TEST_P(JSONTest, testElements)
|
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
auto const in = std::string{
|
2020-08-11 18:11:55 +00:00
|
|
|
"{ \"string\": \"hello world\","
|
|
|
|
" \"escaped\": \"bell \\b formfeed \\f linefeed \\n carriage return \\r tab \\t\","
|
|
|
|
" \"int\": 5, "
|
|
|
|
" \"float\": 6.5, "
|
|
|
|
" \"true\": true, "
|
|
|
|
" \"false\": false, "
|
|
|
|
" \"null\": null }"
|
|
|
|
};
|
|
|
|
|
2023-08-21 04:15:23 +00:00
|
|
|
auto var = tr_variant_serde::json().inplace().parse(in).value_or(tr_variant{});
|
|
|
|
EXPECT_TRUE(tr_variantIsDict(&var));
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2021-11-12 18:04:55 +00:00
|
|
|
auto sv = std::string_view{};
|
2021-10-15 13:28:47 +00:00
|
|
|
auto key = tr_quark_new("string"sv);
|
2023-08-21 04:15:23 +00:00
|
|
|
EXPECT_TRUE(tr_variantDictFindStrView(&var, key, &sv));
|
2021-11-12 18:04:55 +00:00
|
|
|
EXPECT_EQ("hello world"sv, sv);
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2023-08-21 04:15:23 +00:00
|
|
|
EXPECT_TRUE(tr_variantDictFindStrView(&var, tr_quark_new("escaped"sv), &sv));
|
2021-11-12 18:04:55 +00:00
|
|
|
EXPECT_EQ("bell \b formfeed \f linefeed \n carriage return \r tab \t"sv, sv);
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
auto i = int64_t{};
|
2023-08-21 04:15:23 +00:00
|
|
|
EXPECT_TRUE(tr_variantDictFindInt(&var, tr_quark_new("int"sv), &i));
|
2020-08-11 18:11:55 +00:00
|
|
|
EXPECT_EQ(5, i);
|
|
|
|
|
|
|
|
auto d = double{};
|
2023-08-21 04:15:23 +00:00
|
|
|
EXPECT_TRUE(tr_variantDictFindReal(&var, tr_quark_new("float"sv), &d));
|
2020-08-11 18:11:55 +00:00
|
|
|
EXPECT_EQ(65, int(d * 10));
|
|
|
|
|
|
|
|
auto f = bool{};
|
2023-08-21 04:15:23 +00:00
|
|
|
EXPECT_TRUE(tr_variantDictFindBool(&var, tr_quark_new("true"sv), &f));
|
2020-08-11 18:11:55 +00:00
|
|
|
EXPECT_TRUE(f);
|
|
|
|
|
2023-08-21 04:15:23 +00:00
|
|
|
EXPECT_TRUE(tr_variantDictFindBool(&var, tr_quark_new("false"sv), &f));
|
2020-08-11 18:11:55 +00:00
|
|
|
EXPECT_FALSE(f);
|
|
|
|
|
2023-08-21 04:15:23 +00:00
|
|
|
EXPECT_TRUE(tr_variantDictFindStrView(&var, tr_quark_new("null"sv), &sv));
|
2021-11-12 18:04:55 +00:00
|
|
|
EXPECT_EQ(""sv, sv);
|
2020-08-11 18:11:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_P(JSONTest, testUtf8)
|
|
|
|
{
|
2021-11-18 05:37:35 +00:00
|
|
|
auto in = "{ \"key\": \"Letöltések\" }"sv;
|
2021-11-12 18:04:55 +00:00
|
|
|
auto sv = std::string_view{};
|
2021-10-15 13:28:47 +00:00
|
|
|
tr_quark const key = tr_quark_new("key"sv);
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2023-08-17 16:02:45 +00:00
|
|
|
auto serde = tr_variant_serde::json();
|
|
|
|
serde.inplace();
|
2023-08-21 04:15:23 +00:00
|
|
|
auto var = serde.parse(in).value_or(tr_variant{});
|
|
|
|
EXPECT_TRUE(tr_variantIsDict(&var));
|
|
|
|
EXPECT_TRUE(tr_variantDictFindStrView(&var, key, &sv));
|
2021-11-12 18:04:55 +00:00
|
|
|
EXPECT_EQ("Letöltések"sv, sv);
|
2023-08-21 04:15:23 +00:00
|
|
|
var.clear();
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2021-11-18 05:37:35 +00:00
|
|
|
in = R"({ "key": "\u005C" })"sv;
|
2023-08-21 04:15:23 +00:00
|
|
|
var = serde.parse(in).value_or(tr_variant{});
|
|
|
|
EXPECT_TRUE(tr_variantIsDict(&var));
|
|
|
|
EXPECT_TRUE(tr_variantDictFindStrView(&var, key, &sv));
|
2021-11-12 18:04:55 +00:00
|
|
|
EXPECT_EQ("\\"sv, sv);
|
2023-08-21 04:15:23 +00:00
|
|
|
var.clear();
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 1. Feed it JSON-escaped nonascii to the JSON decoder.
|
|
|
|
* 2. Confirm that the result is UTF-8.
|
|
|
|
* 3. Feed the same UTF-8 back into the JSON encoder.
|
|
|
|
* 4. Confirm that the result is JSON-escaped.
|
|
|
|
* 5. Dogfood that result back into the parser.
|
|
|
|
* 6. Confirm that the result is UTF-8.
|
|
|
|
*/
|
2021-11-18 05:37:35 +00:00
|
|
|
in = R"({ "key": "Let\u00f6lt\u00e9sek" })"sv;
|
2023-08-21 04:15:23 +00:00
|
|
|
var = serde.parse(in).value_or(tr_variant{});
|
|
|
|
EXPECT_TRUE(tr_variantIsDict(&var));
|
|
|
|
EXPECT_TRUE(tr_variantDictFindStrView(&var, key, &sv));
|
2021-11-12 18:04:55 +00:00
|
|
|
EXPECT_EQ("Letöltések"sv, sv);
|
2023-08-21 04:15:23 +00:00
|
|
|
auto json = serde.to_string(var);
|
|
|
|
var.clear();
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2021-12-27 22:47:25 +00:00
|
|
|
EXPECT_FALSE(std::empty(json));
|
|
|
|
EXPECT_NE(std::string::npos, json.find("\\u00f6"));
|
|
|
|
EXPECT_NE(std::string::npos, json.find("\\u00e9"));
|
2023-08-21 04:15:23 +00:00
|
|
|
var = serde.parse(json).value_or(tr_variant{});
|
|
|
|
EXPECT_TRUE(tr_variantIsDict(&var));
|
|
|
|
EXPECT_TRUE(tr_variantDictFindStrView(&var, key, &sv));
|
2021-11-12 18:04:55 +00:00
|
|
|
EXPECT_EQ("Letöltések"sv, sv);
|
2020-08-11 18:11:55 +00:00
|
|
|
}
|
|
|
|
|
2023-03-02 06:55:16 +00:00
|
|
|
TEST_P(JSONTest, testUtf16Surrogates)
|
|
|
|
{
|
|
|
|
static auto constexpr ThinkingFaceEmojiUtf8 = "\xf0\x9f\xa4\x94"sv;
|
2023-08-21 04:15:23 +00:00
|
|
|
auto var = tr_variant{};
|
|
|
|
tr_variantInitDict(&var, 1);
|
2023-03-02 06:55:16 +00:00
|
|
|
auto const key = tr_quark_new("key"sv);
|
2023-08-21 04:15:23 +00:00
|
|
|
tr_variantDictAddStr(&var, key, ThinkingFaceEmojiUtf8);
|
2023-08-17 16:02:45 +00:00
|
|
|
|
|
|
|
auto serde = tr_variant_serde::json();
|
2023-08-21 04:15:23 +00:00
|
|
|
auto const json = serde.compact().to_string(var);
|
2023-03-02 06:55:16 +00:00
|
|
|
EXPECT_NE(std::string::npos, json.find("ud83e"));
|
|
|
|
EXPECT_NE(std::string::npos, json.find("udd14"));
|
|
|
|
|
2023-08-17 16:02:45 +00:00
|
|
|
auto parsed = serde.parse(json).value_or(tr_variant{});
|
2023-03-02 06:55:16 +00:00
|
|
|
EXPECT_TRUE(tr_variantIsDict(&parsed));
|
|
|
|
auto value = std::string_view{};
|
|
|
|
EXPECT_TRUE(tr_variantDictFindStrView(&parsed, key, &value));
|
|
|
|
EXPECT_EQ(ThinkingFaceEmojiUtf8, value);
|
|
|
|
}
|
|
|
|
|
2020-08-11 18:11:55 +00:00
|
|
|
TEST_P(JSONTest, test1)
|
|
|
|
{
|
2023-08-17 16:02:45 +00:00
|
|
|
static auto constexpr Input =
|
2020-08-11 18:11:55 +00:00
|
|
|
"{\n"
|
|
|
|
" \"headers\": {\n"
|
|
|
|
" \"type\": \"request\",\n"
|
|
|
|
" \"tag\": 666\n"
|
|
|
|
" },\n"
|
|
|
|
" \"body\": {\n"
|
|
|
|
" \"name\": \"torrent-info\",\n"
|
|
|
|
" \"arguments\": {\n"
|
|
|
|
" \"ids\": [ 7, 10 ]\n"
|
|
|
|
" }\n"
|
|
|
|
" }\n"
|
2023-08-17 16:02:45 +00:00
|
|
|
"}\n"sv;
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2023-08-17 16:02:45 +00:00
|
|
|
auto serde = tr_variant_serde::json();
|
2023-08-21 04:15:23 +00:00
|
|
|
auto var = serde.inplace().parse(Input).value_or(tr_variant{});
|
|
|
|
EXPECT_TRUE(tr_variantIsDict(&var));
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2021-11-12 18:04:55 +00:00
|
|
|
auto sv = std::string_view{};
|
2022-11-14 20:16:29 +00:00
|
|
|
auto i = int64_t{};
|
2023-08-21 04:15:23 +00:00
|
|
|
auto* headers = tr_variantDictFind(&var, tr_quark_new("headers"sv));
|
2020-08-11 18:11:55 +00:00
|
|
|
EXPECT_NE(nullptr, headers);
|
|
|
|
EXPECT_TRUE(tr_variantIsDict(headers));
|
2021-11-12 18:04:55 +00:00
|
|
|
EXPECT_TRUE(tr_variantDictFindStrView(headers, tr_quark_new("type"sv), &sv));
|
|
|
|
EXPECT_EQ("request"sv, sv);
|
2020-08-11 18:11:55 +00:00
|
|
|
EXPECT_TRUE(tr_variantDictFindInt(headers, TR_KEY_tag, &i));
|
|
|
|
EXPECT_EQ(666, i);
|
2023-08-21 04:15:23 +00:00
|
|
|
auto* body = tr_variantDictFind(&var, tr_quark_new("body"sv));
|
2020-08-11 18:11:55 +00:00
|
|
|
EXPECT_NE(nullptr, body);
|
2021-11-12 18:04:55 +00:00
|
|
|
EXPECT_TRUE(tr_variantDictFindStrView(body, TR_KEY_name, &sv));
|
|
|
|
EXPECT_EQ("torrent-info"sv, sv);
|
2021-10-15 13:28:47 +00:00
|
|
|
auto* args = tr_variantDictFind(body, tr_quark_new("arguments"sv));
|
2020-08-11 18:11:55 +00:00
|
|
|
EXPECT_NE(nullptr, args);
|
|
|
|
EXPECT_TRUE(tr_variantIsDict(args));
|
|
|
|
auto* ids = tr_variantDictFind(args, TR_KEY_ids);
|
|
|
|
EXPECT_NE(nullptr, ids);
|
|
|
|
EXPECT_TRUE(tr_variantIsList(ids));
|
2022-04-02 14:06:02 +00:00
|
|
|
EXPECT_EQ(2U, tr_variantListSize(ids));
|
2020-08-11 18:11:55 +00:00
|
|
|
EXPECT_TRUE(tr_variantGetInt(tr_variantListChild(ids, 0), &i));
|
|
|
|
EXPECT_EQ(7, i);
|
|
|
|
EXPECT_TRUE(tr_variantGetInt(tr_variantListChild(ids, 1), &i));
|
|
|
|
EXPECT_EQ(10, i);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_P(JSONTest, test2)
|
|
|
|
{
|
2023-08-17 16:02:45 +00:00
|
|
|
static auto constexpr Input = " "sv;
|
2023-08-21 04:15:23 +00:00
|
|
|
auto var = tr_variant_serde::json().inplace().parse(Input);
|
|
|
|
EXPECT_FALSE(var.has_value());
|
2020-08-11 18:11:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_P(JSONTest, test3)
|
|
|
|
{
|
2023-08-17 16:02:45 +00:00
|
|
|
static auto constexpr Input =
|
|
|
|
"{ \"error\": 2,"
|
|
|
|
" \"errorString\": \"torrent not registered with this tracker 6UHsVW'*C\","
|
|
|
|
" \"eta\": 262792,"
|
|
|
|
" \"id\": 25,"
|
|
|
|
" \"leftUntilDone\": 2275655680 }"sv;
|
|
|
|
|
2023-08-21 04:15:23 +00:00
|
|
|
auto var = tr_variant_serde::json().inplace().parse(Input).value_or(tr_variant{});
|
|
|
|
EXPECT_TRUE(tr_variantIsDict(&var));
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2021-11-12 18:04:55 +00:00
|
|
|
auto sv = std::string_view{};
|
2023-08-21 04:15:23 +00:00
|
|
|
EXPECT_TRUE(tr_variantDictFindStrView(&var, TR_KEY_errorString, &sv));
|
2021-11-12 18:04:55 +00:00
|
|
|
EXPECT_EQ("torrent not registered with this tracker 6UHsVW'*C"sv, sv);
|
2020-08-11 18:11:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_P(JSONTest, unescape)
|
|
|
|
{
|
2023-08-17 16:02:45 +00:00
|
|
|
static auto constexpr Input = R"({ "string-1": "\/usr\/lib" })"sv;
|
|
|
|
|
2023-08-21 04:15:23 +00:00
|
|
|
auto var = tr_variant_serde::json().inplace().parse(Input).value_or(tr_variant{});
|
|
|
|
EXPECT_TRUE(tr_variantIsDict(&var));
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2021-11-12 18:04:55 +00:00
|
|
|
auto sv = std::string_view{};
|
2023-08-21 04:15:23 +00:00
|
|
|
EXPECT_TRUE(tr_variantDictFindStrView(&var, tr_quark_new("string-1"sv), &sv));
|
2021-11-12 18:04:55 +00:00
|
|
|
EXPECT_EQ("/usr/lib"sv, sv);
|
2020-08-11 18:11:55 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
INSTANTIATE_TEST_SUITE_P( //
|
2020-08-11 18:11:55 +00:00
|
|
|
JSON,
|
|
|
|
JSONTest,
|
2021-08-15 09:41:48 +00:00
|
|
|
::testing::Values( //
|
|
|
|
"C",
|
|
|
|
"da_DK.UTF-8",
|
|
|
|
"fr_FR.UTF-8",
|
|
|
|
"ru_RU.UTF-8"));
|