Junxiao Shi | 7809e30 | 2016-07-23 14:11:25 +0000 | [diff] [blame^] | 1 | /* -*- 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 is part of ndn-tools (Named Data Networking Essential Tools). |
| 12 | * See AUTHORS.md for complete list of ndn-tools authors and contributors. |
| 13 | * |
| 14 | * ndn-tools 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 | * ndn-tools 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 | * ndn-tools, 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 | |
| 29 | namespace ndn { |
| 30 | namespace tests { |
| 31 | |
| 32 | UnitTestTimeFixture::UnitTestTimeFixture() |
| 33 | : steadyClock(make_shared<time::UnitTestSteadyClock>()) |
| 34 | , systemClock(make_shared<time::UnitTestSystemClock>()) |
| 35 | { |
| 36 | time::setCustomClocks(steadyClock, systemClock); |
| 37 | } |
| 38 | |
| 39 | UnitTestTimeFixture::~UnitTestTimeFixture() |
| 40 | { |
| 41 | time::setCustomClocks(nullptr, nullptr); |
| 42 | } |
| 43 | |
| 44 | void |
| 45 | UnitTestTimeFixture::advanceClocks(boost::asio::io_service& io, |
| 46 | time::nanoseconds tick, size_t nTicks) |
| 47 | { |
| 48 | this->advanceClocks(io, tick, tick * nTicks); |
| 49 | } |
| 50 | |
| 51 | void |
| 52 | UnitTestTimeFixture::advanceClocks(boost::asio::io_service& io, |
| 53 | time::nanoseconds tick, time::nanoseconds total) |
| 54 | { |
| 55 | BOOST_ASSERT(tick > time::nanoseconds::zero()); |
| 56 | BOOST_ASSERT(total >= time::nanoseconds::zero()); |
| 57 | |
| 58 | time::nanoseconds remaining = total; |
| 59 | while (remaining > time::nanoseconds::zero()) { |
| 60 | if (remaining >= tick) { |
| 61 | steadyClock->advance(tick); |
| 62 | systemClock->advance(tick); |
| 63 | remaining -= tick; |
| 64 | } |
| 65 | else { |
| 66 | steadyClock->advance(remaining); |
| 67 | systemClock->advance(remaining); |
| 68 | remaining = time::nanoseconds::zero(); |
| 69 | } |
| 70 | |
| 71 | if (io.stopped()) |
| 72 | io.reset(); |
| 73 | io.poll(); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | shared_ptr<Interest> |
| 78 | makeInterest(const Name& name, uint32_t nonce) |
| 79 | { |
| 80 | auto interest = make_shared<Interest>(name); |
| 81 | if (nonce != 0) { |
| 82 | interest->setNonce(nonce); |
| 83 | } |
| 84 | return interest; |
| 85 | } |
| 86 | |
| 87 | shared_ptr<Data> |
| 88 | makeData(const Name& name) |
| 89 | { |
| 90 | auto data = make_shared<Data>(name); |
| 91 | return signData(data); |
| 92 | } |
| 93 | |
| 94 | Data& |
| 95 | signData(Data& data) |
| 96 | { |
| 97 | ndn::SignatureSha256WithRsa fakeSignature; |
| 98 | fakeSignature.setValue(ndn::encoding::makeEmptyBlock(tlv::SignatureValue)); |
| 99 | data.setSignature(fakeSignature); |
| 100 | data.wireEncode(); |
| 101 | |
| 102 | return data; |
| 103 | } |
| 104 | |
| 105 | shared_ptr<Link> |
| 106 | makeLink(const Name& name, std::initializer_list<std::pair<uint32_t, Name>> delegations) |
| 107 | { |
| 108 | auto link = make_shared<Link>(name, delegations); |
| 109 | signData(link); |
| 110 | return link; |
| 111 | } |
| 112 | |
| 113 | lp::Nack |
| 114 | makeNack(const Name& name, uint32_t nonce, lp::NackReason reason) |
| 115 | { |
| 116 | Interest interest(name); |
| 117 | interest.setNonce(nonce); |
| 118 | lp::Nack nack(std::move(interest)); |
| 119 | nack.setReason(reason); |
| 120 | return nack; |
| 121 | } |
| 122 | |
| 123 | } // namespace tests |
| 124 | } // namespace ndn |