Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 3 | * Copyright (c) 2013-2016 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" |
| 23 | #include "management/nfd-control-parameters.hpp" |
| 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 | |
Junxiao Shi | f65a336 | 2015-09-06 20:54:54 -0700 | [diff] [blame] | 37 | BOOST_AUTO_TEST_SUITE(Mgmt) |
| 38 | BOOST_AUTO_TEST_SUITE(TestDispatcher) |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 39 | |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame^] | 40 | class DispatcherFixture : public IdentityManagementTimeFixture |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 41 | { |
| 42 | public: |
| 43 | DispatcherFixture() |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame^] | 44 | : face(io, m_keyChain, {true, true}) |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 45 | , dispatcher(face, m_keyChain, security::SigningInfo()) |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 46 | , storage(dispatcher.m_storage) |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 47 | { |
| 48 | } |
| 49 | |
| 50 | public: |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 51 | util::DummyClientFace face; |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 52 | mgmt::Dispatcher dispatcher; |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 53 | util::InMemoryStorageFifo& storage; |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 54 | }; |
| 55 | |
| 56 | class VoidParameters : public mgmt::ControlParameters |
| 57 | { |
| 58 | public: |
| 59 | explicit |
| 60 | VoidParameters(const Block& wire) |
| 61 | { |
| 62 | wireDecode(wire); |
| 63 | } |
| 64 | |
| 65 | virtual Block |
Davide Pesavento | aa82eb6 | 2016-04-22 19:08:40 +0200 | [diff] [blame] | 66 | wireEncode() const final |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 67 | { |
| 68 | return Block(128); |
| 69 | } |
| 70 | |
| 71 | virtual void |
Davide Pesavento | aa82eb6 | 2016-04-22 19:08:40 +0200 | [diff] [blame] | 72 | wireDecode(const Block& wire) final |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 73 | { |
| 74 | if (wire.type() != 128) |
| 75 | throw tlv::Error("Expecting TLV type 128"); |
| 76 | } |
| 77 | }; |
| 78 | |
| 79 | static Authorization |
| 80 | makeTestAuthorization() |
| 81 | { |
| 82 | return [] (const Name& prefix, |
| 83 | const Interest& interest, |
| 84 | const ControlParameters* params, |
| 85 | AcceptContinuation accept, |
| 86 | RejectContinuation reject) { |
| 87 | if (interest.getName()[-1] == name::Component("valid")) { |
| 88 | accept(""); |
| 89 | } |
| 90 | else { |
| 91 | if (interest.getName()[-1] == name::Component("silent")) { |
| 92 | reject(RejectReply::SILENT); |
| 93 | } |
| 94 | else { |
| 95 | reject(RejectReply::STATUS403); |
| 96 | } |
| 97 | } |
| 98 | }; |
| 99 | } |
| 100 | |
| 101 | BOOST_FIXTURE_TEST_CASE(BasicUsageSemantics, DispatcherFixture) |
| 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 | |
| 148 | BOOST_FIXTURE_TEST_CASE(AddRemoveTopPrefix, DispatcherFixture) |
| 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 | |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 161 | face.receive(*util::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 | |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 169 | face.receive(*util::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 | |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 174 | face.receive(*util::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 | |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 179 | face.receive(*util::makeInterest("/root/2/test/1/%80%00")); |
| 180 | face.receive(*util::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 | |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 188 | face.receive(*util::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 | |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 192 | face.receive(*util::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 | |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 199 | face.receive(*util::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 | |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 203 | face.receive(*util::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 | |
| 208 | BOOST_FIXTURE_TEST_CASE(ControlCommand, DispatcherFixture) |
| 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 | |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 221 | face.receive(*util::makeInterest("/root/test/%80%00")); // returns 403 |
| 222 | face.receive(*util::makeInterest("/root/test/%80%00/invalid")); // returns 403 |
| 223 | face.receive(*util::makeInterest("/root/test/%80%00/silent")); // silently ignored |
| 224 | face.receive(*util::makeInterest("/root/test/.../invalid")); // silently ignored (wrong format) |
| 225 | face.receive(*util::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 | |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 235 | face.receive(*util::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 | |
| 240 | BOOST_FIXTURE_TEST_CASE(StatusDataset, DispatcherFixture) |
| 241 | { |
| 242 | static Block smallBlock("\x81\x01\0x01", 3); |
| 243 | static Block largeBlock = [] () -> Block { |
| 244 | EncodingBuffer encoder; |
| 245 | for (size_t i = 0; i < 2500; ++i) { |
| 246 | encoder.prependByte(1); |
| 247 | } |
| 248 | encoder.prependVarNumber(2500); |
| 249 | encoder.prependVarNumber(129); |
| 250 | return encoder.block(); |
| 251 | }(); |
| 252 | |
| 253 | dispatcher.addStatusDataset("test/small", |
| 254 | makeTestAuthorization(), |
| 255 | [] (const Name& prefix, const Interest& interest, |
Junxiao Shi | f65a336 | 2015-09-06 20:54:54 -0700 | [diff] [blame] | 256 | StatusDatasetContext& context) { |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 257 | context.append(smallBlock); |
| 258 | context.append(smallBlock); |
| 259 | context.append(smallBlock); |
| 260 | context.end(); |
| 261 | }); |
| 262 | |
| 263 | dispatcher.addStatusDataset("test/large", |
| 264 | makeTestAuthorization(), |
| 265 | [] (const Name& prefix, const Interest& interest, |
Junxiao Shi | f65a336 | 2015-09-06 20:54:54 -0700 | [diff] [blame] | 266 | StatusDatasetContext& context) { |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 267 | context.append(largeBlock); |
| 268 | context.append(largeBlock); |
| 269 | context.append(largeBlock); |
| 270 | context.end(); |
| 271 | }); |
| 272 | |
| 273 | dispatcher.addStatusDataset("test/reject", |
| 274 | makeTestAuthorization(), |
| 275 | [] (const Name& prefix, const Interest& interest, |
Junxiao Shi | f65a336 | 2015-09-06 20:54:54 -0700 | [diff] [blame] | 276 | StatusDatasetContext& context) { |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 277 | context.reject(); |
| 278 | }); |
| 279 | |
| 280 | dispatcher.addTopPrefix("/root"); |
| 281 | advanceClocks(time::milliseconds(1)); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 282 | face.sentData.clear(); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 283 | |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 284 | face.receive(*util::makeInterest("/root/test/small/%80%00")); // returns 403 |
| 285 | face.receive(*util::makeInterest("/root/test/small/%80%00/invalid")); // returns 403 |
| 286 | face.receive(*util::makeInterest("/root/test/small/%80%00/silent")); // silently ignored |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 287 | advanceClocks(time::milliseconds(1), 20); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 288 | BOOST_CHECK_EQUAL(face.sentData.size(), 2); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 289 | |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 290 | BOOST_CHECK(face.sentData[0].getContentType() == tlv::ContentType_Blob); |
| 291 | BOOST_CHECK_EQUAL(ControlResponse(face.sentData[0].getContent().blockFromValue()).getCode(), 403); |
| 292 | BOOST_CHECK(face.sentData[1].getContentType() == tlv::ContentType_Blob); |
| 293 | BOOST_CHECK_EQUAL(ControlResponse(face.sentData[1].getContent().blockFromValue()).getCode(), 403); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 294 | |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 295 | face.sentData.clear(); |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 296 | |
| 297 | auto interestSmall = *util::makeInterest("/root/test/small/valid"); |
| 298 | face.receive(interestSmall); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 299 | advanceClocks(time::milliseconds(1), 10); |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 300 | |
| 301 | // one data packet is generated and sent to both places |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 302 | BOOST_CHECK_EQUAL(face.sentData.size(), 1); |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 303 | BOOST_CHECK_EQUAL(storage.size(), 1); |
| 304 | |
| 305 | auto fetchedData = storage.find(interestSmall); |
| 306 | BOOST_REQUIRE(fetchedData != nullptr); |
| 307 | BOOST_CHECK(face.sentData[0].wireEncode() == fetchedData->wireEncode()); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 308 | |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 309 | face.receive(*util::makeInterest(Name("/root/test/small/valid").appendVersion(10))); // should be ignored |
| 310 | face.receive(*util::makeInterest(Name("/root/test/small/valid").appendSegment(20))); // should be ignored |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 311 | advanceClocks(time::milliseconds(1), 10); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 312 | BOOST_CHECK_EQUAL(face.sentData.size(), 1); |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 313 | BOOST_CHECK_EQUAL(storage.size(), 1); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 314 | |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 315 | Block content = face.sentData[0].getContent(); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 316 | BOOST_CHECK_NO_THROW(content.parse()); |
| 317 | |
| 318 | BOOST_CHECK_EQUAL(content.elements().size(), 3); |
| 319 | BOOST_CHECK(content.elements()[0] == smallBlock); |
| 320 | BOOST_CHECK(content.elements()[1] == smallBlock); |
| 321 | BOOST_CHECK(content.elements()[2] == smallBlock); |
| 322 | |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 323 | storage.erase("/", true); // clear the storage |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 324 | face.sentData.clear(); |
| 325 | face.receive(*util::makeInterest("/root/test/large/valid")); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 326 | advanceClocks(time::milliseconds(1), 10); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 327 | |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 328 | // two data packets are generated, the first one will be sent to both places |
| 329 | // while the second one will only be inserted into the in-memory storage |
| 330 | BOOST_CHECK_EQUAL(face.sentData.size(), 1); |
| 331 | BOOST_CHECK_EQUAL(storage.size(), 2); |
| 332 | |
| 333 | // segment0 should be sent through the face |
| 334 | const auto& component = face.sentData[0].getName().at(-1); |
| 335 | BOOST_CHECK(component.isSegment()); |
| 336 | BOOST_CHECK_EQUAL(component.toSegment(), 0); |
| 337 | |
| 338 | std::vector<Data> dataInStorage; |
| 339 | std::copy(storage.begin(), storage.end(), std::back_inserter(dataInStorage)); |
| 340 | |
| 341 | // the Data sent through the face should be the same as the first Data in the storage |
| 342 | BOOST_CHECK_EQUAL(face.sentData[0].getName(), dataInStorage[0].getName()); |
| 343 | BOOST_CHECK(face.sentData[0].getContent() == dataInStorage[0].getContent()); |
| 344 | |
| 345 | content = [&dataInStorage] () -> Block { |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 346 | EncodingBuffer encoder; |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 347 | size_t valueLength = encoder.prependByteArray(dataInStorage[1].getContent().value(), |
| 348 | dataInStorage[1].getContent().value_size()); |
| 349 | valueLength += encoder.prependByteArray(dataInStorage[0].getContent().value(), |
| 350 | dataInStorage[0].getContent().value_size()); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 351 | encoder.prependVarNumber(valueLength); |
| 352 | encoder.prependVarNumber(tlv::Content); |
| 353 | return encoder.block(); |
| 354 | }(); |
| 355 | |
| 356 | BOOST_CHECK_NO_THROW(content.parse()); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 357 | BOOST_CHECK_EQUAL(content.elements().size(), 3); |
| 358 | BOOST_CHECK(content.elements()[0] == largeBlock); |
| 359 | BOOST_CHECK(content.elements()[1] == largeBlock); |
| 360 | BOOST_CHECK(content.elements()[2] == largeBlock); |
| 361 | |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 362 | storage.erase("/", true);// clear the storage |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 363 | face.sentData.clear(); |
| 364 | face.receive(*util::makeInterest("/root/test/reject/%80%00/valid")); // returns nack |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 365 | advanceClocks(time::milliseconds(1)); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 366 | BOOST_CHECK_EQUAL(face.sentData.size(), 1); |
| 367 | BOOST_CHECK(face.sentData[0].getContentType() == tlv::ContentType_Nack); |
| 368 | BOOST_CHECK_EQUAL(ControlResponse(face.sentData[0].getContent().blockFromValue()).getCode(), 400); |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 369 | 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] | 370 | } |
| 371 | |
| 372 | BOOST_FIXTURE_TEST_CASE(NotificationStream, DispatcherFixture) |
| 373 | { |
| 374 | static Block block("\x82\x01\x02", 3); |
| 375 | |
| 376 | auto post = dispatcher.addNotificationStream("test"); |
| 377 | |
| 378 | post(block); |
| 379 | advanceClocks(time::milliseconds(1)); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 380 | BOOST_CHECK_EQUAL(face.sentData.size(), 0); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 381 | |
| 382 | dispatcher.addTopPrefix("/root"); |
| 383 | advanceClocks(time::milliseconds(1)); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 384 | face.sentData.clear(); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 385 | |
| 386 | post(block); |
| 387 | advanceClocks(time::milliseconds(1)); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 388 | BOOST_CHECK_EQUAL(face.sentData.size(), 1); |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 389 | BOOST_CHECK_EQUAL(storage.size(), 1); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 390 | |
| 391 | post(block); |
| 392 | post(block); |
| 393 | post(block); |
| 394 | advanceClocks(time::milliseconds(1), 10); |
| 395 | |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 396 | BOOST_CHECK_EQUAL(face.sentData.size(), 4); |
| 397 | BOOST_CHECK_EQUAL(face.sentData[0].getName(), "/root/test/%FE%00"); |
| 398 | BOOST_CHECK_EQUAL(face.sentData[1].getName(), "/root/test/%FE%01"); |
| 399 | BOOST_CHECK_EQUAL(face.sentData[2].getName(), "/root/test/%FE%02"); |
| 400 | BOOST_CHECK_EQUAL(face.sentData[3].getName(), "/root/test/%FE%03"); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 401 | |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 402 | BOOST_CHECK(face.sentData[0].getContent().blockFromValue() == block); |
| 403 | BOOST_CHECK(face.sentData[1].getContent().blockFromValue() == block); |
| 404 | BOOST_CHECK(face.sentData[2].getContent().blockFromValue() == block); |
| 405 | BOOST_CHECK(face.sentData[3].getContent().blockFromValue() == block); |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 406 | |
| 407 | // each version of notification will be sent to both places |
| 408 | std::vector<Data> dataInStorage; |
| 409 | std::copy(storage.begin(), storage.end(), std::back_inserter(dataInStorage)); |
| 410 | BOOST_CHECK_EQUAL(dataInStorage.size(), 4); |
| 411 | BOOST_CHECK_EQUAL(dataInStorage[0].getName(), "/root/test/%FE%00"); |
| 412 | BOOST_CHECK_EQUAL(dataInStorage[1].getName(), "/root/test/%FE%01"); |
| 413 | BOOST_CHECK_EQUAL(dataInStorage[2].getName(), "/root/test/%FE%02"); |
| 414 | BOOST_CHECK_EQUAL(dataInStorage[3].getName(), "/root/test/%FE%03"); |
| 415 | |
| 416 | BOOST_CHECK(dataInStorage[0].getContent().blockFromValue() == block); |
| 417 | BOOST_CHECK(dataInStorage[1].getContent().blockFromValue() == block); |
| 418 | BOOST_CHECK(dataInStorage[2].getContent().blockFromValue() == block); |
| 419 | BOOST_CHECK(dataInStorage[3].getContent().blockFromValue() == block); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 420 | } |
| 421 | |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 422 | BOOST_AUTO_TEST_SUITE_END() // TestDispatcher |
| 423 | BOOST_AUTO_TEST_SUITE_END() // Mgmt |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 424 | |
| 425 | } // namespace tests |
| 426 | } // namespace mgmt |
| 427 | } // namespace ndn |