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