blob: 3d5dec112e392fda754b052048e29907b540bcd1 [file] [log] [blame]
Zhiyi Zhang8617a792017-01-17 16:45:56 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014-2017, Regents of the University of California,
4 * 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, originally written as part of NFD (Named Data Networking Forwarding Daemon),
12 * is a part of ndncert, a certificate management system based on NDN.
13 *
14 * ndncert 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, either
16 * version 3 of the License, or (at your option) any later version.
17 *
18 * ndncert is distributed in the hope that it will be useful, but WITHOUT ANY
19 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
20 * PARTICULAR PURPOSE. See the GNU General Public License for more details.
21 *
22 * You should have received copies of the GNU General Public License along with
23 * ndncert, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
24 *
25 * See AUTHORS.md for complete list of ndncert authors and contributors.
26 */
27
28#ifndef NDNCERT_TESTS_TEST_COMMON_HPP
29#define NDNCERT_TESTS_TEST_COMMON_HPP
30
31#include "logging.hpp"
32
33#include "boost-test.hpp"
34
35#include <boost/asio/io_service.hpp>
36#include <boost/filesystem.hpp>
37#include <boost/filesystem/fstream.hpp>
38
Zhiyi Zhangb6fab0f2017-09-21 16:26:27 -070039#include <ndn-cxx/link.hpp>
Zhiyi Zhang8617a792017-01-17 16:45:56 -080040#include <ndn-cxx/name.hpp>
41#include <ndn-cxx/data.hpp>
42#include <ndn-cxx/lp/nack.hpp>
43#include <ndn-cxx/util/time-unit-test-clock.hpp>
44#include <ndn-cxx/util/string-helper.hpp>
45
46namespace ndn {
47namespace ndncert {
48namespace tests {
49
50/** \brief base test fixture
51 *
52 * Every test case should be based on this fixture,
53 * to have per test case io_service initialization.
54 */
55class BaseFixture
56{
57protected:
58 /** \brief reference to global io_service
59 */
60 boost::asio::io_service m_io;
61};
62
63/** \brief a base test fixture that overrides steady clock and system clock
64 */
65class UnitTestTimeFixture : public virtual BaseFixture
66{
67protected:
68 UnitTestTimeFixture();
69
70 ~UnitTestTimeFixture();
71
72 /** \brief advance steady and system clocks
73 *
74 * Clocks are advanced in increments of \p tick for \p nTicks ticks.
75 * After each tick, global io_service is polled to process pending I/O events.
76 *
77 * Exceptions thrown during I/O events are propagated to the caller.
78 * Clock advancing would stop in case of an exception.
79 */
80 void
81 advanceClocks(const time::nanoseconds& tick, size_t nTicks = 1);
82
83 /** \brief advance steady and system clocks
84 *
85 * Clocks are advanced in increments of \p tick for \p total time.
86 * The last increment might be shorter than \p tick.
87 * After each tick, global io_service is polled to process pending I/O events.
88 *
89 * Exceptions thrown during I/O events are propagated to the caller.
90 * Clock advancing would stop in case of an exception.
91 */
92 void
93 advanceClocks(const time::nanoseconds& tick, const time::nanoseconds& total);
94
95protected:
96 shared_ptr<time::UnitTestSteadyClock> steadyClock;
97 shared_ptr<time::UnitTestSystemClock> systemClock;
98
99 friend class LimitedIo;
100};
101
102/** \brief create an Interest
103 * \param name Interest name
104 * \param nonce if non-zero, set Nonce to this value
105 * (useful for creating Nack with same Nonce)
106 */
107shared_ptr<Interest>
108makeInterest(const Name& name, uint32_t nonce = 0);
109
110/** \brief create a Data with fake signature
111 * \note Data may be modified afterwards without losing the fake signature.
112 * If a real signature is desired, sign again with KeyChain.
113 */
114shared_ptr<Data>
115makeData(const Name& name);
116
117/** \brief add a fake signature to Data
118 */
119Data&
120signData(Data& data);
121
122/** \brief add a fake signature to Data
123 */
124inline shared_ptr<Data>
125signData(shared_ptr<Data> data)
126{
127 signData(*data);
128 return data;
129}
130
131/** \brief create a Link object with fake signature
132 * \note Link may be modified afterwards without losing the fake signature.
133 * If a real signature is desired, sign again with KeyChain.
134 */
135shared_ptr<Link>
Zhiyi Zhangb6fab0f2017-09-21 16:26:27 -0700136makeLink(const Name& name, std::initializer_list<Delegation> delegations);
Zhiyi Zhang8617a792017-01-17 16:45:56 -0800137
138/** \brief create a Nack
139 * \param name Interest name
140 * \param nonce Interest nonce
141 * \param reason Nack reason
142 */
143lp::Nack
144makeNack(const Name& name, uint32_t nonce, lp::NackReason reason);
145
146/** \brief replace a name component
147 * \param[inout] name name
148 * \param index name component index
149 * \param a arguments to name::Component constructor
150 */
151template<typename...A>
152void
153setNameComponent(Name& name, ssize_t index, const A& ...a)
154{
155 Name name2 = name.getPrefix(index);
156 name2.append(name::Component(a...));
157 name2.append(name.getSubName(name2.size()));
158 name = name2;
159}
160
161template<typename Packet, typename...A>
162void
163setNameComponent(Packet& packet, ssize_t index, const A& ...a)
164{
165 Name name = packet.getName();
166 setNameComponent(name, index, a...);
167 packet.setName(name);
168}
169
170/** \brief convert file to digest
171 */
172ndn::ConstBufferPtr
173digestFromFile(const boost::filesystem::path& filename);
174
175} // namespace tests
176} // namespace ndncert
177} // namespace ndn
178
Zhiyi Zhang8617a792017-01-17 16:45:56 -0800179#endif // NDNCERT_TESTS_TEST_COMMON_HPP