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 | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 3 | * Copyright (c) 2013-2018 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")); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 161 | advanceClocks(1_ms); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 162 | BOOST_CHECK_EQUAL(nCallbackCalled["test/1"], 0); |
| 163 | BOOST_CHECK_EQUAL(nCallbackCalled["test/2"], 0); |
| 164 | |
| 165 | dispatcher.addTopPrefix("/root/1"); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 166 | advanceClocks(1_ms); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 167 | |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 168 | face.receive(*makeInterest("/root/1/test/1/%80%00")); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 169 | advanceClocks(1_ms); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 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")); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 174 | advanceClocks(1_ms); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 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")); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 180 | advanceClocks(1_ms); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 181 | BOOST_CHECK_EQUAL(nCallbackCalled["test/1"], 1); |
| 182 | BOOST_CHECK_EQUAL(nCallbackCalled["test/2"], 1); |
| 183 | |
| 184 | dispatcher.addTopPrefix("/root/2"); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 185 | advanceClocks(1_ms); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 186 | |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 187 | face.receive(*makeInterest("/root/1/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"], 2); |
| 190 | |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 191 | face.receive(*makeInterest("/root/2/test/1/%80%00")); |
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 | BOOST_CHECK_EQUAL(nCallbackCalled["test/1"], 3); |
| 194 | |
| 195 | dispatcher.removeTopPrefix("/root/1"); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 196 | advanceClocks(1_ms); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 197 | |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 198 | face.receive(*makeInterest("/root/1/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"], 3); |
| 201 | |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 202 | face.receive(*makeInterest("/root/2/test/1/%80%00")); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 203 | advanceClocks(1_ms); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 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"); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 217 | advanceClocks(1_ms); |
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) |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 225 | advanceClocks(1_ms, 20); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 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 | |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 229 | BOOST_CHECK_EQUAL(face.sentData[0].getContentType(), tlv::ContentType_Blob); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 230 | BOOST_CHECK_EQUAL(ControlResponse(face.sentData[0].getContent().blockFromValue()).getCode(), 403); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 231 | BOOST_CHECK_EQUAL(face.sentData[1].getContentType(), tlv::ContentType_Blob); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 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")); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 235 | advanceClocks(1_ms, 10); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 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"); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 293 | advanceClocks(1_ms); |
Junxiao Shi | d97c953 | 2017-04-27 16:17:04 +0000 | [diff] [blame] | 294 | |
| 295 | face.receive(*makeInterest("/root/test/%80%00")); |
| 296 | BOOST_CHECK_EQUAL(nCallbackCalled, 0); |
| 297 | BOOST_REQUIRE(authorizationAccept != nullptr); |
| 298 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 299 | advanceClocks(1_ms); |
Junxiao Shi | d97c953 | 2017-04-27 16:17:04 +0000 | [diff] [blame] | 300 | authorizationAccept(""); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 301 | advanceClocks(1_ms); |
Junxiao Shi | d97c953 | 2017-04-27 16:17:04 +0000 | [diff] [blame] | 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 | { |
Davide Pesavento | b10024c | 2017-09-22 01:36:44 -0400 | [diff] [blame] | 307 | const uint8_t smallBuf[] = {0x81, 0x01, 0x01}; |
| 308 | const Block smallBlock(smallBuf, sizeof(smallBuf)); |
| 309 | Block largeBlock; |
| 310 | { |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 311 | EncodingBuffer encoder; |
| 312 | for (size_t i = 0; i < 2500; ++i) { |
| 313 | encoder.prependByte(1); |
| 314 | } |
| 315 | encoder.prependVarNumber(2500); |
| 316 | encoder.prependVarNumber(129); |
Davide Pesavento | b10024c | 2017-09-22 01:36:44 -0400 | [diff] [blame] | 317 | largeBlock = encoder.block(); |
| 318 | } |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 319 | |
| 320 | dispatcher.addStatusDataset("test/small", |
| 321 | makeTestAuthorization(), |
Davide Pesavento | b10024c | 2017-09-22 01:36:44 -0400 | [diff] [blame] | 322 | [&smallBlock] (const Name& prefix, const Interest& interest, |
| 323 | StatusDatasetContext& context) { |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 324 | context.append(smallBlock); |
| 325 | context.append(smallBlock); |
| 326 | context.append(smallBlock); |
| 327 | context.end(); |
| 328 | }); |
| 329 | |
| 330 | dispatcher.addStatusDataset("test/large", |
| 331 | makeTestAuthorization(), |
Davide Pesavento | b10024c | 2017-09-22 01:36:44 -0400 | [diff] [blame] | 332 | [&largeBlock] (const Name& prefix, const Interest& interest, |
| 333 | StatusDatasetContext& context) { |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 334 | context.append(largeBlock); |
| 335 | context.append(largeBlock); |
| 336 | context.append(largeBlock); |
| 337 | context.end(); |
| 338 | }); |
| 339 | |
| 340 | dispatcher.addStatusDataset("test/reject", |
| 341 | makeTestAuthorization(), |
| 342 | [] (const Name& prefix, const Interest& interest, |
Junxiao Shi | f65a336 | 2015-09-06 20:54:54 -0700 | [diff] [blame] | 343 | StatusDatasetContext& context) { |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 344 | context.reject(); |
| 345 | }); |
| 346 | |
| 347 | dispatcher.addTopPrefix("/root"); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 348 | advanceClocks(1_ms); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 349 | face.sentData.clear(); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 350 | |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 351 | face.receive(*makeInterest("/root/test/small/%80%00")); // returns 403 |
| 352 | face.receive(*makeInterest("/root/test/small/%80%00/invalid")); // returns 403 |
| 353 | face.receive(*makeInterest("/root/test/small/%80%00/silent")); // silently ignored |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 354 | advanceClocks(1_ms, 20); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 355 | BOOST_CHECK_EQUAL(face.sentData.size(), 2); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 356 | |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 357 | BOOST_CHECK_EQUAL(face.sentData[0].getContentType(), tlv::ContentType_Blob); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 358 | BOOST_CHECK_EQUAL(ControlResponse(face.sentData[0].getContent().blockFromValue()).getCode(), 403); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 359 | BOOST_CHECK_EQUAL(face.sentData[1].getContentType(), tlv::ContentType_Blob); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 360 | BOOST_CHECK_EQUAL(ControlResponse(face.sentData[1].getContent().blockFromValue()).getCode(), 403); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 361 | |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 362 | face.sentData.clear(); |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 363 | |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 364 | auto interestSmall = *makeInterest("/root/test/small/valid"); |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 365 | face.receive(interestSmall); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 366 | advanceClocks(1_ms, 10); |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 367 | |
| 368 | // one data packet is generated and sent to both places |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 369 | BOOST_CHECK_EQUAL(face.sentData.size(), 1); |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 370 | BOOST_CHECK_EQUAL(storage.size(), 1); |
| 371 | |
| 372 | auto fetchedData = storage.find(interestSmall); |
| 373 | BOOST_REQUIRE(fetchedData != nullptr); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 374 | BOOST_CHECK_EQUAL(face.sentData[0].wireEncode(), fetchedData->wireEncode()); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 375 | |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 376 | face.receive(*makeInterest(Name("/root/test/small/valid").appendVersion(10))); // should be ignored |
| 377 | 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] | 378 | advanceClocks(1_ms, 10); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 379 | BOOST_CHECK_EQUAL(face.sentData.size(), 1); |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 380 | BOOST_CHECK_EQUAL(storage.size(), 1); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 381 | |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 382 | Block content = face.sentData[0].getContent(); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 383 | BOOST_CHECK_NO_THROW(content.parse()); |
| 384 | |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 385 | BOOST_REQUIRE_EQUAL(content.elements().size(), 3); |
| 386 | BOOST_CHECK_EQUAL(content.elements()[0], smallBlock); |
| 387 | BOOST_CHECK_EQUAL(content.elements()[1], smallBlock); |
| 388 | BOOST_CHECK_EQUAL(content.elements()[2], smallBlock); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 389 | |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 390 | storage.erase("/", true); // clear the storage |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 391 | face.sentData.clear(); |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 392 | face.receive(*makeInterest("/root/test/large/valid")); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 393 | advanceClocks(1_ms, 10); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 394 | |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 395 | // two data packets are generated, the first one will be sent to both places |
| 396 | // while the second one will only be inserted into the in-memory storage |
| 397 | BOOST_CHECK_EQUAL(face.sentData.size(), 1); |
| 398 | BOOST_CHECK_EQUAL(storage.size(), 2); |
| 399 | |
| 400 | // segment0 should be sent through the face |
| 401 | const auto& component = face.sentData[0].getName().at(-1); |
| 402 | BOOST_CHECK(component.isSegment()); |
| 403 | BOOST_CHECK_EQUAL(component.toSegment(), 0); |
| 404 | |
| 405 | std::vector<Data> dataInStorage; |
| 406 | std::copy(storage.begin(), storage.end(), std::back_inserter(dataInStorage)); |
| 407 | |
| 408 | // the Data sent through the face should be the same as the first Data in the storage |
| 409 | BOOST_CHECK_EQUAL(face.sentData[0].getName(), dataInStorage[0].getName()); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 410 | BOOST_CHECK_EQUAL(face.sentData[0].getContent(), dataInStorage[0].getContent()); |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 411 | |
| 412 | content = [&dataInStorage] () -> Block { |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 413 | EncodingBuffer encoder; |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 414 | size_t valueLength = encoder.prependByteArray(dataInStorage[1].getContent().value(), |
| 415 | dataInStorage[1].getContent().value_size()); |
| 416 | valueLength += encoder.prependByteArray(dataInStorage[0].getContent().value(), |
| 417 | dataInStorage[0].getContent().value_size()); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 418 | encoder.prependVarNumber(valueLength); |
| 419 | encoder.prependVarNumber(tlv::Content); |
| 420 | return encoder.block(); |
| 421 | }(); |
| 422 | |
| 423 | BOOST_CHECK_NO_THROW(content.parse()); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 424 | BOOST_REQUIRE_EQUAL(content.elements().size(), 3); |
| 425 | BOOST_CHECK_EQUAL(content.elements()[0], largeBlock); |
| 426 | BOOST_CHECK_EQUAL(content.elements()[1], largeBlock); |
| 427 | BOOST_CHECK_EQUAL(content.elements()[2], largeBlock); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 428 | |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 429 | storage.erase("/", true);// clear the storage |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 430 | face.sentData.clear(); |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 431 | face.receive(*makeInterest("/root/test/reject/%80%00/valid")); // returns nack |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 432 | advanceClocks(1_ms); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 433 | BOOST_CHECK_EQUAL(face.sentData.size(), 1); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 434 | BOOST_CHECK_EQUAL(face.sentData[0].getContentType(), tlv::ContentType_Nack); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 435 | BOOST_CHECK_EQUAL(ControlResponse(face.sentData[0].getContent().blockFromValue()).getCode(), 400); |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 436 | 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] | 437 | } |
| 438 | |
Junxiao Shi | d97c953 | 2017-04-27 16:17:04 +0000 | [diff] [blame] | 439 | BOOST_AUTO_TEST_CASE(NotificationStream) |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 440 | { |
Davide Pesavento | b10024c | 2017-09-22 01:36:44 -0400 | [diff] [blame] | 441 | const uint8_t buf[] = {0x82, 0x01, 0x02}; |
| 442 | const Block block(buf, sizeof(buf)); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 443 | auto post = dispatcher.addNotificationStream("test"); |
| 444 | |
| 445 | post(block); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 446 | advanceClocks(1_ms); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 447 | BOOST_CHECK_EQUAL(face.sentData.size(), 0); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 448 | |
| 449 | dispatcher.addTopPrefix("/root"); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 450 | advanceClocks(1_ms); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 451 | face.sentData.clear(); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 452 | |
| 453 | post(block); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 454 | advanceClocks(1_ms); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 455 | BOOST_CHECK_EQUAL(face.sentData.size(), 1); |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 456 | BOOST_CHECK_EQUAL(storage.size(), 1); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 457 | |
| 458 | post(block); |
| 459 | post(block); |
| 460 | post(block); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 461 | advanceClocks(1_ms, 10); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 462 | |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 463 | BOOST_REQUIRE_EQUAL(face.sentData.size(), 4); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 464 | BOOST_CHECK_EQUAL(face.sentData[0].getName(), "/root/test/%FE%00"); |
| 465 | BOOST_CHECK_EQUAL(face.sentData[1].getName(), "/root/test/%FE%01"); |
| 466 | BOOST_CHECK_EQUAL(face.sentData[2].getName(), "/root/test/%FE%02"); |
| 467 | BOOST_CHECK_EQUAL(face.sentData[3].getName(), "/root/test/%FE%03"); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 468 | |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 469 | BOOST_CHECK_EQUAL(face.sentData[0].getContent().blockFromValue(), block); |
| 470 | BOOST_CHECK_EQUAL(face.sentData[1].getContent().blockFromValue(), block); |
| 471 | BOOST_CHECK_EQUAL(face.sentData[2].getContent().blockFromValue(), block); |
| 472 | BOOST_CHECK_EQUAL(face.sentData[3].getContent().blockFromValue(), block); |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 473 | |
| 474 | // each version of notification will be sent to both places |
| 475 | std::vector<Data> dataInStorage; |
| 476 | std::copy(storage.begin(), storage.end(), std::back_inserter(dataInStorage)); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 477 | BOOST_REQUIRE_EQUAL(dataInStorage.size(), 4); |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 478 | BOOST_CHECK_EQUAL(dataInStorage[0].getName(), "/root/test/%FE%00"); |
| 479 | BOOST_CHECK_EQUAL(dataInStorage[1].getName(), "/root/test/%FE%01"); |
| 480 | BOOST_CHECK_EQUAL(dataInStorage[2].getName(), "/root/test/%FE%02"); |
| 481 | BOOST_CHECK_EQUAL(dataInStorage[3].getName(), "/root/test/%FE%03"); |
| 482 | |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 483 | BOOST_CHECK_EQUAL(dataInStorage[0].getContent().blockFromValue(), block); |
| 484 | BOOST_CHECK_EQUAL(dataInStorage[1].getContent().blockFromValue(), block); |
| 485 | BOOST_CHECK_EQUAL(dataInStorage[2].getContent().blockFromValue(), block); |
| 486 | BOOST_CHECK_EQUAL(dataInStorage[3].getContent().blockFromValue(), block); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 487 | } |
| 488 | |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 489 | BOOST_AUTO_TEST_SUITE_END() // TestDispatcher |
| 490 | BOOST_AUTO_TEST_SUITE_END() // Mgmt |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 491 | |
| 492 | } // namespace tests |
| 493 | } // namespace mgmt |
| 494 | } // namespace ndn |