blob: 42cbf37593821e9f7d46b2f7d388428ed1b2374e [file] [log] [blame]
Zhiyi Zhang8617a792017-01-17 16:45:56 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014-2017, 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 ndncert, a certificate management system based on NDN.
13 *
14 * ndncert 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 * ndncert 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 * ndncert, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
24 *
25 * See AUTHORS.md for complete list of ndncert authors and contributors.
26 */
27
28#include "test-common.hpp"
29
Zhiyi Zhangb6fab0f2017-09-21 16:26:27 -070030#include <ndn-cxx/util/sha256.hpp>
Zhiyi Zhang8617a792017-01-17 16:45:56 -080031#include <ndn-cxx/security/signature-sha256-with-rsa.hpp>
32
33namespace ndn {
34namespace ndncert {
35namespace tests {
36
37UnitTestTimeFixture::UnitTestTimeFixture()
38 : steadyClock(make_shared<time::UnitTestSteadyClock>())
39 , systemClock(make_shared<time::UnitTestSystemClock>())
40{
41 time::setCustomClocks(steadyClock, systemClock);
42}
43
44UnitTestTimeFixture::~UnitTestTimeFixture()
45{
46 time::setCustomClocks(nullptr, nullptr);
47}
48
49void
50UnitTestTimeFixture::advanceClocks(const time::nanoseconds& tick, size_t nTicks)
51{
52 this->advanceClocks(tick, tick * nTicks);
53}
54
55void
56UnitTestTimeFixture::advanceClocks(const time::nanoseconds& tick, const time::nanoseconds& total)
57{
58 BOOST_ASSERT(tick > time::nanoseconds::zero());
59 BOOST_ASSERT(total >= time::nanoseconds::zero());
60
61 time::nanoseconds remaining = total;
62 while (remaining > time::nanoseconds::zero()) {
63 if (remaining >= tick) {
64 steadyClock->advance(tick);
65 systemClock->advance(tick);
66 remaining -= tick;
67 }
68 else {
69 steadyClock->advance(remaining);
70 systemClock->advance(remaining);
71 remaining = time::nanoseconds::zero();
72 }
73
74 if (m_io.stopped())
75 m_io.reset();
76 m_io.poll();
77 }
78}
79
80shared_ptr<Interest>
81makeInterest(const Name& name, uint32_t nonce)
82{
83 auto interest = make_shared<Interest>(name);
84 if (nonce != 0) {
85 interest->setNonce(nonce);
86 }
87 return interest;
88}
89
90shared_ptr<Data>
91makeData(const Name& name)
92{
93 auto data = make_shared<Data>(name);
94 return signData(data);
95}
96
97Data&
98signData(Data& data)
99{
100 ndn::SignatureSha256WithRsa fakeSignature;
101 fakeSignature.setValue(ndn::encoding::makeEmptyBlock(tlv::SignatureValue));
102 data.setSignature(fakeSignature);
103 data.wireEncode();
104
105 return data;
106}
107
108shared_ptr<Link>
Zhiyi Zhangb6fab0f2017-09-21 16:26:27 -0700109makeLink(const Name& name, std::initializer_list<Delegation> delegations)
Zhiyi Zhang8617a792017-01-17 16:45:56 -0800110{
111 auto link = make_shared<Link>(name, delegations);
112 signData(link);
113 return link;
114}
115
116lp::Nack
117makeNack(const Name& name, uint32_t nonce, lp::NackReason reason)
118{
119 Interest interest(name);
120 interest.setNonce(nonce);
121 lp::Nack nack(std::move(interest));
122 nack.setReason(reason);
123 return nack;
124}
125
126ConstBufferPtr
127digestFromFile(const boost::filesystem::path& filename)
128{
129 boost::filesystem::ifstream iff(filename, std::ios::in | std::ios::binary);
130 util::Sha256 digest(iff);
131 return digest.computeDigest();
132}
133
134} // namespace tests
135} // namespace ndncert
136} // namespace ndn