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