blob: dcba6c093cb450592fbf11f7953ce6fa9c4f4f3a [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"
26
Vince Lehman7c603292014-09-11 17:48:16 -050027#include <boost/asio.hpp>
Vince Lehman904c2412014-09-23 19:36:11 -050028#include <boost/test/unit_test.hpp>
Vince Lehman942eb7b2014-10-02 10:09:27 -050029
Vince Lehman7c603292014-09-11 17:48:16 -050030#include <ndn-cxx/util/scheduler.hpp>
Laqin Fana4cf4022017-01-03 18:57:35 +000031#include <ndn-cxx/security/key-chain.hpp>
Vince Lehman02e32992015-03-11 12:31:20 -050032#include <ndn-cxx/util/time-unit-test-clock.hpp>
Vince Lehman7c603292014-09-11 17:48:16 -050033
Nick Gordond5c1a372016-10-31 13:56:23 -050034#include <ndn-cxx/security/signature-sha256-with-rsa.hpp>
35#include <ndn-cxx/face.hpp>
36#include <ndn-cxx/util/dummy-client-face.hpp>
37
Vince Lehman7c603292014-09-11 17:48:16 -050038namespace nlsr {
39namespace test {
40
Nick Gordond5c1a372016-10-31 13:56:23 -050041ndn::Data&
42signData(ndn::Data& data);
43
44/** \brief add a fake signature to Data
45 */
46inline shared_ptr<ndn::Data>
47signData(shared_ptr<ndn::Data> data)
48{
49 signData(*data);
50 return data;
51}
52
Vince Lehman7c603292014-09-11 17:48:16 -050053class BaseFixture
54{
55public:
56 BaseFixture()
57 : g_scheduler(g_ioService)
58 {
59 }
60
61protected:
62 boost::asio::io_service g_ioService;
63 ndn::Scheduler g_scheduler;
Laqin Fana4cf4022017-01-03 18:57:35 +000064 ndn::KeyChain g_keyChain;
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.
143 if (face->sentInterests.empty()) {
144 BOOST_WARN_MESSAGE(false, "no Interest expressed");
145 }
146 else {
147 BOOST_WARN_MESSAGE(face->sentInterests.back().getName().isPrefixOf(name),
148 "last Interest " << face->sentInterests.back().getName() <<
149 " 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);
156 face->receive(*data);
157 }
158
159 virtual void
160 signDatasetReply(ndn::Data& data);
161
162public:
163 std::shared_ptr<ndn::util::DummyClientFace> face;
164};
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