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 | 3dade00 | 2019-03-19 11:29:56 -0600 | [diff] [blame] | 3 | * Copyright (c) 2014-2019, 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 | |
Davide Pesavento | 3dade00 | 2019-03-19 11:29:56 -0600 | [diff] [blame] | 31 | #include "core/common.hpp" |
Alexander Afanasyev | 8552a38 | 2014-05-15 20:13:42 -0700 | [diff] [blame] | 32 | |
Junxiao Shi | d47cd63 | 2018-09-11 03:10:00 +0000 | [diff] [blame] | 33 | #include <ndn-cxx/prefix-announcement.hpp> |
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 | |
Teng Liang | 952d6fd | 2018-05-29 21:09:52 -0700 | [diff] [blame] | 62 | virtual |
Junxiao Shi | d23de8b | 2016-07-23 20:05:42 +0000 | [diff] [blame] | 63 | ~BaseFixture(); |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 64 | |
| 65 | protected: |
Junxiao Shi | d23de8b | 2016-07-23 20:05:42 +0000 | [diff] [blame] | 66 | /** \brief reference to global io_service |
| 67 | */ |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 68 | boost::asio::io_service& g_io; |
| 69 | }; |
| 70 | |
Junxiao Shi | 455581d | 2014-11-17 18:38:40 -0700 | [diff] [blame] | 71 | /** \brief a base test fixture that overrides steady clock and system clock |
| 72 | */ |
Yukai Tu | 0a49d34 | 2015-09-13 12:54:22 +0800 | [diff] [blame] | 73 | class UnitTestTimeFixture : public virtual BaseFixture |
Junxiao Shi | 455581d | 2014-11-17 18:38:40 -0700 | [diff] [blame] | 74 | { |
Junxiao Shi | e368d99 | 2014-12-02 23:44:31 -0700 | [diff] [blame] | 75 | protected: |
Junxiao Shi | d23de8b | 2016-07-23 20:05:42 +0000 | [diff] [blame] | 76 | UnitTestTimeFixture(); |
Junxiao Shi | 455581d | 2014-11-17 18:38:40 -0700 | [diff] [blame] | 77 | |
Teng Liang | 952d6fd | 2018-05-29 21:09:52 -0700 | [diff] [blame] | 78 | virtual |
Junxiao Shi | d23de8b | 2016-07-23 20:05:42 +0000 | [diff] [blame] | 79 | ~UnitTestTimeFixture(); |
Junxiao Shi | 455581d | 2014-11-17 18:38:40 -0700 | [diff] [blame] | 80 | |
| 81 | /** \brief advance steady and system clocks |
| 82 | * |
| 83 | * Clocks are advanced in increments of \p tick for \p nTicks ticks. |
| 84 | * After each tick, global io_service is polled to process pending I/O events. |
| 85 | * |
| 86 | * Exceptions thrown during I/O events are propagated to the caller. |
| 87 | * Clock advancing would stop in case of an exception. |
| 88 | */ |
Teng Liang | 952d6fd | 2018-05-29 21:09:52 -0700 | [diff] [blame] | 89 | virtual void |
| 90 | advanceClocks(time::nanoseconds tick, size_t nTicks = 1); |
Junxiao Shi | e368d99 | 2014-12-02 23:44:31 -0700 | [diff] [blame] | 91 | |
| 92 | /** \brief advance steady and system clocks |
| 93 | * |
| 94 | * Clocks are advanced in increments of \p tick for \p total time. |
| 95 | * The last increment might be shorter than \p tick. |
| 96 | * After each tick, global io_service is polled to process pending I/O events. |
| 97 | * |
| 98 | * Exceptions thrown during I/O events are propagated to the caller. |
| 99 | * Clock advancing would stop in case of an exception. |
| 100 | */ |
Teng Liang | 952d6fd | 2018-05-29 21:09:52 -0700 | [diff] [blame] | 101 | virtual void |
| 102 | advanceClocks(time::nanoseconds tick, time::nanoseconds total); |
Junxiao Shi | 3cb4fc6 | 2014-12-25 22:17:39 -0700 | [diff] [blame] | 103 | |
Junxiao Shi | 455581d | 2014-11-17 18:38:40 -0700 | [diff] [blame] | 104 | protected: |
| 105 | shared_ptr<time::UnitTestSteadyClock> steadyClock; |
| 106 | shared_ptr<time::UnitTestSystemClock> systemClock; |
Junxiao Shi | d23de8b | 2016-07-23 20:05:42 +0000 | [diff] [blame] | 107 | |
| 108 | friend class LimitedIo; |
Junxiao Shi | 455581d | 2014-11-17 18:38:40 -0700 | [diff] [blame] | 109 | }; |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 110 | |
Junxiao Shi | d23de8b | 2016-07-23 20:05:42 +0000 | [diff] [blame] | 111 | /** \brief create an Interest |
| 112 | * \param name Interest name |
| 113 | * \param nonce if non-zero, set Nonce to this value |
| 114 | * (useful for creating Nack with same Nonce) |
| 115 | */ |
| 116 | shared_ptr<Interest> |
| 117 | makeInterest(const Name& name, uint32_t nonce = 0); |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 118 | |
Junxiao Shi | d23de8b | 2016-07-23 20:05:42 +0000 | [diff] [blame] | 119 | /** \brief create a Data with fake signature |
| 120 | * \note Data may be modified afterwards without losing the fake signature. |
| 121 | * If a real signature is desired, sign again with KeyChain. |
| 122 | */ |
| 123 | shared_ptr<Data> |
| 124 | makeData(const Name& name); |
| 125 | |
| 126 | /** \brief add a fake signature to Data |
| 127 | */ |
| 128 | Data& |
| 129 | signData(Data& data); |
| 130 | |
| 131 | /** \brief add a fake signature to Data |
| 132 | */ |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 133 | inline shared_ptr<Data> |
Junxiao Shi | 0355e9f | 2015-09-02 07:24:53 -0700 | [diff] [blame] | 134 | signData(shared_ptr<Data> data) |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 135 | { |
Junxiao Shi | d23de8b | 2016-07-23 20:05:42 +0000 | [diff] [blame] | 136 | signData(*data); |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 137 | return data; |
| 138 | } |
| 139 | |
Junxiao Shi | d23de8b | 2016-07-23 20:05:42 +0000 | [diff] [blame] | 140 | /** \brief create a Nack |
Junxiao Shi | 9954007 | 2017-01-27 19:57:33 +0000 | [diff] [blame] | 141 | * \param interest Interest |
| 142 | * \param reason Nack reason |
| 143 | */ |
| 144 | lp::Nack |
| 145 | makeNack(Interest interest, lp::NackReason reason); |
| 146 | |
| 147 | /** \brief create a Nack |
Junxiao Shi | d23de8b | 2016-07-23 20:05:42 +0000 | [diff] [blame] | 148 | * \param name Interest name |
| 149 | * \param nonce Interest nonce |
| 150 | * \param reason Nack reason |
| 151 | */ |
| 152 | lp::Nack |
| 153 | makeNack(const Name& name, uint32_t nonce, lp::NackReason reason); |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 154 | |
Junxiao Shi | d763127 | 2016-08-17 04:16:31 +0000 | [diff] [blame] | 155 | /** \brief replace a name component |
| 156 | * \param[inout] name name |
| 157 | * \param index name component index |
| 158 | * \param a arguments to name::Component constructor |
| 159 | */ |
Davide Pesavento | 3dade00 | 2019-03-19 11:29:56 -0600 | [diff] [blame] | 160 | template<typename... A> |
Junxiao Shi | d763127 | 2016-08-17 04:16:31 +0000 | [diff] [blame] | 161 | void |
Davide Pesavento | 3dade00 | 2019-03-19 11:29:56 -0600 | [diff] [blame] | 162 | setNameComponent(Name& name, ssize_t index, A&&... a) |
Junxiao Shi | d763127 | 2016-08-17 04:16:31 +0000 | [diff] [blame] | 163 | { |
| 164 | Name name2 = name.getPrefix(index); |
Davide Pesavento | 3dade00 | 2019-03-19 11:29:56 -0600 | [diff] [blame] | 165 | name2.append(name::Component(std::forward<A>(a)...)); |
Junxiao Shi | d763127 | 2016-08-17 04:16:31 +0000 | [diff] [blame] | 166 | name2.append(name.getSubName(name2.size())); |
Davide Pesavento | 3dade00 | 2019-03-19 11:29:56 -0600 | [diff] [blame] | 167 | name = std::move(name2); |
Junxiao Shi | d763127 | 2016-08-17 04:16:31 +0000 | [diff] [blame] | 168 | } |
| 169 | |
Davide Pesavento | 3dade00 | 2019-03-19 11:29:56 -0600 | [diff] [blame] | 170 | template<typename Packet, typename... A> |
Junxiao Shi | d763127 | 2016-08-17 04:16:31 +0000 | [diff] [blame] | 171 | void |
Davide Pesavento | 3dade00 | 2019-03-19 11:29:56 -0600 | [diff] [blame] | 172 | setNameComponent(Packet& packet, ssize_t index, A&&... a) |
Junxiao Shi | d763127 | 2016-08-17 04:16:31 +0000 | [diff] [blame] | 173 | { |
| 174 | Name name = packet.getName(); |
Davide Pesavento | 3dade00 | 2019-03-19 11:29:56 -0600 | [diff] [blame] | 175 | setNameComponent(name, index, std::forward<A>(a)...); |
Junxiao Shi | d763127 | 2016-08-17 04:16:31 +0000 | [diff] [blame] | 176 | packet.setName(name); |
| 177 | } |
| 178 | |
Junxiao Shi | d47cd63 | 2018-09-11 03:10:00 +0000 | [diff] [blame] | 179 | /** \brief create a prefix announcement without signing |
| 180 | */ |
| 181 | ndn::PrefixAnnouncement |
| 182 | makePrefixAnn(const Name& announcedName, time::milliseconds expiration, |
| 183 | optional<ndn::security::ValidityPeriod> validity = nullopt); |
| 184 | |
| 185 | /** \brief create a prefix announcement without signing |
| 186 | * \param announcedName announced name |
| 187 | * \param expiration expiration period |
| 188 | * \param validityFromNow validity period, relative from now |
| 189 | */ |
| 190 | ndn::PrefixAnnouncement |
| 191 | makePrefixAnn(const Name& announcedName, time::milliseconds expiration, |
| 192 | std::pair<time::seconds, time::seconds> validityFromNow); |
| 193 | |
| 194 | /** \brief sign a prefix announcement |
| 195 | */ |
| 196 | ndn::PrefixAnnouncement |
| 197 | signPrefixAnn(ndn::PrefixAnnouncement&& pa, ndn::KeyChain& keyChain, |
| 198 | const ndn::security::SigningInfo& si = ndn::KeyChain::getDefaultSigningInfo(), |
| 199 | optional<uint64_t> version = nullopt); |
| 200 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 201 | } // namespace tests |
| 202 | } // namespace nfd |
| 203 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 204 | #endif // NFD_TESTS_TEST_COMMON_HPP |