Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 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" |
| 27 | #include "identity-management-fixture.hpp" |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 28 | #include "../identity-management-time-fixture.hpp" |
| 29 | #include "../make-interest-data.hpp" |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 30 | |
| 31 | namespace ndn { |
| 32 | namespace mgmt { |
| 33 | namespace tests { |
| 34 | |
| 35 | using namespace ndn::tests; |
| 36 | |
Alexander Afanasyev | 70244f4 | 2017-01-04 12:47:12 -0800 | [diff] [blame] | 37 | class DispatcherFixture : public IdentityManagementV1TimeFixture |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 38 | { |
| 39 | public: |
| 40 | DispatcherFixture() |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 41 | : face(io, m_keyChain, {true, true}) |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 42 | , dispatcher(face, m_keyChain, security::SigningInfo()) |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 43 | , storage(dispatcher.m_storage) |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 44 | { |
| 45 | } |
| 46 | |
| 47 | public: |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 48 | util::DummyClientFace face; |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 49 | mgmt::Dispatcher dispatcher; |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 50 | util::InMemoryStorageFifo& storage; |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 51 | }; |
| 52 | |
| 53 | class VoidParameters : public mgmt::ControlParameters |
| 54 | { |
| 55 | public: |
| 56 | explicit |
| 57 | VoidParameters(const Block& wire) |
| 58 | { |
| 59 | wireDecode(wire); |
| 60 | } |
| 61 | |
Junxiao Shi | d97c953 | 2017-04-27 16:17:04 +0000 | [diff] [blame] | 62 | Block |
Davide Pesavento | aa82eb6 | 2016-04-22 19:08:40 +0200 | [diff] [blame] | 63 | wireEncode() const final |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 64 | { |
| 65 | return Block(128); |
| 66 | } |
| 67 | |
Junxiao Shi | d97c953 | 2017-04-27 16:17:04 +0000 | [diff] [blame] | 68 | void |
Davide Pesavento | aa82eb6 | 2016-04-22 19:08:40 +0200 | [diff] [blame] | 69 | wireDecode(const Block& wire) final |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 70 | { |
| 71 | if (wire.type() != 128) |
Junxiao Shi | d97c953 | 2017-04-27 16:17:04 +0000 | [diff] [blame] | 72 | BOOST_THROW_EXCEPTION(tlv::Error("Expecting TLV type 128")); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 73 | } |
| 74 | }; |
| 75 | |
| 76 | static Authorization |
| 77 | makeTestAuthorization() |
| 78 | { |
| 79 | return [] (const Name& prefix, |
| 80 | const Interest& interest, |
| 81 | const ControlParameters* params, |
| 82 | AcceptContinuation accept, |
| 83 | RejectContinuation reject) { |
| 84 | if (interest.getName()[-1] == name::Component("valid")) { |
| 85 | accept(""); |
| 86 | } |
| 87 | else { |
| 88 | if (interest.getName()[-1] == name::Component("silent")) { |
| 89 | reject(RejectReply::SILENT); |
| 90 | } |
| 91 | else { |
| 92 | reject(RejectReply::STATUS403); |
| 93 | } |
| 94 | } |
| 95 | }; |
| 96 | } |
| 97 | |
Junxiao Shi | d97c953 | 2017-04-27 16:17:04 +0000 | [diff] [blame] | 98 | BOOST_AUTO_TEST_SUITE(Mgmt) |
| 99 | BOOST_FIXTURE_TEST_SUITE(TestDispatcher, DispatcherFixture) |
| 100 | |
| 101 | BOOST_AUTO_TEST_CASE(Basic) |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 102 | { |
| 103 | BOOST_CHECK_NO_THROW(dispatcher |
| 104 | .addControlCommand<VoidParameters>("test/1", makeAcceptAllAuthorization(), |
| 105 | bind([] { return true; }), |
| 106 | bind([]{}))); |
| 107 | BOOST_CHECK_NO_THROW(dispatcher |
| 108 | .addControlCommand<VoidParameters>("test/2", makeAcceptAllAuthorization(), |
| 109 | bind([] { return true; }), |
| 110 | bind([]{}))); |
| 111 | |
| 112 | BOOST_CHECK_THROW(dispatcher |
| 113 | .addControlCommand<VoidParameters>("test", makeAcceptAllAuthorization(), |
| 114 | bind([] { return true; }), |
| 115 | bind([]{})), |
| 116 | std::out_of_range); |
| 117 | |
| 118 | BOOST_CHECK_NO_THROW(dispatcher.addStatusDataset("status/1", |
| 119 | makeAcceptAllAuthorization(), bind([]{}))); |
| 120 | BOOST_CHECK_NO_THROW(dispatcher.addStatusDataset("status/2", |
| 121 | makeAcceptAllAuthorization(), bind([]{}))); |
| 122 | BOOST_CHECK_THROW(dispatcher.addStatusDataset("status", |
| 123 | makeAcceptAllAuthorization(), bind([]{})), |
| 124 | std::out_of_range); |
| 125 | |
| 126 | BOOST_CHECK_NO_THROW(dispatcher.addNotificationStream("stream/1")); |
| 127 | BOOST_CHECK_NO_THROW(dispatcher.addNotificationStream("stream/2")); |
| 128 | BOOST_CHECK_THROW(dispatcher.addNotificationStream("stream"), std::out_of_range); |
| 129 | |
| 130 | |
| 131 | BOOST_CHECK_NO_THROW(dispatcher.addTopPrefix("/root/1")); |
| 132 | BOOST_CHECK_NO_THROW(dispatcher.addTopPrefix("/root/2")); |
| 133 | BOOST_CHECK_THROW(dispatcher.addTopPrefix("/root"), std::out_of_range); |
| 134 | |
| 135 | BOOST_CHECK_THROW(dispatcher |
| 136 | .addControlCommand<VoidParameters>("test/3", makeAcceptAllAuthorization(), |
| 137 | bind([] { return true; }), |
| 138 | bind([]{})), |
| 139 | std::domain_error); |
| 140 | |
| 141 | BOOST_CHECK_THROW(dispatcher.addStatusDataset("status/3", |
| 142 | makeAcceptAllAuthorization(), bind([]{})), |
| 143 | std::domain_error); |
| 144 | |
| 145 | BOOST_CHECK_THROW(dispatcher.addNotificationStream("stream/3"), std::domain_error); |
| 146 | } |
| 147 | |
Junxiao Shi | d97c953 | 2017-04-27 16:17:04 +0000 | [diff] [blame] | 148 | BOOST_AUTO_TEST_CASE(AddRemoveTopPrefix) |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 149 | { |
| 150 | std::map<std::string, size_t> nCallbackCalled; |
| 151 | dispatcher |
| 152 | .addControlCommand<VoidParameters>("test/1", makeAcceptAllAuthorization(), |
| 153 | bind([] { return true; }), |
| 154 | bind([&nCallbackCalled] { ++nCallbackCalled["test/1"]; })); |
| 155 | |
| 156 | dispatcher |
| 157 | .addControlCommand<VoidParameters>("test/2", makeAcceptAllAuthorization(), |
| 158 | bind([] { return true; }), |
| 159 | bind([&nCallbackCalled] { ++nCallbackCalled["test/2"]; })); |
| 160 | |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 161 | face.receive(*makeInterest("/root/1/test/1/%80%00")); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 162 | advanceClocks(time::milliseconds(1)); |
| 163 | BOOST_CHECK_EQUAL(nCallbackCalled["test/1"], 0); |
| 164 | BOOST_CHECK_EQUAL(nCallbackCalled["test/2"], 0); |
| 165 | |
| 166 | dispatcher.addTopPrefix("/root/1"); |
| 167 | advanceClocks(time::milliseconds(1)); |
| 168 | |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 169 | face.receive(*makeInterest("/root/1/test/1/%80%00")); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 170 | advanceClocks(time::milliseconds(1)); |
| 171 | BOOST_CHECK_EQUAL(nCallbackCalled["test/1"], 1); |
| 172 | BOOST_CHECK_EQUAL(nCallbackCalled["test/2"], 0); |
| 173 | |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 174 | face.receive(*makeInterest("/root/1/test/2/%80%00")); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 175 | advanceClocks(time::milliseconds(1)); |
| 176 | BOOST_CHECK_EQUAL(nCallbackCalled["test/1"], 1); |
| 177 | BOOST_CHECK_EQUAL(nCallbackCalled["test/2"], 1); |
| 178 | |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 179 | face.receive(*makeInterest("/root/2/test/1/%80%00")); |
| 180 | face.receive(*makeInterest("/root/2/test/2/%80%00")); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 181 | advanceClocks(time::milliseconds(1)); |
| 182 | BOOST_CHECK_EQUAL(nCallbackCalled["test/1"], 1); |
| 183 | BOOST_CHECK_EQUAL(nCallbackCalled["test/2"], 1); |
| 184 | |
| 185 | dispatcher.addTopPrefix("/root/2"); |
| 186 | advanceClocks(time::milliseconds(1)); |
| 187 | |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 188 | face.receive(*makeInterest("/root/1/test/1/%80%00")); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 189 | advanceClocks(time::milliseconds(1)); |
| 190 | BOOST_CHECK_EQUAL(nCallbackCalled["test/1"], 2); |
| 191 | |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 192 | face.receive(*makeInterest("/root/2/test/1/%80%00")); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 193 | advanceClocks(time::milliseconds(1)); |
| 194 | BOOST_CHECK_EQUAL(nCallbackCalled["test/1"], 3); |
| 195 | |
| 196 | dispatcher.removeTopPrefix("/root/1"); |
| 197 | advanceClocks(time::milliseconds(1)); |
| 198 | |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 199 | face.receive(*makeInterest("/root/1/test/1/%80%00")); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 200 | advanceClocks(time::milliseconds(1)); |
| 201 | BOOST_CHECK_EQUAL(nCallbackCalled["test/1"], 3); |
| 202 | |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 203 | face.receive(*makeInterest("/root/2/test/1/%80%00")); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 204 | advanceClocks(time::milliseconds(1)); |
| 205 | BOOST_CHECK_EQUAL(nCallbackCalled["test/1"], 4); |
| 206 | } |
| 207 | |
Junxiao Shi | d97c953 | 2017-04-27 16:17:04 +0000 | [diff] [blame] | 208 | BOOST_AUTO_TEST_CASE(ControlCommand) |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 209 | { |
| 210 | size_t nCallbackCalled = 0; |
| 211 | dispatcher |
| 212 | .addControlCommand<VoidParameters>("test", |
| 213 | makeTestAuthorization(), |
| 214 | bind([] { return true; }), |
| 215 | bind([&nCallbackCalled] { ++nCallbackCalled; })); |
| 216 | |
| 217 | dispatcher.addTopPrefix("/root"); |
| 218 | advanceClocks(time::milliseconds(1)); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 219 | face.sentData.clear(); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 220 | |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 221 | face.receive(*makeInterest("/root/test/%80%00")); // returns 403 |
| 222 | face.receive(*makeInterest("/root/test/%80%00/invalid")); // returns 403 |
| 223 | face.receive(*makeInterest("/root/test/%80%00/silent")); // silently ignored |
| 224 | face.receive(*makeInterest("/root/test/.../invalid")); // silently ignored (wrong format) |
| 225 | face.receive(*makeInterest("/root/test/.../valid")); // silently ignored (wrong format) |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 226 | advanceClocks(time::milliseconds(1), 20); |
| 227 | BOOST_CHECK_EQUAL(nCallbackCalled, 0); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 228 | BOOST_CHECK_EQUAL(face.sentData.size(), 2); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 229 | |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 230 | BOOST_CHECK(face.sentData[0].getContentType() == tlv::ContentType_Blob); |
| 231 | BOOST_CHECK_EQUAL(ControlResponse(face.sentData[0].getContent().blockFromValue()).getCode(), 403); |
| 232 | BOOST_CHECK(face.sentData[1].getContentType() == tlv::ContentType_Blob); |
| 233 | BOOST_CHECK_EQUAL(ControlResponse(face.sentData[1].getContent().blockFromValue()).getCode(), 403); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 234 | |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 235 | face.receive(*makeInterest("/root/test/%80%00/valid")); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 236 | advanceClocks(time::milliseconds(1), 10); |
| 237 | BOOST_CHECK_EQUAL(nCallbackCalled, 1); |
| 238 | } |
| 239 | |
Junxiao Shi | d97c953 | 2017-04-27 16:17:04 +0000 | [diff] [blame] | 240 | class StatefulParameters : public mgmt::ControlParameters |
| 241 | { |
| 242 | public: |
| 243 | explicit |
| 244 | StatefulParameters(const Block& wire) |
| 245 | { |
| 246 | wireDecode(wire); |
| 247 | } |
| 248 | |
| 249 | Block |
| 250 | wireEncode() const final |
| 251 | { |
| 252 | return Block(); |
| 253 | } |
| 254 | |
| 255 | void |
| 256 | wireDecode(const Block& wire) final |
| 257 | { |
| 258 | m_state = EXPECTED_STATE; |
| 259 | } |
| 260 | |
| 261 | bool |
| 262 | check() const |
| 263 | { |
| 264 | return m_state == EXPECTED_STATE; |
| 265 | } |
| 266 | |
| 267 | private: |
| 268 | static constexpr int EXPECTED_STATE = 12602; |
| 269 | int m_state = 0; |
| 270 | }; |
| 271 | |
| 272 | BOOST_AUTO_TEST_CASE(ControlCommandAsyncAuthorization) // Bug 4059 |
| 273 | { |
| 274 | AcceptContinuation authorizationAccept; |
| 275 | auto authorization = |
| 276 | [&authorizationAccept] (const Name& prefix, const Interest& interest, const ControlParameters* params, |
| 277 | AcceptContinuation accept, RejectContinuation reject) { |
| 278 | authorizationAccept = accept; |
| 279 | }; |
| 280 | |
| 281 | auto validateParameters = |
| 282 | [] (const ControlParameters& params) { |
| 283 | return dynamic_cast<const StatefulParameters&>(params).check(); |
| 284 | }; |
| 285 | |
| 286 | size_t nCallbackCalled = 0; |
| 287 | dispatcher |
| 288 | .addControlCommand<StatefulParameters>("test", |
| 289 | authorization, |
| 290 | validateParameters, |
| 291 | bind([&nCallbackCalled] { ++nCallbackCalled; })); |
| 292 | |
| 293 | dispatcher.addTopPrefix("/root"); |
| 294 | advanceClocks(time::milliseconds(1)); |
| 295 | |
| 296 | face.receive(*makeInterest("/root/test/%80%00")); |
| 297 | BOOST_CHECK_EQUAL(nCallbackCalled, 0); |
| 298 | BOOST_REQUIRE(authorizationAccept != nullptr); |
| 299 | |
| 300 | advanceClocks(time::milliseconds(1)); |
| 301 | authorizationAccept(""); |
| 302 | advanceClocks(time::milliseconds(1)); |
| 303 | BOOST_CHECK_EQUAL(nCallbackCalled, 1); |
| 304 | } |
| 305 | |
| 306 | BOOST_AUTO_TEST_CASE(StatusDataset) |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 307 | { |
| 308 | static Block smallBlock("\x81\x01\0x01", 3); |
| 309 | static Block largeBlock = [] () -> Block { |
| 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); |
| 316 | return encoder.block(); |
| 317 | }(); |
| 318 | |
| 319 | dispatcher.addStatusDataset("test/small", |
| 320 | makeTestAuthorization(), |
| 321 | [] (const Name& prefix, const Interest& interest, |
Junxiao Shi | f65a336 | 2015-09-06 20:54:54 -0700 | [diff] [blame] | 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(), |
| 331 | [] (const Name& prefix, const Interest& interest, |
Junxiao Shi | f65a336 | 2015-09-06 20:54:54 -0700 | [diff] [blame] | 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"); |
| 347 | advanceClocks(time::milliseconds(1)); |
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 |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 353 | advanceClocks(time::milliseconds(1), 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 | |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 356 | BOOST_CHECK(face.sentData[0].getContentType() == tlv::ContentType_Blob); |
| 357 | BOOST_CHECK_EQUAL(ControlResponse(face.sentData[0].getContent().blockFromValue()).getCode(), 403); |
| 358 | BOOST_CHECK(face.sentData[1].getContentType() == tlv::ContentType_Blob); |
| 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 | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 363 | auto interestSmall = *makeInterest("/root/test/small/valid"); |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 364 | face.receive(interestSmall); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 365 | advanceClocks(time::milliseconds(1), 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); |
| 373 | BOOST_CHECK(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 |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 377 | advanceClocks(time::milliseconds(1), 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 | |
| 384 | BOOST_CHECK_EQUAL(content.elements().size(), 3); |
| 385 | BOOST_CHECK(content.elements()[0] == smallBlock); |
| 386 | BOOST_CHECK(content.elements()[1] == smallBlock); |
| 387 | BOOST_CHECK(content.elements()[2] == smallBlock); |
| 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")); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 392 | advanceClocks(time::milliseconds(1), 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()); |
| 409 | BOOST_CHECK(face.sentData[0].getContent() == dataInStorage[0].getContent()); |
| 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()); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 423 | BOOST_CHECK_EQUAL(content.elements().size(), 3); |
| 424 | BOOST_CHECK(content.elements()[0] == largeBlock); |
| 425 | BOOST_CHECK(content.elements()[1] == largeBlock); |
| 426 | BOOST_CHECK(content.elements()[2] == largeBlock); |
| 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 |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 431 | advanceClocks(time::milliseconds(1)); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 432 | BOOST_CHECK_EQUAL(face.sentData.size(), 1); |
| 433 | BOOST_CHECK(face.sentData[0].getContentType() == tlv::ContentType_Nack); |
| 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 | { |
| 440 | static Block block("\x82\x01\x02", 3); |
| 441 | |
| 442 | auto post = dispatcher.addNotificationStream("test"); |
| 443 | |
| 444 | post(block); |
| 445 | advanceClocks(time::milliseconds(1)); |
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"); |
| 449 | advanceClocks(time::milliseconds(1)); |
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); |
| 453 | advanceClocks(time::milliseconds(1)); |
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); |
| 460 | advanceClocks(time::milliseconds(1), 10); |
| 461 | |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 462 | BOOST_CHECK_EQUAL(face.sentData.size(), 4); |
| 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 | |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 468 | BOOST_CHECK(face.sentData[0].getContent().blockFromValue() == block); |
| 469 | BOOST_CHECK(face.sentData[1].getContent().blockFromValue() == block); |
| 470 | BOOST_CHECK(face.sentData[2].getContent().blockFromValue() == block); |
| 471 | BOOST_CHECK(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)); |
| 476 | BOOST_CHECK_EQUAL(dataInStorage.size(), 4); |
| 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 | |
| 482 | BOOST_CHECK(dataInStorage[0].getContent().blockFromValue() == block); |
| 483 | BOOST_CHECK(dataInStorage[1].getContent().blockFromValue() == block); |
| 484 | BOOST_CHECK(dataInStorage[2].getContent().blockFromValue() == block); |
| 485 | BOOST_CHECK(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 |