blob: c01484c5fdaec59fde3bcfa323ad2f42206a732c [file] [log] [blame]
Junxiao Shid9ee45c2014-02-27 15:38:11 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shifc2e13d2017-07-25 02:08:48 +00002/*
Davide Pesaventoa3148082018-04-12 18:21:54 -04003 * Copyright (c) 2014-2018, Regents of the University of California,
Yukai Tu0a49d342015-09-13 12:54:22 +08004 * Arizona Board of Regents,
5 * Colorado State University,
6 * University Pierre & Marie Curie, Sorbonne University,
7 * Washington University in St. Louis,
8 * Beijing Institute of Technology,
9 * The University of Memphis.
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
11 * This file is part of NFD (Named Data Networking Forwarding Daemon).
12 * See AUTHORS.md for complete list of NFD authors and contributors.
13 *
14 * NFD is free software: you can redistribute it and/or modify it under the terms
15 * of the GNU General Public License as published by the Free Software Foundation,
16 * either version 3 of the License, or (at your option) any later version.
17 *
18 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 * PURPOSE. See the GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
Alexander Afanasyevc026d252014-06-16 11:14:15 -070024 */
Junxiao Shid9ee45c2014-02-27 15:38:11 -070025
Alexander Afanasyev613e2a92014-04-15 13:36:58 -070026#ifndef NFD_TESTS_TEST_COMMON_HPP
27#define NFD_TESTS_TEST_COMMON_HPP
Junxiao Shid9ee45c2014-02-27 15:38:11 -070028
Alexander Afanasyev8552a382014-05-15 20:13:42 -070029#include "boost-test.hpp"
30
Junxiao Shid9ee45c2014-02-27 15:38:11 -070031#include "core/global-io.hpp"
Alexander Afanasyev8552a382014-05-15 20:13:42 -070032
Junxiao Shid47cd632018-09-11 03:10:00 +000033#include <ndn-cxx/prefix-announcement.hpp>
Junxiao Shi455581d2014-11-17 18:38:40 -070034#include <ndn-cxx/util/time-unit-test-clock.hpp>
Junxiao Shid9ee45c2014-02-27 15:38:11 -070035
Weiwei Liuf5aee942016-03-19 07:00:42 +000036#ifdef HAVE_PRIVILEGE_DROP_AND_ELEVATE
37#include <unistd.h>
38#define SKIP_IF_NOT_SUPERUSER() \
39 do { \
40 if (::geteuid() != 0) { \
Davide Pesavento231ddd72016-09-02 22:20:00 +000041 BOOST_WARN_MESSAGE(false, "skipping assertions that require superuser privileges"); \
Weiwei Liuf5aee942016-03-19 07:00:42 +000042 return; \
43 } \
44 } while (false)
45#else
46#define SKIP_IF_NOT_SUPERUSER()
47#endif // HAVE_PRIVILEGE_DROP_AND_ELEVATE
48
Junxiao Shid9ee45c2014-02-27 15:38:11 -070049namespace nfd {
50namespace tests {
51
52/** \brief base test fixture
53 *
54 * Every test case should be based on this fixture,
55 * to have per test case io_service initialization.
56 */
57class BaseFixture
58{
59protected:
Junxiao Shid23de8b2016-07-23 20:05:42 +000060 BaseFixture();
Junxiao Shif3c07812014-03-11 21:48:49 -070061
Teng Liang952d6fd2018-05-29 21:09:52 -070062 virtual
Junxiao Shid23de8b2016-07-23 20:05:42 +000063 ~BaseFixture();
Junxiao Shid9ee45c2014-02-27 15:38:11 -070064
65protected:
Junxiao Shid23de8b2016-07-23 20:05:42 +000066 /** \brief reference to global io_service
67 */
Junxiao Shid9ee45c2014-02-27 15:38:11 -070068 boost::asio::io_service& g_io;
69};
70
Junxiao Shi455581d2014-11-17 18:38:40 -070071/** \brief a base test fixture that overrides steady clock and system clock
72 */
Yukai Tu0a49d342015-09-13 12:54:22 +080073class UnitTestTimeFixture : public virtual BaseFixture
Junxiao Shi455581d2014-11-17 18:38:40 -070074{
Junxiao Shie368d992014-12-02 23:44:31 -070075protected:
Junxiao Shid23de8b2016-07-23 20:05:42 +000076 UnitTestTimeFixture();
Junxiao Shi455581d2014-11-17 18:38:40 -070077
Teng Liang952d6fd2018-05-29 21:09:52 -070078 virtual
Junxiao Shid23de8b2016-07-23 20:05:42 +000079 ~UnitTestTimeFixture();
Junxiao Shi455581d2014-11-17 18:38:40 -070080
81 /** \brief advance steady and system clocks
82 *
83 * Clocks are advanced in increments of \p tick for \p nTicks ticks.
84 * After each tick, global io_service is polled to process pending I/O events.
85 *
86 * Exceptions thrown during I/O events are propagated to the caller.
87 * Clock advancing would stop in case of an exception.
88 */
Teng Liang952d6fd2018-05-29 21:09:52 -070089 virtual void
90 advanceClocks(time::nanoseconds tick, size_t nTicks = 1);
Junxiao Shie368d992014-12-02 23:44:31 -070091
92 /** \brief advance steady and system clocks
93 *
94 * Clocks are advanced in increments of \p tick for \p total time.
95 * The last increment might be shorter than \p tick.
96 * After each tick, global io_service is polled to process pending I/O events.
97 *
98 * Exceptions thrown during I/O events are propagated to the caller.
99 * Clock advancing would stop in case of an exception.
100 */
Teng Liang952d6fd2018-05-29 21:09:52 -0700101 virtual void
102 advanceClocks(time::nanoseconds tick, time::nanoseconds total);
Junxiao Shi3cb4fc62014-12-25 22:17:39 -0700103
Junxiao Shi455581d2014-11-17 18:38:40 -0700104protected:
105 shared_ptr<time::UnitTestSteadyClock> steadyClock;
106 shared_ptr<time::UnitTestSystemClock> systemClock;
Junxiao Shid23de8b2016-07-23 20:05:42 +0000107
108 friend class LimitedIo;
Junxiao Shi455581d2014-11-17 18:38:40 -0700109};
Junxiao Shif3c07812014-03-11 21:48:49 -0700110
Junxiao Shid23de8b2016-07-23 20:05:42 +0000111/** \brief create an Interest
112 * \param name Interest name
113 * \param nonce if non-zero, set Nonce to this value
114 * (useful for creating Nack with same Nonce)
115 */
116shared_ptr<Interest>
117makeInterest(const Name& name, uint32_t nonce = 0);
Junxiao Shif3c07812014-03-11 21:48:49 -0700118
Junxiao Shid23de8b2016-07-23 20:05:42 +0000119/** \brief create a Data with fake signature
120 * \note Data may be modified afterwards without losing the fake signature.
121 * If a real signature is desired, sign again with KeyChain.
122 */
123shared_ptr<Data>
124makeData(const Name& name);
125
126/** \brief add a fake signature to Data
127 */
128Data&
129signData(Data& data);
130
131/** \brief add a fake signature to Data
132 */
Junxiao Shif3c07812014-03-11 21:48:49 -0700133inline shared_ptr<Data>
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700134signData(shared_ptr<Data> data)
Junxiao Shif3c07812014-03-11 21:48:49 -0700135{
Junxiao Shid23de8b2016-07-23 20:05:42 +0000136 signData(*data);
Junxiao Shif3c07812014-03-11 21:48:49 -0700137 return data;
138}
139
Junxiao Shid23de8b2016-07-23 20:05:42 +0000140/** \brief create a Nack
Junxiao Shi99540072017-01-27 19:57:33 +0000141 * \param interest Interest
142 * \param reason Nack reason
143 */
144lp::Nack
145makeNack(Interest interest, lp::NackReason reason);
146
147/** \brief create a Nack
Junxiao Shid23de8b2016-07-23 20:05:42 +0000148 * \param name Interest name
149 * \param nonce Interest nonce
150 * \param reason Nack reason
151 */
152lp::Nack
153makeNack(const Name& name, uint32_t nonce, lp::NackReason reason);
Eric Newberrya98bf932015-09-21 00:58:47 -0700154
Junxiao Shid7631272016-08-17 04:16:31 +0000155/** \brief replace a name component
156 * \param[inout] name name
157 * \param index name component index
158 * \param a arguments to name::Component constructor
159 */
160template<typename...A>
161void
162setNameComponent(Name& name, ssize_t index, const A& ...a)
163{
164 Name name2 = name.getPrefix(index);
165 name2.append(name::Component(a...));
166 name2.append(name.getSubName(name2.size()));
167 name = name2;
168}
169
170template<typename Packet, typename...A>
171void
172setNameComponent(Packet& packet, ssize_t index, const A& ...a)
173{
174 Name name = packet.getName();
175 setNameComponent(name, index, a...);
176 packet.setName(name);
177}
178
Junxiao Shid47cd632018-09-11 03:10:00 +0000179/** \brief create a prefix announcement without signing
180 */
181ndn::PrefixAnnouncement
182makePrefixAnn(const Name& announcedName, time::milliseconds expiration,
183 optional<ndn::security::ValidityPeriod> validity = nullopt);
184
185/** \brief create a prefix announcement without signing
186 * \param announcedName announced name
187 * \param expiration expiration period
188 * \param validityFromNow validity period, relative from now
189 */
190ndn::PrefixAnnouncement
191makePrefixAnn(const Name& announcedName, time::milliseconds expiration,
192 std::pair<time::seconds, time::seconds> validityFromNow);
193
194/** \brief sign a prefix announcement
195 */
196ndn::PrefixAnnouncement
197signPrefixAnn(ndn::PrefixAnnouncement&& pa, ndn::KeyChain& keyChain,
198 const ndn::security::SigningInfo& si = ndn::KeyChain::getDefaultSigningInfo(),
199 optional<uint64_t> version = nullopt);
200
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700201} // namespace tests
202} // namespace nfd
203
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700204#endif // NFD_TESTS_TEST_COMMON_HPP