Commit ff277dce authored by Jakob Otto's avatar Jakob Otto

Refactor ipv6_endpoint tests

parent 558571e5
...@@ -64,7 +64,7 @@ struct fixture { ...@@ -64,7 +64,7 @@ struct fixture {
if (auto err = sink(x)) if (auto err = sink(x))
CAF_FAIL("serialization failed: " << sys.render(err)); CAF_FAIL("serialization failed: " << sys.render(err));
binary_deserializer source(sys, make_span(buf)); binary_deserializer source(sys, make_span(buf));
ipv4_endpoint y; T y;
if (auto err = source(y)) if (auto err = source(y))
CAF_FAIL("deserialization failed: " << sys.render(err)); CAF_FAIL("deserialization failed: " << sys.render(err));
return y; return y;
......
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
#include "caf/test/unit_test.hpp" #include "caf/test/unit_test.hpp"
#include <cassert>
#include <vector> #include <vector>
#include "caf/actor_system.hpp" #include "caf/actor_system.hpp"
...@@ -36,91 +37,90 @@ using namespace caf; ...@@ -36,91 +37,90 @@ using namespace caf;
namespace { namespace {
struct test_data { ipv6_endpoint operator"" _ep(const char* str, size_t) {
using comparison_testcase = std::pair<ipv6_endpoint, ipv6_endpoint>; union {
using to_string_testcase = std::pair<ipv6_endpoint, std::string>; std::array<uint8_t, 16> xs_bytes;
using addr_bytes = ipv6_address::array_type; std::array<uint16_t, 8> xs;
};
test_data() { union {
// different ip but same port std::array<uint8_t, 16> ys_bytes;
add(make_ipv6_endpoint({0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, std::array<uint16_t, 8> ys;
8888), };
make_ipv6_endpoint({0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2}, memset(xs_bytes.data(), 0, xs_bytes.size());
8888)); memset(ys_bytes.data(), 0, xs_bytes.size());
add(make_ipv6_endpoint({0, 78, 0, 0, 0, 0, 13, 0, 0, 0, 237, 0, 0, 0, 0, 1}, assert(*str == '[');
8888), ++str;
make_ipv6_endpoint({1, 0, 0, 0, 9, 0, 0, 0, 0, 27, 0, 0, 0, 0, 0, 2}, char* next = nullptr;
8888)); auto pull = [&] {
add(make_ipv6_endpoint({0, 78, 0, 0, 0, 255, 13, 0, 0, 0, 237, 0, 0, 0, 0, auto x = static_cast<uint16_t>(strtol(str, &next, 16));
17}, return detail::to_network_order(x);
8888), };
make_ipv6_endpoint({1, 0, 0, 0, 9, 0, 0, 0, 0, 27, 0, 0, 0, 255, 0, 3}, size_t n = 0;
8888)); do {
add(make_ipv6_endpoint({0, 78, 0, 0, 0, 0, 13, 0, 0, 0, 237, 0, 0, 0, 0, 1}, xs[n++] = pull();
8888), assert(next != nullptr);
make_ipv6_endpoint({1, 0, 0, 0, 9, 0, 0, 0, 0, 27, 0, 0, 0, 0, 0, 2}, str = next + 1;
8888)); } while (*str != ':' && *str != ']');
// same ip but different port if (*str == ':') {
add(make_ipv6_endpoint({0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, ++str;
1111), do {
make_ipv6_endpoint({0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, ys[0] = pull();
8888)); std::rotate(ys.begin(), ys.begin() + 1, ys.end());
add(make_ipv6_endpoint({0, 78, 0, 0, 0, 0, 13, 0, 0, 0, 237, 0, 0, 0, 0, 1}, assert(next != nullptr);
1234), str = next + 1;
make_ipv6_endpoint({0, 78, 0, 0, 0, 0, 13, 0, 0, 0, 237, 0, 0, 0, 0, 1}, } while (*next != ']');
5678));
add(make_ipv6_endpoint({0, 78, 0, 0, 0, 255, 13, 0, 0, 0, 237, 0, 0, 0, 0,
17},
5678),
make_ipv6_endpoint({0, 78, 0, 0, 0, 255, 13, 0, 0, 0, 237, 0, 0, 0, 0,
17},
12345));
add(make_ipv6_endpoint({0, 78, 0, 0, 0, 0, 13, 0, 0, 0, 237, 0, 0, 0, 0, 1},
8888),
make_ipv6_endpoint({0, 78, 0, 0, 0, 0, 13, 0, 0, 0, 237, 0, 0, 0, 0, 1},
9999));
// String test-data
add(make_ipv6_endpoint({0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
1111),
"[::1]:1111");
add(make_ipv6_endpoint({0, 78, 0, 0, 0, 0, 13, 0, 0, 0, 237, 0, 0, 0, 0, 1},
1234),
"[4e::d00:0:ed00:0:1]:1234");
add(make_ipv6_endpoint({0, 78, 0, 0, 0, 255, 13, 0, 0, 0, 237, 0, 0, 0, 0,
17},
2345),
"[4e:0:ff:d00:0:ed00:0:11]:2345");
add(make_ipv6_endpoint({0, 78, 0, 0, 0, 0, 13, 0, 0, 0, 237, 0, 0, 0, 0, 1},
1234),
"[4e::d00:0:ed00:0:1]:1234");
} }
assert(*next == ']');
// First endpoint is always smaller than the second. assert(*str == ':');
std::vector<comparison_testcase> comparison_testdata; ++str;
std::vector<to_string_testcase> to_string_testdata; auto port = static_cast<uint16_t>(strtol(str, nullptr, 10));
std::array<uint8_t, 16> bytes;
ipv6_endpoint make_ipv6_endpoint(addr_bytes bytes, uint16_t port) { for (size_t i = 0; i < 16; ++i)
bytes[i] = xs_bytes[i] | ys_bytes[i];
return ipv6_endpoint{ipv6_address{bytes}, port}; return ipv6_endpoint{ipv6_address{bytes}, port};
} }
private: struct fixture {
// Convenience functions for adding testcases. actor_system_config cfg;
void add(ipv6_endpoint ep1, ipv6_endpoint ep2) { actor_system sys{cfg};
comparison_testdata.emplace_back(comparison_testcase{ep1, ep2});
}
void add(ipv6_endpoint ep, const std::string& str) { template <class T>
to_string_testdata.emplace_back(to_string_testcase{ep, str}); T roundtrip(T x) {
using container_type = std::vector<byte>;
container_type buf;
serializer_impl<container_type> sink(sys, buf);
if (auto err = sink(x))
CAF_FAIL("serialization failed: " << sys.render(err));
binary_deserializer source(sys, make_span(buf));
T y;
if (auto err = source(y))
CAF_FAIL("deserialization failed: " << sys.render(err));
return y;
} }
}; };
struct test_fixture : public test_data { #define CHECK_TO_STRING(addr) CAF_CHECK_EQUAL(addr, to_string(addr##_ep))
actor_system_config cfg;
actor_system sys{cfg}; #define CHECK_COMPARISON(addr1, addr2) \
}; CAF_CHECK_GREATER(addr2##_ep, addr1##_ep); \
CAF_CHECK_GREATER_OR_EQUAL(addr2##_ep, addr1##_ep); \
CAF_CHECK_GREATER_OR_EQUAL(addr1##_ep, addr1##_ep); \
CAF_CHECK_GREATER_OR_EQUAL(addr2##_ep, addr2##_ep); \
CAF_CHECK_EQUAL(addr1##_ep, addr1##_ep); \
CAF_CHECK_EQUAL(addr2##_ep, addr2##_ep); \
CAF_CHECK_LESS_OR_EQUAL(addr1##_ep, addr2##_ep); \
CAF_CHECK_LESS_OR_EQUAL(addr1##_ep, addr1##_ep); \
CAF_CHECK_LESS_OR_EQUAL(addr2##_ep, addr2##_ep); \
CAF_CHECK_NOT_EQUAL(addr1##_ep, addr2##_ep); \
CAF_CHECK_NOT_EQUAL(addr2##_ep, addr1##_ep);
#define CHECK_SERIALIZATION(addr) \
CAF_CHECK_EQUAL(addr##_ep, roundtrip(addr##_ep))
} // namespace } // namespace
CAF_TEST_FIXTURE_SCOPE(comparison_scope, fixture)
CAF_TEST(constructing assigning and hash_code) { CAF_TEST(constructing assigning and hash_code) {
const uint16_t port = 8888; const uint16_t port = 8888;
ipv6_address::array_type bytes{0, 0, 0, 0, 0, 0, 0, 0, ipv6_address::array_type bytes{0, 0, 0, 0, 0, 0, 0, 0,
...@@ -138,45 +138,47 @@ CAF_TEST(constructing assigning and hash_code) { ...@@ -138,45 +138,47 @@ CAF_TEST(constructing assigning and hash_code) {
CAF_CHECK_EQUAL(ep1.hash_code(), ep2.hash_code()); CAF_CHECK_EQUAL(ep1.hash_code(), ep2.hash_code());
} }
CAF_TEST_FIXTURE_SCOPE(comparison_scope, test_fixture)
CAF_TEST(to_string) { CAF_TEST(to_string) {
for (auto& testcase : to_string_testdata) CHECK_TO_STRING("[::1]:8888");
CAF_CHECK_EQUAL(to_string(testcase.first), testcase.second); CHECK_TO_STRING("[4e::d00:0:ed00:0:1]:1234");
CHECK_TO_STRING("[::1]:1111");
CHECK_TO_STRING("[4432::33:1]:8732");
CHECK_TO_STRING("[::2]:8888");
CHECK_TO_STRING("[4f::d00:12:ed00:0:1]:1234");
CHECK_TO_STRING("[4f::1]:2222");
CHECK_TO_STRING("[4432:8d::33:1]:8732");
CHECK_TO_STRING("[4e::d00:0:ed00:0:1]:5678");
CHECK_TO_STRING("[::1]:2221");
CHECK_TO_STRING("[::1]:2222");
CHECK_TO_STRING("[4432::33:1]:872");
CHECK_TO_STRING("[4432::33:1]:999");
} }
CAF_TEST(comparison) { CAF_TEST(comparison) {
for (auto testcase : comparison_testdata) { CHECK_COMPARISON("[::1]:8888", "[::2]:8888");
// First member of this pair is always smaller than the second one. CHECK_COMPARISON("[4e::d00:0:ed00:0:1]:1234", "[4f::d00:12:ed00:0:1]:1234");
auto ep1 = testcase.first; CHECK_COMPARISON("[::1]:1111", "[4f::1]:2222");
auto ep2 = testcase.second; CHECK_COMPARISON("[4432::33:1]:8732", "[4432:8d::33:1]:8732");
CAF_CHECK_GREATER(ep2, ep1); CHECK_COMPARISON("[::1]:1111", "[::1]:8888");
CAF_CHECK_GREATER_OR_EQUAL(ep2, ep1); CHECK_COMPARISON("[4e::d00:0:ed00:0:1]:1234", "[4e::d00:0:ed00:0:1]:5678");
CAF_CHECK_GREATER_OR_EQUAL(ep1, ep1); CHECK_COMPARISON("[::1]:2221", "[::1]:2222");
CAF_CHECK_GREATER_OR_EQUAL(ep2, ep2); CHECK_COMPARISON("[4432::33:1]:872", "[4432::33:1]:999");
CAF_CHECK_EQUAL(ep1, ep1);
CAF_CHECK_EQUAL(ep2, ep2);
CAF_CHECK_LESS_OR_EQUAL(ep1, ep2);
CAF_CHECK_LESS_OR_EQUAL(ep1, ep1);
CAF_CHECK_LESS_OR_EQUAL(ep2, ep2);
CAF_CHECK_NOT_EQUAL(ep1, ep2);
CAF_CHECK_NOT_EQUAL(ep2, ep1);
}
} }
CAF_TEST(serialization) { CAF_TEST(serialization) {
using container_type = std::vector<byte>; CHECK_SERIALIZATION("[::1]:8888");
for (auto& testcase : comparison_testdata) { CHECK_SERIALIZATION("[4e::d00:0:ed00:0:1]:1234");
container_type buf; CHECK_SERIALIZATION("[::1]:1111");
serializer_impl<container_type> sink(sys, buf); CHECK_SERIALIZATION("[4432::33:1]:8732");
if (auto err = sink(testcase.first)) CHECK_SERIALIZATION("[::2]:8888");
CAF_FAIL("serialization failed: " << sys.render(err)); CHECK_SERIALIZATION("[4f::d00:12:ed00:0:1]:1234");
binary_deserializer source(sys, make_span(buf)); CHECK_SERIALIZATION("[4f::1]:2222");
ipv6_endpoint deserialized_data; CHECK_SERIALIZATION("[4432:8d::33:1]:8732");
if (auto err = source(deserialized_data)) CHECK_SERIALIZATION("[4e::d00:0:ed00:0:1]:5678");
CAF_FAIL("deserialization failed: " << sys.render(err)); CHECK_SERIALIZATION("[::1]:2221");
CAF_CHECK_EQUAL(testcase.first, deserialized_data); CHECK_SERIALIZATION("[::1]:2222");
} CHECK_SERIALIZATION("[4432::33:1]:872");
CHECK_SERIALIZATION("[4432::33:1]:999");
} }
CAF_TEST_FIXTURE_SCOPE_END() CAF_TEST_FIXTURE_SCOPE_END()
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment