1 /* 2 * Copyright 2010-2011, 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 77 BNetworkAddress null(AF_INET, NULL); 78 CPPUNIT_ASSERT(wildcard == null); 79 } 80 81 82 void 83 NetworkAddressTest::TestIsLocal() 84 { 85 BNetworkAddress local(AF_INET, "localhost"); 86 CPPUNIT_ASSERT(local.IsLocal()); 87 88 BNetworkAddress google(AF_INET, "google.com"); 89 CPPUNIT_ASSERT(!google.IsLocal()); 90 } 91 92 93 void 94 NetworkAddressTest::TestFlatten() 95 { 96 // IPv4 97 98 BNetworkAddress ipv4(AF_INET, "localhost", 9999); 99 100 char buffer[256]; 101 CPPUNIT_ASSERT(ipv4.Flatten(buffer, sizeof(buffer)) == B_OK); 102 103 BNetworkAddress unflattened; 104 CPPUNIT_ASSERT(unflattened.Unflatten(B_NETWORK_ADDRESS_TYPE, buffer, 105 sizeof(buffer)) == B_OK); 106 107 // unflatten buffer too small 108 CPPUNIT_ASSERT(unflattened.Unflatten(B_NETWORK_ADDRESS_TYPE, buffer, 0) 109 != B_OK); 110 CPPUNIT_ASSERT(unflattened.Unflatten(B_NETWORK_ADDRESS_TYPE, buffer, 3) 111 != B_OK); 112 CPPUNIT_ASSERT(unflattened.Unflatten(B_NETWORK_ADDRESS_TYPE, buffer, 16) 113 != B_OK); 114 115 CPPUNIT_ASSERT(ipv4 == unflattened); 116 } 117 118 119 /*static*/ void 120 NetworkAddressTest::AddTests(BTestSuite& parent) 121 { 122 CppUnit::TestSuite& suite = *new CppUnit::TestSuite("NetworkAddressTest"); 123 124 suite.addTest(new CppUnit::TestCaller<NetworkAddressTest>( 125 "NetworkAddressTest::TestUnset", &NetworkAddressTest::TestUnset)); 126 suite.addTest(new CppUnit::TestCaller<NetworkAddressTest>( 127 "NetworkAddressTest::TestSetTo", &NetworkAddressTest::TestSetTo)); 128 suite.addTest(new CppUnit::TestCaller<NetworkAddressTest>( 129 "NetworkAddressTest::TestWildcard", &NetworkAddressTest::TestWildcard)); 130 suite.addTest(new CppUnit::TestCaller<NetworkAddressTest>( 131 "NetworkAddressTest::TestIsLocal", &NetworkAddressTest::TestIsLocal)); 132 suite.addTest(new CppUnit::TestCaller<NetworkAddressTest>( 133 "NetworkAddressTest::TestFlatten", &NetworkAddressTest::TestFlatten)); 134 135 parent.addTest("NetworkAddressTest", &suite); 136 } 137