diff --git a/src/tests/kits/net/libnetapi/Jamfile b/src/tests/kits/net/libnetapi/Jamfile index 257700e82a..5283c93a31 100644 --- a/src/tests/kits/net/libnetapi/Jamfile +++ b/src/tests/kits/net/libnetapi/Jamfile @@ -4,6 +4,7 @@ UnitTestLib libnetapitest.so : NetAPITestAddon.cpp NetworkAddressTest.cpp + NetworkInterfaceTest.cpp : bnetapi $(TARGET_LIBSTDC++) $(TARGET_LIBSUPC++) ; diff --git a/src/tests/kits/net/libnetapi/NetworkInterfaceTest.cpp b/src/tests/kits/net/libnetapi/NetworkInterfaceTest.cpp new file mode 100644 index 0000000000..0b9f63671b --- /dev/null +++ b/src/tests/kits/net/libnetapi/NetworkInterfaceTest.cpp @@ -0,0 +1,134 @@ +/* + * Copyright 2011, Axel Dörfler, axeld@pinc-software.de. + * Distributed under the terms of the MIT License. + */ + + +#include "NetworkInterfaceTest.h" + +#include +#include + +#include +#include + + +NetworkInterfaceTest::NetworkInterfaceTest() +{ +} + + +NetworkInterfaceTest::~NetworkInterfaceTest() +{ +} + + +void +NetworkInterfaceTest::TestUnset() +{ + BNetworkInterface unset("test"); + unset.Unset(); + CPPUNIT_ASSERT(unset.Name() == NULL || unset.Name()[0] == '\0'); +} + + +void +NetworkInterfaceTest::TestFindAddress() +{ + BNetworkInterface interface("testInterface"); + CPPUNIT_ASSERT(BNetworkRoster::Default().AddInterface(interface) == B_OK); + CPPUNIT_ASSERT(interface.CountAddresses() == 0); + + // Add first + + BNetworkInterfaceAddress first; + first.SetAddress(BNetworkAddress(AF_INET, "8.8.8.8")); + + CPPUNIT_ASSERT(interface.FindAddress(first.Address()) < 0); + CPPUNIT_ASSERT(interface.AddAddress(first) == B_OK); + CPPUNIT_ASSERT(interface.FindAddress(first.Address()) == 0); + + // Add second + + BNetworkInterfaceAddress second; + second.SetAddress(BNetworkAddress(AF_INET6, "::1")); + + CPPUNIT_ASSERT(interface.FindAddress(second.Address()) < 0); + CPPUNIT_ASSERT(interface.AddAddress(second) == B_OK); + CPPUNIT_ASSERT(interface.FindAddress(second.Address()) >= 0); + + // Remove them again + + CPPUNIT_ASSERT(interface.RemoveAddress(first) == B_OK); + CPPUNIT_ASSERT(interface.FindAddress(first.Address()) < 0); + CPPUNIT_ASSERT(interface.FindAddress(second.Address()) >= 0); + + CPPUNIT_ASSERT(interface.RemoveAddress(second.Address()) == B_OK); + CPPUNIT_ASSERT(interface.FindAddress(first.Address()) < 0); + CPPUNIT_ASSERT(interface.FindAddress(second.Address()) < 0); + + CPPUNIT_ASSERT(BNetworkRoster::Default().RemoveInterface(interface) + == B_OK); +} + + +void +NetworkInterfaceTest::TestFindFirstAddress() +{ + BNetworkInterface interface("testInterface"); + CPPUNIT_ASSERT(BNetworkRoster::Default().AddInterface(interface) == B_OK); + CPPUNIT_ASSERT(interface.CountAddresses() == 0); + + // Add first + + BNetworkInterfaceAddress first; + first.SetAddress(BNetworkAddress(AF_INET, "8.8.8.8")); + + CPPUNIT_ASSERT(interface.FindFirstAddress(AF_INET) < 0); + CPPUNIT_ASSERT(interface.FindFirstAddress(AF_INET6) < 0); + CPPUNIT_ASSERT(interface.AddAddress(first) == B_OK); + CPPUNIT_ASSERT(interface.FindFirstAddress(AF_INET) == 0); + CPPUNIT_ASSERT(interface.FindFirstAddress(AF_INET6) < 0); + + // Add second + + BNetworkInterfaceAddress second; + second.SetAddress(BNetworkAddress(AF_INET6, "::1")); + + CPPUNIT_ASSERT(interface.AddAddress(second) == B_OK); + CPPUNIT_ASSERT(interface.FindFirstAddress(AF_INET) >= 0); + CPPUNIT_ASSERT(interface.FindFirstAddress(AF_INET6) >= 0); + CPPUNIT_ASSERT(interface.FindFirstAddress(AF_INET) + != interface.FindFirstAddress(AF_INET6)); + + // Remove them again + + CPPUNIT_ASSERT(interface.RemoveAddress(first) == B_OK); + CPPUNIT_ASSERT(interface.FindFirstAddress(AF_INET) < 0); + CPPUNIT_ASSERT(interface.FindFirstAddress(AF_INET6) >= 0); + + CPPUNIT_ASSERT(interface.RemoveAddress(second.Address()) == B_OK); + CPPUNIT_ASSERT(interface.FindFirstAddress(AF_INET) < 0); + CPPUNIT_ASSERT(interface.FindFirstAddress(AF_INET6) < 0); + + CPPUNIT_ASSERT(BNetworkRoster::Default().RemoveInterface(interface) + == B_OK); +} + + +/*static*/ void +NetworkInterfaceTest::AddTests(BTestSuite& parent) +{ + CppUnit::TestSuite& suite = *new CppUnit::TestSuite("NetworkInterfaceTest"); + + suite.addTest(new CppUnit::TestCaller( + "NetworkInterfaceTest::TestUnset", &NetworkInterfaceTest::TestUnset)); + suite.addTest(new CppUnit::TestCaller( + "NetworkInterfaceTest::TestFindAddress", + &NetworkInterfaceTest::TestFindAddress)); + suite.addTest(new CppUnit::TestCaller( + "NetworkInterfaceTest::TestFindFirstAddress", + &NetworkInterfaceTest::TestFindFirstAddress)); + + parent.addTest("NetworkInterfaceTest", &suite); +} diff --git a/src/tests/kits/net/libnetapi/NetworkInterfaceTest.h b/src/tests/kits/net/libnetapi/NetworkInterfaceTest.h new file mode 100644 index 0000000000..1326c53f16 --- /dev/null +++ b/src/tests/kits/net/libnetapi/NetworkInterfaceTest.h @@ -0,0 +1,26 @@ +/* + * Copyright 2011, Axel Dörfler, axeld@pinc-software.de. + * Distributed under the terms of the MIT License. + */ +#ifndef NETWORK_INTERFACE_TEST_H +#define NETWORK_INTERFACE_TEST_H + + +#include +#include + + +class NetworkInterfaceTest : public CppUnit::TestCase { +public: + NetworkInterfaceTest(); + virtual ~NetworkInterfaceTest(); + + void TestUnset(); + void TestFindAddress(); + void TestFindFirstAddress(); + + static void AddTests(BTestSuite& suite); +}; + + +#endif // NETWORK_INTERFACE_TEST_H