From 58aff2b96684229d0ce5e9560f3a31392c46a837 Mon Sep 17 00:00:00 2001 From: Carsten Patzke <carsten.patzke@desy.de> Date: Thu, 1 Feb 2018 14:19:06 +0100 Subject: [PATCH] Added hostname resolution to system io --- common/cpp/include/system_wrappers/io.h | 2 + .../cpp/include/system_wrappers/system_io.h | 3 +- common/cpp/src/system_io.cpp | 14 +- common/cpp/src/system_io_linux.cpp | 15 ++ common/cpp/unittests/MockIO.h | 1 + receiver/CMakeLists.txt | 11 +- receiver/src/main.cpp | 2 +- receiver/src/network_producer_peer.cpp | 1 - receiver/src/network_producer_peer.h | 3 - receiver/src/receiver.cpp | 10 +- receiver/src/receiver.h | 9 +- receiver/unittests/test_receiver.cpp | 211 +++--------------- .../test_file_reference_handler.cpp | 0 receiver/unittests_OLD/test_receiver.cpp | 194 ++++++++++++++++ tests/system_io/CMakeLists.txt | 1 + .../resolve_hostname_to_ip/CMakeLists.txt | 22 ++ .../resolve_hostname_to_ip.cpp | 38 ++++ 17 files changed, 337 insertions(+), 200 deletions(-) rename receiver/{unittests => unittests_OLD}/test_file_reference_handler.cpp (100%) create mode 100644 receiver/unittests_OLD/test_receiver.cpp create mode 100644 tests/system_io/resolve_hostname_to_ip/CMakeLists.txt create mode 100644 tests/system_io/resolve_hostname_to_ip/resolve_hostname_to_ip.cpp diff --git a/common/cpp/include/system_wrappers/io.h b/common/cpp/include/system_wrappers/io.h index da8f97113..45a0fd458 100644 --- a/common/cpp/include/system_wrappers/io.h +++ b/common/cpp/include/system_wrappers/io.h @@ -32,6 +32,7 @@ enum class IOErrors { kSocketOperationOnNonSocket, kMemoryAllocationError, kInvalidMemoryAddress, + kUnableToResolveHostname, }; enum FileOpenMode { @@ -80,6 +81,7 @@ class IO { virtual void InetBind(SocketDescriptor socket_fd, const std::string& address, IOErrors* err) const = 0; virtual std::unique_ptr<std::tuple<std::string, SocketDescriptor>> InetAccept(SocketDescriptor socket_fd, IOErrors* err) const = 0; + virtual std::string ResolveHostnameToIp(const std::string& hostname, IOErrors* err) const = 0; virtual void InetConnect(SocketDescriptor socket_fd, const std::string& address, IOErrors* err) const = 0; virtual SocketDescriptor CreateAndConnectIPTCPSocket(const std::string& address, IOErrors* err) const = 0; virtual size_t Receive(SocketDescriptor socket_fd, void* buf, size_t length, IOErrors* err) const = 0; diff --git a/common/cpp/include/system_wrappers/system_io.h b/common/cpp/include/system_wrappers/system_io.h index cdb8222c3..baa6d2529 100644 --- a/common/cpp/include/system_wrappers/system_io.h +++ b/common/cpp/include/system_wrappers/system_io.h @@ -39,7 +39,7 @@ class SystemIO final : public IO { ssize_t _recv(SocketDescriptor socket_fd, void* buffer, size_t length) const; bool _close_socket(SocketDescriptor socket_fd) const; - std::unique_ptr<std::tuple<std::string, uint16_t>> SplitAddressToHostAndPort(std::string address) const; + std::unique_ptr<std::tuple<std::string, uint16_t>> SplitAddressToHostnameAndPort(std::string address) const; public: /* @@ -59,6 +59,7 @@ class SystemIO final : public IO { void Listen(SocketDescriptor socket_fd, int backlog, IOErrors* err) const; void InetBind(SocketDescriptor socket_fd, const std::string& address, IOErrors* err) const; std::unique_ptr<std::tuple<std::string, SocketDescriptor>> InetAccept(SocketDescriptor socket_fd, IOErrors* err) const; + std::string ResolveHostnameToIp(const std::string& hostname, IOErrors* err) const; void InetConnect(SocketDescriptor socket_fd, const std::string& address, IOErrors* err) const; SocketDescriptor CreateAndConnectIPTCPSocket(const std::string& address, IOErrors* err) const; size_t Receive(SocketDescriptor socket_fd, void* buf, size_t length, IOErrors* err) const; diff --git a/common/cpp/src/system_io.cpp b/common/cpp/src/system_io.cpp index 933c55bdc..6b74299a3 100644 --- a/common/cpp/src/system_io.cpp +++ b/common/cpp/src/system_io.cpp @@ -256,7 +256,7 @@ void hidra2::SystemIO::InetBind(SocketDescriptor socket_fd, const std::string& a return; } - auto host_port_tuple = SplitAddressToHostAndPort(address); + auto host_port_tuple = SplitAddressToHostnameAndPort(address); if (!host_port_tuple) { *err = IOErrors::kInvalidAddressFormat; return; @@ -413,7 +413,7 @@ void hidra2::SystemIO::CreateNewDirectory(const std::string& directory_name, hid *err = IOErrors::kNoError; } } -std::unique_ptr<std::tuple<std::string, uint16_t>> SystemIO::SplitAddressToHostAndPort(std::string address) const { +std::unique_ptr<std::tuple<std::string, uint16_t>> SystemIO::SplitAddressToHostnameAndPort(std::string address) const { try { std::string host = address.substr(0, address.find(':')); @@ -427,14 +427,18 @@ std::unique_ptr<std::tuple<std::string, uint16_t>> SystemIO::SplitAddressToHostA } void hidra2::SystemIO::InetConnect(SocketDescriptor socket_fd, const std::string& address, IOErrors* err) const { - auto host_port_tuple = SplitAddressToHostAndPort(address); - if (!host_port_tuple) { + auto hostname_port_tuple = SplitAddressToHostnameAndPort(address); + if (!hostname_port_tuple) { *err = IOErrors::kInvalidAddressFormat; return; } std::string host; uint16_t port = 0; - std::tie(host, port) = *host_port_tuple; + std::tie(host, port) = *hostname_port_tuple; + host = ResolveHostnameToIp(host, err); + if(*err != IOErrors::kNoError) { + return; + } short family = AddressFamilyToPosixFamily(AddressFamilies::INET); if (family == -1) { diff --git a/common/cpp/src/system_io_linux.cpp b/common/cpp/src/system_io_linux.cpp index 02b2c6503..2532548d2 100644 --- a/common/cpp/src/system_io_linux.cpp +++ b/common/cpp/src/system_io_linux.cpp @@ -11,6 +11,7 @@ #include <fcntl.h> #include <iostream> #include <zconf.h> +#include <netdb.h> using std::string; using std::vector; @@ -176,6 +177,20 @@ void hidra2::SystemIO::ApplyNetworkOptions(SocketDescriptor socket_fd, IOErrors* } } + +std::string SystemIO::ResolveHostnameToIp(const std::string& hostname, IOErrors* err) const { + hostent* record = gethostbyname(hostname.c_str()); + if (record == nullptr) { + *err = IOErrors::kUnableToResolveHostname; + return ""; + } + in_addr* address = (in_addr*)(record->h_addr); + string ip_address = inet_ntoa(*address); + + *err = IOErrors::kNoError; + return ip_address; +} + hidra2::FileDescriptor hidra2::SystemIO::_open(const char* filename, int posix_open_flags) const { return ::open(filename, posix_open_flags, S_IWUSR | S_IRWXU); } diff --git a/common/cpp/unittests/MockIO.h b/common/cpp/unittests/MockIO.h index 1421d378b..92149df4c 100644 --- a/common/cpp/unittests/MockIO.h +++ b/common/cpp/unittests/MockIO.h @@ -22,6 +22,7 @@ class MockIO : public IO { } MOCK_CONST_METHOD2(InetAccept_proxy, std::tuple<std::string, SocketDescriptor>* (SocketDescriptor socket_fd, IOErrors* err)); + MOCK_CONST_METHOD2(ResolveHostnameToIp, std::string(const std::string& hostname, IOErrors* err)); MOCK_CONST_METHOD3(InetConnect, void(SocketDescriptor socket_fd, const std::string& address, IOErrors* err)); MOCK_CONST_METHOD2(CreateAndConnectIPTCPSocket, diff --git a/receiver/CMakeLists.txt b/receiver/CMakeLists.txt index 88ac9b4fa..c1a611100 100644 --- a/receiver/CMakeLists.txt +++ b/receiver/CMakeLists.txt @@ -18,10 +18,17 @@ target_link_libraries(${TARGET_NAME} ${HIDRA2_COMMON_IO_LIBRARIES}) set_target_properties(${TARGET_NAME} PROPERTIES LINKER_LANGUAGE CXX) + ################################ # Testing ################################ -#set(TEST_SOURCE_FILES unittests/test_receiver.cpp unittests/test_file_reference_handler.cpp src/receiver.h src/receiver.cpp src/network_producer_peer.h src/network_producer_peer.cpp src/network_producer_peer_handlers.cpp) -#set(TEST_LIBRARIES common pthread) + +#set_property(TARGET ${TARGET_NAME} PROPERTY ENABLE_EXPORTS true) # +#set(TEST_SOURCE_FILES +# unittests/test_receiver.cpp +# ) +# +#set(TEST_LIBRARIES "${TARGET_NAME} common") #gtest(${TARGET_NAME} "${TEST_SOURCE_FILES}" "${TEST_LIBRARIES}") + diff --git a/receiver/src/main.cpp b/receiver/src/main.cpp index 55f460f2b..cba32d5c5 100644 --- a/receiver/src/main.cpp +++ b/receiver/src/main.cpp @@ -9,7 +9,7 @@ int main (int argc, char* argv[]) { hidra2::ReceiverError err; receiver->StartListener(address, &err); - if(err != hidra2::ReceiverError::NO_ERROR) { + if(err != hidra2::ReceiverError::kNoError) { std::cerr << "Fail to start receiver" << std::endl; return 1; } diff --git a/receiver/src/network_producer_peer.cpp b/receiver/src/network_producer_peer.cpp index b78927b75..f11f19b23 100644 --- a/receiver/src/network_producer_peer.cpp +++ b/receiver/src/network_producer_peer.cpp @@ -11,7 +11,6 @@ const std::vector<NetworkProducerPeer::RequestHandlerInformation> NetworkProduce NetworkProducerPeer::init_request_handlers(); NetworkProducerPeer::NetworkProducerPeer(int socket_fd, std::string address) : HasIO() { - address_ = std::move(address); socket_fd_ = socket_fd; connection_id_ = kNetworkProducerPeerCount++; } diff --git a/receiver/src/network_producer_peer.h b/receiver/src/network_producer_peer.h index 473bc15f4..ccb75fe6b 100644 --- a/receiver/src/network_producer_peer.h +++ b/receiver/src/network_producer_peer.h @@ -31,7 +31,6 @@ class NetworkProducerPeer : HasIO { uint32_t connection_id_; int socket_fd_; - std::string address_; bool is_listening_ = false; std::thread* listener_thread_ = nullptr; @@ -58,8 +57,6 @@ class NetworkProducerPeer : HasIO { void stop_peer_listener(); uint32_t connection_id() const; - const std::string& address() const; - bool is_listening() const; }; } diff --git a/receiver/src/receiver.cpp b/receiver/src/receiver.cpp index b25cedd76..b8f77ae5a 100644 --- a/receiver/src/receiver.cpp +++ b/receiver/src/receiver.cpp @@ -6,10 +6,10 @@ const int hidra2::Receiver::kMaxUnacceptedConnectionsBacklog = 5; void hidra2::Receiver::StartListener(std::string listener_address, ReceiverError* err) { - *err = ReceiverError::NO_ERROR; + *err = ReceiverError::kNoError; if(listener_running_) { - *err = ReceiverError::ALREADY_LISTEING; + *err = ReceiverError::kAlreadyListeing; return; } listener_running_ = true; @@ -19,7 +19,7 @@ void hidra2::Receiver::StartListener(std::string listener_address, ReceiverError FileDescriptor listener_fd = io->CreateSocket(AddressFamilies::INET, SocketTypes::STREAM, SocketProtocols::IP, &io_error); if(io_error != IOErrors::kNoError) { - *err = ReceiverError::FAILED_CREATING_SOCKET; + *err = ReceiverError::FailToCreateSocket; listener_running_ = false; std::cerr << "Fail to create socket" << std::endl; return; @@ -28,7 +28,7 @@ void hidra2::Receiver::StartListener(std::string listener_address, ReceiverError io->InetBind(listener_fd, listener_address, &io_error); if(io_error != IOErrors::kNoError) { io->CloseSocket(listener_fd, nullptr); - *err = ReceiverError::FAILED_CREATING_SOCKET; + *err = ReceiverError::FailToCreateSocket; listener_running_ = false; std::cerr << "Fail to bind socket" << std::endl; return; @@ -37,7 +37,7 @@ void hidra2::Receiver::StartListener(std::string listener_address, ReceiverError io->Listen(listener_fd, kMaxUnacceptedConnectionsBacklog, &io_error); if(io_error != IOErrors::kNoError) { io->CloseSocket(listener_fd, nullptr); - *err = ReceiverError::FAILED_CREATING_SOCKET; + *err = ReceiverError::FailToCreateSocket; listener_running_ = false; std::cerr << "Fail to start listen" << std::endl; return; diff --git a/receiver/src/receiver.h b/receiver/src/receiver.h index d5c07b452..1f448ea9a 100644 --- a/receiver/src/receiver.h +++ b/receiver/src/receiver.h @@ -10,16 +10,16 @@ namespace hidra2 { enum class ReceiverError { - NO_ERROR, - ALREADY_LISTEING, - FAILED_CREATING_SOCKET, + kNoError, + kAlreadyListeing, + FailToCreateSocket, }; class Receiver : public HasIO { friend NetworkProducerPeer; private: bool listener_running_ = false; - FileDescriptor listener_fd_; + FileDescriptor listener_fd_ = -1; std::thread* accept_thread_ = nullptr; void AcceptThreadLogic(); @@ -31,6 +31,7 @@ class Receiver : public HasIO { Receiver(const Receiver&) = delete; Receiver& operator=(const Receiver&) = delete; + Receiver() = default; void StartListener(std::string listener_address, ReceiverError* err); diff --git a/receiver/unittests/test_receiver.cpp b/receiver/unittests/test_receiver.cpp index e96b9c1da..41cba0595 100644 --- a/receiver/unittests/test_receiver.cpp +++ b/receiver/unittests/test_receiver.cpp @@ -1,11 +1,8 @@ #include <gtest/gtest.h> #include <gmock/gmock.h> -#include <system_wrappers/io.h> #include "../src/receiver.h" #include "../../common/cpp/unittests/MockIO.h" -namespace { - using ::testing::Return; using ::testing::_; using ::testing::DoAll; @@ -14,181 +11,39 @@ using ::testing::Gt; using ::testing::Mock; using ::testing::InSequence; -//Need to be redone completely +namespace { + +TEST(a, b) { + hidra2::MockIO mockIO; + + hidra2::Receiver receiver; +} + +/* + TEST(Receiver, start_Listener__InetBind_fail) { + hidra2::MockIO mockIO; -// hidra2::Receiver receiver; -// hidra2::FileDescriptor expected_socket_fd = 1338; -// -//TEST(Receiver, start_Listener__CreateSocket_fail) { -// hidra2::MockIO mockIO; -// receiver.__set_io(&mockIO); -// -// InSequence sequence; -// -// std::string expected_address = "127.0.0.1"; -// uint16_t expected_port = 9876; -// -// EXPECT_CALL(mockIO, CreateSocket(hidra2::AddressFamilies::INET, hidra2::SocketTypes::STREAM, -// hidra2::SocketProtocols::IP, _)) -// .Times(1) -// .WillOnce( -// DoAll( -// testing::SetArgPointee<3>(hidra2::IOError::UNKNOWN_ERROR), -// Return(-1) -// )); -// -// hidra2::ReceiverError receiver_error; -// receiver.StartListener(expected_address, expected_port, &receiver_error); -// EXPECT_EQ(receiver_error, hidra2::ReceiverError::FAILED_CREATING_SOCKET); -// -// Mock::VerifyAndClearExpectations(&mockIO); -//} -// -//TEST(Receiver, start_Listener__InetBind_fail) { -// hidra2::MockIO mockIO; -// receiver.__set_io(&mockIO); -// -// InSequence sequence; -// -// std::string expected_address = "127.0.0.1"; -// uint16_t expected_port = 9876; -// -// EXPECT_CALL(mockIO, CreateSocket(hidra2::AddressFamilies::INET, hidra2::SocketTypes::STREAM, -// hidra2::SocketProtocols::IP, _)) -// .Times(1) -// .WillOnce( -// DoAll( -// testing::SetArgPointee<3>(hidra2::IOError::NO_ERROR), -// Return(expected_socket_fd) -// )); -// -// EXPECT_CALL(mockIO, InetBind(expected_socket_fd, expected_address, expected_port, _)) -// .Times(1) -// .WillOnce(testing::SetArgPointee<3>(hidra2::IOError::ADDRESS_ALREADY_IN_USE)); -// -// EXPECT_CALL(mockIO, Close(expected_socket_fd)) -// .Times(1); -// -// hidra2::ReceiverError receiver_error; -// receiver.StartListener(expected_address, expected_port, &receiver_error); -// EXPECT_EQ(receiver_error, hidra2::ReceiverError::FAILED_CREATING_SOCKET); -// -// Mock::VerifyAndClearExpectations(&mockIO); -//} -// -//TEST(Receiver, start_Listener__Listen_fail) { -// hidra2::MockIO mockIO; -// receiver.__set_io(&mockIO); -// -// InSequence sequence; -// -// std::string expected_address = "127.0.0.1"; -// uint16_t expected_port = 9876; -// -// EXPECT_CALL(mockIO, CreateSocket(hidra2::AddressFamilies::INET, hidra2::SocketTypes::STREAM, -// hidra2::SocketProtocols::IP, _)) -// .Times(1) -// .WillOnce( -// DoAll( -// testing::SetArgPointee<3>(hidra2::IOError::NO_ERROR), -// Return(expected_socket_fd) -// )); -// -// EXPECT_CALL(mockIO, InetBind(expected_socket_fd, expected_address, expected_port, _)) -// .Times(1) -// .WillOnce(testing::SetArgPointee<3>(hidra2::IOError::NO_ERROR)); -// -// EXPECT_CALL(mockIO, Listen(expected_socket_fd, receiver.kMaxUnacceptedConnectionsBacklog, _)) -// .Times(1) -// .WillOnce(testing::SetArgPointee<2>(hidra2::IOError::BAD_FILE_NUMBER)); -// -// EXPECT_CALL(mockIO, Close(expected_socket_fd, _)) -// .Times(1); -// -// hidra2::ReceiverError receiver_error; -// receiver.StartListener(expected_address, expected_port, &receiver_error); -// EXPECT_EQ(receiver_error, hidra2::ReceiverError::FAILED_CREATING_SOCKET); -// -// Mock::VerifyAndClearExpectations(&mockIO); -//} -// -//TEST(Receiver, start_Listener) { -// hidra2::MockIO mockIO; -// receiver.__set_io(&mockIO); -// -// InSequence sequence; -// -// std::string expected_address = "127.0.0.1"; -// uint16_t expected_port = 9876; -// -// EXPECT_CALL(mockIO, CreateSocket(hidra2::AddressFamilies::INET, hidra2::SocketTypes::STREAM, -// hidra2::SocketProtocols::IP, _)) -// .Times(1) -// .WillOnce( -// DoAll( -// testing::SetArgPointee<3>(hidra2::IOError::NO_ERROR), -// Return(expected_socket_fd) -// )); -// -// EXPECT_CALL(mockIO, InetBind(expected_socket_fd, expected_address, expected_port, _)) -// .Times(1) -// .WillOnce(testing::SetArgPointee<3>(hidra2::IOError::NO_ERROR)); -// -// EXPECT_CALL(mockIO, Listen(expected_socket_fd, receiver.kMaxUnacceptedConnectionsBacklog, _)) -// .Times(1) -// .WillOnce(testing::SetArgPointee<2>(hidra2::IOError::NO_ERROR)); -// -// /** -// * TODO: Since StartListener will start a new thread -// * we need to mock std::thread -// */ -// EXPECT_CALL(mockIO, InetAcceptProxy(expected_socket_fd, _)) -// .Times(1) -// .WillOnce( -// DoAll( -// testing::SetArgPointee<1>(hidra2::IOError::BAD_FILE_NUMBER), -// Return(nullptr) -// )); -// -// -// hidra2::ReceiverError receiver_error; -// receiver.StartListener(expected_address, expected_port, &receiver_error); -// EXPECT_EQ(receiver_error, hidra2::ReceiverError::NO_ERROR); -// -// sleep(1); //Make sure that the thread is running so InetAcceptProxy would work -// -// Mock::VerifyAndClearExpectations(&mockIO); -//} -// -//TEST(Receiver, start_Listener_already_Listening) { -// InSequence sequence; -// -// std::string expected_address = "127.0.0.1"; -// uint16_t expected_port = 9876; -// -// hidra2::ReceiverError receiver_error; -// receiver.StartListener(expected_address, expected_port, &receiver_error); -// EXPECT_EQ(receiver_error, hidra2::ReceiverError::ALREADY_LISTEING); -//} -// -//TEST(Receiver, stop_Listener) { -// hidra2::MockIO mockIO; -// receiver.__set_io(&mockIO); -// -// -// EXPECT_CALL(mockIO, Close(expected_socket_fd)) -// .Times(1) -// .WillOnce( -// Return(0) -// ); -// -// -// hidra2::ReceiverError receiver_error; -// -// receiver.stop_listener(&receiver_error); -// EXPECT_EQ(receiver_error, hidra2::ReceiverError::NO_ERROR); -// -// Mock::VerifyAndClearExpectations(&mockIO); -//} + hidra2::Receiver receiver; + + receiver.__set_io(&mockIO); + + InSequence sequence; + + std::string expected_address = "127.0.0.1:9876"; + + EXPECT_CALL(mockIO, CreateSocket(hidra2::AddressFamilies::INET, hidra2::SocketTypes::STREAM, + hidra2::SocketProtocols::IP, _)) + .Times(1) + .WillOnce( + DoAll( + testing::SetArgPointee<3>(hidra2::IOErrors::kUnknownError), + Return(-1) + )); + + hidra2::ReceiverError receiver_error; + receiver.StartListener(expected_address, &receiver_error); + EXPECT_EQ(receiver_error, hidra2::ReceiverError::FailToCreateSocket); +} +*/ } diff --git a/receiver/unittests/test_file_reference_handler.cpp b/receiver/unittests_OLD/test_file_reference_handler.cpp similarity index 100% rename from receiver/unittests/test_file_reference_handler.cpp rename to receiver/unittests_OLD/test_file_reference_handler.cpp diff --git a/receiver/unittests_OLD/test_receiver.cpp b/receiver/unittests_OLD/test_receiver.cpp new file mode 100644 index 000000000..e96b9c1da --- /dev/null +++ b/receiver/unittests_OLD/test_receiver.cpp @@ -0,0 +1,194 @@ +#include <gtest/gtest.h> +#include <gmock/gmock.h> +#include <system_wrappers/io.h> +#include "../src/receiver.h" +#include "../../common/cpp/unittests/MockIO.h" + +namespace { + +using ::testing::Return; +using ::testing::_; +using ::testing::DoAll; +using ::testing::SetArgReferee; +using ::testing::Gt; +using ::testing::Mock; +using ::testing::InSequence; + +//Need to be redone completely + +// hidra2::Receiver receiver; +// hidra2::FileDescriptor expected_socket_fd = 1338; +// +//TEST(Receiver, start_Listener__CreateSocket_fail) { +// hidra2::MockIO mockIO; +// receiver.__set_io(&mockIO); +// +// InSequence sequence; +// +// std::string expected_address = "127.0.0.1"; +// uint16_t expected_port = 9876; +// +// EXPECT_CALL(mockIO, CreateSocket(hidra2::AddressFamilies::INET, hidra2::SocketTypes::STREAM, +// hidra2::SocketProtocols::IP, _)) +// .Times(1) +// .WillOnce( +// DoAll( +// testing::SetArgPointee<3>(hidra2::IOError::UNKNOWN_ERROR), +// Return(-1) +// )); +// +// hidra2::ReceiverError receiver_error; +// receiver.StartListener(expected_address, expected_port, &receiver_error); +// EXPECT_EQ(receiver_error, hidra2::ReceiverError::FAILED_CREATING_SOCKET); +// +// Mock::VerifyAndClearExpectations(&mockIO); +//} +// +//TEST(Receiver, start_Listener__InetBind_fail) { +// hidra2::MockIO mockIO; +// receiver.__set_io(&mockIO); +// +// InSequence sequence; +// +// std::string expected_address = "127.0.0.1"; +// uint16_t expected_port = 9876; +// +// EXPECT_CALL(mockIO, CreateSocket(hidra2::AddressFamilies::INET, hidra2::SocketTypes::STREAM, +// hidra2::SocketProtocols::IP, _)) +// .Times(1) +// .WillOnce( +// DoAll( +// testing::SetArgPointee<3>(hidra2::IOError::NO_ERROR), +// Return(expected_socket_fd) +// )); +// +// EXPECT_CALL(mockIO, InetBind(expected_socket_fd, expected_address, expected_port, _)) +// .Times(1) +// .WillOnce(testing::SetArgPointee<3>(hidra2::IOError::ADDRESS_ALREADY_IN_USE)); +// +// EXPECT_CALL(mockIO, Close(expected_socket_fd)) +// .Times(1); +// +// hidra2::ReceiverError receiver_error; +// receiver.StartListener(expected_address, expected_port, &receiver_error); +// EXPECT_EQ(receiver_error, hidra2::ReceiverError::FAILED_CREATING_SOCKET); +// +// Mock::VerifyAndClearExpectations(&mockIO); +//} +// +//TEST(Receiver, start_Listener__Listen_fail) { +// hidra2::MockIO mockIO; +// receiver.__set_io(&mockIO); +// +// InSequence sequence; +// +// std::string expected_address = "127.0.0.1"; +// uint16_t expected_port = 9876; +// +// EXPECT_CALL(mockIO, CreateSocket(hidra2::AddressFamilies::INET, hidra2::SocketTypes::STREAM, +// hidra2::SocketProtocols::IP, _)) +// .Times(1) +// .WillOnce( +// DoAll( +// testing::SetArgPointee<3>(hidra2::IOError::NO_ERROR), +// Return(expected_socket_fd) +// )); +// +// EXPECT_CALL(mockIO, InetBind(expected_socket_fd, expected_address, expected_port, _)) +// .Times(1) +// .WillOnce(testing::SetArgPointee<3>(hidra2::IOError::NO_ERROR)); +// +// EXPECT_CALL(mockIO, Listen(expected_socket_fd, receiver.kMaxUnacceptedConnectionsBacklog, _)) +// .Times(1) +// .WillOnce(testing::SetArgPointee<2>(hidra2::IOError::BAD_FILE_NUMBER)); +// +// EXPECT_CALL(mockIO, Close(expected_socket_fd, _)) +// .Times(1); +// +// hidra2::ReceiverError receiver_error; +// receiver.StartListener(expected_address, expected_port, &receiver_error); +// EXPECT_EQ(receiver_error, hidra2::ReceiverError::FAILED_CREATING_SOCKET); +// +// Mock::VerifyAndClearExpectations(&mockIO); +//} +// +//TEST(Receiver, start_Listener) { +// hidra2::MockIO mockIO; +// receiver.__set_io(&mockIO); +// +// InSequence sequence; +// +// std::string expected_address = "127.0.0.1"; +// uint16_t expected_port = 9876; +// +// EXPECT_CALL(mockIO, CreateSocket(hidra2::AddressFamilies::INET, hidra2::SocketTypes::STREAM, +// hidra2::SocketProtocols::IP, _)) +// .Times(1) +// .WillOnce( +// DoAll( +// testing::SetArgPointee<3>(hidra2::IOError::NO_ERROR), +// Return(expected_socket_fd) +// )); +// +// EXPECT_CALL(mockIO, InetBind(expected_socket_fd, expected_address, expected_port, _)) +// .Times(1) +// .WillOnce(testing::SetArgPointee<3>(hidra2::IOError::NO_ERROR)); +// +// EXPECT_CALL(mockIO, Listen(expected_socket_fd, receiver.kMaxUnacceptedConnectionsBacklog, _)) +// .Times(1) +// .WillOnce(testing::SetArgPointee<2>(hidra2::IOError::NO_ERROR)); +// +// /** +// * TODO: Since StartListener will start a new thread +// * we need to mock std::thread +// */ +// EXPECT_CALL(mockIO, InetAcceptProxy(expected_socket_fd, _)) +// .Times(1) +// .WillOnce( +// DoAll( +// testing::SetArgPointee<1>(hidra2::IOError::BAD_FILE_NUMBER), +// Return(nullptr) +// )); +// +// +// hidra2::ReceiverError receiver_error; +// receiver.StartListener(expected_address, expected_port, &receiver_error); +// EXPECT_EQ(receiver_error, hidra2::ReceiverError::NO_ERROR); +// +// sleep(1); //Make sure that the thread is running so InetAcceptProxy would work +// +// Mock::VerifyAndClearExpectations(&mockIO); +//} +// +//TEST(Receiver, start_Listener_already_Listening) { +// InSequence sequence; +// +// std::string expected_address = "127.0.0.1"; +// uint16_t expected_port = 9876; +// +// hidra2::ReceiverError receiver_error; +// receiver.StartListener(expected_address, expected_port, &receiver_error); +// EXPECT_EQ(receiver_error, hidra2::ReceiverError::ALREADY_LISTEING); +//} +// +//TEST(Receiver, stop_Listener) { +// hidra2::MockIO mockIO; +// receiver.__set_io(&mockIO); +// +// +// EXPECT_CALL(mockIO, Close(expected_socket_fd)) +// .Times(1) +// .WillOnce( +// Return(0) +// ); +// +// +// hidra2::ReceiverError receiver_error; +// +// receiver.stop_listener(&receiver_error); +// EXPECT_EQ(receiver_error, hidra2::ReceiverError::NO_ERROR); +// +// Mock::VerifyAndClearExpectations(&mockIO); +//} + +} diff --git a/tests/system_io/CMakeLists.txt b/tests/system_io/CMakeLists.txt index de44b3dd3..8a3129b78 100644 --- a/tests/system_io/CMakeLists.txt +++ b/tests/system_io/CMakeLists.txt @@ -4,4 +4,5 @@ add_subdirectory(read_files_in_folder) add_subdirectory(read_file_content) add_subdirectory(ip_tcp_network) add_subdirectory(ip_tcp_network_speedtest) +add_subdirectory(resolve_hostname_to_ip) diff --git a/tests/system_io/resolve_hostname_to_ip/CMakeLists.txt b/tests/system_io/resolve_hostname_to_ip/CMakeLists.txt new file mode 100644 index 000000000..62bd06b7d --- /dev/null +++ b/tests/system_io/resolve_hostname_to_ip/CMakeLists.txt @@ -0,0 +1,22 @@ +set(TARGET_NAME resolve_hostname_to_ip) +set(SOURCE_FILES resolve_hostname_to_ip.cpp) + + +################################ +# Executable and link +################################ +add_executable(${TARGET_NAME} ${SOURCE_FILES} $<TARGET_OBJECTS:common>) + +#Add all necessary common libraries +GET_PROPERTY(HIDRA2_COMMON_IO_LIBRARIES GLOBAL PROPERTY HIDRA2_COMMON_IO_LIBRARIES) +target_link_libraries(${TARGET_NAME} ${HIDRA2_COMMON_IO_LIBRARIES}) + +target_link_libraries(${TARGET_NAME} test_common) +target_include_directories(${TARGET_NAME} PUBLIC ${CMAKE_SOURCE_DIR}/common/cpp/include) +set_target_properties(${TARGET_NAME} PROPERTIES LINKER_LANGUAGE CXX) + +################################ +# Testing +################################ + +add_integration_test(${TARGET_NAME} resolve_hostname_to_ip "") diff --git a/tests/system_io/resolve_hostname_to_ip/resolve_hostname_to_ip.cpp b/tests/system_io/resolve_hostname_to_ip/resolve_hostname_to_ip.cpp new file mode 100644 index 000000000..1a41c284c --- /dev/null +++ b/tests/system_io/resolve_hostname_to_ip/resolve_hostname_to_ip.cpp @@ -0,0 +1,38 @@ +#include <iostream> +#include <system_wrappers/system_io.h> + +#include "testing.h" + +using hidra2::SystemIO; +using hidra2::IOErrors; + +using hidra2::M_AssertEq; +using hidra2::M_AssertTrue; + +SystemIO systemIO; + +void Check(const std::string& expected_ip_address, const std::string& hostname) { + std::cout << "Checking: " << hostname << std::endl; + IOErrors err; + std::string ip_address = systemIO.ResolveHostnameToIp(hostname, &err); + M_AssertEq(expected_ip_address, ip_address); + if(expected_ip_address.empty()) { + M_AssertTrue(IOErrors::kUnableToResolveHostname == err); + return; + } + M_AssertTrue(IOErrors::kNoError == err); +} + +int main(int argc, char* argv[]) { + Check("127.0.0.1", "localhost"); + Check("8.8.8.8", "google-public-dns-a.google.com"); + Check("8.8.4.4", "google-public-dns-b.google.com"); + Check("4.2.2.1", "a.resolvers.level3.net"); + Check("4.2.2.2", "b.resolvers.level3.net"); + Check("4.2.2.3", "c.resolvers.level3.net"); + Check("4.2.2.4", "d.resolvers.level3.net"); + + Check("", "some-address-that-does-not-exists.ff"); + Check("", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.ff"); + return 0; +} -- GitLab