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