blob: 878a27d949187bc908ca5bbbc62d327c9eca7ad3 [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/*
Ashlesh Gawande85998a12017-12-07 22:22:13 -06003 * Copyright (c) 2014-2019, 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"
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050026#include "identity-management-fixture.hpp"
Ashlesh Gawande85998a12017-12-07 22:22:13 -060027#include "conf-parameter.hpp"
Vince Lehman0a7da612014-10-29 14:39:29 -050028
Vince Lehman7c603292014-09-11 17:48:16 -050029#include <boost/asio.hpp>
Vince Lehman904c2412014-09-23 19:36:11 -050030#include <boost/test/unit_test.hpp>
Vince Lehman942eb7b2014-10-02 10:09:27 -050031
Nick Gordond5c1a372016-10-31 13:56:23 -050032#include <ndn-cxx/face.hpp>
Junxiao Shi0b1b7d92019-05-22 15:37:18 +000033#include <ndn-cxx/security/key-chain.hpp>
34#include <ndn-cxx/security/signature-sha256-with-rsa.hpp>
Nick Gordond5c1a372016-10-31 13:56:23 -050035#include <ndn-cxx/util/dummy-client-face.hpp>
Junxiao Shi0b1b7d92019-05-22 15:37:18 +000036#include <ndn-cxx/util/scheduler.hpp>
37#include <ndn-cxx/util/time-unit-test-clock.hpp>
Nick Gordond5c1a372016-10-31 13:56:23 -050038
Vince Lehman7c603292014-09-11 17:48:16 -050039namespace nlsr {
40namespace test {
41
Junxiao Shi0b1b7d92019-05-22 15:37:18 +000042using namespace ndn::time_literals;
43
Nick Gordond5c1a372016-10-31 13:56:23 -050044ndn::Data&
45signData(ndn::Data& data);
46
47/** \brief add a fake signature to Data
48 */
49inline shared_ptr<ndn::Data>
50signData(shared_ptr<ndn::Data> data)
51{
52 signData(*data);
53 return data;
54}
55
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050056class BaseFixture : public tests::IdentityManagementFixture
Vince Lehman7c603292014-09-11 17:48:16 -050057{
58public:
59 BaseFixture()
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050060 : m_scheduler(m_ioService)
Vince Lehman7c603292014-09-11 17:48:16 -050061 {
62 }
63
64protected:
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050065 boost::asio::io_service m_ioService;
66 ndn::Scheduler m_scheduler;
Vince Lehman7c603292014-09-11 17:48:16 -050067};
68
Vince Lehman02e32992015-03-11 12:31:20 -050069class UnitTestTimeFixture : public BaseFixture
70{
71protected:
72 UnitTestTimeFixture()
dmcoomes9f936662017-03-02 10:33:09 -060073 : steadyClock(std::make_shared<ndn::time::UnitTestSteadyClock>())
74 , systemClock(std::make_shared<ndn::time::UnitTestSystemClock>())
Vince Lehman02e32992015-03-11 12:31:20 -050075 {
76 ndn::time::setCustomClocks(steadyClock, systemClock);
77 }
78
79 ~UnitTestTimeFixture()
80 {
81 ndn::time::setCustomClocks(nullptr, nullptr);
82 }
83
84 void
Nick Gordond5c1a372016-10-31 13:56:23 -050085 advanceClocks(const ndn::time::nanoseconds& tick, size_t nTicks = 1);
Vince Lehman02e32992015-03-11 12:31:20 -050086
87protected:
dmcoomes9f936662017-03-02 10:33:09 -060088 std::shared_ptr<ndn::time::UnitTestSteadyClock> steadyClock;
89 std::shared_ptr<ndn::time::UnitTestSystemClock> systemClock;
Vince Lehman02e32992015-03-11 12:31:20 -050090};
91
Nick Gordond5c1a372016-10-31 13:56:23 -050092class MockNfdMgmtFixture : public UnitTestTimeFixture
93{
94public:
95 MockNfdMgmtFixture();
96
Alexander Afanasyev67758b12018-03-06 18:36:44 -050097 virtual
98 ~MockNfdMgmtFixture() = default;
99
Nick Gordond5c1a372016-10-31 13:56:23 -0500100 /** \brief send one WireEncodable in reply to StatusDataset request
101 * \param prefix dataset prefix without version and segment
102 * \param payload payload block
103 * \note payload must fit in one Data
104 * \pre Interest for dataset has been expressed, sendDataset has not been invoked
105 */
106 template<typename T>
107 void
108 sendDataset(const ndn::Name& prefix, const T& payload)
109 {
110 BOOST_CONCEPT_ASSERT((ndn::WireEncodable<T>));
111
112 this->sendDatasetReply(prefix, payload.wireEncode());
113 }
114
115 /** \brief send two WireEncodables in reply to StatusDataset request
116 * \param prefix dataset prefix without version and segment
117 * \param payload1 first vector item
118 * \param payload2 second vector item
119 * \note all payloads must fit in one Data
120 * \pre Interest for dataset has been expressed, sendDataset has not been invoked
121 */
122 template<typename T1, typename T2>
123 void
124 sendDataset(const ndn::Name& prefix, const T1& payload1, const T2& payload2)
125 {
126 BOOST_CONCEPT_ASSERT((ndn::WireEncodable<T1>));
127 BOOST_CONCEPT_ASSERT((ndn::WireEncodable<T2>));
128
129 ndn::encoding::EncodingBuffer buffer;
130 payload2.wireEncode(buffer);
131 payload1.wireEncode(buffer);
132
133 this->sendDatasetReply(prefix, buffer.buf(), buffer.size());
134 }
135
136 /** \brief send a payload in reply to StatusDataset request
137 * \param name dataset prefix without version and segment
138 * \param contentArgs passed to Data::setContent
139 */
140 template<typename ...ContentArgs>
141 void
142 sendDatasetReply(ndn::Name name, ContentArgs&&... contentArgs)
143 {
144 name.appendVersion().appendSegment(0);
145
146 // These warnings assist in debugging when nfdc does not receive StatusDataset.
147 // They usually indicate a misspelled prefix or incorrect timing in the test case.
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500148 if (m_face.sentInterests.empty()) {
Nick Gordond5c1a372016-10-31 13:56:23 -0500149 BOOST_WARN_MESSAGE(false, "no Interest expressed");
150 }
151 else {
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500152 BOOST_WARN_MESSAGE(m_face.sentInterests.back().getName().isPrefixOf(name),
153 "last Interest " << m_face.sentInterests.back().getName() <<
Nick Gordond5c1a372016-10-31 13:56:23 -0500154 " cannot be satisfied by this Data " << name);
155 }
156
157 auto data = make_shared<ndn::Data>(name);
Junxiao Shi0b1b7d92019-05-22 15:37:18 +0000158 data->setFreshnessPeriod(1_s);
Ashlesh Gawanded65786a2018-03-30 01:17:58 -0500159 data->setFinalBlock(name[-1]);
Nick Gordond5c1a372016-10-31 13:56:23 -0500160 data->setContent(std::forward<ContentArgs>(contentArgs)...);
161 this->signDatasetReply(*data);
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500162 m_face.receive(*data);
Nick Gordond5c1a372016-10-31 13:56:23 -0500163 }
164
165 virtual void
166 signDatasetReply(ndn::Data& data);
167
168public:
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500169 ndn::util::DummyClientFace m_face;
Nick Gordond5c1a372016-10-31 13:56:23 -0500170};
171
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600172class DummyConfFileProcessor
173{
174 typedef std::function<void(ConfParameter&)> AfterConfProcessing;
175
176public:
177 DummyConfFileProcessor(ConfParameter& conf,
178 int32_t protocol = SYNC_PROTOCOL_PSYNC,
179 int32_t hyperbolicState = HYPERBOLIC_STATE_OFF)
180 {
181 conf.setNetwork("/ndn");
182 conf.setSiteName("/site");
183 conf.setRouterName("/%C1.Router/this-router");
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500184 conf.buildRouterAndSyncUserPrefix();
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600185
186 conf.setSyncProtocol(protocol);
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500187 conf.setHyperbolicState(hyperbolicState);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600188 }
189};
190
Vince Lehman7c603292014-09-11 17:48:16 -0500191} // namespace test
192} // namespace nlsr
193
Vince Lehman942eb7b2014-10-02 10:09:27 -0500194#endif // NLSR_TEST_COMMON_HPP