blob: c0052e29eec6a72806b34e080cfb88e674976e0f [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
78 * \param nonce if non-zero, set Nonce to this value
79 * (useful for creating Nack with same Nonce)
80 */
81shared_ptr<Interest>
82makeInterest(const Name& name, uint32_t nonce = 0);
Junxiao Shi2713a3b2015-06-22 16:19:05 -070083
Junxiao Shi7809e302016-07-23 14:11:25 +000084/** \brief create a Data with fake signature
85 * \note Data may be modified afterwards without losing the fake signature.
86 * If a real signature is desired, sign again with KeyChain.
87 */
88shared_ptr<Data>
89makeData(const Name& name);
90
91/** \brief add a fake signature to Data
92 */
93Data&
94signData(Data& data);
95
96/** \brief add a fake signature to Data
97 */
Junxiao Shi2713a3b2015-06-22 16:19:05 -070098inline shared_ptr<Data>
Junxiao Shi7809e302016-07-23 14:11:25 +000099signData(shared_ptr<Data> data)
Junxiao Shi2713a3b2015-06-22 16:19:05 -0700100{
Junxiao Shi7809e302016-07-23 14:11:25 +0000101 signData(*data);
Junxiao Shi2713a3b2015-06-22 16:19:05 -0700102 return data;
103}
104
Junxiao Shi7809e302016-07-23 14:11:25 +0000105/** \brief create a Nack
Zhuo Lib3558892016-08-12 15:51:12 -0700106 * \param interest Interest
107 * \param reason Nack reason
108 */
109lp::Nack
110makeNack(const Interest& interest, lp::NackReason reason);
111
112/** \brief create a Nack
Junxiao Shi7809e302016-07-23 14:11:25 +0000113 * \param name Interest name
114 * \param nonce Interest nonce
115 * \param reason Nack reason
116 */
117lp::Nack
118makeNack(const Name& name, uint32_t nonce, lp::NackReason reason);
Junxiao Shi2713a3b2015-06-22 16:19:05 -0700119
120} // namespace tests
121} // namespace ndn
122
123#endif // NDN_TOOLS_TESTS_TEST_COMMON_HPP