blob: 1a4a0d50487f2ca3d276127139fc42e85e79135d [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
Teng Liang952d6fd2018-05-29 21:09:52 -070061 virtual
Junxiao Shid23de8b2016-07-23 20:05:42 +000062 ~BaseFixture();
Junxiao Shid9ee45c2014-02-27 15:38:11 -070063
64protected:
Junxiao Shid23de8b2016-07-23 20:05:42 +000065 /** \brief reference to global io_service
66 */
Junxiao Shid9ee45c2014-02-27 15:38:11 -070067 boost::asio::io_service& g_io;
68};
69
Junxiao Shi455581d2014-11-17 18:38:40 -070070/** \brief a base test fixture that overrides steady clock and system clock
71 */
Yukai Tu0a49d342015-09-13 12:54:22 +080072class UnitTestTimeFixture : public virtual BaseFixture
Junxiao Shi455581d2014-11-17 18:38:40 -070073{
Junxiao Shie368d992014-12-02 23:44:31 -070074protected:
Junxiao Shid23de8b2016-07-23 20:05:42 +000075 UnitTestTimeFixture();
Junxiao Shi455581d2014-11-17 18:38:40 -070076
Teng Liang952d6fd2018-05-29 21:09:52 -070077 virtual
Junxiao Shid23de8b2016-07-23 20:05:42 +000078 ~UnitTestTimeFixture();
Junxiao Shi455581d2014-11-17 18:38:40 -070079
80 /** \brief advance steady and system clocks
81 *
82 * Clocks are advanced in increments of \p tick for \p nTicks ticks.
83 * After each tick, global io_service is polled to process pending I/O events.
84 *
85 * Exceptions thrown during I/O events are propagated to the caller.
86 * Clock advancing would stop in case of an exception.
87 */
Teng Liang952d6fd2018-05-29 21:09:52 -070088 virtual void
89 advanceClocks(time::nanoseconds tick, size_t nTicks = 1);
Junxiao Shie368d992014-12-02 23:44:31 -070090
91 /** \brief advance steady and system clocks
92 *
93 * Clocks are advanced in increments of \p tick for \p total time.
94 * The last increment might be shorter than \p tick.
95 * After each tick, global io_service is polled to process pending I/O events.
96 *
97 * Exceptions thrown during I/O events are propagated to the caller.
98 * Clock advancing would stop in case of an exception.
99 */
Teng Liang952d6fd2018-05-29 21:09:52 -0700100 virtual void
101 advanceClocks(time::nanoseconds tick, time::nanoseconds total);
Junxiao Shi3cb4fc62014-12-25 22:17:39 -0700102
Junxiao Shi455581d2014-11-17 18:38:40 -0700103protected:
104 shared_ptr<time::UnitTestSteadyClock> steadyClock;
105 shared_ptr<time::UnitTestSystemClock> systemClock;
Junxiao Shid23de8b2016-07-23 20:05:42 +0000106
107 friend class LimitedIo;
Junxiao Shi455581d2014-11-17 18:38:40 -0700108};
Junxiao Shif3c07812014-03-11 21:48:49 -0700109
Junxiao Shid23de8b2016-07-23 20:05:42 +0000110/** \brief create an Interest
111 * \param name Interest name
112 * \param nonce if non-zero, set Nonce to this value
113 * (useful for creating Nack with same Nonce)
114 */
115shared_ptr<Interest>
116makeInterest(const Name& name, uint32_t nonce = 0);
Junxiao Shif3c07812014-03-11 21:48:49 -0700117
Junxiao Shid23de8b2016-07-23 20:05:42 +0000118/** \brief create a Data with fake signature
119 * \note Data may be modified afterwards without losing the fake signature.
120 * If a real signature is desired, sign again with KeyChain.
121 */
122shared_ptr<Data>
123makeData(const Name& name);
124
125/** \brief add a fake signature to Data
126 */
127Data&
128signData(Data& data);
129
130/** \brief add a fake signature to Data
131 */
Junxiao Shif3c07812014-03-11 21:48:49 -0700132inline shared_ptr<Data>
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700133signData(shared_ptr<Data> data)
Junxiao Shif3c07812014-03-11 21:48:49 -0700134{
Junxiao Shid23de8b2016-07-23 20:05:42 +0000135 signData(*data);
Junxiao Shif3c07812014-03-11 21:48:49 -0700136 return data;
137}
138
Junxiao Shid23de8b2016-07-23 20:05:42 +0000139/** \brief create a Nack
Junxiao Shi99540072017-01-27 19:57:33 +0000140 * \param interest Interest
141 * \param reason Nack reason
142 */
143lp::Nack
144makeNack(Interest interest, lp::NackReason reason);
145
146/** \brief create a Nack
Junxiao Shid23de8b2016-07-23 20:05:42 +0000147 * \param name Interest name
148 * \param nonce Interest nonce
149 * \param reason Nack reason
150 */
151lp::Nack
152makeNack(const Name& name, uint32_t nonce, lp::NackReason reason);
Eric Newberrya98bf932015-09-21 00:58:47 -0700153
Junxiao Shid7631272016-08-17 04:16:31 +0000154/** \brief replace a name component
155 * \param[inout] name name
156 * \param index name component index
157 * \param a arguments to name::Component constructor
158 */
159template<typename...A>
160void
161setNameComponent(Name& name, ssize_t index, const A& ...a)
162{
163 Name name2 = name.getPrefix(index);
164 name2.append(name::Component(a...));
165 name2.append(name.getSubName(name2.size()));
166 name = name2;
167}
168
169template<typename Packet, typename...A>
170void
171setNameComponent(Packet& packet, ssize_t index, const A& ...a)
172{
173 Name name = packet.getName();
174 setNameComponent(name, index, a...);
175 packet.setName(name);
176}
177
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700178} // namespace tests
179} // namespace nfd
180
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700181#endif // NFD_TESTS_TEST_COMMON_HPP