Junxiao Shi | 2713a3b | 2015-06-22 16:19:05 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014-2015, 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 | #ifndef NDN_TOOLS_TESTS_TEST_COMMON_HPP |
| 27 | #define NDN_TOOLS_TESTS_TEST_COMMON_HPP |
| 28 | |
| 29 | #include "boost-test.hpp" |
| 30 | |
| 31 | #include <ndn-cxx/util/time-unit-test-clock.hpp> |
| 32 | #include <ndn-cxx/name.hpp> |
| 33 | #include <ndn-cxx/interest.hpp> |
| 34 | #include <ndn-cxx/data.hpp> |
| 35 | #include <ndn-cxx/signature.hpp> |
| 36 | #include <ndn-cxx/security/signature-sha256-with-rsa.hpp> |
| 37 | |
| 38 | #include <boost/asio/io_service.hpp> |
| 39 | |
| 40 | namespace ndn { |
| 41 | namespace tests { |
| 42 | |
| 43 | /** \brief a test fixture that overrides steady clock and system clock |
| 44 | */ |
| 45 | class UnitTestTimeFixture |
| 46 | { |
| 47 | protected: |
| 48 | UnitTestTimeFixture() |
| 49 | : steadyClock(make_shared<time::UnitTestSteadyClock>()) |
| 50 | , systemClock(make_shared<time::UnitTestSystemClock>()) |
| 51 | { |
| 52 | time::setCustomClocks(steadyClock, systemClock); |
| 53 | } |
| 54 | |
| 55 | ~UnitTestTimeFixture() |
| 56 | { |
| 57 | time::setCustomClocks(nullptr, nullptr); |
| 58 | } |
| 59 | |
| 60 | /** \brief advance steady and system clocks |
| 61 | * |
| 62 | * Clocks are advanced in increments of \p tick for \p nTicks ticks. |
| 63 | * After each tick, the supplied io_service is polled to process pending I/O events. |
| 64 | * |
| 65 | * Exceptions thrown during I/O events are propagated to the caller. |
| 66 | * Clock advancing would stop in case of an exception. |
| 67 | */ |
| 68 | void |
| 69 | advanceClocks(boost::asio::io_service& io, |
| 70 | const time::nanoseconds& tick, size_t nTicks = 1) |
| 71 | { |
| 72 | BOOST_ASSERT(nTicks >= 0); |
| 73 | |
| 74 | this->advanceClocks(io, tick, tick * nTicks); |
| 75 | } |
| 76 | |
| 77 | /** \brief advance steady and system clocks |
| 78 | * |
| 79 | * Clocks are advanced in increments of \p tick for \p total time. |
| 80 | * The last increment might be shorter than \p tick. |
| 81 | * After each tick, the supplied io_service is polled to process pending I/O events. |
| 82 | * |
| 83 | * Exceptions thrown during I/O events are propagated to the caller. |
| 84 | * Clock advancing would stop in case of an exception. |
| 85 | */ |
| 86 | void |
| 87 | advanceClocks(boost::asio::io_service& io, |
| 88 | const time::nanoseconds& tick, const time::nanoseconds& total) |
| 89 | { |
| 90 | BOOST_ASSERT(tick > time::nanoseconds::zero()); |
| 91 | BOOST_ASSERT(total >= time::nanoseconds::zero()); |
| 92 | |
| 93 | time::nanoseconds remaining = total; |
| 94 | while (remaining > time::nanoseconds::zero()) { |
| 95 | if (remaining >= tick) { |
| 96 | steadyClock->advance(tick); |
| 97 | systemClock->advance(tick); |
| 98 | remaining -= tick; |
| 99 | } |
| 100 | else { |
| 101 | steadyClock->advance(remaining); |
| 102 | systemClock->advance(remaining); |
| 103 | remaining = time::nanoseconds::zero(); |
| 104 | } |
| 105 | |
| 106 | if (io.stopped()) |
| 107 | io.reset(); |
| 108 | io.poll(); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | protected: |
| 113 | shared_ptr<time::UnitTestSteadyClock> steadyClock; |
| 114 | shared_ptr<time::UnitTestSystemClock> systemClock; |
| 115 | }; |
| 116 | |
| 117 | inline shared_ptr<Interest> |
| 118 | makeInterest(const Name& name) |
| 119 | { |
| 120 | return make_shared<Interest>(name); |
| 121 | } |
| 122 | |
| 123 | inline shared_ptr<Data> |
| 124 | signData(const shared_ptr<Data>& data) |
| 125 | { |
| 126 | ndn::SignatureSha256WithRsa fakeSignature; |
| 127 | fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue, |
| 128 | static_cast<const uint8_t*>(nullptr), 0)); |
| 129 | data->setSignature(fakeSignature); |
| 130 | data->wireEncode(); |
| 131 | |
| 132 | return data; |
| 133 | } |
| 134 | |
| 135 | inline shared_ptr<Data> |
| 136 | makeData(const Name& name) |
| 137 | { |
| 138 | shared_ptr<Data> data = make_shared<Data>(name); |
| 139 | return signData(data); |
| 140 | } |
| 141 | |
| 142 | |
| 143 | } // namespace tests |
| 144 | } // namespace ndn |
| 145 | |
| 146 | #endif // NDN_TOOLS_TESTS_TEST_COMMON_HPP |