lokinet/test/util/test_llarp_util_bencode.cpp

378 lines
9.0 KiB
C++
Raw Normal View History

#include <util/bencode.h>
#include <util/bencode.hpp>
2019-01-10 17:43:29 +00:00
#include <iostream>
#include <string>
#include <utility>
#include <vector>
2021-03-01 21:07:32 +00:00
#include <catch2/catch.hpp>
2019-01-10 17:43:29 +00:00
2021-03-01 21:07:32 +00:00
using TestBuffer = std::vector<byte_t>;
2019-01-10 17:43:29 +00:00
2021-03-01 21:07:32 +00:00
template <typename Result>
2019-01-10 17:43:29 +00:00
struct TestReadData
{
TestBuffer buffer;
bool rc;
Result result;
};
2021-03-01 21:07:32 +00:00
using TestReadInt = TestReadData<uint64_t>;
using TestReadString = TestReadData<std::string>;
2019-01-10 17:43:29 +00:00
2021-03-01 21:07:32 +00:00
template <typename Result>
2019-01-10 17:43:29 +00:00
std::ostream&
2021-03-01 21:07:32 +00:00
operator<<(std::ostream& os, const TestReadData<Result>& d)
2019-01-10 17:43:29 +00:00
{
os << "buf = [ ";
2021-03-01 21:07:32 +00:00
for (auto x : d.buffer)
2019-01-10 17:43:29 +00:00
{
os << x << " ";
}
os << "] rc = ";
os << std::boolalpha << d.rc << " result = " << d.result;
return os;
}
2021-03-01 21:07:32 +00:00
static constexpr byte_t i = 'i';
static constexpr byte_t e = 'e';
static constexpr byte_t zero = '0';
static constexpr byte_t one = '1';
static constexpr byte_t two = '2';
static constexpr byte_t f = 'f';
static constexpr byte_t z = 'z';
2019-01-10 17:43:29 +00:00
static constexpr byte_t colon = ':';
2021-03-01 21:07:32 +00:00
std::vector<TestReadInt> testReadInt{
2019-01-10 17:43:29 +00:00
// good cases
{{i, 0, e}, true, 0},
{{i, zero, e}, true, 0},
{{i, one, e}, true, 1},
{{i, two, e}, true, 2},
{{i, two, e, e, e}, true, 2},
{{i, one, one, one, two, e}, true, 1112},
{{i, f, e}, true, 0},
{{i, z, e}, true, 0},
{{i, one, two, e, one, one}, true, 12},
// failure cases
{{i, e}, false, 0},
{{e}, false, 0},
{{z}, false, 0},
};
2021-03-01 21:07:32 +00:00
TEST_CASE("Read int", "[bencode]")
2019-01-10 17:43:29 +00:00
{
2021-03-01 21:07:32 +00:00
auto d = GENERATE(from_range(testReadInt));
2019-01-10 17:43:29 +00:00
2019-02-02 23:12:42 +00:00
llarp_buffer_t buffer(d.buffer);
2019-01-10 17:43:29 +00:00
2021-03-01 21:07:32 +00:00
uint64_t result = 0;
bool rc = bencode_read_integer(&buffer, &result);
2019-01-10 17:43:29 +00:00
2021-03-01 21:07:32 +00:00
CHECK(rc == d.rc);
CHECK(result == d.result);
2019-01-10 17:43:29 +00:00
}
2021-03-01 21:07:32 +00:00
std::vector<TestReadString> testReadStr{
2019-01-10 17:43:29 +00:00
// good cases
{{one, colon, 'a'}, true, "a"},
{{one, colon, 'b'}, true, "b"},
{{two, colon, f, z}, true, "fz"},
{{two, colon, f, z, f, f}, true, "fz"},
{{zero, colon}, true, ""},
// failure cases
{{two, colon, f}, false, ""},
{{two, f}, false, ""},
{{'-', one, colon, f}, false, ""},
{{f}, false, ""},
{{one, f, colon}, false, ""},
{{colon}, false, ""},
{{colon, colon}, false, ""},
};
2021-03-01 21:07:32 +00:00
TEST_CASE("Read str", "[bencode]")
{
auto d = GENERATE(from_range(testReadStr));
llarp_buffer_t buffer(d.buffer);
llarp_buffer_t result;
bool rc = bencode_read_string(&buffer, &result);
2019-01-10 17:43:29 +00:00
2021-03-01 21:07:32 +00:00
CHECK(rc == d.rc);
CHECK(result.sz == d.result.size());
CHECK(std::string(result.base, result.base + result.sz) == d.result);
}
template <typename Input>
2019-01-10 17:43:29 +00:00
struct TestWriteData
{
Input input;
size_t bufferSize;
bool rc;
std::string output;
};
2021-03-01 21:07:32 +00:00
using TestWriteByteString = TestWriteData<std::string>;
using TestWriteInt = TestWriteData<uint64_t>;
2019-01-10 17:43:29 +00:00
2021-03-01 21:07:32 +00:00
static constexpr size_t MAX_1 = static_cast<size_t>(std::numeric_limits<int16_t>::max()) + 1;
2019-01-10 17:43:29 +00:00
2021-03-01 21:07:32 +00:00
std::vector<TestWriteByteString> testWriteByteString{
2019-01-10 17:43:29 +00:00
// good cases
{"abacus", 100, true, "6:abacus"},
{" abacus", 100, true, "8: abacus"},
{"", 100, true, "0:"},
{std::string("\0\0\0", 3), 100, true, std::string("3:\0\0\0", 5)},
2021-03-01 21:07:32 +00:00
{std::string(MAX_1, 'a'),
MAX_1 + 100,
true,
2019-01-10 17:43:29 +00:00
std::to_string(MAX_1) + std::string(":") + std::string(MAX_1, 'a')},
// bad cases
{"a", 1, false, ""},
};
2021-03-01 21:07:32 +00:00
TEST_CASE("Write byte str", "[bencode]")
2019-01-10 17:43:29 +00:00
{
2021-03-01 21:07:32 +00:00
auto d = GENERATE(from_range(testWriteByteString));
2019-01-10 17:43:29 +00:00
2021-03-01 21:07:32 +00:00
std::vector<byte_t> backingBuffer(d.bufferSize, 0);
2019-02-02 23:12:42 +00:00
llarp_buffer_t buffer(backingBuffer);
2019-01-10 17:43:29 +00:00
2021-03-01 21:07:32 +00:00
bool rc = bencode_write_bytestring(&buffer, d.input.data(), d.input.size());
2019-01-10 17:43:29 +00:00
2021-03-01 21:07:32 +00:00
REQUIRE(rc == d.rc);
REQUIRE(std::string(buffer.base, buffer.cur) == d.output);
2019-01-10 17:43:29 +00:00
}
2021-03-01 21:07:32 +00:00
std::vector<TestWriteInt> testWriteInt{
2019-01-10 17:43:29 +00:00
// Good cases
{0, 100, true, "i0e"},
{1234, 100, true, "i1234e"},
{uint64_t(-1), 100, true, "i18446744073709551615e"},
// Bad cases
{1234567, 3, false, ""},
};
2021-03-01 21:07:32 +00:00
TEST_CASE("Write int", "[bencode]")
2019-01-10 17:43:29 +00:00
{
2021-03-01 21:07:32 +00:00
auto d = GENERATE(from_range(testWriteInt));
std::vector<byte_t> backingBuffer(d.bufferSize, 0);
llarp_buffer_t buffer(backingBuffer);
2019-01-10 17:43:29 +00:00
2021-03-01 21:07:32 +00:00
bool rc = bencode_write_uint64(&buffer, d.input);
REQUIRE(rc == d.rc);
REQUIRE(std::string(buffer.base, buffer.cur) == d.output);
}
TEST_CASE("Write int values", "[bencode]")
2019-01-10 17:43:29 +00:00
{
// test we can encode any uint64_t into a buffer.
2021-03-01 21:07:32 +00:00
uint64_t val = GENERATE(
std::numeric_limits<uint64_t>::min(),
std::numeric_limits<uint64_t>::max(),
std::numeric_limits<uint64_t>::max() / 2,
std::numeric_limits<uint64_t>::max() / 3);
2019-01-10 17:43:29 +00:00
2021-03-01 21:07:32 +00:00
std::vector<byte_t> backingBuffer(100, 0);
2019-01-10 17:43:29 +00:00
{
2019-02-02 23:12:42 +00:00
llarp_buffer_t buffer(backingBuffer);
2019-01-10 17:43:29 +00:00
bool rc = bencode_write_uint64(&buffer, val);
2021-03-01 21:07:32 +00:00
REQUIRE(rc);
}
{
2019-02-02 23:12:42 +00:00
uint64_t result = 0;
llarp_buffer_t buffer(backingBuffer);
bool rc = bencode_read_integer(&buffer, &result);
2021-03-01 21:07:32 +00:00
REQUIRE(rc);
REQUIRE(result == val);
}
2019-01-10 17:43:29 +00:00
}
2021-03-01 21:07:32 +00:00
TEST_CASE("Bencode: good uint64 entry", "[bencode]")
2019-01-10 17:43:29 +00:00
{
2021-03-01 21:07:32 +00:00
std::vector<byte_t> backingBuffer(100, 0);
2019-02-02 23:12:42 +00:00
llarp_buffer_t buffer(backingBuffer);
2019-01-10 17:43:29 +00:00
2021-03-01 21:07:32 +00:00
REQUIRE(bencode_write_uint64_entry(&buffer, "v", 1, 0));
2019-01-10 17:43:29 +00:00
2021-03-01 21:07:32 +00:00
REQUIRE(std::string(buffer.base, buffer.cur) == "1:vi0e");
}
2019-01-10 17:43:29 +00:00
2021-03-01 21:07:32 +00:00
TEST_CASE("Bencode: bad uint64 entry", "[bencode]")
{
2021-03-01 21:07:32 +00:00
std::vector<byte_t> otherBuffer(1, 0);
2019-02-02 23:12:42 +00:00
llarp_buffer_t buffer(otherBuffer);
2019-01-10 17:43:29 +00:00
2021-03-01 21:07:32 +00:00
REQUIRE_FALSE(bencode_write_uint64_entry(&buffer, "v", 1, 0));
2019-01-10 17:43:29 +00:00
}
struct ValueData
{
// Variant-ish
std::string theString;
uint64_t theInt;
bool isString;
};
struct ListTestData
{
2021-03-01 21:07:32 +00:00
std::vector<ValueData> list;
2019-01-10 17:43:29 +00:00
size_t bufferSize;
std::string result;
};
2021-03-01 21:07:32 +00:00
std::vector<ListTestData> listTestData{
{{}, 100, "le"},
{{{"", 0, true}}, 100, "l0:e"},
{{{"", 0, false}}, 100, "li0ee"},
{{{"", 0, false}, {"", 0, true}}, 100, "li0e0:e"},
{{{"", 123, false}, {"abc", 0, true}}, 100, "li123e3:abce"},
{{{"", 123, false}, {"abc", 0, true}, {"abc", 0, true}}, 100, "li123e3:abc3:abce"},
2019-01-10 17:43:29 +00:00
};
2021-03-01 21:07:32 +00:00
TEST_CASE("List test", "[bencode]")
2019-01-10 17:43:29 +00:00
{
2021-03-01 21:07:32 +00:00
auto d = GENERATE(from_range(listTestData));
2019-01-10 17:43:29 +00:00
2021-03-01 21:07:32 +00:00
std::vector<byte_t> backingBuffer(d.bufferSize, 0);
2019-02-02 23:12:42 +00:00
llarp_buffer_t buffer(backingBuffer);
2019-01-10 17:43:29 +00:00
2021-03-01 21:07:32 +00:00
REQUIRE(bencode_start_list(&buffer));
2019-01-10 17:43:29 +00:00
2021-03-01 21:07:32 +00:00
for (const auto& x : d.list)
2019-01-10 17:43:29 +00:00
{
2021-03-01 21:07:32 +00:00
if (x.isString)
2019-01-10 17:43:29 +00:00
{
2021-03-01 21:07:32 +00:00
REQUIRE(bencode_write_bytestring(&buffer, x.theString.data(), x.theString.size()));
2019-01-10 17:43:29 +00:00
}
else
{
2021-03-01 21:07:32 +00:00
REQUIRE(bencode_write_uint64(&buffer, x.theInt));
2019-01-10 17:43:29 +00:00
}
}
2021-03-01 21:07:32 +00:00
REQUIRE(bencode_end(&buffer));
2019-01-10 17:43:29 +00:00
2021-03-01 21:07:32 +00:00
REQUIRE(std::string(buffer.base, buffer.cur) == d.result);
2019-01-10 17:43:29 +00:00
}
struct DictTestData
{
2021-03-01 21:07:32 +00:00
std::vector<std::pair<char, ValueData>> list;
2019-01-10 17:43:29 +00:00
size_t bufferSize;
std::string result;
};
2021-03-01 21:07:32 +00:00
std::vector<DictTestData> dictTestData{
{{}, 100, "de"},
{{{'a', {"", 0, true}}}, 100, "d1:a0:e"},
{{{'b', {"", 0, false}}}, 100, "d1:bi0ee"},
{{{'c', {"", 0, false}}, {'d', {"", 0, true}}}, 100, "d1:ci0e1:d0:e"},
{{{'e', {"", 123, false}}, {'f', {"abc", 0, true}}}, 100, "d1:ei123e1:f3:abce"},
{{{'a', {"", 123, false}}, {'b', {"abc", 0, true}}, {'c', {"abc", 0, true}}},
100,
"d1:ai123e1:b3:abc1:c3:abce"},
2019-01-10 17:43:29 +00:00
};
2021-03-01 21:07:32 +00:00
TEST_CASE("Dict test", "[bencode]")
2019-01-10 17:43:29 +00:00
{
2021-03-01 21:07:32 +00:00
auto d = GENERATE(from_range(dictTestData));
2019-01-10 17:43:29 +00:00
2021-03-01 21:07:32 +00:00
std::vector<byte_t> backingBuffer(d.bufferSize, 0);
2019-02-02 23:12:42 +00:00
llarp_buffer_t buffer(backingBuffer);
2019-01-10 17:43:29 +00:00
2021-03-01 21:07:32 +00:00
REQUIRE(bencode_start_dict(&buffer));
2019-01-10 17:43:29 +00:00
2021-03-01 21:07:32 +00:00
for (const auto& x : d.list)
2019-01-10 17:43:29 +00:00
{
2021-03-01 21:07:32 +00:00
REQUIRE(bencode_write_bytestring(&buffer, &x.first, 1));
if (x.second.isString)
2019-01-10 17:43:29 +00:00
{
2021-03-01 21:07:32 +00:00
REQUIRE(
bencode_write_bytestring(&buffer, x.second.theString.data(), x.second.theString.size()));
2019-01-10 17:43:29 +00:00
}
else
{
2021-03-01 21:07:32 +00:00
REQUIRE(bencode_write_uint64(&buffer, x.second.theInt));
2019-01-10 17:43:29 +00:00
}
}
2021-03-01 21:07:32 +00:00
REQUIRE(bencode_end(&buffer));
2019-01-10 17:43:29 +00:00
2021-03-01 21:07:32 +00:00
REQUIRE(std::string(buffer.base, buffer.cur) == d.result);
2019-01-10 17:43:29 +00:00
}
struct ReadData
{
std::string input;
2021-03-01 21:07:32 +00:00
std::vector<std::string> output;
2019-01-10 17:43:29 +00:00
};
2021-03-01 21:07:32 +00:00
std::vector<ReadData> dictReadData{
{"de", {}}, {"d1:a0:e", {"a", ""}}, {"d1:be", {"b"}}, {"d1:b2:23e", {"b", "23"}}};
2019-01-10 17:43:29 +00:00
2021-03-01 21:07:32 +00:00
TEST_CASE("Read dict", "[bencode]")
2019-01-10 17:43:29 +00:00
{
2021-03-01 21:07:32 +00:00
auto d = GENERATE(from_range(dictReadData));
2019-01-10 17:43:29 +00:00
2021-03-01 21:07:32 +00:00
byte_t* input = const_cast<byte_t*>(reinterpret_cast<const byte_t*>(d.input.data()));
2019-01-10 17:43:29 +00:00
llarp_buffer_t buffer(input, input, d.input.size());
2021-03-01 21:07:32 +00:00
std::vector<std::string> result;
2019-01-10 17:43:29 +00:00
2021-03-01 21:07:32 +00:00
REQUIRE(llarp::bencode_read_dict(
[&](llarp_buffer_t*, llarp_buffer_t* key) {
2021-03-01 21:07:32 +00:00
if (key)
{
result.emplace_back(key->base, key->base + key->sz);
}
return true;
},
&buffer));
2021-03-01 21:07:32 +00:00
REQUIRE(result == d.output);
2019-01-10 17:43:29 +00:00
}
2021-03-01 21:07:32 +00:00
std::vector<ReadData> listReadData{
{"le", {}}, {"l1:ae", {"a"}}, {"l1:be", {"b"}}, {"l1:b2:23e", {"b", "23"}}};
2019-01-10 17:43:29 +00:00
2021-03-01 21:07:32 +00:00
TEST_CASE("Read list", "[bencode]")
2019-01-10 17:43:29 +00:00
{
2021-03-01 21:07:32 +00:00
auto d = GENERATE(from_range(listReadData));
2019-01-10 17:43:29 +00:00
2021-03-01 21:07:32 +00:00
byte_t* input = const_cast<byte_t*>(reinterpret_cast<const byte_t*>(d.input.data()));
2019-01-10 17:43:29 +00:00
llarp_buffer_t buffer(input, input, d.input.size());
2021-03-01 21:07:32 +00:00
std::vector<std::string> result;
2019-01-10 17:43:29 +00:00
2021-03-01 21:07:32 +00:00
REQUIRE(llarp::bencode_read_list(
[&](llarp_buffer_t* b, bool cont) {
2021-03-01 21:07:32 +00:00
if (cont)
{
llarp_buffer_t tmp;
bencode_read_string(b, &tmp);
result.emplace_back(tmp.base, tmp.base + tmp.sz);
}
return true;
},
&buffer));
2021-03-01 21:07:32 +00:00
REQUIRE(result == d.output);
2019-01-10 17:43:29 +00:00
}
2021-03-01 21:07:32 +00:00
TEST_CASE("Read dict to empty buffer", "[bencode]")
2019-03-28 19:15:20 +00:00
{
llarp_buffer_t buf((byte_t*)nullptr, 0);
2021-03-01 21:07:32 +00:00
REQUIRE_FALSE(
llarp::bencode_read_dict([](llarp_buffer_t*, llarp_buffer_t*) { return true; }, &buf));
2019-04-25 23:21:19 +00:00
}