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-01-02 16:23:51 +00:00
|
|
|
#include <libtransmission/transmission.h>
|
2022-01-24 06:30:00 +00:00
|
|
|
|
2023-01-02 16:23:51 +00:00
|
|
|
#include <libtransmission/benc.h>
|
|
|
|
#include <libtransmission/crypto-utils.h> // tr_rand_buffer(), tr_rand_int()
|
|
|
|
#include <libtransmission/error.h>
|
|
|
|
#include <libtransmission/variant-common.h>
|
|
|
|
#include <libtransmission/variant.h>
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <array>
|
|
|
|
#include <cmath> // lrint()
|
|
|
|
#include <cctype> // isspace()
|
|
|
|
#include <string>
|
2021-10-15 13:28:47 +00:00
|
|
|
#include <string_view>
|
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 VariantTest : public ::testing::Test
|
|
|
|
{
|
|
|
|
protected:
|
2022-01-24 19:07:55 +00:00
|
|
|
static std::string stripWhitespace(std::string const& in)
|
2020-08-11 18:11:55 +00:00
|
|
|
{
|
|
|
|
auto s = in;
|
|
|
|
s.erase(s.begin(), std::find_if_not(s.begin(), s.end(), ::isspace));
|
|
|
|
s.erase(std::find_if_not(s.rbegin(), s.rend(), ::isspace).base(), s.end());
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#ifndef _WIN32
|
2021-08-15 09:41:48 +00:00
|
|
|
#define STACK_SMASH_DEPTH (1 * 1000 * 1000)
|
2020-08-11 18:11:55 +00:00
|
|
|
#else
|
2021-08-15 09:41:48 +00:00
|
|
|
#define STACK_SMASH_DEPTH (100 * 1000)
|
2020-08-11 18:11:55 +00:00
|
|
|
#endif
|
|
|
|
|
2021-11-01 21:30:18 +00:00
|
|
|
TEST_F(VariantTest, getType)
|
|
|
|
{
|
|
|
|
auto i = int64_t{};
|
|
|
|
auto b = bool{};
|
|
|
|
auto d = double{};
|
|
|
|
auto sv = std::string_view{};
|
|
|
|
auto v = tr_variant{};
|
|
|
|
|
|
|
|
tr_variantInitInt(&v, 30);
|
|
|
|
EXPECT_TRUE(tr_variantGetInt(&v, &i));
|
|
|
|
EXPECT_EQ(30, i);
|
|
|
|
EXPECT_TRUE(tr_variantGetReal(&v, &d));
|
|
|
|
EXPECT_EQ(30, int(d));
|
|
|
|
EXPECT_FALSE(tr_variantGetBool(&v, &b));
|
|
|
|
EXPECT_FALSE(tr_variantGetStrView(&v, &sv));
|
|
|
|
|
|
|
|
auto strkey = "foo"sv;
|
|
|
|
tr_variantInitStr(&v, strkey);
|
|
|
|
EXPECT_FALSE(tr_variantGetBool(&v, &b));
|
|
|
|
EXPECT_TRUE(tr_variantGetStrView(&v, &sv));
|
|
|
|
EXPECT_EQ(strkey, sv);
|
2021-11-15 07:21:57 +00:00
|
|
|
EXPECT_NE(std::data(strkey), std::data(sv));
|
|
|
|
|
|
|
|
strkey = "anything"sv;
|
|
|
|
tr_variantInitStrView(&v, strkey);
|
|
|
|
EXPECT_TRUE(tr_variantGetStrView(&v, &sv));
|
|
|
|
EXPECT_EQ(strkey, sv);
|
|
|
|
EXPECT_EQ(std::data(strkey), std::data(sv)); // literally the same memory
|
|
|
|
EXPECT_EQ(std::size(strkey), std::size(sv));
|
2021-11-01 21:30:18 +00:00
|
|
|
|
|
|
|
strkey = "true"sv;
|
|
|
|
tr_variantInitStr(&v, strkey);
|
|
|
|
EXPECT_TRUE(tr_variantGetBool(&v, &b));
|
|
|
|
EXPECT_TRUE(b);
|
|
|
|
EXPECT_TRUE(tr_variantGetStrView(&v, &sv));
|
|
|
|
EXPECT_EQ(strkey, sv);
|
|
|
|
|
|
|
|
strkey = "false"sv;
|
|
|
|
tr_variantInitStr(&v, strkey);
|
|
|
|
EXPECT_TRUE(tr_variantGetBool(&v, &b));
|
|
|
|
EXPECT_FALSE(b);
|
|
|
|
EXPECT_TRUE(tr_variantGetStrView(&v, &sv));
|
|
|
|
EXPECT_EQ(strkey, sv);
|
|
|
|
}
|
|
|
|
|
2020-08-11 18:11:55 +00:00
|
|
|
TEST_F(VariantTest, parseInt)
|
|
|
|
{
|
2021-11-17 03:29:30 +00:00
|
|
|
auto constexpr Benc = "i64e"sv;
|
2021-08-15 09:41:48 +00:00
|
|
|
auto constexpr ExpectVal = int64_t{ 64 };
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2021-11-17 03:29:30 +00:00
|
|
|
auto benc = Benc;
|
2022-01-24 06:30:00 +00:00
|
|
|
auto const value = transmission::benc::impl::ParseInt(&benc);
|
2023-01-26 16:50:42 +00:00
|
|
|
EXPECT_TRUE(value.has_value());
|
|
|
|
assert(value.has_value());
|
2021-11-17 03:29:30 +00:00
|
|
|
EXPECT_EQ(ExpectVal, *value);
|
|
|
|
EXPECT_EQ(std::data(Benc) + std::size(Benc), std::data(benc));
|
2020-08-11 18:11:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(VariantTest, parseIntWithMissingEnd)
|
|
|
|
{
|
2021-11-17 03:29:30 +00:00
|
|
|
auto constexpr Benc = "i64"sv;
|
|
|
|
|
|
|
|
auto benc = Benc;
|
2022-01-24 06:30:00 +00:00
|
|
|
EXPECT_FALSE(transmission::benc::impl::ParseInt(&benc));
|
2021-11-17 03:29:30 +00:00
|
|
|
EXPECT_EQ(std::data(Benc), std::data(benc));
|
2020-08-11 18:11:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(VariantTest, parseIntEmptyBuffer)
|
|
|
|
{
|
2021-11-17 03:29:30 +00:00
|
|
|
auto constexpr Benc = ""sv;
|
|
|
|
|
|
|
|
auto benc = Benc;
|
2022-01-24 06:30:00 +00:00
|
|
|
EXPECT_FALSE(transmission::benc::impl::ParseInt(&benc));
|
2021-11-17 03:29:30 +00:00
|
|
|
EXPECT_EQ(std::data(Benc), std::data(benc));
|
2020-08-11 18:11:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(VariantTest, parseIntWithBadDigits)
|
|
|
|
{
|
2021-11-17 03:29:30 +00:00
|
|
|
auto constexpr Benc = "i6z4e"sv;
|
|
|
|
|
|
|
|
auto benc = Benc;
|
2022-01-24 06:30:00 +00:00
|
|
|
EXPECT_FALSE(transmission::benc::impl::ParseInt(&benc));
|
2021-11-17 03:29:30 +00:00
|
|
|
EXPECT_EQ(std::data(Benc), std::data(benc));
|
2020-08-11 18:11:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(VariantTest, parseNegativeInt)
|
|
|
|
{
|
2021-11-17 03:29:30 +00:00
|
|
|
auto constexpr Benc = "i-3e"sv;
|
|
|
|
auto constexpr Expected = int64_t{ -3 };
|
|
|
|
|
|
|
|
auto benc = Benc;
|
2022-01-24 06:30:00 +00:00
|
|
|
auto const value = transmission::benc::impl::ParseInt(&benc);
|
2023-01-26 16:50:42 +00:00
|
|
|
EXPECT_TRUE(value.has_value());
|
|
|
|
assert(value.has_value());
|
2021-11-17 03:29:30 +00:00
|
|
|
EXPECT_EQ(Expected, *value);
|
|
|
|
EXPECT_EQ(std::data(Benc) + std::size(Benc), std::data(benc));
|
|
|
|
}
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2021-11-17 03:29:30 +00:00
|
|
|
TEST_F(VariantTest, parseNegativeWithLeadingZero)
|
|
|
|
{
|
|
|
|
auto constexpr Benc = "i-03e"sv;
|
|
|
|
|
|
|
|
auto benc = Benc;
|
2022-01-24 06:30:00 +00:00
|
|
|
EXPECT_FALSE(transmission::benc::impl::ParseInt(&benc));
|
2021-11-17 03:29:30 +00:00
|
|
|
EXPECT_EQ(std::data(Benc), std::data(benc));
|
2020-08-11 18:11:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(VariantTest, parseIntZero)
|
|
|
|
{
|
2021-11-17 03:29:30 +00:00
|
|
|
auto constexpr Benc = "i0e"sv;
|
|
|
|
auto constexpr Expected = int64_t{ 0 };
|
|
|
|
|
|
|
|
auto benc = Benc;
|
2022-01-24 06:30:00 +00:00
|
|
|
auto const value = transmission::benc::impl::ParseInt(&benc);
|
2023-01-26 16:50:42 +00:00
|
|
|
EXPECT_TRUE(value.has_value());
|
|
|
|
assert(value.has_value());
|
2021-11-17 03:29:30 +00:00
|
|
|
EXPECT_EQ(Expected, *value);
|
|
|
|
EXPECT_EQ(std::data(Benc) + std::size(Benc), std::data(benc));
|
2020-08-11 18:11:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(VariantTest, parseIntWithLeadingZero)
|
|
|
|
{
|
2021-11-17 03:29:30 +00:00
|
|
|
auto constexpr Benc = "i04e"sv;
|
|
|
|
|
|
|
|
auto benc = Benc;
|
2022-01-24 06:30:00 +00:00
|
|
|
EXPECT_FALSE(transmission::benc::impl::ParseInt(&benc));
|
2021-11-17 03:29:30 +00:00
|
|
|
EXPECT_EQ(std::data(Benc), std::data(benc));
|
2020-08-11 18:11:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(VariantTest, str)
|
|
|
|
{
|
2022-01-24 06:30:00 +00:00
|
|
|
using namespace transmission::benc::impl;
|
|
|
|
|
2020-08-11 18:11:55 +00:00
|
|
|
// string len is designed to overflow
|
2021-11-17 03:29:30 +00:00
|
|
|
auto benc = "99999999999999999999:boat"sv;
|
|
|
|
auto inout = benc;
|
2022-01-24 06:30:00 +00:00
|
|
|
auto value = ParseString(&inout);
|
2021-11-17 03:29:30 +00:00
|
|
|
EXPECT_FALSE(value);
|
|
|
|
EXPECT_EQ(benc, inout);
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
// good string
|
2021-11-17 03:29:30 +00:00
|
|
|
inout = benc = "4:boat";
|
2022-01-24 06:30:00 +00:00
|
|
|
value = ParseString(&inout);
|
2023-01-26 16:50:42 +00:00
|
|
|
EXPECT_TRUE(value.has_value());
|
|
|
|
assert(value.has_value());
|
2021-11-17 03:29:30 +00:00
|
|
|
EXPECT_EQ("boat"sv, *value);
|
|
|
|
EXPECT_EQ(std::data(benc) + std::size(benc), std::data(inout));
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
// string goes past end of buffer
|
2021-11-17 03:29:30 +00:00
|
|
|
inout = benc = "4:boa"sv;
|
2022-01-24 06:30:00 +00:00
|
|
|
value = ParseString(&inout);
|
2021-11-17 03:29:30 +00:00
|
|
|
EXPECT_FALSE(value);
|
|
|
|
EXPECT_EQ(benc, inout);
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
// empty string
|
2021-11-17 03:29:30 +00:00
|
|
|
inout = benc = "0:"sv;
|
2022-01-24 06:30:00 +00:00
|
|
|
value = ParseString(&inout);
|
2023-01-26 16:50:42 +00:00
|
|
|
EXPECT_TRUE(value.has_value());
|
|
|
|
assert(value.has_value());
|
2021-11-17 03:29:30 +00:00
|
|
|
EXPECT_EQ(""sv, *value);
|
|
|
|
EXPECT_EQ(std::data(benc) + std::size(benc), std::data(inout));
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
// short string
|
2021-11-17 03:29:30 +00:00
|
|
|
inout = benc = "3:boat";
|
2022-01-24 06:30:00 +00:00
|
|
|
value = ParseString(&inout);
|
2023-01-26 16:50:42 +00:00
|
|
|
EXPECT_TRUE(value.has_value());
|
|
|
|
assert(value.has_value());
|
2021-11-17 03:29:30 +00:00
|
|
|
EXPECT_EQ("boa"sv, *value);
|
|
|
|
EXPECT_EQ(std::data(benc) + benc.find('t'), std::data(inout));
|
2020-08-11 18:11:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(VariantTest, parse)
|
|
|
|
{
|
2021-11-15 07:21:57 +00:00
|
|
|
auto benc = "i64e"sv;
|
|
|
|
auto i = int64_t{};
|
|
|
|
auto val = tr_variant{};
|
2022-11-14 20:16:29 +00:00
|
|
|
char const* end = nullptr;
|
2021-11-18 05:37:35 +00:00
|
|
|
auto ok = tr_variantFromBuf(&val, TR_VARIANT_PARSE_BENC | TR_VARIANT_PARSE_INPLACE, benc, &end);
|
|
|
|
EXPECT_TRUE(ok);
|
2020-08-11 18:11:55 +00:00
|
|
|
EXPECT_TRUE(tr_variantGetInt(&val, &i));
|
|
|
|
EXPECT_EQ(int64_t(64), i);
|
2021-11-15 07:21:57 +00:00
|
|
|
EXPECT_EQ(std::data(benc) + std::size(benc), end);
|
2022-08-18 01:25:42 +00:00
|
|
|
tr_variantClear(&val);
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2021-11-15 07:21:57 +00:00
|
|
|
benc = "li64ei32ei16ee"sv;
|
2021-11-18 05:37:35 +00:00
|
|
|
ok = tr_variantFromBuf(&val, TR_VARIANT_PARSE_BENC | TR_VARIANT_PARSE_INPLACE, benc, &end);
|
|
|
|
EXPECT_TRUE(ok);
|
2021-11-15 07:21:57 +00:00
|
|
|
EXPECT_EQ(std::data(benc) + std::size(benc), end);
|
2020-08-11 18:11:55 +00:00
|
|
|
EXPECT_EQ(size_t{ 3 }, tr_variantListSize(&val));
|
|
|
|
EXPECT_TRUE(tr_variantGetInt(tr_variantListChild(&val, 0), &i));
|
|
|
|
EXPECT_EQ(64, i);
|
|
|
|
EXPECT_TRUE(tr_variantGetInt(tr_variantListChild(&val, 1), &i));
|
|
|
|
EXPECT_EQ(32, i);
|
|
|
|
EXPECT_TRUE(tr_variantGetInt(tr_variantListChild(&val, 2), &i));
|
|
|
|
EXPECT_EQ(16, i);
|
2021-12-27 22:47:25 +00:00
|
|
|
EXPECT_EQ(benc, tr_variantToStr(&val, TR_VARIANT_FMT_BENC));
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2022-08-18 01:25:42 +00:00
|
|
|
tr_variantClear(&val);
|
2020-08-11 18:11:55 +00:00
|
|
|
end = nullptr;
|
|
|
|
|
2021-11-15 07:21:57 +00:00
|
|
|
benc = "lllee"sv;
|
2021-11-18 05:37:35 +00:00
|
|
|
ok = tr_variantFromBuf(&val, TR_VARIANT_PARSE_BENC | TR_VARIANT_PARSE_INPLACE, benc, &end);
|
|
|
|
EXPECT_FALSE(ok);
|
2020-08-11 18:11:55 +00:00
|
|
|
EXPECT_EQ(nullptr, end);
|
|
|
|
|
2021-11-15 07:21:57 +00:00
|
|
|
benc = "le"sv;
|
2021-11-18 05:37:35 +00:00
|
|
|
EXPECT_TRUE(tr_variantFromBuf(&val, TR_VARIANT_PARSE_BENC | TR_VARIANT_PARSE_INPLACE, benc, &end));
|
2021-11-15 07:21:57 +00:00
|
|
|
EXPECT_EQ(std::data(benc) + std::size(benc), end);
|
2021-12-27 22:47:25 +00:00
|
|
|
EXPECT_EQ(benc, tr_variantToStr(&val, TR_VARIANT_FMT_BENC));
|
2022-08-18 01:25:42 +00:00
|
|
|
tr_variantClear(&val);
|
2022-01-10 20:27:05 +00:00
|
|
|
|
|
|
|
benc = "d20:"sv;
|
|
|
|
end = nullptr;
|
|
|
|
ok = tr_variantFromBuf(&val, TR_VARIANT_PARSE_BENC | TR_VARIANT_PARSE_INPLACE, benc, &end);
|
|
|
|
EXPECT_FALSE(ok);
|
|
|
|
EXPECT_EQ(nullptr, end);
|
2020-08-11 18:11:55 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
TEST_F(VariantTest, bencParseAndReencode)
|
|
|
|
{
|
2020-09-07 21:19:10 +00:00
|
|
|
struct LocalTest
|
2020-08-11 18:11:55 +00:00
|
|
|
{
|
2021-11-15 07:21:57 +00:00
|
|
|
std::string_view benc;
|
2020-08-11 18:11:55 +00:00
|
|
|
bool is_good;
|
|
|
|
};
|
|
|
|
|
2021-11-15 07:21:57 +00:00
|
|
|
auto constexpr Tests = std::array<LocalTest, 9>{ {
|
|
|
|
{ "llleee"sv, true },
|
|
|
|
{ "d3:cow3:moo4:spam4:eggse"sv, true },
|
|
|
|
{ "d4:spaml1:a1:bee"sv, true },
|
|
|
|
{ "d5:greenli1ei2ei3ee4:spamd1:ai123e3:keyi214eee"sv, true },
|
|
|
|
{ "d9:publisher3:bob17:publisher-webpage15:www.example.com18:publisher.location4:homee"sv, true },
|
|
|
|
{ "d8:completei1e8:intervali1800e12:min intervali1800e5:peers0:e"sv, true },
|
|
|
|
{ "d1:ai0e1:be"sv, false }, // odd number of children
|
|
|
|
{ ""sv, false },
|
|
|
|
{ " "sv, false },
|
|
|
|
} };
|
|
|
|
|
|
|
|
for (auto const& test : Tests)
|
2020-08-11 18:11:55 +00:00
|
|
|
{
|
|
|
|
tr_variant val;
|
|
|
|
char const* end = nullptr;
|
2021-11-18 05:37:35 +00:00
|
|
|
auto const is_good = tr_variantFromBuf(&val, TR_VARIANT_PARSE_BENC | TR_VARIANT_PARSE_INPLACE, test.benc, &end);
|
2022-01-24 06:30:00 +00:00
|
|
|
|
2021-11-18 05:37:35 +00:00
|
|
|
EXPECT_EQ(test.is_good, is_good);
|
|
|
|
if (is_good)
|
2020-08-11 18:11:55 +00:00
|
|
|
{
|
|
|
|
EXPECT_EQ(test.benc.data() + test.benc.size(), end);
|
2021-12-27 22:47:25 +00:00
|
|
|
EXPECT_EQ(test.benc, tr_variantToStr(&val, TR_VARIANT_FMT_BENC));
|
2022-08-18 01:25:42 +00:00
|
|
|
tr_variantClear(&val);
|
2020-08-11 18:11:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(VariantTest, bencSortWhenSerializing)
|
|
|
|
{
|
2021-11-15 07:21:57 +00:00
|
|
|
auto constexpr In = "lld1:bi32e1:ai64eeee"sv;
|
|
|
|
auto constexpr ExpectedOut = "lld1:ai64e1:bi32eeee"sv;
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
tr_variant val;
|
2022-11-14 20:16:29 +00:00
|
|
|
char const* end = nullptr;
|
2021-11-18 05:37:35 +00:00
|
|
|
auto const ok = tr_variantFromBuf(&val, TR_VARIANT_PARSE_BENC | TR_VARIANT_PARSE_INPLACE, In, &end);
|
|
|
|
EXPECT_TRUE(ok);
|
2021-11-15 07:21:57 +00:00
|
|
|
EXPECT_EQ(std::data(In) + std::size(In), end);
|
2021-12-27 22:47:25 +00:00
|
|
|
EXPECT_EQ(ExpectedOut, tr_variantToStr(&val, TR_VARIANT_FMT_BENC));
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2022-08-18 01:25:42 +00:00
|
|
|
tr_variantClear(&val);
|
2020-08-11 18:11:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(VariantTest, bencMalformedTooManyEndings)
|
|
|
|
{
|
2021-11-15 07:21:57 +00:00
|
|
|
auto constexpr In = "leee"sv;
|
|
|
|
auto constexpr ExpectedOut = "le"sv;
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
tr_variant val;
|
2022-11-14 20:16:29 +00:00
|
|
|
char const* end = nullptr;
|
2021-11-18 05:37:35 +00:00
|
|
|
auto const ok = tr_variantFromBuf(&val, TR_VARIANT_PARSE_BENC | TR_VARIANT_PARSE_INPLACE, In, &end);
|
|
|
|
EXPECT_TRUE(ok);
|
2021-11-15 07:21:57 +00:00
|
|
|
EXPECT_EQ(std::data(In) + std::size(ExpectedOut), end);
|
2021-12-27 22:47:25 +00:00
|
|
|
EXPECT_EQ(ExpectedOut, tr_variantToStr(&val, TR_VARIANT_FMT_BENC));
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2022-08-18 01:25:42 +00:00
|
|
|
tr_variantClear(&val);
|
2020-08-11 18:11:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(VariantTest, bencMalformedNoEnding)
|
|
|
|
{
|
2021-11-15 07:21:57 +00:00
|
|
|
auto constexpr In = "l1:a1:b1:c"sv;
|
2020-08-11 18:11:55 +00:00
|
|
|
tr_variant val;
|
2021-11-18 05:37:35 +00:00
|
|
|
EXPECT_FALSE(tr_variantFromBuf(&val, TR_VARIANT_PARSE_BENC | TR_VARIANT_PARSE_INPLACE, In));
|
2020-08-11 18:11:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(VariantTest, bencMalformedIncompleteString)
|
|
|
|
{
|
2021-11-15 07:21:57 +00:00
|
|
|
auto constexpr In = "1:"sv;
|
2020-08-11 18:11:55 +00:00
|
|
|
tr_variant val;
|
2021-11-18 05:37:35 +00:00
|
|
|
EXPECT_FALSE(tr_variantFromBuf(&val, TR_VARIANT_PARSE_BENC | TR_VARIANT_PARSE_INPLACE, In));
|
2020-08-11 18:11:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(VariantTest, bencToJson)
|
|
|
|
{
|
2020-09-07 21:19:10 +00:00
|
|
|
struct LocalTest
|
2020-08-11 18:11:55 +00:00
|
|
|
{
|
2021-11-15 07:21:57 +00:00
|
|
|
std::string_view benc;
|
|
|
|
std::string_view expected;
|
2020-08-11 18:11:55 +00:00
|
|
|
};
|
|
|
|
|
2021-11-15 07:21:57 +00:00
|
|
|
auto constexpr Tests = std::array<LocalTest, 5>{
|
|
|
|
{ { "i6e"sv, "6"sv },
|
|
|
|
{ "d5:helloi1e5:worldi2ee"sv, R"({"hello":1,"world":2})"sv },
|
|
|
|
{ "d5:helloi1e5:worldi2e3:fooli1ei2ei3eee"sv, R"({"foo":[1,2,3],"hello":1,"world":2})"sv },
|
|
|
|
{ "d5:helloi1e5:worldi2e3:fooli1ei2ei3ed1:ai0eeee"sv, R"({"foo":[1,2,3,{"a":0}],"hello":1,"world":2})"sv },
|
|
|
|
{ "d4:argsd6:statusle7:status2lee6:result7:successe"sv,
|
|
|
|
R"({"args":{"status":[],"status2":[]},"result":"success"})"sv } }
|
2020-08-11 18:11:55 +00:00
|
|
|
};
|
|
|
|
|
2021-11-15 07:21:57 +00:00
|
|
|
for (auto const& test : Tests)
|
2020-08-11 18:11:55 +00:00
|
|
|
{
|
|
|
|
tr_variant top;
|
2021-11-18 05:37:35 +00:00
|
|
|
tr_variantFromBuf(&top, TR_VARIANT_PARSE_BENC | TR_VARIANT_PARSE_INPLACE, test.benc);
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2021-12-27 22:47:25 +00:00
|
|
|
auto const str = tr_variantToStr(&top, TR_VARIANT_FMT_JSON_LEAN);
|
|
|
|
EXPECT_EQ(test.expected, stripWhitespace(str));
|
2022-08-18 01:25:42 +00:00
|
|
|
tr_variantClear(&top);
|
2020-08-11 18:11:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(VariantTest, merge)
|
|
|
|
{
|
2021-10-15 13:28:47 +00:00
|
|
|
auto const i1 = tr_quark_new("i1"sv);
|
|
|
|
auto const i2 = tr_quark_new("i2"sv);
|
|
|
|
auto const i3 = tr_quark_new("i3"sv);
|
|
|
|
auto const i4 = tr_quark_new("i4"sv);
|
|
|
|
auto const s5 = tr_quark_new("s5"sv);
|
|
|
|
auto const s6 = tr_quark_new("s6"sv);
|
|
|
|
auto const s7 = tr_quark_new("s7"sv);
|
|
|
|
auto const s8 = tr_quark_new("s8"sv);
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
/* initial dictionary (default values) */
|
|
|
|
tr_variant dest;
|
|
|
|
tr_variantInitDict(&dest, 10);
|
|
|
|
tr_variantDictAddInt(&dest, i1, 1);
|
|
|
|
tr_variantDictAddInt(&dest, i2, 2);
|
|
|
|
tr_variantDictAddInt(&dest, i4, -35); /* remains untouched */
|
2021-11-18 05:37:35 +00:00
|
|
|
tr_variantDictAddStrView(&dest, s5, "abc");
|
|
|
|
tr_variantDictAddStrView(&dest, s6, "def");
|
|
|
|
tr_variantDictAddStrView(&dest, s7, "127.0.0.1"); /* remains untouched */
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
/* new dictionary, will overwrite items in dest */
|
|
|
|
tr_variant src;
|
|
|
|
tr_variantInitDict(&src, 10);
|
|
|
|
tr_variantDictAddInt(&src, i1, 1); /* same value */
|
|
|
|
tr_variantDictAddInt(&src, i2, 4); /* new value */
|
|
|
|
tr_variantDictAddInt(&src, i3, 3); /* new key:value */
|
2021-11-18 05:37:35 +00:00
|
|
|
tr_variantDictAddStrView(&src, s5, "abc"); /* same value */
|
|
|
|
tr_variantDictAddStrView(&src, s6, "xyz"); /* new value */
|
|
|
|
tr_variantDictAddStrView(&src, s8, "ghi"); /* new key:value */
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
tr_variantMergeDicts(&dest, /*const*/ &src);
|
|
|
|
|
2022-11-14 20:16:29 +00:00
|
|
|
auto i = int64_t{};
|
2020-08-11 18:11:55 +00:00
|
|
|
EXPECT_TRUE(tr_variantDictFindInt(&dest, i1, &i));
|
|
|
|
EXPECT_EQ(1, i);
|
|
|
|
EXPECT_TRUE(tr_variantDictFindInt(&dest, i2, &i));
|
|
|
|
EXPECT_EQ(4, i);
|
|
|
|
EXPECT_TRUE(tr_variantDictFindInt(&dest, i3, &i));
|
|
|
|
EXPECT_EQ(3, i);
|
|
|
|
EXPECT_TRUE(tr_variantDictFindInt(&dest, i4, &i));
|
|
|
|
EXPECT_EQ(-35, i);
|
2021-11-12 18:04:55 +00:00
|
|
|
auto sv = std::string_view{};
|
|
|
|
EXPECT_TRUE(tr_variantDictFindStrView(&dest, s5, &sv));
|
|
|
|
EXPECT_EQ("abc"sv, sv);
|
|
|
|
EXPECT_TRUE(tr_variantDictFindStrView(&dest, s6, &sv));
|
|
|
|
EXPECT_EQ("xyz"sv, sv);
|
|
|
|
EXPECT_TRUE(tr_variantDictFindStrView(&dest, s7, &sv));
|
|
|
|
EXPECT_EQ("127.0.0.1"sv, sv);
|
|
|
|
EXPECT_TRUE(tr_variantDictFindStrView(&dest, s8, &sv));
|
|
|
|
EXPECT_EQ("ghi"sv, sv);
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2022-08-18 01:25:42 +00:00
|
|
|
tr_variantClear(&dest);
|
|
|
|
tr_variantClear(&src);
|
2020-08-11 18:11:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(VariantTest, stackSmash)
|
|
|
|
{
|
|
|
|
// make a nested list of list of lists.
|
|
|
|
int constexpr Depth = STACK_SMASH_DEPTH;
|
|
|
|
std::string const in = std::string(Depth, 'l') + std::string(Depth, 'e');
|
|
|
|
|
2022-01-24 06:30:00 +00:00
|
|
|
// confirm that it fails instead of crashing
|
2022-11-14 20:16:29 +00:00
|
|
|
char const* end = nullptr;
|
2020-08-11 18:11:55 +00:00
|
|
|
tr_variant val;
|
2022-01-24 06:30:00 +00:00
|
|
|
tr_error* error = nullptr;
|
|
|
|
auto ok = tr_variantFromBuf(&val, TR_VARIANT_PARSE_BENC | TR_VARIANT_PARSE_INPLACE, in, &end, &error);
|
|
|
|
EXPECT_NE(nullptr, error);
|
|
|
|
EXPECT_EQ(E2BIG, error->code);
|
|
|
|
EXPECT_FALSE(ok);
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2022-01-24 06:30:00 +00:00
|
|
|
tr_error_clear(&error);
|
2020-08-11 18:11:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(VariantTest, boolAndIntRecast)
|
|
|
|
{
|
2021-10-15 13:28:47 +00:00
|
|
|
auto const key1 = tr_quark_new("key1"sv);
|
|
|
|
auto const key2 = tr_quark_new("key2"sv);
|
|
|
|
auto const key3 = tr_quark_new("key3"sv);
|
|
|
|
auto const key4 = tr_quark_new("key4"sv);
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2022-11-14 20:16:29 +00:00
|
|
|
auto top = tr_variant{};
|
2020-08-11 18:11:55 +00:00
|
|
|
tr_variantInitDict(&top, 10);
|
|
|
|
tr_variantDictAddBool(&top, key1, false);
|
|
|
|
tr_variantDictAddBool(&top, key2, 0); // NOLINT modernize-use-bool-literals
|
2022-01-24 19:07:55 +00:00
|
|
|
tr_variantDictAddInt(&top, key3, true); // NOLINT readability-implicit-bool-conversion
|
2020-08-11 18:11:55 +00:00
|
|
|
tr_variantDictAddInt(&top, key4, 1);
|
|
|
|
|
|
|
|
// confirm we can read both bools and ints as bools
|
2022-11-14 20:16:29 +00:00
|
|
|
auto b = bool{};
|
2020-08-11 18:11:55 +00:00
|
|
|
EXPECT_TRUE(tr_variantDictFindBool(&top, key1, &b));
|
|
|
|
EXPECT_FALSE(b);
|
|
|
|
EXPECT_TRUE(tr_variantDictFindBool(&top, key2, &b));
|
|
|
|
EXPECT_FALSE(b);
|
|
|
|
EXPECT_TRUE(tr_variantDictFindBool(&top, key3, &b));
|
|
|
|
EXPECT_TRUE(b);
|
|
|
|
EXPECT_TRUE(tr_variantDictFindBool(&top, key4, &b));
|
|
|
|
EXPECT_TRUE(b);
|
|
|
|
|
|
|
|
// confirm we can read both bools and ints as ints
|
2022-11-14 20:16:29 +00:00
|
|
|
auto i = int64_t{};
|
2020-08-11 18:11:55 +00:00
|
|
|
EXPECT_TRUE(tr_variantDictFindInt(&top, key1, &i));
|
|
|
|
EXPECT_EQ(0, i);
|
|
|
|
EXPECT_TRUE(tr_variantDictFindInt(&top, key2, &i));
|
|
|
|
EXPECT_EQ(0, i);
|
|
|
|
EXPECT_TRUE(tr_variantDictFindInt(&top, key3, &i));
|
|
|
|
EXPECT_NE(0, i);
|
|
|
|
EXPECT_TRUE(tr_variantDictFindInt(&top, key4, &i));
|
|
|
|
EXPECT_NE(0, i);
|
|
|
|
|
2022-08-18 01:25:42 +00:00
|
|
|
tr_variantClear(&top);
|
2020-08-11 18:11:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(VariantTest, dictFindType)
|
|
|
|
{
|
2021-11-01 21:30:18 +00:00
|
|
|
auto constexpr ExpectedStr = "this-is-a-string"sv;
|
|
|
|
auto constexpr ExpectedBool = bool{ true };
|
|
|
|
auto constexpr ExpectedInt = int{ 1234 };
|
|
|
|
auto constexpr ExpectedReal = double{ 0.3 };
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2021-10-15 13:28:47 +00:00
|
|
|
auto const key_bool = tr_quark_new("this-is-a-bool"sv);
|
|
|
|
auto const key_real = tr_quark_new("this-is-a-real"sv);
|
|
|
|
auto const key_int = tr_quark_new("this-is-an-int"sv);
|
|
|
|
auto const key_str = tr_quark_new("this-is-a-string"sv);
|
2021-11-01 21:30:18 +00:00
|
|
|
auto const key_unknown = tr_quark_new("this-is-a-missing-entry"sv);
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
// populate a dict
|
|
|
|
tr_variant top;
|
|
|
|
tr_variantInitDict(&top, 0);
|
2021-11-01 21:30:18 +00:00
|
|
|
tr_variantDictAddBool(&top, key_bool, ExpectedBool);
|
|
|
|
tr_variantDictAddInt(&top, key_int, ExpectedInt);
|
|
|
|
tr_variantDictAddReal(&top, key_real, ExpectedReal);
|
|
|
|
tr_variantDictAddStr(&top, key_str, ExpectedStr.data());
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
// look up the keys as strings
|
2021-11-01 21:30:18 +00:00
|
|
|
auto sv = std::string_view{};
|
2021-11-12 18:04:55 +00:00
|
|
|
EXPECT_FALSE(tr_variantDictFindStrView(&top, key_bool, &sv));
|
|
|
|
EXPECT_FALSE(tr_variantDictFindStrView(&top, key_real, &sv));
|
|
|
|
EXPECT_FALSE(tr_variantDictFindStrView(&top, key_int, &sv));
|
|
|
|
EXPECT_TRUE(tr_variantDictFindStrView(&top, key_str, &sv));
|
|
|
|
EXPECT_EQ(ExpectedStr, sv);
|
2021-11-01 21:30:18 +00:00
|
|
|
EXPECT_TRUE(tr_variantDictFindStrView(&top, key_str, &sv));
|
|
|
|
EXPECT_EQ(ExpectedStr, sv);
|
|
|
|
EXPECT_FALSE(tr_variantDictFindStrView(&top, key_unknown, &sv));
|
2021-11-12 18:04:55 +00:00
|
|
|
EXPECT_FALSE(tr_variantDictFindStrView(&top, key_unknown, &sv));
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
// look up the keys as bools
|
|
|
|
auto b = bool{};
|
|
|
|
EXPECT_FALSE(tr_variantDictFindBool(&top, key_int, &b));
|
|
|
|
EXPECT_FALSE(tr_variantDictFindBool(&top, key_real, &b));
|
|
|
|
EXPECT_FALSE(tr_variantDictFindBool(&top, key_str, &b));
|
|
|
|
EXPECT_TRUE(tr_variantDictFindBool(&top, key_bool, &b));
|
2021-11-01 21:30:18 +00:00
|
|
|
EXPECT_EQ(ExpectedBool, b);
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
// look up the keys as doubles
|
|
|
|
auto d = double{};
|
|
|
|
EXPECT_FALSE(tr_variantDictFindReal(&top, key_bool, &d));
|
|
|
|
EXPECT_TRUE(tr_variantDictFindReal(&top, key_int, &d));
|
2021-11-01 21:30:18 +00:00
|
|
|
EXPECT_EQ(ExpectedInt, std::lrint(d));
|
2020-08-11 18:11:55 +00:00
|
|
|
EXPECT_FALSE(tr_variantDictFindReal(&top, key_str, &d));
|
|
|
|
EXPECT_TRUE(tr_variantDictFindReal(&top, key_real, &d));
|
2021-11-01 21:30:18 +00:00
|
|
|
EXPECT_EQ(std::lrint(ExpectedReal * 100), std::lrint(d * 100));
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
// look up the keys as ints
|
2021-08-15 09:41:48 +00:00
|
|
|
auto i = int64_t{};
|
2020-08-11 18:11:55 +00:00
|
|
|
EXPECT_TRUE(tr_variantDictFindInt(&top, key_bool, &i));
|
2021-11-01 21:30:18 +00:00
|
|
|
EXPECT_EQ(ExpectedBool ? 1 : 0, i);
|
2020-08-11 18:11:55 +00:00
|
|
|
EXPECT_FALSE(tr_variantDictFindInt(&top, key_real, &i));
|
|
|
|
EXPECT_FALSE(tr_variantDictFindInt(&top, key_str, &i));
|
|
|
|
EXPECT_TRUE(tr_variantDictFindInt(&top, key_int, &i));
|
2021-11-01 21:30:18 +00:00
|
|
|
EXPECT_EQ(ExpectedInt, i);
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2022-08-18 01:25:42 +00:00
|
|
|
tr_variantClear(&top);
|
2020-08-11 18:11:55 +00:00
|
|
|
}
|
2022-04-06 21:39:39 +00:00
|
|
|
|
|
|
|
TEST_F(VariantTest, variantFromBufFuzz)
|
|
|
|
{
|
|
|
|
auto buf = std::vector<char>{};
|
|
|
|
|
|
|
|
for (size_t i = 0; i < 100000; ++i)
|
|
|
|
{
|
2022-12-21 15:58:32 +00:00
|
|
|
buf.resize(tr_rand_int(4096U));
|
2022-04-06 21:39:39 +00:00
|
|
|
tr_rand_buffer(std::data(buf), std::size(buf));
|
|
|
|
// std::cerr << '[' << tr_base64_encode({ std::data(buf), std::size(buf) }) << ']' << std::endl;
|
|
|
|
|
2022-04-15 23:39:04 +00:00
|
|
|
if (auto top = tr_variant{};
|
2022-08-05 21:12:45 +00:00
|
|
|
tr_variantFromBuf(&top, TR_VARIANT_PARSE_JSON | TR_VARIANT_PARSE_INPLACE, buf, nullptr, nullptr))
|
2022-04-15 23:39:04 +00:00
|
|
|
{
|
2022-08-18 01:25:42 +00:00
|
|
|
tr_variantClear(&top);
|
2022-04-15 23:39:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (auto top = tr_variant{};
|
2022-08-05 21:12:45 +00:00
|
|
|
tr_variantFromBuf(&top, TR_VARIANT_PARSE_BENC | TR_VARIANT_PARSE_INPLACE, buf, nullptr, nullptr))
|
2022-04-15 23:39:04 +00:00
|
|
|
{
|
2022-08-18 01:25:42 +00:00
|
|
|
tr_variantClear(&top);
|
2022-04-15 23:39:04 +00:00
|
|
|
}
|
2022-04-06 21:39:39 +00:00
|
|
|
}
|
|
|
|
}
|