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 | |
Davide Pesavento | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame] | 29 | #include "core/common.hpp" |
| 30 | #include "tests/boost-test.hpp" |
Alexander Afanasyev | 8552a38 | 2014-05-15 20:13:42 -0700 | [diff] [blame] | 31 | |
Junxiao Shi | d47cd63 | 2018-09-11 03:10:00 +0000 | [diff] [blame] | 32 | #include <ndn-cxx/prefix-announcement.hpp> |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 33 | |
Weiwei Liu | f5aee94 | 2016-03-19 07:00:42 +0000 | [diff] [blame] | 34 | #ifdef HAVE_PRIVILEGE_DROP_AND_ELEVATE |
| 35 | #include <unistd.h> |
| 36 | #define SKIP_IF_NOT_SUPERUSER() \ |
| 37 | do { \ |
| 38 | if (::geteuid() != 0) { \ |
Davide Pesavento | 231ddd7 | 2016-09-02 22:20:00 +0000 | [diff] [blame] | 39 | BOOST_WARN_MESSAGE(false, "skipping assertions that require superuser privileges"); \ |
Weiwei Liu | f5aee94 | 2016-03-19 07:00:42 +0000 | [diff] [blame] | 40 | return; \ |
| 41 | } \ |
| 42 | } while (false) |
| 43 | #else |
| 44 | #define SKIP_IF_NOT_SUPERUSER() |
| 45 | #endif // HAVE_PRIVILEGE_DROP_AND_ELEVATE |
| 46 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 47 | namespace nfd { |
| 48 | namespace tests { |
| 49 | |
Junxiao Shi | d23de8b | 2016-07-23 20:05:42 +0000 | [diff] [blame] | 50 | /** \brief create an Interest |
| 51 | * \param name Interest name |
| 52 | * \param nonce if non-zero, set Nonce to this value |
| 53 | * (useful for creating Nack with same Nonce) |
| 54 | */ |
| 55 | shared_ptr<Interest> |
| 56 | makeInterest(const Name& name, uint32_t nonce = 0); |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 57 | |
Junxiao Shi | d23de8b | 2016-07-23 20:05:42 +0000 | [diff] [blame] | 58 | /** \brief create a Data with fake signature |
| 59 | * \note Data may be modified afterwards without losing the fake signature. |
| 60 | * If a real signature is desired, sign again with KeyChain. |
| 61 | */ |
| 62 | shared_ptr<Data> |
| 63 | makeData(const Name& name); |
| 64 | |
| 65 | /** \brief add a fake signature to Data |
| 66 | */ |
| 67 | Data& |
| 68 | signData(Data& data); |
| 69 | |
| 70 | /** \brief add a fake signature to Data |
| 71 | */ |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 72 | inline shared_ptr<Data> |
Junxiao Shi | 0355e9f | 2015-09-02 07:24:53 -0700 | [diff] [blame] | 73 | signData(shared_ptr<Data> data) |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 74 | { |
Junxiao Shi | d23de8b | 2016-07-23 20:05:42 +0000 | [diff] [blame] | 75 | signData(*data); |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 76 | return data; |
| 77 | } |
| 78 | |
Junxiao Shi | d23de8b | 2016-07-23 20:05:42 +0000 | [diff] [blame] | 79 | /** \brief create a Nack |
Junxiao Shi | 9954007 | 2017-01-27 19:57:33 +0000 | [diff] [blame] | 80 | * \param interest Interest |
| 81 | * \param reason Nack reason |
| 82 | */ |
| 83 | lp::Nack |
| 84 | makeNack(Interest interest, lp::NackReason reason); |
| 85 | |
| 86 | /** \brief create a Nack |
Junxiao Shi | d23de8b | 2016-07-23 20:05:42 +0000 | [diff] [blame] | 87 | * \param name Interest name |
| 88 | * \param nonce Interest nonce |
| 89 | * \param reason Nack reason |
| 90 | */ |
| 91 | lp::Nack |
| 92 | makeNack(const Name& name, uint32_t nonce, lp::NackReason reason); |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 93 | |
Junxiao Shi | d763127 | 2016-08-17 04:16:31 +0000 | [diff] [blame] | 94 | /** \brief replace a name component |
| 95 | * \param[inout] name name |
| 96 | * \param index name component index |
| 97 | * \param a arguments to name::Component constructor |
| 98 | */ |
Davide Pesavento | 3dade00 | 2019-03-19 11:29:56 -0600 | [diff] [blame] | 99 | template<typename... A> |
Junxiao Shi | d763127 | 2016-08-17 04:16:31 +0000 | [diff] [blame] | 100 | void |
Davide Pesavento | 3dade00 | 2019-03-19 11:29:56 -0600 | [diff] [blame] | 101 | setNameComponent(Name& name, ssize_t index, A&&... a) |
Junxiao Shi | d763127 | 2016-08-17 04:16:31 +0000 | [diff] [blame] | 102 | { |
| 103 | Name name2 = name.getPrefix(index); |
Davide Pesavento | 3dade00 | 2019-03-19 11:29:56 -0600 | [diff] [blame] | 104 | name2.append(name::Component(std::forward<A>(a)...)); |
Junxiao Shi | d763127 | 2016-08-17 04:16:31 +0000 | [diff] [blame] | 105 | name2.append(name.getSubName(name2.size())); |
Davide Pesavento | 3dade00 | 2019-03-19 11:29:56 -0600 | [diff] [blame] | 106 | name = std::move(name2); |
Junxiao Shi | d763127 | 2016-08-17 04:16:31 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Davide Pesavento | 3dade00 | 2019-03-19 11:29:56 -0600 | [diff] [blame] | 109 | template<typename Packet, typename... A> |
Junxiao Shi | d763127 | 2016-08-17 04:16:31 +0000 | [diff] [blame] | 110 | void |
Davide Pesavento | 3dade00 | 2019-03-19 11:29:56 -0600 | [diff] [blame] | 111 | setNameComponent(Packet& packet, ssize_t index, A&&... a) |
Junxiao Shi | d763127 | 2016-08-17 04:16:31 +0000 | [diff] [blame] | 112 | { |
| 113 | Name name = packet.getName(); |
Davide Pesavento | 3dade00 | 2019-03-19 11:29:56 -0600 | [diff] [blame] | 114 | setNameComponent(name, index, std::forward<A>(a)...); |
Junxiao Shi | d763127 | 2016-08-17 04:16:31 +0000 | [diff] [blame] | 115 | packet.setName(name); |
| 116 | } |
| 117 | |
Junxiao Shi | d47cd63 | 2018-09-11 03:10:00 +0000 | [diff] [blame] | 118 | /** \brief create a prefix announcement without signing |
| 119 | */ |
| 120 | ndn::PrefixAnnouncement |
| 121 | makePrefixAnn(const Name& announcedName, time::milliseconds expiration, |
| 122 | optional<ndn::security::ValidityPeriod> validity = nullopt); |
| 123 | |
| 124 | /** \brief create a prefix announcement without signing |
| 125 | * \param announcedName announced name |
| 126 | * \param expiration expiration period |
| 127 | * \param validityFromNow validity period, relative from now |
| 128 | */ |
| 129 | ndn::PrefixAnnouncement |
| 130 | makePrefixAnn(const Name& announcedName, time::milliseconds expiration, |
| 131 | std::pair<time::seconds, time::seconds> validityFromNow); |
| 132 | |
| 133 | /** \brief sign a prefix announcement |
| 134 | */ |
| 135 | ndn::PrefixAnnouncement |
| 136 | signPrefixAnn(ndn::PrefixAnnouncement&& pa, ndn::KeyChain& keyChain, |
| 137 | const ndn::security::SigningInfo& si = ndn::KeyChain::getDefaultSigningInfo(), |
| 138 | optional<uint64_t> version = nullopt); |
| 139 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 140 | } // namespace tests |
| 141 | } // namespace nfd |
| 142 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 143 | #endif // NFD_TESTS_TEST_COMMON_HPP |