Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | c542f63 | 2017-07-18 14:20:32 +0000 | [diff] [blame] | 2 | /* |
Davide Pesavento | 3c34ec1 | 2021-03-28 21:50:06 -0400 | [diff] [blame^] | 3 | * Copyright (c) 2013-2021 Regents of the University of California. |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 4 | * |
Alexander Afanasyev | 80b68e1 | 2015-09-17 17:01:04 -0700 | [diff] [blame] | 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | 80b68e1 | 2015-09-17 17:01:04 -0700 | [diff] [blame] | 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 10 | * |
Alexander Afanasyev | 80b68e1 | 2015-09-17 17:01:04 -0700 | [diff] [blame] | 11 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 14 | * |
Alexander Afanasyev | 80b68e1 | 2015-09-17 17:01:04 -0700 | [diff] [blame] | 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 20 | */ |
| 21 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 22 | #include "ndn-cxx/mgmt/dispatcher.hpp" |
| 23 | #include "ndn-cxx/mgmt/nfd/control-parameters.hpp" |
| 24 | #include "ndn-cxx/util/dummy-client-face.hpp" |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 25 | |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 26 | #include "tests/test-common.hpp" |
| 27 | #include "tests/unit/io-key-chain-fixture.hpp" |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 28 | |
| 29 | namespace ndn { |
| 30 | namespace mgmt { |
| 31 | namespace tests { |
| 32 | |
| 33 | using namespace ndn::tests; |
| 34 | |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 35 | class DispatcherFixture : public IoKeyChainFixture |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 36 | { |
| 37 | public: |
| 38 | DispatcherFixture() |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 39 | : face(m_io, m_keyChain, {true, true}) |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 40 | , dispatcher(face, m_keyChain, security::SigningInfo()) |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 41 | , storage(dispatcher.m_storage) |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 42 | { |
| 43 | } |
| 44 | |
| 45 | public: |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 46 | util::DummyClientFace face; |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 47 | mgmt::Dispatcher dispatcher; |
Junxiao Shi | c542f63 | 2017-07-18 14:20:32 +0000 | [diff] [blame] | 48 | InMemoryStorageFifo& storage; |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 49 | }; |
| 50 | |
| 51 | class VoidParameters : public mgmt::ControlParameters |
| 52 | { |
| 53 | public: |
| 54 | explicit |
| 55 | VoidParameters(const Block& wire) |
| 56 | { |
| 57 | wireDecode(wire); |
| 58 | } |
| 59 | |
Junxiao Shi | d97c953 | 2017-04-27 16:17:04 +0000 | [diff] [blame] | 60 | Block |
Davide Pesavento | aa82eb6 | 2016-04-22 19:08:40 +0200 | [diff] [blame] | 61 | wireEncode() const final |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 62 | { |
| 63 | return Block(128); |
| 64 | } |
| 65 | |
Junxiao Shi | d97c953 | 2017-04-27 16:17:04 +0000 | [diff] [blame] | 66 | void |
Davide Pesavento | aa82eb6 | 2016-04-22 19:08:40 +0200 | [diff] [blame] | 67 | wireDecode(const Block& wire) final |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 68 | { |
| 69 | if (wire.type() != 128) |
Davide Pesavento | 923ba44 | 2019-02-12 22:00:38 -0500 | [diff] [blame] | 70 | NDN_THROW(tlv::Error("Expecting TLV type 128")); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 71 | } |
| 72 | }; |
| 73 | |
| 74 | static Authorization |
| 75 | makeTestAuthorization() |
| 76 | { |
Davide Pesavento | 3c34ec1 | 2021-03-28 21:50:06 -0400 | [diff] [blame^] | 77 | return [] (const Name&, const Interest& interest, const ControlParameters*, |
| 78 | AcceptContinuation accept, RejectContinuation reject) { |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 79 | if (interest.getName()[-1] == name::Component("valid")) { |
| 80 | accept(""); |
| 81 | } |
| 82 | else { |
| 83 | if (interest.getName()[-1] == name::Component("silent")) { |
| 84 | reject(RejectReply::SILENT); |
| 85 | } |
| 86 | else { |
| 87 | reject(RejectReply::STATUS403); |
| 88 | } |
| 89 | } |
| 90 | }; |
| 91 | } |
| 92 | |
Junxiao Shi | d97c953 | 2017-04-27 16:17:04 +0000 | [diff] [blame] | 93 | BOOST_AUTO_TEST_SUITE(Mgmt) |
| 94 | BOOST_FIXTURE_TEST_SUITE(TestDispatcher, DispatcherFixture) |
| 95 | |
| 96 | BOOST_AUTO_TEST_CASE(Basic) |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 97 | { |
| 98 | BOOST_CHECK_NO_THROW(dispatcher |
| 99 | .addControlCommand<VoidParameters>("test/1", makeAcceptAllAuthorization(), |
| 100 | bind([] { return true; }), |
| 101 | bind([]{}))); |
| 102 | BOOST_CHECK_NO_THROW(dispatcher |
| 103 | .addControlCommand<VoidParameters>("test/2", makeAcceptAllAuthorization(), |
| 104 | bind([] { return true; }), |
| 105 | bind([]{}))); |
| 106 | |
| 107 | BOOST_CHECK_THROW(dispatcher |
| 108 | .addControlCommand<VoidParameters>("test", makeAcceptAllAuthorization(), |
| 109 | bind([] { return true; }), |
| 110 | bind([]{})), |
| 111 | std::out_of_range); |
| 112 | |
| 113 | BOOST_CHECK_NO_THROW(dispatcher.addStatusDataset("status/1", |
| 114 | makeAcceptAllAuthorization(), bind([]{}))); |
| 115 | BOOST_CHECK_NO_THROW(dispatcher.addStatusDataset("status/2", |
| 116 | makeAcceptAllAuthorization(), bind([]{}))); |
| 117 | BOOST_CHECK_THROW(dispatcher.addStatusDataset("status", |
| 118 | makeAcceptAllAuthorization(), bind([]{})), |
| 119 | std::out_of_range); |
| 120 | |
| 121 | BOOST_CHECK_NO_THROW(dispatcher.addNotificationStream("stream/1")); |
| 122 | BOOST_CHECK_NO_THROW(dispatcher.addNotificationStream("stream/2")); |
| 123 | BOOST_CHECK_THROW(dispatcher.addNotificationStream("stream"), std::out_of_range); |
| 124 | |
| 125 | |
| 126 | BOOST_CHECK_NO_THROW(dispatcher.addTopPrefix("/root/1")); |
| 127 | BOOST_CHECK_NO_THROW(dispatcher.addTopPrefix("/root/2")); |
| 128 | BOOST_CHECK_THROW(dispatcher.addTopPrefix("/root"), std::out_of_range); |
| 129 | |
| 130 | BOOST_CHECK_THROW(dispatcher |
| 131 | .addControlCommand<VoidParameters>("test/3", makeAcceptAllAuthorization(), |
| 132 | bind([] { return true; }), |
| 133 | bind([]{})), |
| 134 | std::domain_error); |
| 135 | |
| 136 | BOOST_CHECK_THROW(dispatcher.addStatusDataset("status/3", |
| 137 | makeAcceptAllAuthorization(), bind([]{})), |
| 138 | std::domain_error); |
| 139 | |
| 140 | BOOST_CHECK_THROW(dispatcher.addNotificationStream("stream/3"), std::domain_error); |
| 141 | } |
| 142 | |
Junxiao Shi | d97c953 | 2017-04-27 16:17:04 +0000 | [diff] [blame] | 143 | BOOST_AUTO_TEST_CASE(AddRemoveTopPrefix) |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 144 | { |
| 145 | std::map<std::string, size_t> nCallbackCalled; |
| 146 | dispatcher |
| 147 | .addControlCommand<VoidParameters>("test/1", makeAcceptAllAuthorization(), |
| 148 | bind([] { return true; }), |
| 149 | bind([&nCallbackCalled] { ++nCallbackCalled["test/1"]; })); |
| 150 | |
| 151 | dispatcher |
| 152 | .addControlCommand<VoidParameters>("test/2", makeAcceptAllAuthorization(), |
| 153 | bind([] { return true; }), |
| 154 | bind([&nCallbackCalled] { ++nCallbackCalled["test/2"]; })); |
| 155 | |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 156 | face.receive(*makeInterest("/root/1/test/1/%80%00")); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 157 | advanceClocks(1_ms); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 158 | BOOST_CHECK_EQUAL(nCallbackCalled["test/1"], 0); |
| 159 | BOOST_CHECK_EQUAL(nCallbackCalled["test/2"], 0); |
| 160 | |
| 161 | dispatcher.addTopPrefix("/root/1"); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 162 | advanceClocks(1_ms); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 163 | |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 164 | face.receive(*makeInterest("/root/1/test/1/%80%00")); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 165 | advanceClocks(1_ms); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 166 | BOOST_CHECK_EQUAL(nCallbackCalled["test/1"], 1); |
| 167 | BOOST_CHECK_EQUAL(nCallbackCalled["test/2"], 0); |
| 168 | |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 169 | face.receive(*makeInterest("/root/1/test/2/%80%00")); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 170 | advanceClocks(1_ms); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 171 | BOOST_CHECK_EQUAL(nCallbackCalled["test/1"], 1); |
| 172 | BOOST_CHECK_EQUAL(nCallbackCalled["test/2"], 1); |
| 173 | |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 174 | face.receive(*makeInterest("/root/2/test/1/%80%00")); |
| 175 | face.receive(*makeInterest("/root/2/test/2/%80%00")); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 176 | advanceClocks(1_ms); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 177 | BOOST_CHECK_EQUAL(nCallbackCalled["test/1"], 1); |
| 178 | BOOST_CHECK_EQUAL(nCallbackCalled["test/2"], 1); |
| 179 | |
| 180 | dispatcher.addTopPrefix("/root/2"); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 181 | advanceClocks(1_ms); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 182 | |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 183 | face.receive(*makeInterest("/root/1/test/1/%80%00")); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 184 | advanceClocks(1_ms); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 185 | BOOST_CHECK_EQUAL(nCallbackCalled["test/1"], 2); |
| 186 | |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 187 | face.receive(*makeInterest("/root/2/test/1/%80%00")); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 188 | advanceClocks(1_ms); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 189 | BOOST_CHECK_EQUAL(nCallbackCalled["test/1"], 3); |
| 190 | |
| 191 | dispatcher.removeTopPrefix("/root/1"); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 192 | advanceClocks(1_ms); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 193 | |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 194 | face.receive(*makeInterest("/root/1/test/1/%80%00")); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 195 | advanceClocks(1_ms); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 196 | BOOST_CHECK_EQUAL(nCallbackCalled["test/1"], 3); |
| 197 | |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 198 | face.receive(*makeInterest("/root/2/test/1/%80%00")); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 199 | advanceClocks(1_ms); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 200 | BOOST_CHECK_EQUAL(nCallbackCalled["test/1"], 4); |
| 201 | } |
| 202 | |
Junxiao Shi | d97c953 | 2017-04-27 16:17:04 +0000 | [diff] [blame] | 203 | BOOST_AUTO_TEST_CASE(ControlCommand) |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 204 | { |
| 205 | size_t nCallbackCalled = 0; |
| 206 | dispatcher |
| 207 | .addControlCommand<VoidParameters>("test", |
| 208 | makeTestAuthorization(), |
| 209 | bind([] { return true; }), |
| 210 | bind([&nCallbackCalled] { ++nCallbackCalled; })); |
| 211 | |
| 212 | dispatcher.addTopPrefix("/root"); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 213 | advanceClocks(1_ms); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 214 | face.sentData.clear(); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 215 | |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 216 | face.receive(*makeInterest("/root/test/%80%00")); // returns 403 |
| 217 | face.receive(*makeInterest("/root/test/%80%00/invalid")); // returns 403 |
| 218 | face.receive(*makeInterest("/root/test/%80%00/silent")); // silently ignored |
| 219 | face.receive(*makeInterest("/root/test/.../invalid")); // silently ignored (wrong format) |
| 220 | face.receive(*makeInterest("/root/test/.../valid")); // silently ignored (wrong format) |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 221 | advanceClocks(1_ms, 20); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 222 | BOOST_CHECK_EQUAL(nCallbackCalled, 0); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 223 | BOOST_CHECK_EQUAL(face.sentData.size(), 2); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 224 | |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 225 | BOOST_CHECK_EQUAL(face.sentData[0].getContentType(), tlv::ContentType_Blob); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 226 | BOOST_CHECK_EQUAL(ControlResponse(face.sentData[0].getContent().blockFromValue()).getCode(), 403); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 227 | BOOST_CHECK_EQUAL(face.sentData[1].getContentType(), tlv::ContentType_Blob); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 228 | BOOST_CHECK_EQUAL(ControlResponse(face.sentData[1].getContent().blockFromValue()).getCode(), 403); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 229 | |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 230 | face.receive(*makeInterest("/root/test/%80%00/valid")); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 231 | advanceClocks(1_ms, 10); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 232 | BOOST_CHECK_EQUAL(nCallbackCalled, 1); |
| 233 | } |
| 234 | |
Junxiao Shi | d97c953 | 2017-04-27 16:17:04 +0000 | [diff] [blame] | 235 | class StatefulParameters : public mgmt::ControlParameters |
| 236 | { |
| 237 | public: |
| 238 | explicit |
| 239 | StatefulParameters(const Block& wire) |
| 240 | { |
| 241 | wireDecode(wire); |
| 242 | } |
| 243 | |
| 244 | Block |
| 245 | wireEncode() const final |
| 246 | { |
| 247 | return Block(); |
| 248 | } |
| 249 | |
| 250 | void |
Davide Pesavento | 3c34ec1 | 2021-03-28 21:50:06 -0400 | [diff] [blame^] | 251 | wireDecode(const Block&) final |
Junxiao Shi | d97c953 | 2017-04-27 16:17:04 +0000 | [diff] [blame] | 252 | { |
| 253 | m_state = EXPECTED_STATE; |
| 254 | } |
| 255 | |
| 256 | bool |
| 257 | check() const |
| 258 | { |
| 259 | return m_state == EXPECTED_STATE; |
| 260 | } |
| 261 | |
| 262 | private: |
| 263 | static constexpr int EXPECTED_STATE = 12602; |
| 264 | int m_state = 0; |
| 265 | }; |
| 266 | |
| 267 | BOOST_AUTO_TEST_CASE(ControlCommandAsyncAuthorization) // Bug 4059 |
| 268 | { |
| 269 | AcceptContinuation authorizationAccept; |
| 270 | auto authorization = |
Davide Pesavento | 3c34ec1 | 2021-03-28 21:50:06 -0400 | [diff] [blame^] | 271 | [&authorizationAccept] (const Name&, const Interest&, const ControlParameters*, |
| 272 | AcceptContinuation accept, RejectContinuation) { |
| 273 | authorizationAccept = std::move(accept); |
Junxiao Shi | d97c953 | 2017-04-27 16:17:04 +0000 | [diff] [blame] | 274 | }; |
| 275 | |
| 276 | auto validateParameters = |
| 277 | [] (const ControlParameters& params) { |
| 278 | return dynamic_cast<const StatefulParameters&>(params).check(); |
| 279 | }; |
| 280 | |
| 281 | size_t nCallbackCalled = 0; |
Davide Pesavento | 3c34ec1 | 2021-03-28 21:50:06 -0400 | [diff] [blame^] | 282 | dispatcher.addControlCommand<StatefulParameters>("test", authorization, validateParameters, |
| 283 | bind([&nCallbackCalled] { ++nCallbackCalled; })); |
Junxiao Shi | d97c953 | 2017-04-27 16:17:04 +0000 | [diff] [blame] | 284 | |
| 285 | dispatcher.addTopPrefix("/root"); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 286 | advanceClocks(1_ms); |
Junxiao Shi | d97c953 | 2017-04-27 16:17:04 +0000 | [diff] [blame] | 287 | |
| 288 | face.receive(*makeInterest("/root/test/%80%00")); |
| 289 | BOOST_CHECK_EQUAL(nCallbackCalled, 0); |
| 290 | BOOST_REQUIRE(authorizationAccept != nullptr); |
| 291 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 292 | advanceClocks(1_ms); |
Junxiao Shi | d97c953 | 2017-04-27 16:17:04 +0000 | [diff] [blame] | 293 | authorizationAccept(""); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 294 | advanceClocks(1_ms); |
Junxiao Shi | d97c953 | 2017-04-27 16:17:04 +0000 | [diff] [blame] | 295 | BOOST_CHECK_EQUAL(nCallbackCalled, 1); |
| 296 | } |
| 297 | |
| 298 | BOOST_AUTO_TEST_CASE(StatusDataset) |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 299 | { |
Davide Pesavento | b10024c | 2017-09-22 01:36:44 -0400 | [diff] [blame] | 300 | const uint8_t smallBuf[] = {0x81, 0x01, 0x01}; |
| 301 | const Block smallBlock(smallBuf, sizeof(smallBuf)); |
Davide Pesavento | 3c34ec1 | 2021-03-28 21:50:06 -0400 | [diff] [blame^] | 302 | const Block largeBlock = [] { |
| 303 | Block b(129, std::make_shared<const Buffer>(3000)); |
| 304 | b.encode(); |
| 305 | return b; |
| 306 | }(); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 307 | |
| 308 | dispatcher.addStatusDataset("test/small", |
| 309 | makeTestAuthorization(), |
Davide Pesavento | 3c34ec1 | 2021-03-28 21:50:06 -0400 | [diff] [blame^] | 310 | [&smallBlock] (const Name&, const Interest&, |
Davide Pesavento | b10024c | 2017-09-22 01:36:44 -0400 | [diff] [blame] | 311 | StatusDatasetContext& context) { |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 312 | context.append(smallBlock); |
| 313 | context.append(smallBlock); |
| 314 | context.append(smallBlock); |
| 315 | context.end(); |
| 316 | }); |
| 317 | |
| 318 | dispatcher.addStatusDataset("test/large", |
| 319 | makeTestAuthorization(), |
Davide Pesavento | 3c34ec1 | 2021-03-28 21:50:06 -0400 | [diff] [blame^] | 320 | [&largeBlock] (const Name&, const Interest&, |
Davide Pesavento | b10024c | 2017-09-22 01:36:44 -0400 | [diff] [blame] | 321 | StatusDatasetContext& context) { |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 322 | context.append(largeBlock); |
| 323 | context.append(largeBlock); |
| 324 | context.append(largeBlock); |
| 325 | context.end(); |
| 326 | }); |
| 327 | |
| 328 | dispatcher.addStatusDataset("test/reject", |
| 329 | makeTestAuthorization(), |
Davide Pesavento | 3c34ec1 | 2021-03-28 21:50:06 -0400 | [diff] [blame^] | 330 | [] (const Name&, const Interest&, StatusDatasetContext& context) { |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 331 | context.reject(); |
| 332 | }); |
| 333 | |
| 334 | dispatcher.addTopPrefix("/root"); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 335 | advanceClocks(1_ms); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 336 | face.sentData.clear(); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 337 | |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 338 | face.receive(*makeInterest("/root/test/small/%80%00")); // returns 403 |
| 339 | face.receive(*makeInterest("/root/test/small/%80%00/invalid")); // returns 403 |
| 340 | face.receive(*makeInterest("/root/test/small/%80%00/silent")); // silently ignored |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 341 | advanceClocks(1_ms, 20); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 342 | |
Davide Pesavento | 3c34ec1 | 2021-03-28 21:50:06 -0400 | [diff] [blame^] | 343 | BOOST_REQUIRE_EQUAL(face.sentData.size(), 2); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 344 | BOOST_CHECK_EQUAL(face.sentData[0].getContentType(), tlv::ContentType_Blob); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 345 | BOOST_CHECK_EQUAL(ControlResponse(face.sentData[0].getContent().blockFromValue()).getCode(), 403); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 346 | BOOST_CHECK_EQUAL(face.sentData[1].getContentType(), tlv::ContentType_Blob); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 347 | BOOST_CHECK_EQUAL(ControlResponse(face.sentData[1].getContent().blockFromValue()).getCode(), 403); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 348 | |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 349 | face.sentData.clear(); |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 350 | |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 351 | auto interestSmall = *makeInterest("/root/test/small/valid", true); |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 352 | face.receive(interestSmall); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 353 | advanceClocks(1_ms, 10); |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 354 | |
| 355 | // one data packet is generated and sent to both places |
Davide Pesavento | 3c34ec1 | 2021-03-28 21:50:06 -0400 | [diff] [blame^] | 356 | BOOST_REQUIRE_EQUAL(face.sentData.size(), 1); |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 357 | BOOST_CHECK_EQUAL(storage.size(), 1); |
| 358 | |
| 359 | auto fetchedData = storage.find(interestSmall); |
| 360 | BOOST_REQUIRE(fetchedData != nullptr); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 361 | BOOST_CHECK_EQUAL(face.sentData[0].wireEncode(), fetchedData->wireEncode()); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 362 | |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 363 | face.receive(*makeInterest(Name("/root/test/small/valid").appendVersion(10))); // should be ignored |
| 364 | face.receive(*makeInterest(Name("/root/test/small/valid").appendSegment(20))); // should be ignored |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 365 | advanceClocks(1_ms, 10); |
Davide Pesavento | 3c34ec1 | 2021-03-28 21:50:06 -0400 | [diff] [blame^] | 366 | BOOST_REQUIRE_EQUAL(face.sentData.size(), 1); |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 367 | BOOST_CHECK_EQUAL(storage.size(), 1); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 368 | |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 369 | Block content = face.sentData[0].getContent(); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 370 | BOOST_CHECK_NO_THROW(content.parse()); |
| 371 | |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 372 | BOOST_REQUIRE_EQUAL(content.elements().size(), 3); |
| 373 | BOOST_CHECK_EQUAL(content.elements()[0], smallBlock); |
| 374 | BOOST_CHECK_EQUAL(content.elements()[1], smallBlock); |
| 375 | BOOST_CHECK_EQUAL(content.elements()[2], smallBlock); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 376 | |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 377 | storage.erase("/", true); // clear the storage |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 378 | face.sentData.clear(); |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 379 | face.receive(*makeInterest("/root/test/large/valid")); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 380 | advanceClocks(1_ms, 10); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 381 | |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 382 | // two data packets are generated, the first one will be sent to both places |
| 383 | // while the second one will only be inserted into the in-memory storage |
Davide Pesavento | 3c34ec1 | 2021-03-28 21:50:06 -0400 | [diff] [blame^] | 384 | BOOST_REQUIRE_EQUAL(face.sentData.size(), 1); |
| 385 | BOOST_REQUIRE_EQUAL(storage.size(), 2); |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 386 | |
| 387 | // segment0 should be sent through the face |
| 388 | const auto& component = face.sentData[0].getName().at(-1); |
| 389 | BOOST_CHECK(component.isSegment()); |
| 390 | BOOST_CHECK_EQUAL(component.toSegment(), 0); |
| 391 | |
| 392 | std::vector<Data> dataInStorage; |
| 393 | std::copy(storage.begin(), storage.end(), std::back_inserter(dataInStorage)); |
| 394 | |
| 395 | // the Data sent through the face should be the same as the first Data in the storage |
| 396 | BOOST_CHECK_EQUAL(face.sentData[0].getName(), dataInStorage[0].getName()); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 397 | BOOST_CHECK_EQUAL(face.sentData[0].getContent(), dataInStorage[0].getContent()); |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 398 | |
| 399 | content = [&dataInStorage] () -> Block { |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 400 | EncodingBuffer encoder; |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 401 | size_t valueLength = encoder.prependByteArray(dataInStorage[1].getContent().value(), |
| 402 | dataInStorage[1].getContent().value_size()); |
| 403 | valueLength += encoder.prependByteArray(dataInStorage[0].getContent().value(), |
| 404 | dataInStorage[0].getContent().value_size()); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 405 | encoder.prependVarNumber(valueLength); |
| 406 | encoder.prependVarNumber(tlv::Content); |
| 407 | return encoder.block(); |
| 408 | }(); |
| 409 | |
| 410 | BOOST_CHECK_NO_THROW(content.parse()); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 411 | BOOST_REQUIRE_EQUAL(content.elements().size(), 3); |
| 412 | BOOST_CHECK_EQUAL(content.elements()[0], largeBlock); |
| 413 | BOOST_CHECK_EQUAL(content.elements()[1], largeBlock); |
| 414 | BOOST_CHECK_EQUAL(content.elements()[2], largeBlock); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 415 | |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 416 | storage.erase("/", true);// clear the storage |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 417 | face.sentData.clear(); |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 418 | face.receive(*makeInterest("/root/test/reject/%80%00/valid")); // returns nack |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 419 | advanceClocks(1_ms); |
Davide Pesavento | 3c34ec1 | 2021-03-28 21:50:06 -0400 | [diff] [blame^] | 420 | |
| 421 | BOOST_REQUIRE_EQUAL(face.sentData.size(), 1); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 422 | BOOST_CHECK_EQUAL(face.sentData[0].getContentType(), tlv::ContentType_Nack); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 423 | BOOST_CHECK_EQUAL(ControlResponse(face.sentData[0].getContent().blockFromValue()).getCode(), 400); |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 424 | BOOST_CHECK_EQUAL(storage.size(), 0); // the nack packet will not be inserted into the in-memory storage |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 425 | } |
| 426 | |
Junxiao Shi | d97c953 | 2017-04-27 16:17:04 +0000 | [diff] [blame] | 427 | BOOST_AUTO_TEST_CASE(NotificationStream) |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 428 | { |
Davide Pesavento | b10024c | 2017-09-22 01:36:44 -0400 | [diff] [blame] | 429 | const uint8_t buf[] = {0x82, 0x01, 0x02}; |
| 430 | const Block block(buf, sizeof(buf)); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 431 | auto post = dispatcher.addNotificationStream("test"); |
| 432 | |
| 433 | post(block); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 434 | advanceClocks(1_ms); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 435 | BOOST_CHECK_EQUAL(face.sentData.size(), 0); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 436 | |
| 437 | dispatcher.addTopPrefix("/root"); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 438 | advanceClocks(1_ms); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 439 | face.sentData.clear(); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 440 | |
| 441 | post(block); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 442 | advanceClocks(1_ms); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 443 | BOOST_CHECK_EQUAL(face.sentData.size(), 1); |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 444 | BOOST_CHECK_EQUAL(storage.size(), 1); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 445 | |
| 446 | post(block); |
| 447 | post(block); |
| 448 | post(block); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 449 | advanceClocks(1_ms, 10); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 450 | |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 451 | BOOST_REQUIRE_EQUAL(face.sentData.size(), 4); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 452 | BOOST_CHECK_EQUAL(face.sentData[0].getName(), "/root/test/%FE%00"); |
| 453 | BOOST_CHECK_EQUAL(face.sentData[1].getName(), "/root/test/%FE%01"); |
| 454 | BOOST_CHECK_EQUAL(face.sentData[2].getName(), "/root/test/%FE%02"); |
| 455 | BOOST_CHECK_EQUAL(face.sentData[3].getName(), "/root/test/%FE%03"); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 456 | |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 457 | BOOST_CHECK_EQUAL(face.sentData[0].getContent().blockFromValue(), block); |
| 458 | BOOST_CHECK_EQUAL(face.sentData[1].getContent().blockFromValue(), block); |
| 459 | BOOST_CHECK_EQUAL(face.sentData[2].getContent().blockFromValue(), block); |
| 460 | BOOST_CHECK_EQUAL(face.sentData[3].getContent().blockFromValue(), block); |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 461 | |
| 462 | // each version of notification will be sent to both places |
| 463 | std::vector<Data> dataInStorage; |
| 464 | std::copy(storage.begin(), storage.end(), std::back_inserter(dataInStorage)); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 465 | BOOST_REQUIRE_EQUAL(dataInStorage.size(), 4); |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 466 | BOOST_CHECK_EQUAL(dataInStorage[0].getName(), "/root/test/%FE%00"); |
| 467 | BOOST_CHECK_EQUAL(dataInStorage[1].getName(), "/root/test/%FE%01"); |
| 468 | BOOST_CHECK_EQUAL(dataInStorage[2].getName(), "/root/test/%FE%02"); |
| 469 | BOOST_CHECK_EQUAL(dataInStorage[3].getName(), "/root/test/%FE%03"); |
| 470 | |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 471 | BOOST_CHECK_EQUAL(dataInStorage[0].getContent().blockFromValue(), block); |
| 472 | BOOST_CHECK_EQUAL(dataInStorage[1].getContent().blockFromValue(), block); |
| 473 | BOOST_CHECK_EQUAL(dataInStorage[2].getContent().blockFromValue(), block); |
| 474 | BOOST_CHECK_EQUAL(dataInStorage[3].getContent().blockFromValue(), block); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 475 | } |
| 476 | |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 477 | BOOST_AUTO_TEST_SUITE_END() // TestDispatcher |
| 478 | BOOST_AUTO_TEST_SUITE_END() // Mgmt |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 479 | |
| 480 | } // namespace tests |
| 481 | } // namespace mgmt |
| 482 | } // namespace ndn |