blob: 71c1d087878fd4f0bbfdc4eb35bdb14d5a6684f3 [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
Vince Lehman7c603292014-09-11 17:48:16 -050032#include <ndn-cxx/util/scheduler.hpp>
Laqin Fana4cf4022017-01-03 18:57:35 +000033#include <ndn-cxx/security/key-chain.hpp>
Vince Lehman02e32992015-03-11 12:31:20 -050034#include <ndn-cxx/util/time-unit-test-clock.hpp>
Vince Lehman7c603292014-09-11 17:48:16 -050035
Nick Gordond5c1a372016-10-31 13:56:23 -050036#include <ndn-cxx/security/signature-sha256-with-rsa.hpp>
37#include <ndn-cxx/face.hpp>
38#include <ndn-cxx/util/dummy-client-face.hpp>
39
Vince Lehman7c603292014-09-11 17:48:16 -050040namespace nlsr {
41namespace test {
42
Nick Gordond5c1a372016-10-31 13:56:23 -050043ndn::Data&
44signData(ndn::Data& data);
45
46/** \brief add a fake signature to Data
47 */
48inline shared_ptr<ndn::Data>
49signData(shared_ptr<ndn::Data> data)
50{
51 signData(*data);
52 return data;
53}
54
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050055class BaseFixture : public tests::IdentityManagementFixture
Vince Lehman7c603292014-09-11 17:48:16 -050056{
57public:
58 BaseFixture()
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050059 : m_scheduler(m_ioService)
Vince Lehman7c603292014-09-11 17:48:16 -050060 {
61 }
62
63protected:
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050064 boost::asio::io_service m_ioService;
65 ndn::Scheduler m_scheduler;
Vince Lehman7c603292014-09-11 17:48:16 -050066};
67
Vince Lehman02e32992015-03-11 12:31:20 -050068class UnitTestTimeFixture : public BaseFixture
69{
70protected:
71 UnitTestTimeFixture()
dmcoomes9f936662017-03-02 10:33:09 -060072 : steadyClock(std::make_shared<ndn::time::UnitTestSteadyClock>())
73 , systemClock(std::make_shared<ndn::time::UnitTestSystemClock>())
Vince Lehman02e32992015-03-11 12:31:20 -050074 {
75 ndn::time::setCustomClocks(steadyClock, systemClock);
76 }
77
78 ~UnitTestTimeFixture()
79 {
80 ndn::time::setCustomClocks(nullptr, nullptr);
81 }
82
83 void
Nick Gordond5c1a372016-10-31 13:56:23 -050084 advanceClocks(const ndn::time::nanoseconds& tick, size_t nTicks = 1);
Vince Lehman02e32992015-03-11 12:31:20 -050085
86protected:
dmcoomes9f936662017-03-02 10:33:09 -060087 std::shared_ptr<ndn::time::UnitTestSteadyClock> steadyClock;
88 std::shared_ptr<ndn::time::UnitTestSystemClock> systemClock;
Vince Lehman02e32992015-03-11 12:31:20 -050089};
90
Nick Gordond5c1a372016-10-31 13:56:23 -050091class MockNfdMgmtFixture : public UnitTestTimeFixture
92{
93public:
94 MockNfdMgmtFixture();
95
Alexander Afanasyev67758b12018-03-06 18:36:44 -050096 virtual
97 ~MockNfdMgmtFixture() = default;
98
Nick Gordond5c1a372016-10-31 13:56:23 -050099 /** \brief send one WireEncodable in reply to StatusDataset request
100 * \param prefix dataset prefix without version and segment
101 * \param payload payload block
102 * \note payload must fit in one Data
103 * \pre Interest for dataset has been expressed, sendDataset has not been invoked
104 */
105 template<typename T>
106 void
107 sendDataset(const ndn::Name& prefix, const T& payload)
108 {
109 BOOST_CONCEPT_ASSERT((ndn::WireEncodable<T>));
110
111 this->sendDatasetReply(prefix, payload.wireEncode());
112 }
113
114 /** \brief send two WireEncodables in reply to StatusDataset request
115 * \param prefix dataset prefix without version and segment
116 * \param payload1 first vector item
117 * \param payload2 second vector item
118 * \note all payloads must fit in one Data
119 * \pre Interest for dataset has been expressed, sendDataset has not been invoked
120 */
121 template<typename T1, typename T2>
122 void
123 sendDataset(const ndn::Name& prefix, const T1& payload1, const T2& payload2)
124 {
125 BOOST_CONCEPT_ASSERT((ndn::WireEncodable<T1>));
126 BOOST_CONCEPT_ASSERT((ndn::WireEncodable<T2>));
127
128 ndn::encoding::EncodingBuffer buffer;
129 payload2.wireEncode(buffer);
130 payload1.wireEncode(buffer);
131
132 this->sendDatasetReply(prefix, buffer.buf(), buffer.size());
133 }
134
135 /** \brief send a payload in reply to StatusDataset request
136 * \param name dataset prefix without version and segment
137 * \param contentArgs passed to Data::setContent
138 */
139 template<typename ...ContentArgs>
140 void
141 sendDatasetReply(ndn::Name name, ContentArgs&&... contentArgs)
142 {
143 name.appendVersion().appendSegment(0);
144
145 // These warnings assist in debugging when nfdc does not receive StatusDataset.
146 // They usually indicate a misspelled prefix or incorrect timing in the test case.
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500147 if (m_face.sentInterests.empty()) {
Nick Gordond5c1a372016-10-31 13:56:23 -0500148 BOOST_WARN_MESSAGE(false, "no Interest expressed");
149 }
150 else {
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500151 BOOST_WARN_MESSAGE(m_face.sentInterests.back().getName().isPrefixOf(name),
152 "last Interest " << m_face.sentInterests.back().getName() <<
Nick Gordond5c1a372016-10-31 13:56:23 -0500153 " cannot be satisfied by this Data " << name);
154 }
155
156 auto data = make_shared<ndn::Data>(name);
Ashlesh Gawanded65786a2018-03-30 01:17:58 -0500157 data->setFinalBlock(name[-1]);
Nick Gordond5c1a372016-10-31 13:56:23 -0500158 data->setContent(std::forward<ContentArgs>(contentArgs)...);
159 this->signDatasetReply(*data);
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500160 m_face.receive(*data);
Nick Gordond5c1a372016-10-31 13:56:23 -0500161 }
162
163 virtual void
164 signDatasetReply(ndn::Data& data);
165
166public:
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500167 ndn::util::DummyClientFace m_face;
Nick Gordond5c1a372016-10-31 13:56:23 -0500168};
169
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600170class DummyConfFileProcessor
171{
172 typedef std::function<void(ConfParameter&)> AfterConfProcessing;
173
174public:
175 DummyConfFileProcessor(ConfParameter& conf,
176 int32_t protocol = SYNC_PROTOCOL_PSYNC,
177 int32_t hyperbolicState = HYPERBOLIC_STATE_OFF)
178 {
179 conf.setNetwork("/ndn");
180 conf.setSiteName("/site");
181 conf.setRouterName("/%C1.Router/this-router");
182 conf.buildRouterPrefix();
183
184 conf.setSyncProtocol(protocol);
185 conf.setHyperbolicState(HYPERBOLIC_STATE_OFF);
186 }
187};
188
Vince Lehman7c603292014-09-11 17:48:16 -0500189} // namespace test
190} // namespace nlsr
191
Vince Lehman942eb7b2014-10-02 10:09:27 -0500192#endif // NLSR_TEST_COMMON_HPP