ensuring no duplicate rx hostname port combo (#604)

* rx_hostname and port combo to one, or hostname to all, or a vector of hostnames and ports, ignoring none or empty, then verifying no duplicates for the host port combo including from shared memory

* extracted function for rx_hostname (#694)

* c++14 revert

* unique hostname-port combo for port, hostname, rx_tcpport (#696)

* verify unique combo for rx_port as well

* check unique hostname-port combo also when setting control port, hostname, rx_hostname and rx_tcpport

---------

Co-authored-by: Erik Fröjdh <erik.frojdh@gmail.com>
Co-authored-by: Erik Frojdh <erik.frojdh@psi.ch>
This commit is contained in:
Dhanya Thattil
2023-03-20 12:30:12 +01:00
committed by GitHub
parent c9215a6d9b
commit b67c6dea08
11 changed files with 228 additions and 44 deletions

View File

@ -10,6 +10,7 @@
#include <string>
#include <type_traits>
#include <vector>
// #include <utility> //support pair in vectors
#include "sls/TypeTraits.h"
@ -148,6 +149,12 @@ Squash(const Container &c, typename Container::value_type default_value = {}) {
return default_value;
}
template <typename Container> bool hasDuplicates(Container c) {
std::sort(c.begin(), c.end());
auto pos = std::adjacent_find(c.begin(), c.end());
return pos != c.end(); // if we found something there are duplicates
}
template <typename T>
typename std::enable_if<is_container<T>::value, bool>::type
removeDuplicates(T &c) {

View File

@ -5,6 +5,7 @@
#include <cassert>
#include <cstring>
#include <string>
#include <utility>
#include <vector>
namespace sls {
@ -59,4 +60,6 @@ bool is_int(const std::string &s);
bool replace_first(std::string *s, const std::string &substr,
const std::string &repl);
std::pair<std::string, int> ParseHostPort(const std::string &s);
} // namespace sls

View File

@ -50,4 +50,17 @@ bool replace_first(std::string *s, const std::string &substr,
return false;
}
std::pair<std::string, int> ParseHostPort(const std::string &s) {
// TODO deal with to many :, port not there?
// no port return hostname as is and port as 0
std::string host;
int port{0};
auto res = split(s, ':');
host = res[0];
if (res.size() > 1) {
port = std::stoi(res[1]);
}
return std::make_pair(host, port);
}
}; // namespace sls

View File

@ -136,6 +136,23 @@ TEST_CASE("compare a vector of arrays", "[support]") {
CHECK(minusOneIfDifferent(vec1) == arr);
}
TEST_CASE("check if vector has duplicates") {
std::vector<int> vec{1, 0, 2, 5, 3, 1, 8, 6};
REQUIRE(hasDuplicates(vec) == true);
}
TEST_CASE("check for duplicates in vector of pairs") {
std::vector<std::pair<std::string, int>> vec;
vec.emplace_back("localhost", 1954);
REQUIRE(hasDuplicates(vec) == false);
vec.emplace_back("localhost", 1800);
REQUIRE(hasDuplicates(vec) == false);
vec.emplace_back("localhost", 1954);
REQUIRE(hasDuplicates(vec) == true);
}
TEST_CASE("remove duplicates from vector") {
std::vector<int> v{5, 6, 5, 3};
auto r = removeDuplicates(v);

View File

@ -108,6 +108,21 @@ TEST_CASE("replace --help") {
REQUIRE(s == "list");
}
TEST_CASE("port host") {
std::string hostport = "localhost:1954";
auto res = ParseHostPort(hostport);
REQUIRE(res.first == "localhost");
REQUIRE(res.second == 1954);
}
TEST_CASE("port missing") {
// TODO! is this the intended result?
std::string host = "localhost";
auto res = ParseHostPort(host);
REQUIRE(res.first == "localhost");
REQUIRE(res.second == 0);
}
// TEST_CASE("concat things not being strings")
} // namespace sls