| 1 | // |
| 2 | // NetworkInterfaceTest.cpp |
| 3 | // |
| 4 | // Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH. |
| 5 | // and Contributors. |
| 6 | // |
| 7 | // SPDX-License-Identifier: BSL-1.0 |
| 8 | // |
| 9 | |
| 10 | |
| 11 | #include "NetworkInterfaceTest.h" |
| 12 | |
| 13 | |
| 14 | #ifdef POCO_NET_HAS_INTERFACE |
| 15 | |
| 16 | |
| 17 | #include "Poco/CppUnit/TestCaller.h" |
| 18 | #include "Poco/CppUnit/TestSuite.h" |
| 19 | #include "Poco/Net/NetworkInterface.h" |
| 20 | #include "Poco/Net/IPAddress.h" |
| 21 | #include <iostream> |
| 22 | #include <iomanip> |
| 23 | |
| 24 | |
| 25 | using Poco::Net::NetworkInterface; |
| 26 | using Poco::Net::IPAddress; |
| 27 | using Poco::NotFoundException; |
| 28 | |
| 29 | |
| 30 | NetworkInterfaceTest::NetworkInterfaceTest(const std::string& name): CppUnit::TestCase(name) |
| 31 | { |
| 32 | } |
| 33 | |
| 34 | |
| 35 | NetworkInterfaceTest::~NetworkInterfaceTest() |
| 36 | { |
| 37 | } |
| 38 | |
| 39 | |
| 40 | void NetworkInterfaceTest::testMap() |
| 41 | { |
| 42 | try |
| 43 | { |
| 44 | NetworkInterface::Map m = NetworkInterface::map(false, false); |
| 45 | assertTrue (!m.empty()); |
| 46 | for (NetworkInterface::Map::const_iterator it = m.begin(); it != m.end(); ++it) |
| 47 | { |
| 48 | std::cout << std::endl << "=============" << std::endl; |
| 49 | |
| 50 | std::cout << "Index: " << it->second.index() << std::endl; |
| 51 | std::cout << "Name: " << it->second.name() << std::endl; |
| 52 | std::cout << "DisplayName: " << it->second.displayName() << std::endl; |
| 53 | std::cout << "Status: " << (it->second.isUp() ? "Up" : "Down" ) << std::endl; |
| 54 | |
| 55 | NetworkInterface::MACAddress mac(it->second.macAddress()); |
| 56 | if (!mac.empty() && (it->second.type() != NetworkInterface::NI_TYPE_SOFTWARE_LOOPBACK)) |
| 57 | std::cout << "MAC Address: (" << it->second.type() << ") " << mac << std::endl; |
| 58 | |
| 59 | typedef NetworkInterface::AddressList List; |
| 60 | const List& ipList = it->second.addressList(); |
| 61 | List::const_iterator ipIt = ipList.begin(); |
| 62 | List::const_iterator ipEnd = ipList.end(); |
| 63 | for (int counter = 0; ipIt != ipEnd; ++ipIt, ++counter) |
| 64 | { |
| 65 | std::cout << std::endl << "----------" << std::endl; |
| 66 | std::cout << "Address " << counter << std::endl; |
| 67 | std::cout << "----------" << std::endl; |
| 68 | std::cout << "Address: " << ipIt->get<NetworkInterface::IP_ADDRESS>() << std::endl; |
| 69 | IPAddress addr = ipIt->get<NetworkInterface::SUBNET_MASK>(); |
| 70 | if (!addr.isWildcard()) std::cout << "Subnet: " << addr << " (/" << addr.prefixLength() << ")" << std::endl; |
| 71 | addr = ipIt->get<NetworkInterface::BROADCAST_ADDRESS>(); |
| 72 | if (!addr.isWildcard()) std::cout << "Broadcast: " << addr << std::endl; |
| 73 | } |
| 74 | |
| 75 | std::cout << "=============" << std::endl << std::endl; |
| 76 | } |
| 77 | } |
| 78 | catch(Poco::NotImplementedException e) |
| 79 | { |
| 80 | #if POCO_OS != POCO_OS_ANDROID |
| 81 | throw e; |
| 82 | #endif |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | |
| 87 | void NetworkInterfaceTest::testList() |
| 88 | { |
| 89 | try |
| 90 | { |
| 91 | NetworkInterface::List list = NetworkInterface::list(false, false); |
| 92 | assertTrue (!list.empty()); |
| 93 | for (NetworkInterface::List::const_iterator it = list.begin(); it != list.end(); ++it) |
| 94 | { |
| 95 | std::cout << std::endl << "==============" << std::endl; |
| 96 | |
| 97 | std::cout << "Index: " << it->index() << std::endl; |
| 98 | std::cout << "Name: " << it->name() << std::endl; |
| 99 | std::cout << "DisplayName: " << it->displayName() << std::endl; |
| 100 | std::cout << "Status: " << (it->isUp() ? "Up" : "Down" ) << std::endl; |
| 101 | |
| 102 | NetworkInterface::MACAddress mac(it->macAddress()); |
| 103 | if (!mac.empty() && (it->type() != NetworkInterface::NI_TYPE_SOFTWARE_LOOPBACK)) |
| 104 | std::cout << "MAC Address: (" << it->type() << ") " << mac << std::endl; |
| 105 | |
| 106 | typedef NetworkInterface::AddressList AddrList; |
| 107 | const AddrList& ipList = it->addressList(); |
| 108 | AddrList::const_iterator ipIt = ipList.begin(); |
| 109 | AddrList::const_iterator ipEnd = ipList.end(); |
| 110 | for (int counter = 0; ipIt != ipEnd; ++ipIt, ++counter) |
| 111 | { |
| 112 | std::cout << "IP Address: " << ipIt->get<NetworkInterface::IP_ADDRESS>() << std::endl; |
| 113 | IPAddress addr = ipIt->get<NetworkInterface::SUBNET_MASK>(); |
| 114 | if (!addr.isWildcard()) std::cout << "Subnet: " << ipIt->get<NetworkInterface::SUBNET_MASK>() << " (/" << ipIt->get<NetworkInterface::SUBNET_MASK>().prefixLength() << ")" << std::endl; |
| 115 | addr = ipIt->get<NetworkInterface::BROADCAST_ADDRESS>(); |
| 116 | if (!addr.isWildcard()) std::cout << "Broadcast: " << ipIt->get<NetworkInterface::BROADCAST_ADDRESS>() << std::endl; |
| 117 | } |
| 118 | |
| 119 | std::cout << "==============" << std::endl << std::endl; |
| 120 | } |
| 121 | } |
| 122 | catch(Poco::NotImplementedException e) |
| 123 | { |
| 124 | #if POCO_OS != POCO_OS_ANDROID |
| 125 | throw e; |
| 126 | #endif |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | |
| 131 | void NetworkInterfaceTest::testForName() |
| 132 | { |
| 133 | try |
| 134 | { |
| 135 | NetworkInterface::Map map = NetworkInterface::map(); |
| 136 | for (NetworkInterface::Map::const_iterator it = map.begin(); it != map.end(); ++it) |
| 137 | { |
| 138 | NetworkInterface ifc = NetworkInterface::forName(it->second.name()); |
| 139 | assertTrue (ifc.name() == it->second.name()); |
| 140 | } |
| 141 | } |
| 142 | catch(Poco::NotImplementedException e) |
| 143 | { |
| 144 | #if POCO_OS != POCO_OS_ANDROID |
| 145 | throw e; |
| 146 | #endif |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | |
| 151 | void NetworkInterfaceTest::testForAddress() |
| 152 | { |
| 153 | try |
| 154 | { |
| 155 | NetworkInterface::Map map = NetworkInterface::map(); |
| 156 | for (NetworkInterface::Map::const_iterator it = map.begin(); it != map.end(); ++it) |
| 157 | { |
| 158 | // not all interfaces have IP configured |
| 159 | if (it->second.addressList().empty()) continue; |
| 160 | |
| 161 | if (it->second.supportsIPv4()) |
| 162 | { |
| 163 | NetworkInterface ifc = NetworkInterface::forAddress(it->second.firstAddress(IPAddress::IPv4)); |
| 164 | assertTrue (ifc.firstAddress(IPAddress::IPv4) == it->second.firstAddress(IPAddress::IPv4)); |
| 165 | |
| 166 | IPAddress addr(IPAddress::IPv4); |
| 167 | assertTrue (addr.isWildcard()); |
| 168 | it->second.firstAddress(addr, IPAddress::IPv4); |
| 169 | assertTrue (!addr.isWildcard()); |
| 170 | } |
| 171 | else |
| 172 | { |
| 173 | try |
| 174 | { |
| 175 | it->second.firstAddress(IPAddress::IPv4); |
| 176 | fail ("must throw" ); |
| 177 | } |
| 178 | catch (NotFoundException&) { } |
| 179 | |
| 180 | IPAddress addr(IPAddress::IPv4); |
| 181 | assertTrue (addr.isWildcard()); |
| 182 | it->second.firstAddress(addr, IPAddress::IPv4); |
| 183 | assertTrue (addr.isWildcard()); |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | catch(Poco::NotImplementedException e) |
| 188 | { |
| 189 | #if POCO_OS != POCO_OS_ANDROID |
| 190 | throw e; |
| 191 | #endif |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | |
| 196 | void NetworkInterfaceTest::testForIndex() |
| 197 | { |
| 198 | try |
| 199 | { |
| 200 | NetworkInterface::Map map = NetworkInterface::map(); |
| 201 | for (NetworkInterface::Map::const_iterator it = map.begin(); it != map.end(); ++it) |
| 202 | { |
| 203 | NetworkInterface ifc = NetworkInterface::forIndex(it->second.index()); |
| 204 | assertTrue (ifc.index() == it->second.index()); |
| 205 | } |
| 206 | } |
| 207 | catch(Poco::NotImplementedException e) |
| 208 | { |
| 209 | #if POCO_OS != POCO_OS_ANDROID |
| 210 | throw e; |
| 211 | #endif |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | |
| 216 | void NetworkInterfaceTest::testMapIpOnly() |
| 217 | { |
| 218 | try |
| 219 | { |
| 220 | NetworkInterface::Map m = NetworkInterface::map(true, false); |
| 221 | assertTrue (!m.empty()); |
| 222 | |
| 223 | std::cout << std::endl; |
| 224 | for (NetworkInterface::Map::const_iterator it = m.begin(); it != m.end(); ++it) |
| 225 | { |
| 226 | assertTrue (it->second.supportsIPv4() || it->second.supportsIPv6()); |
| 227 | std::cout << "Interface: (" << it->second.index() << ")" << std::endl; |
| 228 | std::cout << "Address: " << it->second.address() << std::endl; |
| 229 | NetworkInterface::MACAddress mac(it->second.macAddress()); |
| 230 | if (!mac.empty() && (it->second.type() != NetworkInterface::NI_TYPE_SOFTWARE_LOOPBACK)) |
| 231 | std::cout << "MAC Address:" << mac << std::endl; |
| 232 | } |
| 233 | } |
| 234 | catch(Poco::NotImplementedException e) |
| 235 | { |
| 236 | #if POCO_OS != POCO_OS_ANDROID |
| 237 | throw e; |
| 238 | #endif |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | |
| 243 | void NetworkInterfaceTest::testMapUpOnly() |
| 244 | { |
| 245 | try |
| 246 | { |
| 247 | NetworkInterface::Map m = NetworkInterface::map(false, true); |
| 248 | assertTrue (!m.empty()); |
| 249 | for (NetworkInterface::Map::const_iterator it = m.begin(); it != m.end(); ++it) |
| 250 | { |
| 251 | assertTrue (it->second.isUp()); |
| 252 | } |
| 253 | } |
| 254 | catch(Poco::NotImplementedException e) |
| 255 | { |
| 256 | #if POCO_OS != POCO_OS_ANDROID |
| 257 | throw e; |
| 258 | #endif |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | |
| 263 | void NetworkInterfaceTest::testListMapConformance() |
| 264 | { |
| 265 | try |
| 266 | { |
| 267 | NetworkInterface::Map m = NetworkInterface::map(false, false); |
| 268 | assertTrue (!m.empty()); |
| 269 | NetworkInterface::List l = NetworkInterface::list(false, false); |
| 270 | assertTrue (!l.empty()); |
| 271 | |
| 272 | int counter = 0; |
| 273 | NetworkInterface::Map::const_iterator mapIt = m.begin(); |
| 274 | NetworkInterface::List::const_iterator listIt = l.begin(); |
| 275 | for (; mapIt != m.end(); ++mapIt) |
| 276 | { |
| 277 | NetworkInterface::MACAddress mac(mapIt->second.macAddress()); |
| 278 | typedef NetworkInterface::AddressList List; |
| 279 | const List& ipList = mapIt->second.addressList(); |
| 280 | if (ipList.size() > 0) |
| 281 | { |
| 282 | List::const_iterator ipIt = ipList.begin(); |
| 283 | List::const_iterator ipEnd = ipList.end(); |
| 284 | for(; ipIt != ipEnd; ++ipIt, ++counter, ++listIt) |
| 285 | { |
| 286 | if(listIt == l.end()) fail("wrong number of list items" ); |
| 287 | NetworkInterface::MACAddress lmac = listIt->macAddress(); |
| 288 | assertTrue (lmac == mac); |
| 289 | } |
| 290 | } |
| 291 | else |
| 292 | { |
| 293 | ++listIt; |
| 294 | ++counter; |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | assertTrue (counter == l.size()); |
| 299 | } |
| 300 | catch(Poco::NotImplementedException e) |
| 301 | { |
| 302 | #if POCO_OS != POCO_OS_ANDROID |
| 303 | throw e; |
| 304 | #endif |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | |
| 309 | void NetworkInterfaceTest::setUp() |
| 310 | { |
| 311 | } |
| 312 | |
| 313 | |
| 314 | void NetworkInterfaceTest::tearDown() |
| 315 | { |
| 316 | } |
| 317 | |
| 318 | |
| 319 | CppUnit::Test* NetworkInterfaceTest::suite() |
| 320 | { |
| 321 | CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("NetworkInterfaceTest" ); |
| 322 | |
| 323 | CppUnit_addTest(pSuite, NetworkInterfaceTest, testList); |
| 324 | CppUnit_addTest(pSuite, NetworkInterfaceTest, testMap); |
| 325 | CppUnit_addTest(pSuite, NetworkInterfaceTest, testForName); |
| 326 | CppUnit_addTest(pSuite, NetworkInterfaceTest, testForAddress); |
| 327 | CppUnit_addTest(pSuite, NetworkInterfaceTest, testForIndex); |
| 328 | CppUnit_addTest(pSuite, NetworkInterfaceTest, testMapIpOnly); |
| 329 | CppUnit_addTest(pSuite, NetworkInterfaceTest, testMapUpOnly); |
| 330 | CppUnit_addTest(pSuite, NetworkInterfaceTest, testListMapConformance); |
| 331 | |
| 332 | return pSuite; |
| 333 | } |
| 334 | |
| 335 | |
| 336 | #endif // POCO_NET_HAS_INTERFACE |
| 337 | |