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