blob: 13b0b19686bc6b2e6590bc88615e85b6cefad585 [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/*
3 * Copyright (c) 2014-2018, The University of Memphis,
4 * 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"
Vince Lehman0a7da612014-10-29 14:39:29 -050027
Vince Lehman7c603292014-09-11 17:48:16 -050028#include <boost/asio.hpp>
Vince Lehman904c2412014-09-23 19:36:11 -050029#include <boost/test/unit_test.hpp>
Vince Lehman942eb7b2014-10-02 10:09:27 -050030
Vince Lehman7c603292014-09-11 17:48:16 -050031#include <ndn-cxx/util/scheduler.hpp>
Laqin Fana4cf4022017-01-03 18:57:35 +000032#include <ndn-cxx/security/key-chain.hpp>
Vince Lehman02e32992015-03-11 12:31:20 -050033#include <ndn-cxx/util/time-unit-test-clock.hpp>
Vince Lehman7c603292014-09-11 17:48:16 -050034
Nick Gordond5c1a372016-10-31 13:56:23 -050035#include <ndn-cxx/security/signature-sha256-with-rsa.hpp>
36#include <ndn-cxx/face.hpp>
37#include <ndn-cxx/util/dummy-client-face.hpp>
38
Vince Lehman7c603292014-09-11 17:48:16 -050039namespace nlsr {
40namespace test {
41
Nick Gordond5c1a372016-10-31 13:56:23 -050042ndn::Data&
43signData(ndn::Data& data);
44
45/** \brief add a fake signature to Data
46 */
47inline shared_ptr<ndn::Data>
48signData(shared_ptr<ndn::Data> data)
49{
50 signData(*data);
51 return data;
52}
53
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050054class BaseFixture : public tests::IdentityManagementFixture
Vince Lehman7c603292014-09-11 17:48:16 -050055{
56public:
57 BaseFixture()
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050058 : m_scheduler(m_ioService)
Vince Lehman7c603292014-09-11 17:48:16 -050059 {
60 }
61
62protected:
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050063 boost::asio::io_service m_ioService;
64 ndn::Scheduler m_scheduler;
Vince Lehman7c603292014-09-11 17:48:16 -050065};
66
Vince Lehman02e32992015-03-11 12:31:20 -050067class UnitTestTimeFixture : public BaseFixture
68{
69protected:
70 UnitTestTimeFixture()
dmcoomes9f936662017-03-02 10:33:09 -060071 : steadyClock(std::make_shared<ndn::time::UnitTestSteadyClock>())
72 , systemClock(std::make_shared<ndn::time::UnitTestSystemClock>())
Vince Lehman02e32992015-03-11 12:31:20 -050073 {
74 ndn::time::setCustomClocks(steadyClock, systemClock);
75 }
76
77 ~UnitTestTimeFixture()
78 {
79 ndn::time::setCustomClocks(nullptr, nullptr);
80 }
81
82 void
Nick Gordond5c1a372016-10-31 13:56:23 -050083 advanceClocks(const ndn::time::nanoseconds& tick, size_t nTicks = 1);
Vince Lehman02e32992015-03-11 12:31:20 -050084
85protected:
dmcoomes9f936662017-03-02 10:33:09 -060086 std::shared_ptr<ndn::time::UnitTestSteadyClock> steadyClock;
87 std::shared_ptr<ndn::time::UnitTestSystemClock> systemClock;
Vince Lehman02e32992015-03-11 12:31:20 -050088};
89
Nick Gordond5c1a372016-10-31 13:56:23 -050090class MockNfdMgmtFixture : public UnitTestTimeFixture
91{
92public:
93 MockNfdMgmtFixture();
94
Alexander Afanasyev67758b12018-03-06 18:36:44 -050095 virtual
96 ~MockNfdMgmtFixture() = default;
97
Nick Gordond5c1a372016-10-31 13:56:23 -050098 /** \brief send one WireEncodable in reply to StatusDataset request
99 * \param prefix dataset prefix without version and segment
100 * \param payload payload block
101 * \note payload must fit in one Data
102 * \pre Interest for dataset has been expressed, sendDataset has not been invoked
103 */
104 template<typename T>
105 void
106 sendDataset(const ndn::Name& prefix, const T& payload)
107 {
108 BOOST_CONCEPT_ASSERT((ndn::WireEncodable<T>));
109
110 this->sendDatasetReply(prefix, payload.wireEncode());
111 }
112
113 /** \brief send two WireEncodables in reply to StatusDataset request
114 * \param prefix dataset prefix without version and segment
115 * \param payload1 first vector item
116 * \param payload2 second vector item
117 * \note all payloads must fit in one Data
118 * \pre Interest for dataset has been expressed, sendDataset has not been invoked
119 */
120 template<typename T1, typename T2>
121 void
122 sendDataset(const ndn::Name& prefix, const T1& payload1, const T2& payload2)
123 {
124 BOOST_CONCEPT_ASSERT((ndn::WireEncodable<T1>));
125 BOOST_CONCEPT_ASSERT((ndn::WireEncodable<T2>));
126
127 ndn::encoding::EncodingBuffer buffer;
128 payload2.wireEncode(buffer);
129 payload1.wireEncode(buffer);
130
131 this->sendDatasetReply(prefix, buffer.buf(), buffer.size());
132 }
133
134 /** \brief send a payload in reply to StatusDataset request
135 * \param name dataset prefix without version and segment
136 * \param contentArgs passed to Data::setContent
137 */
138 template<typename ...ContentArgs>
139 void
140 sendDatasetReply(ndn::Name name, ContentArgs&&... contentArgs)
141 {
142 name.appendVersion().appendSegment(0);
143
144 // These warnings assist in debugging when nfdc does not receive StatusDataset.
145 // They usually indicate a misspelled prefix or incorrect timing in the test case.
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500146 if (m_face.sentInterests.empty()) {
Nick Gordond5c1a372016-10-31 13:56:23 -0500147 BOOST_WARN_MESSAGE(false, "no Interest expressed");
148 }
149 else {
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500150 BOOST_WARN_MESSAGE(m_face.sentInterests.back().getName().isPrefixOf(name),
151 "last Interest " << m_face.sentInterests.back().getName() <<
Nick Gordond5c1a372016-10-31 13:56:23 -0500152 " cannot be satisfied by this Data " << name);
153 }
154
155 auto data = make_shared<ndn::Data>(name);
Ashlesh Gawanded65786a2018-03-30 01:17:58 -0500156 data->setFinalBlock(name[-1]);
Nick Gordond5c1a372016-10-31 13:56:23 -0500157 data->setContent(std::forward<ContentArgs>(contentArgs)...);
158 this->signDatasetReply(*data);
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500159 m_face.receive(*data);
Nick Gordond5c1a372016-10-31 13:56:23 -0500160 }
161
162 virtual void
163 signDatasetReply(ndn::Data& data);
164
165public:
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500166 ndn::util::DummyClientFace m_face;
Nick Gordond5c1a372016-10-31 13:56:23 -0500167};
168
Vince Lehman7c603292014-09-11 17:48:16 -0500169} // namespace test
170} // namespace nlsr
171
Vince Lehman942eb7b2014-10-02 10:09:27 -0500172#endif // NLSR_TEST_COMMON_HPP