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 <netinet6/in6.h> 10 11 #include <NetworkAddress.h> 12 #include <TypeConstants.h> 13 14 #include <cppunit/TestCaller.h> 15 #include <cppunit/TestSuite.h> 16 17 18 NetworkAddressTest::NetworkAddressTest() 19 { 20 } 21 22 23 NetworkAddressTest::~NetworkAddressTest() 24 { 25 } 26 27 28 void 29 NetworkAddressTest::TestUnset() 30 { 31 BNetworkAddress unset; 32 33 CPPUNIT_ASSERT(unset.Family() == AF_UNSPEC); 34 CPPUNIT_ASSERT(unset.Port() == 0); 35 36 BNetworkAddress set(AF_INET, NULL); 37 CPPUNIT_ASSERT(set.Family() == AF_INET); 38 CPPUNIT_ASSERT(unset != set); 39 40 set.Unset(); 41 CPPUNIT_ASSERT(unset == set); 42 } 43 44 45 void 46 NetworkAddressTest::TestSetTo() 47 { 48 BNetworkAddress address; 49 50 CPPUNIT_ASSERT(address.SetTo("127.0.0.1") == B_OK); 51 CPPUNIT_ASSERT(address.Family() == AF_INET); 52 CPPUNIT_ASSERT(address == BNetworkAddress(htonl(INADDR_LOOPBACK))); 53 54 CPPUNIT_ASSERT(address.SetTo("::1") == B_OK); 55 CPPUNIT_ASSERT(address.Family() == AF_INET6); 56 CPPUNIT_ASSERT(address == BNetworkAddress(in6addr_loopback)); 57 58 CPPUNIT_ASSERT(address.SetTo(AF_INET, "::1") != B_OK); 59 CPPUNIT_ASSERT(address.SetTo(AF_INET6, "127.0.0.1") != B_OK); 60 CPPUNIT_ASSERT(address.SetTo(AF_INET, "127.0.0.1") == B_OK); 61 CPPUNIT_ASSERT(address.SetTo(AF_INET6, "::1") == B_OK); 62 } 63 64 65 void 66 NetworkAddressTest::TestWildcard() 67 { 68 BNetworkAddress wildcard; 69 wildcard.SetToWildcard(AF_INET); 70 71 CPPUNIT_ASSERT(wildcard.Family() == AF_INET); 72 CPPUNIT_ASSERT(wildcard.Length() == sizeof(sockaddr_in)); 73 CPPUNIT_ASSERT(wildcard.Port() == 0); 74 CPPUNIT_ASSERT(((sockaddr_in&)wildcard.SockAddr()).sin_addr.s_addr 75 == INADDR_ANY); 76 CPPUNIT_ASSERT(wildcard.IsEmpty()); 77 78 BNetworkAddress null(AF_INET, NULL); 79 CPPUNIT_ASSERT(wildcard == null); 80 81 wildcard.SetPort(555); 82 CPPUNIT_ASSERT(!wildcard.IsEmpty()); 83 } 84 85 86 void 87 NetworkAddressTest::TestIsLocal() 88 { 89 BNetworkAddress local(AF_INET, "localhost"); 90 CPPUNIT_ASSERT(local.IsLocal()); 91 92 BNetworkAddress google(AF_INET, "google.com"); 93 CPPUNIT_ASSERT(!google.IsLocal()); 94 } 95 96 97 void 98 NetworkAddressTest::TestFlatten() 99 { 100 // IPv4 101 102 BNetworkAddress ipv4(AF_INET, "localhost", 9999); 103 104 char buffer[256]; 105 CPPUNIT_ASSERT(ipv4.Flatten(buffer, sizeof(buffer)) == B_OK); 106 107 BNetworkAddress unflattened; 108 CPPUNIT_ASSERT(unflattened.Unflatten(B_NETWORK_ADDRESS_TYPE, buffer, 109 sizeof(buffer)) == B_OK); 110 111 // unflatten buffer too small 112 CPPUNIT_ASSERT(unflattened.Unflatten(B_NETWORK_ADDRESS_TYPE, buffer, 0) 113 != B_OK); 114 CPPUNIT_ASSERT(unflattened.Unflatten(B_NETWORK_ADDRESS_TYPE, buffer, 3) 115 != B_OK); 116 CPPUNIT_ASSERT(unflattened.Unflatten(B_NETWORK_ADDRESS_TYPE, buffer, 16) 117 != B_OK); 118 119 CPPUNIT_ASSERT(ipv4 == unflattened); 120 } 121 122 123 /*static*/ void 124 NetworkAddressTest::AddTests(BTestSuite& parent) 125 { 126 CppUnit::TestSuite& suite = *new CppUnit::TestSuite("NetworkAddressTest"); 127 128 suite.addTest(new CppUnit::TestCaller<NetworkAddressTest>( 129 "NetworkAddressTest::TestUnset", &NetworkAddressTest::TestUnset)); 130 suite.addTest(new CppUnit::TestCaller<NetworkAddressTest>( 131 "NetworkAddressTest::TestSetTo", &NetworkAddressTest::TestSetTo)); 132 suite.addTest(new CppUnit::TestCaller<NetworkAddressTest>( 133 "NetworkAddressTest::TestWildcard", &NetworkAddressTest::TestWildcard)); 134 suite.addTest(new CppUnit::TestCaller<NetworkAddressTest>( 135 "NetworkAddressTest::TestIsLocal", &NetworkAddressTest::TestIsLocal)); 136 suite.addTest(new CppUnit::TestCaller<NetworkAddressTest>( 137 "NetworkAddressTest::TestFlatten", &NetworkAddressTest::TestFlatten)); 138 139 parent.addTest("NetworkAddressTest", &suite); 140 } 141