From ce9e476882a6d9f5ff13efb45809906f05d9aa40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Axel=20D=C3=B6rfler?= Date: Thu, 10 Feb 2011 18:15:38 +0000 Subject: [PATCH] * Added basic test for BNetworkInterface - I did not run that one yet, though (switching to Haiku now to change that). git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@40422 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/tests/kits/net/libnetapi/Jamfile | 1 + .../net/libnetapi/NetworkInterfaceTest.cpp | 134 ++++++++++++++++++ .../kits/net/libnetapi/NetworkInterfaceTest.h | 26 ++++ 3 files changed, 161 insertions(+) create mode 100644 src/tests/kits/net/libnetapi/NetworkInterfaceTest.cpp create mode 100644 src/tests/kits/net/libnetapi/NetworkInterfaceTest.h 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