blob: 339fd82a113a118217d16fd3c19244e7e7e3f6e2 [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"
Ashlesh Gawande85998a12017-12-07 22:22:13 -060026#include "conf-parameter.hpp"
Vince Lehman0a7da612014-10-29 14:39:29 -050027
Davide Pesaventocb065f12019-12-27 01:03:34 -050028#include "boost-test.hpp"
29#include "identity-management-fixture.hpp"
30
Vince Lehman7c603292014-09-11 17:48:16 -050031#include <boost/asio.hpp>
Vince Lehman942eb7b2014-10-02 10:09:27 -050032
Nick Gordond5c1a372016-10-31 13:56:23 -050033#include <ndn-cxx/face.hpp>
Junxiao Shi0b1b7d92019-05-22 15:37:18 +000034#include <ndn-cxx/security/key-chain.hpp>
35#include <ndn-cxx/security/signature-sha256-with-rsa.hpp>
Nick Gordond5c1a372016-10-31 13:56:23 -050036#include <ndn-cxx/util/dummy-client-face.hpp>
Junxiao Shi0b1b7d92019-05-22 15:37:18 +000037#include <ndn-cxx/util/scheduler.hpp>
38#include <ndn-cxx/util/time-unit-test-clock.hpp>
Nick Gordond5c1a372016-10-31 13:56:23 -050039
Vince Lehman7c603292014-09-11 17:48:16 -050040namespace nlsr {
41namespace test {
42
Junxiao Shi0b1b7d92019-05-22 15:37:18 +000043using namespace ndn::time_literals;
44
Nick Gordond5c1a372016-10-31 13:56:23 -050045ndn::Data&
46signData(ndn::Data& data);
47
48/** \brief add a fake signature to Data
49 */
50inline shared_ptr<ndn::Data>
51signData(shared_ptr<ndn::Data> data)
52{
53 signData(*data);
54 return data;
55}
56
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050057class BaseFixture : public tests::IdentityManagementFixture
Vince Lehman7c603292014-09-11 17:48:16 -050058{
59public:
60 BaseFixture()
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050061 : m_scheduler(m_ioService)
Vince Lehman7c603292014-09-11 17:48:16 -050062 {
63 }
64
65protected:
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050066 boost::asio::io_service m_ioService;
67 ndn::Scheduler m_scheduler;
Vince Lehman7c603292014-09-11 17:48:16 -050068};
69
Vince Lehman02e32992015-03-11 12:31:20 -050070class UnitTestTimeFixture : public BaseFixture
71{
72protected:
73 UnitTestTimeFixture()
dmcoomes9f936662017-03-02 10:33:09 -060074 : steadyClock(std::make_shared<ndn::time::UnitTestSteadyClock>())
75 , systemClock(std::make_shared<ndn::time::UnitTestSystemClock>())
Vince Lehman02e32992015-03-11 12:31:20 -050076 {
77 ndn::time::setCustomClocks(steadyClock, systemClock);
78 }
79
80 ~UnitTestTimeFixture()
81 {
82 ndn::time::setCustomClocks(nullptr, nullptr);
83 }
84
85 void
Nick Gordond5c1a372016-10-31 13:56:23 -050086 advanceClocks(const ndn::time::nanoseconds& tick, size_t nTicks = 1);
Vince Lehman02e32992015-03-11 12:31:20 -050087
88protected:
dmcoomes9f936662017-03-02 10:33:09 -060089 std::shared_ptr<ndn::time::UnitTestSteadyClock> steadyClock;
90 std::shared_ptr<ndn::time::UnitTestSystemClock> systemClock;
Vince Lehman02e32992015-03-11 12:31:20 -050091};
92
Nick Gordond5c1a372016-10-31 13:56:23 -050093class MockNfdMgmtFixture : public UnitTestTimeFixture
94{
95public:
96 MockNfdMgmtFixture();
97
Alexander Afanasyev67758b12018-03-06 18:36:44 -050098 virtual
99 ~MockNfdMgmtFixture() = default;
100
Nick Gordond5c1a372016-10-31 13:56:23 -0500101 /** \brief send one WireEncodable in reply to StatusDataset request
102 * \param prefix dataset prefix without version and segment
103 * \param payload payload block
104 * \note payload must fit in one Data
105 * \pre Interest for dataset has been expressed, sendDataset has not been invoked
106 */
107 template<typename T>
108 void
109 sendDataset(const ndn::Name& prefix, const T& payload)
110 {
111 BOOST_CONCEPT_ASSERT((ndn::WireEncodable<T>));
112
113 this->sendDatasetReply(prefix, payload.wireEncode());
114 }
115
116 /** \brief send two WireEncodables in reply to StatusDataset request
117 * \param prefix dataset prefix without version and segment
118 * \param payload1 first vector item
119 * \param payload2 second vector item
120 * \note all payloads must fit in one Data
121 * \pre Interest for dataset has been expressed, sendDataset has not been invoked
122 */
123 template<typename T1, typename T2>
124 void
125 sendDataset(const ndn::Name& prefix, const T1& payload1, const T2& payload2)
126 {
127 BOOST_CONCEPT_ASSERT((ndn::WireEncodable<T1>));
128 BOOST_CONCEPT_ASSERT((ndn::WireEncodable<T2>));
129
130 ndn::encoding::EncodingBuffer buffer;
131 payload2.wireEncode(buffer);
132 payload1.wireEncode(buffer);
133
134 this->sendDatasetReply(prefix, buffer.buf(), buffer.size());
135 }
136
137 /** \brief send a payload in reply to StatusDataset request
138 * \param name dataset prefix without version and segment
139 * \param contentArgs passed to Data::setContent
140 */
141 template<typename ...ContentArgs>
142 void
143 sendDatasetReply(ndn::Name name, ContentArgs&&... contentArgs)
144 {
145 name.appendVersion().appendSegment(0);
146
147 // These warnings assist in debugging when nfdc does not receive StatusDataset.
148 // They usually indicate a misspelled prefix or incorrect timing in the test case.
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500149 if (m_face.sentInterests.empty()) {
Nick Gordond5c1a372016-10-31 13:56:23 -0500150 BOOST_WARN_MESSAGE(false, "no Interest expressed");
151 }
152 else {
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500153 BOOST_WARN_MESSAGE(m_face.sentInterests.back().getName().isPrefixOf(name),
154 "last Interest " << m_face.sentInterests.back().getName() <<
Nick Gordond5c1a372016-10-31 13:56:23 -0500155 " cannot be satisfied by this Data " << name);
156 }
157
158 auto data = make_shared<ndn::Data>(name);
Junxiao Shi0b1b7d92019-05-22 15:37:18 +0000159 data->setFreshnessPeriod(1_s);
Ashlesh Gawanded65786a2018-03-30 01:17:58 -0500160 data->setFinalBlock(name[-1]);
Nick Gordond5c1a372016-10-31 13:56:23 -0500161 data->setContent(std::forward<ContentArgs>(contentArgs)...);
162 this->signDatasetReply(*data);
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500163 m_face.receive(*data);
Nick Gordond5c1a372016-10-31 13:56:23 -0500164 }
165
166 virtual void
167 signDatasetReply(ndn::Data& data);
168
169public:
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500170 ndn::util::DummyClientFace m_face;
Nick Gordond5c1a372016-10-31 13:56:23 -0500171};
172
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600173class DummyConfFileProcessor
174{
175 typedef std::function<void(ConfParameter&)> AfterConfProcessing;
176
177public:
178 DummyConfFileProcessor(ConfParameter& conf,
179 int32_t protocol = SYNC_PROTOCOL_PSYNC,
180 int32_t hyperbolicState = HYPERBOLIC_STATE_OFF)
181 {
182 conf.setNetwork("/ndn");
183 conf.setSiteName("/site");
184 conf.setRouterName("/%C1.Router/this-router");
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500185 conf.buildRouterAndSyncUserPrefix();
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600186
187 conf.setSyncProtocol(protocol);
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500188 conf.setHyperbolicState(hyperbolicState);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600189 }
190};
191
Vince Lehman7c603292014-09-11 17:48:16 -0500192} // namespace test
193} // namespace nlsr
194
Vince Lehman942eb7b2014-10-02 10:09:27 -0500195#endif // NLSR_TEST_COMMON_HPP