tests: Import unit-tests infrastructure code from NFD
diff --git a/tests/boost-test.hpp b/tests/boost-test.hpp
new file mode 100644
index 0000000..751064d
--- /dev/null
+++ b/tests/boost-test.hpp
@@ -0,0 +1,39 @@
+/* -*- 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, originally written as part of NFD (Named Data Networking Forwarding Daemon),
+ * is a part of ChronoShare, a decentralized file sharing application over NDN.
+ *
+ * ChronoShare 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.
+ *
+ * ChronoShare 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 copies of the GNU General Public License along with
+ * ChronoShare, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ChronoShare authors and contributors.
+ */
+
+#ifndef NFD_TESTS_BOOST_TEST_HPP
+#define NFD_TESTS_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 // NFD_TESTS_BOOST_TEST_HPP
diff --git a/tests/identity-management-fixture.cpp b/tests/identity-management-fixture.cpp
new file mode 100644
index 0000000..936e641
--- /dev/null
+++ b/tests/identity-management-fixture.cpp
@@ -0,0 +1,94 @@
+/* -*- 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, originally written as part of NFD (Named Data Networking Forwarding Daemon),
+ * is a part of ChronoShare, a decentralized file sharing application over NDN.
+ *
+ * ChronoShare 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.
+ *
+ * ChronoShare 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 copies of the GNU General Public License along with
+ * ChronoShare, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ChronoShare authors and contributors.
+ */
+
+#include "identity-management-fixture.hpp"
+
+#include <ndn-cxx/util/io.hpp>
+#include <boost/filesystem.hpp>
+
+namespace ndn {
+namespace chronoshare {
+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 chronoshare
+} // namespace ndn
diff --git a/tests/identity-management-fixture.hpp b/tests/identity-management-fixture.hpp
new file mode 100644
index 0000000..75d24ce
--- /dev/null
+++ b/tests/identity-management-fixture.hpp
@@ -0,0 +1,86 @@
+/* -*- 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, originally written as part of NFD (Named Data Networking Forwarding Daemon),
+ * is a part of ChronoShare, a decentralized file sharing application over NDN.
+ *
+ * ChronoShare 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.
+ *
+ * ChronoShare 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 copies of the GNU General Public License along with
+ * ChronoShare, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ChronoShare authors and contributors.
+ */
+
+#ifndef NFD_TESTS_IDENTITY_MANAGEMENT_FIXTURE_HPP
+#define NFD_TESTS_IDENTITY_MANAGEMENT_FIXTURE_HPP
+
+#include "tests/test-common.hpp"
+
+#include <ndn-cxx/security/key-chain.hpp>
+
+namespace ndn {
+namespace chronoshare {
+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 chronoshare
+} // namespace ndn
+
+#endif // NFD_TESTS_IDENTITY_MANAGEMENT_FIXTURE_HPP
diff --git a/tests/logger-config-global-fixture.cpp b/tests/logger-config-global-fixture.cpp
new file mode 100644
index 0000000..2663333
--- /dev/null
+++ b/tests/logger-config-global-fixture.cpp
@@ -0,0 +1,80 @@
+/* -*- 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 "core/chronoshare-config.hpp"
+
+#include "test-common.hpp"
+
+namespace ndn {
+namespace chronoshare {
+namespace tests {
+
+class GlobalConfigurationFixture : boost::noncopyable
+{
+public:
+ GlobalConfigurationFixture()
+ {
+ INIT_LOGGERS();
+
+ 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(UNIT_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 chronoshare
+} // namespace ndn
diff --git a/tests/main.cpp b/tests/main.cpp
index 4abe5de..76e08fe 100644
--- a/tests/main.cpp
+++ b/tests/main.cpp
@@ -17,9 +17,9 @@
*
* See AUTHORS.md for complete list of ChronoShare authors and contributors.
*/
+
#define BOOST_TEST_MAIN 1
#define BOOST_TEST_DYN_LINK 1
#include "test-common.hpp"
-
#include <boost/test/unit_test.hpp>
diff --git a/tests/test-common.cpp b/tests/test-common.cpp
new file mode 100644
index 0000000..b9a9464
--- /dev/null
+++ b/tests/test-common.cpp
@@ -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, originally written as part of NFD (Named Data Networking Forwarding Daemon),
+ * is a part of ChronoShare, a decentralized file sharing application over NDN.
+ *
+ * ChronoShare 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.
+ *
+ * ChronoShare 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 copies of the GNU General Public License along with
+ * ChronoShare, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ChronoShare authors and contributors.
+ */
+
+#include "test-common.hpp"
+#include <ndn-cxx/security/signature-sha256-with-rsa.hpp>
+
+namespace ndn {
+namespace chronoshare {
+namespace tests {
+
+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();
+ }
+}
+
+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 chronoshare
+} // namespace ndn
diff --git a/tests/test-common.hpp b/tests/test-common.hpp
new file mode 100644
index 0000000..6802e60
--- /dev/null
+++ b/tests/test-common.hpp
@@ -0,0 +1,174 @@
+/* -*- 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, originally written as part of NFD (Named Data Networking Forwarding Daemon),
+ * is a part of ChronoShare, a decentralized file sharing application over NDN.
+ *
+ * ChronoShare 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.
+ *
+ * ChronoShare 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 copies of the GNU General Public License along with
+ * ChronoShare, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ChronoShare authors and contributors.
+ */
+
+#ifndef CHRONOSHARE_TESTS_TEST_COMMON_HPP
+#define CHRONOSHARE_TESTS_TEST_COMMON_HPP
+
+#include "core/logging.hpp"
+
+#include "boost-test.hpp"
+
+#include <boost/asio/io_service.hpp>
+#include <boost/filesystem.hpp>
+
+#include <ndn-cxx/name.hpp>
+#include <ndn-cxx/data.hpp>
+#include <ndn-cxx/lp/nack.hpp>
+#include <ndn-cxx/util/time-unit-test-clock.hpp>
+#include <ndn-cxx/util/string-helper.hpp>
+
+namespace ndn {
+namespace chronoshare {
+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:
+ /** \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;
+};
+
+/** \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 chronoshare
+} // namespace ndn
+
+#include "identity-management-fixture.hpp"
+
+#endif // CHRONOSHARE_TESTS_TEST_COMMON_HPP
diff --git a/tests/wscript b/tests/wscript
index 4de283c..d6bcf93 100644
--- a/tests/wscript
+++ b/tests/wscript
@@ -13,13 +13,15 @@
name='unit-tests-main',
source='main.cpp',
use='BOOST',
- defines=['BOOST_TEST_MODULE=CHRONOSHARE Unit Test'])
+ includes='.. .',
+ defines=['BOOST_TEST_MODULE=ChronoShare Unit Tests'])
unit_tests = bld.program(
target='../unit-tests',
features='cxx cxxprogram',
source=bld.path.ant_glob(['**/*.cpp'], excl=['main.cpp']),
- use='unit-tests-main',
+ use='unit-tests-main chronoshare',
install_path=None,
- defines='UNIT_TEST_CONFIG_PATH=\"%s/tmp-files/\"' % (bld.bldnode)
+ defines='UNIT_TEST_CONFIG_PATH=\"%s/tmp-files/\"' % (bld.bldnode),
+ includes='.. ../src .',
)