Skip to content

Instantly share code, notes, and snippets.

@gorgonical
Created January 13, 2017 16:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gorgonical/4b3be61b1faac093693a484213457b93 to your computer and use it in GitHub Desktop.
Save gorgonical/4b3be61b1faac093693a484213457b93 to your computer and use it in GitHub Desktop.
Contents of my unit test for readvertise.t.cpp
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
* Copyright (c) 2014-2017, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
* Washington University in St. Louis,
* Beijing Institute of Technology,
* The University of Memphis.
*
* This file is part of NFD (Named Data Networking Forwarding Daemon).
* See AUTHORS.md for complete list of NFD authors and contributors.
*
* NFD is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*
* NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
*/
#include "tests/test-common.hpp"
#include <ndn-cxx/util/dummy-client-face.hpp>
#include "rib/readvertise/readvertise.hpp"
#include "core/scheduler.hpp"
#include "tests/identity-management-fixture.hpp"
namespace nfd {
namespace rib {
namespace tests {
using namespace nfd::tests;
class ReadvertisePolicyFake : public ReadvertisePolicy
{
public:
ReadvertisePolicyFake()
: doAdvertise(true)
{
}
ReadvertisePolicyFake(ReadvertisePolicyFake&&) = default;
//ReadvertisePolicyFake(const ReadvertisePolicyFake&) = default;
virtual optional<ReadvertiseAction>
handleNewRoute(const RibRouteRef& route) const override
{
if (doAdvertise) {
return ReadvertiseAction{route.entry->getName(), ndn::KeyChain::DEFAULT_SIGNING_INFO};
}
else {
return {};
}
}
virtual time::nanoseconds
getRefreshInterval() const override
{
return time::seconds(1);
}
public:
bool doAdvertise;
};
class ReadvertiseDestinationFake : public ReadvertiseDestination
{
public:
ReadvertiseDestinationFake()
: advertiseCalls(0)
, withdrawCalls(0)
, prefix("/ndn/test")
{
}
ReadvertiseDestinationFake(ReadvertiseDestinationFake&&) = default;
//ReadvertiseDestinationFake(const ReadvertiseDestinationFake&) = default;
void
advertise(nfd::rib::ReadvertisedRoute& rr,
std::function<void()> successCb,
std::function<void(const std::string&)> failureCb) override
{
advertisedPrefixes.push_back(rr.getPrefix());
advertiseCalls++;
}
void
withdraw(nfd::rib::ReadvertisedRoute& rr,
std::function<void()> successCb,
std::function<void(const std::string&)> failureCb) override
{
withdrawnPrefixes.push_back(rr.getPrefix());
withdrawCalls++;
}
const Name&
getName() override
{
return prefix;
}
public:
uint32_t advertiseCalls;
uint32_t withdrawCalls;
std::vector<ndn::Name> advertisedPrefixes;
std::vector<ndn::Name> withdrawnPrefixes;
ndn::Name prefix;
};
class EventIdFake : public ndn::EventId
{
public:
EventIdFake(uint32_t eventId)
: id(eventId)
, active(true)
{
}
public:
uint32_t id;
bool active;
};
class ReadvertisedRouteFake : public ReadvertisedRoute
{
public:
ReadvertisedRouteFake(const ndn::Name& prefix)
: m_prefix(prefix)
{
}
public:
std::vector<EventIdFake> events;
const ndn::Name m_prefix;
};
class TestReadvertiseFixture : public IdentityManagementFixture
{
public:
TestReadvertiseFixture()
: face(getGlobalIoService(), m_keyChain, {true, false})
, policy()
, destination()
, readvertise(rib, ndn::make_unique<ReadvertisePolicyFake>(policy),
ndn::make_unique<ReadvertiseDestinationFake>(destination))
{
// Readvertise readvertise(rib, ndn::make_unique<ReadvertisePolicyFake>(policy),
// ndn::make_unique<ReadvertiseDestinationFake>(destination));
}
public:
ndn::KeyChain m_keyChain;
ndn::util::DummyClientFace face;
ReadvertisePolicyFake policy;
ReadvertiseDestinationFake destination;
Rib rib;
Readvertise readvertise;
};
BOOST_FIXTURE_TEST_SUITE(TestReadvertise, TestReadvertiseFixture)
BOOST_AUTO_TEST_CASE(afterDestinationUnavailable)
{
std::vector<scheduler::EventId> events = readvertise.getRetryEvents();
EventIdFake event1(10);
EventIdFake event2(20);
EventIdFake event3(30);
events.push_back(event1);
events.push_back(event2);
events.push_back(event3);
readvertise.afterDestinationUnavailable();
BOOST_REQUIRE_EQUAL(events.size(), 0);
}
BOOST_AUTO_TEST_CASE(afterDestinationAvailable)
{
std::map<Name, ReadvertisedRoute> routes = readvertise.getRoutes();
ReadvertisedRouteFake route1("/test/A");
ReadvertisedRouteFake route2("/test/B");
ReadvertisedRouteFake route3("/test/C");
routes.emplace("/test/A", route1);
routes.emplace("/test/B", route2);
routes.emplace("/test/C", route3);
readvertise.afterDestinationAvailable();
BOOST_CHECK_EQUAL(destination.advertiseCalls, 3);
auto routeCheck = std::find(destination.advertisedPrefixes.begin(),
destination.advertisedPrefixes.end(), route1.getPrefix());
BOOST_CHECK(routeCheck != destination.advertisedPrefixes.end());
routeCheck = std::find(destination.advertisedPrefixes.begin(),
destination.advertisedPrefixes.end(), route2.getPrefix());
BOOST_CHECK(routeCheck != destination.advertisedPrefixes.end());
routeCheck = std::find(destination.advertisedPrefixes.begin(),
destination.advertisedPrefixes.end(), route3.getPrefix());
BOOST_CHECK(routeCheck != destination.advertisedPrefixes.end());
}
BOOST_AUTO_TEST_CASE(afterAddRouteNoAdvertise)
{
RibEntry entry;
entry.setName("/test/A");
shared_ptr<RibEntry> entryPtr = make_shared<RibEntry>(entry);
Route route;
std::list<Route> routes;
routes.push_back(route);
auto routeItr = routes.cbegin();
RibRouteRef routeRef{entryPtr, routeItr};
BOOST_CHECK_EQUAL(readvertise.getRoutes().size(), 0);
policy.doAdvertise = false;
readvertise.afterAddRoute(routeRef);
BOOST_CHECK_EQUAL(readvertise.getRoutes().size(), 0);
}
BOOST_AUTO_TEST_CASE(afterAddRouteNewRoute)
{
RibEntry entry;
entry.setName("/test/A");
shared_ptr<RibEntry> entryPtr = make_shared<RibEntry>(entry);
Route route;
std::list<Route> routes;
routes.push_back(route);
auto routeItr = routes.cbegin();
RibRouteRef routeRef{entryPtr, routeItr};
readvertise.afterAddRoute(routeRef);
BOOST_CHECK_EQUAL(readvertise.getRoutes().size(), 1);
std::map<Name, ReadvertisedRoute>& routesMap = readvertise.getRoutes();
BOOST_CHECK_EQUAL(routesMap.begin()->second.getPrefix(), "/test/A");
BOOST_CHECK_EQUAL(routesMap.begin()->second.getRibRoutes().size(), 1);
}
BOOST_AUTO_TEST_CASE(afterAddRouteExistingRoute)
{
RibEntry entry1;
entry1.setName("/test/A");
shared_ptr<RibEntry> entryPtr1 = make_shared<RibEntry>(entry1);
Route route1;
route1.faceId = 1;
std::list<Route> routes1;
routes1.push_back(route1);
auto routeItr1 = routes1.cbegin();
RibRouteRef routeRef1{entryPtr1, routeItr1};
RibEntry entry2;
entry2.setName("/test/A");
shared_ptr<RibEntry> entryPtr2 = make_shared<RibEntry>(entry2);
Route route2;
route2.faceId = 2;
std::list<Route> routes2;
routes2.push_back(route2);
auto routeItr2 = routes2.cbegin();
RibRouteRef routeRef2{entryPtr2, routeItr2};
readvertise.afterAddRoute(routeRef1);
readvertise.afterAddRoute(routeRef2);
BOOST_CHECK_EQUAL(readvertise.getRoutes().size(), 1);
std::map<Name, ReadvertisedRoute>& routesMap = readvertise.getRoutes();
BOOST_CHECK_EQUAL(routesMap.begin()->second.getPrefix(), "/test/A");
BOOST_CHECK_EQUAL(routesMap.begin()->second.getRibRoutes().size(), 2);
}
BOOST_AUTO_TEST_CASE(beforeRemoveRouteLastRoute)
{
RibEntry entry;
entry.setName("/test/A");
shared_ptr<RibEntry> entryPtr = make_shared<RibEntry>(entry);
Route route;
std::list<Route> routes;
routes.push_back(route);
auto routeItr = routes.cbegin();
RibRouteRef routeRef{entryPtr, routeItr};
readvertise.afterAddRoute(routeRef);
readvertise.beforeRemoveRoute(routeRef);
BOOST_CHECK_EQUAL(readvertise.getRoutes().size(), 0);
}
BOOST_AUTO_TEST_CASE(beforeRemoveRouteMoreRoutes)
{
RibEntry entry1;
entry1.setName("/test/A");
shared_ptr<RibEntry> entryPtr1 = make_shared<RibEntry>(entry1);
Route route1;
route1.faceId = 1;
std::list<Route> routes1;
routes1.push_back(route1);
auto routeItr1 = routes1.cbegin();
RibRouteRef routeRef1{entryPtr1, routeItr1};
RibEntry entry2;
entry2.setName("/test/A");
shared_ptr<RibEntry> entryPtr2 = make_shared<RibEntry>(entry2);
Route route2;
route2.faceId = 2;
std::list<Route> routes2;
routes2.push_back(route2);
auto routeItr2 = routes2.cbegin();
RibRouteRef routeRef2{entryPtr2, routeItr2};
readvertise.afterAddRoute(routeRef1);
readvertise.afterAddRoute(routeRef2);
readvertise.beforeRemoveRoute(routeRef2);
BOOST_CHECK_EQUAL(readvertise.getRoutes().size(), 1);
std::map<Name, ReadvertisedRoute>& routesMap = readvertise.getRoutes();
BOOST_CHECK_EQUAL(routesMap.begin()->second.getRibRoutes().size(), 1);
}
BOOST_AUTO_TEST_SUITE_END() // TestReadvertise
} // namespace tests
} // namespace rib
} // namespace nfd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment