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