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