blob: 62e728afe35e3fac5e82f6997f04a18161f11852 [file] [log] [blame]
Junxiao Shid23de8b2016-07-23 20:05:42 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shifc2e13d2017-07-25 02:08:48 +00002/*
Teng Liang952d6fd2018-05-29 21:09:52 -07003 * Copyright (c) 2014-2018, Regents of the University of California,
Junxiao Shid23de8b2016-07-23 20:05:42 +00004 * 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
Teng Liang952d6fd2018-05-29 21:09:52 -070055UnitTestTimeFixture::advanceClocks(time::nanoseconds tick, size_t nTicks)
Junxiao Shid23de8b2016-07-23 20:05:42 +000056{
Teng Liang952d6fd2018-05-29 21:09:52 -070057 advanceClocks(tick, tick * nTicks);
Junxiao Shid23de8b2016-07-23 20:05:42 +000058}
59
60void
Teng Liang952d6fd2018-05-29 21:09:52 -070061UnitTestTimeFixture::advanceClocks(time::nanoseconds tick, time::nanoseconds total)
Junxiao Shid23de8b2016-07-23 20:05:42 +000062{
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
Junxiao Shid23de8b2016-07-23 20:05:42 +0000113lp::Nack
Junxiao Shi99540072017-01-27 19:57:33 +0000114makeNack(Interest interest, lp::NackReason reason)
115{
116 lp::Nack nack(std::move(interest));
117 nack.setReason(reason);
118 return nack;
119}
120
121lp::Nack
Junxiao Shid23de8b2016-07-23 20:05:42 +0000122makeNack(const Name& name, uint32_t nonce, lp::NackReason reason)
123{
124 Interest interest(name);
125 interest.setNonce(nonce);
Junxiao Shi99540072017-01-27 19:57:33 +0000126 return makeNack(std::move(interest), reason);
Junxiao Shid23de8b2016-07-23 20:05:42 +0000127}
128
Junxiao Shid47cd632018-09-11 03:10:00 +0000129ndn::PrefixAnnouncement
130makePrefixAnn(const Name& announcedName, time::milliseconds expiration,
131 optional<ndn::security::ValidityPeriod> validity)
132{
133 ndn::PrefixAnnouncement pa;
134 pa.setAnnouncedName(announcedName);
135 pa.setExpiration(expiration);
136 pa.setValidityPeriod(validity);
137 return pa;
138}
139
140ndn::PrefixAnnouncement
141makePrefixAnn(const Name& announcedName, time::milliseconds expiration,
142 std::pair<time::seconds, time::seconds> validityFromNow)
143{
144 auto now = time::system_clock::now();
145 return makePrefixAnn(announcedName, expiration,
146 ndn::security::ValidityPeriod(now + validityFromNow.first, now + validityFromNow.second));
147}
148
149ndn::PrefixAnnouncement
150signPrefixAnn(ndn::PrefixAnnouncement&& pa, ndn::KeyChain& keyChain,
151 const ndn::security::SigningInfo& si, optional<uint64_t> version)
152{
153 pa.toData(keyChain, si, version);
154 return std::move(pa);
155}
156
Junxiao Shid23de8b2016-07-23 20:05:42 +0000157} // namespace tests
158} // namespace nfd