Alexander Afanasyev | fde570c | 2016-12-19 16:02:55 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014-2016, Regents of the University of California, |
| 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. |
| 10 | * |
| 11 | * This file is part of NDNS (Named Data Networking Domain Name Service) and is |
| 12 | * based on the code written as part of NFD (Named Data Networking Daemon). |
| 13 | * See AUTHORS.md for complete list of NDNS authors and contributors. |
| 14 | * |
| 15 | * NDNS is free software: you can redistribute it and/or modify it under the terms |
| 16 | * of the GNU General Public License as published by the Free Software Foundation, |
| 17 | * either version 3 of the License, or (at your option) any later version. |
| 18 | * |
| 19 | * NDNS is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 20 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 21 | * PURPOSE. See the GNU General Public License for more details. |
| 22 | * |
| 23 | * You should have received a copy of the GNU General Public License along with |
| 24 | * NDNS, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 25 | */ |
| 26 | |
| 27 | #ifndef NDNS_TESTS_TEST_COMMON_HPP |
| 28 | #define NDNS_TESTS_TEST_COMMON_HPP |
| 29 | |
| 30 | #include "logger.hpp" |
| 31 | |
| 32 | #include <ndn-cxx/name.hpp> |
| 33 | #include <ndn-cxx/data.hpp> |
| 34 | #include <ndn-cxx/interest.hpp> |
| 35 | #include <ndn-cxx/link.hpp> |
| 36 | #include <ndn-cxx/lp/nack.hpp> |
| 37 | #include <ndn-cxx/util/dummy-client-face.hpp> |
| 38 | #include <ndn-cxx/security/key-chain.hpp> |
| 39 | |
| 40 | #include <boost/version.hpp> |
| 41 | #include <boost/asio.hpp> |
| 42 | #include <boost/filesystem.hpp> |
| 43 | |
| 44 | #include <fstream> |
| 45 | |
| 46 | #include "boost-test.hpp" |
| 47 | #include "unit-test-common-fixtures.hpp" |
| 48 | #include "identity-management-fixture.hpp" |
| 49 | |
| 50 | namespace ndn { |
| 51 | namespace ndns { |
| 52 | namespace tests { |
| 53 | |
| 54 | /** \brief create an Interest |
| 55 | * \param name Interest name |
| 56 | * \param nonce if non-zero, set Nonce to this value |
| 57 | * (useful for creating Nack with same Nonce) |
| 58 | */ |
| 59 | shared_ptr<Interest> |
| 60 | makeInterest(const Name& name, uint32_t nonce = 0); |
| 61 | |
| 62 | /** \brief create a Data with fake signature |
| 63 | * \note Data may be modified afterwards without losing the fake signature. |
| 64 | * If a real signature is desired, sign again with KeyChain. |
| 65 | */ |
| 66 | shared_ptr<Data> |
| 67 | makeData(const Name& name); |
| 68 | |
| 69 | /** \brief add a fake signature to Data |
| 70 | */ |
| 71 | Data& |
| 72 | signData(Data& data); |
| 73 | |
| 74 | /** \brief add a fake signature to Data |
| 75 | */ |
| 76 | inline shared_ptr<Data> |
| 77 | signData(shared_ptr<Data> data) |
| 78 | { |
| 79 | signData(*data); |
| 80 | return data; |
| 81 | } |
| 82 | |
| 83 | /** \brief create a Link object with fake signature |
| 84 | * \note Link may be modified afterwards without losing the fake signature. |
| 85 | * If a real signature is desired, sign again with KeyChain. |
| 86 | */ |
| 87 | shared_ptr<Link> |
| 88 | makeLink(const Name& name, std::initializer_list<std::pair<uint32_t, Name>> delegations); |
| 89 | |
| 90 | /** \brief create a Nack |
| 91 | * \param name Interest name |
| 92 | * \param nonce Interest nonce |
| 93 | * \param reason Nack reason |
| 94 | */ |
| 95 | lp::Nack |
| 96 | makeNack(const Name& name, uint32_t nonce, lp::NackReason reason); |
| 97 | |
| 98 | /** \brief replace a name component |
| 99 | * \param[inout] name name |
| 100 | * \param index name component index |
| 101 | * \param a arguments to name::Component constructor |
| 102 | */ |
| 103 | template<typename...A> |
| 104 | void |
| 105 | setNameComponent(Name& name, ssize_t index, const A& ...a) |
| 106 | { |
| 107 | Name name2 = name.getPrefix(index); |
| 108 | name2.append(name::Component(a...)); |
| 109 | name2.append(name.getSubName(name2.size())); |
| 110 | name = name2; |
| 111 | } |
| 112 | |
| 113 | template<typename Packet, typename...A> |
| 114 | void |
| 115 | setNameComponent(Packet& packet, ssize_t index, const A& ...a) |
| 116 | { |
| 117 | Name name = packet.getName(); |
| 118 | setNameComponent(name, index, a...); |
| 119 | packet.setName(name); |
| 120 | } |
| 121 | |
| 122 | } // namespace tests |
| 123 | } // namespace ndns |
| 124 | } // namespace ndn |
| 125 | |
| 126 | #endif // NFD_TESTS_TEST_COMMON_HPP |