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