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