Commit 2c43bb48 authored by Jakob Otto's avatar Jakob Otto

Add more ip_endpoint_tests

parent f0f4ee65
......@@ -40,16 +40,24 @@ public:
// -- properties -------------------------------------------------------------
/// Returns the IPv4 address.
ipv4_address address() const noexcept;
ipv4_address address() const noexcept {
return address_;
}
/// Sets the address of this endpoint.
void address(ipv4_address x) noexcept;
void address(ipv4_address x) noexcept {
address_ = x;
}
/// Returns the port of this endpoint.
uint16_t port() const noexcept;
uint16_t port() const noexcept {
return port_;
}
/// Sets the port of this endpoint.
void port(uint16_t x) noexcept;
void port(uint16_t x) noexcept {
port_ = x;
}
/// Returns a hash for this object
size_t hash_code() const noexcept;
......
......@@ -40,16 +40,24 @@ public:
// -- properties -------------------------------------------------------------
/// Returns the IPv6 address.
ipv6_address address() const noexcept;
ipv6_address address() const noexcept {
return address_;
}
/// Sets the address of this endpoint.
void address(ipv6_address x) noexcept;
void address(ipv6_address x) noexcept {
address_ = x;
}
/// Returns the port of this endpoint.
uint16_t port() const noexcept;
uint16_t port() const noexcept {
return port_;
}
/// Sets the port of this endpoint.
void port(uint16_t x) noexcept;
void port(uint16_t x) noexcept {
port_ = x;
}
/// Returns a hash for this object.
size_t hash_code() const noexcept;
......
......@@ -27,22 +27,6 @@ ipv4_endpoint::ipv4_endpoint(ipv4_address address, uint16_t port)
// nop
}
ipv4_address ipv4_endpoint::address() const noexcept {
return address_;
}
void ipv4_endpoint::address(ipv4_address x) noexcept {
address_ = x;
}
uint16_t ipv4_endpoint::port() const noexcept {
return port_;
}
void ipv4_endpoint::port(uint16_t x) noexcept {
port_ = x;
}
size_t ipv4_endpoint::hash_code() const noexcept {
auto result = detail::fnv_hash(address_.data());
return detail::fnv_hash_append(result, port_);
......
......@@ -27,22 +27,6 @@ ipv6_endpoint::ipv6_endpoint(ipv6_address address, uint16_t port)
// nop
}
ipv6_address ipv6_endpoint::address() const noexcept {
return address_;
}
void ipv6_endpoint::address(ipv6_address x) noexcept {
address_ = x;
}
uint16_t ipv6_endpoint::port() const noexcept {
return port_;
}
void ipv6_endpoint::port(uint16_t x) noexcept {
port_ = x;
}
size_t ipv6_endpoint::hash_code() const noexcept {
auto result = detail::fnv_hash(address_.data());
return detail::fnv_hash_append(result, port_);
......
......@@ -8,7 +8,7 @@
* Copyright 2011-2019 Dominik Charousset *
* *
* Distributed under the terms and conditions of the BSD 3-Clause License or *
* (at your option) under the terms and conditions of the Boost Software *
* (at your option) under th#e terms and conditions of the Boost Software *
* License 1.0. See accompanying files LICENSE and LICENSE_ALTERNATIVE. *
* *
* If you did not receive a copy of the license files, see *
......@@ -22,11 +22,68 @@
#include "caf/test/unit_test.hpp"
#include <vector>
#include "caf/actor_system.hpp"
#include "caf/actor_system_config.hpp"
#include "caf/binary_deserializer.hpp"
#include "caf/byte.hpp"
#include "caf/ipv4_address.hpp"
#include "caf/serializer_impl.hpp"
#include "caf/span.hpp"
using namespace caf;
CAF_TEST(constructing and equality) {
namespace {
struct test_data {
using endpoint = ipv4_endpoint;
using comparison_testcase = std::pair<endpoint, endpoint>;
using to_string_testcase = std::pair<endpoint, std::string>;
test_data() {
const auto addr = make_ipv4_address;
// different ip but same port
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");
}
// First endpoint is always smaller than the second.
std::vector<comparison_testcase> comparison_testdata;
std::vector<to_string_testcase> to_string_testdata;
private:
void add(endpoint ep1, endpoint ep2) {
comparison_testdata.emplace_back(comparison_testcase{ep1, ep2});
}
void add(endpoint ep, const std::string& str) {
to_string_testdata.emplace_back(to_string_testcase{ep, str});
}
};
struct test_fixture : public test_data {
actor_system_config cfg;
actor_system sys{cfg};
};
} // namespace
CAF_TEST(constructing assigning and hash_code) {
const uint16_t port = 8888;
auto addr = make_ipv4_address(127, 0, 0, 1);
ipv4_endpoint ep1(addr, port);
......@@ -38,4 +95,48 @@ CAF_TEST(constructing and equality) {
CAF_CHECK_EQUAL(ep2.address(), addr);
CAF_CHECK_EQUAL(ep2.port(), port);
CAF_CHECK_EQUAL(ep1, ep2);
CAF_CHECK_EQUAL(ep1.hash_code(), ep2.hash_code());
}
CAF_TEST_FIXTURE_SCOPE(comparison_scope, test_fixture)
CAF_TEST(to_string) {
for (auto& testcase : to_string_testdata)
CAF_CHECK_EQUAL(to_string(testcase.first), testcase.second);
}
CAF_TEST(comparison) {
for (auto testcase : comparison_testdata) {
// First member of this pair is always smaller than the second one.
auto ep1 = testcase.first;
auto ep2 = testcase.second;
CAF_CHECK_GREATER(ep2, ep1);
CAF_CHECK_GREATER_OR_EQUAL(ep2, ep1);
CAF_CHECK_GREATER_OR_EQUAL(ep1, ep1);
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) {
using container_type = std::vector<byte>;
for (auto& testcase : comparison_testdata) {
container_type buf;
serializer_impl<container_type> sink(sys, buf);
if (auto err = sink(testcase.first))
CAF_FAIL("serialization failed: " << sys.render(err));
binary_deserializer source(sys, make_span(buf));
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()
\ No newline at end of file
......@@ -22,15 +22,110 @@
#include "caf/test/unit_test.hpp"
#include <vector>
#include "caf/actor_system.hpp"
#include "caf/actor_system_config.hpp"
#include "caf/binary_deserializer.hpp"
#include "caf/byte.hpp"
#include "caf/ipv6_address.hpp"
#include "caf/serializer_impl.hpp"
#include "caf/span.hpp"
using namespace caf;
CAF_TEST(constructing and equality) {
namespace {
struct test_data {
using comparison_testcase = std::pair<ipv6_endpoint, ipv6_endpoint>;
using to_string_testcase = std::pair<ipv6_endpoint, std::string>;
using addr_bytes = ipv6_address::array_type;
test_data() {
// different ip but same port
add(make_ipv6_endpoint({0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
8888),
make_ipv6_endpoint({0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2},
8888));
add(make_ipv6_endpoint({0, 78, 0, 0, 0, 0, 13, 0, 0, 0, 237, 0, 0, 0, 0, 1},
8888),
make_ipv6_endpoint({1, 0, 0, 0, 9, 0, 0, 0, 0, 27, 0, 0, 0, 0, 0, 2},
8888));
add(make_ipv6_endpoint({0, 78, 0, 0, 0, 255, 13, 0, 0, 0, 237, 0, 0, 0, 0,
17},
8888),
make_ipv6_endpoint({1, 0, 0, 0, 9, 0, 0, 0, 0, 27, 0, 0, 0, 255, 0, 3},
8888));
add(make_ipv6_endpoint({0, 78, 0, 0, 0, 0, 13, 0, 0, 0, 237, 0, 0, 0, 0, 1},
8888),
make_ipv6_endpoint({1, 0, 0, 0, 9, 0, 0, 0, 0, 27, 0, 0, 0, 0, 0, 2},
8888));
// same ip but different port
add(make_ipv6_endpoint({0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
1111),
make_ipv6_endpoint({0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
8888));
add(make_ipv6_endpoint({0, 78, 0, 0, 0, 0, 13, 0, 0, 0, 237, 0, 0, 0, 0, 1},
1234),
make_ipv6_endpoint({0, 78, 0, 0, 0, 0, 13, 0, 0, 0, 237, 0, 0, 0, 0, 1},
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");
}
// First endpoint is always smaller than the second.
std::vector<comparison_testcase> comparison_testdata;
std::vector<to_string_testcase> to_string_testdata;
ipv6_endpoint make_ipv6_endpoint(addr_bytes bytes, uint16_t port) {
return ipv6_endpoint{ipv6_address{bytes}, port};
}
private:
// Convenience functions for adding testcases.
void add(ipv6_endpoint ep1, ipv6_endpoint ep2) {
comparison_testdata.emplace_back(comparison_testcase{ep1, ep2});
}
void add(ipv6_endpoint ep, const std::string& str) {
to_string_testdata.emplace_back(to_string_testcase{ep, str});
}
};
struct test_fixture : public test_data {
actor_system_config cfg;
actor_system sys{cfg};
};
} // namespace
CAF_TEST(constructing assigning and hash_code) {
const uint16_t port = 8888;
ipv6_address::array_type localhost_bytes{
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}};
ipv6_address addr(localhost_bytes);
ipv6_address::array_type bytes{0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 1};
auto addr = ipv6_address{bytes};
ipv6_endpoint ep1(addr, port);
CAF_CHECK_EQUAL(ep1.address(), addr);
CAF_CHECK_EQUAL(ep1.port(), port);
......@@ -40,4 +135,48 @@ CAF_TEST(constructing and equality) {
CAF_CHECK_EQUAL(ep2.address(), addr);
CAF_CHECK_EQUAL(ep2.port(), port);
CAF_CHECK_EQUAL(ep1, ep2);
CAF_CHECK_EQUAL(ep1.hash_code(), ep2.hash_code());
}
CAF_TEST_FIXTURE_SCOPE(comparison_scope, test_fixture)
CAF_TEST(to_string) {
for (auto& testcase : to_string_testdata)
CAF_CHECK_EQUAL(to_string(testcase.first), testcase.second);
}
CAF_TEST(comparison) {
for (auto testcase : comparison_testdata) {
// First member of this pair is always smaller than the second one.
auto ep1 = testcase.first;
auto ep2 = testcase.second;
CAF_CHECK_GREATER(ep2, ep1);
CAF_CHECK_GREATER_OR_EQUAL(ep2, ep1);
CAF_CHECK_GREATER_OR_EQUAL(ep1, ep1);
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) {
using container_type = std::vector<byte>;
for (auto& testcase : comparison_testdata) {
container_type buf;
serializer_impl<container_type> sink(sys, buf);
if (auto err = sink(testcase.first))
CAF_FAIL("serialization failed: " << sys.render(err));
binary_deserializer source(sys, make_span(buf));
ipv6_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()
\ 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