blob: 0986f11de327019a51e1b0104dd73a7288d88949 [file] [log] [blame]
Junxiao Shid9ee45c2014-02-27 15:38:11 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shifc2e13d2017-07-25 02:08:48 +00002/*
Davide Pesaventob7bfcb92022-05-22 23:55:23 -04003 * Copyright (c) 2014-2022, Regents of the University of California,
Yukai Tu0a49d342015-09-13 12:54:22 +08004 * 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 Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
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 Afanasyevc026d252014-06-16 11:14:15 -070024 */
Junxiao Shid9ee45c2014-02-27 15:38:11 -070025
Alexander Afanasyev613e2a92014-04-15 13:36:58 -070026#ifndef NFD_TESTS_TEST_COMMON_HPP
27#define NFD_TESTS_TEST_COMMON_HPP
Junxiao Shid9ee45c2014-02-27 15:38:11 -070028
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040029#include "core/common.hpp"
30#include "tests/boost-test.hpp"
Alexander Afanasyev8552a382014-05-15 20:13:42 -070031
Junxiao Shid47cd632018-09-11 03:10:00 +000032#include <ndn-cxx/prefix-announcement.hpp>
Junxiao Shid9ee45c2014-02-27 15:38:11 -070033
Davide Pesavento264af772021-02-09 21:48:24 -050034#ifdef NFD_HAVE_PRIVILEGE_DROP_AND_ELEVATE
Weiwei Liuf5aee942016-03-19 07:00:42 +000035#include <unistd.h>
36#define SKIP_IF_NOT_SUPERUSER() \
37 do { \
38 if (::geteuid() != 0) { \
Davide Pesavento231ddd72016-09-02 22:20:00 +000039 BOOST_WARN_MESSAGE(false, "skipping assertions that require superuser privileges"); \
Weiwei Liuf5aee942016-03-19 07:00:42 +000040 return; \
41 } \
42 } while (false)
43#else
44#define SKIP_IF_NOT_SUPERUSER()
Davide Pesavento264af772021-02-09 21:48:24 -050045#endif // NFD_HAVE_PRIVILEGE_DROP_AND_ELEVATE
Weiwei Liuf5aee942016-03-19 07:00:42 +000046
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040047namespace nfd::tests {
Junxiao Shid9ee45c2014-02-27 15:38:11 -070048
Davide Pesaventob7e72c32020-10-02 20:00:03 -040049/**
50 * \brief Create an Interest
Junxiao Shid23de8b2016-07-23 20:05:42 +000051 */
52shared_ptr<Interest>
Junxiao Shi9d727852019-05-14 13:44:22 -060053makeInterest(const Name& name, bool canBePrefix = false,
Davide Pesaventob7bfcb92022-05-22 23:55:23 -040054 std::optional<time::milliseconds> lifetime = std::nullopt,
55 std::optional<Interest::Nonce> nonce = std::nullopt);
Junxiao Shif3c07812014-03-11 21:48:49 -070056
Davide Pesaventob7e72c32020-10-02 20:00:03 -040057/**
58 * \brief Create a Data with a null (i.e., empty) signature
59 *
Davide Pesavento21353752020-11-20 00:43:44 -050060 * If a "real" signature is desired, use KeyChainFixture and sign again with `m_keyChain`.
Junxiao Shid23de8b2016-07-23 20:05:42 +000061 */
62shared_ptr<Data>
63makeData(const Name& name);
64
Davide Pesaventob7e72c32020-10-02 20:00:03 -040065/**
66 * \brief Add a null signature to \p data
Junxiao Shid23de8b2016-07-23 20:05:42 +000067 */
68Data&
69signData(Data& data);
70
Davide Pesaventob7e72c32020-10-02 20:00:03 -040071/**
72 * \brief Add a null signature to \p data
Junxiao Shid23de8b2016-07-23 20:05:42 +000073 */
Junxiao Shif3c07812014-03-11 21:48:49 -070074inline shared_ptr<Data>
Junxiao Shi0355e9f2015-09-02 07:24:53 -070075signData(shared_ptr<Data> data)
Junxiao Shif3c07812014-03-11 21:48:49 -070076{
Junxiao Shid23de8b2016-07-23 20:05:42 +000077 signData(*data);
Junxiao Shif3c07812014-03-11 21:48:49 -070078 return data;
79}
80
Davide Pesaventob7e72c32020-10-02 20:00:03 -040081/**
82 * \brief Create a Nack
Junxiao Shi99540072017-01-27 19:57:33 +000083 */
84lp::Nack
85makeNack(Interest interest, lp::NackReason reason);
86
Davide Pesaventob7e72c32020-10-02 20:00:03 -040087/**
88 * \brief Replace a name component in a packet
89 * \param[inout] pkt the packet
90 * \param index the index of the name component to replace
91 * \param args arguments to name::Component constructor
Junxiao Shid7631272016-08-17 04:16:31 +000092 */
Davide Pesavento51cf75c2020-03-11 22:21:13 -040093template<typename Packet, typename ...Args>
Junxiao Shid7631272016-08-17 04:16:31 +000094void
Davide Pesavento51cf75c2020-03-11 22:21:13 -040095setNameComponent(Packet& pkt, ssize_t index, Args&& ...args)
Junxiao Shid7631272016-08-17 04:16:31 +000096{
Davide Pesavento51cf75c2020-03-11 22:21:13 -040097 Name name = pkt.getName();
98 name.set(index, name::Component(std::forward<Args>(args)...));
99 pkt.setName(name);
Junxiao Shid7631272016-08-17 04:16:31 +0000100}
101
Davide Pesaventob7e72c32020-10-02 20:00:03 -0400102/**
103 * \brief Create a prefix announcement without signing
Junxiao Shid47cd632018-09-11 03:10:00 +0000104 */
105ndn::PrefixAnnouncement
106makePrefixAnn(const Name& announcedName, time::milliseconds expiration,
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400107 std::optional<ndn::security::ValidityPeriod> validity = std::nullopt);
Junxiao Shid47cd632018-09-11 03:10:00 +0000108
Davide Pesaventob7e72c32020-10-02 20:00:03 -0400109/**
110 * \brief Create a prefix announcement without signing
111 * \param announcedName announced name
112 * \param expiration expiration period
113 * \param validityFromNow validity period, relative from now
Junxiao Shid47cd632018-09-11 03:10:00 +0000114 */
115ndn::PrefixAnnouncement
116makePrefixAnn(const Name& announcedName, time::milliseconds expiration,
117 std::pair<time::seconds, time::seconds> validityFromNow);
118
Davide Pesaventob7e72c32020-10-02 20:00:03 -0400119/**
120 * \brief Sign a prefix announcement
Junxiao Shid47cd632018-09-11 03:10:00 +0000121 */
122ndn::PrefixAnnouncement
123signPrefixAnn(ndn::PrefixAnnouncement&& pa, ndn::KeyChain& keyChain,
Eric Newberry1f237ab2020-05-28 14:10:07 -0700124 const ndn::security::SigningInfo& si = ndn::security::SigningInfo(),
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400125 std::optional<uint64_t> version = std::nullopt);
Junxiao Shid47cd632018-09-11 03:10:00 +0000126
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400127} // namespace nfd::tests
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700128
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700129#endif // NFD_TESTS_TEST_COMMON_HPP