blob: ef9c2de22c0baf5884483461ad78af66851a9ae6 [file] [log] [blame]
Vince Lehman7c603292014-09-11 17:48:16 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Nick Gordonc6a85222017-01-03 16:54:34 -06003 * Copyright (c) 2014-2017, The University of Memphis,
Nick Gordonf8b5bcd2016-08-11 15:06:50 -05004 * Regents of the University of California
Vince Lehman7c603292014-09-11 17:48:16 -05005 *
6 * This file is part of NLSR (Named-data Link State Routing).
7 * See AUTHORS.md for complete list of NLSR authors and contributors.
8 *
9 * NLSR is free software: you can redistribute it and/or modify it under the terms
10 * of the GNU General Public License as published by the Free Software Foundation,
11 * either version 3 of the License, or (at your option) any later version.
12 *
13 * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
14 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
19 *
Vince Lehman7c603292014-09-11 17:48:16 -050020 **/
21
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
95 /** \brief send one WireEncodable in reply to StatusDataset request
96 * \param prefix dataset prefix without version and segment
97 * \param payload payload block
98 * \note payload must fit in one Data
99 * \pre Interest for dataset has been expressed, sendDataset has not been invoked
100 */
101 template<typename T>
102 void
103 sendDataset(const ndn::Name& prefix, const T& payload)
104 {
105 BOOST_CONCEPT_ASSERT((ndn::WireEncodable<T>));
106
107 this->sendDatasetReply(prefix, payload.wireEncode());
108 }
109
110 /** \brief send two WireEncodables in reply to StatusDataset request
111 * \param prefix dataset prefix without version and segment
112 * \param payload1 first vector item
113 * \param payload2 second vector item
114 * \note all payloads must fit in one Data
115 * \pre Interest for dataset has been expressed, sendDataset has not been invoked
116 */
117 template<typename T1, typename T2>
118 void
119 sendDataset(const ndn::Name& prefix, const T1& payload1, const T2& payload2)
120 {
121 BOOST_CONCEPT_ASSERT((ndn::WireEncodable<T1>));
122 BOOST_CONCEPT_ASSERT((ndn::WireEncodable<T2>));
123
124 ndn::encoding::EncodingBuffer buffer;
125 payload2.wireEncode(buffer);
126 payload1.wireEncode(buffer);
127
128 this->sendDatasetReply(prefix, buffer.buf(), buffer.size());
129 }
130
131 /** \brief send a payload in reply to StatusDataset request
132 * \param name dataset prefix without version and segment
133 * \param contentArgs passed to Data::setContent
134 */
135 template<typename ...ContentArgs>
136 void
137 sendDatasetReply(ndn::Name name, ContentArgs&&... contentArgs)
138 {
139 name.appendVersion().appendSegment(0);
140
141 // These warnings assist in debugging when nfdc does not receive StatusDataset.
142 // They usually indicate a misspelled prefix or incorrect timing in the test case.
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500143 if (m_face.sentInterests.empty()) {
Nick Gordond5c1a372016-10-31 13:56:23 -0500144 BOOST_WARN_MESSAGE(false, "no Interest expressed");
145 }
146 else {
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500147 BOOST_WARN_MESSAGE(m_face.sentInterests.back().getName().isPrefixOf(name),
148 "last Interest " << m_face.sentInterests.back().getName() <<
Nick Gordond5c1a372016-10-31 13:56:23 -0500149 " cannot be satisfied by this Data " << name);
150 }
151
152 auto data = make_shared<ndn::Data>(name);
153 data->setFinalBlockId(name[-1]);
154 data->setContent(std::forward<ContentArgs>(contentArgs)...);
155 this->signDatasetReply(*data);
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500156 m_face.receive(*data);
Nick Gordond5c1a372016-10-31 13:56:23 -0500157 }
158
159 virtual void
160 signDatasetReply(ndn::Data& data);
161
162public:
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500163 ndn::util::DummyClientFace m_face;
Nick Gordond5c1a372016-10-31 13:56:23 -0500164};
165
Vince Lehman7c603292014-09-11 17:48:16 -0500166} // namespace test
167} // namespace nlsr
168
Vince Lehman942eb7b2014-10-02 10:09:27 -0500169#endif // NLSR_TEST_COMMON_HPP