blob: 7cf56718aac109854fe39a1027f7e7d2d929c87d [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/*
Davide Pesavento4ab5eed2020-03-12 20:50:04 -04003 * Copyright (c) 2014-2020, 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
Junxiao Shi7809e302016-07-23 14:11:25 +000077 */
78shared_ptr<Interest>
Junxiao Shi20d5a0b2018-08-14 09:26:11 -060079makeInterest(const Name& name, bool canBePrefix = false,
Davide Pesavento4ab5eed2020-03-12 20:50:04 -040080 time::milliseconds lifetime = DEFAULT_INTEREST_LIFETIME,
81 optional<Interest::Nonce> nonce = nullopt);
Junxiao Shi2713a3b2015-06-22 16:19:05 -070082
Junxiao Shi7809e302016-07-23 14:11:25 +000083/** \brief create a Data with fake signature
84 * \note Data may be modified afterwards without losing the fake signature.
85 * If a real signature is desired, sign again with KeyChain.
86 */
87shared_ptr<Data>
88makeData(const Name& name);
89
90/** \brief add a fake signature to Data
91 */
92Data&
93signData(Data& data);
94
95/** \brief add a fake signature to Data
96 */
Junxiao Shi2713a3b2015-06-22 16:19:05 -070097inline shared_ptr<Data>
Davide Pesavento4ab5eed2020-03-12 20:50:04 -040098signData(shared_ptr<Data> data)
Junxiao Shi2713a3b2015-06-22 16:19:05 -070099{
Junxiao Shi7809e302016-07-23 14:11:25 +0000100 signData(*data);
Junxiao Shi2713a3b2015-06-22 16:19:05 -0700101 return data;
102}
103
Junxiao Shi7809e302016-07-23 14:11:25 +0000104/** \brief create a Nack
Zhuo Lib3558892016-08-12 15:51:12 -0700105 */
106lp::Nack
Davide Pesavento4ab5eed2020-03-12 20:50:04 -0400107makeNack(Interest interest, lp::NackReason reason);
Zhuo Lib3558892016-08-12 15:51:12 -0700108
Junxiao Shi2713a3b2015-06-22 16:19:05 -0700109} // namespace tests
110} // namespace ndn
111
112#endif // NDN_TOOLS_TESTS_TEST_COMMON_HPP