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 | /* |
Davide Pesavento | e28d875 | 2022-03-19 03:55:25 -0400 | [diff] [blame^] | 3 | * Copyright (c) 2014-2022, The University of Memphis, |
Alexander Afanasyev | 67758b1 | 2018-03-06 18:36:44 -0500 | [diff] [blame] | 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" |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 26 | #include "conf-parameter.hpp" |
Davide Pesavento | e28d875 | 2022-03-19 03:55:25 -0400 | [diff] [blame^] | 27 | #include "route/fib.hpp" |
Vince Lehman | 0a7da61 | 2014-10-29 14:39:29 -0500 | [diff] [blame] | 28 | |
Davide Pesavento | cb065f1 | 2019-12-27 01:03:34 -0500 | [diff] [blame] | 29 | #include "boost-test.hpp" |
Davide Pesavento | e28d875 | 2022-03-19 03:55:25 -0400 | [diff] [blame^] | 30 | #include "identity-management-fixture.hpp" |
Davide Pesavento | cb065f1 | 2019-12-27 01:03:34 -0500 | [diff] [blame] | 31 | |
Vince Lehman | 7c60329 | 2014-09-11 17:48:16 -0500 | [diff] [blame] | 32 | #include <boost/asio.hpp> |
Vince Lehman | 942eb7b | 2014-10-02 10:09:27 -0500 | [diff] [blame] | 33 | |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 34 | #include <ndn-cxx/face.hpp> |
Junxiao Shi | 0b1b7d9 | 2019-05-22 15:37:18 +0000 | [diff] [blame] | 35 | #include <ndn-cxx/security/key-chain.hpp> |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 36 | #include <ndn-cxx/util/dummy-client-face.hpp> |
Junxiao Shi | 0b1b7d9 | 2019-05-22 15:37:18 +0000 | [diff] [blame] | 37 | #include <ndn-cxx/util/scheduler.hpp> |
| 38 | #include <ndn-cxx/util/time-unit-test-clock.hpp> |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 39 | |
Vince Lehman | 7c60329 | 2014-09-11 17:48:16 -0500 | [diff] [blame] | 40 | namespace nlsr { |
| 41 | namespace test { |
| 42 | |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 43 | ndn::Data& |
| 44 | signData(ndn::Data& data); |
| 45 | |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame] | 46 | void |
| 47 | checkPrefixRegistered(const ndn::util::DummyClientFace& face, const ndn::Name& prefix); |
| 48 | |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 49 | /** \brief add a fake signature to Data |
| 50 | */ |
Davide Pesavento | d90338d | 2021-01-07 17:50:05 -0500 | [diff] [blame] | 51 | inline std::shared_ptr<ndn::Data> |
| 52 | signData(std::shared_ptr<ndn::Data> data) |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 53 | { |
| 54 | signData(*data); |
| 55 | return data; |
| 56 | } |
| 57 | |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 58 | class BaseFixture : public tests::IdentityManagementFixture |
Vince Lehman | 7c60329 | 2014-09-11 17:48:16 -0500 | [diff] [blame] | 59 | { |
| 60 | public: |
| 61 | BaseFixture() |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 62 | : m_scheduler(m_ioService) |
Vince Lehman | 7c60329 | 2014-09-11 17:48:16 -0500 | [diff] [blame] | 63 | { |
| 64 | } |
| 65 | |
| 66 | protected: |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 67 | boost::asio::io_service m_ioService; |
| 68 | ndn::Scheduler m_scheduler; |
Vince Lehman | 7c60329 | 2014-09-11 17:48:16 -0500 | [diff] [blame] | 69 | }; |
| 70 | |
Vince Lehman | 02e3299 | 2015-03-11 12:31:20 -0500 | [diff] [blame] | 71 | class UnitTestTimeFixture : public BaseFixture |
| 72 | { |
| 73 | protected: |
| 74 | UnitTestTimeFixture() |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 75 | : steadyClock(std::make_shared<ndn::time::UnitTestSteadyClock>()) |
| 76 | , systemClock(std::make_shared<ndn::time::UnitTestSystemClock>()) |
Vince Lehman | 02e3299 | 2015-03-11 12:31:20 -0500 | [diff] [blame] | 77 | { |
| 78 | ndn::time::setCustomClocks(steadyClock, systemClock); |
| 79 | } |
| 80 | |
| 81 | ~UnitTestTimeFixture() |
| 82 | { |
| 83 | ndn::time::setCustomClocks(nullptr, nullptr); |
| 84 | } |
| 85 | |
| 86 | void |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 87 | advanceClocks(const ndn::time::nanoseconds& tick, size_t nTicks = 1); |
Vince Lehman | 02e3299 | 2015-03-11 12:31:20 -0500 | [diff] [blame] | 88 | |
| 89 | protected: |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 90 | std::shared_ptr<ndn::time::UnitTestSteadyClock> steadyClock; |
| 91 | std::shared_ptr<ndn::time::UnitTestSystemClock> systemClock; |
Vince Lehman | 02e3299 | 2015-03-11 12:31:20 -0500 | [diff] [blame] | 92 | }; |
| 93 | |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 94 | class MockNfdMgmtFixture : public UnitTestTimeFixture |
| 95 | { |
| 96 | public: |
| 97 | MockNfdMgmtFixture(); |
| 98 | |
Alexander Afanasyev | 67758b1 | 2018-03-06 18:36:44 -0500 | [diff] [blame] | 99 | virtual |
| 100 | ~MockNfdMgmtFixture() = default; |
| 101 | |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 102 | /** \brief send one WireEncodable in reply to StatusDataset request |
| 103 | * \param prefix dataset prefix without version and segment |
| 104 | * \param payload payload block |
| 105 | * \note payload must fit in one Data |
| 106 | * \pre Interest for dataset has been expressed, sendDataset has not been invoked |
| 107 | */ |
| 108 | template<typename T> |
| 109 | void |
| 110 | sendDataset(const ndn::Name& prefix, const T& payload) |
| 111 | { |
| 112 | BOOST_CONCEPT_ASSERT((ndn::WireEncodable<T>)); |
| 113 | |
| 114 | this->sendDatasetReply(prefix, payload.wireEncode()); |
| 115 | } |
| 116 | |
| 117 | /** \brief send two WireEncodables in reply to StatusDataset request |
| 118 | * \param prefix dataset prefix without version and segment |
| 119 | * \param payload1 first vector item |
| 120 | * \param payload2 second vector item |
| 121 | * \note all payloads must fit in one Data |
| 122 | * \pre Interest for dataset has been expressed, sendDataset has not been invoked |
| 123 | */ |
| 124 | template<typename T1, typename T2> |
| 125 | void |
| 126 | sendDataset(const ndn::Name& prefix, const T1& payload1, const T2& payload2) |
| 127 | { |
| 128 | BOOST_CONCEPT_ASSERT((ndn::WireEncodable<T1>)); |
| 129 | BOOST_CONCEPT_ASSERT((ndn::WireEncodable<T2>)); |
| 130 | |
| 131 | ndn::encoding::EncodingBuffer buffer; |
| 132 | payload2.wireEncode(buffer); |
| 133 | payload1.wireEncode(buffer); |
| 134 | |
Davide Pesavento | e28d875 | 2022-03-19 03:55:25 -0400 | [diff] [blame^] | 135 | this->sendDatasetReply(prefix, buffer); |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | /** \brief send a payload in reply to StatusDataset request |
| 139 | * \param name dataset prefix without version and segment |
| 140 | * \param contentArgs passed to Data::setContent |
| 141 | */ |
| 142 | template<typename ...ContentArgs> |
| 143 | void |
| 144 | sendDatasetReply(ndn::Name name, ContentArgs&&... contentArgs) |
| 145 | { |
| 146 | name.appendVersion().appendSegment(0); |
| 147 | |
| 148 | // These warnings assist in debugging when nfdc does not receive StatusDataset. |
| 149 | // 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] | 150 | if (m_face.sentInterests.empty()) { |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 151 | BOOST_WARN_MESSAGE(false, "no Interest expressed"); |
| 152 | } |
| 153 | else { |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 154 | BOOST_WARN_MESSAGE(m_face.sentInterests.back().getName().isPrefixOf(name), |
| 155 | "last Interest " << m_face.sentInterests.back().getName() << |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 156 | " cannot be satisfied by this Data " << name); |
| 157 | } |
| 158 | |
Davide Pesavento | d90338d | 2021-01-07 17:50:05 -0500 | [diff] [blame] | 159 | auto data = std::make_shared<ndn::Data>(name); |
Junxiao Shi | 0b1b7d9 | 2019-05-22 15:37:18 +0000 | [diff] [blame] | 160 | data->setFreshnessPeriod(1_s); |
Ashlesh Gawande | d65786a | 2018-03-30 01:17:58 -0500 | [diff] [blame] | 161 | data->setFinalBlock(name[-1]); |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 162 | data->setContent(std::forward<ContentArgs>(contentArgs)...); |
| 163 | this->signDatasetReply(*data); |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 164 | m_face.receive(*data); |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | virtual void |
| 168 | signDatasetReply(ndn::Data& data); |
| 169 | |
| 170 | public: |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 171 | ndn::util::DummyClientFace m_face; |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 172 | }; |
| 173 | |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 174 | class DummyConfFileProcessor |
| 175 | { |
| 176 | typedef std::function<void(ConfParameter&)> AfterConfProcessing; |
| 177 | |
| 178 | public: |
| 179 | DummyConfFileProcessor(ConfParameter& conf, |
Ashlesh Gawande | 30d96e4 | 2021-03-21 19:15:33 -0700 | [diff] [blame] | 180 | SyncProtocol protocol = SYNC_PROTOCOL_PSYNC, |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame] | 181 | int32_t hyperbolicState = HYPERBOLIC_STATE_OFF, |
| 182 | ndn::Name networkName = "/ndn", ndn::Name siteName = "/site", |
| 183 | ndn::Name routerName = "/%C1.Router/this-router") |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 184 | { |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame] | 185 | conf.setNetwork(networkName); |
| 186 | conf.setSiteName(siteName); |
| 187 | conf.setRouterName(routerName); |
Ashlesh Gawande | 6b388fc | 2019-09-30 10:14:41 -0500 | [diff] [blame] | 188 | conf.buildRouterAndSyncUserPrefix(); |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 189 | conf.setSyncProtocol(protocol); |
Ashlesh Gawande | 6b388fc | 2019-09-30 10:14:41 -0500 | [diff] [blame] | 190 | conf.setHyperbolicState(hyperbolicState); |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 191 | } |
| 192 | }; |
| 193 | |
Vince Lehman | 7c60329 | 2014-09-11 17:48:16 -0500 | [diff] [blame] | 194 | } // namespace test |
| 195 | } // namespace nlsr |
| 196 | |
Vince Lehman | 942eb7b | 2014-10-02 10:09:27 -0500 | [diff] [blame] | 197 | #endif // NLSR_TEST_COMMON_HPP |