blob: 59060d6f9994a9d7cb2d2f6c49320735eef41b98 [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/*
Davide Pesavento3dade002019-03-19 11:29:56 -06003 * Copyright (c) 2014-2019, 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"
Davide Pesavento3dade002019-03-19 11:29:56 -060027#include "daemon/global.hpp"
28
Junxiao Shid23de8b2016-07-23 20:05:42 +000029#include <ndn-cxx/security/signature-sha256-with-rsa.hpp>
30
31namespace nfd {
32namespace tests {
33
34BaseFixture::BaseFixture()
35 : g_io(getGlobalIoService())
36{
37}
38
39BaseFixture::~BaseFixture()
40{
41 resetGlobalIoService();
42}
43
44UnitTestTimeFixture::UnitTestTimeFixture()
45 : steadyClock(make_shared<time::UnitTestSteadyClock>())
46 , systemClock(make_shared<time::UnitTestSystemClock>())
47{
48 time::setCustomClocks(steadyClock, systemClock);
49}
50
51UnitTestTimeFixture::~UnitTestTimeFixture()
52{
53 time::setCustomClocks(nullptr, nullptr);
54}
55
56void
Teng Liang952d6fd2018-05-29 21:09:52 -070057UnitTestTimeFixture::advanceClocks(time::nanoseconds tick, size_t nTicks)
Junxiao Shid23de8b2016-07-23 20:05:42 +000058{
Teng Liang952d6fd2018-05-29 21:09:52 -070059 advanceClocks(tick, tick * nTicks);
Junxiao Shid23de8b2016-07-23 20:05:42 +000060}
61
62void
Teng Liang952d6fd2018-05-29 21:09:52 -070063UnitTestTimeFixture::advanceClocks(time::nanoseconds tick, time::nanoseconds total)
Junxiao Shid23de8b2016-07-23 20:05:42 +000064{
65 BOOST_ASSERT(tick > time::nanoseconds::zero());
66 BOOST_ASSERT(total >= time::nanoseconds::zero());
67
68 time::nanoseconds remaining = total;
69 while (remaining > time::nanoseconds::zero()) {
70 if (remaining >= tick) {
71 steadyClock->advance(tick);
72 systemClock->advance(tick);
73 remaining -= tick;
74 }
75 else {
76 steadyClock->advance(remaining);
77 systemClock->advance(remaining);
78 remaining = time::nanoseconds::zero();
79 }
80
81 if (g_io.stopped())
82 g_io.reset();
83 g_io.poll();
84 }
85}
86
87shared_ptr<Interest>
88makeInterest(const Name& name, uint32_t nonce)
89{
90 auto interest = make_shared<Interest>(name);
91 if (nonce != 0) {
92 interest->setNonce(nonce);
93 }
94 return interest;
95}
96
97shared_ptr<Data>
98makeData(const Name& name)
99{
100 auto data = make_shared<Data>(name);
101 return signData(data);
102}
103
104Data&
105signData(Data& data)
106{
107 ndn::SignatureSha256WithRsa fakeSignature;
108 fakeSignature.setValue(ndn::encoding::makeEmptyBlock(tlv::SignatureValue));
109 data.setSignature(fakeSignature);
110 data.wireEncode();
111
112 return data;
113}
114
Junxiao Shid23de8b2016-07-23 20:05:42 +0000115lp::Nack
Junxiao Shi99540072017-01-27 19:57:33 +0000116makeNack(Interest interest, lp::NackReason reason)
117{
118 lp::Nack nack(std::move(interest));
119 nack.setReason(reason);
120 return nack;
121}
122
123lp::Nack
Junxiao Shid23de8b2016-07-23 20:05:42 +0000124makeNack(const Name& name, uint32_t nonce, lp::NackReason reason)
125{
126 Interest interest(name);
127 interest.setNonce(nonce);
Junxiao Shi99540072017-01-27 19:57:33 +0000128 return makeNack(std::move(interest), reason);
Junxiao Shid23de8b2016-07-23 20:05:42 +0000129}
130
Junxiao Shid47cd632018-09-11 03:10:00 +0000131ndn::PrefixAnnouncement
132makePrefixAnn(const Name& announcedName, time::milliseconds expiration,
133 optional<ndn::security::ValidityPeriod> validity)
134{
135 ndn::PrefixAnnouncement pa;
136 pa.setAnnouncedName(announcedName);
137 pa.setExpiration(expiration);
138 pa.setValidityPeriod(validity);
139 return pa;
140}
141
142ndn::PrefixAnnouncement
143makePrefixAnn(const Name& announcedName, time::milliseconds expiration,
144 std::pair<time::seconds, time::seconds> validityFromNow)
145{
146 auto now = time::system_clock::now();
147 return makePrefixAnn(announcedName, expiration,
148 ndn::security::ValidityPeriod(now + validityFromNow.first, now + validityFromNow.second));
149}
150
151ndn::PrefixAnnouncement
152signPrefixAnn(ndn::PrefixAnnouncement&& pa, ndn::KeyChain& keyChain,
153 const ndn::security::SigningInfo& si, optional<uint64_t> version)
154{
155 pa.toData(keyChain, si, version);
156 return std::move(pa);
157}
158
Junxiao Shid23de8b2016-07-23 20:05:42 +0000159} // namespace tests
160} // namespace nfd