blob: 0f4a77b922b939effa5a0477cd6516e724334e64 [file] [log] [blame]
Junxiao Shid23de8b2016-07-23 20:05:42 +00001/* -*- 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 is part of NFD (Named Data Networking Forwarding Daemon).
12 * See AUTHORS.md for complete list of NFD authors and contributors.
13 *
14 * NFD 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 * NFD 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 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
24 */
25
26#include "test-common.hpp"
27#include <ndn-cxx/security/signature-sha256-with-rsa.hpp>
28
29namespace nfd {
30namespace tests {
31
32BaseFixture::BaseFixture()
33 : g_io(getGlobalIoService())
34{
35}
36
37BaseFixture::~BaseFixture()
38{
39 resetGlobalIoService();
40}
41
42UnitTestTimeFixture::UnitTestTimeFixture()
43 : steadyClock(make_shared<time::UnitTestSteadyClock>())
44 , systemClock(make_shared<time::UnitTestSystemClock>())
45{
46 time::setCustomClocks(steadyClock, systemClock);
47}
48
49UnitTestTimeFixture::~UnitTestTimeFixture()
50{
51 time::setCustomClocks(nullptr, nullptr);
52}
53
54void
55UnitTestTimeFixture::advanceClocks(const time::nanoseconds& tick, size_t nTicks)
56{
57 this->advanceClocks(tick, tick * nTicks);
58}
59
60void
61UnitTestTimeFixture::advanceClocks(const time::nanoseconds& tick, const time::nanoseconds& total)
62{
63 BOOST_ASSERT(tick > time::nanoseconds::zero());
64 BOOST_ASSERT(total >= time::nanoseconds::zero());
65
66 time::nanoseconds remaining = total;
67 while (remaining > time::nanoseconds::zero()) {
68 if (remaining >= tick) {
69 steadyClock->advance(tick);
70 systemClock->advance(tick);
71 remaining -= tick;
72 }
73 else {
74 steadyClock->advance(remaining);
75 systemClock->advance(remaining);
76 remaining = time::nanoseconds::zero();
77 }
78
79 if (g_io.stopped())
80 g_io.reset();
81 g_io.poll();
82 }
83}
84
85shared_ptr<Interest>
86makeInterest(const Name& name, uint32_t nonce)
87{
88 auto interest = make_shared<Interest>(name);
89 if (nonce != 0) {
90 interest->setNonce(nonce);
91 }
92 return interest;
93}
94
95shared_ptr<Data>
96makeData(const Name& name)
97{
98 auto data = make_shared<Data>(name);
99 return signData(data);
100}
101
102Data&
103signData(Data& data)
104{
105 ndn::SignatureSha256WithRsa fakeSignature;
106 fakeSignature.setValue(ndn::encoding::makeEmptyBlock(tlv::SignatureValue));
107 data.setSignature(fakeSignature);
108 data.wireEncode();
109
110 return data;
111}
112
113shared_ptr<Link>
114makeLink(const Name& name, std::initializer_list<std::pair<uint32_t, Name>> delegations)
115{
116 auto link = make_shared<Link>(name, delegations);
117 signData(link);
118 return link;
119}
120
121lp::Nack
122makeNack(const Name& name, uint32_t nonce, lp::NackReason reason)
123{
124 Interest interest(name);
125 interest.setNonce(nonce);
126 lp::Nack nack(std::move(interest));
127 nack.setReason(reason);
128 return nack;
129}
130
131} // namespace tests
132} // namespace nfd