1 /* 2 * Copyright 2011, Axel Dörfler, axeld@pinc-software.de. 3 * Distributed under the terms of the MIT License. 4 */ 5 6 7 #include "NetworkInterfaceTest.h" 8 9 #include <NetworkInterface.h> 10 #include <NetworkRoster.h> 11 12 #include <cppunit/TestCaller.h> 13 #include <cppunit/TestSuite.h> 14 15 16 NetworkInterfaceTest::NetworkInterfaceTest() 17 { 18 } 19 20 21 NetworkInterfaceTest::~NetworkInterfaceTest() 22 { 23 } 24 25 26 void 27 NetworkInterfaceTest::setUp() 28 { 29 fInterface.SetTo("loopTest"); 30 BNetworkRoster::Default().RemoveInterface(fInterface); 31 // just in case 32 33 CPPUNIT_ASSERT(BNetworkRoster::Default().AddInterface(fInterface) == B_OK); 34 CPPUNIT_ASSERT(fInterface.CountAddresses() == 1); 35 // every interface has one unspec address 36 } 37 38 39 void 40 NetworkInterfaceTest::tearDown() 41 { 42 CPPUNIT_ASSERT(BNetworkRoster::Default().RemoveInterface(fInterface) 43 == B_OK); 44 } 45 46 47 void 48 NetworkInterfaceTest::TestUnset() 49 { 50 BNetworkInterface unset("test"); 51 unset.Unset(); 52 CPPUNIT_ASSERT(unset.Name() == NULL || unset.Name()[0] == '\0'); 53 } 54 55 56 void 57 NetworkInterfaceTest::TestFindAddress() 58 { 59 fInterface.RemoveAddressAt(0); 60 // Remove empty address 61 62 // Add first 63 64 BNetworkInterfaceAddress first; 65 first.SetAddress(BNetworkAddress(AF_INET, "8.8.8.8")); 66 67 CPPUNIT_ASSERT(fInterface.FindAddress(first.Address()) < 0); 68 CPPUNIT_ASSERT(fInterface.AddAddress(first) == B_OK); 69 CPPUNIT_ASSERT(fInterface.FindAddress(first.Address()) == 0); 70 71 // Add second 72 73 BNetworkInterfaceAddress second; 74 second.SetAddress(BNetworkAddress(AF_INET6, "::1")); 75 76 CPPUNIT_ASSERT(fInterface.FindAddress(second.Address()) < 0); 77 CPPUNIT_ASSERT(fInterface.AddAddress(second) == B_OK); 78 CPPUNIT_ASSERT(fInterface.FindAddress(second.Address()) >= 0); 79 80 // Remove them again 81 82 CPPUNIT_ASSERT(fInterface.RemoveAddress(first) == B_OK); 83 CPPUNIT_ASSERT(fInterface.FindAddress(first.Address()) < 0); 84 CPPUNIT_ASSERT(fInterface.FindAddress(second.Address()) >= 0); 85 86 CPPUNIT_ASSERT(fInterface.RemoveAddress(second.Address()) == B_OK); 87 CPPUNIT_ASSERT(fInterface.FindAddress(first.Address()) < 0); 88 CPPUNIT_ASSERT(fInterface.FindAddress(second.Address()) < 0); 89 } 90 91 92 void 93 NetworkInterfaceTest::TestFindFirstAddress() 94 { 95 fInterface.RemoveAddressAt(0); 96 // Remove empty address 97 98 // Add first 99 100 BNetworkInterfaceAddress first; 101 first.SetAddress(BNetworkAddress(AF_INET, "8.8.8.8")); 102 103 CPPUNIT_ASSERT(fInterface.FindFirstAddress(AF_INET) < 0); 104 CPPUNIT_ASSERT(fInterface.FindFirstAddress(AF_INET6) < 0); 105 CPPUNIT_ASSERT(fInterface.AddAddress(first) == B_OK); 106 CPPUNIT_ASSERT(fInterface.FindFirstAddress(AF_INET) == 0); 107 CPPUNIT_ASSERT(fInterface.FindFirstAddress(AF_INET6) < 0); 108 109 // Add second 110 111 BNetworkInterfaceAddress second; 112 second.SetAddress(BNetworkAddress(AF_INET6, "::1")); 113 114 CPPUNIT_ASSERT(fInterface.AddAddress(second) == B_OK); 115 CPPUNIT_ASSERT(fInterface.FindFirstAddress(AF_INET) >= 0); 116 CPPUNIT_ASSERT(fInterface.FindFirstAddress(AF_INET6) >= 0); 117 CPPUNIT_ASSERT(fInterface.FindFirstAddress(AF_INET) 118 != fInterface.FindFirstAddress(AF_INET6)); 119 120 // Remove them again 121 122 CPPUNIT_ASSERT(fInterface.RemoveAddress(first) == B_OK); 123 CPPUNIT_ASSERT(fInterface.FindFirstAddress(AF_INET) < 0); 124 CPPUNIT_ASSERT(fInterface.FindFirstAddress(AF_INET6) >= 0); 125 126 CPPUNIT_ASSERT(fInterface.RemoveAddress(second.Address()) == B_OK); 127 CPPUNIT_ASSERT(fInterface.FindFirstAddress(AF_INET) < 0); 128 CPPUNIT_ASSERT(fInterface.FindFirstAddress(AF_INET6) < 0); 129 } 130 131 132 /*static*/ void 133 NetworkInterfaceTest::AddTests(BTestSuite& parent) 134 { 135 CppUnit::TestSuite& suite = *new CppUnit::TestSuite("NetworkInterfaceTest"); 136 137 suite.addTest(new CppUnit::TestCaller<NetworkInterfaceTest>( 138 "NetworkInterfaceTest::TestUnset", &NetworkInterfaceTest::TestUnset)); 139 suite.addTest(new CppUnit::TestCaller<NetworkInterfaceTest>( 140 "NetworkInterfaceTest::TestFindAddress", 141 &NetworkInterfaceTest::TestFindAddress)); 142 suite.addTest(new CppUnit::TestCaller<NetworkInterfaceTest>( 143 "NetworkInterfaceTest::TestFindFirstAddress", 144 &NetworkInterfaceTest::TestFindFirstAddress)); 145 146 parent.addTest("NetworkInterfaceTest", &suite); 147 } 148