blob: 110a1e2fb582a4185556c8b1e7f3cebba9136caa [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 Shi455581d2014-11-17 18:38:40 -070033#include <ndn-cxx/util/time-unit-test-clock.hpp>
Junxiao Shid9ee45c2014-02-27 15:38:11 -070034
Weiwei Liuf5aee942016-03-19 07:00:42 +000035#ifdef HAVE_PRIVILEGE_DROP_AND_ELEVATE
36#include <unistd.h>
37#define SKIP_IF_NOT_SUPERUSER() \
38 do { \
39 if (::geteuid() != 0) { \
Davide Pesavento231ddd72016-09-02 22:20:00 +000040 BOOST_WARN_MESSAGE(false, "skipping assertions that require superuser privileges"); \
Weiwei Liuf5aee942016-03-19 07:00:42 +000041 return; \
42 } \
43 } while (false)
44#else
45#define SKIP_IF_NOT_SUPERUSER()
46#endif // HAVE_PRIVILEGE_DROP_AND_ELEVATE
47
Junxiao Shid9ee45c2014-02-27 15:38:11 -070048namespace nfd {
49namespace tests {
50
51/** \brief base test fixture
52 *
53 * Every test case should be based on this fixture,
54 * to have per test case io_service initialization.
55 */
56class BaseFixture
57{
58protected:
Junxiao Shid23de8b2016-07-23 20:05:42 +000059 BaseFixture();
Junxiao Shif3c07812014-03-11 21:48:49 -070060
Junxiao Shid23de8b2016-07-23 20:05:42 +000061 ~BaseFixture();
Junxiao Shid9ee45c2014-02-27 15:38:11 -070062
63protected:
Junxiao Shid23de8b2016-07-23 20:05:42 +000064 /** \brief reference to global io_service
65 */
Junxiao Shid9ee45c2014-02-27 15:38:11 -070066 boost::asio::io_service& g_io;
67};
68
Junxiao Shi455581d2014-11-17 18:38:40 -070069/** \brief a base test fixture that overrides steady clock and system clock
70 */
Yukai Tu0a49d342015-09-13 12:54:22 +080071class UnitTestTimeFixture : public virtual BaseFixture
Junxiao Shi455581d2014-11-17 18:38:40 -070072{
Junxiao Shie368d992014-12-02 23:44:31 -070073protected:
Junxiao Shid23de8b2016-07-23 20:05:42 +000074 UnitTestTimeFixture();
Junxiao Shi455581d2014-11-17 18:38:40 -070075
Junxiao Shid23de8b2016-07-23 20:05:42 +000076 ~UnitTestTimeFixture();
Junxiao Shi455581d2014-11-17 18:38:40 -070077
78 /** \brief advance steady and system clocks
79 *
80 * Clocks are advanced in increments of \p tick for \p nTicks ticks.
81 * After each tick, global io_service is polled to process pending I/O events.
82 *
83 * Exceptions thrown during I/O events are propagated to the caller.
84 * Clock advancing would stop in case of an exception.
85 */
86 void
Junxiao Shid23de8b2016-07-23 20:05:42 +000087 advanceClocks(const time::nanoseconds& tick, size_t nTicks = 1);
Junxiao Shie368d992014-12-02 23:44:31 -070088
89 /** \brief advance steady and system clocks
90 *
91 * Clocks are advanced in increments of \p tick for \p total time.
92 * The last increment might be shorter than \p tick.
93 * After each tick, global io_service is polled to process pending I/O events.
94 *
95 * Exceptions thrown during I/O events are propagated to the caller.
96 * Clock advancing would stop in case of an exception.
97 */
98 void
Junxiao Shid23de8b2016-07-23 20:05:42 +000099 advanceClocks(const time::nanoseconds& tick, const time::nanoseconds& total);
Junxiao Shi3cb4fc62014-12-25 22:17:39 -0700100
Junxiao Shi455581d2014-11-17 18:38:40 -0700101protected:
102 shared_ptr<time::UnitTestSteadyClock> steadyClock;
103 shared_ptr<time::UnitTestSystemClock> systemClock;
Junxiao Shid23de8b2016-07-23 20:05:42 +0000104
105 friend class LimitedIo;
Junxiao Shi455581d2014-11-17 18:38:40 -0700106};
Junxiao Shif3c07812014-03-11 21:48:49 -0700107
Junxiao Shid23de8b2016-07-23 20:05:42 +0000108/** \brief create an Interest
109 * \param name Interest name
110 * \param nonce if non-zero, set Nonce to this value
111 * (useful for creating Nack with same Nonce)
112 */
113shared_ptr<Interest>
114makeInterest(const Name& name, uint32_t nonce = 0);
Junxiao Shif3c07812014-03-11 21:48:49 -0700115
Junxiao Shid23de8b2016-07-23 20:05:42 +0000116/** \brief create a Data with fake signature
117 * \note Data may be modified afterwards without losing the fake signature.
118 * If a real signature is desired, sign again with KeyChain.
119 */
120shared_ptr<Data>
121makeData(const Name& name);
122
123/** \brief add a fake signature to Data
124 */
125Data&
126signData(Data& data);
127
128/** \brief add a fake signature to Data
129 */
Junxiao Shif3c07812014-03-11 21:48:49 -0700130inline shared_ptr<Data>
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700131signData(shared_ptr<Data> data)
Junxiao Shif3c07812014-03-11 21:48:49 -0700132{
Junxiao Shid23de8b2016-07-23 20:05:42 +0000133 signData(*data);
Junxiao Shif3c07812014-03-11 21:48:49 -0700134 return data;
135}
136
Junxiao Shid23de8b2016-07-23 20:05:42 +0000137/** \brief create a Nack
Junxiao Shi99540072017-01-27 19:57:33 +0000138 * \param interest Interest
139 * \param reason Nack reason
140 */
141lp::Nack
142makeNack(Interest interest, lp::NackReason reason);
143
144/** \brief create a Nack
Junxiao Shid23de8b2016-07-23 20:05:42 +0000145 * \param name Interest name
146 * \param nonce Interest nonce
147 * \param reason Nack reason
148 */
149lp::Nack
150makeNack(const Name& name, uint32_t nonce, lp::NackReason reason);
Eric Newberrya98bf932015-09-21 00:58:47 -0700151
Junxiao Shid7631272016-08-17 04:16:31 +0000152/** \brief replace a name component
153 * \param[inout] name name
154 * \param index name component index
155 * \param a arguments to name::Component constructor
156 */
157template<typename...A>
158void
159setNameComponent(Name& name, ssize_t index, const A& ...a)
160{
161 Name name2 = name.getPrefix(index);
162 name2.append(name::Component(a...));
163 name2.append(name.getSubName(name2.size()));
164 name = name2;
165}
166
167template<typename Packet, typename...A>
168void
169setNameComponent(Packet& packet, ssize_t index, const A& ...a)
170{
171 Name name = packet.getName();
172 setNameComponent(name, index, a...);
173 packet.setName(name);
174}
175
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700176} // namespace tests
177} // namespace nfd
178
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700179#endif // NFD_TESTS_TEST_COMMON_HPP