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 | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 3 | * Copyright (c) 2013-2020 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 | { |
| 77 | return [] (const Name& prefix, |
| 78 | const Interest& interest, |
| 79 | const ControlParameters* params, |
| 80 | AcceptContinuation accept, |
| 81 | RejectContinuation reject) { |
| 82 | if (interest.getName()[-1] == name::Component("valid")) { |
| 83 | accept(""); |
| 84 | } |
| 85 | else { |
| 86 | if (interest.getName()[-1] == name::Component("silent")) { |
| 87 | reject(RejectReply::SILENT); |
| 88 | } |
| 89 | else { |
| 90 | reject(RejectReply::STATUS403); |
| 91 | } |
| 92 | } |
| 93 | }; |
| 94 | } |
| 95 | |
Junxiao Shi | d97c953 | 2017-04-27 16:17:04 +0000 | [diff] [blame] | 96 | BOOST_AUTO_TEST_SUITE(Mgmt) |
| 97 | BOOST_FIXTURE_TEST_SUITE(TestDispatcher, DispatcherFixture) |
| 98 | |
| 99 | BOOST_AUTO_TEST_CASE(Basic) |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 100 | { |
| 101 | BOOST_CHECK_NO_THROW(dispatcher |
| 102 | .addControlCommand<VoidParameters>("test/1", makeAcceptAllAuthorization(), |
| 103 | bind([] { return true; }), |
| 104 | bind([]{}))); |
| 105 | BOOST_CHECK_NO_THROW(dispatcher |
| 106 | .addControlCommand<VoidParameters>("test/2", makeAcceptAllAuthorization(), |
| 107 | bind([] { return true; }), |
| 108 | bind([]{}))); |
| 109 | |
| 110 | BOOST_CHECK_THROW(dispatcher |
| 111 | .addControlCommand<VoidParameters>("test", makeAcceptAllAuthorization(), |
| 112 | bind([] { return true; }), |
| 113 | bind([]{})), |
| 114 | std::out_of_range); |
| 115 | |
| 116 | BOOST_CHECK_NO_THROW(dispatcher.addStatusDataset("status/1", |
| 117 | makeAcceptAllAuthorization(), bind([]{}))); |
| 118 | BOOST_CHECK_NO_THROW(dispatcher.addStatusDataset("status/2", |
| 119 | makeAcceptAllAuthorization(), bind([]{}))); |
| 120 | BOOST_CHECK_THROW(dispatcher.addStatusDataset("status", |
| 121 | makeAcceptAllAuthorization(), bind([]{})), |
| 122 | std::out_of_range); |
| 123 | |
| 124 | BOOST_CHECK_NO_THROW(dispatcher.addNotificationStream("stream/1")); |
| 125 | BOOST_CHECK_NO_THROW(dispatcher.addNotificationStream("stream/2")); |
| 126 | BOOST_CHECK_THROW(dispatcher.addNotificationStream("stream"), std::out_of_range); |
| 127 | |
| 128 | |
| 129 | BOOST_CHECK_NO_THROW(dispatcher.addTopPrefix("/root/1")); |
| 130 | BOOST_CHECK_NO_THROW(dispatcher.addTopPrefix("/root/2")); |
| 131 | BOOST_CHECK_THROW(dispatcher.addTopPrefix("/root"), std::out_of_range); |
| 132 | |
| 133 | BOOST_CHECK_THROW(dispatcher |
| 134 | .addControlCommand<VoidParameters>("test/3", makeAcceptAllAuthorization(), |
| 135 | bind([] { return true; }), |
| 136 | bind([]{})), |
| 137 | std::domain_error); |
| 138 | |
| 139 | BOOST_CHECK_THROW(dispatcher.addStatusDataset("status/3", |
| 140 | makeAcceptAllAuthorization(), bind([]{})), |
| 141 | std::domain_error); |
| 142 | |
| 143 | BOOST_CHECK_THROW(dispatcher.addNotificationStream("stream/3"), std::domain_error); |
| 144 | } |
| 145 | |
Junxiao Shi | d97c953 | 2017-04-27 16:17:04 +0000 | [diff] [blame] | 146 | BOOST_AUTO_TEST_CASE(AddRemoveTopPrefix) |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 147 | { |
| 148 | std::map<std::string, size_t> nCallbackCalled; |
| 149 | dispatcher |
| 150 | .addControlCommand<VoidParameters>("test/1", makeAcceptAllAuthorization(), |
| 151 | bind([] { return true; }), |
| 152 | bind([&nCallbackCalled] { ++nCallbackCalled["test/1"]; })); |
| 153 | |
| 154 | dispatcher |
| 155 | .addControlCommand<VoidParameters>("test/2", makeAcceptAllAuthorization(), |
| 156 | bind([] { return true; }), |
| 157 | bind([&nCallbackCalled] { ++nCallbackCalled["test/2"]; })); |
| 158 | |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 159 | face.receive(*makeInterest("/root/1/test/1/%80%00")); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 160 | advanceClocks(1_ms); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 161 | BOOST_CHECK_EQUAL(nCallbackCalled["test/1"], 0); |
| 162 | BOOST_CHECK_EQUAL(nCallbackCalled["test/2"], 0); |
| 163 | |
| 164 | dispatcher.addTopPrefix("/root/1"); |
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 | |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 167 | face.receive(*makeInterest("/root/1/test/1/%80%00")); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 168 | advanceClocks(1_ms); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 169 | BOOST_CHECK_EQUAL(nCallbackCalled["test/1"], 1); |
| 170 | BOOST_CHECK_EQUAL(nCallbackCalled["test/2"], 0); |
| 171 | |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 172 | face.receive(*makeInterest("/root/1/test/2/%80%00")); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 173 | advanceClocks(1_ms); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 174 | BOOST_CHECK_EQUAL(nCallbackCalled["test/1"], 1); |
| 175 | BOOST_CHECK_EQUAL(nCallbackCalled["test/2"], 1); |
| 176 | |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 177 | face.receive(*makeInterest("/root/2/test/1/%80%00")); |
| 178 | face.receive(*makeInterest("/root/2/test/2/%80%00")); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 179 | advanceClocks(1_ms); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 180 | BOOST_CHECK_EQUAL(nCallbackCalled["test/1"], 1); |
| 181 | BOOST_CHECK_EQUAL(nCallbackCalled["test/2"], 1); |
| 182 | |
| 183 | dispatcher.addTopPrefix("/root/2"); |
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 | |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 186 | face.receive(*makeInterest("/root/1/test/1/%80%00")); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 187 | advanceClocks(1_ms); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 188 | BOOST_CHECK_EQUAL(nCallbackCalled["test/1"], 2); |
| 189 | |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 190 | face.receive(*makeInterest("/root/2/test/1/%80%00")); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 191 | advanceClocks(1_ms); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 192 | BOOST_CHECK_EQUAL(nCallbackCalled["test/1"], 3); |
| 193 | |
| 194 | dispatcher.removeTopPrefix("/root/1"); |
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 | |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 197 | face.receive(*makeInterest("/root/1/test/1/%80%00")); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 198 | advanceClocks(1_ms); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 199 | BOOST_CHECK_EQUAL(nCallbackCalled["test/1"], 3); |
| 200 | |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 201 | face.receive(*makeInterest("/root/2/test/1/%80%00")); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 202 | advanceClocks(1_ms); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 203 | BOOST_CHECK_EQUAL(nCallbackCalled["test/1"], 4); |
| 204 | } |
| 205 | |
Junxiao Shi | d97c953 | 2017-04-27 16:17:04 +0000 | [diff] [blame] | 206 | BOOST_AUTO_TEST_CASE(ControlCommand) |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 207 | { |
| 208 | size_t nCallbackCalled = 0; |
| 209 | dispatcher |
| 210 | .addControlCommand<VoidParameters>("test", |
| 211 | makeTestAuthorization(), |
| 212 | bind([] { return true; }), |
| 213 | bind([&nCallbackCalled] { ++nCallbackCalled; })); |
| 214 | |
| 215 | dispatcher.addTopPrefix("/root"); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 216 | advanceClocks(1_ms); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 217 | face.sentData.clear(); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 218 | |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 219 | face.receive(*makeInterest("/root/test/%80%00")); // returns 403 |
| 220 | face.receive(*makeInterest("/root/test/%80%00/invalid")); // returns 403 |
| 221 | face.receive(*makeInterest("/root/test/%80%00/silent")); // silently ignored |
| 222 | face.receive(*makeInterest("/root/test/.../invalid")); // silently ignored (wrong format) |
| 223 | face.receive(*makeInterest("/root/test/.../valid")); // silently ignored (wrong format) |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 224 | advanceClocks(1_ms, 20); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 225 | BOOST_CHECK_EQUAL(nCallbackCalled, 0); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 226 | BOOST_CHECK_EQUAL(face.sentData.size(), 2); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 227 | |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 228 | BOOST_CHECK_EQUAL(face.sentData[0].getContentType(), tlv::ContentType_Blob); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 229 | BOOST_CHECK_EQUAL(ControlResponse(face.sentData[0].getContent().blockFromValue()).getCode(), 403); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 230 | BOOST_CHECK_EQUAL(face.sentData[1].getContentType(), tlv::ContentType_Blob); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 231 | BOOST_CHECK_EQUAL(ControlResponse(face.sentData[1].getContent().blockFromValue()).getCode(), 403); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 232 | |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 233 | face.receive(*makeInterest("/root/test/%80%00/valid")); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 234 | advanceClocks(1_ms, 10); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 235 | BOOST_CHECK_EQUAL(nCallbackCalled, 1); |
| 236 | } |
| 237 | |
Junxiao Shi | d97c953 | 2017-04-27 16:17:04 +0000 | [diff] [blame] | 238 | class StatefulParameters : public mgmt::ControlParameters |
| 239 | { |
| 240 | public: |
| 241 | explicit |
| 242 | StatefulParameters(const Block& wire) |
| 243 | { |
| 244 | wireDecode(wire); |
| 245 | } |
| 246 | |
| 247 | Block |
| 248 | wireEncode() const final |
| 249 | { |
| 250 | return Block(); |
| 251 | } |
| 252 | |
| 253 | void |
| 254 | wireDecode(const Block& wire) final |
| 255 | { |
| 256 | m_state = EXPECTED_STATE; |
| 257 | } |
| 258 | |
| 259 | bool |
| 260 | check() const |
| 261 | { |
| 262 | return m_state == EXPECTED_STATE; |
| 263 | } |
| 264 | |
| 265 | private: |
| 266 | static constexpr int EXPECTED_STATE = 12602; |
| 267 | int m_state = 0; |
| 268 | }; |
| 269 | |
| 270 | BOOST_AUTO_TEST_CASE(ControlCommandAsyncAuthorization) // Bug 4059 |
| 271 | { |
| 272 | AcceptContinuation authorizationAccept; |
| 273 | auto authorization = |
| 274 | [&authorizationAccept] (const Name& prefix, const Interest& interest, const ControlParameters* params, |
| 275 | AcceptContinuation accept, RejectContinuation reject) { |
| 276 | authorizationAccept = accept; |
| 277 | }; |
| 278 | |
| 279 | auto validateParameters = |
| 280 | [] (const ControlParameters& params) { |
| 281 | return dynamic_cast<const StatefulParameters&>(params).check(); |
| 282 | }; |
| 283 | |
| 284 | size_t nCallbackCalled = 0; |
| 285 | dispatcher |
| 286 | .addControlCommand<StatefulParameters>("test", |
| 287 | authorization, |
| 288 | validateParameters, |
| 289 | bind([&nCallbackCalled] { ++nCallbackCalled; })); |
| 290 | |
| 291 | dispatcher.addTopPrefix("/root"); |
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 | |
| 294 | face.receive(*makeInterest("/root/test/%80%00")); |
| 295 | BOOST_CHECK_EQUAL(nCallbackCalled, 0); |
| 296 | BOOST_REQUIRE(authorizationAccept != nullptr); |
| 297 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 298 | advanceClocks(1_ms); |
Junxiao Shi | d97c953 | 2017-04-27 16:17:04 +0000 | [diff] [blame] | 299 | authorizationAccept(""); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 300 | advanceClocks(1_ms); |
Junxiao Shi | d97c953 | 2017-04-27 16:17:04 +0000 | [diff] [blame] | 301 | BOOST_CHECK_EQUAL(nCallbackCalled, 1); |
| 302 | } |
| 303 | |
| 304 | BOOST_AUTO_TEST_CASE(StatusDataset) |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 305 | { |
Davide Pesavento | b10024c | 2017-09-22 01:36:44 -0400 | [diff] [blame] | 306 | const uint8_t smallBuf[] = {0x81, 0x01, 0x01}; |
| 307 | const Block smallBlock(smallBuf, sizeof(smallBuf)); |
| 308 | Block largeBlock; |
| 309 | { |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 310 | EncodingBuffer encoder; |
| 311 | for (size_t i = 0; i < 2500; ++i) { |
| 312 | encoder.prependByte(1); |
| 313 | } |
| 314 | encoder.prependVarNumber(2500); |
| 315 | encoder.prependVarNumber(129); |
Davide Pesavento | b10024c | 2017-09-22 01:36:44 -0400 | [diff] [blame] | 316 | largeBlock = encoder.block(); |
| 317 | } |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 318 | |
| 319 | dispatcher.addStatusDataset("test/small", |
| 320 | makeTestAuthorization(), |
Davide Pesavento | b10024c | 2017-09-22 01:36:44 -0400 | [diff] [blame] | 321 | [&smallBlock] (const Name& prefix, const Interest& interest, |
| 322 | StatusDatasetContext& context) { |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 323 | context.append(smallBlock); |
| 324 | context.append(smallBlock); |
| 325 | context.append(smallBlock); |
| 326 | context.end(); |
| 327 | }); |
| 328 | |
| 329 | dispatcher.addStatusDataset("test/large", |
| 330 | makeTestAuthorization(), |
Davide Pesavento | b10024c | 2017-09-22 01:36:44 -0400 | [diff] [blame] | 331 | [&largeBlock] (const Name& prefix, const Interest& interest, |
| 332 | StatusDatasetContext& context) { |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 333 | context.append(largeBlock); |
| 334 | context.append(largeBlock); |
| 335 | context.append(largeBlock); |
| 336 | context.end(); |
| 337 | }); |
| 338 | |
| 339 | dispatcher.addStatusDataset("test/reject", |
| 340 | makeTestAuthorization(), |
| 341 | [] (const Name& prefix, const Interest& interest, |
Junxiao Shi | f65a336 | 2015-09-06 20:54:54 -0700 | [diff] [blame] | 342 | StatusDatasetContext& context) { |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 343 | context.reject(); |
| 344 | }); |
| 345 | |
| 346 | dispatcher.addTopPrefix("/root"); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 347 | advanceClocks(1_ms); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 348 | face.sentData.clear(); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 349 | |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 350 | face.receive(*makeInterest("/root/test/small/%80%00")); // returns 403 |
| 351 | face.receive(*makeInterest("/root/test/small/%80%00/invalid")); // returns 403 |
| 352 | face.receive(*makeInterest("/root/test/small/%80%00/silent")); // silently ignored |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 353 | advanceClocks(1_ms, 20); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 354 | BOOST_CHECK_EQUAL(face.sentData.size(), 2); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 355 | |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 356 | BOOST_CHECK_EQUAL(face.sentData[0].getContentType(), tlv::ContentType_Blob); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 357 | BOOST_CHECK_EQUAL(ControlResponse(face.sentData[0].getContent().blockFromValue()).getCode(), 403); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 358 | BOOST_CHECK_EQUAL(face.sentData[1].getContentType(), tlv::ContentType_Blob); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 359 | BOOST_CHECK_EQUAL(ControlResponse(face.sentData[1].getContent().blockFromValue()).getCode(), 403); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 360 | |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 361 | face.sentData.clear(); |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 362 | |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 363 | auto interestSmall = *makeInterest("/root/test/small/valid", true); |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 364 | face.receive(interestSmall); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 365 | advanceClocks(1_ms, 10); |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 366 | |
| 367 | // one data packet is generated and sent to both places |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 368 | BOOST_CHECK_EQUAL(face.sentData.size(), 1); |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 369 | BOOST_CHECK_EQUAL(storage.size(), 1); |
| 370 | |
| 371 | auto fetchedData = storage.find(interestSmall); |
| 372 | BOOST_REQUIRE(fetchedData != nullptr); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 373 | BOOST_CHECK_EQUAL(face.sentData[0].wireEncode(), fetchedData->wireEncode()); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 374 | |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 375 | face.receive(*makeInterest(Name("/root/test/small/valid").appendVersion(10))); // should be ignored |
| 376 | 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] | 377 | advanceClocks(1_ms, 10); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 378 | BOOST_CHECK_EQUAL(face.sentData.size(), 1); |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 379 | BOOST_CHECK_EQUAL(storage.size(), 1); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 380 | |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 381 | Block content = face.sentData[0].getContent(); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 382 | BOOST_CHECK_NO_THROW(content.parse()); |
| 383 | |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 384 | BOOST_REQUIRE_EQUAL(content.elements().size(), 3); |
| 385 | BOOST_CHECK_EQUAL(content.elements()[0], smallBlock); |
| 386 | BOOST_CHECK_EQUAL(content.elements()[1], smallBlock); |
| 387 | BOOST_CHECK_EQUAL(content.elements()[2], smallBlock); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 388 | |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 389 | storage.erase("/", true); // clear the storage |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 390 | face.sentData.clear(); |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 391 | face.receive(*makeInterest("/root/test/large/valid")); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 392 | advanceClocks(1_ms, 10); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 393 | |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 394 | // two data packets are generated, the first one will be sent to both places |
| 395 | // while the second one will only be inserted into the in-memory storage |
| 396 | BOOST_CHECK_EQUAL(face.sentData.size(), 1); |
| 397 | BOOST_CHECK_EQUAL(storage.size(), 2); |
| 398 | |
| 399 | // segment0 should be sent through the face |
| 400 | const auto& component = face.sentData[0].getName().at(-1); |
| 401 | BOOST_CHECK(component.isSegment()); |
| 402 | BOOST_CHECK_EQUAL(component.toSegment(), 0); |
| 403 | |
| 404 | std::vector<Data> dataInStorage; |
| 405 | std::copy(storage.begin(), storage.end(), std::back_inserter(dataInStorage)); |
| 406 | |
| 407 | // the Data sent through the face should be the same as the first Data in the storage |
| 408 | BOOST_CHECK_EQUAL(face.sentData[0].getName(), dataInStorage[0].getName()); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 409 | BOOST_CHECK_EQUAL(face.sentData[0].getContent(), dataInStorage[0].getContent()); |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 410 | |
| 411 | content = [&dataInStorage] () -> Block { |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 412 | EncodingBuffer encoder; |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 413 | size_t valueLength = encoder.prependByteArray(dataInStorage[1].getContent().value(), |
| 414 | dataInStorage[1].getContent().value_size()); |
| 415 | valueLength += encoder.prependByteArray(dataInStorage[0].getContent().value(), |
| 416 | dataInStorage[0].getContent().value_size()); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 417 | encoder.prependVarNumber(valueLength); |
| 418 | encoder.prependVarNumber(tlv::Content); |
| 419 | return encoder.block(); |
| 420 | }(); |
| 421 | |
| 422 | BOOST_CHECK_NO_THROW(content.parse()); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 423 | BOOST_REQUIRE_EQUAL(content.elements().size(), 3); |
| 424 | BOOST_CHECK_EQUAL(content.elements()[0], largeBlock); |
| 425 | BOOST_CHECK_EQUAL(content.elements()[1], largeBlock); |
| 426 | BOOST_CHECK_EQUAL(content.elements()[2], largeBlock); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 427 | |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 428 | storage.erase("/", true);// clear the storage |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 429 | face.sentData.clear(); |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 430 | face.receive(*makeInterest("/root/test/reject/%80%00/valid")); // returns nack |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 431 | advanceClocks(1_ms); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 432 | BOOST_CHECK_EQUAL(face.sentData.size(), 1); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 433 | BOOST_CHECK_EQUAL(face.sentData[0].getContentType(), tlv::ContentType_Nack); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 434 | BOOST_CHECK_EQUAL(ControlResponse(face.sentData[0].getContent().blockFromValue()).getCode(), 400); |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 435 | 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] | 436 | } |
| 437 | |
Junxiao Shi | d97c953 | 2017-04-27 16:17:04 +0000 | [diff] [blame] | 438 | BOOST_AUTO_TEST_CASE(NotificationStream) |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 439 | { |
Davide Pesavento | b10024c | 2017-09-22 01:36:44 -0400 | [diff] [blame] | 440 | const uint8_t buf[] = {0x82, 0x01, 0x02}; |
| 441 | const Block block(buf, sizeof(buf)); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 442 | auto post = dispatcher.addNotificationStream("test"); |
| 443 | |
| 444 | post(block); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 445 | advanceClocks(1_ms); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 446 | BOOST_CHECK_EQUAL(face.sentData.size(), 0); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 447 | |
| 448 | dispatcher.addTopPrefix("/root"); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 449 | advanceClocks(1_ms); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 450 | face.sentData.clear(); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 451 | |
| 452 | post(block); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 453 | advanceClocks(1_ms); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 454 | BOOST_CHECK_EQUAL(face.sentData.size(), 1); |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 455 | BOOST_CHECK_EQUAL(storage.size(), 1); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 456 | |
| 457 | post(block); |
| 458 | post(block); |
| 459 | post(block); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 460 | advanceClocks(1_ms, 10); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 461 | |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 462 | BOOST_REQUIRE_EQUAL(face.sentData.size(), 4); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 463 | BOOST_CHECK_EQUAL(face.sentData[0].getName(), "/root/test/%FE%00"); |
| 464 | BOOST_CHECK_EQUAL(face.sentData[1].getName(), "/root/test/%FE%01"); |
| 465 | BOOST_CHECK_EQUAL(face.sentData[2].getName(), "/root/test/%FE%02"); |
| 466 | BOOST_CHECK_EQUAL(face.sentData[3].getName(), "/root/test/%FE%03"); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 467 | |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 468 | BOOST_CHECK_EQUAL(face.sentData[0].getContent().blockFromValue(), block); |
| 469 | BOOST_CHECK_EQUAL(face.sentData[1].getContent().blockFromValue(), block); |
| 470 | BOOST_CHECK_EQUAL(face.sentData[2].getContent().blockFromValue(), block); |
| 471 | BOOST_CHECK_EQUAL(face.sentData[3].getContent().blockFromValue(), block); |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 472 | |
| 473 | // each version of notification will be sent to both places |
| 474 | std::vector<Data> dataInStorage; |
| 475 | std::copy(storage.begin(), storage.end(), std::back_inserter(dataInStorage)); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 476 | BOOST_REQUIRE_EQUAL(dataInStorage.size(), 4); |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 477 | BOOST_CHECK_EQUAL(dataInStorage[0].getName(), "/root/test/%FE%00"); |
| 478 | BOOST_CHECK_EQUAL(dataInStorage[1].getName(), "/root/test/%FE%01"); |
| 479 | BOOST_CHECK_EQUAL(dataInStorage[2].getName(), "/root/test/%FE%02"); |
| 480 | BOOST_CHECK_EQUAL(dataInStorage[3].getName(), "/root/test/%FE%03"); |
| 481 | |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 482 | BOOST_CHECK_EQUAL(dataInStorage[0].getContent().blockFromValue(), block); |
| 483 | BOOST_CHECK_EQUAL(dataInStorage[1].getContent().blockFromValue(), block); |
| 484 | BOOST_CHECK_EQUAL(dataInStorage[2].getContent().blockFromValue(), block); |
| 485 | BOOST_CHECK_EQUAL(dataInStorage[3].getContent().blockFromValue(), block); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 486 | } |
| 487 | |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 488 | BOOST_AUTO_TEST_SUITE_END() // TestDispatcher |
| 489 | BOOST_AUTO_TEST_SUITE_END() // Mgmt |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 490 | |
| 491 | } // namespace tests |
| 492 | } // namespace mgmt |
| 493 | } // namespace ndn |