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