* Added some unit tests for BNetworkAddress.

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@39694 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Axel Dörfler 2010-12-01 17:33:09 +00:00
parent 19b4490fd2
commit fc958868b4
5 changed files with 170 additions and 0 deletions

View File

@ -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 ;

View File

@ -0,0 +1,9 @@
SubDir HAIKU_TOP src tests kits net libnetapi ;
UnitTestLib libnetapitest.so :
NetAPITestAddon.cpp
NetworkAddressTest.cpp
: bnetapi $(TARGET_LIBSTDC++) $(TARGET_LIBSUPC++)
;

View File

@ -0,0 +1,21 @@
/*
* Copyright 2010, Axel Dörfler, axeld@pinc-software.de.
* Distributed under the terms of the MIT License.
*/
#include <TestSuite.h>
#include <TestSuiteAddon.h>
#include "NetworkAddressTest.h"
BTestSuite*
getTestSuite()
{
BTestSuite* suite = new BTestSuite("NetAPI");
NetworkAddressTest::AddTests(*suite);
return suite;
}

View File

@ -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 <NetworkAddress.h>
#include <TypeConstants.h>
#include <cppunit/TestCaller.h>
#include <cppunit/TestSuite.h>
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>(
"NetworkAddressTest::TestUnset", &NetworkAddressTest::TestUnset));
suite.addTest(new CppUnit::TestCaller<NetworkAddressTest>(
"NetworkAddressTest::TestWildcard", &NetworkAddressTest::TestWildcard));
suite.addTest(new CppUnit::TestCaller<NetworkAddressTest>(
"NetworkAddressTest::TestIsLocal", &NetworkAddressTest::TestIsLocal));
suite.addTest(new CppUnit::TestCaller<NetworkAddressTest>(
"NetworkAddressTest::TestFlatten", &NetworkAddressTest::TestFlatten));
parent.addTest("NetworkAddressTest", &suite);
}

View File

@ -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 <TestCase.h>
#include <TestSuite.h>
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