Commit 558571e5 authored by Jakob Otto's avatar Jakob Otto

Refactor ipv4_endpoint tests

parent 05a8f44a
...@@ -19,9 +19,11 @@ ...@@ -19,9 +19,11 @@
#define CAF_SUITE ipv4_endpoint #define CAF_SUITE ipv4_endpoint
#include "caf/ipv4_endpoint.hpp" #include "caf/ipv4_endpoint.hpp"
#include "caf/test/dsl.hpp"
#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,53 +38,61 @@ using namespace caf; ...@@ -36,53 +38,61 @@ using namespace caf;
namespace { namespace {
struct test_data { ipv4_endpoint operator"" _ep(const char* str, size_t) {
using endpoint = ipv4_endpoint; std::array<uint8_t, 4> bytes;
using comparison_testcase = std::pair<endpoint, endpoint>; char* next = nullptr;
using to_string_testcase = std::pair<endpoint, std::string>; bytes[0] = static_cast<uint8_t>(strtol(str, &next, 10));
for (size_t n = 1; n < 4; ++n) {
test_data() { assert(*next == '.');
const auto addr = make_ipv4_address; str = next + 1;
// different ip but same port bytes[n] = static_cast<uint8_t>(strtol(str, &next, 10));
add({addr(127, 0, 0, 1), 8888}, {addr(127, 0, 0, 2), 8888});
add({addr(192, 168, 178, 1), 8888}, {addr(245, 114, 2, 89), 8888});
add({addr(188, 56, 23, 97), 1211}, {addr(189, 22, 36, 0), 1211});
add({addr(0, 0, 0, 0), 8888}, {addr(255, 255, 255, 1), 8888});
// same ip but different port
add({addr(127, 0, 0, 1), 111}, {addr(127, 0, 0, 1), 8888});
add({addr(192, 168, 178, 1), 8888}, {addr(245, 114, 2, 89), 8888});
add({addr(127, 0, 0, 1), 111}, {addr(127, 0, 0, 1), 8888});
add({addr(123, 123, 123, 123), 8888}, {addr(123, 123, 123, 123), 8889});
add({addr(127, 0, 0, 1), 8888}, "127.0.0.1:8888");
add({addr(192, 168, 178, 1), 8888}, "192.168.178.1:8888");
add({addr(127, 0, 0, 1), 111}, "127.0.0.1:111");
add({addr(192, 168, 178, 1), 8888}, "192.168.178.1:8888");
add({addr(127, 0, 0, 1), 111}, "127.0.0.1:111");
add({addr(123, 123, 123, 123), 8888}, "123.123.123.123:8888");
} }
assert(*next == ':');
auto port = static_cast<uint16_t>(strtol(next + 1, nullptr, 10));
return ipv4_endpoint{ipv4_address{bytes}, port};
}
// First endpoint is always smaller than the second. struct fixture {
std::vector<comparison_testcase> comparison_testdata; actor_system_config cfg;
std::vector<to_string_testcase> to_string_testdata; actor_system sys{cfg};
private:
void add(endpoint ep1, endpoint ep2) {
comparison_testdata.emplace_back(comparison_testcase{ep1, ep2});
}
void add(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));
ipv4_endpoint 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(ipv4_endpoint_tests, 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;
auto addr = make_ipv4_address(127, 0, 0, 1); auto addr = make_ipv4_address(127, 0, 0, 1);
...@@ -98,45 +108,34 @@ CAF_TEST(constructing assigning and hash_code) { ...@@ -98,45 +108,34 @@ 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) {
CHECK_TO_STRING("127.0.0.1:8888");
CAF_TEST(to_string) { CHECK_TO_STRING("192.168.178.1:8888");
for (auto& testcase : to_string_testdata) CHECK_TO_STRING("255.255.255.1:17");
CAF_CHECK_EQUAL(to_string(testcase.first), testcase.second); CHECK_TO_STRING("192.168.178.1:8888");
CHECK_TO_STRING("127.0.0.1:111");
CHECK_TO_STRING("123.123.123.123:8888");
CHECK_TO_STRING("127.0.0.1:8888");
} }
CAF_TEST(comparison) { CAF_TEST(comparison) {
for (auto testcase : comparison_testdata) { CHECK_COMPARISON("127.0.0.1:8888", "127.0.0.2:8888");
// First member of this pair is always smaller than the second one. CHECK_COMPARISON("192.168.178.1:8888", "245.114.2.89:8888");
auto ep1 = testcase.first; CHECK_COMPARISON("188.56.23.97:1211", "189.22.36.0:1211");
auto ep2 = testcase.second; CHECK_COMPARISON("0.0.0.0:8888", "255.255.255.1:8888");
CAF_CHECK_GREATER(ep2, ep1); CHECK_COMPARISON("127.0.0.1:111", "127.0.0.1:8888");
CAF_CHECK_GREATER_OR_EQUAL(ep2, ep1); CHECK_COMPARISON("192.168.178.1:8888", "245.114.2.89:8888");
CAF_CHECK_GREATER_OR_EQUAL(ep1, ep1); CHECK_COMPARISON("123.123.123.123:8888", "123.123.123.123:8889");
CAF_CHECK_GREATER_OR_EQUAL(ep2, ep2);
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("127.0.0.1:8888");
for (auto& testcase : comparison_testdata) { CHECK_SERIALIZATION("192.168.178.1:8888");
container_type buf; CHECK_SERIALIZATION("255.255.255.1:17");
serializer_impl<container_type> sink(sys, buf); CHECK_SERIALIZATION("192.168.178.1:8888");
if (auto err = sink(testcase.first)) CHECK_SERIALIZATION("127.0.0.1:111");
CAF_FAIL("serialization failed: " << sys.render(err)); CHECK_SERIALIZATION("123.123.123.123:8888");
binary_deserializer source(sys, make_span(buf)); CHECK_SERIALIZATION("127.0.0.1:8888");
ipv4_endpoint deserialized_data;
if (auto err = source(deserialized_data))
CAF_FAIL("deserialization failed: " << sys.render(err));
CAF_CHECK_EQUAL(testcase.first, deserialized_data);
}
} }
CAF_TEST_FIXTURE_SCOPE_END() CAF_TEST_FIXTURE_SCOPE_END()
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