blob: e123b5e9c77692922256e7d8abd120e44102a090 [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 Pesavento3dade002019-03-19 11:29:56 -06003 * Copyright (c) 2014-2019, 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
Weiwei Liuf5aee942016-03-19 07:00:42 +000034#ifdef HAVE_PRIVILEGE_DROP_AND_ELEVATE
35#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()
45#endif // HAVE_PRIVILEGE_DROP_AND_ELEVATE
46
Junxiao Shid9ee45c2014-02-27 15:38:11 -070047namespace nfd {
48namespace tests {
49
Junxiao Shid23de8b2016-07-23 20:05:42 +000050/** \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,
54 optional<time::milliseconds> lifetime = nullopt,
55 optional<uint32_t> nonce = nullopt);
Junxiao Shif3c07812014-03-11 21:48:49 -070056
Junxiao Shid23de8b2016-07-23 20:05:42 +000057/** \brief create a Data with fake signature
58 * \note Data may be modified afterwards without losing the fake signature.
59 * If a real signature is desired, sign again with KeyChain.
60 */
61shared_ptr<Data>
62makeData(const Name& name);
63
64/** \brief add a fake signature to Data
65 */
66Data&
67signData(Data& data);
68
69/** \brief add a fake signature to Data
70 */
Junxiao Shif3c07812014-03-11 21:48:49 -070071inline shared_ptr<Data>
Junxiao Shi0355e9f2015-09-02 07:24:53 -070072signData(shared_ptr<Data> data)
Junxiao Shif3c07812014-03-11 21:48:49 -070073{
Junxiao Shid23de8b2016-07-23 20:05:42 +000074 signData(*data);
Junxiao Shif3c07812014-03-11 21:48:49 -070075 return data;
76}
77
Junxiao Shid23de8b2016-07-23 20:05:42 +000078/** \brief create a Nack
Junxiao Shi99540072017-01-27 19:57:33 +000079 * \param interest Interest
80 * \param reason Nack reason
81 */
82lp::Nack
83makeNack(Interest interest, lp::NackReason reason);
84
Junxiao Shid7631272016-08-17 04:16:31 +000085/** \brief replace a name component
86 * \param[inout] name name
87 * \param index name component index
88 * \param a arguments to name::Component constructor
89 */
Davide Pesavento3dade002019-03-19 11:29:56 -060090template<typename... A>
Junxiao Shid7631272016-08-17 04:16:31 +000091void
Davide Pesavento3dade002019-03-19 11:29:56 -060092setNameComponent(Name& name, ssize_t index, A&&... a)
Junxiao Shid7631272016-08-17 04:16:31 +000093{
94 Name name2 = name.getPrefix(index);
Davide Pesavento3dade002019-03-19 11:29:56 -060095 name2.append(name::Component(std::forward<A>(a)...));
Junxiao Shid7631272016-08-17 04:16:31 +000096 name2.append(name.getSubName(name2.size()));
Davide Pesavento3dade002019-03-19 11:29:56 -060097 name = std::move(name2);
Junxiao Shid7631272016-08-17 04:16:31 +000098}
99
Davide Pesavento3dade002019-03-19 11:29:56 -0600100template<typename Packet, typename... A>
Junxiao Shid7631272016-08-17 04:16:31 +0000101void
Davide Pesavento3dade002019-03-19 11:29:56 -0600102setNameComponent(Packet& packet, ssize_t index, A&&... a)
Junxiao Shid7631272016-08-17 04:16:31 +0000103{
104 Name name = packet.getName();
Davide Pesavento3dade002019-03-19 11:29:56 -0600105 setNameComponent(name, index, std::forward<A>(a)...);
Junxiao Shid7631272016-08-17 04:16:31 +0000106 packet.setName(name);
107}
108
Junxiao Shid47cd632018-09-11 03:10:00 +0000109/** \brief create a prefix announcement without signing
110 */
111ndn::PrefixAnnouncement
112makePrefixAnn(const Name& announcedName, time::milliseconds expiration,
113 optional<ndn::security::ValidityPeriod> validity = nullopt);
114
115/** \brief create a prefix announcement without signing
116 * \param announcedName announced name
117 * \param expiration expiration period
118 * \param validityFromNow validity period, relative from now
119 */
120ndn::PrefixAnnouncement
121makePrefixAnn(const Name& announcedName, time::milliseconds expiration,
122 std::pair<time::seconds, time::seconds> validityFromNow);
123
124/** \brief sign a prefix announcement
125 */
126ndn::PrefixAnnouncement
127signPrefixAnn(ndn::PrefixAnnouncement&& pa, ndn::KeyChain& keyChain,
128 const ndn::security::SigningInfo& si = ndn::KeyChain::getDefaultSigningInfo(),
129 optional<uint64_t> version = nullopt);
130
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700131} // namespace tests
132} // namespace nfd
133
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700134#endif // NFD_TESTS_TEST_COMMON_HPP