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; |
Davide Pesavento | ac238f2 | 2017-09-12 15:19:40 -0400 | [diff] [blame^] | 84 | module.fetchStatus(controller, |
| 85 | [this] { ++nFetchStatusSuccess; }, |
| 86 | [] (uint32_t code, const std::string& reason) { |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 87 | BOOST_FAIL("fetchStatus failure " << code << " " << reason); |
| 88 | }, |
| 89 | CommandOptions()); |
| 90 | this->advanceClocks(time::milliseconds(1)); |
| 91 | } |
| 92 | |
| 93 | /** \brief prepare status output as XML and text |
| 94 | * \pre sendDataset has been invoked |
| 95 | */ |
| 96 | void |
| 97 | prepareStatusOutput() |
| 98 | { |
| 99 | this->advanceClocks(time::milliseconds(1)); |
| 100 | BOOST_REQUIRE_EQUAL(nFetchStatusSuccess, 1); |
| 101 | |
| 102 | statusXml.str(""); |
| 103 | module.formatStatusXml(statusXml); |
| 104 | statusText.str(""); |
| 105 | module.formatStatusText(statusText); |
| 106 | } |
| 107 | |
| 108 | protected: |
| 109 | ValidatorUniquePtr validator; |
| 110 | Controller controller; |
| 111 | |
| 112 | M module; |
| 113 | |
| 114 | int nFetchStatusSuccess; |
| 115 | output_test_stream statusXml; |
| 116 | output_test_stream statusText; |
| 117 | }; |
| 118 | |
| 119 | /** \brief strips leading spaces on every line in expected XML |
| 120 | * |
| 121 | * This allows expected XML to be written as: |
| 122 | * \code |
| 123 | * const std::string STATUS_XML = stripXmlSpaces(R"XML( |
| 124 | * <rootElement> |
| 125 | * <element>value</element> |
| 126 | * </rootElement> |
| 127 | * )XML"); |
| 128 | * \endcode |
| 129 | * And \p STATUS_XML would be assigned: |
| 130 | * \code |
| 131 | * "<rootElement><element>value</element></rootElement>" |
| 132 | * \endcode |
| 133 | */ |
| 134 | inline std::string |
| 135 | stripXmlSpaces(const std::string& xml) |
| 136 | { |
| 137 | std::string s; |
| 138 | bool isSkipping = true; |
| 139 | std::copy_if(xml.begin(), xml.end(), std::back_inserter(s), |
| 140 | [&isSkipping] (char ch) { |
| 141 | if (ch == '\n') { |
| 142 | isSkipping = true; |
| 143 | } |
| 144 | else if (ch != ' ') { |
| 145 | isSkipping = false; |
| 146 | } |
| 147 | return !isSkipping; |
| 148 | }); |
| 149 | return s; |
| 150 | } |
| 151 | |
| 152 | } // namespace tests |
| 153 | } // namespace nfdc |
| 154 | } // namespace tools |
| 155 | } // namespace nfd |
| 156 | |
| 157 | #endif // NFD_TESTS_TOOLS_NFDC_STATUS_FIXTURE_HPP |