blob: 6802e60c17d3f7828e2757c54c854fb33fdc3336 [file] [log] [blame]
Alexander Afanasyev8495a4a2016-12-25 15:27:25 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014-2016, 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 ChronoShare, a decentralized file sharing application over NDN.
13 *
14 * ChronoShare 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 * ChronoShare 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 * ChronoShare, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
24 *
25 * See AUTHORS.md for complete list of ChronoShare authors and contributors.
26 */
27
28#ifndef CHRONOSHARE_TESTS_TEST_COMMON_HPP
29#define CHRONOSHARE_TESTS_TEST_COMMON_HPP
30
31#include "core/logging.hpp"
32
33#include "boost-test.hpp"
34
35#include <boost/asio/io_service.hpp>
36#include <boost/filesystem.hpp>
37
38#include <ndn-cxx/name.hpp>
39#include <ndn-cxx/data.hpp>
40#include <ndn-cxx/lp/nack.hpp>
41#include <ndn-cxx/util/time-unit-test-clock.hpp>
42#include <ndn-cxx/util/string-helper.hpp>
43
44namespace ndn {
45namespace chronoshare {
46namespace tests {
47
48/** \brief base test fixture
49 *
50 * Every test case should be based on this fixture,
51 * to have per test case io_service initialization.
52 */
53class BaseFixture
54{
55protected:
56 /** \brief reference to global io_service
57 */
58 boost::asio::io_service m_io;
59};
60
61/** \brief a base test fixture that overrides steady clock and system clock
62 */
63class UnitTestTimeFixture : public virtual BaseFixture
64{
65protected:
66 UnitTestTimeFixture();
67
68 ~UnitTestTimeFixture();
69
70 /** \brief advance steady and system clocks
71 *
72 * Clocks are advanced in increments of \p tick for \p nTicks ticks.
73 * After each tick, global io_service is polled to process pending I/O events.
74 *
75 * Exceptions thrown during I/O events are propagated to the caller.
76 * Clock advancing would stop in case of an exception.
77 */
78 void
79 advanceClocks(const time::nanoseconds& tick, size_t nTicks = 1);
80
81 /** \brief advance steady and system clocks
82 *
83 * Clocks are advanced in increments of \p tick for \p total time.
84 * The last increment might be shorter than \p tick.
85 * After each tick, global io_service is polled to process pending I/O events.
86 *
87 * Exceptions thrown during I/O events are propagated to the caller.
88 * Clock advancing would stop in case of an exception.
89 */
90 void
91 advanceClocks(const time::nanoseconds& tick, const time::nanoseconds& total);
92
93protected:
94 shared_ptr<time::UnitTestSteadyClock> steadyClock;
95 shared_ptr<time::UnitTestSystemClock> systemClock;
96
97 friend class LimitedIo;
98};
99
100/** \brief create an Interest
101 * \param name Interest name
102 * \param nonce if non-zero, set Nonce to this value
103 * (useful for creating Nack with same Nonce)
104 */
105shared_ptr<Interest>
106makeInterest(const Name& name, uint32_t nonce = 0);
107
108/** \brief create a Data with fake signature
109 * \note Data may be modified afterwards without losing the fake signature.
110 * If a real signature is desired, sign again with KeyChain.
111 */
112shared_ptr<Data>
113makeData(const Name& name);
114
115/** \brief add a fake signature to Data
116 */
117Data&
118signData(Data& data);
119
120/** \brief add a fake signature to Data
121 */
122inline shared_ptr<Data>
123signData(shared_ptr<Data> data)
124{
125 signData(*data);
126 return data;
127}
128
129/** \brief create a Link object with fake signature
130 * \note Link may be modified afterwards without losing the fake signature.
131 * If a real signature is desired, sign again with KeyChain.
132 */
133shared_ptr<Link>
134makeLink(const Name& name, std::initializer_list<std::pair<uint32_t, Name>> delegations);
135
136/** \brief create a Nack
137 * \param name Interest name
138 * \param nonce Interest nonce
139 * \param reason Nack reason
140 */
141lp::Nack
142makeNack(const Name& name, uint32_t nonce, lp::NackReason reason);
143
144/** \brief replace a name component
145 * \param[inout] name name
146 * \param index name component index
147 * \param a arguments to name::Component constructor
148 */
149template<typename...A>
150void
151setNameComponent(Name& name, ssize_t index, const A& ...a)
152{
153 Name name2 = name.getPrefix(index);
154 name2.append(name::Component(a...));
155 name2.append(name.getSubName(name2.size()));
156 name = name2;
157}
158
159template<typename Packet, typename...A>
160void
161setNameComponent(Packet& packet, ssize_t index, const A& ...a)
162{
163 Name name = packet.getName();
164 setNameComponent(name, index, a...);
165 packet.setName(name);
166}
167
168} // namespace tests
169} // namespace chronoshare
170} // namespace ndn
171
172#include "identity-management-fixture.hpp"
173
174#endif // CHRONOSHARE_TESTS_TEST_COMMON_HPP