Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | fc2e13d | 2017-07-25 02:08:48 +0000 | [diff] [blame] | 2 | /* |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2018, Regents of the University of California, |
Yukai Tu | 0a49d34 | 2015-09-13 12:54:22 +0800 | [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. |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 10 | * |
| 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD 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 | * NFD 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 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
Alexander Afanasyev | c026d25 | 2014-06-16 11:14:15 -0700 | [diff] [blame] | 24 | */ |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 25 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 26 | #ifndef NFD_TESTS_TEST_COMMON_HPP |
| 27 | #define NFD_TESTS_TEST_COMMON_HPP |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 28 | |
Alexander Afanasyev | 8552a38 | 2014-05-15 20:13:42 -0700 | [diff] [blame] | 29 | #include "boost-test.hpp" |
| 30 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 31 | #include "core/global-io.hpp" |
Alexander Afanasyev | 8552a38 | 2014-05-15 20:13:42 -0700 | [diff] [blame] | 32 | |
Junxiao Shi | 455581d | 2014-11-17 18:38:40 -0700 | [diff] [blame] | 33 | #include <ndn-cxx/util/time-unit-test-clock.hpp> |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 34 | |
Weiwei Liu | f5aee94 | 2016-03-19 07:00:42 +0000 | [diff] [blame] | 35 | #ifdef HAVE_PRIVILEGE_DROP_AND_ELEVATE |
| 36 | #include <unistd.h> |
| 37 | #define SKIP_IF_NOT_SUPERUSER() \ |
| 38 | do { \ |
| 39 | if (::geteuid() != 0) { \ |
Davide Pesavento | 231ddd7 | 2016-09-02 22:20:00 +0000 | [diff] [blame] | 40 | BOOST_WARN_MESSAGE(false, "skipping assertions that require superuser privileges"); \ |
Weiwei Liu | f5aee94 | 2016-03-19 07:00:42 +0000 | [diff] [blame] | 41 | return; \ |
| 42 | } \ |
| 43 | } while (false) |
| 44 | #else |
| 45 | #define SKIP_IF_NOT_SUPERUSER() |
| 46 | #endif // HAVE_PRIVILEGE_DROP_AND_ELEVATE |
| 47 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 48 | namespace nfd { |
| 49 | namespace tests { |
| 50 | |
| 51 | /** \brief base test fixture |
| 52 | * |
| 53 | * Every test case should be based on this fixture, |
| 54 | * to have per test case io_service initialization. |
| 55 | */ |
| 56 | class BaseFixture |
| 57 | { |
| 58 | protected: |
Junxiao Shi | d23de8b | 2016-07-23 20:05:42 +0000 | [diff] [blame] | 59 | BaseFixture(); |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 60 | |
Teng Liang | 952d6fd | 2018-05-29 21:09:52 -0700 | [diff] [blame] | 61 | virtual |
Junxiao Shi | d23de8b | 2016-07-23 20:05:42 +0000 | [diff] [blame] | 62 | ~BaseFixture(); |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 63 | |
| 64 | protected: |
Junxiao Shi | d23de8b | 2016-07-23 20:05:42 +0000 | [diff] [blame] | 65 | /** \brief reference to global io_service |
| 66 | */ |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 67 | boost::asio::io_service& g_io; |
| 68 | }; |
| 69 | |
Junxiao Shi | 455581d | 2014-11-17 18:38:40 -0700 | [diff] [blame] | 70 | /** \brief a base test fixture that overrides steady clock and system clock |
| 71 | */ |
Yukai Tu | 0a49d34 | 2015-09-13 12:54:22 +0800 | [diff] [blame] | 72 | class UnitTestTimeFixture : public virtual BaseFixture |
Junxiao Shi | 455581d | 2014-11-17 18:38:40 -0700 | [diff] [blame] | 73 | { |
Junxiao Shi | e368d99 | 2014-12-02 23:44:31 -0700 | [diff] [blame] | 74 | protected: |
Junxiao Shi | d23de8b | 2016-07-23 20:05:42 +0000 | [diff] [blame] | 75 | UnitTestTimeFixture(); |
Junxiao Shi | 455581d | 2014-11-17 18:38:40 -0700 | [diff] [blame] | 76 | |
Teng Liang | 952d6fd | 2018-05-29 21:09:52 -0700 | [diff] [blame] | 77 | virtual |
Junxiao Shi | d23de8b | 2016-07-23 20:05:42 +0000 | [diff] [blame] | 78 | ~UnitTestTimeFixture(); |
Junxiao Shi | 455581d | 2014-11-17 18:38:40 -0700 | [diff] [blame] | 79 | |
| 80 | /** \brief advance steady and system clocks |
| 81 | * |
| 82 | * Clocks are advanced in increments of \p tick for \p nTicks ticks. |
| 83 | * After each tick, global io_service is polled to process pending I/O events. |
| 84 | * |
| 85 | * Exceptions thrown during I/O events are propagated to the caller. |
| 86 | * Clock advancing would stop in case of an exception. |
| 87 | */ |
Teng Liang | 952d6fd | 2018-05-29 21:09:52 -0700 | [diff] [blame] | 88 | virtual void |
| 89 | advanceClocks(time::nanoseconds tick, size_t nTicks = 1); |
Junxiao Shi | e368d99 | 2014-12-02 23:44:31 -0700 | [diff] [blame] | 90 | |
| 91 | /** \brief advance steady and system clocks |
| 92 | * |
| 93 | * Clocks are advanced in increments of \p tick for \p total time. |
| 94 | * The last increment might be shorter than \p tick. |
| 95 | * After each tick, global io_service is polled to process pending I/O events. |
| 96 | * |
| 97 | * Exceptions thrown during I/O events are propagated to the caller. |
| 98 | * Clock advancing would stop in case of an exception. |
| 99 | */ |
Teng Liang | 952d6fd | 2018-05-29 21:09:52 -0700 | [diff] [blame] | 100 | virtual void |
| 101 | advanceClocks(time::nanoseconds tick, time::nanoseconds total); |
Junxiao Shi | 3cb4fc6 | 2014-12-25 22:17:39 -0700 | [diff] [blame] | 102 | |
Junxiao Shi | 455581d | 2014-11-17 18:38:40 -0700 | [diff] [blame] | 103 | protected: |
| 104 | shared_ptr<time::UnitTestSteadyClock> steadyClock; |
| 105 | shared_ptr<time::UnitTestSystemClock> systemClock; |
Junxiao Shi | d23de8b | 2016-07-23 20:05:42 +0000 | [diff] [blame] | 106 | |
| 107 | friend class LimitedIo; |
Junxiao Shi | 455581d | 2014-11-17 18:38:40 -0700 | [diff] [blame] | 108 | }; |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 109 | |
Junxiao Shi | d23de8b | 2016-07-23 20:05:42 +0000 | [diff] [blame] | 110 | /** \brief create an Interest |
| 111 | * \param name Interest name |
| 112 | * \param nonce if non-zero, set Nonce to this value |
| 113 | * (useful for creating Nack with same Nonce) |
| 114 | */ |
| 115 | shared_ptr<Interest> |
| 116 | makeInterest(const Name& name, uint32_t nonce = 0); |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 117 | |
Junxiao Shi | d23de8b | 2016-07-23 20:05:42 +0000 | [diff] [blame] | 118 | /** \brief create a Data with fake signature |
| 119 | * \note Data may be modified afterwards without losing the fake signature. |
| 120 | * If a real signature is desired, sign again with KeyChain. |
| 121 | */ |
| 122 | shared_ptr<Data> |
| 123 | makeData(const Name& name); |
| 124 | |
| 125 | /** \brief add a fake signature to Data |
| 126 | */ |
| 127 | Data& |
| 128 | signData(Data& data); |
| 129 | |
| 130 | /** \brief add a fake signature to Data |
| 131 | */ |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 132 | inline shared_ptr<Data> |
Junxiao Shi | 0355e9f | 2015-09-02 07:24:53 -0700 | [diff] [blame] | 133 | signData(shared_ptr<Data> data) |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 134 | { |
Junxiao Shi | d23de8b | 2016-07-23 20:05:42 +0000 | [diff] [blame] | 135 | signData(*data); |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 136 | return data; |
| 137 | } |
| 138 | |
Junxiao Shi | d23de8b | 2016-07-23 20:05:42 +0000 | [diff] [blame] | 139 | /** \brief create a Nack |
Junxiao Shi | 9954007 | 2017-01-27 19:57:33 +0000 | [diff] [blame] | 140 | * \param interest Interest |
| 141 | * \param reason Nack reason |
| 142 | */ |
| 143 | lp::Nack |
| 144 | makeNack(Interest interest, lp::NackReason reason); |
| 145 | |
| 146 | /** \brief create a Nack |
Junxiao Shi | d23de8b | 2016-07-23 20:05:42 +0000 | [diff] [blame] | 147 | * \param name Interest name |
| 148 | * \param nonce Interest nonce |
| 149 | * \param reason Nack reason |
| 150 | */ |
| 151 | lp::Nack |
| 152 | makeNack(const Name& name, uint32_t nonce, lp::NackReason reason); |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 153 | |
Junxiao Shi | d763127 | 2016-08-17 04:16:31 +0000 | [diff] [blame] | 154 | /** \brief replace a name component |
| 155 | * \param[inout] name name |
| 156 | * \param index name component index |
| 157 | * \param a arguments to name::Component constructor |
| 158 | */ |
| 159 | template<typename...A> |
| 160 | void |
| 161 | setNameComponent(Name& name, ssize_t index, const A& ...a) |
| 162 | { |
| 163 | Name name2 = name.getPrefix(index); |
| 164 | name2.append(name::Component(a...)); |
| 165 | name2.append(name.getSubName(name2.size())); |
| 166 | name = name2; |
| 167 | } |
| 168 | |
| 169 | template<typename Packet, typename...A> |
| 170 | void |
| 171 | setNameComponent(Packet& packet, ssize_t index, const A& ...a) |
| 172 | { |
| 173 | Name name = packet.getName(); |
| 174 | setNameComponent(name, index, a...); |
| 175 | packet.setName(name); |
| 176 | } |
| 177 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 178 | } // namespace tests |
| 179 | } // namespace nfd |
| 180 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 181 | #endif // NFD_TESTS_TEST_COMMON_HPP |