blob: 39fe43409fa2c5cc0b2c9f36ca7f8bf9d5c03983 [file] [log] [blame]
Junxiao Shi2713a3b2015-06-22 16:19:05 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento60f8cc12018-05-10 22:05:21 -04002/*
3 * Copyright (c) 2014-2018, Regents of the University of California,
Junxiao Shi2713a3b2015-06-22 16:19:05 -07004 * 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.
10 *
11 * This file is part of ndn-tools (Named Data Networking Essential Tools).
12 * See AUTHORS.md for complete list of ndn-tools authors and contributors.
13 *
14 * ndn-tools 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 * ndn-tools 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 * ndn-tools, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
24 */
25
26#ifndef NDN_TOOLS_TESTS_TEST_COMMON_HPP
27#define NDN_TOOLS_TESTS_TEST_COMMON_HPP
28
29#include "boost-test.hpp"
Davide Pesavento60f8cc12018-05-10 22:05:21 -040030#include "core/common.hpp"
Junxiao Shi2713a3b2015-06-22 16:19:05 -070031
Junxiao Shi7809e302016-07-23 14:11:25 +000032#include <ndn-cxx/util/time-unit-test-clock.hpp>
Junxiao Shi2713a3b2015-06-22 16:19:05 -070033
Junxiao Shi2713a3b2015-06-22 16:19:05 -070034namespace ndn {
35namespace tests {
36
37/** \brief a test fixture that overrides steady clock and system clock
38 */
39class UnitTestTimeFixture
40{
41protected:
Junxiao Shi7809e302016-07-23 14:11:25 +000042 UnitTestTimeFixture();
Junxiao Shi2713a3b2015-06-22 16:19:05 -070043
Junxiao Shi7809e302016-07-23 14:11:25 +000044 ~UnitTestTimeFixture();
Junxiao Shi2713a3b2015-06-22 16:19:05 -070045
46 /** \brief advance steady and system clocks
47 *
48 * Clocks are advanced in increments of \p tick for \p nTicks ticks.
49 * After each tick, the supplied io_service is polled to process pending I/O events.
50 *
51 * Exceptions thrown during I/O events are propagated to the caller.
52 * Clock advancing would stop in case of an exception.
53 */
54 void
55 advanceClocks(boost::asio::io_service& io,
Junxiao Shi7809e302016-07-23 14:11:25 +000056 time::nanoseconds tick, size_t nTicks = 1);
Junxiao Shi2713a3b2015-06-22 16:19:05 -070057
58 /** \brief advance steady and system clocks
59 *
60 * Clocks are advanced in increments of \p tick for \p total time.
61 * The last increment might be shorter than \p tick.
62 * After each tick, the supplied io_service is polled to process pending I/O events.
63 *
64 * Exceptions thrown during I/O events are propagated to the caller.
65 * Clock advancing would stop in case of an exception.
66 */
67 void
68 advanceClocks(boost::asio::io_service& io,
Junxiao Shi7809e302016-07-23 14:11:25 +000069 time::nanoseconds tick, time::nanoseconds total);
Junxiao Shi2713a3b2015-06-22 16:19:05 -070070
71protected:
72 shared_ptr<time::UnitTestSteadyClock> steadyClock;
73 shared_ptr<time::UnitTestSystemClock> systemClock;
74};
75
Junxiao Shi7809e302016-07-23 14:11:25 +000076/** \brief create an Interest
77 * \param name Interest name
Junxiao Shi20d5a0b2018-08-14 09:26:11 -060078 * \param canBePrefix CanBePrefix setting
79 * \param lifetime InterestLifetime
80 * \param nonce if non-zero, set Nonce to this value (useful for creating Nack with same Nonce)
Junxiao Shi7809e302016-07-23 14:11:25 +000081 */
82shared_ptr<Interest>
Junxiao Shi20d5a0b2018-08-14 09:26:11 -060083makeInterest(const Name& name, bool canBePrefix = false,
84 time::milliseconds lifetime = DEFAULT_INTEREST_LIFETIME, uint32_t nonce = 0);
Junxiao Shi2713a3b2015-06-22 16:19:05 -070085
Junxiao Shi7809e302016-07-23 14:11:25 +000086/** \brief create a Data with fake signature
87 * \note Data may be modified afterwards without losing the fake signature.
88 * If a real signature is desired, sign again with KeyChain.
89 */
90shared_ptr<Data>
91makeData(const Name& name);
92
93/** \brief add a fake signature to Data
94 */
95Data&
96signData(Data& data);
97
98/** \brief add a fake signature to Data
99 */
Junxiao Shi2713a3b2015-06-22 16:19:05 -0700100inline shared_ptr<Data>
Junxiao Shi20d5a0b2018-08-14 09:26:11 -0600101signData(const shared_ptr<Data>& data)
Junxiao Shi2713a3b2015-06-22 16:19:05 -0700102{
Junxiao Shi7809e302016-07-23 14:11:25 +0000103 signData(*data);
Junxiao Shi2713a3b2015-06-22 16:19:05 -0700104 return data;
105}
106
Junxiao Shi7809e302016-07-23 14:11:25 +0000107/** \brief create a Nack
Zhuo Lib3558892016-08-12 15:51:12 -0700108 * \param interest Interest
109 * \param reason Nack reason
110 */
111lp::Nack
112makeNack(const Interest& interest, lp::NackReason reason);
113
Junxiao Shi2713a3b2015-06-22 16:19:05 -0700114} // namespace tests
115} // namespace ndn
116
117#endif // NDN_TOOLS_TESTS_TEST_COMMON_HPP