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