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