blob: aafb70de1e61a30966442b3eadc97f7c3d64ea7c [file] [log] [blame]
Junxiao Shi2713a3b2015-06-22 16:19:05 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014-2015, 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 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
31#include <ndn-cxx/util/time-unit-test-clock.hpp>
32#include <ndn-cxx/name.hpp>
33#include <ndn-cxx/interest.hpp>
34#include <ndn-cxx/data.hpp>
35#include <ndn-cxx/signature.hpp>
36#include <ndn-cxx/security/signature-sha256-with-rsa.hpp>
37
38#include <boost/asio/io_service.hpp>
39
40namespace ndn {
41namespace tests {
42
43/** \brief a test fixture that overrides steady clock and system clock
44 */
45class UnitTestTimeFixture
46{
47protected:
48 UnitTestTimeFixture()
49 : steadyClock(make_shared<time::UnitTestSteadyClock>())
50 , systemClock(make_shared<time::UnitTestSystemClock>())
51 {
52 time::setCustomClocks(steadyClock, systemClock);
53 }
54
55 ~UnitTestTimeFixture()
56 {
57 time::setCustomClocks(nullptr, nullptr);
58 }
59
60 /** \brief advance steady and system clocks
61 *
62 * Clocks are advanced in increments of \p tick for \p nTicks ticks.
63 * After each tick, the supplied io_service is polled to process pending I/O events.
64 *
65 * Exceptions thrown during I/O events are propagated to the caller.
66 * Clock advancing would stop in case of an exception.
67 */
68 void
69 advanceClocks(boost::asio::io_service& io,
70 const time::nanoseconds& tick, size_t nTicks = 1)
71 {
Junxiao Shi2713a3b2015-06-22 16:19:05 -070072 this->advanceClocks(io, tick, tick * nTicks);
73 }
74
75 /** \brief advance steady and system clocks
76 *
77 * Clocks are advanced in increments of \p tick for \p total time.
78 * The last increment might be shorter than \p tick.
79 * After each tick, the supplied io_service is polled to process pending I/O events.
80 *
81 * Exceptions thrown during I/O events are propagated to the caller.
82 * Clock advancing would stop in case of an exception.
83 */
84 void
85 advanceClocks(boost::asio::io_service& io,
86 const time::nanoseconds& tick, const time::nanoseconds& total)
87 {
88 BOOST_ASSERT(tick > time::nanoseconds::zero());
89 BOOST_ASSERT(total >= time::nanoseconds::zero());
90
91 time::nanoseconds remaining = total;
92 while (remaining > time::nanoseconds::zero()) {
93 if (remaining >= tick) {
94 steadyClock->advance(tick);
95 systemClock->advance(tick);
96 remaining -= tick;
97 }
98 else {
99 steadyClock->advance(remaining);
100 systemClock->advance(remaining);
101 remaining = time::nanoseconds::zero();
102 }
103
104 if (io.stopped())
105 io.reset();
106 io.poll();
107 }
108 }
109
110protected:
111 shared_ptr<time::UnitTestSteadyClock> steadyClock;
112 shared_ptr<time::UnitTestSystemClock> systemClock;
113};
114
115inline shared_ptr<Interest>
116makeInterest(const Name& name)
117{
118 return make_shared<Interest>(name);
119}
120
121inline shared_ptr<Data>
122signData(const shared_ptr<Data>& data)
123{
124 ndn::SignatureSha256WithRsa fakeSignature;
125 fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue,
126 static_cast<const uint8_t*>(nullptr), 0));
127 data->setSignature(fakeSignature);
128 data->wireEncode();
129
130 return data;
131}
132
133inline shared_ptr<Data>
134makeData(const Name& name)
135{
136 shared_ptr<Data> data = make_shared<Data>(name);
137 return signData(data);
138}
139
140
141} // namespace tests
142} // namespace ndn
143
144#endif // NDN_TOOLS_TESTS_TEST_COMMON_HPP