blob: d6c82cde9c22f035d2d87b7aa5e6093a1ba97523 [file] [log] [blame]
Junxiao Shi2713a3b2015-06-22 16:19:05 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi7809e302016-07-23 14:11:25 +00003 * Copyright (c) 2014-2016, 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/interest.hpp>
32#include <ndn-cxx/data.hpp>
Junxiao Shi7809e302016-07-23 14:11:25 +000033#include <ndn-cxx/link.hpp>
34#include <ndn-cxx/lp/nack.hpp>
35#include <ndn-cxx/util/time-unit-test-clock.hpp>
Junxiao Shi2713a3b2015-06-22 16:19:05 -070036
37#include <boost/asio/io_service.hpp>
38
39namespace ndn {
40namespace tests {
41
42/** \brief a test fixture that overrides steady clock and system clock
43 */
44class UnitTestTimeFixture
45{
46protected:
Junxiao Shi7809e302016-07-23 14:11:25 +000047 UnitTestTimeFixture();
Junxiao Shi2713a3b2015-06-22 16:19:05 -070048
Junxiao Shi7809e302016-07-23 14:11:25 +000049 ~UnitTestTimeFixture();
Junxiao Shi2713a3b2015-06-22 16:19:05 -070050
51 /** \brief advance steady and system clocks
52 *
53 * Clocks are advanced in increments of \p tick for \p nTicks ticks.
54 * After each tick, the supplied io_service is polled to process pending I/O events.
55 *
56 * Exceptions thrown during I/O events are propagated to the caller.
57 * Clock advancing would stop in case of an exception.
58 */
59 void
60 advanceClocks(boost::asio::io_service& io,
Junxiao Shi7809e302016-07-23 14:11:25 +000061 time::nanoseconds tick, size_t nTicks = 1);
Junxiao Shi2713a3b2015-06-22 16:19:05 -070062
63 /** \brief advance steady and system clocks
64 *
65 * Clocks are advanced in increments of \p tick for \p total time.
66 * The last increment might be shorter than \p tick.
67 * After each tick, the supplied io_service is polled to process pending I/O events.
68 *
69 * Exceptions thrown during I/O events are propagated to the caller.
70 * Clock advancing would stop in case of an exception.
71 */
72 void
73 advanceClocks(boost::asio::io_service& io,
Junxiao Shi7809e302016-07-23 14:11:25 +000074 time::nanoseconds tick, time::nanoseconds total);
Junxiao Shi2713a3b2015-06-22 16:19:05 -070075
76protected:
77 shared_ptr<time::UnitTestSteadyClock> steadyClock;
78 shared_ptr<time::UnitTestSystemClock> systemClock;
79};
80
Junxiao Shi7809e302016-07-23 14:11:25 +000081/** \brief create an Interest
82 * \param name Interest name
83 * \param nonce if non-zero, set Nonce to this value
84 * (useful for creating Nack with same Nonce)
85 */
86shared_ptr<Interest>
87makeInterest(const Name& name, uint32_t nonce = 0);
Junxiao Shi2713a3b2015-06-22 16:19:05 -070088
Junxiao Shi7809e302016-07-23 14:11:25 +000089/** \brief create a Data with fake signature
90 * \note Data may be modified afterwards without losing the fake signature.
91 * If a real signature is desired, sign again with KeyChain.
92 */
93shared_ptr<Data>
94makeData(const Name& name);
95
96/** \brief add a fake signature to Data
97 */
98Data&
99signData(Data& data);
100
101/** \brief add a fake signature to Data
102 */
Junxiao Shi2713a3b2015-06-22 16:19:05 -0700103inline shared_ptr<Data>
Junxiao Shi7809e302016-07-23 14:11:25 +0000104signData(shared_ptr<Data> data)
Junxiao Shi2713a3b2015-06-22 16:19:05 -0700105{
Junxiao Shi7809e302016-07-23 14:11:25 +0000106 signData(*data);
Junxiao Shi2713a3b2015-06-22 16:19:05 -0700107 return data;
108}
109
Junxiao Shi7809e302016-07-23 14:11:25 +0000110/** \brief create a Link object with fake signature
111 * \note Link may be modified afterwards without losing the fake signature.
112 * If a real signature is desired, sign again with KeyChain.
113 */
114shared_ptr<Link>
115makeLink(const Name& name, std::initializer_list<std::pair<uint32_t, Name>> delegations);
Junxiao Shi2713a3b2015-06-22 16:19:05 -0700116
Junxiao Shi7809e302016-07-23 14:11:25 +0000117/** \brief create a Nack
118 * \param name Interest name
119 * \param nonce Interest nonce
120 * \param reason Nack reason
121 */
122lp::Nack
123makeNack(const Name& name, uint32_t nonce, lp::NackReason reason);
Junxiao Shi2713a3b2015-06-22 16:19:05 -0700124
125} // namespace tests
126} // namespace ndn
127
128#endif // NDN_TOOLS_TESTS_TEST_COMMON_HPP