blob: b9a9464d59a22b5c6717cf14ba3455ab25cf5d35 [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#include "test-common.hpp"
29#include <ndn-cxx/security/signature-sha256-with-rsa.hpp>
30
31namespace ndn {
32namespace chronoshare {
33namespace tests {
34
35UnitTestTimeFixture::UnitTestTimeFixture()
36 : steadyClock(make_shared<time::UnitTestSteadyClock>())
37 , systemClock(make_shared<time::UnitTestSystemClock>())
38{
39 time::setCustomClocks(steadyClock, systemClock);
40}
41
42UnitTestTimeFixture::~UnitTestTimeFixture()
43{
44 time::setCustomClocks(nullptr, nullptr);
45}
46
47void
48UnitTestTimeFixture::advanceClocks(const time::nanoseconds& tick, size_t nTicks)
49{
50 this->advanceClocks(tick, tick * nTicks);
51}
52
53void
54UnitTestTimeFixture::advanceClocks(const time::nanoseconds& tick, const time::nanoseconds& total)
55{
56 BOOST_ASSERT(tick > time::nanoseconds::zero());
57 BOOST_ASSERT(total >= time::nanoseconds::zero());
58
59 time::nanoseconds remaining = total;
60 while (remaining > time::nanoseconds::zero()) {
61 if (remaining >= tick) {
62 steadyClock->advance(tick);
63 systemClock->advance(tick);
64 remaining -= tick;
65 }
66 else {
67 steadyClock->advance(remaining);
68 systemClock->advance(remaining);
69 remaining = time::nanoseconds::zero();
70 }
71
72 if (m_io.stopped())
73 m_io.reset();
74 m_io.poll();
75 }
76}
77
78shared_ptr<Interest>
79makeInterest(const Name& name, uint32_t nonce)
80{
81 auto interest = make_shared<Interest>(name);
82 if (nonce != 0) {
83 interest->setNonce(nonce);
84 }
85 return interest;
86}
87
88shared_ptr<Data>
89makeData(const Name& name)
90{
91 auto data = make_shared<Data>(name);
92 return signData(data);
93}
94
95Data&
96signData(Data& data)
97{
98 ndn::SignatureSha256WithRsa fakeSignature;
99 fakeSignature.setValue(ndn::encoding::makeEmptyBlock(tlv::SignatureValue));
100 data.setSignature(fakeSignature);
101 data.wireEncode();
102
103 return data;
104}
105
106shared_ptr<Link>
107makeLink(const Name& name, std::initializer_list<std::pair<uint32_t, Name>> delegations)
108{
109 auto link = make_shared<Link>(name, delegations);
110 signData(link);
111 return link;
112}
113
114lp::Nack
115makeNack(const Name& name, uint32_t nonce, lp::NackReason reason)
116{
117 Interest interest(name);
118 interest.setNonce(nonce);
119 lp::Nack nack(std::move(interest));
120 nack.setReason(reason);
121 return nack;
122}
123
124} // namespace tests
125} // namespace chronoshare
126} // namespace ndn