tests+mgmt: Import common unit test code NFD
This commit introduces BaseFixture, UnitTestTimeFixture,
IdentityManagementFixture, and IdentityManagementTimeFixture.
In addition to that, the command line of the unit test binary (for boost
libraries before 1.62) are extended to support multi-format output.
This commit also addresses a strange bug in the management tool that was
causing spurious segmentation faults during unit tests.
Change-Id: Ib34b4007773f0b35d9ed919eebbdc4e0d413fa1e
diff --git a/src/mgmt/management-tool.cpp b/src/mgmt/management-tool.cpp
index 62b57af..9ade73f 100644
--- a/src/mgmt/management-tool.cpp
+++ b/src/mgmt/management-tool.cpp
@@ -337,36 +337,40 @@
throw Error("input does not contain a valid Data packet");
}
- //determine whether the data is a self-signed certificate
+ // determine whether the data is a self-signed certificate
shared_ptr<Regex> regex1 = make_shared<Regex>("(<>*)<KEY>(<>+)<ID-CERT><>");
if (regex1->match(data->getName())) {
IdentityCertificate scert(*data);
Name keyName = scert.getPublicKeyName();
+ if (keyName.getPrefix(zoneName.size()) != zoneName) {
+ throw Error("the input key does not belong to the zone");
+ }
+
Name keyLocator = scert.getSignature().getKeyLocator().getName();
- //if it is, extract the content and name from the data, and resign it using the dsk.
+ // if it is, extract the content and name from the data, and resign it using the dsk.
shared_ptr<Regex> regex2 = make_shared<Regex>("(<>*)<KEY>(<>+)<ID-CERT>");
BOOST_VERIFY(regex2->match(keyLocator) == true);
if (keyName == regex2->expand("\\1\\2")) {
- shared_ptr<Data> pre = data;
- Name name = pre->getName();
- //check whether the name is legal or not. if not converting it to a legal name
- if (zoneName != regex1->expand("\\1")) {
- Name comp1 = regex1->expand("\\1").getSubName(zoneName.size());
- Name comp2 = regex1->expand("\\2");
- name = zoneName;
- name.append("KEY");
- name.append(comp1);
- name.append(comp2);
- name.append("ID-CERT");
- name.append(pre->getName().get(-1));
+
+ Name canonicalName;
+ canonicalName
+ .append(zoneName)
+ .append("KEY")
+ .append(keyName.getSubName(zoneName.size(), keyName.size() - zoneName.size()))
+ .append("ID-CERT")
+ .append(data->getName().get(-1));
+
+ if (data->getName() != canonicalName) {
+ // name need to be adjusted
+ auto newData = make_shared<Data>();
+ newData->setName(canonicalName);
+ newData->setMetaInfo(data->getMetaInfo());
+ newData->setContent(data->getContent());
+ m_keyChain.sign(*newData);
+
+ data = newData;
}
-
- data = make_shared<Data>();
- data->setName(name);
- data->setContent(pre->getContent());
-
- m_keyChain.sign(*data, dskCertName);
}
}
diff --git a/tests/boost-multi-log-formatter.hpp b/tests/boost-multi-log-formatter.hpp
new file mode 100644
index 0000000..ae37416
--- /dev/null
+++ b/tests/boost-multi-log-formatter.hpp
@@ -0,0 +1,214 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2015 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
diff --git a/tests/boost-test.hpp b/tests/boost-test.hpp
index b900be4..89e67b8 100644
--- a/tests/boost-test.hpp
+++ b/tests/boost-test.hpp
@@ -1,3 +1,29 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2014-2016, 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 NDNS (Named Data Networking Domain Name Service) and is
+ * based on the code written as part of NFD (Named Data Networking Daemon).
+ * See AUTHORS.md for complete list of NDNS authors and contributors.
+ *
+ * NDNS 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.
+ *
+ * NDNS 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
+ * NDNS, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
+ */
+
#ifndef NDNS_TESTS_BOOST_TEST_HPP
#define NDNS_TESTS_BOOST_TEST_HPP
@@ -5,23 +31,9 @@
#pragma GCC system_header
#pragma clang system_header
-#include "logger.hpp"
-
#include <boost/test/test_tools.hpp>
#include <boost/test/unit_test.hpp>
#include <boost/concept_check.hpp>
#include <boost/test/output_test_stream.hpp>
-#include <ndn-cxx/name.hpp>
-#include <ndn-cxx/data.hpp>
-#include <ndn-cxx/interest.hpp>
-
-namespace ndn {
-namespace ndns {
-namespace tests {
-
-} //tests
-} //ndns
-} //ndn
-
#endif // NDN_TESTS_BOOST_TEST_HPP
diff --git a/tests/global-configuration-fixture.cpp b/tests/global-configuration-fixture.cpp
new file mode 100644
index 0000000..25dd100
--- /dev/null
+++ b/tests/global-configuration-fixture.cpp
@@ -0,0 +1,78 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2014-2016, Regents of the University of California.
+ *
+ * This file is part of NDNS (Named Data Networking Domain Name Service).
+ * See AUTHORS.md for complete list of NDNS authors and contributors.
+ *
+ * NDNS 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.
+ *
+ * NDNS 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
+ * NDNS, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "test-common.hpp"
+
+namespace ndn {
+namespace ndns {
+namespace tests {
+
+class GlobalConfigurationFixture : boost::noncopyable
+{
+public:
+ GlobalConfigurationFixture()
+ {
+ log::init("unit-tests.log4cxx");
+
+ if (getenv("HOME") != nullptr) {
+ m_home = getenv("HOME");
+ }
+ if (getenv("NDN_CLIENT_PIB") != nullptr) {
+ m_pib = getenv("NDN_CLIENT_PIB");
+ }
+ if (getenv("NDN_CLIENT_TPM") != nullptr) {
+ m_tpm = getenv("NDN_CLIENT_TPM");
+ }
+
+ boost::filesystem::path dir(TEST_CONFIG_PATH);
+ dir /= "test-home";
+ setenv("HOME", dir.generic_string().c_str(), 1);
+
+ setenv("NDN_CLIENT_PIB", "pib-sqlite3", 1);
+ setenv("NDN_CLIENT_TPM", "tpm-file", 1);
+ boost::filesystem::create_directories(dir);
+ }
+
+ ~GlobalConfigurationFixture()
+ {
+ if (!m_home.empty()) {
+ setenv("HOME", m_home.c_str(), 1);
+ }
+ if (!m_pib.empty()) {
+ setenv("NDN_CLIENT_PIB", m_home.c_str(), 1);
+ }
+ if (!m_tpm.empty()) {
+ setenv("NDN_CLIENT_TPM", m_home.c_str(), 1);
+ }
+ }
+
+private:
+ std::string m_home;
+ std::string m_pib;
+ std::string m_tpm;
+};
+
+BOOST_GLOBAL_FIXTURE(GlobalConfigurationFixture)
+#if (BOOST_VERSION >= 105900)
+;
+#endif // BOOST_VERSION >= 105900
+
+} // namespace tests
+} // namespace ndns
+} // namespace ndn
diff --git a/tests/identity-management-fixture.cpp b/tests/identity-management-fixture.cpp
new file mode 100644
index 0000000..f3e1dbe
--- /dev/null
+++ b/tests/identity-management-fixture.cpp
@@ -0,0 +1,92 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2014-2016, 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 NDNS (Named Data Networking Domain Name Service) and is
+ * based on the code written as part of NFD (Named Data Networking Daemon).
+ * See AUTHORS.md for complete list of NDNS authors and contributors.
+ *
+ * NDNS 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.
+ *
+ * NDNS 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
+ * NDNS, 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>
+
+namespace ndn {
+namespace ndns {
+namespace tests {
+
+IdentityManagementFixture::IdentityManagementFixture()
+ : m_keyChain("sqlite3", "file")
+{
+ m_keyChain.getDefaultCertificate(); // side effect: create a default cert if it doesn't exist
+}
+
+IdentityManagementFixture::~IdentityManagementFixture()
+{
+ for (const auto& id : m_identities) {
+ m_keyChain.deleteIdentity(id);
+ }
+
+ boost::system::error_code ec;
+ for (const auto& certFile : m_certFiles) {
+ boost::filesystem::remove(certFile, ec); // ignore error
+ }
+}
+
+bool
+IdentityManagementFixture::addIdentity(const Name& identity, const ndn::KeyParams& params)
+{
+ try {
+ m_keyChain.createIdentity(identity, params);
+ m_identities.push_back(identity);
+ return true;
+ }
+ catch (std::runtime_error&) {
+ return false;
+ }
+}
+
+bool
+IdentityManagementFixture::saveIdentityCertificate(const Name& identity, const std::string& filename, bool wantAdd)
+{
+ shared_ptr<ndn::IdentityCertificate> cert;
+ try {
+ cert = m_keyChain.getCertificate(m_keyChain.getDefaultCertificateNameForIdentity(identity));
+ }
+ catch (const ndn::SecPublicInfo::Error&) {
+ if (wantAdd && this->addIdentity(identity)) {
+ return this->saveIdentityCertificate(identity, filename, false);
+ }
+ return false;
+ }
+
+ m_certFiles.push_back(filename);
+ try {
+ ndn::io::save(*cert, filename);
+ return true;
+ }
+ catch (const ndn::io::Error&) {
+ return false;
+ }
+}
+
+} // namespace tests
+} // namespace ndns
+} // namespace ndn
diff --git a/tests/identity-management-fixture.hpp b/tests/identity-management-fixture.hpp
new file mode 100644
index 0000000..63c7fb7
--- /dev/null
+++ b/tests/identity-management-fixture.hpp
@@ -0,0 +1,83 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2014-2016, 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 NDNS (Named Data Networking Domain Name Service) and is
+ * based on the code written as part of NFD (Named Data Networking Daemon).
+ * See AUTHORS.md for complete list of NDNS authors and contributors.
+ *
+ * NDNS 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.
+ *
+ * NDNS 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
+ * NDNS, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef NDNS_TESTS_IDENTITY_MANAGEMENT_FIXTURE_HPP
+#define NDNS_TESTS_IDENTITY_MANAGEMENT_FIXTURE_HPP
+
+#include "test-common.hpp"
+
+namespace ndn {
+namespace ndns {
+namespace tests {
+
+/** \brief a fixture that cleans up KeyChain identities and certificate files upon destruction
+ */
+class IdentityManagementFixture : public virtual BaseFixture
+{
+public:
+ IdentityManagementFixture();
+
+ /** \brief deletes created identities and saved certificate files
+ */
+ ~IdentityManagementFixture();
+
+ /** \brief add identity
+ * \return whether successful
+ */
+ bool
+ addIdentity(const Name& identity,
+ const ndn::KeyParams& params = ndn::KeyChain::DEFAULT_KEY_PARAMS);
+
+ /** \brief save identity certificate to a file
+ * \param identity identity name
+ * \param filename file name, should be writable
+ * \param wantAdd if true, add new identity when necessary
+ * \return whether successful
+ */
+ bool
+ saveIdentityCertificate(const Name& identity, const std::string& filename, bool wantAdd = false);
+
+protected:
+ ndn::KeyChain m_keyChain;
+
+private:
+ std::vector<ndn::Name> m_identities;
+ std::vector<std::string> m_certFiles;
+};
+
+/** \brief convenience base class for inheriting from both UnitTestTimeFixture
+ * and IdentityManagementFixture
+ */
+class IdentityManagementTimeFixture : public UnitTestTimeFixture
+ , public IdentityManagementFixture
+{
+};
+
+} // namespace tests
+} // namespace ndns
+} // namespace ndn
+
+#endif // NDNS_TESTS_IDENTITY_MANAGEMENT_FIXTURE_HPP
diff --git a/tests/main.cpp b/tests/main.cpp
index f9bf6b6..368f405 100644
--- a/tests/main.cpp
+++ b/tests/main.cpp
@@ -1,8 +1,15 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
- * Copyright (c) 2014, Regents of the University of California.
+ * Copyright (c) 2014-2016, 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 NDNS (Named Data Networking Domain Name Service).
+ * This file is part of NDNS (Named Data Networking Domain Name Service) and is
+ * based on the code written as part of NFD (Named Data Networking Daemon).
* See AUTHORS.md for complete list of NDNS authors and contributors.
*
* NDNS is free software: you can redistribute it and/or modify it under the terms
@@ -17,59 +24,93 @@
* NDNS, 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 "config.hpp"
-#include "logger.hpp"
+#define BOOST_TEST_DYN_LINK
+#define BOOST_TEST_ALTERNATIVE_INIT_API
#include <boost/version.hpp>
-#include <boost/test/unit_test.hpp>
-#include <boost/noncopyable.hpp>
-#include <boost/filesystem.hpp>
+
+#if BOOST_VERSION >= 106200
+// Boost.Test v3.3 (Boost 1.62) natively supports multi-logger output
+#include "boost-test.hpp"
+#else
+#define BOOST_TEST_NO_MAIN
+#include "boost-test.hpp"
+
+#include "boost-multi-log-formatter.hpp"
+
+#include <boost/program_options/options_description.hpp>
+#include <boost/program_options/variables_map.hpp>
+#include <boost/program_options/parsers.hpp>
#include <fstream>
#include <iostream>
-namespace ndn {
-namespace ndns {
-namespace tests {
-
-class GlobalConfigurationFixture : boost::noncopyable
+static bool
+init_tests()
{
-public:
- GlobalConfigurationFixture()
- {
- log::init("unit-tests.log4cxx");
+ init_unit_test();
- if (std::getenv("HOME"))
- m_home = std::getenv("HOME");
+ namespace po = boost::program_options;
+ namespace ut = boost::unit_test;
- boost::filesystem::path dir(TEST_CONFIG_PATH);
- dir /= "test-home";
- setenv("HOME", dir.generic_string().c_str(), 1);
- boost::filesystem::create_directories(dir);
- std::ofstream clientConf((dir / ".ndn" / "client.conf").generic_string().c_str());
- clientConf << "pib=pib-sqlite3\n"
- << "tpm=tpm-file" << std::endl;
+ po::options_description extraOptions;
+ std::string logger;
+ std::string outputFile = "-";
+ extraOptions.add_options()
+ ("log_format2", po::value<std::string>(&logger), "Type of second log formatter: HRF or XML")
+ ("log_sink2", po::value<std::string>(&outputFile)->default_value(outputFile), "Second log sink, - for stdout")
+ ;
+ po::variables_map vm;
+ try {
+ po::store(po::command_line_parser(ut::framework::master_test_suite().argc,
+ ut::framework::master_test_suite().argv)
+ .options(extraOptions)
+ .run(),
+ vm);
+ po::notify(vm);
+ }
+ catch (const std::exception& e) {
+ std::cerr << "ERROR: " << e.what() << "\n"
+ << extraOptions << std::endl;
+ return false;
}
- ~GlobalConfigurationFixture()
- {
- if (!m_home.empty()) {
- setenv("HOME", m_home.c_str(), 1);
- }
+ if (vm.count("log_format2") == 0) {
+ // second logger is not configured
+ return true;
}
-private:
- std::string m_home;
-};
+ std::shared_ptr<ut::unit_test_log_formatter> formatter;
+ if (logger == "XML") {
+ formatter = std::make_shared<ut::output::xml_log_formatter>();
+ }
+ else if (logger == "HRF") {
+ formatter = std::make_shared<ut::output::compiler_log_formatter>();
+ }
+ else {
+ std::cerr << "ERROR: only HRF or XML log formatter can be specified" << std::endl;
+ return false;
+ }
-BOOST_GLOBAL_FIXTURE(GlobalConfigurationFixture)
-#if (BOOST_VERSION >= 105900)
-;
-#endif // BOOST_VERSION >= 105900
+ std::shared_ptr<std::ostream> output;
+ if (outputFile == "-") {
+ output = std::shared_ptr<std::ostream>(&std::cout, std::bind([]{}));
+ }
+ else {
+ output = std::make_shared<std::ofstream>(outputFile.c_str());
+ }
-} // namespace tests
-} // namespace ndns
-} // namespace ndn
+ auto multiFormatter = new ut::output::multi_log_formatter;
+ multiFormatter->add(formatter, output);
+ ut::unit_test_log.set_formatter(multiFormatter);
+
+ return true;
+}
+
+int
+main(int argc, char* argv[])
+{
+ return ::boost::unit_test::unit_test_main(&init_tests, argc, argv);
+}
+
+#endif // BOOST_VERSION >= 106200
diff --git a/tests/test-common.cpp b/tests/test-common.cpp
new file mode 100644
index 0000000..e929ab1
--- /dev/null
+++ b/tests/test-common.cpp
@@ -0,0 +1,83 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2014-2016, 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 NDNS (Named Data Networking Domain Name Service) and is
+ * based on the code written as part of NFD (Named Data Networking Daemon).
+ * See AUTHORS.md for complete list of NDNS authors and contributors.
+ *
+ * NDNS 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.
+ *
+ * NDNS 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
+ * NDNS, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "test-common.hpp"
+
+#include <ndn-cxx/security/signature-sha256-with-rsa.hpp>
+
+namespace ndn {
+namespace ndns {
+namespace tests {
+
+shared_ptr<Interest>
+makeInterest(const Name& name, uint32_t nonce)
+{
+ auto interest = make_shared<Interest>(name);
+ if (nonce != 0) {
+ interest->setNonce(nonce);
+ }
+ return interest;
+}
+
+shared_ptr<Data>
+makeData(const Name& name)
+{
+ auto data = make_shared<Data>(name);
+ return signData(data);
+}
+
+Data&
+signData(Data& data)
+{
+ ndn::SignatureSha256WithRsa fakeSignature;
+ fakeSignature.setValue(ndn::encoding::makeEmptyBlock(tlv::SignatureValue));
+ data.setSignature(fakeSignature);
+ data.wireEncode();
+
+ return data;
+}
+
+shared_ptr<Link>
+makeLink(const Name& name, std::initializer_list<std::pair<uint32_t, Name>> delegations)
+{
+ auto link = make_shared<Link>(name, delegations);
+ signData(link);
+ return link;
+}
+
+lp::Nack
+makeNack(const Name& name, uint32_t nonce, lp::NackReason reason)
+{
+ Interest interest(name);
+ interest.setNonce(nonce);
+ lp::Nack nack(std::move(interest));
+ nack.setReason(reason);
+ return nack;
+}
+
+} // namespace tests
+} // namespace ndns
+} // namespace ndn
diff --git a/tests/test-common.hpp b/tests/test-common.hpp
new file mode 100644
index 0000000..5b58297
--- /dev/null
+++ b/tests/test-common.hpp
@@ -0,0 +1,126 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2014-2016, 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 NDNS (Named Data Networking Domain Name Service) and is
+ * based on the code written as part of NFD (Named Data Networking Daemon).
+ * See AUTHORS.md for complete list of NDNS authors and contributors.
+ *
+ * NDNS 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.
+ *
+ * NDNS 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
+ * NDNS, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef NDNS_TESTS_TEST_COMMON_HPP
+#define NDNS_TESTS_TEST_COMMON_HPP
+
+#include "logger.hpp"
+
+#include <ndn-cxx/name.hpp>
+#include <ndn-cxx/data.hpp>
+#include <ndn-cxx/interest.hpp>
+#include <ndn-cxx/link.hpp>
+#include <ndn-cxx/lp/nack.hpp>
+#include <ndn-cxx/util/dummy-client-face.hpp>
+#include <ndn-cxx/security/key-chain.hpp>
+
+#include <boost/version.hpp>
+#include <boost/asio.hpp>
+#include <boost/filesystem.hpp>
+
+#include <fstream>
+
+#include "boost-test.hpp"
+#include "unit-test-common-fixtures.hpp"
+#include "identity-management-fixture.hpp"
+
+namespace ndn {
+namespace ndns {
+namespace tests {
+
+/** \brief create an Interest
+ * \param name Interest name
+ * \param nonce if non-zero, set Nonce to this value
+ * (useful for creating Nack with same Nonce)
+ */
+shared_ptr<Interest>
+makeInterest(const Name& name, uint32_t nonce = 0);
+
+/** \brief create a Data with fake signature
+ * \note Data may be modified afterwards without losing the fake signature.
+ * If a real signature is desired, sign again with KeyChain.
+ */
+shared_ptr<Data>
+makeData(const Name& name);
+
+/** \brief add a fake signature to Data
+ */
+Data&
+signData(Data& data);
+
+/** \brief add a fake signature to Data
+ */
+inline shared_ptr<Data>
+signData(shared_ptr<Data> data)
+{
+ signData(*data);
+ return data;
+}
+
+/** \brief create a Link object with fake signature
+ * \note Link may be modified afterwards without losing the fake signature.
+ * If a real signature is desired, sign again with KeyChain.
+ */
+shared_ptr<Link>
+makeLink(const Name& name, std::initializer_list<std::pair<uint32_t, Name>> delegations);
+
+/** \brief create a Nack
+ * \param name Interest name
+ * \param nonce Interest nonce
+ * \param reason Nack reason
+ */
+lp::Nack
+makeNack(const Name& name, uint32_t nonce, lp::NackReason reason);
+
+/** \brief replace a name component
+ * \param[inout] name name
+ * \param index name component index
+ * \param a arguments to name::Component constructor
+ */
+template<typename...A>
+void
+setNameComponent(Name& name, ssize_t index, const A& ...a)
+{
+ Name name2 = name.getPrefix(index);
+ name2.append(name::Component(a...));
+ name2.append(name.getSubName(name2.size()));
+ name = name2;
+}
+
+template<typename Packet, typename...A>
+void
+setNameComponent(Packet& packet, ssize_t index, const A& ...a)
+{
+ Name name = packet.getName();
+ setNameComponent(name, index, a...);
+ packet.setName(name);
+}
+
+} // namespace tests
+} // namespace ndns
+} // namespace ndn
+
+#endif // NFD_TESTS_TEST_COMMON_HPP
diff --git a/tests/unit-test-common-fixtures.cpp b/tests/unit-test-common-fixtures.cpp
new file mode 100644
index 0000000..f16e027
--- /dev/null
+++ b/tests/unit-test-common-fixtures.cpp
@@ -0,0 +1,82 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2014-2016, 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 NDNS (Named Data Networking Domain Name Service) and is
+ * based on the code written as part of NFD (Named Data Networking Daemon).
+ * See AUTHORS.md for complete list of NDNS authors and contributors.
+ *
+ * NDNS 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.
+ *
+ * NDNS 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
+ * NDNS, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "test-common.hpp"
+
+namespace ndn {
+namespace ndns {
+namespace tests {
+
+BaseFixture::BaseFixture()
+{
+}
+
+UnitTestTimeFixture::UnitTestTimeFixture()
+ : steadyClock(make_shared<time::UnitTestSteadyClock>())
+ , systemClock(make_shared<time::UnitTestSystemClock>())
+{
+ time::setCustomClocks(steadyClock, systemClock);
+}
+
+UnitTestTimeFixture::~UnitTestTimeFixture()
+{
+ time::setCustomClocks(nullptr, nullptr);
+}
+
+void
+UnitTestTimeFixture::advanceClocks(const time::nanoseconds& tick, size_t nTicks)
+{
+ this->advanceClocks(tick, tick * nTicks);
+}
+
+void
+UnitTestTimeFixture::advanceClocks(const time::nanoseconds& tick, const time::nanoseconds& total)
+{
+ BOOST_ASSERT(tick > time::nanoseconds::zero());
+ BOOST_ASSERT(total >= time::nanoseconds::zero());
+
+ time::nanoseconds remaining = total;
+ while (remaining > time::nanoseconds::zero()) {
+ if (remaining >= tick) {
+ steadyClock->advance(tick);
+ systemClock->advance(tick);
+ remaining -= tick;
+ }
+ else {
+ steadyClock->advance(remaining);
+ systemClock->advance(remaining);
+ remaining = time::nanoseconds::zero();
+ }
+
+ if (m_io.stopped())
+ m_io.reset();
+ m_io.poll();
+ }
+}
+
+} // namespace tests
+} // namespace ndns
+} // namespace ndn
diff --git a/tests/unit-test-common-fixtures.hpp b/tests/unit-test-common-fixtures.hpp
new file mode 100644
index 0000000..dfed49d
--- /dev/null
+++ b/tests/unit-test-common-fixtures.hpp
@@ -0,0 +1,97 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2014-2016, 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 NDNS (Named Data Networking Domain Name Service) and is
+ * based on the code written as part of NFD (Named Data Networking Daemon).
+ * See AUTHORS.md for complete list of NDNS authors and contributors.
+ *
+ * NDNS 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.
+ *
+ * NDNS 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
+ * NDNS, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef NDNS_TESTS_UNIT_TEST_COMMON_FIXTURES_HPP
+#define NDNS_TESTS_UNIT_TEST_COMMON_FIXTURES_HPP
+
+#include "boost-test.hpp"
+
+#include <ndn-cxx/util/time-unit-test-clock.hpp>
+
+namespace ndn {
+namespace ndns {
+namespace tests {
+
+/** \brief base test fixture
+ *
+ * Every test case should be based on this fixture,
+ * to have per test case io_service initialization.
+ */
+class BaseFixture
+{
+protected:
+ BaseFixture();
+
+protected:
+ /** \brief reference to global io_service
+ */
+ boost::asio::io_service m_io;
+};
+
+/** \brief a base test fixture that overrides steady clock and system clock
+ */
+class UnitTestTimeFixture : public virtual BaseFixture
+{
+protected:
+ UnitTestTimeFixture();
+
+ ~UnitTestTimeFixture();
+
+ /** \brief advance steady and system clocks
+ *
+ * Clocks are advanced in increments of \p tick for \p nTicks ticks.
+ * After each tick, global io_service is polled to process pending I/O events.
+ *
+ * Exceptions thrown during I/O events are propagated to the caller.
+ * Clock advancing would stop in case of an exception.
+ */
+ void
+ advanceClocks(const time::nanoseconds& tick, size_t nTicks = 1);
+
+ /** \brief advance steady and system clocks
+ *
+ * Clocks are advanced in increments of \p tick for \p total time.
+ * The last increment might be shorter than \p tick.
+ * After each tick, global io_service is polled to process pending I/O events.
+ *
+ * Exceptions thrown during I/O events are propagated to the caller.
+ * Clock advancing would stop in case of an exception.
+ */
+ void
+ advanceClocks(const time::nanoseconds& tick, const time::nanoseconds& total);
+
+protected:
+ shared_ptr<time::UnitTestSteadyClock> steadyClock;
+ shared_ptr<time::UnitTestSystemClock> systemClock;
+
+ friend class LimitedIo;
+};
+
+} // namespace tests
+} // namespace ndns
+} // namespace ndn
+
+#endif // NDNS_TESTS_UNIT_TEST_COMMON_FIXTURES_HPP
diff --git a/tests/unit/clients/iterative-query-controller.cpp b/tests/unit/clients/iterative-query-controller.cpp
index a34375b..93bbb4e 100644
--- a/tests/unit/clients/iterative-query-controller.cpp
+++ b/tests/unit/clients/iterative-query-controller.cpp
@@ -19,13 +19,9 @@
#include "clients/iterative-query-controller.hpp"
#include "daemon/name-server.hpp"
-#include "logger.hpp"
-#include "../database-test-data.hpp"
-#include "../../boost-test.hpp"
-#include <ndn-cxx/util/dummy-client-face.hpp>
-
-#include <boost/asio.hpp>
+#include "test-common.hpp"
+#include "unit/database-test-data.hpp"
namespace ndn {
namespace ndns {
diff --git a/tests/unit/clients/query.cpp b/tests/unit/clients/query.cpp
index 6fdca35..40fdfc3 100644
--- a/tests/unit/clients/query.cpp
+++ b/tests/unit/clients/query.cpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
- * Copyright (c) 2014, Regents of the University of California.
+ * Copyright (c) 2014-2016, Regents of the University of California.
*
* This file is part of NDNS (Named Data Networking Domain Name Service).
* See AUTHORS.md for complete list of NDNS authors and contributors.
@@ -19,7 +19,7 @@
#include "clients/query.hpp"
-#include "../../boost-test.hpp"
+#include "test-common.hpp"
namespace ndn {
namespace ndns {
@@ -69,6 +69,6 @@
BOOST_AUTO_TEST_SUITE_END()
-}// namespace tests
-}// namespace ndns
+} // namespace tests
+} // namespace ndns
} // namespace ndn
diff --git a/tests/unit/clients/response.cpp b/tests/unit/clients/response.cpp
index 7332c35..5950063 100644
--- a/tests/unit/clients/response.cpp
+++ b/tests/unit/clients/response.cpp
@@ -18,20 +18,17 @@
*/
#include "clients/response.hpp"
-#include <ndn-cxx/security/key-chain.hpp>
-#include "../../boost-test.hpp"
+
+#include "test-common.hpp"
namespace ndn {
namespace ndns {
namespace tests {
-
-
-BOOST_AUTO_TEST_SUITE(Response)
+BOOST_FIXTURE_TEST_SUITE(Response, IdentityManagementFixture)
BOOST_AUTO_TEST_CASE(Basic)
{
- KeyChain keyChain("sqlite3", "file");
Name hint;
Name zone("/net");
name::Component qType = ndns::label::NDNS_ITERATIVE_QUERY;
@@ -54,7 +51,7 @@
//const Block& block = r.wireEncode();
shared_ptr<Data> data = r.toData();
- // keyChain.sign(*data);
+ // m_keyChain.sign(*data);
ndns::Response r2;
BOOST_CHECK_EQUAL(r2.fromData(hint, zone, *data), true);
@@ -75,7 +72,7 @@
BOOST_CHECK_NE(r2, r4);
data = r4.toData();
- // keyChain.sign(*data);
+ // m_keyChain.sign(*data);
ndns::Response r5(zone, qType);
diff --git a/tests/unit/daemon/db-mgr.cpp b/tests/unit/daemon/db-mgr.cpp
index 4b628b3..09fdc9f 100644
--- a/tests/unit/daemon/db-mgr.cpp
+++ b/tests/unit/daemon/db-mgr.cpp
@@ -17,14 +17,11 @@
* NDNS, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
*/
-#include "config.hpp"
#include "daemon/db-mgr.hpp"
-#include "logger.hpp"
-#include "../../boost-test.hpp"
-#include <boost/filesystem.hpp>
+#include <algorithm>
-#include <algorithm> // std::sort
+#include "test-common.hpp"
namespace ndn {
namespace ndns {
diff --git a/tests/unit/daemon/name-server.cpp b/tests/unit/daemon/name-server.cpp
index 6a9be97..820dbd2 100644
--- a/tests/unit/daemon/name-server.cpp
+++ b/tests/unit/daemon/name-server.cpp
@@ -17,18 +17,13 @@
* NDNS, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
*/
-#include "daemon/db-mgr.hpp"
#include "daemon/name-server.hpp"
+#include "daemon/db-mgr.hpp"
#include "clients/response.hpp"
#include "clients/query.hpp"
-#include "logger.hpp"
-#include "../database-test-data.hpp"
-#include "../../boost-test.hpp"
-
-#include <boost/filesystem.hpp>
-#include <ndn-cxx/face.hpp>
-#include <ndn-cxx/util/dummy-client-face.hpp>
+#include "test-common.hpp"
+#include "unit/database-test-data.hpp"
namespace ndn {
namespace ndns {
diff --git a/tests/unit/daemon/rrset.cpp b/tests/unit/daemon/rrset.cpp
index ddcff15..c71fe0b 100644
--- a/tests/unit/daemon/rrset.cpp
+++ b/tests/unit/daemon/rrset.cpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
- * Copyright (c) 2014, Regents of the University of California.
+ * Copyright (c) 2014-2016, Regents of the University of California.
*
* This file is part of NDNS (Named Data Networking Domain Name Service).
* See AUTHORS.md for complete list of NDNS authors and contributors.
@@ -18,9 +18,8 @@
*/
#include "daemon/rrset.hpp"
-#include "../../boost-test.hpp"
-#include <ndn-cxx/name.hpp>
+#include "test-common.hpp"
namespace ndn {
namespace ndns {
diff --git a/tests/unit/daemon/zone.cpp b/tests/unit/daemon/zone.cpp
index 3d60cd2..ae13d36 100644
--- a/tests/unit/daemon/zone.cpp
+++ b/tests/unit/daemon/zone.cpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
- * Copyright (c) 2014, Regents of the University of California.
+ * Copyright (c) 2014-2016, Regents of the University of California.
*
* This file is part of NDNS (Named Data Networking Domain Name Service).
* See AUTHORS.md for complete list of NDNS authors and contributors.
@@ -18,9 +18,8 @@
*/
#include "daemon/zone.hpp"
-#include "../../boost-test.hpp"
-#include <ndn-cxx/name.hpp>
+#include "test-common.hpp"
namespace ndn {
namespace ndns {
diff --git a/tests/unit/database-test-data.cpp b/tests/unit/database-test-data.cpp
index 077d09b..90f2587 100644
--- a/tests/unit/database-test-data.cpp
+++ b/tests/unit/database-test-data.cpp
@@ -18,13 +18,11 @@
*/
#include "database-test-data.hpp"
-#include "logger.hpp"
-
-#include <boost/filesystem.hpp>
namespace ndn {
namespace ndns {
namespace tests {
+
NDNS_LOG_INIT("TestFakeData")
const boost::filesystem::path DbTestData::TEST_DATABASE = TEST_CONFIG_PATH "/" "test-ndns.db";
@@ -40,7 +38,6 @@
DbTestData::DbTestData()
: m_session(TEST_DATABASE.string())
- , m_keyChain("sqlite3", "file")
{
NDNS_LOG_TRACE("start creating test data");
diff --git a/tests/unit/database-test-data.hpp b/tests/unit/database-test-data.hpp
index 27f7aaa..104dd5d 100644
--- a/tests/unit/database-test-data.hpp
+++ b/tests/unit/database-test-data.hpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
- * Copyright (c) 2014, Regents of the University of California.
+ * Copyright (c) 2014-2016, Regents of the University of California.
*
* This file is part of NDNS (Named Data Networking Domain Name Service).
* See AUTHORS.md for complete list of NDNS authors and contributors.
@@ -25,16 +25,13 @@
#include "clients/query.hpp"
#include "validator.hpp"
-#include "../boost-test.hpp"
-#include <ndn-cxx/security/key-chain.hpp>
-#include <ndn-cxx/util/dummy-client-face.hpp>
-#include <boost/filesystem.hpp>
+#include "test-common.hpp"
namespace ndn {
namespace ndns {
namespace tests {
-class DbTestData
+class DbTestData : public IdentityManagementFixture
{
public:
static const boost::filesystem::path TEST_DATABASE;
@@ -66,7 +63,6 @@
Zone m_net;
Zone m_ndnsim;
DbMgr m_session;
- KeyChain m_keyChain;
};
} // namespace tests
diff --git a/tests/unit/logger.cpp b/tests/unit/logger.cpp
index c2491ad..e7ba277 100644
--- a/tests/unit/logger.cpp
+++ b/tests/unit/logger.cpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
- * Copyright (c) 2014, Regents of the University of California.
+ * Copyright (c) 2014-2016, Regents of the University of California.
*
* This file is part of NDNS (Named Data Networking Domain Name Service).
* See AUTHORS.md for complete list of NDNS authors and contributors.
@@ -18,14 +18,11 @@
*/
#include "logger.hpp"
-#include "../boost-test.hpp"
-#include "config.hpp"
-
-#include <fstream>
#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/classification.hpp>
-#include <boost/filesystem.hpp>
+
+#include "test-common.hpp"
namespace ndn {
namespace ndns {
diff --git a/tests/unit/mgmt/management-tool.cpp b/tests/unit/mgmt/management-tool.cpp
index 459e9eb..b61ac86 100644
--- a/tests/unit/mgmt/management-tool.cpp
+++ b/tests/unit/mgmt/management-tool.cpp
@@ -17,29 +17,21 @@
* NDNS, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
*/
-#include "../../src/mgmt/management-tool.hpp"
-
-#include "../../boost-test.hpp"
-#include <boost/test/output_test_stream.hpp>
-using boost::test_tools::output_test_stream;
+#include "mgmt/management-tool.hpp"
#include "ndns-enum.hpp"
#include "ndns-label.hpp"
#include "ndns-tlv.hpp"
-#include <vector>
-#include <iostream>
-#include <fstream>
-#include <string>
-
-#include <boost/filesystem.hpp>
#include <boost/algorithm/string/replace.hpp>
-#include <ndn-cxx/security/key-chain.hpp>
-#include <ndn-cxx/security/validator.hpp>
#include <ndn-cxx/util/io.hpp>
#include <ndn-cxx/util/regex.hpp>
+#include "test-common.hpp"
+
+using boost::test_tools::output_test_stream;
+
namespace ndn {
namespace ndns {
namespace tests {
@@ -50,7 +42,6 @@
static const boost::filesystem::path TEST_CERTDIR = TEST_CONFIG_PATH "/management_tool_certs";
static const Name FAKE_ROOT("/fake-root/123456789");
-
/**
* @brief Recursive copy a directory using Boost Filesystem
*
@@ -102,7 +93,7 @@
};
-class ManagementToolFixture : public TestHome
+class ManagementToolFixture : public TestHome, public IdentityManagementFixture
{
public:
class Error : public std::runtime_error
@@ -126,8 +117,7 @@
};
ManagementToolFixture()
- : m_keyChain("sqlite3", "file")
- , m_tool(TEST_DATABASE.string().c_str(), m_keyChain)
+ : m_tool(TEST_DATABASE.string().c_str(), m_keyChain)
, m_dbMgr(TEST_DATABASE.string().c_str())
, rootKsk("/KEY/ksk-1416974006376/ID-CERT/%FD%00%00%01I%EA%3Bx%BD")
@@ -204,7 +194,6 @@
public:
PreviousStateCleaner cleaner; // must be first variable
- ndn::KeyChain m_keyChain;
ndns::ManagementTool m_tool;
ndns::DbMgr m_dbMgr;
diff --git a/tests/unit/ndns-label.cpp b/tests/unit/ndns-label.cpp
index 09bb182..719d786 100644
--- a/tests/unit/ndns-label.cpp
+++ b/tests/unit/ndns-label.cpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
- * Copyright (c) 2014, Regents of the University of California.
+ * Copyright (c) 2014-2016, Regents of the University of California.
*
* This file is part of NDNS (Named Data Networking Domain Name Service).
* See AUTHORS.md for complete list of NDNS authors and contributors.
@@ -18,7 +18,8 @@
*/
#include "ndns-label.hpp"
-#include "../boost-test.hpp"
+
+#include "test-common.hpp"
namespace ndn {
namespace ndns {
diff --git a/tests/unit/validator.cpp b/tests/unit/validator.cpp
index b5fcfeb..ff9547f 100644
--- a/tests/unit/validator.cpp
+++ b/tests/unit/validator.cpp
@@ -18,10 +18,8 @@
*/
#include "validator.hpp"
-#include "../boost-test.hpp"
-#include <ndn-cxx/util/dummy-client-face.hpp>
-#include <ndn-cxx/security/key-chain.hpp>
-#include <boost/asio.hpp>
+
+#include "test-common.hpp"
namespace ndn {
namespace ndns {
@@ -31,7 +29,7 @@
BOOST_AUTO_TEST_SUITE(Validator)
-class Fixture
+class Fixture : public IdentityManagementFixture
{
public:
Fixture()
@@ -39,15 +37,9 @@
, m_testId2("/test02/ndn")
, m_testId3("/test02/ndn/edu")
, m_randomId("/test03")
- , m_keyChain("sqlite3", "file")
, m_version(name::Component::fromVersion(0))
, m_face(m_keyChain, {false, true})
{
- m_keyChain.deleteIdentity(m_testId1);
- m_keyChain.deleteIdentity(m_testId2);
- m_keyChain.deleteIdentity(m_testId3);
- m_keyChain.deleteIdentity(m_randomId);
-
m_randomDsk = createRoot(m_randomId); // generate a root cert
m_dsk1 = createRoot(m_testId1); // replace to root cert
@@ -67,10 +59,6 @@
{
m_face.getIoService().stop();
m_face.shutdown();
- m_keyChain.deleteIdentity(m_testId1);
- m_keyChain.deleteIdentity(m_testId2);
- m_keyChain.deleteIdentity(m_testId3);
- m_keyChain.deleteIdentity(m_randomId);
}
const Name
@@ -146,8 +134,6 @@
Name m_rootCert;
- KeyChain m_keyChain;
-
Name m_dsk1;
Name m_dsk2;
Name m_dsk3;
@@ -167,8 +153,10 @@
// validator must be created after root key is saved to the target
ndns::Validator validator(m_face, TEST_CONFIG_PATH "/" "validator.conf");
- Name dataName(m_testId3);
- dataName.append("NDNS")
+ Name dataName;
+ dataName
+ .append(m_testId3)
+ .append("NDNS")
.append("rrLabel")
.append("rrType")
.appendVersion();
@@ -190,9 +178,10 @@
BOOST_CHECK_EQUAL(hasValidated, true);
-
- dataName = m_testId2;
- dataName.append("KEY")
+ dataName = Name();
+ dataName
+ .append(m_testId2)
+ .append("KEY")
.append("rrLabel")
.append("ID-CERT")
.appendVersion();
@@ -214,9 +203,10 @@
// cannot pass verification due to key's owner's name is longer than data owner's
BOOST_CHECK_EQUAL(hasValidated, true);
-
- dataName = m_testId3;
- dataName.append("KEY")
+ dataName = Name();
+ dataName
+ .append(m_testId3)
+ .append("KEY")
.append("rrLabel")
.append("ID-CERT")
.appendVersion();
@@ -238,8 +228,10 @@
// cannot pass due to self-sign cert is used
BOOST_CHECK_EQUAL(hasValidated, true);
- dataName = m_testId2;
- dataName.append("KEY")
+ dataName = Name();
+ dataName
+ .append(m_testId2)
+ .append("KEY")
.append("rrLabel")
.append("ID-CERT")
.appendVersion();
diff --git a/tests/wscript b/tests/wscript
index 58de95d..a44fede 100644
--- a/tests/wscript
+++ b/tests/wscript
@@ -41,6 +41,7 @@
features='cxx cxxprogram',
source=bld.path.ant_glob(['**/*.cpp'], excl=['main.cpp']),
use='ndns-objects unit-tests-main BOOST',
+ includes='../src .',
install_path=None,
defines='TEST_CONFIG_PATH=\"%s/conf-test\"' %(bld.bldnode)
)