blob: be0b63d661b936a2324d8a8f12aae66733438bcd [file] [log] [blame]
Junxiao Shi2713a3b2015-06-22 16:19:05 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Davide Pesaventobf28cd72017-08-13 17:22:47 -04003 * Copyright (c) 2014-2017, 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"
30
Junxiao Shi2713a3b2015-06-22 16:19:05 -070031#include <ndn-cxx/data.hpp>
Davide Pesaventobf28cd72017-08-13 17:22:47 -040032#include <ndn-cxx/interest.hpp>
Junxiao Shi7809e302016-07-23 14:11:25 +000033#include <ndn-cxx/lp/nack.hpp>
34#include <ndn-cxx/util/time-unit-test-clock.hpp>
Junxiao Shi2713a3b2015-06-22 16:19:05 -070035
36#include <boost/asio/io_service.hpp>
37
38namespace ndn {
39namespace tests {
40
41/** \brief a test fixture that overrides steady clock and system clock
42 */
43class UnitTestTimeFixture
44{
45protected:
Junxiao Shi7809e302016-07-23 14:11:25 +000046 UnitTestTimeFixture();
Junxiao Shi2713a3b2015-06-22 16:19:05 -070047
Junxiao Shi7809e302016-07-23 14:11:25 +000048 ~UnitTestTimeFixture();
Junxiao Shi2713a3b2015-06-22 16:19:05 -070049
50 /** \brief advance steady and system clocks
51 *
52 * Clocks are advanced in increments of \p tick for \p nTicks ticks.
53 * After each tick, the supplied io_service is polled to process pending I/O events.
54 *
55 * Exceptions thrown during I/O events are propagated to the caller.
56 * Clock advancing would stop in case of an exception.
57 */
58 void
59 advanceClocks(boost::asio::io_service& io,
Junxiao Shi7809e302016-07-23 14:11:25 +000060 time::nanoseconds tick, size_t nTicks = 1);
Junxiao Shi2713a3b2015-06-22 16:19:05 -070061
62 /** \brief advance steady and system clocks
63 *
64 * Clocks are advanced in increments of \p tick for \p total time.
65 * The last increment might be shorter than \p tick.
66 * After each tick, the supplied io_service is polled to process pending I/O events.
67 *
68 * Exceptions thrown during I/O events are propagated to the caller.
69 * Clock advancing would stop in case of an exception.
70 */
71 void
72 advanceClocks(boost::asio::io_service& io,
Junxiao Shi7809e302016-07-23 14:11:25 +000073 time::nanoseconds tick, time::nanoseconds total);
Junxiao Shi2713a3b2015-06-22 16:19:05 -070074
75protected:
76 shared_ptr<time::UnitTestSteadyClock> steadyClock;
77 shared_ptr<time::UnitTestSystemClock> systemClock;
78};
79
Junxiao Shi7809e302016-07-23 14:11:25 +000080/** \brief create an Interest
81 * \param name Interest name
82 * \param nonce if non-zero, set Nonce to this value
83 * (useful for creating Nack with same Nonce)
84 */
85shared_ptr<Interest>
86makeInterest(const Name& name, uint32_t nonce = 0);
Junxiao Shi2713a3b2015-06-22 16:19:05 -070087
Junxiao Shi7809e302016-07-23 14:11:25 +000088/** \brief create a Data with fake signature
89 * \note Data may be modified afterwards without losing the fake signature.
90 * If a real signature is desired, sign again with KeyChain.
91 */
92shared_ptr<Data>
93makeData(const Name& name);
94
95/** \brief add a fake signature to Data
96 */
97Data&
98signData(Data& data);
99
100/** \brief add a fake signature to Data
101 */
Junxiao Shi2713a3b2015-06-22 16:19:05 -0700102inline shared_ptr<Data>
Junxiao Shi7809e302016-07-23 14:11:25 +0000103signData(shared_ptr<Data> data)
Junxiao Shi2713a3b2015-06-22 16:19:05 -0700104{
Junxiao Shi7809e302016-07-23 14:11:25 +0000105 signData(*data);
Junxiao Shi2713a3b2015-06-22 16:19:05 -0700106 return data;
107}
108
Junxiao Shi7809e302016-07-23 14:11:25 +0000109/** \brief create a Nack
Zhuo Lib3558892016-08-12 15:51:12 -0700110 * \param interest Interest
111 * \param reason Nack reason
112 */
113lp::Nack
114makeNack(const Interest& interest, lp::NackReason reason);
115
116/** \brief create a Nack
Junxiao Shi7809e302016-07-23 14:11:25 +0000117 * \param name Interest name
118 * \param nonce Interest nonce
119 * \param reason Nack reason
120 */
121lp::Nack
122makeNack(const Name& name, uint32_t nonce, lp::NackReason reason);
Junxiao Shi2713a3b2015-06-22 16:19:05 -0700123
124} // namespace tests
125} // namespace ndn
126
127#endif // NDN_TOOLS_TESTS_TEST_COMMON_HPP