Vince Lehman | 7c60329 | 2014-09-11 17:48:16 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | 67758b1 | 2018-03-06 18:36:44 -0500 | [diff] [blame^] | 2 | /* |
| 3 | * Copyright (c) 2014-2018, The University of Memphis, |
| 4 | * Regents of the University of California, |
| 5 | * Arizona Board of Regents. |
Vince Lehman | 7c60329 | 2014-09-11 17:48:16 -0500 | [diff] [blame] | 6 | * |
| 7 | * This file is part of NLSR (Named-data Link State Routing). |
| 8 | * See AUTHORS.md for complete list of NLSR authors and contributors. |
| 9 | * |
| 10 | * NLSR is free software: you can redistribute it and/or modify it under the terms |
| 11 | * of the GNU General Public License as published by the Free Software Foundation, |
| 12 | * either version 3 of the License, or (at your option) any later version. |
| 13 | * |
| 14 | * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 15 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE. See the GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along with |
| 19 | * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
Alexander Afanasyev | 67758b1 | 2018-03-06 18:36:44 -0500 | [diff] [blame^] | 20 | */ |
Vince Lehman | 7c60329 | 2014-09-11 17:48:16 -0500 | [diff] [blame] | 21 | |
| 22 | #ifndef NLSR_TEST_COMMON_HPP |
| 23 | #define NLSR_TEST_COMMON_HPP |
| 24 | |
Vince Lehman | 0a7da61 | 2014-10-29 14:39:29 -0500 | [diff] [blame] | 25 | #include "common.hpp" |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 26 | #include "identity-management-fixture.hpp" |
Vince Lehman | 0a7da61 | 2014-10-29 14:39:29 -0500 | [diff] [blame] | 27 | |
Vince Lehman | 7c60329 | 2014-09-11 17:48:16 -0500 | [diff] [blame] | 28 | #include <boost/asio.hpp> |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 29 | #include <boost/test/unit_test.hpp> |
Vince Lehman | 942eb7b | 2014-10-02 10:09:27 -0500 | [diff] [blame] | 30 | |
Vince Lehman | 7c60329 | 2014-09-11 17:48:16 -0500 | [diff] [blame] | 31 | #include <ndn-cxx/util/scheduler.hpp> |
Laqin Fan | a4cf402 | 2017-01-03 18:57:35 +0000 | [diff] [blame] | 32 | #include <ndn-cxx/security/key-chain.hpp> |
Vince Lehman | 02e3299 | 2015-03-11 12:31:20 -0500 | [diff] [blame] | 33 | #include <ndn-cxx/util/time-unit-test-clock.hpp> |
Vince Lehman | 7c60329 | 2014-09-11 17:48:16 -0500 | [diff] [blame] | 34 | |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 35 | #include <ndn-cxx/security/signature-sha256-with-rsa.hpp> |
| 36 | #include <ndn-cxx/face.hpp> |
| 37 | #include <ndn-cxx/util/dummy-client-face.hpp> |
| 38 | |
Vince Lehman | 7c60329 | 2014-09-11 17:48:16 -0500 | [diff] [blame] | 39 | namespace nlsr { |
| 40 | namespace test { |
| 41 | |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 42 | ndn::Data& |
| 43 | signData(ndn::Data& data); |
| 44 | |
| 45 | /** \brief add a fake signature to Data |
| 46 | */ |
| 47 | inline shared_ptr<ndn::Data> |
| 48 | signData(shared_ptr<ndn::Data> data) |
| 49 | { |
| 50 | signData(*data); |
| 51 | return data; |
| 52 | } |
| 53 | |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 54 | class BaseFixture : public tests::IdentityManagementFixture |
Vince Lehman | 7c60329 | 2014-09-11 17:48:16 -0500 | [diff] [blame] | 55 | { |
| 56 | public: |
| 57 | BaseFixture() |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 58 | : m_scheduler(m_ioService) |
Vince Lehman | 7c60329 | 2014-09-11 17:48:16 -0500 | [diff] [blame] | 59 | { |
| 60 | } |
| 61 | |
| 62 | protected: |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 63 | boost::asio::io_service m_ioService; |
| 64 | ndn::Scheduler m_scheduler; |
Vince Lehman | 7c60329 | 2014-09-11 17:48:16 -0500 | [diff] [blame] | 65 | }; |
| 66 | |
Vince Lehman | 02e3299 | 2015-03-11 12:31:20 -0500 | [diff] [blame] | 67 | class UnitTestTimeFixture : public BaseFixture |
| 68 | { |
| 69 | protected: |
| 70 | UnitTestTimeFixture() |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 71 | : steadyClock(std::make_shared<ndn::time::UnitTestSteadyClock>()) |
| 72 | , systemClock(std::make_shared<ndn::time::UnitTestSystemClock>()) |
Vince Lehman | 02e3299 | 2015-03-11 12:31:20 -0500 | [diff] [blame] | 73 | { |
| 74 | ndn::time::setCustomClocks(steadyClock, systemClock); |
| 75 | } |
| 76 | |
| 77 | ~UnitTestTimeFixture() |
| 78 | { |
| 79 | ndn::time::setCustomClocks(nullptr, nullptr); |
| 80 | } |
| 81 | |
| 82 | void |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 83 | advanceClocks(const ndn::time::nanoseconds& tick, size_t nTicks = 1); |
Vince Lehman | 02e3299 | 2015-03-11 12:31:20 -0500 | [diff] [blame] | 84 | |
| 85 | protected: |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 86 | std::shared_ptr<ndn::time::UnitTestSteadyClock> steadyClock; |
| 87 | std::shared_ptr<ndn::time::UnitTestSystemClock> systemClock; |
Vince Lehman | 02e3299 | 2015-03-11 12:31:20 -0500 | [diff] [blame] | 88 | }; |
| 89 | |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 90 | class MockNfdMgmtFixture : public UnitTestTimeFixture |
| 91 | { |
| 92 | public: |
| 93 | MockNfdMgmtFixture(); |
| 94 | |
Alexander Afanasyev | 67758b1 | 2018-03-06 18:36:44 -0500 | [diff] [blame^] | 95 | virtual |
| 96 | ~MockNfdMgmtFixture() = default; |
| 97 | |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 98 | /** \brief send one WireEncodable in reply to StatusDataset request |
| 99 | * \param prefix dataset prefix without version and segment |
| 100 | * \param payload payload block |
| 101 | * \note payload must fit in one Data |
| 102 | * \pre Interest for dataset has been expressed, sendDataset has not been invoked |
| 103 | */ |
| 104 | template<typename T> |
| 105 | void |
| 106 | sendDataset(const ndn::Name& prefix, const T& payload) |
| 107 | { |
| 108 | BOOST_CONCEPT_ASSERT((ndn::WireEncodable<T>)); |
| 109 | |
| 110 | this->sendDatasetReply(prefix, payload.wireEncode()); |
| 111 | } |
| 112 | |
| 113 | /** \brief send two WireEncodables in reply to StatusDataset request |
| 114 | * \param prefix dataset prefix without version and segment |
| 115 | * \param payload1 first vector item |
| 116 | * \param payload2 second vector item |
| 117 | * \note all payloads must fit in one Data |
| 118 | * \pre Interest for dataset has been expressed, sendDataset has not been invoked |
| 119 | */ |
| 120 | template<typename T1, typename T2> |
| 121 | void |
| 122 | sendDataset(const ndn::Name& prefix, const T1& payload1, const T2& payload2) |
| 123 | { |
| 124 | BOOST_CONCEPT_ASSERT((ndn::WireEncodable<T1>)); |
| 125 | BOOST_CONCEPT_ASSERT((ndn::WireEncodable<T2>)); |
| 126 | |
| 127 | ndn::encoding::EncodingBuffer buffer; |
| 128 | payload2.wireEncode(buffer); |
| 129 | payload1.wireEncode(buffer); |
| 130 | |
| 131 | this->sendDatasetReply(prefix, buffer.buf(), buffer.size()); |
| 132 | } |
| 133 | |
| 134 | /** \brief send a payload in reply to StatusDataset request |
| 135 | * \param name dataset prefix without version and segment |
| 136 | * \param contentArgs passed to Data::setContent |
| 137 | */ |
| 138 | template<typename ...ContentArgs> |
| 139 | void |
| 140 | sendDatasetReply(ndn::Name name, ContentArgs&&... contentArgs) |
| 141 | { |
| 142 | name.appendVersion().appendSegment(0); |
| 143 | |
| 144 | // These warnings assist in debugging when nfdc does not receive StatusDataset. |
| 145 | // They usually indicate a misspelled prefix or incorrect timing in the test case. |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 146 | if (m_face.sentInterests.empty()) { |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 147 | BOOST_WARN_MESSAGE(false, "no Interest expressed"); |
| 148 | } |
| 149 | else { |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 150 | BOOST_WARN_MESSAGE(m_face.sentInterests.back().getName().isPrefixOf(name), |
| 151 | "last Interest " << m_face.sentInterests.back().getName() << |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 152 | " cannot be satisfied by this Data " << name); |
| 153 | } |
| 154 | |
| 155 | auto data = make_shared<ndn::Data>(name); |
| 156 | data->setFinalBlockId(name[-1]); |
| 157 | data->setContent(std::forward<ContentArgs>(contentArgs)...); |
| 158 | this->signDatasetReply(*data); |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 159 | m_face.receive(*data); |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | virtual void |
| 163 | signDatasetReply(ndn::Data& data); |
| 164 | |
| 165 | public: |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 166 | ndn::util::DummyClientFace m_face; |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 167 | }; |
| 168 | |
Vince Lehman | 7c60329 | 2014-09-11 17:48:16 -0500 | [diff] [blame] | 169 | } // namespace test |
| 170 | } // namespace nlsr |
| 171 | |
Vince Lehman | 942eb7b | 2014-10-02 10:09:27 -0500 | [diff] [blame] | 172 | #endif // NLSR_TEST_COMMON_HPP |