Zhiyi Zhang | 8617a79 | 2017-01-17 16:45:56 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014-2017, 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, originally written as part of NFD (Named Data Networking Forwarding Daemon), |
| 12 | * is a part of ndncert, a certificate management system based on NDN. |
| 13 | * |
| 14 | * ndncert 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, either |
| 16 | * version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * ndncert is distributed in the hope that it will be useful, but WITHOUT ANY |
| 19 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 20 | * PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received copies of the GNU General Public License along with |
| 23 | * ndncert, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 24 | * |
| 25 | * See AUTHORS.md for complete list of ndncert authors and contributors. |
| 26 | */ |
| 27 | |
| 28 | #ifndef NDNCERT_TESTS_TEST_COMMON_HPP |
| 29 | #define NDNCERT_TESTS_TEST_COMMON_HPP |
| 30 | |
| 31 | #include "logging.hpp" |
| 32 | |
| 33 | #include "boost-test.hpp" |
| 34 | |
| 35 | #include <boost/asio/io_service.hpp> |
| 36 | #include <boost/filesystem.hpp> |
| 37 | #include <boost/filesystem/fstream.hpp> |
| 38 | |
| 39 | #include <ndn-cxx/name.hpp> |
| 40 | #include <ndn-cxx/data.hpp> |
| 41 | #include <ndn-cxx/lp/nack.hpp> |
| 42 | #include <ndn-cxx/util/time-unit-test-clock.hpp> |
| 43 | #include <ndn-cxx/util/string-helper.hpp> |
| 44 | |
| 45 | namespace ndn { |
| 46 | namespace ndncert { |
| 47 | namespace tests { |
| 48 | |
| 49 | /** \brief base test fixture |
| 50 | * |
| 51 | * Every test case should be based on this fixture, |
| 52 | * to have per test case io_service initialization. |
| 53 | */ |
| 54 | class BaseFixture |
| 55 | { |
| 56 | protected: |
| 57 | /** \brief reference to global io_service |
| 58 | */ |
| 59 | boost::asio::io_service m_io; |
| 60 | }; |
| 61 | |
| 62 | /** \brief a base test fixture that overrides steady clock and system clock |
| 63 | */ |
| 64 | class UnitTestTimeFixture : public virtual BaseFixture |
| 65 | { |
| 66 | protected: |
| 67 | UnitTestTimeFixture(); |
| 68 | |
| 69 | ~UnitTestTimeFixture(); |
| 70 | |
| 71 | /** \brief advance steady and system clocks |
| 72 | * |
| 73 | * Clocks are advanced in increments of \p tick for \p nTicks ticks. |
| 74 | * After each tick, global io_service is polled to process pending I/O events. |
| 75 | * |
| 76 | * Exceptions thrown during I/O events are propagated to the caller. |
| 77 | * Clock advancing would stop in case of an exception. |
| 78 | */ |
| 79 | void |
| 80 | advanceClocks(const time::nanoseconds& tick, size_t nTicks = 1); |
| 81 | |
| 82 | /** \brief advance steady and system clocks |
| 83 | * |
| 84 | * Clocks are advanced in increments of \p tick for \p total time. |
| 85 | * The last increment might be shorter than \p tick. |
| 86 | * After each tick, global io_service is polled to process pending I/O events. |
| 87 | * |
| 88 | * Exceptions thrown during I/O events are propagated to the caller. |
| 89 | * Clock advancing would stop in case of an exception. |
| 90 | */ |
| 91 | void |
| 92 | advanceClocks(const time::nanoseconds& tick, const time::nanoseconds& total); |
| 93 | |
| 94 | protected: |
| 95 | shared_ptr<time::UnitTestSteadyClock> steadyClock; |
| 96 | shared_ptr<time::UnitTestSystemClock> systemClock; |
| 97 | |
| 98 | friend class LimitedIo; |
| 99 | }; |
| 100 | |
| 101 | /** \brief create an Interest |
| 102 | * \param name Interest name |
| 103 | * \param nonce if non-zero, set Nonce to this value |
| 104 | * (useful for creating Nack with same Nonce) |
| 105 | */ |
| 106 | shared_ptr<Interest> |
| 107 | makeInterest(const Name& name, uint32_t nonce = 0); |
| 108 | |
| 109 | /** \brief create a Data with fake signature |
| 110 | * \note Data may be modified afterwards without losing the fake signature. |
| 111 | * If a real signature is desired, sign again with KeyChain. |
| 112 | */ |
| 113 | shared_ptr<Data> |
| 114 | makeData(const Name& name); |
| 115 | |
| 116 | /** \brief add a fake signature to Data |
| 117 | */ |
| 118 | Data& |
| 119 | signData(Data& data); |
| 120 | |
| 121 | /** \brief add a fake signature to Data |
| 122 | */ |
| 123 | inline shared_ptr<Data> |
| 124 | signData(shared_ptr<Data> data) |
| 125 | { |
| 126 | signData(*data); |
| 127 | return data; |
| 128 | } |
| 129 | |
| 130 | /** \brief create a Link object with fake signature |
| 131 | * \note Link may be modified afterwards without losing the fake signature. |
| 132 | * If a real signature is desired, sign again with KeyChain. |
| 133 | */ |
| 134 | shared_ptr<Link> |
| 135 | makeLink(const Name& name, std::initializer_list<std::pair<uint32_t, Name>> delegations); |
| 136 | |
| 137 | /** \brief create a Nack |
| 138 | * \param name Interest name |
| 139 | * \param nonce Interest nonce |
| 140 | * \param reason Nack reason |
| 141 | */ |
| 142 | lp::Nack |
| 143 | makeNack(const Name& name, uint32_t nonce, lp::NackReason reason); |
| 144 | |
| 145 | /** \brief replace a name component |
| 146 | * \param[inout] name name |
| 147 | * \param index name component index |
| 148 | * \param a arguments to name::Component constructor |
| 149 | */ |
| 150 | template<typename...A> |
| 151 | void |
| 152 | setNameComponent(Name& name, ssize_t index, const A& ...a) |
| 153 | { |
| 154 | Name name2 = name.getPrefix(index); |
| 155 | name2.append(name::Component(a...)); |
| 156 | name2.append(name.getSubName(name2.size())); |
| 157 | name = name2; |
| 158 | } |
| 159 | |
| 160 | template<typename Packet, typename...A> |
| 161 | void |
| 162 | setNameComponent(Packet& packet, ssize_t index, const A& ...a) |
| 163 | { |
| 164 | Name name = packet.getName(); |
| 165 | setNameComponent(name, index, a...); |
| 166 | packet.setName(name); |
| 167 | } |
| 168 | |
| 169 | /** \brief convert file to digest |
| 170 | */ |
| 171 | ndn::ConstBufferPtr |
| 172 | digestFromFile(const boost::filesystem::path& filename); |
| 173 | |
| 174 | } // namespace tests |
| 175 | } // namespace ndncert |
| 176 | } // namespace ndn |
| 177 | |
| 178 | #include "identity-management-fixture.hpp" |
| 179 | |
| 180 | #endif // NDNCERT_TESTS_TEST_COMMON_HPP |