forked from Twon/Morpheus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Asio IP address program options test * Ip6 test cases * Pre-commit formatting * Formatting * Todo extend namedendpoint to convert to address * Work in progress URL support * Clean up * Remove header
- Loading branch information
Showing
11 changed files
with
181 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#pragma once | ||
|
||
#include <morpheus/core/base/prerequisites.hpp> | ||
|
||
#include <boost/algorithm/string.hpp> | ||
#include <boost/any.hpp> | ||
#include <boost/asio/ip/tcp.hpp> | ||
#include <boost/system/error_code.hpp> | ||
#include <boost/program_options.hpp> | ||
|
||
#include <string> | ||
#include <vector> | ||
|
||
namespace boost::asio::ip | ||
{ | ||
|
||
template <class CharType> | ||
void validate(boost::any& v, std::vector<std::basic_string<CharType>> const& values, address* p, int) | ||
{ | ||
namespace po = boost::program_options; | ||
po::validators::check_first_occurrence(v); | ||
auto const& s = po::validators::get_single_string(values); | ||
|
||
boost::system::error_code ec = make_error_code(boost::system::errc::success); | ||
auto const address = boost::asio::ip::make_address(s, ec); | ||
if (ec == boost::asio::error::invalid_argument) | ||
{ | ||
throw po::validation_error(po::validation_error::invalid_option_value); | ||
} | ||
|
||
v = address; | ||
} | ||
|
||
} // namespace boost::asio::ip |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
target_sources(MorpheusApplicationTests | ||
PRIVATE | ||
asio.tests.cpp | ||
log.tests.cpp | ||
) |
63 changes: 63 additions & 0 deletions
63
libraries/application/tests/po/adapters/boost/asio.tests.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
#include "morpheus/application/po/adapters/boost/asio.hpp" | ||
#include "morpheus/application/po/options.hpp" | ||
#include "morpheus/core/conformance/ranges.hpp" | ||
#include "morpheus/logging.hpp" | ||
#include "morpheus/redirect_stream.hpp" | ||
|
||
#include <catch2/catch_test_macros.hpp> | ||
#include <iostream> | ||
|
||
namespace morpheus::application::po | ||
{ | ||
|
||
struct BoostLogFixture | ||
{ | ||
LoggingFixture disableLogging; | ||
RedirectStream captureCout = RedirectStream(std::cout); | ||
RedirectStream captureCerr = RedirectStream(std::cerr); | ||
}; | ||
|
||
struct Address | ||
{ | ||
boost::asio::ip::address ipAddress; | ||
|
||
void addOptions(boost::program_options::options_description& options) | ||
{ | ||
namespace po = boost::program_options; | ||
// clang-format off | ||
options.add_options() | ||
("address", po::value(&ipAddress)->required(), "The ip address."); | ||
// clang-format on | ||
} | ||
}; | ||
|
||
TEST_CASE_METHOD(BoostLogFixture, "Test parsing of boost asio address as program options", "[morpheus.application.po.adapters.boost.asio.address]") | ||
{ | ||
|
||
SECTION("Ensure valid value parse correctly") | ||
{ | ||
auto getAddress = [](std::string_view param) | ||
{ | ||
Address address{}; | ||
std::array cliOptions = {"dummyProgram.exe", "--address", param.data()}; | ||
auto const result = parseProgramOptions(cliOptions.size(), cliOptions.data(), HelpDocumentation{}, address); | ||
REQUIRE(!result); | ||
return address.ipAddress; | ||
}; | ||
|
||
REQUIRE(getAddress("127.0.0.1") == boost::asio::ip::address_v4::loopback()); | ||
REQUIRE(getAddress("192.168.1.218") == boost::asio::ip::address_v4({192, 168, 1, 218})); | ||
REQUIRE(getAddress("0:0:0:0:0:0:0:1") == boost::asio::ip::address_v6::loopback()); | ||
REQUIRE(getAddress("fd00::f06b:9ee5:8bfa:666c") == | ||
boost::asio::ip::address_v6({0xfd, 0, 0, 0, 0, 0, 0, 0, 0xf0, 0x6b, 0x9e, 0xe5, 0x8b, 0xfa, 0x66, 0x6c})); | ||
} | ||
SECTION("Ensure invalid value parse correctly") | ||
{ | ||
std::array cliOptions = {"dummyProgram.exe", "--address", "invalid"}; | ||
Address address; | ||
auto const result = parseProgramOptions(cliOptions.size(), cliOptions.data(), HelpDocumentation{}, address); | ||
REQUIRE(result); | ||
} | ||
} | ||
|
||
} // namespace morpheus::application::po |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
target_sources(MorpheusCore | ||
PUBLIC | ||
named_endpoint.hpp | ||
) |
48 changes: 48 additions & 0 deletions
48
libraries/core/src/morpheus/core/network/named_endpoint.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#pragma once | ||
|
||
#include <boost/container_hash/hash.hpp> | ||
|
||
#include <compare> | ||
#include <cstdint> | ||
#include <string_view> | ||
|
||
namespace morpheus::network | ||
{ | ||
|
||
/// \class NamedEndpoint | ||
/// Allows for hostname to endpont resolution via DNS lookup.a | ||
/// \todo | ||
/// https://stackoverflow.com/questions/31314433/how-do-i-convert-a-host-name-in-to-a-boost-address-or-endpoint | ||
class NamedEndpoint | ||
{ | ||
public: | ||
NamedEndpoint(std::string_view const name, std::uint16_t const port) | ||
: mName(name) | ||
, mPort(port) | ||
{} | ||
|
||
[[nodisard]] auto name() const noexcept { return mName; } | ||
|
||
[[nodisard]] auto port() const noexcept { return mPort; } | ||
|
||
[[nodisard]] auto operator<=>(NamedEndpoint const& rhs) const noexcept = default; | ||
|
||
private: | ||
std::string mName; | ||
std::uint16_t mPort; | ||
}; | ||
|
||
} // namespace morpheus::network | ||
|
||
/// std::hash specialisation for named enpoint | ||
template <> | ||
struct std::hash<morpheus::network::NamedEndpoint> | ||
{ | ||
std::size_t operator()(morpheus::network::NamedEndpoint const& n) const | ||
{ | ||
std::size_t seed = 0; | ||
boost::hash_combine(seed, n.name()); | ||
boost::hash_combine(seed, n.port()); | ||
return seed; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
target_sources(MorpheusCoreTests | ||
PRIVATE | ||
named_endpoint.tests.cpp | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#include "morpheus/core/network/named_endpoint.hpp" | ||
|
||
#include <catch2/catch_all.hpp> | ||
#include <gmock/gmock.h> | ||
|
||
namespace morpheus::network | ||
{ | ||
|
||
using namespace ::testing; | ||
|
||
TEST_CASE("Verify construction of named endpoints", "[morpheus.network.named_endpoint.construction]") {} | ||
|
||
} // namespace morpheus::network |