Files
poco/Net/testsuite/src/RouteTest.cpp
2012-11-08 22:56:25 -06:00

191 lines
5.7 KiB
C++

//
// RouteTest.cpp
//
// $Id: //poco/1.4/Net/testsuite/src/RouteTest.cpp#2 $
//
// Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// Permission is hereby granted, free of charge, to any person or organization
// obtaining a copy of the software and accompanying documentation covered by
// this license (the "Software") to use, reproduce, display, distribute,
// execute, and transmit the Software, and to prepare derivative works of the
// Software, and to permit third-parties to whom the Software is furnished to
// do so, all subject to the following:
//
// The copyright notices in the Software and this entire statement, including
// the above license grant, this restriction and the following disclaimer,
// must be included in all copies of the Software, in whole or in part, and
// all derivative works of the Software, unless such copies or derivative
// works are solely in the form of machine-executable object code generated by
// a source language processor.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
#include "RouteTest.h"
#include "CppUnit/TestCaller.h"
#include "CppUnit/TestSuite.h"
#include "Poco/Net/IPAddress.h"
#include "Poco/Net/Route.h"
#include "Poco/Net/NetException.h"
#include <iostream>
#include <iomanip>
using Poco::Net::IPAddress;
using Poco::Net::Route;
using Poco::Net::InvalidAddressException;
RouteTest::RouteTest(const std::string& name): CppUnit::TestCase(name)
{
}
RouteTest::~RouteTest()
{
}
//
// we should have on most systems at least 3 routes: one for the local subnet,
// one for the loopback subnet, and a default route.
//
// we should have at least one default route (possibly more on a multi-homed
// system).
//
// we should have at least 2 multicast all-hosts routes: one for the local subnet,
// and one for the loopback subnet.
//
// if we wish to do more sanity checking than this, we'd need to correlate it
// with results we get back from NetworkInterface::list().
//
void RouteTest::testDefaultRoute()
{
Route::RouteList defaults = Route::defaults(IPAddress::IPv4);
assert(defaults.size() >= 1);
}
void RouteTest::testAllHostsRoutes()
{
IPAddress target("224.0.0.1");
Route::RouteList targets = Route::match(target);
assert(targets.size() >= 2);
}
void RouteTest::testLoopbackRoute()
{
IPAddress target("127.0.0.1");
Route::RouteList targets = Route::match(target);
assert(targets.size() >= 1);
}
void RouteTest::setUp()
{
}
void RouteTest::tearDown()
{
}
void RouteTest::testAllRoutes()
{
std::string metric = "Metric";
#if defined(POCO_OS_FAMILY_BSD)
metric = "Expire";
#endif
Route::RouteList routes = Route::list(IPAddress::IPv4);
assert(routes.size() >= 3);
std::cout << std::endl << "Active IPv4 Routes:" << std::endl;
std::cout << "=============================================================================" << std::endl;
std::cout << std::setw(17) << "Destinaton"
<< std::setw(17) << "Netmask"
<< std::setw(17) << "Gateway"
<< std::setw(17) << "Interface"
<< std::setw(8) << metric << std::endl;
Route::RouteList::const_iterator it = routes.begin();
Route::RouteList::const_iterator end = routes.end();
for (; it != end; ++it)
{
IPAddress ip = it->getNetworkInterface().firstAddress(IPAddress::IPv4);
if (!ip.isLinkLocal())
{
std::string gateway = it->getNextHop().isWildcard() ? "On-link" : it->getNextHop().toString();
std::cout << std::setw(17) << it->getDest().toString()
<< std::setw(17) << it->getNetmask().toString()
<< std::setw(17) << gateway
#if defined(POCO_OS_FAMILY_BSD)
<< std::setw(17) << it->getNetworkInterface().name();
#else
<< std::setw(17) << ip.toString();
#endif
if (it->getMetric() != Route::ROUTE_METRIC_UNKNOWN)
std::cout << std::setw(8) << it->getMetric();
std::cout << std::endl;
}
}
std::cout << "=============================================================================" << std::endl;
routes = Route::list(IPAddress::IPv6);
assert(routes.size() >= 3);
std::cout << std::endl << "Active IPv6 Routes:" << std::endl;
std::cout << "=============================================================" << std::endl;
std::cout << std::setw(4) << "If"
<< std::setw(7) << metric
<< std::setw(41) << "Destinaton"
<< std::setw(41) << "Gateway"
<< std::endl;
it = routes.begin();
for (; it != end; ++it)
{
IPAddress ip = it->getNetworkInterface().firstAddress(IPAddress::IPv6);
if (!ip.isLinkLocal())
{
std::string gateway = it->getNextHop().isWildcard() ? "On-link" : it->getNextHop().toString();
#if defined(POCO_OS_FAMILY_BSD)
std::cout << std::setw(4) << it->getNetworkInterface().name();
#else
std::cout << std::setw(4) << it->getIfIndex();
#endif
if (it->getMetric() != Route::ROUTE_METRIC_UNKNOWN)
std::cout << std::setw(7) << it->getMetric();
else
std::cout << std::setw(7) << "";
std::cout << std::setw(41) << it->getDest().toString()
<< std::setw(41) << gateway << std::endl;
}
}
std::cout << "=============================================================" << std::endl;
}
CppUnit::Test* RouteTest::suite()
{
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("RouteTest");
CppUnit_addTest(pSuite, RouteTest, testDefaultRoute);
CppUnit_addTest(pSuite, RouteTest, testAllRoutes);
// CppUnit_addTest(pSuite, RouteTest, testAllHostsRoutes);
CppUnit_addTest(pSuite, RouteTest, testLoopbackRoute);
return pSuite;
}