1 /* 2 * Copyright 2010-2015, Axel Dörfler, axeld@pinc-software.de. 3 * Distributed under the terms of the MIT License. 4 */ 5 6 7 #include "NetworkAddressTest.h" 8 9 #include <arpa/inet.h> 10 #include <netinet6/in6.h> 11 12 #include <NetworkAddress.h> 13 #include <TypeConstants.h> 14 15 #include <cppunit/TestCaller.h> 16 #include <cppunit/TestSuite.h> 17 18 19 NetworkAddressTest::NetworkAddressTest() 20 { 21 } 22 23 24 NetworkAddressTest::~NetworkAddressTest() 25 { 26 } 27 28 29 void 30 NetworkAddressTest::TestUnset() 31 { 32 BNetworkAddress unset; 33 34 CPPUNIT_ASSERT(unset.InitCheck() == B_OK); 35 CPPUNIT_ASSERT(unset.Family() == AF_UNSPEC); 36 CPPUNIT_ASSERT(unset.Port() == 0); 37 38 BNetworkAddress set(AF_INET, "127.0.0.1"); 39 CPPUNIT_ASSERT(set.InitCheck() == B_OK); 40 CPPUNIT_ASSERT(set.Family() == AF_INET); 41 CPPUNIT_ASSERT(unset != set); 42 43 set.Unset(); 44 CPPUNIT_ASSERT(unset == set); 45 } 46 47 48 void 49 NetworkAddressTest::TestSetTo() 50 { 51 BNetworkAddress address; 52 53 CPPUNIT_ASSERT(address.SetTo("127.0.0.1") == B_OK); 54 CPPUNIT_ASSERT(address.Family() == AF_INET); 55 CPPUNIT_ASSERT(address == BNetworkAddress(htonl(INADDR_LOOPBACK))); 56 57 CPPUNIT_ASSERT(address.SetTo("::1", (uint16)0, 58 B_UNCONFIGURED_ADDRESS_FAMILIES) == B_OK); 59 CPPUNIT_ASSERT(address.Family() == AF_INET6); 60 CPPUNIT_ASSERT(address == BNetworkAddress(in6addr_loopback)); 61 62 CPPUNIT_ASSERT(address.SetTo(AF_INET, "::1") != B_OK); 63 CPPUNIT_ASSERT(address.SetTo(AF_INET6, "127.0.0.1") != B_OK); 64 CPPUNIT_ASSERT(address.SetTo(AF_INET, "127.0.0.1") == B_OK); 65 CPPUNIT_ASSERT(address.SetTo(AF_INET6, "::1", (uint16)0, 66 B_UNCONFIGURED_ADDRESS_FAMILIES) == B_OK); 67 } 68 69 70 void 71 NetworkAddressTest::TestWildcard() 72 { 73 BNetworkAddress wildcard; 74 wildcard.SetToWildcard(AF_INET); 75 76 CPPUNIT_ASSERT(wildcard.Family() == AF_INET); 77 CPPUNIT_ASSERT(wildcard.Length() == sizeof(sockaddr_in)); 78 CPPUNIT_ASSERT(wildcard.Port() == 0); 79 CPPUNIT_ASSERT(((sockaddr_in&)wildcard.SockAddr()).sin_addr.s_addr 80 == INADDR_ANY); 81 CPPUNIT_ASSERT(wildcard.IsEmpty()); 82 83 BNetworkAddress null(AF_INET, NULL); 84 CPPUNIT_ASSERT(wildcard == null); 85 86 wildcard.SetPort(555); 87 CPPUNIT_ASSERT(!wildcard.IsEmpty()); 88 } 89 90 91 void 92 NetworkAddressTest::TestNullAddr() 93 { 94 BNetworkAddress nullAddr(AF_INET, NULL, 555); 95 CPPUNIT_ASSERT(nullAddr.InitCheck() == B_OK); 96 97 CPPUNIT_ASSERT(nullAddr.Family() == AF_INET); 98 CPPUNIT_ASSERT(nullAddr.Length() == sizeof(sockaddr_in)); 99 CPPUNIT_ASSERT(nullAddr.Port() == 555); 100 CPPUNIT_ASSERT(!nullAddr.IsEmpty()); 101 } 102 103 104 void 105 NetworkAddressTest::TestSetAddressFromFamilyPort() 106 { 107 BString addressStr = "192.168.1.1"; 108 BNetworkAddress nullAddr(AF_INET, NULL, 555); 109 in_addr_t inetAddress = inet_addr(addressStr.String()); 110 CPPUNIT_ASSERT(nullAddr.SetAddress(inetAddress) == B_OK); 111 CPPUNIT_ASSERT(nullAddr.InitCheck() == B_OK); 112 113 CPPUNIT_ASSERT(nullAddr.Family() == AF_INET); 114 CPPUNIT_ASSERT(nullAddr.Length() == sizeof(sockaddr_in)); 115 116 sockaddr_in& sin = (sockaddr_in&)nullAddr.SockAddr(); 117 CPPUNIT_ASSERT(addressStr == inet_ntoa(sin.sin_addr)); 118 CPPUNIT_ASSERT(nullAddr.Port() == 555); 119 CPPUNIT_ASSERT(!nullAddr.IsEmpty()); 120 } 121 122 123 void 124 NetworkAddressTest::TestIsLocal() 125 { 126 BNetworkAddress local(AF_INET, "localhost"); 127 CPPUNIT_ASSERT(local.IsLocal()); 128 129 BNetworkAddress google(AF_INET, "google.com"); 130 CPPUNIT_ASSERT(!google.IsLocal()); 131 } 132 133 134 void 135 NetworkAddressTest::TestFlatten() 136 { 137 // IPv4 138 139 BNetworkAddress ipv4(AF_INET, "localhost", 9999); 140 141 char buffer[256]; 142 CPPUNIT_ASSERT(ipv4.Flatten(buffer, sizeof(buffer)) == B_OK); 143 144 BNetworkAddress unflattened; 145 CPPUNIT_ASSERT(unflattened.Unflatten(B_NETWORK_ADDRESS_TYPE, buffer, 146 sizeof(buffer)) == B_OK); 147 148 // unflatten buffer too small 149 CPPUNIT_ASSERT(unflattened.Unflatten(B_NETWORK_ADDRESS_TYPE, buffer, 0) 150 != B_OK); 151 CPPUNIT_ASSERT(unflattened.Unflatten(B_NETWORK_ADDRESS_TYPE, buffer, 3) 152 != B_OK); 153 CPPUNIT_ASSERT(unflattened.Unflatten(B_NETWORK_ADDRESS_TYPE, buffer, 16) 154 != B_OK); 155 156 CPPUNIT_ASSERT(ipv4 == unflattened); 157 } 158 159 160 void 161 NetworkAddressTest::TestEquals() 162 { 163 BNetworkAddress v4AddressA("192.168.1.100"); 164 BNetworkAddress v4AddressB("192.168.1.100"); 165 BNetworkAddress v6AddressA("feed::dead:beef", (uint16)0, 166 B_UNCONFIGURED_ADDRESS_FAMILIES); 167 BNetworkAddress v6AddressB("feed::dead:beef", (uint16)0, 168 B_UNCONFIGURED_ADDRESS_FAMILIES); 169 170 CPPUNIT_ASSERT(v4AddressA.Equals(v4AddressB)); 171 CPPUNIT_ASSERT(v6AddressA.Equals(v6AddressB)); 172 CPPUNIT_ASSERT(!v4AddressA.Equals(v6AddressA)); 173 } 174 175 176 /*static*/ void 177 NetworkAddressTest::AddTests(BTestSuite& parent) 178 { 179 CppUnit::TestSuite& suite = *new CppUnit::TestSuite("NetworkAddressTest"); 180 181 suite.addTest(new CppUnit::TestCaller<NetworkAddressTest>( 182 "NetworkAddressTest::TestUnset", &NetworkAddressTest::TestUnset)); 183 suite.addTest(new CppUnit::TestCaller<NetworkAddressTest>( 184 "NetworkAddressTest::TestSetTo", &NetworkAddressTest::TestSetTo)); 185 suite.addTest(new CppUnit::TestCaller<NetworkAddressTest>( 186 "NetworkAddressTest::TestWildcard", &NetworkAddressTest::TestWildcard)); 187 suite.addTest(new CppUnit::TestCaller<NetworkAddressTest>( 188 "NetworkAddressTest::TestNullAddr", &NetworkAddressTest::TestNullAddr)); 189 suite.addTest(new CppUnit::TestCaller<NetworkAddressTest>( 190 "NetworkAddressTest::TestSetAddressFromFamilyPort", 191 &NetworkAddressTest::TestSetAddressFromFamilyPort)); 192 suite.addTest(new CppUnit::TestCaller<NetworkAddressTest>( 193 "NetworkAddressTest::TestIsLocal", &NetworkAddressTest::TestIsLocal)); 194 suite.addTest(new CppUnit::TestCaller<NetworkAddressTest>( 195 "NetworkAddressTest::TestFlatten", &NetworkAddressTest::TestFlatten)); 196 suite.addTest(new CppUnit::TestCaller<NetworkAddressTest>( 197 "NetworkAddressTest::TestEquals", &NetworkAddressTest::TestEquals)); 198 199 parent.addTest("NetworkAddressTest", &suite); 200 } 201