62 changed files with 1541 additions and 1514 deletions
@ -1,89 +0,0 @@
|
||||
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
||||
/**
|
||||
* Copyright (c) 2014-2017, The University of Memphis, |
||||
* Regents of the University of California |
||||
* |
||||
* This file is part of NLSR (Named-data Link State Routing). |
||||
* See AUTHORS.md for complete list of NLSR authors and contributors. |
||||
* |
||||
* NLSR 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. |
||||
* |
||||
* NLSR 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 |
||||
* NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
|
||||
**/ |
||||
|
||||
#include "validator.hpp" |
||||
|
||||
namespace nlsr { |
||||
|
||||
void |
||||
Validator::checkPolicy(const ndn::Data& data, int nSteps, const ndn::OnDataValidated& onValidated, |
||||
const ndn::OnDataValidationFailed& onValidationFailed, |
||||
std::vector<shared_ptr<ndn::ValidationRequest>>& nextSteps) |
||||
{ |
||||
if (!m_shouldValidate) { |
||||
onValidated(data.shared_from_this()); |
||||
} |
||||
else { |
||||
ValidatorConfig::checkPolicy(data, nSteps, onValidated, onValidationFailed, nextSteps); |
||||
} |
||||
} |
||||
|
||||
void |
||||
Validator::afterCheckPolicy(const NextSteps& nextSteps, const OnFailure& onFailure) |
||||
{ |
||||
if (m_face == nullptr) { |
||||
onFailure("Require more information to validate the packet!"); |
||||
return; |
||||
} |
||||
|
||||
for (const std::shared_ptr<ndn::ValidationRequest>& request : nextSteps) { |
||||
|
||||
ndn::Interest& interest = request->m_interest; |
||||
|
||||
// Look for certificate in permanent storage
|
||||
std::shared_ptr<const ndn::IdentityCertificate> cert = m_certStore.find(interest.getName()); |
||||
|
||||
if (cert != nullptr) { |
||||
// If the certificate is found, no reason to express interest
|
||||
std::shared_ptr<ndn::Data> data = std::make_shared<ndn::Data>(interest.getName()); |
||||
data->setContent(cert->wireEncode()); |
||||
|
||||
Validator::onData(interest, *data, request); |
||||
} |
||||
else { |
||||
// Prepend broadcast prefix to interest name
|
||||
ndn::Name broadcastName = m_broadcastPrefix; |
||||
broadcastName.append(interest.getName()); |
||||
interest.setName(broadcastName); |
||||
|
||||
// Attempt to fetch the certificate
|
||||
m_face->expressInterest(interest, |
||||
std::bind(&Validator::onData, this, _1, _2, request), |
||||
std::bind(&Validator::onTimeout, // Nack
|
||||
this, _1, request->m_nRetries, |
||||
onFailure, |
||||
request), |
||||
std::bind(&Validator::onTimeout, |
||||
this, _1, request->m_nRetries, |
||||
onFailure, |
||||
request)); |
||||
} |
||||
} |
||||
} |
||||
|
||||
std::shared_ptr<const ndn::Data> |
||||
Validator::preCertificateValidation(const ndn::Data& data) |
||||
{ |
||||
std::shared_ptr<ndn::Data> internalData = std::make_shared<ndn::Data>(); |
||||
internalData->wireDecode(data.getContent().blockFromValue()); |
||||
return internalData; |
||||
} |
||||
|
||||
} // namespace nlsr
|
@ -1,103 +0,0 @@
|
||||
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
||||
/**
|
||||
* Copyright (c) 2014-2017, The University of Memphis, |
||||
* Regents of the University of California, |
||||
* Arizona Board of Regents. |
||||
* |
||||
* This file is part of NLSR (Named-data Link State Routing). |
||||
* See AUTHORS.md for complete list of NLSR authors and contributors. |
||||
* |
||||
* NLSR 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. |
||||
* |
||||
* NLSR 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 |
||||
* NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
|
||||
**/ |
||||
|
||||
#ifndef NLSR_VALIDATOR_HPP |
||||
#define NLSR_VALIDATOR_HPP |
||||
|
||||
#include "common.hpp" |
||||
#include "security/certificate-store.hpp" |
||||
|
||||
#include <ndn-cxx/security/validator-config.hpp> |
||||
|
||||
namespace nlsr { |
||||
|
||||
class Validator : public ndn::ValidatorConfig |
||||
{ |
||||
public: |
||||
class Error : public ndn::ValidatorConfig::Error |
||||
{ |
||||
public: |
||||
explicit |
||||
Error(const std::string& what) |
||||
: ndn::ValidatorConfig::Error(what) |
||||
{ |
||||
} |
||||
}; |
||||
|
||||
explicit |
||||
Validator(ndn::Face& face, |
||||
const ndn::Name broadcastPrefix, |
||||
const std::shared_ptr<ndn::CertificateCache>& cache, |
||||
security::CertificateStore& certStore, |
||||
const int stepLimit = 10) |
||||
: ndn::ValidatorConfig(face, cache, ndn::ValidatorConfig::DEFAULT_GRACE_INTERVAL, stepLimit) |
||||
, m_shouldValidate(true) |
||||
, m_broadcastPrefix(broadcastPrefix) |
||||
, m_certStore(certStore) |
||||
{ |
||||
m_broadcastPrefix.append("KEYS"); |
||||
} |
||||
|
||||
virtual |
||||
~Validator() |
||||
{ |
||||
} |
||||
|
||||
const ndn::Name& |
||||
getBroadcastPrefix() |
||||
{ |
||||
return m_broadcastPrefix; |
||||
} |
||||
|
||||
void |
||||
setBroadcastPrefix(const ndn::Name& broadcastPrefix) |
||||
{ |
||||
m_broadcastPrefix = broadcastPrefix; |
||||
} |
||||
|
||||
protected: |
||||
typedef std::vector<std::shared_ptr<ndn::ValidationRequest>> NextSteps; |
||||
|
||||
void |
||||
checkPolicy(const ndn::Data& data, |
||||
int nSteps, |
||||
const ndn::OnDataValidated& onValidated, |
||||
const ndn::OnDataValidationFailed& onValidationFailed, |
||||
std::vector<shared_ptr<ndn::ValidationRequest>>& nextSteps) override; |
||||
|
||||
void |
||||
afterCheckPolicy(const NextSteps& nextSteps, |
||||
const OnFailure& onFailure) override; |
||||
|
||||
std::shared_ptr<const ndn::Data> |
||||
preCertificateValidation(const ndn::Data& data) override; |
||||
|
||||
PUBLIC_WITH_TESTS_ELSE_PRIVATE: |
||||
bool m_shouldValidate; |
||||
|
||||
private: |
||||
ndn::Name m_broadcastPrefix; |
||||
security::CertificateStore& m_certStore; |
||||
}; |
||||
|
||||
} // namespace nlsr
|
||||
|
||||
#endif // NLSR_VALIDATOR_HPP
|
@ -1,34 +0,0 @@
|
||||
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
||||
/**
|
||||
* Copyright (c) 2014-2017, The University of Memphis |
||||
* Regents of the University of California, |
||||
* |
||||
* This file is part of NLSR (Named-data Link State Routing). |
||||
* See AUTHORS.md for complete list of NLSR authors and contributors. |
||||
* |
||||
* NLSR 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. |
||||
* |
||||
* NLSR 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 |
||||
* NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
|
||||
**/ |
||||
|
||||
|
||||
|
||||
#ifndef NLSR_TESTS_INTEGRATED_BOOST_TEST_HPP |
||||
#define NLSR_TESTS_INTEGRATED_BOOST_TEST_HPP |
||||
|
||||
// suppress warnings from Boost.Test
|
||||
#pragma GCC system_header |
||||
#pragma clang system_header |
||||
|
||||
#include <boost/test/unit_test.hpp> |
||||
#include <boost/concept_check.hpp> |
||||
#include <boost/test/output_test_stream.hpp> |
||||
|
||||
#endif // NLSR_TESTS_INTEGRATED_BOOST_TEST_HPP
|
@ -1,24 +0,0 @@
|
||||
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
||||
/**
|
||||
* Copyright (c) 2014-2017, The University of Memphis, |
||||
* Regents of the University of California |
||||
* |
||||
* This file is part of NLSR (Named-data Link State Routing). |
||||
* See AUTHORS.md for complete list of NLSR authors and contributors. |
||||
* |
||||
* NLSR 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. |
||||
* |
||||
* NLSR 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 |
||||
* NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
|
||||
**/ |
||||
|
||||
#define BOOST_TEST_MAIN 1 |
||||
#define BOOST_TEST_DYN_LINK 1 |
||||
|
||||
#include "boost-test.hpp" |
@ -1,273 +0,0 @@
|
||||
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
||||
/**
|
||||
* Copyright (c) 2014-2017, The University of Memphis, |
||||
* Regents of the University of California, |
||||
* Arizona Board of Regents. |
||||
* |
||||
* This file is part of NLSR (Named-data Link State Routing). |
||||
* See AUTHORS.md for complete list of NLSR authors and contributors. |
||||
* |
||||
* NLSR 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. |
||||
* |
||||
* NLSR 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 |
||||
* NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
|
||||
**/ |
||||
|
||||
#include "validator.hpp" |
||||
|
||||
#include <ndn-cxx/security/certificate-cache-ttl.hpp> |
||||
#include <ndn-cxx/security/key-chain.hpp> |
||||
#include <ndn-cxx/util/scheduler.hpp> |
||||
|
||||
#include "boost-test.hpp" |
||||
#include "common.hpp" |
||||
#include "security/certificate-store.hpp" |
||||
|
||||
namespace nlsr { |
||||
|
||||
namespace test { |
||||
|
||||
BOOST_AUTO_TEST_SUITE(TestValidator) |
||||
|
||||
struct ValidatorFixture |
||||
{ |
||||
ValidatorFixture() |
||||
: m_face2(m_face.getIoService()) |
||||
, m_scheduler(m_face.getIoService()) |
||||
, m_keyPrefix("/ndn/broadcast/KEYS") |
||||
, m_certificateCache(new ndn::CertificateCacheTtl(m_face.getIoService())) |
||||
, m_validator(m_face2, ndn::Name("/ndn/broadcast"), m_certificateCache, m_certStore) |
||||
, m_identity("/TestValidator/NLSR") |
||||
, m_wasValidated(false) |
||||
{ |
||||
m_face.setInterestFilter(m_keyPrefix, |
||||
std::bind(&ValidatorFixture::onKeyInterest, this, _1, _2), |
||||
std::bind(&ValidatorFixture::onKeyPrefixRegSuccess, this, _1), |
||||
std::bind(&ValidatorFixture::registrationFailed, this, _1, _2)); |
||||
|
||||
m_keyChain.createIdentity(m_identity); |
||||
ndn::Name certName = m_keyChain.getDefaultCertificateNameForIdentity(m_identity); |
||||
m_cert = m_keyChain.getCertificate(certName); |
||||
ndn::io::save(*m_cert, "trust-anchor.cert"); |
||||
|
||||
const std::string CONFIG = |
||||
"rule\n" |
||||
"{\n" |
||||
" id \"NSLR Hello Rule\"\n" |
||||
" for data\n" |
||||
" filter\n" |
||||
" {\n" |
||||
" type name\n" |
||||
" regex ^[^<NLSR><INFO>]*<NLSR><INFO><><>$\n" |
||||
" }\n" |
||||
" checker\n" |
||||
" {\n" |
||||
" type customized\n" |
||||
" sig-type rsa-sha256\n" |
||||
" key-locator\n" |
||||
" {\n" |
||||
" type name\n" |
||||
" hyper-relation\n" |
||||
" {\n" |
||||
" k-regex ^([^<KEY><NLSR>]*)<NLSR><KEY><ksk-.*><ID-CERT>$\n" |
||||
" k-expand \\\\1\n" |
||||
" h-relation equal\n" |
||||
" p-regex ^([^<NLSR><INFO>]*)<NLSR><INFO><><>$\n" |
||||
" p-expand \\\\1\n" |
||||
" }\n" |
||||
" }\n" |
||||
" }\n" |
||||
"}\n" |
||||
"rule\n" |
||||
"{\n" |
||||
" id \"Single Rule\"\n" |
||||
" for data\n" |
||||
" filter\n" |
||||
" {\n" |
||||
" type name\n" |
||||
" regex ^<TestValidator>([^<KEY><NLSR>]*)<NLSR><KEY><ksk-.*><><>$\n" |
||||
" }\n" |
||||
" checker\n" |
||||
" {\n" |
||||
" type fixed-signer\n" |
||||
" sig-type rsa-sha256\n" |
||||
" signer\n" |
||||
" {\n" |
||||
" type file\n" |
||||
" file-name \"trust-anchor.cert\"\n" |
||||
" }\n" |
||||
" }\n" |
||||
"}\n"; |
||||
|
||||
const boost::filesystem::path CONFIG_PATH = |
||||
(boost::filesystem::current_path() / std::string("unit-test.conf")); |
||||
|
||||
m_validator.load(CONFIG, CONFIG_PATH.native()); |
||||
} |
||||
|
||||
~ValidatorFixture() |
||||
{ |
||||
m_keyChain.deleteIdentity(m_identity); |
||||
|
||||
const boost::filesystem::path CERT_PATH = |
||||
(boost::filesystem::current_path() / std::string("trust-anchor.cert")); |
||||
boost::filesystem::remove(CERT_PATH); |
||||
} |
||||
|
||||
void |
||||
onKeyInterest(const ndn::Name& name, const ndn::Interest& interest) |
||||
{ |
||||
const ndn::Name& interestName = interest.getName(); |
||||
|
||||
ndn::Name certName = interestName.getSubName(name.size()); |
||||
|
||||
if (certName[-2].toUri() == "ID-CERT") |
||||
{ |
||||
certName = certName.getPrefix(-1); |
||||
} |
||||
else if (certName[-1].toUri() != "ID-CERT") |
||||
return; //Wrong key interest.
|
||||
|
||||
if (certName != m_cert->getName().getPrefix(-1)) |
||||
return; //No such a cert
|
||||
|
||||
std::shared_ptr<ndn::Data> data = std::make_shared<ndn::Data>(interestName); |
||||
data->setContent(m_cert->wireEncode()); |
||||
m_keyChain.signWithSha256(*data); |
||||
|
||||
m_face.put(*data); |
||||
} |
||||
|
||||
void |
||||
onKeyPrefixRegSuccess(const ndn::Name& name) |
||||
{ |
||||
BOOST_REQUIRE(true); |
||||
} |
||||
|
||||
void |
||||
registrationFailed(const ndn::Name& name, const std::string& msg) |
||||
{ |
||||
std::cerr << "Failure Info: " << msg << std::endl; |
||||
BOOST_REQUIRE(false); |
||||
} |
||||
|
||||
void |
||||
onValidated(const std::shared_ptr<const ndn::Data>& data) |
||||
{ |
||||
m_wasValidated = true; |
||||
} |
||||
|
||||
void |
||||
onValidationFailed(const std::shared_ptr<const ndn::Data>& data, |
||||
const std::string& failureInfo) |
||||
{ |
||||
std::cerr << "Failure Info: " << failureInfo << std::endl; |
||||
m_wasValidated = false; |
||||
} |
||||
|
||||
void |
||||
validate(const std::shared_ptr<const ndn::Data>& data) |
||||
{ |
||||
m_validator.validate(*data, |
||||
std::bind(&ValidatorFixture::onValidated, this, _1), |
||||
std::bind(&ValidatorFixture::onValidationFailed, this, _1, _2)); |
||||
} |
||||
|
||||
void |
||||
terminate() |
||||
{ |
||||
m_face.getIoService().stop(); |
||||
} |
||||
|
||||
protected: |
||||
ndn::Face m_face; |
||||
ndn::Face m_face2; |
||||
ndn::Scheduler m_scheduler; |
||||
const ndn::Name m_keyPrefix; |
||||
std::shared_ptr<ndn::CertificateCacheTtl> m_certificateCache; |
||||
security::CertificateStore m_certStore; |
||||
nlsr::Validator m_validator; |
||||
|
||||
ndn::KeyChain m_keyChain; |
||||
ndn::Name m_identity; |
||||
std::shared_ptr<ndn::IdentityCertificate> m_cert; |
||||
|
||||
bool m_wasValidated; |
||||
}; |
||||
|
||||
BOOST_FIXTURE_TEST_CASE(InfoCertFetch, ValidatorFixture) |
||||
{ |
||||
ndn::Name dataName = m_identity; |
||||
dataName.append("INFO").append("neighbor").append("version"); |
||||
std::shared_ptr<ndn::Data> data = std::make_shared<ndn::Data>(dataName); |
||||
m_keyChain.signByIdentity(*data, m_identity); |
||||
|
||||
m_scheduler.scheduleEvent(ndn::time::milliseconds(200), |
||||
std::bind(&ValidatorFixture::validate, this, data)); |
||||
m_scheduler.scheduleEvent(ndn::time::milliseconds(1000), |
||||
std::bind(&ValidatorFixture::terminate, this)); |
||||
BOOST_REQUIRE_NO_THROW(m_face.processEvents()); |
||||
|
||||
BOOST_CHECK(m_wasValidated); |
||||
} |
||||
|
||||
BOOST_FIXTURE_TEST_CASE(CertificateStorage, ValidatorFixture) |
||||
{ |
||||
std::vector<ndn::security::v1::CertificateSubjectDescription> subjectDescription; |
||||
|
||||
// Create an operator identity
|
||||
ndn::Name opIdentity("/TestValidator/operator/NLSR"); |
||||
m_keyChain.createIdentity(opIdentity); |
||||
|
||||
// Create an operator cert signed by the trust anchor
|
||||
ndn::Name keyName = m_keyChain.generateRsaKeyPairAsDefault(opIdentity, true); |
||||
std::shared_ptr<ndn::IdentityCertificate> opCert = |
||||
m_keyChain.prepareUnsignedIdentityCertificate(keyName, |
||||
m_identity, |
||||
ndn::time::system_clock::now(), |
||||
ndn::time::system_clock::now() |
||||
+ ndn::time::days(1), |
||||
subjectDescription); |
||||
m_keyChain.signByIdentity(*opCert, m_identity); |
||||
m_keyChain.addCertificateAsIdentityDefault(*opCert); |
||||
|
||||
// Sign data with operator cert
|
||||
ndn::Name dataName = opIdentity; |
||||
dataName.append("INFO").append("neighbor").append("version"); |
||||
std::shared_ptr<ndn::Data> data = std::make_shared<ndn::Data>(dataName); |
||||
m_keyChain.signByIdentity(*data, opIdentity); |
||||
|
||||
// Check without cert in CertificateStore
|
||||
m_scheduler.scheduleEvent(ndn::time::milliseconds(200), |
||||
std::bind(&ValidatorFixture::validate, this, data)); |
||||
m_scheduler.scheduleEvent(ndn::time::milliseconds(1000), |
||||
std::bind(&ValidatorFixture::terminate, this)); |
||||
|
||||
BOOST_REQUIRE_NO_THROW(m_face.processEvents()); |
||||
BOOST_CHECK_EQUAL(m_wasValidated, false); |
||||
|
||||
// Check with cert in CertificateStore
|
||||
m_certStore.insert(opCert); |
||||
|
||||
m_scheduler.scheduleEvent(ndn::time::milliseconds(200), |
||||
std::bind(&ValidatorFixture::validate, this, data)); |
||||
m_scheduler.scheduleEvent(ndn::time::milliseconds(1000), |
||||
std::bind(&ValidatorFixture::terminate, this)); |
||||
|
||||
BOOST_REQUIRE_NO_THROW(m_face.processEvents()); |
||||
BOOST_CHECK(m_wasValidated); |
||||
|
||||
// Cleanup
|
||||
m_keyChain.deleteIdentity(opIdentity); |
||||
} |
||||
|
||||
BOOST_AUTO_TEST_SUITE_END() |
||||
|
||||
} // namespace test
|
||||
} // namespace nlsr
|
@ -1,41 +0,0 @@
|
||||
# -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*- |
||||
|
||||
""" |
||||
Copyright (c) 2014-2016, The University of Memphis |
||||
Regents of the University of California, |
||||
|
||||
This file is part of NLSR (Named-data Link State Routing). |
||||
See AUTHORS.md for complete list of NLSR authors and contributors. |
||||
|
||||
NLSR 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. |
||||
|
||||
NLSR 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 |
||||
NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
||||
""" |
||||
|
||||
top = '..' |
||||
|
||||
def build(bld): |
||||
if bld.env['WITH_TESTS']: |
||||
integrated_test_main = bld( |
||||
target='integrated-tests-main', |
||||
name='integrated-tests-main', |
||||
features='cxx', |
||||
source=bld.path.ant_glob(['*.cpp']), |
||||
use='nlsr-objects', |
||||
) |
||||
|
||||
integrated_test_nlsr = bld.program ( |
||||
target="../integrated-tests-nlsr", |
||||
features="cxx cxxprogram", |
||||
source=bld.path.ant_glob(['nlsr/**/*.cpp']), |
||||
use='nlsr-objects integrated-tests-main', |
||||
includes='.', |
||||
install_path=None, |
||||
) |
@ -0,0 +1,214 @@
|
||||
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
||||
/**
|
||||
* Copyright (c) 2014-2017 Regents of the University of California. |
||||
* |
||||
* Based on work by Martin Ba (http://stackoverflow.com/a/26718189)
|
||||
* |
||||
* This file is distributed under the Boost Software License, Version 1.0. |
||||
* (See http://www.boost.org/LICENSE_1_0.txt)
|
||||
*/ |
||||
|
||||
#ifndef NDN_TESTS_BOOST_MULTI_LOG_FORMATTER_HPP |
||||
#define NDN_TESTS_BOOST_MULTI_LOG_FORMATTER_HPP |
||||
|
||||
#include <boost/version.hpp> |
||||
|
||||
#if BOOST_VERSION >= 105900 |
||||
#include <boost/test/unit_test_parameters.hpp> |
||||
#else |
||||
#include <boost/test/detail/unit_test_parameters.hpp> |
||||
#endif // BOOST_VERSION >= 105900
|
||||
|
||||
#include <boost/test/unit_test_log_formatter.hpp> |
||||
#include <boost/test/output/compiler_log_formatter.hpp> |
||||
#include <boost/test/output/xml_log_formatter.hpp> |
||||
|
||||
namespace boost { |
||||
namespace unit_test { |
||||
namespace output { |
||||
|
||||
/**
|
||||
* @brief Log formatter for Boost.Test that outputs the logging to multiple formatters |
||||
* |
||||
* The log formatter is designed to output to one or multiple formatters at the same time. For |
||||
* example, one HRF formatter can output to the standard output, while XML formatter output to |
||||
* the file. |
||||
* |
||||
* Usage: |
||||
* |
||||
* // Call in init_unit_test_suite: (this will override the --log_format parameter)
|
||||
* auto formatter = new boost::unit_test::output::multi_log_formatter; // same as already configured logger
|
||||
* |
||||
* // Prepare and add additional logger(s)
|
||||
* formatter.add(std::make_shared<boost::unit_test::output::xml_log_formatter>(), |
||||
* std::make_shared<std::ofstream>("out.xml")); |
||||
* |
||||
* boost::unit_test::unit_test_log.set_formatter(formatter); |
||||
* |
||||
* @note Calling `boost::unit_test::unit_test_log.set_stream(...)` will change the stream for |
||||
* the original logger. |
||||
*/ |
||||
class multi_log_formatter : public unit_test_log_formatter |
||||
{ |
||||
public: |
||||
/**
|
||||
* @brief Create instance of the logger, based on the configured logger instance |
||||
*/ |
||||
multi_log_formatter() |
||||
{ |
||||
auto format = |
||||
#if BOOST_VERSION > 105900 |
||||
runtime_config::get<output_format>(runtime_config::LOG_FORMAT); |
||||
#else |
||||
runtime_config::log_format(); |
||||
#endif // BOOST_VERSION > 105900
|
||||
|
||||
switch (format) { |
||||
default: |
||||
#if BOOST_VERSION >= 105900 |
||||
case OF_CLF: |
||||
#else |
||||
case CLF: |
||||
#endif // BOOST_VERSION >= 105900
|
||||
m_loggers.push_back({std::make_shared<compiler_log_formatter>(), nullptr}); |
||||
break; |
||||
#if BOOST_VERSION >= 105900 |
||||
case OF_XML: |
||||
#else |
||||
case XML: |
||||
#endif // BOOST_VERSION >= 105900
|
||||
m_loggers.push_back({std::make_shared<xml_log_formatter>(), nullptr}); |
||||
break; |
||||
} |
||||
} |
||||
|
||||
void |
||||
add(std::shared_ptr<unit_test_log_formatter> formatter, std::shared_ptr<std::ostream> os) |
||||
{ |
||||
m_loggers.push_back({formatter, os}); |
||||
} |
||||
|
||||
// Formatter interface
|
||||
void |
||||
log_start(std::ostream& os, counter_t test_cases_amount) |
||||
{ |
||||
for (auto& l : m_loggers) |
||||
l.logger->log_start(l.os == nullptr ? os : *l.os, test_cases_amount); |
||||
} |
||||
|
||||
void |
||||
log_finish(std::ostream& os) |
||||
{ |
||||
for (auto& l : m_loggers) |
||||
l.logger->log_finish(l.os == nullptr ? os : *l.os); |
||||
} |
||||
|
||||
void |
||||
log_build_info(std::ostream& os) |
||||
{ |
||||
for (auto& l : m_loggers) |
||||
l.logger->log_build_info(l.os == nullptr ? os : *l.os); |
||||
} |
||||
|
||||
void |
||||
test_unit_start(std::ostream& os, const test_unit& tu) |
||||
{ |
||||
for (auto& l : m_loggers) |
||||
l.logger->test_unit_start(l.os == nullptr ? os : *l.os, tu); |
||||
} |
||||
|
||||
void |
||||
test_unit_finish(std::ostream& os, const test_unit& tu, unsigned long elapsed) |
||||
{ |
||||
for (auto& l : m_loggers) |
||||
l.logger->test_unit_finish(l.os == nullptr ? os : *l.os, tu, elapsed); |
||||
} |
||||
|
||||
void |
||||
test_unit_skipped(std::ostream& os, const test_unit& tu) |
||||
{ |
||||
for (auto& l : m_loggers) |
||||
l.logger->test_unit_skipped(l.os == nullptr ? os : *l.os, tu); |
||||
} |
||||
|
||||
#if BOOST_VERSION >= 105900 |
||||
void |
||||
log_exception_start(std::ostream& os, const log_checkpoint_data& lcd, const execution_exception& ex) |
||||
{ |
||||
for (auto& l : m_loggers) |
||||
l.logger->log_exception_start(l.os == nullptr ? os : *l.os, lcd, ex); |
||||
} |
||||
|
||||
void |
||||
log_exception_finish(std::ostream& os) |
||||
{ |
||||
for (auto& l : m_loggers) |
||||
l.logger->log_exception_finish(l.os == nullptr ? os : *l.os); |
||||
} |
||||
#else |
||||
void |
||||
log_exception(std::ostream& os, const log_checkpoint_data& lcd, const execution_exception& ex) |
||||
{ |
||||
for (auto& l : m_loggers) |
||||
l.logger->log_exception(l.os == nullptr ? os : *l.os, lcd, ex); |
||||
} |
||||
#endif // BOOST_VERSION >= 105900
|
||||
|
||||
void |
||||
log_entry_start(std::ostream& os, const log_entry_data& entry_data, log_entry_types let) |
||||
{ |
||||
for (auto& l : m_loggers) |
||||
l.logger->log_entry_start(l.os == nullptr ? os : *l.os, entry_data, let); |
||||
} |
||||
|
||||
void |
||||
log_entry_value(std::ostream& os, const_string value) |
||||
{ |
||||
for (auto& l : m_loggers) |
||||
l.logger->log_entry_value(l.os == nullptr ? os : *l.os, value); |
||||
} |
||||
|
||||
void |
||||
log_entry_finish(std::ostream& os) |
||||
{ |
||||
for (auto& l : m_loggers) |
||||
l.logger->log_entry_finish(l.os == nullptr ? os : *l.os); |
||||
} |
||||
|
||||
#if BOOST_VERSION >= 105900 |
||||
void |
||||
entry_context_start(std::ostream& os, log_level level) |
||||
{ |
||||
for (auto& l : m_loggers) |
||||
l.logger->entry_context_start(l.os == nullptr ? os : *l.os, level); |
||||
} |
||||
|
||||
void |
||||
log_entry_context(std::ostream& os, const_string value) |
||||
{ |
||||
for (auto& l : m_loggers) |
||||
l.logger->log_entry_context(l.os == nullptr ? os : *l.os, value); |
||||
} |
||||
|
||||
void |
||||
entry_context_finish(std::ostream& os) |
||||
{ |
||||
for (auto& l : m_loggers) |
||||
l.logger->entry_context_finish(l.os == nullptr ? os : *l.os); |
||||
} |
||||
#endif // BOOST_VERSION >= 105900
|
||||
|
||||
private: |
||||
struct LoggerInfo |
||||
{ |
||||
std::shared_ptr<unit_test_log_formatter> logger; |
||||
std::shared_ptr<std::ostream> os; |
||||
}; |
||||
std::vector<LoggerInfo> m_loggers; |
||||
}; |
||||
|
||||
} // namespace output
|
||||
} // namespace unit_test
|
||||
} // namespace boost
|
||||
|
||||
#endif // NDN_TESTS_BOOST_MULTI_LOG_FORMATTER_HPP
|
@ -1,102 +0,0 @@
|
||||
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
||||
/**
|
||||
* Copyright (c) 2014-2017, The University of Memphis, |
||||
* Regents of the University of California |
||||
* |
||||
* This file is part of NLSR (Named-data Link State Routing). |
||||
* See AUTHORS.md for complete list of NLSR authors and contributors. |
||||
* |
||||
* NLSR 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. |
||||
* |
||||
* NLSR 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 |
||||
* NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
|
||||
* |
||||
**/ |
||||
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
||||
/**
|
||||
* Copyright (c) 2013-2017 Regents of the University of California. |
||||
* |
||||
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
||||
* |
||||
* ndn-cxx library is free software: you can redistribute it and/or modify it under the |
||||
* terms of the GNU Lesser General Public License as published by the Free Software |
||||
* Foundation, either version 3 of the License, or (at your option) any later version. |
||||
* |
||||
* ndn-cxx library 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 Lesser General Public License for more details. |
||||
* |
||||
* You should have received copies of the GNU General Public License and GNU Lesser |
||||
* General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
||||
* <http://www.gnu.org/licenses/>.
|
||||
* |
||||
* See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
||||
*/ |
||||
|
||||
#include <ndn-cxx/security/key-chain.hpp> |
||||
|
||||
#include "boost-test.hpp" |
||||
|
||||
namespace nlsr { |
||||
|
||||
// OSX KeyChain, when used on a headless server,
|
||||
// forbids usage of a private key if that key isn't created by the calling process.
|
||||
// Therefore, unit testing must create its own key pair.
|
||||
|
||||
class IdentityFixture |
||||
{ |
||||
public: |
||||
IdentityFixture() |
||||
{ |
||||
// save the old default identity
|
||||
try { |
||||
m_oldDefaultIdentity = m_keyChain.getDefaultIdentity(); |
||||
m_hasOldDefaultIdentity = true; |
||||
} |
||||
catch (ndn::SecPublicInfo::Error& e) { |
||||
m_hasOldDefaultIdentity = false; |
||||
} |
||||
|
||||
m_newIdentity = "/nlsr-test-identity"; |
||||
m_newIdentity.appendVersion(); |
||||
|
||||
// create the new identity and self-signed certificate
|
||||
m_keyChain.createIdentity(m_newIdentity); |
||||
|
||||
// set the new identity as default identity,
|
||||
// and the corresponding certificate becomes the default certificate
|
||||
m_keyChain.setDefaultIdentity(m_newIdentity); |
||||
} |
||||
|
||||
~IdentityFixture() |
||||
{ |
||||
// recover the old default setting
|
||||
if (m_hasOldDefaultIdentity) { |
||||
m_keyChain.setDefaultIdentity(m_oldDefaultIdentity); |
||||
} |
||||
|
||||
// remove the temporarily created identity and certificates
|
||||
// XXX This has no effect if oldDefaultIdentity doesn't exist.
|
||||
// newIdentity would be kept as default.
|
||||
m_keyChain.deleteIdentity(m_newIdentity); |
||||
} |
||||
|
||||
private: |
||||
ndn::KeyChain m_keyChain; |
||||
bool m_hasOldDefaultIdentity; |
||||
ndn::Name m_oldDefaultIdentity; |
||||
ndn::Name m_newIdentity; |
||||
}; |
||||
|
||||
BOOST_GLOBAL_FIXTURE(IdentityFixture) |
||||
#if BOOST_VERSION >= 105900 |
||||
; |
||||
#endif // BOOST_VERSION >= 105900
|
||||
|
||||
} // namespace nlsr
|
@ -0,0 +1,143 @@
|
||||
/* -*- 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 NLSR (Named-data Link State Routing). |
||||
* See AUTHORS.md for complete list of NLSR authors and contributors. |
||||
* |
||||
* NLSR 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. |
||||
* |
||||
* NLSR 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 |
||||
* NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/ |
||||
|
||||
#include "identity-management-fixture.hpp" |
||||
|
||||
#include <ndn-cxx/util/io.hpp> |
||||
#include <ndn-cxx/security/v2/additional-description.hpp> |
||||
|
||||
#include <boost/filesystem.hpp> |
||||
|
||||
namespace nlsr { |
||||
namespace tests { |
||||
|
||||
namespace v2 = ndn::security::v2; |
||||
namespace io = ndn::io; |
||||
namespace time = ndn::time; |
||||
|
||||
IdentityManagementBaseFixture::~IdentityManagementBaseFixture() |
||||
{ |
||||
boost::system::error_code ec; |
||||
for (const auto& certFile : m_certFiles) { |
||||
boost::filesystem::remove(certFile, ec); // ignore error
|
||||
} |
||||
} |
||||
|
||||
bool |
||||
IdentityManagementBaseFixture::saveCertToFile(const ndn::Data& obj, |
||||
const std::string& filename) |
||||
{ |
||||
m_certFiles.insert(filename); |
||||
try { |
||||
io::save(obj, filename); |
||||
return true; |
||||
} |
||||
catch (const io::Error&) { |
||||
return false; |
||||
} |
||||
} |
||||
|
||||
IdentityManagementFixture::IdentityManagementFixture() |
||||
: m_keyChain("pib-memory:", "tpm-memory:") |
||||
{ |
||||
} |
||||
|
||||
ndn::security::Identity |
||||
IdentityManagementFixture::addIdentity(const ndn::Name& identityName, |
||||
const ndn::KeyParams& params) |
||||
{ |
||||
auto identity = m_keyChain.createIdentity(identityName, params); |
||||
m_identities.insert(identityName); |
||||
return identity; |
||||
} |
||||
|
||||
bool |
||||
IdentityManagementFixture::saveCertificate(const ndn::security::Identity& identity, |
||||
const std::string& filename) |
||||
{ |
||||
try { |
||||
auto cert = identity.getDefaultKey().getDefaultCertificate(); |
||||
return saveCertToFile(cert, filename); |
||||
} |
||||
catch (const ndn::security::Pib::Error&) { |
||||
return false; |
||||
} |
||||
} |
||||
|
||||
ndn::security::Identity |
||||
IdentityManagementFixture::addSubCertificate(const ndn::Name& subIdentityName, |
||||
const ndn::security::Identity& issuer, |
||||
const ndn::KeyParams& params) |
||||
{ |
||||
auto subIdentity = addIdentity(subIdentityName, params); |
||||
|
||||
v2::Certificate request = subIdentity.getDefaultKey().getDefaultCertificate(); |
||||
|
||||
request.setName(request.getKeyName().append("parent").appendVersion()); |
||||
|
||||
ndn::SignatureInfo info; |
||||
info.setValidityPeriod(ndn::security::ValidityPeriod(time::system_clock::now(), |
||||
time::system_clock::now() |
||||
+ time::days(7300))); |
||||
|
||||
v2::AdditionalDescription description; |
||||
description.set("type", "sub-certificate"); |
||||
info.appendTypeSpecificTlv(description.wireEncode()); |
||||
|
||||
m_keyChain.sign(request, ndn::signingByIdentity(issuer).setSignatureInfo(info)); |
||||
m_keyChain.setDefaultCertificate(subIdentity.getDefaultKey(), request); |
||||
|
||||
return subIdentity; |
||||
} |
||||
|
||||
v2::Certificate |
||||
IdentityManagementFixture::addCertificate(const ndn::security::Key& key, |
||||
const std::string& issuer) |
||||
{ |
||||
ndn::Name certificateName = key.getName(); |
||||
certificateName |
||||
.append(issuer) |
||||
.appendVersion(); |
||||
v2::Certificate certificate; |
||||
certificate.setName(certificateName); |
||||
|
||||
// set metainfo
|
||||
certificate.setContentType(ndn::tlv::ContentType_Key); |
||||
certificate.setFreshnessPeriod(time::hours(1)); |
||||
|
||||
// set content
|
||||
certificate.setContent(key.getPublicKey().data(), key.getPublicKey().size()); |
||||
|
||||
// set signature-info
|
||||
ndn::SignatureInfo info; |
||||
info.setValidityPeriod(ndn::security::ValidityPeriod(time::system_clock::now(), |
||||
time::system_clock::now() + time::days(10))); |
||||
|
||||
m_keyChain.sign(certificate, ndn::signingByKey(key).setSignatureInfo(info)); |
||||
return certificate; |
||||
} |
||||
|
||||
} // namespace tests
|
||||
} // namespace nlsr
|
@ -0,0 +1,106 @@
|
||||
/* -*- 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 NLSR (Named-data Link State Routing). |
||||
* See AUTHORS.md for complete list of NLSR authors and contributors. |
||||
* |
||||
* NLSR 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. |
||||
* |
||||
* NLSR 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 |
||||
* NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/ |
||||
|
||||
#ifndef NLSR_TESTS_IDENTITY_MANAGEMENT_FIXTURE_HPP |
||||
#define NLSR_TESTS_IDENTITY_MANAGEMENT_FIXTURE_HPP |
||||
|
||||
#include "boost-test.hpp" |
||||
#include "test-home-fixture.hpp" |
||||
|
||||
#include <vector> |
||||
|
||||
#include <ndn-cxx/security/v2/key-chain.hpp> |
||||
#include <ndn-cxx/security/signing-helpers.hpp> |
||||
|
||||
namespace nlsr { |
||||
namespace tests { |
||||
|
||||
class IdentityManagementBaseFixture : public TestHomeFixture<DefaultPibDir> |
||||
{ |
||||
public: |
||||
~IdentityManagementBaseFixture(); |
||||
|
||||
bool |
||||
saveCertToFile(const ndn::Data& obj, const std::string& filename); |
||||
|
||||
protected: |
||||
std::set<ndn::Name> m_identities; |
||||
std::set<std::string> m_certFiles; |
||||
}; |
||||
|
||||
/**
|
||||
* @brief A test suite level fixture to help with identity management |
||||
* |
||||
* Test cases in the suite can use this fixture to create identities. Identities, |
||||
* certificates, and saved certificates are automatically removed during test teardown. |
||||
*/ |
||||
class IdentityManagementFixture : public IdentityManagementBaseFixture |
||||
{ |
||||
public: |
||||
IdentityManagementFixture(); |
||||
|
||||
/**
|
||||
* @brief Add identity @p identityName |
||||
* @return name of the created self-signed certificate |
||||
*/ |
||||
ndn::security::Identity |
||||
addIdentity(const ndn::Name& identityName, const ndn::KeyParams& params = ndn::KeyChain::getDefaultKeyParams()); |
||||
|
||||
/**
|
||||
* @brief Save identity certificate to a file |
||||
* @param identity identity |
||||
* @param filename file name, should be writable |
||||
* @return whether successful |
||||
*/ |
||||
bool |
||||
saveCertificate(const ndn::security::Identity& identity, const std::string& filename); |
||||
|
||||
/**
|
||||
* @brief Issue a certificate for \p subidentityName signed by \p issuer |
||||
* |
||||
* If identity does not exist, it is created. |
||||
* A new key is generated as the default key for identity. |
||||
* A default certificate for the key is signed by the issuer using its default certificate. |
||||
* |
||||
* @return the sub identity |
||||
*/ |
||||
ndn::security::Identity |
||||
addSubCertificate(const ndn::Name& identityName, const ndn::security::Identity& issuer, |
||||
const ndn::KeyParams& params = ndn::KeyChain::getDefaultKeyParams()); |
||||
|
||||
/**
|
||||
* @brief Add a self-signed certificate to @p key with issuer ID @p issuer |
||||
*/ |
||||
ndn::security::v2::Certificate |
||||
addCertificate(const ndn::security::Key& key, const std::string& issuer); |
||||
|
||||
protected: |
||||
ndn::KeyChain m_keyChain; |
||||
}; |
||||
|
||||
} // namespace tests
|
||||
} // namespace nlsr
|
||||
|
||||
#endif // NLSR_TESTS_IDENTITY_MANAGEMENT_FIXTURE_HPP
|
@ -0,0 +1,132 @@
|
||||
/* -*- 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 NLSR (Named-data Link State Routing). |
||||
* See AUTHORS.md for complete list of NLSR authors and contributors. |
||||
* |
||||
* NLSR 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. |
||||
* |
||||
* NLSR 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 |
||||
* NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/ |
||||
|
||||
#ifndef NLSR_TEST_HOME_FIXTURE_HPP |
||||
#define NLSR_TEST_HOME_FIXTURE_HPP |
||||
|
||||
#include "boost-test.hpp" |
||||
|
||||
#include <fstream> |
||||
|
||||
#include <ndn-cxx/security/v2/key-chain.hpp> |
||||
|
||||
#include <boost/filesystem.hpp> |
||||
#include <boost/algorithm/string.hpp> |
||||
|
||||
namespace nlsr { |
||||
namespace tests { |
||||
|
||||
/**
|
||||
* @brief Fixture to adjust/restore NDN_CLIENT_PIB and NDN_CLIENT_TPM paths |
||||
* |
||||
* Note that the specified PATH will be removed after fixture is destroyed. |
||||
* **Do not specify non-temporary paths.** |
||||
*/ |
||||
template<class Path> |
||||
class PibDirFixture |
||||
{ |
||||
public: |
||||
PibDirFixture() |
||||
: m_pibDir(Path().PATH) |
||||
{ |
||||
if (getenv("NDN_CLIENT_PIB") != nullptr) { |
||||
m_oldPib = getenv("NDN_CLIENT_PIB"); |
||||
} |
||||
if (getenv("NDN_CLIENT_TPM") != nullptr) { |
||||
m_oldTpm = getenv("NDN_CLIENT_TPM"); |
||||
} |
||||
|
||||
/// @todo Consider change to an in-memory PIB/TPM
|
||||
setenv("NDN_CLIENT_PIB", ("pib-sqlite3:" + m_pibDir).c_str(), true); |
||||
setenv("NDN_CLIENT_TPM", ("tpm-file:" + m_pibDir).c_str(), true); |
||||
} |
||||
|
||||
~PibDirFixture() |
||||
{ |
||||
if (!m_oldPib.empty()) { |
||||
setenv("NDN_CLIENT_PIB", m_oldPib.c_str(), true); |
||||
} |
||||
else { |
||||
unsetenv("NDN_CLIENT_PIB"); |
||||
} |
||||
|
||||
if (!m_oldTpm.empty()) { |
||||
setenv("NDN_CLIENT_TPM", m_oldTpm.c_str(), true); |
||||
} |
||||
else { |
||||
unsetenv("NDN_CLIENT_TPM"); |
||||
} |
||||
|
||||
boost::filesystem::remove_all(m_pibDir); |
||||
//const_cast<std::string&>(ndn::security::v2::KeyChain::getDefaultPibLocator()).clear();
|
||||
//const_cast<std::string&>(ndn::security::v2::KeyChain::getDefaultTpmLocator()).clear();
|
||||
} |
||||
|
||||
protected: |
||||
const std::string m_pibDir; |
||||
|
||||
private: |
||||
std::string m_oldPib; |
||||
std::string m_oldTpm; |
||||
}; |
||||
|
||||
/**
|
||||
* @brief Extension of PibDirFixture to set TEST_HOME variable and allow config file creation |
||||
*/ |
||||
template<class Path> |
||||
class TestHomeFixture : public PibDirFixture<Path> |
||||
{ |
||||
public: |
||||
TestHomeFixture() |
||||
{ |
||||
setenv("TEST_HOME", this->m_pibDir.c_str(), true); |
||||
} |
||||
|
||||
~TestHomeFixture() |
||||
{ |
||||
unsetenv("TEST_HOME"); |
||||
} |
||||
|
||||
void |
||||
createClientConf(std::initializer_list<std::string> lines) |
||||
{ |
||||
boost::filesystem::create_directories(boost::filesystem::path(this->m_pibDir) / ".ndn"); |
||||
std::ofstream of((boost::filesystem::path(this->m_pibDir) / ".ndn" / "client.conf").c_str()); |
||||
for (auto line : lines) { |
||||
boost::replace_all(line, "%PATH%", this->m_pibDir); |
||||
of << line << std::endl; |
||||
} |
||||
} |
||||
}; |
||||
|
||||
struct DefaultPibDir |
||||
{ |
||||
const std::string PATH = "build/keys"; |
||||
}; |
||||
|
||||
} // namespace tests
|
||||
} // namespace nlsr
|
||||
|
||||
#endif // NLSR_TEST_HOME_FIXTURE_HPP
|
Loading…
Reference in new issue