Compare commits

...

3 Commits

Author SHA1 Message Date
Teo Mrnjavac
227a302903 Avoid boost::uuids::entropy_error on some systems 2018-11-05 13:18:08 +01:00
Alexey Rybalchenko
bd899a2806 Add test for channel validation 2018-11-01 15:43:40 +01:00
Alexey Rybalchenko
0b199e779a Add test for interface IP detection tools 2018-11-01 15:43:40 +01:00
9 changed files with 188 additions and 69 deletions

View File

@@ -4,6 +4,7 @@ Eulisse, Giulio
Karabowicz, Radoslaw
Kretz, Matthias <kretz@kde.org>
Krzewicki, Mikolaj
Mrnjavac, Teo <teo.m@cern.ch>
Neskovic, Gvozden
Richter, Matthias
Uhlig, Florian

View File

@@ -399,8 +399,7 @@ try {
}
// validate socket type
const string socketTypeNames[] = { "sub", "pub", "pull", "push", "req", "rep", "xsub", "xpub", "dealer", "router", "pair" };
const set<string> socketTypes(socketTypeNames, socketTypeNames + sizeof(socketTypeNames) / sizeof(string));
const set<string> socketTypes{ "sub", "pub", "pull", "push", "req", "rep", "xsub", "xpub", "dealer", "router", "pair" };
if (socketTypes.find(fType) == socketTypes.end())
{
ss << "INVALID";
@@ -431,8 +430,7 @@ try {
else
{
// we don't have a method modifier, check if the default method is set
const string socketMethodNames[] = { "bind", "connect" };
const set<string> socketMethods(socketMethodNames, socketMethodNames + sizeof(socketMethodNames) / sizeof(string));
const set<string> socketMethods{ "bind", "connect" };
if (socketMethods.find(fMethod) == socketMethods.end())
{
ss << "INVALID";
@@ -482,7 +480,7 @@ try {
else if (address.compare(0, 8, "verbs://") == 0)
{
// check if IPC address is not empty
string addressString = address.substr(9);
string addressString = address.substr(8);
if (addressString == "")
{
ss << "INVALID";

View File

@@ -19,10 +19,9 @@
#include "FairMQParser.h"
#include "FairMQSuboptParser.h"
#include "tools/Unique.h"
#include <boost/algorithm/string.hpp> // join/split
#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_generators.hpp>
#include <boost/uuid/uuid_io.hpp>
#include <algorithm>
#include <iomanip>
@@ -227,7 +226,7 @@ void FairMQProgOptions::ParseCmdLine(const int argc, char const* const* argv, bo
void FairMQProgOptions::ParseDefaults()
{
vector<string> emptyArgs = {"dummy", "--id", boost::uuids::to_string(boost::uuids::random_generator()())};
vector<string> emptyArgs = {"dummy", "--id", tools::Uuid()};
vector<const char*> argv(emptyArgs.size());

View File

@@ -28,6 +28,7 @@
#include <iostream>
#include <array>
#include <exception>
#include <stdexcept>
#include <algorithm>
using namespace std;
@@ -40,54 +41,52 @@ namespace tools
{
// returns a map with network interface names as keys and their IP addresses as values
int getHostIPs(map<string, string>& addressMap)
map<string, string> getHostIPs()
{
map<string, string> addressMap;
struct ifaddrs *ifaddr, *ifa;
int s;
char host[NI_MAXHOST];
if (getifaddrs(&ifaddr) == -1)
{
if (getifaddrs(&ifaddr) == -1) {
perror("getifaddrs");
return -1;
throw runtime_error("getifaddrs failed");
}
for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next)
{
if (ifa->ifa_addr == NULL)
{
for (ifa = ifaddr; ifa != nullptr; ifa = ifa->ifa_next) {
if (ifa->ifa_addr == nullptr) {
continue;
}
if (ifa->ifa_addr->sa_family == AF_INET)
{
s = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in), host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
if (s != 0)
{
if (ifa->ifa_addr->sa_family == AF_INET) {
s = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in), host, NI_MAXHOST, nullptr, 0, NI_NUMERICHOST);
if (s != 0) {
cout << "getnameinfo() failed: " << gai_strerror(s) << endl;
return -1;
throw runtime_error("getnameinfo() failed");
}
addressMap.insert(pair<string, string>(ifa->ifa_name, host));
}
}
freeifaddrs(ifaddr);
return 0;
return addressMap;
}
// get IP address of a given interface name
string getInterfaceIP(const string& interface)
{
map<string, string> IPs;
getHostIPs(IPs);
if (IPs.count(interface))
{
return IPs[interface];
}
else
{
LOG(error) << "Could not find provided network interface: \"" << interface << "\"!, exiting.";
try {
auto IPs = getHostIPs();
if (IPs.count(interface)) {
return IPs[interface];
} else {
LOG(error) << "Could not find provided network interface: \"" << interface << "\"!, exiting.";
return "";
}
} catch (runtime_error& re) {
cout << "could not get interface IP: " << re.what();
return "";
}
}
@@ -104,28 +103,22 @@ string getDefaultRouteNetworkInterface()
unique_ptr<FILE, decltype(pclose) *> file(popen("ip route | grep default | cut -d \" \" -f 5 | head -n 1", "r"), pclose);
#endif
if (!file)
{
if (!file) {
LOG(error) << "Could not detect default route network interface name - popen() failed!";
return "";
}
while (!feof(file.get()))
{
if (fgets(buffer.data(), 128, file.get()) != NULL)
{
while (!feof(file.get())) {
if (fgets(buffer.data(), 128, file.get()) != nullptr) {
interfaceName += buffer.data();
}
}
boost::algorithm::trim(interfaceName);
if (interfaceName == "")
{
if (interfaceName == "") {
LOG(error) << "Could not detect default route network interface name";
}
else
{
} else {
LOG(debug) << "Detected network interface name for the default route: " << interfaceName;
}
@@ -134,30 +127,8 @@ string getDefaultRouteNetworkInterface()
string getIpFromHostname(const string& hostname)
{
try {
namespace bai = boost::asio::ip;
boost::asio::io_service ios;
bai::tcp::resolver resolver(ios);
bai::tcp::resolver::query query(hostname, "");
bai::tcp::resolver::iterator end;
auto it = find_if(static_cast<bai::basic_resolver_iterator<bai::tcp>>(resolver.resolve(query)), end, [](const bai::tcp::endpoint& ep) {
return ep.address().is_v4();
});
if (it != end) {
stringstream ss;
ss << static_cast<bai::tcp::endpoint>(*it).address();
return ss.str();
}
LOG(warn) << "could not find ipv4 address for hostname '" << hostname << "'";
return "";
} catch (exception& e) {
LOG(error) << "could not resolve hostname '" << hostname << "', reason: " << e.what();
return "";
}
boost::asio::io_service ios;
return getIpFromHostname(hostname, ios);
}
string getIpFromHostname(const string& hostname, boost::asio::io_service& ios)

View File

@@ -32,7 +32,7 @@ namespace tools
{
// returns a map with network interface names as keys and their IP addresses as values
int getHostIPs(std::map<std::string, std::string>& addressMap);
std::map<std::string, std::string> getHostIPs();
// get IP address of a given interface name
std::string getInterfaceIP(const std::string& interface);

View File

@@ -8,6 +8,10 @@
#include <fairmq/tools/Unique.h>
// We have to force boost::uuids to rely on /dev/*random instead of getrandom(2) or getentropy(3)
// otherwise on some systems we'd get boost::uuids::entropy_error
#define BOOST_UUID_RANDOM_PROVIDER_FORCE_POSIX
#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_generators.hpp>
#include <boost/uuid/uuid_io.hpp>

View File

@@ -198,6 +198,28 @@ add_testsuite(FairMQ.StateMachine
TIMEOUT 10
)
add_testsuite(FairMQ.Tools
SOURCES
${CMAKE_CURRENT_BINARY_DIR}/runner.cxx
tools/_network.cxx
LINKS FairMQ
INCLUDES ${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_BINARY_DIR}
TIMEOUT 10
)
add_testsuite(FairMQ.Channel
SOURCES
${CMAKE_CURRENT_BINARY_DIR}/runner.cxx
channel/_channel.cxx
LINKS FairMQ
INCLUDES ${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_BINARY_DIR}
TIMEOUT 10
)
add_testsuite(FairMQ.Transport
SOURCES
${CMAKE_CURRENT_BINARY_DIR}/runner.cxx

96
test/channel/_channel.cxx Normal file
View File

@@ -0,0 +1,96 @@
/********************************************************************************
* Copyright (C) 2018 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
* *
* This software is distributed under the terms of the *
* GNU Lesser General Public Licence (LGPL) version 3, *
* copied verbatim in the file "LICENSE" *
********************************************************************************/
#include <FairMQChannel.h>
#include <gtest/gtest.h>
#include <string>
namespace
{
using namespace std;
using namespace fair::mq;
TEST(Channel, Validation)
{
FairMQChannel channel;
ASSERT_THROW(channel.ValidateChannel(), FairMQChannel::ChannelConfigurationError);
channel.UpdateType("pair");
ASSERT_EQ(channel.ValidateChannel(), false);
ASSERT_EQ(channel.IsValid(), false);
channel.UpdateAddress("bla");
ASSERT_THROW(channel.ValidateChannel(), FairMQChannel::ChannelConfigurationError);
channel.UpdateMethod("connect");
ASSERT_EQ(channel.ValidateChannel(), false);
ASSERT_EQ(channel.IsValid(), false);
channel.UpdateAddress("ipc://");
ASSERT_EQ(channel.ValidateChannel(), false);
ASSERT_EQ(channel.IsValid(), false);
channel.UpdateAddress("verbs://");
ASSERT_EQ(channel.ValidateChannel(), false);
ASSERT_EQ(channel.IsValid(), false);
channel.UpdateAddress("inproc://");
ASSERT_EQ(channel.ValidateChannel(), false);
ASSERT_EQ(channel.IsValid(), false);
channel.UpdateAddress("tcp://");
ASSERT_EQ(channel.ValidateChannel(), false);
ASSERT_EQ(channel.IsValid(), false);
channel.UpdateAddress("tcp://localhost:5555");
ASSERT_EQ(channel.ValidateChannel(), true);
ASSERT_EQ(channel.IsValid(), true);
channel.UpdateSndBufSize(-1);
ASSERT_THROW(channel.ValidateChannel(), FairMQChannel::ChannelConfigurationError);
channel.UpdateSndBufSize(1000);
ASSERT_NO_THROW(channel.ValidateChannel());
channel.UpdateRcvBufSize(-1);
ASSERT_THROW(channel.ValidateChannel(), FairMQChannel::ChannelConfigurationError);
channel.UpdateRcvBufSize(1000);
ASSERT_NO_THROW(channel.ValidateChannel());
channel.UpdateSndKernelSize(-1);
ASSERT_THROW(channel.ValidateChannel(), FairMQChannel::ChannelConfigurationError);
channel.UpdateSndKernelSize(1000);
ASSERT_NO_THROW(channel.ValidateChannel());
channel.UpdateRcvKernelSize(-1);
ASSERT_THROW(channel.ValidateChannel(), FairMQChannel::ChannelConfigurationError);
channel.UpdateRcvKernelSize(1000);
ASSERT_NO_THROW(channel.ValidateChannel());
channel.UpdateRateLogging(-1);
ASSERT_THROW(channel.ValidateChannel(), FairMQChannel::ChannelConfigurationError);
channel.UpdateRateLogging(1);
ASSERT_NO_THROW(channel.ValidateChannel());
FairMQChannel channel2 = channel;
ASSERT_NO_THROW(channel2.ValidateChannel());
ASSERT_EQ(channel2.ValidateChannel(), true);
ASSERT_EQ(channel2.IsValid(), true);
ASSERT_EQ(channel2.ValidateChannel(), true);
channel2.UpdateChannelName("Kanal");
ASSERT_EQ(channel2.GetChannelName(), "Kanal");
channel2.ResetChannel();
ASSERT_EQ(channel2.IsValid(), false);
ASSERT_EQ(channel2.ValidateChannel(), true);
}
} /* namespace */

28
test/tools/_network.cxx Normal file
View File

@@ -0,0 +1,28 @@
/********************************************************************************
* Copyright (C) 2018 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
* *
* This software is distributed under the terms of the *
* GNU Lesser General Public Licence (LGPL) version 3, *
* copied verbatim in the file "LICENSE" *
********************************************************************************/
#include <gtest/gtest.h>
#include <fairmq/Tools.h>
#include <string>
namespace
{
using namespace std;
using namespace fair::mq;
TEST(Tools, Network)
{
string interface = fair::mq::tools::getDefaultRouteNetworkInterface();
EXPECT_NE(interface, "");
string interfaceIP = fair::mq::tools::getInterfaceIP(interface);
EXPECT_NE(interfaceIP, "");
}
} /* namespace */