Zhiyi Zhang | 8617a79 | 2017-01-17 16:45:56 -0800 | [diff] [blame] | 1 | /* -*- 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 Zhang | b6fab0f | 2017-09-21 16:26:27 -0700 | [diff] [blame^] | 30 | #include <ndn-cxx/util/sha256.hpp> |
Zhiyi Zhang | 8617a79 | 2017-01-17 16:45:56 -0800 | [diff] [blame] | 31 | #include <ndn-cxx/security/signature-sha256-with-rsa.hpp> |
| 32 | |
| 33 | namespace ndn { |
| 34 | namespace ndncert { |
| 35 | namespace tests { |
| 36 | |
| 37 | UnitTestTimeFixture::UnitTestTimeFixture() |
| 38 | : steadyClock(make_shared<time::UnitTestSteadyClock>()) |
| 39 | , systemClock(make_shared<time::UnitTestSystemClock>()) |
| 40 | { |
| 41 | time::setCustomClocks(steadyClock, systemClock); |
| 42 | } |
| 43 | |
| 44 | UnitTestTimeFixture::~UnitTestTimeFixture() |
| 45 | { |
| 46 | time::setCustomClocks(nullptr, nullptr); |
| 47 | } |
| 48 | |
| 49 | void |
| 50 | UnitTestTimeFixture::advanceClocks(const time::nanoseconds& tick, size_t nTicks) |
| 51 | { |
| 52 | this->advanceClocks(tick, tick * nTicks); |
| 53 | } |
| 54 | |
| 55 | void |
| 56 | UnitTestTimeFixture::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 | |
| 80 | shared_ptr<Interest> |
| 81 | makeInterest(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 | |
| 90 | shared_ptr<Data> |
| 91 | makeData(const Name& name) |
| 92 | { |
| 93 | auto data = make_shared<Data>(name); |
| 94 | return signData(data); |
| 95 | } |
| 96 | |
| 97 | Data& |
| 98 | signData(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 | |
| 108 | shared_ptr<Link> |
Zhiyi Zhang | b6fab0f | 2017-09-21 16:26:27 -0700 | [diff] [blame^] | 109 | makeLink(const Name& name, std::initializer_list<Delegation> delegations) |
Zhiyi Zhang | 8617a79 | 2017-01-17 16:45:56 -0800 | [diff] [blame] | 110 | { |
| 111 | auto link = make_shared<Link>(name, delegations); |
| 112 | signData(link); |
| 113 | return link; |
| 114 | } |
| 115 | |
| 116 | lp::Nack |
| 117 | makeNack(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 | |
| 126 | ConstBufferPtr |
| 127 | digestFromFile(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 |