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 | d6ea0b1 | 2023-03-13 21:35:03 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2023, 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 | |
Davide Pesavento | 264af77 | 2021-02-09 21:48:24 -0500 | [diff] [blame] | 34 | #ifdef NFD_HAVE_PRIVILEGE_DROP_AND_ELEVATE |
Weiwei Liu | f5aee94 | 2016-03-19 07:00:42 +0000 | [diff] [blame] | 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() |
Davide Pesavento | 264af77 | 2021-02-09 21:48:24 -0500 | [diff] [blame] | 45 | #endif // NFD_HAVE_PRIVILEGE_DROP_AND_ELEVATE |
Weiwei Liu | f5aee94 | 2016-03-19 07:00:42 +0000 | [diff] [blame] | 46 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 47 | namespace nfd::tests { |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 48 | |
Davide Pesavento | b7e72c3 | 2020-10-02 20:00:03 -0400 | [diff] [blame] | 49 | /** |
| 50 | * \brief Create an Interest |
Junxiao Shi | d23de8b | 2016-07-23 20:05:42 +0000 | [diff] [blame] | 51 | */ |
| 52 | shared_ptr<Interest> |
Junxiao Shi | 9d72785 | 2019-05-14 13:44:22 -0600 | [diff] [blame] | 53 | makeInterest(const Name& name, bool canBePrefix = false, |
Davide Pesavento | b7bfcb9 | 2022-05-22 23:55:23 -0400 | [diff] [blame] | 54 | std::optional<time::milliseconds> lifetime = std::nullopt, |
| 55 | std::optional<Interest::Nonce> nonce = std::nullopt); |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 56 | |
Davide Pesavento | b7e72c3 | 2020-10-02 20:00:03 -0400 | [diff] [blame] | 57 | /** |
| 58 | * \brief Create a Data with a null (i.e., empty) signature |
| 59 | * |
Davide Pesavento | 2135375 | 2020-11-20 00:43:44 -0500 | [diff] [blame] | 60 | * If a "real" signature is desired, use KeyChainFixture and sign again with `m_keyChain`. |
Junxiao Shi | d23de8b | 2016-07-23 20:05:42 +0000 | [diff] [blame] | 61 | */ |
| 62 | shared_ptr<Data> |
| 63 | makeData(const Name& name); |
| 64 | |
Davide Pesavento | b7e72c3 | 2020-10-02 20:00:03 -0400 | [diff] [blame] | 65 | /** |
| 66 | * \brief Add a null signature to \p data |
Junxiao Shi | d23de8b | 2016-07-23 20:05:42 +0000 | [diff] [blame] | 67 | */ |
| 68 | Data& |
| 69 | signData(Data& data); |
| 70 | |
Davide Pesavento | b7e72c3 | 2020-10-02 20:00:03 -0400 | [diff] [blame] | 71 | /** |
| 72 | * \brief Add a null signature to \p data |
Junxiao Shi | d23de8b | 2016-07-23 20:05:42 +0000 | [diff] [blame] | 73 | */ |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 74 | inline shared_ptr<Data> |
Junxiao Shi | 0355e9f | 2015-09-02 07:24:53 -0700 | [diff] [blame] | 75 | signData(shared_ptr<Data> data) |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 76 | { |
Junxiao Shi | d23de8b | 2016-07-23 20:05:42 +0000 | [diff] [blame] | 77 | signData(*data); |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 78 | return data; |
| 79 | } |
| 80 | |
Davide Pesavento | b7e72c3 | 2020-10-02 20:00:03 -0400 | [diff] [blame] | 81 | /** |
| 82 | * \brief Create a Nack |
Junxiao Shi | 9954007 | 2017-01-27 19:57:33 +0000 | [diff] [blame] | 83 | */ |
| 84 | lp::Nack |
| 85 | makeNack(Interest interest, lp::NackReason reason); |
| 86 | |
Davide Pesavento | b7e72c3 | 2020-10-02 20:00:03 -0400 | [diff] [blame] | 87 | /** |
| 88 | * \brief Replace a name component in a packet |
Davide Pesavento | d6ea0b1 | 2023-03-13 21:35:03 -0400 | [diff] [blame] | 89 | * \param[in,out] pkt the packet |
Davide Pesavento | b7e72c3 | 2020-10-02 20:00:03 -0400 | [diff] [blame] | 90 | * \param index the index of the name component to replace |
| 91 | * \param args arguments to name::Component constructor |
Junxiao Shi | d763127 | 2016-08-17 04:16:31 +0000 | [diff] [blame] | 92 | */ |
Davide Pesavento | 51cf75c | 2020-03-11 22:21:13 -0400 | [diff] [blame] | 93 | template<typename Packet, typename ...Args> |
Junxiao Shi | d763127 | 2016-08-17 04:16:31 +0000 | [diff] [blame] | 94 | void |
Davide Pesavento | 51cf75c | 2020-03-11 22:21:13 -0400 | [diff] [blame] | 95 | setNameComponent(Packet& pkt, ssize_t index, Args&& ...args) |
Junxiao Shi | d763127 | 2016-08-17 04:16:31 +0000 | [diff] [blame] | 96 | { |
Davide Pesavento | 51cf75c | 2020-03-11 22:21:13 -0400 | [diff] [blame] | 97 | Name name = pkt.getName(); |
| 98 | name.set(index, name::Component(std::forward<Args>(args)...)); |
| 99 | pkt.setName(name); |
Junxiao Shi | d763127 | 2016-08-17 04:16:31 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Davide Pesavento | b7e72c3 | 2020-10-02 20:00:03 -0400 | [diff] [blame] | 102 | /** |
| 103 | * \brief Create a prefix announcement without signing |
Junxiao Shi | d47cd63 | 2018-09-11 03:10:00 +0000 | [diff] [blame] | 104 | */ |
| 105 | ndn::PrefixAnnouncement |
| 106 | makePrefixAnn(const Name& announcedName, time::milliseconds expiration, |
Davide Pesavento | b7bfcb9 | 2022-05-22 23:55:23 -0400 | [diff] [blame] | 107 | std::optional<ndn::security::ValidityPeriod> validity = std::nullopt); |
Junxiao Shi | d47cd63 | 2018-09-11 03:10:00 +0000 | [diff] [blame] | 108 | |
Davide Pesavento | b7e72c3 | 2020-10-02 20:00:03 -0400 | [diff] [blame] | 109 | /** |
Davide Pesavento | b7e72c3 | 2020-10-02 20:00:03 -0400 | [diff] [blame] | 110 | * \brief Sign a prefix announcement |
Junxiao Shi | d47cd63 | 2018-09-11 03:10:00 +0000 | [diff] [blame] | 111 | */ |
| 112 | ndn::PrefixAnnouncement |
| 113 | signPrefixAnn(ndn::PrefixAnnouncement&& pa, ndn::KeyChain& keyChain, |
Eric Newberry | 1f237ab | 2020-05-28 14:10:07 -0700 | [diff] [blame] | 114 | const ndn::security::SigningInfo& si = ndn::security::SigningInfo(), |
Davide Pesavento | b7bfcb9 | 2022-05-22 23:55:23 -0400 | [diff] [blame] | 115 | std::optional<uint64_t> version = std::nullopt); |
Junxiao Shi | d47cd63 | 2018-09-11 03:10:00 +0000 | [diff] [blame] | 116 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 117 | } // namespace nfd::tests |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 118 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 119 | #endif // NFD_TESTS_TEST_COMMON_HPP |