blob: 49238ad5c0946a0c47b281003943afe2e99a20e1 [file] [log] [blame]
Vince Lehman7c603292014-09-11 17:48:16 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyev67758b12018-03-06 18:36:44 -05002/*
Saurab Dulal427e0122019-11-28 11:58:02 -06003 * Copyright (c) 2014-2020, The University of Memphis,
Alexander Afanasyev67758b12018-03-06 18:36:44 -05004 * Regents of the University of California,
5 * Arizona Board of Regents.
Vince Lehman7c603292014-09-11 17:48:16 -05006 *
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 Afanasyev67758b12018-03-06 18:36:44 -050020 */
Vince Lehman7c603292014-09-11 17:48:16 -050021
22#ifndef NLSR_TEST_COMMON_HPP
23#define NLSR_TEST_COMMON_HPP
24
Vince Lehman0a7da612014-10-29 14:39:29 -050025#include "common.hpp"
Ashlesh Gawande85998a12017-12-07 22:22:13 -060026#include "conf-parameter.hpp"
Saurab Dulal427e0122019-11-28 11:58:02 -060027#include "identity-management-fixture.hpp"
Vince Lehman0a7da612014-10-29 14:39:29 -050028
Davide Pesaventocb065f12019-12-27 01:03:34 -050029#include "boost-test.hpp"
Saurab Dulal427e0122019-11-28 11:58:02 -060030#include "route/fib.hpp"
Davide Pesaventocb065f12019-12-27 01:03:34 -050031
Vince Lehman7c603292014-09-11 17:48:16 -050032#include <boost/asio.hpp>
Vince Lehman942eb7b2014-10-02 10:09:27 -050033
Nick Gordond5c1a372016-10-31 13:56:23 -050034#include <ndn-cxx/face.hpp>
Junxiao Shi0b1b7d92019-05-22 15:37:18 +000035#include <ndn-cxx/security/key-chain.hpp>
36#include <ndn-cxx/security/signature-sha256-with-rsa.hpp>
Nick Gordond5c1a372016-10-31 13:56:23 -050037#include <ndn-cxx/util/dummy-client-face.hpp>
Junxiao Shi0b1b7d92019-05-22 15:37:18 +000038#include <ndn-cxx/util/scheduler.hpp>
39#include <ndn-cxx/util/time-unit-test-clock.hpp>
Nick Gordond5c1a372016-10-31 13:56:23 -050040
Vince Lehman7c603292014-09-11 17:48:16 -050041namespace nlsr {
42namespace test {
43
Junxiao Shi0b1b7d92019-05-22 15:37:18 +000044using namespace ndn::time_literals;
45
Nick Gordond5c1a372016-10-31 13:56:23 -050046ndn::Data&
47signData(ndn::Data& data);
48
Saurab Dulal427e0122019-11-28 11:58:02 -060049void
50checkPrefixRegistered(const ndn::util::DummyClientFace& face, const ndn::Name& prefix);
51
Nick Gordond5c1a372016-10-31 13:56:23 -050052/** \brief add a fake signature to Data
53 */
54inline shared_ptr<ndn::Data>
55signData(shared_ptr<ndn::Data> data)
56{
57 signData(*data);
58 return data;
59}
60
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050061class BaseFixture : public tests::IdentityManagementFixture
Vince Lehman7c603292014-09-11 17:48:16 -050062{
63public:
64 BaseFixture()
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050065 : m_scheduler(m_ioService)
Vince Lehman7c603292014-09-11 17:48:16 -050066 {
67 }
68
69protected:
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050070 boost::asio::io_service m_ioService;
71 ndn::Scheduler m_scheduler;
Vince Lehman7c603292014-09-11 17:48:16 -050072};
73
Vince Lehman02e32992015-03-11 12:31:20 -050074class UnitTestTimeFixture : public BaseFixture
75{
76protected:
77 UnitTestTimeFixture()
dmcoomes9f936662017-03-02 10:33:09 -060078 : steadyClock(std::make_shared<ndn::time::UnitTestSteadyClock>())
79 , systemClock(std::make_shared<ndn::time::UnitTestSystemClock>())
Vince Lehman02e32992015-03-11 12:31:20 -050080 {
81 ndn::time::setCustomClocks(steadyClock, systemClock);
82 }
83
84 ~UnitTestTimeFixture()
85 {
86 ndn::time::setCustomClocks(nullptr, nullptr);
87 }
88
89 void
Nick Gordond5c1a372016-10-31 13:56:23 -050090 advanceClocks(const ndn::time::nanoseconds& tick, size_t nTicks = 1);
Vince Lehman02e32992015-03-11 12:31:20 -050091
92protected:
dmcoomes9f936662017-03-02 10:33:09 -060093 std::shared_ptr<ndn::time::UnitTestSteadyClock> steadyClock;
94 std::shared_ptr<ndn::time::UnitTestSystemClock> systemClock;
Vince Lehman02e32992015-03-11 12:31:20 -050095};
96
Nick Gordond5c1a372016-10-31 13:56:23 -050097class MockNfdMgmtFixture : public UnitTestTimeFixture
98{
99public:
100 MockNfdMgmtFixture();
101
Alexander Afanasyev67758b12018-03-06 18:36:44 -0500102 virtual
103 ~MockNfdMgmtFixture() = default;
104
Nick Gordond5c1a372016-10-31 13:56:23 -0500105 /** \brief send one WireEncodable in reply to StatusDataset request
106 * \param prefix dataset prefix without version and segment
107 * \param payload payload block
108 * \note payload must fit in one Data
109 * \pre Interest for dataset has been expressed, sendDataset has not been invoked
110 */
111 template<typename T>
112 void
113 sendDataset(const ndn::Name& prefix, const T& payload)
114 {
115 BOOST_CONCEPT_ASSERT((ndn::WireEncodable<T>));
116
117 this->sendDatasetReply(prefix, payload.wireEncode());
118 }
119
120 /** \brief send two WireEncodables in reply to StatusDataset request
121 * \param prefix dataset prefix without version and segment
122 * \param payload1 first vector item
123 * \param payload2 second vector item
124 * \note all payloads must fit in one Data
125 * \pre Interest for dataset has been expressed, sendDataset has not been invoked
126 */
127 template<typename T1, typename T2>
128 void
129 sendDataset(const ndn::Name& prefix, const T1& payload1, const T2& payload2)
130 {
131 BOOST_CONCEPT_ASSERT((ndn::WireEncodable<T1>));
132 BOOST_CONCEPT_ASSERT((ndn::WireEncodable<T2>));
133
134 ndn::encoding::EncodingBuffer buffer;
135 payload2.wireEncode(buffer);
136 payload1.wireEncode(buffer);
137
138 this->sendDatasetReply(prefix, buffer.buf(), buffer.size());
139 }
140
141 /** \brief send a payload in reply to StatusDataset request
142 * \param name dataset prefix without version and segment
143 * \param contentArgs passed to Data::setContent
144 */
145 template<typename ...ContentArgs>
146 void
147 sendDatasetReply(ndn::Name name, ContentArgs&&... contentArgs)
148 {
149 name.appendVersion().appendSegment(0);
150
151 // These warnings assist in debugging when nfdc does not receive StatusDataset.
152 // They usually indicate a misspelled prefix or incorrect timing in the test case.
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500153 if (m_face.sentInterests.empty()) {
Nick Gordond5c1a372016-10-31 13:56:23 -0500154 BOOST_WARN_MESSAGE(false, "no Interest expressed");
155 }
156 else {
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500157 BOOST_WARN_MESSAGE(m_face.sentInterests.back().getName().isPrefixOf(name),
158 "last Interest " << m_face.sentInterests.back().getName() <<
Nick Gordond5c1a372016-10-31 13:56:23 -0500159 " cannot be satisfied by this Data " << name);
160 }
161
162 auto data = make_shared<ndn::Data>(name);
Junxiao Shi0b1b7d92019-05-22 15:37:18 +0000163 data->setFreshnessPeriod(1_s);
Ashlesh Gawanded65786a2018-03-30 01:17:58 -0500164 data->setFinalBlock(name[-1]);
Nick Gordond5c1a372016-10-31 13:56:23 -0500165 data->setContent(std::forward<ContentArgs>(contentArgs)...);
166 this->signDatasetReply(*data);
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500167 m_face.receive(*data);
Nick Gordond5c1a372016-10-31 13:56:23 -0500168 }
169
170 virtual void
171 signDatasetReply(ndn::Data& data);
172
173public:
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500174 ndn::util::DummyClientFace m_face;
Nick Gordond5c1a372016-10-31 13:56:23 -0500175};
176
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600177class DummyConfFileProcessor
178{
179 typedef std::function<void(ConfParameter&)> AfterConfProcessing;
180
181public:
182 DummyConfFileProcessor(ConfParameter& conf,
183 int32_t protocol = SYNC_PROTOCOL_PSYNC,
Saurab Dulal427e0122019-11-28 11:58:02 -0600184 int32_t hyperbolicState = HYPERBOLIC_STATE_OFF,
185 ndn::Name networkName = "/ndn", ndn::Name siteName = "/site",
186 ndn::Name routerName = "/%C1.Router/this-router")
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600187 {
Saurab Dulal427e0122019-11-28 11:58:02 -0600188 conf.setNetwork(networkName);
189 conf.setSiteName(siteName);
190 conf.setRouterName(routerName);
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500191 conf.buildRouterAndSyncUserPrefix();
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600192
193 conf.setSyncProtocol(protocol);
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500194 conf.setHyperbolicState(hyperbolicState);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600195 }
196};
197
Vince Lehman7c603292014-09-11 17:48:16 -0500198} // namespace test
199} // namespace nlsr
200
Vince Lehman942eb7b2014-10-02 10:09:27 -0500201#endif // NLSR_TEST_COMMON_HPP