blob: f33f6bb4baaf433d3c9dd123e072a96e5d922cc2 [file] [log] [blame]
Junxiao Shi1f481fa2017-01-26 15:14:43 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shib347b7f2017-07-23 14:01:58 +00002/*
Davide Pesavento14e71f02019-03-28 17:35:25 -04003 * Copyright (c) 2014-2019, Regents of the University of California,
Junxiao Shi1f481fa2017-01-26 15:14:43 +00004 * 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 Pesavento14e71f02019-03-28 17:35:25 -040031
Junxiao Shi1f481fa2017-01-26 15:14:43 +000032#include <ndn-cxx/security/validator-null.hpp>
33
34namespace nfd {
35namespace tools {
36namespace nfdc {
37namespace tests {
38
39using ndn::Face;
40using ndn::KeyChain;
Junxiao Shib347b7f2017-07-23 14:01:58 +000041using ndn::security::v2::Validator;
42using ndn::security::v2::ValidatorNull;
Junxiao Shi1f481fa2017-01-26 15:14:43 +000043using boost::test_tools::output_test_stream;
44
45class MakeValidatorNull
46{
47public:
48 unique_ptr<ValidatorNull>
49 operator()(Face&, KeyChain&) const
50 {
51 return make_unique<ValidatorNull>();
Davide Pesaventoc0a5a392017-08-19 16:49:30 -040052 }
Junxiao Shi1f481fa2017-01-26 15:14:43 +000053};
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 */
61template<typename M, typename MakeValidator = MakeValidatorNull>
62class StatusFixture : public MockNfdMgmtFixture
63{
64protected:
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
74protected: // 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 Pesaventoac238f22017-09-12 15:19:40 -040085 module.fetchStatus(controller,
86 [this] { ++nFetchStatusSuccess; },
87 [] (uint32_t code, const std::string& reason) {
Junxiao Shi1f481fa2017-01-26 15:14:43 +000088 BOOST_FAIL("fetchStatus failure " << code << " " << reason);
89 },
90 CommandOptions());
Davide Pesavento14e71f02019-03-28 17:35:25 -040091 this->advanceClocks(1_ms);
Junxiao Shi1f481fa2017-01-26 15:14:43 +000092 }
93
94 /** \brief prepare status output as XML and text
95 * \pre sendDataset has been invoked
96 */
97 void
98 prepareStatusOutput()
99 {
Davide Pesavento14e71f02019-03-28 17:35:25 -0400100 this->advanceClocks(1_ms);
Junxiao Shi1f481fa2017-01-26 15:14:43 +0000101 BOOST_REQUIRE_EQUAL(nFetchStatusSuccess, 1);
102
103 statusXml.str("");
104 module.formatStatusXml(statusXml);
105 statusText.str("");
106 module.formatStatusText(statusText);
107 }
108
109protected:
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 */
135inline std::string
136stripXmlSpaces(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