Junxiao Shi | 2713a3b | 2015-06-22 16:19:05 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | 7809e30 | 2016-07-23 14:11:25 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2016, Regents of the University of California, |
Junxiao Shi | 2713a3b | 2015-06-22 16:19:05 -0700 | [diff] [blame] | 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 | |
Junxiao Shi | 2713a3b | 2015-06-22 16:19:05 -0700 | [diff] [blame] | 31 | #include <ndn-cxx/interest.hpp> |
| 32 | #include <ndn-cxx/data.hpp> |
Junxiao Shi | 7809e30 | 2016-07-23 14:11:25 +0000 | [diff] [blame] | 33 | #include <ndn-cxx/link.hpp> |
| 34 | #include <ndn-cxx/lp/nack.hpp> |
| 35 | #include <ndn-cxx/util/time-unit-test-clock.hpp> |
Junxiao Shi | 2713a3b | 2015-06-22 16:19:05 -0700 | [diff] [blame] | 36 | |
| 37 | #include <boost/asio/io_service.hpp> |
| 38 | |
| 39 | namespace ndn { |
| 40 | namespace tests { |
| 41 | |
| 42 | /** \brief a test fixture that overrides steady clock and system clock |
| 43 | */ |
| 44 | class UnitTestTimeFixture |
| 45 | { |
| 46 | protected: |
Junxiao Shi | 7809e30 | 2016-07-23 14:11:25 +0000 | [diff] [blame] | 47 | UnitTestTimeFixture(); |
Junxiao Shi | 2713a3b | 2015-06-22 16:19:05 -0700 | [diff] [blame] | 48 | |
Junxiao Shi | 7809e30 | 2016-07-23 14:11:25 +0000 | [diff] [blame] | 49 | ~UnitTestTimeFixture(); |
Junxiao Shi | 2713a3b | 2015-06-22 16:19:05 -0700 | [diff] [blame] | 50 | |
| 51 | /** \brief advance steady and system clocks |
| 52 | * |
| 53 | * Clocks are advanced in increments of \p tick for \p nTicks ticks. |
| 54 | * After each tick, the supplied io_service is polled to process pending I/O events. |
| 55 | * |
| 56 | * Exceptions thrown during I/O events are propagated to the caller. |
| 57 | * Clock advancing would stop in case of an exception. |
| 58 | */ |
| 59 | void |
| 60 | advanceClocks(boost::asio::io_service& io, |
Junxiao Shi | 7809e30 | 2016-07-23 14:11:25 +0000 | [diff] [blame] | 61 | time::nanoseconds tick, size_t nTicks = 1); |
Junxiao Shi | 2713a3b | 2015-06-22 16:19:05 -0700 | [diff] [blame] | 62 | |
| 63 | /** \brief advance steady and system clocks |
| 64 | * |
| 65 | * Clocks are advanced in increments of \p tick for \p total time. |
| 66 | * The last increment might be shorter than \p tick. |
| 67 | * After each tick, the supplied io_service is polled to process pending I/O events. |
| 68 | * |
| 69 | * Exceptions thrown during I/O events are propagated to the caller. |
| 70 | * Clock advancing would stop in case of an exception. |
| 71 | */ |
| 72 | void |
| 73 | advanceClocks(boost::asio::io_service& io, |
Junxiao Shi | 7809e30 | 2016-07-23 14:11:25 +0000 | [diff] [blame] | 74 | time::nanoseconds tick, time::nanoseconds total); |
Junxiao Shi | 2713a3b | 2015-06-22 16:19:05 -0700 | [diff] [blame] | 75 | |
| 76 | protected: |
| 77 | shared_ptr<time::UnitTestSteadyClock> steadyClock; |
| 78 | shared_ptr<time::UnitTestSystemClock> systemClock; |
| 79 | }; |
| 80 | |
Junxiao Shi | 7809e30 | 2016-07-23 14:11:25 +0000 | [diff] [blame] | 81 | /** \brief create an Interest |
| 82 | * \param name Interest name |
| 83 | * \param nonce if non-zero, set Nonce to this value |
| 84 | * (useful for creating Nack with same Nonce) |
| 85 | */ |
| 86 | shared_ptr<Interest> |
| 87 | makeInterest(const Name& name, uint32_t nonce = 0); |
Junxiao Shi | 2713a3b | 2015-06-22 16:19:05 -0700 | [diff] [blame] | 88 | |
Junxiao Shi | 7809e30 | 2016-07-23 14:11:25 +0000 | [diff] [blame] | 89 | /** \brief create a Data with fake signature |
| 90 | * \note Data may be modified afterwards without losing the fake signature. |
| 91 | * If a real signature is desired, sign again with KeyChain. |
| 92 | */ |
| 93 | shared_ptr<Data> |
| 94 | makeData(const Name& name); |
| 95 | |
| 96 | /** \brief add a fake signature to Data |
| 97 | */ |
| 98 | Data& |
| 99 | signData(Data& data); |
| 100 | |
| 101 | /** \brief add a fake signature to Data |
| 102 | */ |
Junxiao Shi | 2713a3b | 2015-06-22 16:19:05 -0700 | [diff] [blame] | 103 | inline shared_ptr<Data> |
Junxiao Shi | 7809e30 | 2016-07-23 14:11:25 +0000 | [diff] [blame] | 104 | signData(shared_ptr<Data> data) |
Junxiao Shi | 2713a3b | 2015-06-22 16:19:05 -0700 | [diff] [blame] | 105 | { |
Junxiao Shi | 7809e30 | 2016-07-23 14:11:25 +0000 | [diff] [blame] | 106 | signData(*data); |
Junxiao Shi | 2713a3b | 2015-06-22 16:19:05 -0700 | [diff] [blame] | 107 | return data; |
| 108 | } |
| 109 | |
Junxiao Shi | 7809e30 | 2016-07-23 14:11:25 +0000 | [diff] [blame] | 110 | /** \brief create a Link object with fake signature |
| 111 | * \note Link may be modified afterwards without losing the fake signature. |
| 112 | * If a real signature is desired, sign again with KeyChain. |
| 113 | */ |
| 114 | shared_ptr<Link> |
| 115 | makeLink(const Name& name, std::initializer_list<std::pair<uint32_t, Name>> delegations); |
Junxiao Shi | 2713a3b | 2015-06-22 16:19:05 -0700 | [diff] [blame] | 116 | |
Junxiao Shi | 7809e30 | 2016-07-23 14:11:25 +0000 | [diff] [blame] | 117 | /** \brief create a Nack |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 118 | * \param interest Interest |
| 119 | * \param reason Nack reason |
| 120 | */ |
| 121 | lp::Nack |
| 122 | makeNack(const Interest& interest, lp::NackReason reason); |
| 123 | |
| 124 | /** \brief create a Nack |
Junxiao Shi | 7809e30 | 2016-07-23 14:11:25 +0000 | [diff] [blame] | 125 | * \param name Interest name |
| 126 | * \param nonce Interest nonce |
| 127 | * \param reason Nack reason |
| 128 | */ |
| 129 | lp::Nack |
| 130 | makeNack(const Name& name, uint32_t nonce, lp::NackReason reason); |
Junxiao Shi | 2713a3b | 2015-06-22 16:19:05 -0700 | [diff] [blame] | 131 | |
| 132 | } // namespace tests |
| 133 | } // namespace ndn |
| 134 | |
| 135 | #endif // NDN_TOOLS_TESTS_TEST_COMMON_HPP |