lokinet/test/router/test_llarp_router_version.cpp

88 lines
2.0 KiB
C++
Raw Normal View History

#include <router_version.hpp>
#include "router/router.hpp"
2021-03-01 21:07:32 +00:00
#include <catch2/catch.hpp>
using Catch::Matchers::Equals;
2021-03-01 21:07:32 +00:00
TEST_CASE("Compatibility when protocol equal", "[RouterVersion]")
{
2021-03-01 21:07:32 +00:00
llarp::RouterVersion v1({0, 1, 2}, 1);
llarp::RouterVersion v2({0, 1, 2}, 1);
2021-03-01 21:07:32 +00:00
CHECK(v1.IsCompatableWith(v2));
}
2021-03-01 21:07:32 +00:00
TEST_CASE("Compatibility when protocol unequal", "[RouterVersion]")
{
2021-03-01 21:07:32 +00:00
llarp::RouterVersion older({0, 1, 2}, 1);
llarp::RouterVersion newer({0, 1, 2}, 2);
2021-03-01 21:07:32 +00:00
CHECK_FALSE(older.IsCompatableWith(newer));
CHECK_FALSE(newer.IsCompatableWith(older));
}
2021-03-01 21:07:32 +00:00
TEST_CASE("Empty compatibility", "[RouterVersion]")
{
2021-03-01 21:07:32 +00:00
llarp::RouterVersion v1({0, 0, 1}, LLARP_PROTO_VERSION);
2021-03-01 21:07:32 +00:00
CHECK_FALSE(v1.IsCompatableWith(llarp::emptyRouterVersion));
}
2021-03-01 21:07:32 +00:00
TEST_CASE("IsEmpty", "[RouterVersion]")
{
2021-03-01 21:07:32 +00:00
llarp::RouterVersion notEmpty({0, 0, 1}, LLARP_PROTO_VERSION);
CHECK_FALSE(notEmpty.IsEmpty());
2021-03-01 21:07:32 +00:00
CHECK(llarp::emptyRouterVersion.IsEmpty());
}
2021-03-01 21:07:32 +00:00
TEST_CASE("Clear", "[RouterVersion]")
{
2021-03-01 21:07:32 +00:00
llarp::RouterVersion version({0, 0, 1}, LLARP_PROTO_VERSION);
CHECK_FALSE(version.IsEmpty());
version.Clear();
2021-03-01 21:07:32 +00:00
CHECK(version.IsEmpty());
}
2021-03-01 21:07:32 +00:00
TEST_CASE("BEncode", "[RouterVersion]")
{
2021-03-01 21:07:32 +00:00
llarp::RouterVersion v1235({1, 2, 3}, 5);
2021-03-01 21:07:32 +00:00
std::array<byte_t, 128> tmp{};
llarp_buffer_t buf(tmp);
2021-03-01 21:07:32 +00:00
CHECK(v1235.BEncode(&buf));
std::string s((const char*)buf.begin(), (buf.end() - buf.begin()));
LogInfo("bencoded: ", buf.begin());
2021-03-01 21:07:32 +00:00
CHECK_THAT((const char*)buf.begin(), Equals("li5ei1ei2ei3ee"));
}
2021-03-01 21:07:32 +00:00
TEST_CASE("BDecode", "[RouterVersion]")
{
llarp::RouterVersion version;
version.Clear();
const std::string bString("li9ei3ei2ei1ee");
llarp_buffer_t buf(bString.data(), bString.size());
2021-03-01 21:07:32 +00:00
CHECK(version.BDecode(&buf));
2021-03-01 21:07:32 +00:00
llarp::RouterVersion expected({3, 2, 1}, 9);
2021-03-01 21:07:32 +00:00
CHECK(expected == version);
}
2021-03-01 21:07:32 +00:00
TEST_CASE("Decode long version array", "[RouterVersion]")
{
llarp::RouterVersion version;
version.Clear();
const std::string bString("li9ei3ei2ei1ei2ei3ei4ei5ei6ei7ei8ei9ee");
llarp_buffer_t buf(bString.data(), bString.size());
2021-03-01 21:07:32 +00:00
CHECK_FALSE(version.BDecode(&buf));
}