Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | b347b7f | 2017-07-23 14:01:58 +0000 | [diff] [blame] | 2 | /* |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2019, Regents of the University of California, |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [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. |
| 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/>. |
| 24 | */ |
| 25 | |
| 26 | #ifndef NFD_TESTS_TOOLS_NFDC_STATUS_FIXTURE_HPP |
| 27 | #define NFD_TESTS_TOOLS_NFDC_STATUS_FIXTURE_HPP |
| 28 | |
| 29 | #include "mock-nfd-mgmt-fixture.hpp" |
| 30 | #include "nfdc/module.hpp" |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 31 | |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 32 | #include <ndn-cxx/security/validator-null.hpp> |
| 33 | |
Davide Pesavento | cf9e1c7 | 2019-12-22 17:56:07 -0500 | [diff] [blame] | 34 | #if BOOST_VERSION >= 105900 |
| 35 | #include <boost/test/tools/output_test_stream.hpp> |
| 36 | #else |
| 37 | #include <boost/test/output_test_stream.hpp> |
| 38 | #endif |
| 39 | |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 40 | namespace nfd { |
| 41 | namespace tools { |
| 42 | namespace nfdc { |
| 43 | namespace tests { |
| 44 | |
| 45 | using ndn::Face; |
| 46 | using ndn::KeyChain; |
Junxiao Shi | b347b7f | 2017-07-23 14:01:58 +0000 | [diff] [blame] | 47 | using ndn::security::v2::Validator; |
| 48 | using ndn::security::v2::ValidatorNull; |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 49 | using boost::test_tools::output_test_stream; |
| 50 | |
| 51 | class MakeValidatorNull |
| 52 | { |
| 53 | public: |
| 54 | unique_ptr<ValidatorNull> |
| 55 | operator()(Face&, KeyChain&) const |
| 56 | { |
| 57 | return make_unique<ValidatorNull>(); |
Davide Pesavento | c0a5a39 | 2017-08-19 16:49:30 -0400 | [diff] [blame] | 58 | } |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 59 | }; |
| 60 | |
| 61 | /** \brief fixture to test status fetching routines in a \p Module |
| 62 | * \tparam M a subclass of \p Module |
| 63 | * \tparam MakeValidator a callable to make a Validator for use in \p controller; |
| 64 | * MakeValidator()(Face&, KeyChain&) should return a unique_ptr |
| 65 | * to Validator or its subclass |
| 66 | */ |
| 67 | template<typename M, typename MakeValidator = MakeValidatorNull> |
| 68 | class StatusFixture : public MockNfdMgmtFixture |
| 69 | { |
| 70 | protected: |
| 71 | using ValidatorUniquePtr = typename std::result_of<MakeValidator(Face&, KeyChain&)>::type; |
| 72 | |
| 73 | StatusFixture() |
| 74 | : validator(MakeValidator()(face, m_keyChain)) |
| 75 | , controller(face, m_keyChain, *validator) |
| 76 | , nFetchStatusSuccess(0) |
| 77 | { |
| 78 | } |
| 79 | |
| 80 | protected: // status fetching |
| 81 | /** \brief start fetching status |
| 82 | * |
| 83 | * A test case should call \p fetchStatus, \p sendDataset, and \p prepareStatusOutput |
| 84 | * in this order, and then check \p statusXml and \p statusText contain the correct outputs. |
| 85 | * No advanceClocks is needed in between, as they are handled by the fixture. |
| 86 | */ |
| 87 | void |
| 88 | fetchStatus() |
| 89 | { |
| 90 | nFetchStatusSuccess = 0; |
Davide Pesavento | ac238f2 | 2017-09-12 15:19:40 -0400 | [diff] [blame] | 91 | module.fetchStatus(controller, |
| 92 | [this] { ++nFetchStatusSuccess; }, |
| 93 | [] (uint32_t code, const std::string& reason) { |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 94 | BOOST_FAIL("fetchStatus failure " << code << " " << reason); |
| 95 | }, |
| 96 | CommandOptions()); |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 97 | this->advanceClocks(1_ms); |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | /** \brief prepare status output as XML and text |
| 101 | * \pre sendDataset has been invoked |
| 102 | */ |
| 103 | void |
| 104 | prepareStatusOutput() |
| 105 | { |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 106 | this->advanceClocks(1_ms); |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 107 | BOOST_REQUIRE_EQUAL(nFetchStatusSuccess, 1); |
| 108 | |
| 109 | statusXml.str(""); |
| 110 | module.formatStatusXml(statusXml); |
| 111 | statusText.str(""); |
| 112 | module.formatStatusText(statusText); |
| 113 | } |
| 114 | |
| 115 | protected: |
| 116 | ValidatorUniquePtr validator; |
| 117 | Controller controller; |
| 118 | |
| 119 | M module; |
| 120 | |
| 121 | int nFetchStatusSuccess; |
| 122 | output_test_stream statusXml; |
| 123 | output_test_stream statusText; |
| 124 | }; |
| 125 | |
| 126 | /** \brief strips leading spaces on every line in expected XML |
| 127 | * |
| 128 | * This allows expected XML to be written as: |
| 129 | * \code |
| 130 | * const std::string STATUS_XML = stripXmlSpaces(R"XML( |
| 131 | * <rootElement> |
| 132 | * <element>value</element> |
| 133 | * </rootElement> |
| 134 | * )XML"); |
| 135 | * \endcode |
| 136 | * And \p STATUS_XML would be assigned: |
| 137 | * \code |
| 138 | * "<rootElement><element>value</element></rootElement>" |
| 139 | * \endcode |
| 140 | */ |
| 141 | inline std::string |
| 142 | stripXmlSpaces(const std::string& xml) |
| 143 | { |
| 144 | std::string s; |
| 145 | bool isSkipping = true; |
| 146 | std::copy_if(xml.begin(), xml.end(), std::back_inserter(s), |
| 147 | [&isSkipping] (char ch) { |
| 148 | if (ch == '\n') { |
| 149 | isSkipping = true; |
| 150 | } |
| 151 | else if (ch != ' ') { |
| 152 | isSkipping = false; |
| 153 | } |
| 154 | return !isSkipping; |
| 155 | }); |
| 156 | return s; |
| 157 | } |
| 158 | |
| 159 | } // namespace tests |
| 160 | } // namespace nfdc |
| 161 | } // namespace tools |
| 162 | } // namespace nfd |
| 163 | |
| 164 | #endif // NFD_TESTS_TOOLS_NFDC_STATUS_FIXTURE_HPP |