From fc958868b43e84ad97ff18b869a2a189e868043d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Axel=20D=C3=B6rfler?= Date: Wed, 1 Dec 2010 17:33:09 +0000 Subject: [PATCH] * Added some unit tests for BNetworkAddress. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@39694 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/tests/kits/net/Jamfile | 1 + src/tests/kits/net/libnetapi/Jamfile | 9 ++ .../kits/net/libnetapi/NetAPITestAddon.cpp | 21 ++++ .../kits/net/libnetapi/NetworkAddressTest.cpp | 112 ++++++++++++++++++ .../kits/net/libnetapi/NetworkAddressTest.h | 27 +++++ 5 files changed, 170 insertions(+) create mode 100644 src/tests/kits/net/libnetapi/Jamfile create mode 100644 src/tests/kits/net/libnetapi/NetAPITestAddon.cpp create mode 100644 src/tests/kits/net/libnetapi/NetworkAddressTest.cpp create mode 100644 src/tests/kits/net/libnetapi/NetworkAddressTest.h diff --git a/src/tests/kits/net/Jamfile b/src/tests/kits/net/Jamfile index 37d6ae27fa..d0b45f8d04 100644 --- a/src/tests/kits/net/Jamfile +++ b/src/tests/kits/net/Jamfile @@ -30,6 +30,7 @@ SubInclude HAIKU_TOP src tests kits net cookie ; SubInclude HAIKU_TOP src tests kits net DialUpPreflet ; SubInclude HAIKU_TOP src tests kits net icmp ; SubInclude HAIKU_TOP src tests kits net ipv6 ; +HaikuSubInclude libnetapi ; SubInclude HAIKU_TOP src tests kits net multicast ; SubInclude HAIKU_TOP src tests kits net netperf ; SubInclude HAIKU_TOP src tests kits net preflet ; diff --git a/src/tests/kits/net/libnetapi/Jamfile b/src/tests/kits/net/libnetapi/Jamfile new file mode 100644 index 0000000000..257700e82a --- /dev/null +++ b/src/tests/kits/net/libnetapi/Jamfile @@ -0,0 +1,9 @@ +SubDir HAIKU_TOP src tests kits net libnetapi ; + +UnitTestLib libnetapitest.so : + NetAPITestAddon.cpp + + NetworkAddressTest.cpp + + : bnetapi $(TARGET_LIBSTDC++) $(TARGET_LIBSUPC++) +; diff --git a/src/tests/kits/net/libnetapi/NetAPITestAddon.cpp b/src/tests/kits/net/libnetapi/NetAPITestAddon.cpp new file mode 100644 index 0000000000..a1abe758e3 --- /dev/null +++ b/src/tests/kits/net/libnetapi/NetAPITestAddon.cpp @@ -0,0 +1,21 @@ +/* + * Copyright 2010, Axel Dörfler, axeld@pinc-software.de. + * Distributed under the terms of the MIT License. + */ + + +#include +#include + +#include "NetworkAddressTest.h" + + +BTestSuite* +getTestSuite() +{ + BTestSuite* suite = new BTestSuite("NetAPI"); + + NetworkAddressTest::AddTests(*suite); + + return suite; +} diff --git a/src/tests/kits/net/libnetapi/NetworkAddressTest.cpp b/src/tests/kits/net/libnetapi/NetworkAddressTest.cpp new file mode 100644 index 0000000000..d5e499630d --- /dev/null +++ b/src/tests/kits/net/libnetapi/NetworkAddressTest.cpp @@ -0,0 +1,112 @@ +/* + * Copyright 2010, Axel Dörfler, axeld@pinc-software.de. + * Distributed under the terms of the MIT License. + */ + + +#include "NetworkAddressTest.h" + +#include +#include + +#include +#include + + +NetworkAddressTest::NetworkAddressTest() +{ +} + + +NetworkAddressTest::~NetworkAddressTest() +{ +} + + +void +NetworkAddressTest::TestUnset() +{ + BNetworkAddress unset; + + CPPUNIT_ASSERT(unset.Family() == AF_UNSPEC); + CPPUNIT_ASSERT(unset.Port() == 0); + + BNetworkAddress set(AF_INET, NULL); + CPPUNIT_ASSERT(set.Family() == AF_INET); + CPPUNIT_ASSERT(unset != set); + + set.Unset(); + CPPUNIT_ASSERT(unset == set); +} + + +void +NetworkAddressTest::TestWildcard() +{ + BNetworkAddress wildcard; + wildcard.SetToWildcard(AF_INET); + + CPPUNIT_ASSERT(wildcard.Family() == AF_INET); + CPPUNIT_ASSERT(wildcard.Length() == sizeof(sockaddr_in)); + CPPUNIT_ASSERT(wildcard.Port() == 0); + CPPUNIT_ASSERT(((sockaddr_in&)wildcard.SockAddr()).sin_addr.s_addr + == INADDR_ANY); + + BNetworkAddress null(AF_INET, NULL); + CPPUNIT_ASSERT(wildcard == null); +} + + +void +NetworkAddressTest::TestIsLocal() +{ + BNetworkAddress local(AF_INET, "localhost"); + CPPUNIT_ASSERT(local.IsLocal()); + + BNetworkAddress google(AF_INET, "google.com"); + CPPUNIT_ASSERT(!google.IsLocal()); +} + + +void +NetworkAddressTest::TestFlatten() +{ + // IPv4 + + BNetworkAddress ipv4(AF_INET, "localhost", 9999); + + char buffer[256]; + CPPUNIT_ASSERT(ipv4.Flatten(buffer, sizeof(buffer)) == B_OK); + + BNetworkAddress unflattened; + CPPUNIT_ASSERT(unflattened.Unflatten(B_NETWORK_ADDRESS_TYPE, buffer, + sizeof(buffer)) == B_OK); + + // unflatten buffer too small + CPPUNIT_ASSERT(unflattened.Unflatten(B_NETWORK_ADDRESS_TYPE, buffer, 0) + != B_OK); + CPPUNIT_ASSERT(unflattened.Unflatten(B_NETWORK_ADDRESS_TYPE, buffer, 3) + != B_OK); + CPPUNIT_ASSERT(unflattened.Unflatten(B_NETWORK_ADDRESS_TYPE, buffer, 16) + != B_OK); + + CPPUNIT_ASSERT(ipv4 == unflattened); +} + + +/*static*/ void +NetworkAddressTest::AddTests(BTestSuite& parent) +{ + CppUnit::TestSuite& suite = *new CppUnit::TestSuite("NetworkAddressTest"); + + suite.addTest(new CppUnit::TestCaller( + "NetworkAddressTest::TestUnset", &NetworkAddressTest::TestUnset)); + suite.addTest(new CppUnit::TestCaller( + "NetworkAddressTest::TestWildcard", &NetworkAddressTest::TestWildcard)); + suite.addTest(new CppUnit::TestCaller( + "NetworkAddressTest::TestIsLocal", &NetworkAddressTest::TestIsLocal)); + suite.addTest(new CppUnit::TestCaller( + "NetworkAddressTest::TestFlatten", &NetworkAddressTest::TestFlatten)); + + parent.addTest("NetworkAddressTest", &suite); +} diff --git a/src/tests/kits/net/libnetapi/NetworkAddressTest.h b/src/tests/kits/net/libnetapi/NetworkAddressTest.h new file mode 100644 index 0000000000..e234d9020a --- /dev/null +++ b/src/tests/kits/net/libnetapi/NetworkAddressTest.h @@ -0,0 +1,27 @@ +/* + * Copyright 2010, Axel Dörfler, axeld@pinc-software.de. + * Distributed under the terms of the MIT License. + */ +#ifndef NETWORK_ADDRESS_TEST_H +#define NETWORK_ADDRESS_TEST_H + + +#include +#include + + +class NetworkAddressTest : public CppUnit::TestCase { +public: + NetworkAddressTest(); + virtual ~NetworkAddressTest(); + + void TestUnset(); + void TestWildcard(); + void TestIsLocal(); + void TestFlatten(); + + static void AddTests(BTestSuite& suite); +}; + + +#endif // NETWORK_ADDRESS_TEST_H