blob: 6577f6d71d074a01921f004324d66a63039b756b [file] [log] [blame]
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
* Copyright (C) 2014 Named Data Networking Project
* See COPYING for copyright and distribution information.
*/
#include "mgmt/strategy-choice-manager.hpp"
#include "face/face.hpp"
#include "mgmt/internal-face.hpp"
#include "table/name-tree.hpp"
#include "table/strategy-choice.hpp"
#include "fw/forwarder.hpp"
#include "fw/strategy.hpp"
#include "tests/face/dummy-face.hpp"
#include "tests/fw/dummy-strategy.hpp"
#include "tests/test-common.hpp"
#include "validation-common.hpp"
namespace nfd {
namespace tests {
NFD_LOG_INIT("StrategyChoiceManagerTest");
class StrategyChoiceManagerFixture : protected BaseFixture
{
public:
StrategyChoiceManagerFixture()
: m_strategyChoice(m_forwarder.getStrategyChoice())
, m_face(make_shared<InternalFace>())
, m_manager(m_strategyChoice, m_face)
, m_callbackFired(false)
{
m_strategyChoice.install(make_shared<DummyStrategy>(boost::ref(m_forwarder), "/localhost/nfd/strategy/test-strategy-a"));
m_strategyChoice.insert("ndn:/", "/localhost/nfd/strategy/test-strategy-a");
}
virtual
~StrategyChoiceManagerFixture()
{
}
void
validateControlResponseCommon(const Data& response,
const Name& expectedName,
uint32_t expectedCode,
const std::string& expectedText,
ControlResponse& control)
{
m_callbackFired = true;
Block controlRaw = response.getContent().blockFromValue();
control.wireDecode(controlRaw);
NFD_LOG_DEBUG("received control response"
<< " Name: " << response.getName()
<< " code: " << control.getCode()
<< " text: " << control.getText());
BOOST_CHECK_EQUAL(response.getName(), expectedName);
BOOST_CHECK_EQUAL(control.getCode(), expectedCode);
BOOST_CHECK_EQUAL(control.getText(), expectedText);
}
void
validateControlResponse(const Data& response,
const Name& expectedName,
uint32_t expectedCode,
const std::string& expectedText)
{
ControlResponse control;
validateControlResponseCommon(response, expectedName,
expectedCode, expectedText, control);
if (!control.getBody().empty())
{
BOOST_FAIL("found unexpected control response body");
}
}
void
validateControlResponse(const Data& response,
const Name& expectedName,
uint32_t expectedCode,
const std::string& expectedText,
const Block& expectedBody)
{
ControlResponse control;
validateControlResponseCommon(response, expectedName,
expectedCode, expectedText, control);
BOOST_REQUIRE(!control.getBody().empty());
BOOST_REQUIRE(control.getBody().value_size() == expectedBody.value_size());
BOOST_CHECK(memcmp(control.getBody().value(), expectedBody.value(),
expectedBody.value_size()) == 0);
}
bool
didCallbackFire()
{
return m_callbackFired;
}
void
resetCallbackFired()
{
m_callbackFired = false;
}
shared_ptr<InternalFace>&
getFace()
{
return m_face;
}
StrategyChoiceManager&
getManager()
{
return m_manager;
}
StrategyChoice&
getStrategyChoice()
{
return m_strategyChoice;
}
void
addInterestRule(const std::string& regex,
ndn::IdentityCertificate& certificate)
{
m_manager.addInterestRule(regex, certificate);
}
protected:
Forwarder m_forwarder;
StrategyChoice& m_strategyChoice;
shared_ptr<InternalFace> m_face;
StrategyChoiceManager m_manager;
private:
bool m_callbackFired;
};
class AllStrategiesFixture : public StrategyChoiceManagerFixture
{
public:
AllStrategiesFixture()
{
m_strategyChoice.install(make_shared<DummyStrategy>(boost::ref(m_forwarder), "/localhost/nfd/strategy/test-strategy-b"));
}
virtual
~AllStrategiesFixture()
{
}
};
template <typename T> class AuthorizedCommandFixture : public CommandFixture<T>
{
public:
AuthorizedCommandFixture()
{
const std::string regex = "^<localhost><nfd><strategy-choice>";
T::addInterestRule(regex, *CommandFixture<T>::m_certificate);
}
virtual
~AuthorizedCommandFixture()
{
}
};
BOOST_FIXTURE_TEST_SUITE(MgmtStrategyChoiceManager,
AuthorizedCommandFixture<AllStrategiesFixture>)
BOOST_FIXTURE_TEST_CASE(TestFireInterestFilter, AllStrategiesFixture)
{
shared_ptr<Interest> command(make_shared<Interest>("/localhost/nfd/strategy-choice"));
getFace()->onReceiveData +=
bind(&StrategyChoiceManagerFixture::validateControlResponse, this, _1,
command->getName(), 400, "Malformed command");
getFace()->sendInterest(*command);
BOOST_REQUIRE(didCallbackFire());
}
BOOST_FIXTURE_TEST_CASE(MalformedCommmand, AllStrategiesFixture)
{
shared_ptr<Interest> command(make_shared<Interest>("/localhost/nfd/strategy-choice"));
getFace()->onReceiveData +=
bind(&StrategyChoiceManagerFixture::validateControlResponse, this, _1,
command->getName(), 400, "Malformed command");
getManager().onStrategyChoiceRequest(*command);
BOOST_REQUIRE(didCallbackFire());
}
BOOST_FIXTURE_TEST_CASE(UnsignedCommand, AllStrategiesFixture)
{
ndn::nfd::FibManagementOptions options;
options.setName("/test");
options.setStrategy("/localhost/nfd/strategy/best-route");
Block encodedOptions(options.wireEncode());
Name commandName("/localhost/nfd/strategy-choice");
commandName.append("set");
commandName.append(encodedOptions);
shared_ptr<Interest> command(make_shared<Interest>(commandName));
getFace()->onReceiveData +=
bind(&StrategyChoiceManagerFixture::validateControlResponse, this, _1,
command->getName(), 401, "Signature required");
getManager().onStrategyChoiceRequest(*command);
BOOST_REQUIRE(didCallbackFire());
}
BOOST_FIXTURE_TEST_CASE(UnauthorizedCommand,
UnauthorizedCommandFixture<StrategyChoiceManagerFixture>)
{
ndn::nfd::FibManagementOptions options;
options.setName("/test");
options.setStrategy("/localhost/nfd/strategy/best-route");
Block encodedOptions(options.wireEncode());
Name commandName("/localhost/nfd/strategy-choice");
commandName.append("set");
commandName.append(encodedOptions);
shared_ptr<Interest> command(make_shared<Interest>(commandName));
generateCommand(*command);
getFace()->onReceiveData +=
bind(&StrategyChoiceManagerFixture::validateControlResponse, this, _1,
command->getName(), 403, "Unauthorized command");
getManager().onStrategyChoiceRequest(*command);
BOOST_REQUIRE(didCallbackFire());
}
BOOST_AUTO_TEST_CASE(UnsupportedVerb)
{
ndn::nfd::FibManagementOptions options;
options.setStrategy("/localhost/nfd/strategy/test-strategy-b");
Block encodedOptions(options.wireEncode());
Name commandName("/localhost/nfd/strategy-choice");
commandName.append("unsupported");
commandName.append(encodedOptions);
shared_ptr<Interest> command(make_shared<Interest>(commandName));
generateCommand(*command);
getFace()->onReceiveData +=
bind(&StrategyChoiceManagerFixture::validateControlResponse, this, _1,
command->getName(), 501, "Unsupported command");
getManager().onValidatedStrategyChoiceRequest(command);
BOOST_REQUIRE(didCallbackFire());
}
BOOST_AUTO_TEST_CASE(BadOptionParse)
{
Name commandName("/localhost/nfd/strategy-choice");
commandName.append("set");
commandName.append("NotReallyOptions");
shared_ptr<Interest> command(make_shared<Interest>(commandName));
generateCommand(*command);
getFace()->onReceiveData +=
bind(&StrategyChoiceManagerFixture::validateControlResponse, this, _1,
command->getName(), 400, "Malformed command");
getManager().onValidatedStrategyChoiceRequest(command);
BOOST_REQUIRE(didCallbackFire());
}
BOOST_AUTO_TEST_CASE(SetStrategies)
{
ndn::nfd::FibManagementOptions options;
options.setName("/test");
options.setStrategy("/localhost/nfd/strategy/test-strategy-b");
Block encodedOptions(options.wireEncode());
Name commandName("/localhost/nfd/strategy-choice");
commandName.append("set");
commandName.append(encodedOptions);
shared_ptr<Interest> command(make_shared<Interest>(commandName));
getFace()->onReceiveData +=
bind(&StrategyChoiceManagerFixture::validateControlResponse, this, _1,
command->getName(), 200, "Success", encodedOptions);
getManager().onValidatedStrategyChoiceRequest(command);
BOOST_REQUIRE(didCallbackFire());
fw::Strategy& strategy = getStrategyChoice().findEffectiveStrategy("/test");
BOOST_REQUIRE_EQUAL(strategy.getName(), "/localhost/nfd/strategy/test-strategy-b");
resetCallbackFired();
getFace()->onReceiveData.clear();
}
BOOST_AUTO_TEST_CASE(SetUnsupportedStrategy)
{
ndn::nfd::FibManagementOptions options;
options.setName("/test");
options.setStrategy("/localhost/nfd/strategy/unit-test-doesnotexist");
Block encodedOptions(options.wireEncode());
Name commandName("/localhost/nfd/strategy-choice");
commandName.append("set");
commandName.append(encodedOptions);
shared_ptr<Interest> command(make_shared<Interest>(commandName));
generateCommand(*command);
getFace()->onReceiveData +=
bind(&StrategyChoiceManagerFixture::validateControlResponse, this, _1,
command->getName(), 504, "Unsupported strategy");
getManager().onValidatedStrategyChoiceRequest(command);
BOOST_REQUIRE(didCallbackFire());
fw::Strategy& strategy = getStrategyChoice().findEffectiveStrategy("/test");
BOOST_CHECK_EQUAL(strategy.getName(), "/localhost/nfd/strategy/test-strategy-a");
}
class DefaultStrategyOnlyFixture : public StrategyChoiceManagerFixture
{
public:
DefaultStrategyOnlyFixture()
: StrategyChoiceManagerFixture()
{
}
virtual
~DefaultStrategyOnlyFixture()
{
}
};
/// \todo I'm not sure this code branch (code 405) can happen. The manager tests for the strategy first and will return 504.
// BOOST_FIXTURE_TEST_CASE(SetNotInstalled, DefaultStrategyOnlyFixture)
// {
// BOOST_REQUIRE(!getStrategyChoice().hasStrategy("/localhost/nfd/strategy/test-strategy-b"));
// ndn::nfd::FibManagementOptions options;
// options.setName("/test");
// options.setStrategy("/localhost/nfd/strategy/test-strategy-b");
// Block encodedOptions(options.wireEncode());
// Name commandName("/localhost/nfd/strategy-choice");
// commandName.append("set");
// commandName.append(encodedOptions);
// shared_ptr<Interest> command(make_shared<Interest>(commandName));
// generateCommand(*command);
// getFace()->onReceiveData +=
// bind(&StrategyChoiceManagerFixture::validateControlResponse, this, _1,
// command->getName(), 405, "Strategy not installed");
// getManager().onValidatedStrategyChoiceRequest(command);
// BOOST_REQUIRE(didCallbackFire());
// fw::Strategy& strategy = getStrategyChoice().findEffectiveStrategy("/test");
// BOOST_CHECK_EQUAL(strategy.getName(), "/localhost/nfd/strategy/test-strategy-a");
// }
BOOST_AUTO_TEST_CASE(Unset)
{
ndn::nfd::FibManagementOptions options;
options.setName("/test");
BOOST_REQUIRE(m_strategyChoice.insert("/test", "/localhost/nfd/strategy/test-strategy-b"));
BOOST_REQUIRE_EQUAL(m_strategyChoice.findEffectiveStrategy("/test").getName(),
"/localhost/nfd/strategy/test-strategy-b");
Block encodedOptions(options.wireEncode());
Name commandName("/localhost/nfd/strategy-choice");
commandName.append("unset");
commandName.append(encodedOptions);
shared_ptr<Interest> command(make_shared<Interest>(commandName));
generateCommand(*command);
getFace()->onReceiveData +=
bind(&StrategyChoiceManagerFixture::validateControlResponse, this, _1,
command->getName(), 200, "Success", encodedOptions);
getManager().onValidatedStrategyChoiceRequest(command);
BOOST_REQUIRE(didCallbackFire());
BOOST_CHECK_EQUAL(m_strategyChoice.findEffectiveStrategy("/test").getName(),
"/localhost/nfd/strategy/test-strategy-a");
}
BOOST_AUTO_TEST_CASE(UnsetRoot)
{
ndn::nfd::FibManagementOptions options;
options.setName("/");
Block encodedOptions(options.wireEncode());
Name commandName("/localhost/nfd/strategy-choice");
commandName.append("unset");
commandName.append(encodedOptions);
shared_ptr<Interest> command(make_shared<Interest>(commandName));
generateCommand(*command);
getFace()->onReceiveData +=
bind(&StrategyChoiceManagerFixture::validateControlResponse, this, _1,
command->getName(), 403, "Cannot unset root prefix strategy");
getManager().onValidatedStrategyChoiceRequest(command);
BOOST_REQUIRE(didCallbackFire());
BOOST_CHECK_EQUAL(m_strategyChoice.findEffectiveStrategy("/test").getName(),
"/localhost/nfd/strategy/test-strategy-a");
}
BOOST_AUTO_TEST_SUITE_END()
} // namespace tests
} // namespace nfd