1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
|
// NOLINTBEGIN(*)
#include "cxx_common.h"
#include <tango/internal/net.h>
using namespace Tango::detail;
#undef SUITE_NAME
#define SUITE_NAME CommonMiscTestSuite
class SUITE_NAME : public CxxTest::TestSuite
{
protected:
public:
SUITE_NAME()
{
//
// Arguments check -------------------------------------------------
//
CxxTest::TangoPrinter::validate_args();
//
// Initialization --------------------------------------------------
//
}
virtual ~SUITE_NAME() = default;
static SUITE_NAME *createSuite()
{
return new SUITE_NAME();
}
static void destroySuite(SUITE_NAME *suite)
{
delete suite;
}
//
// Tests -------------------------------------------------------
//
void test_is_ip_address()
{
// IPv4
TS_ASSERT(is_ip_address("127.0.0.1"));
TS_ASSERT(is_ip_address("1.1.1.1"));
// IPv6 is not supported
TS_ASSERT(!is_ip_address("::1"));
// random strings
TS_ASSERT(!is_ip_address("example_dot_org"));
// hostname
TS_ASSERT(!is_ip_address("example.org"));
// we need a non empty string
TS_ASSERT_THROWS_ASSERT(is_ip_address(""),
Tango::DevFailed & e,
TS_ASSERT_EQUALS(std::string(e.errors[0].reason.in()), API_InvalidArgs));
}
void test_resolve_hostname_address()
{
std::vector<std::string> results;
TS_ASSERT_THROWS_NOTHING(results = resolve_hostname_address("localhost"));
TS_ASSERT(results.size() >= 1);
TS_ASSERT(std::find(std::cbegin(results), std::cend(results), "127.0.0.1") != std::end(results));
// IPv4 only
TS_ASSERT_THROWS_NOTHING(results = resolve_hostname_address("ip6-loopback"));
TS_ASSERT(results.size() >= 1);
TS_ASSERT(std::find(std::cbegin(results), std::cend(results), "127.0.0.1") != std::end(results));
TS_ASSERT(std::find(std::cbegin(results), std::cend(results), "::1") == std::end(results));
// invalid hostname
TS_ASSERT_THROWS_ASSERT(auto result = resolve_hostname_address("I_DONT_EXIST..com"),
Tango::DevFailed & e,
TS_ASSERT_EQUALS(std::string(e.errors[0].reason.in()), API_InvalidArgs));
// unresolvable hostname
TS_ASSERT_THROWS_ASSERT(auto result =
resolve_hostname_address("I_DONT_EXIST.AT.NON_EXISTING_SUBDOMAIN.byte-physics.de"),
Tango::DevFailed & e,
TS_ASSERT_EQUALS(std::string(e.errors[0].reason.in()), API_InvalidArgs));
// we need a non empty string
TS_ASSERT_THROWS_ASSERT(auto result = resolve_hostname_address(""),
Tango::DevFailed & e,
TS_ASSERT_EQUALS(std::string(e.errors[0].reason.in()), API_InvalidArgs));
}
void test_get_port_from_endpoint()
{
// empty
TS_ASSERT_THROWS_ASSERT(auto port = get_port_from_endpoint(""),
Tango::DevFailed & e,
TS_ASSERT_EQUALS(std::string(e.errors[0].reason.in()), API_InvalidArgs));
// invalid format without :
TS_ASSERT_THROWS_ASSERT(auto port = get_port_from_endpoint("a_b"),
Tango::DevFailed & e,
TS_ASSERT_EQUALS(std::string(e.errors[0].reason.in()), API_InvalidArgs));
// invalid format as : is the last char
TS_ASSERT_THROWS_ASSERT(auto port = get_port_from_endpoint("b:"),
Tango::DevFailed & e,
TS_ASSERT_EQUALS(std::string(e.errors[0].reason.in()), API_InvalidArgs));
TS_ASSERT_EQUALS(get_port_from_endpoint("a:b"), "b");
TS_ASSERT_EQUALS(get_port_from_endpoint("tcp://a:b"), "b");
}
void test_qualify_host_address()
{
// empty name
TS_ASSERT_THROWS_ASSERT(qualify_host_address("", "b"),
Tango::DevFailed & e,
TS_ASSERT_EQUALS(std::string(e.errors[0].reason.in()), API_InvalidArgs));
// empty port
TS_ASSERT_THROWS_ASSERT(qualify_host_address("a", ""),
Tango::DevFailed & e,
TS_ASSERT_EQUALS(std::string(e.errors[0].reason.in()), API_InvalidArgs));
TS_ASSERT_EQUALS(qualify_host_address("a", "b"), "tcp://a:b");
}
void test_split_endpoint()
{
{
// clears pass-by-reference-parameters
std::string name = "abcd";
std::string port = "efgh";
TS_ASSERT_THROWS_ASSERT(split_endpoint("tcp://a:", name, port),
Tango::DevFailed & e,
TS_ASSERT_EQUALS(std::string(e.errors[0].reason.in()), API_InvalidArgs));
TS_ASSERT(name.empty());
TS_ASSERT(port.empty());
}
{
// requires port spec
std::string name, port;
TS_ASSERT_THROWS_ASSERT(split_endpoint("tcp://ab", name, port),
Tango::DevFailed & e,
TS_ASSERT_EQUALS(std::string(e.errors[0].reason.in()), API_InvalidArgs));
TS_ASSERT(name.empty());
TS_ASSERT(port.empty());
}
{
// too short
std::string name, port;
TS_ASSERT_THROWS_ASSERT(split_endpoint("ab", name, port),
Tango::DevFailed & e,
TS_ASSERT_EQUALS(std::string(e.errors[0].reason.in()), API_InvalidArgs));
TS_ASSERT(name.empty());
TS_ASSERT(port.empty());
}
{
// still too short
std::string name, port;
TS_ASSERT_THROWS_ASSERT(split_endpoint("tcp://", name, port),
Tango::DevFailed & e,
TS_ASSERT_EQUALS(std::string(e.errors[0].reason.in()), API_InvalidArgs));
TS_ASSERT(name.empty());
TS_ASSERT(port.empty());
}
{
std::string name, port;
TS_ASSERT_THROWS_NOTHING(split_endpoint("tcp://a:b", name, port));
TS_ASSERT_EQUALS(name, "a");
TS_ASSERT_EQUALS(port, "b");
}
}
void test_parse_hostname_from_CORBA_URI()
{
// empty
TS_ASSERT_THROWS_ASSERT(auto result = parse_hostname_from_CORBA_URI(""),
Tango::DevFailed & e,
TS_ASSERT_EQUALS(std::string(e.errors[0].reason.in()), API_InvalidArgs));
// only one colon
TS_ASSERT_THROWS_ASSERT(auto result = parse_hostname_from_CORBA_URI(":"),
Tango::DevFailed & e,
TS_ASSERT_EQUALS(std::string(e.errors[0].reason.in()), API_InvalidArgs));
// only two colons
TS_ASSERT_THROWS_ASSERT(auto result = parse_hostname_from_CORBA_URI("::"),
Tango::DevFailed & e,
TS_ASSERT_EQUALS(std::string(e.errors[0].reason.in()), API_InvalidArgs));
// empty hostname
TS_ASSERT_EQUALS(parse_hostname_from_CORBA_URI(":::"), "");
// bare minimum
TS_ASSERT_EQUALS(parse_hostname_from_CORBA_URI("::abcd:"), "abcd");
// full entry with hostname
TS_ASSERT_EQUALS(parse_hostname_from_CORBA_URI("giop:tcp:myhost:12345"), "myhost");
// full entry with IPv4 address
TS_ASSERT_EQUALS(parse_hostname_from_CORBA_URI("giop:tcp:0.0.0.0:12345"), "0.0.0.0");
}
};
// NOLINTEND(*)
|