Line data Source code
1 : /**
2 : *
3 : * @file get_ips_test.cxx Utility functions Unit Tests
4 : *
5 : * This is part of the DUNE DAQ Application Framework, copyright 2020.
6 : * Licensing/copyright details are in the COPYING file that you should have
7 : * received with this code.
8 : */
9 :
10 : #include "utilities/Issues.hpp"
11 : #include "utilities/get_ips.hpp"
12 :
13 : #include "logging/Logging.hpp"
14 :
15 : #define BOOST_TEST_MODULE get_ips_test // NOLINT
16 :
17 : #include "boost/test/unit_test.hpp"
18 :
19 : #include <chrono>
20 : #include <filesystem>
21 : #include <fstream>
22 : #include <regex>
23 :
24 : using namespace dunedaq::utilities;
25 :
26 2 : BOOST_AUTO_TEST_CASE(HostnameLookup)
27 : {
28 3 : TLOG() << "Test HostnameLookup BEGIN";
29 1 : auto res = get_hostname_ips("127.0.0.1");
30 1 : BOOST_REQUIRE_GE(res.size(), 1);
31 1 : BOOST_REQUIRE_EQUAL(res[0], "127.0.0.1");
32 :
33 1 : res = get_hostname_ips("localhost");
34 1 : BOOST_REQUIRE_GE(res.size(), 1);
35 1 : BOOST_REQUIRE(res[0] == "127.0.0.1" || res[0] == "::1");
36 :
37 1 : res = get_hostname_ips("cern.ch");
38 1 : BOOST_REQUIRE_GT(res.size(), 0);
39 :
40 : // Ports are not accepted in input
41 1 : res = get_hostname_ips("127.0.0.1:1234");
42 1 : BOOST_REQUIRE_EQUAL(res.size(), 0);
43 :
44 1 : res = get_hostname_ips("localhost:1234");
45 1 : BOOST_REQUIRE_EQUAL(res.size(), 0);
46 :
47 : // ZMQ URIs are not accepted as input
48 1 : res = get_hostname_ips("tcp://localhost:1234");
49 1 : BOOST_REQUIRE_EQUAL(res.size(), 0);
50 3 : TLOG() << "Test HostnameLookup END";
51 1 : }
52 :
53 2 : BOOST_AUTO_TEST_CASE(InterfaceLookup)
54 : {
55 1 : std::string loopback_if_name;
56 1 : struct ifaddrs* ifaddr = nullptr;
57 1 : getifaddrs(&ifaddr);
58 :
59 1 : for (auto ifa = ifaddr; ifa != nullptr; ifa = ifa->ifa_next) {
60 1 : if (ifa->ifa_name[0] == 'l') {
61 1 : loopback_if_name = std::string(ifa->ifa_name);
62 1 : break;
63 : }
64 : }
65 :
66 1 : freeifaddrs(ifaddr);
67 :
68 1 : auto res = get_interface_ip(loopback_if_name);
69 1 : BOOST_REQUIRE_EQUAL(res, "127.0.0.1");
70 :
71 4 : BOOST_REQUIRE_EXCEPTION(get_interface_ip("thisifdoesntexist", true), InterfaceNotFound, [](InterfaceNotFound const&){return true;});
72 1 : res = get_interface_ip("thisifdoesntexist");
73 1 : BOOST_REQUIRE(res.size() > 0);
74 1 : }
|