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 | cb385e3 | 2024-12-27 15:22:02 -0500 | [diff] [blame^] | 3 | * Copyright (c) 2013-2025 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 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 22 | #include "ndn-cxx/mgmt/dispatcher.hpp" |
Davide Pesavento | cb385e3 | 2024-12-27 15:22:02 -0500 | [diff] [blame^] | 23 | #include "ndn-cxx/mgmt/nfd/control-command.hpp" |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 24 | #include "ndn-cxx/util/dummy-client-face.hpp" |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 25 | |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 26 | #include "tests/test-common.hpp" |
| 27 | #include "tests/unit/io-key-chain-fixture.hpp" |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 28 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 29 | namespace ndn::tests { |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 30 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 31 | using namespace ndn::mgmt; |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 32 | |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 33 | class DispatcherFixture : public IoKeyChainFixture |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 34 | { |
Davide Pesavento | 54dfc4a | 2024-12-26 17:30:41 -0500 | [diff] [blame] | 35 | protected: |
| 36 | DummyClientFace face{m_io, m_keyChain, {true, true}}; |
| 37 | Dispatcher dispatcher{face, m_keyChain}; |
| 38 | InMemoryStorageFifo& storage{dispatcher.m_storage}; |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 39 | }; |
| 40 | |
| 41 | class VoidParameters : public mgmt::ControlParameters |
| 42 | { |
| 43 | public: |
| 44 | explicit |
| 45 | VoidParameters(const Block& wire) |
| 46 | { |
| 47 | wireDecode(wire); |
| 48 | } |
| 49 | |
Junxiao Shi | d97c953 | 2017-04-27 16:17:04 +0000 | [diff] [blame] | 50 | Block |
Davide Pesavento | aa82eb6 | 2016-04-22 19:08:40 +0200 | [diff] [blame] | 51 | wireEncode() const final |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 52 | { |
| 53 | return Block(128); |
| 54 | } |
| 55 | |
Junxiao Shi | d97c953 | 2017-04-27 16:17:04 +0000 | [diff] [blame] | 56 | void |
Davide Pesavento | aa82eb6 | 2016-04-22 19:08:40 +0200 | [diff] [blame] | 57 | wireDecode(const Block& wire) final |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 58 | { |
| 59 | if (wire.type() != 128) |
Davide Pesavento | 923ba44 | 2019-02-12 22:00:38 -0500 | [diff] [blame] | 60 | NDN_THROW(tlv::Error("Expecting TLV type 128")); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 61 | } |
| 62 | }; |
| 63 | |
| 64 | static Authorization |
| 65 | makeTestAuthorization() |
| 66 | { |
Davide Pesavento | 3c34ec1 | 2021-03-28 21:50:06 -0400 | [diff] [blame] | 67 | return [] (const Name&, const Interest& interest, const ControlParameters*, |
| 68 | AcceptContinuation accept, RejectContinuation reject) { |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 69 | if (interest.getName()[-1] == name::Component("valid")) { |
| 70 | accept(""); |
| 71 | } |
| 72 | else { |
| 73 | if (interest.getName()[-1] == name::Component("silent")) { |
| 74 | reject(RejectReply::SILENT); |
| 75 | } |
| 76 | else { |
| 77 | reject(RejectReply::STATUS403); |
| 78 | } |
| 79 | } |
| 80 | }; |
| 81 | } |
| 82 | |
Junxiao Shi | d97c953 | 2017-04-27 16:17:04 +0000 | [diff] [blame] | 83 | BOOST_AUTO_TEST_SUITE(Mgmt) |
| 84 | BOOST_FIXTURE_TEST_SUITE(TestDispatcher, DispatcherFixture) |
| 85 | |
| 86 | BOOST_AUTO_TEST_CASE(Basic) |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 87 | { |
Davide Pesavento | cb385e3 | 2024-12-27 15:22:02 -0500 | [diff] [blame^] | 88 | auto nop = [] (auto&&...) {}; |
| 89 | auto tautology = [] (auto&&...) { return true; }; |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 90 | |
Davide Pesavento | cb385e3 | 2024-12-27 15:22:02 -0500 | [diff] [blame^] | 91 | BOOST_CHECK_NO_THROW(dispatcher |
| 92 | .addControlCommand<VoidParameters>("test/1", makeAcceptAllAuthorization(), |
| 93 | tautology, nop)); |
| 94 | BOOST_CHECK_NO_THROW(dispatcher |
| 95 | .addControlCommand<VoidParameters>("test/2", makeAcceptAllAuthorization(), |
| 96 | tautology, nop)); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 97 | BOOST_CHECK_THROW(dispatcher |
Davide Pesavento | cb385e3 | 2024-12-27 15:22:02 -0500 | [diff] [blame^] | 98 | .addControlCommand<VoidParameters>("test", makeAcceptAllAuthorization(), |
| 99 | tautology, nop), |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 100 | std::out_of_range); |
| 101 | |
Davide Pesavento | cb385e3 | 2024-12-27 15:22:02 -0500 | [diff] [blame^] | 102 | BOOST_CHECK_NO_THROW(dispatcher.addStatusDataset("status/1", makeAcceptAllAuthorization(), nop)); |
| 103 | BOOST_CHECK_NO_THROW(dispatcher.addStatusDataset("status/2", makeAcceptAllAuthorization(), nop)); |
| 104 | BOOST_CHECK_THROW(dispatcher.addStatusDataset("status", makeAcceptAllAuthorization(), nop), |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 105 | std::out_of_range); |
| 106 | |
| 107 | BOOST_CHECK_NO_THROW(dispatcher.addNotificationStream("stream/1")); |
| 108 | BOOST_CHECK_NO_THROW(dispatcher.addNotificationStream("stream/2")); |
| 109 | BOOST_CHECK_THROW(dispatcher.addNotificationStream("stream"), std::out_of_range); |
| 110 | |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 111 | BOOST_CHECK_NO_THROW(dispatcher.addTopPrefix("/root/1")); |
| 112 | BOOST_CHECK_NO_THROW(dispatcher.addTopPrefix("/root/2")); |
| 113 | BOOST_CHECK_THROW(dispatcher.addTopPrefix("/root"), std::out_of_range); |
| 114 | |
| 115 | BOOST_CHECK_THROW(dispatcher |
Davide Pesavento | cb385e3 | 2024-12-27 15:22:02 -0500 | [diff] [blame^] | 116 | .addControlCommand<VoidParameters>("test/3", makeAcceptAllAuthorization(), |
| 117 | tautology, nop), |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 118 | std::domain_error); |
| 119 | |
Davide Pesavento | cb385e3 | 2024-12-27 15:22:02 -0500 | [diff] [blame^] | 120 | BOOST_CHECK_THROW(dispatcher.addStatusDataset("status/3", makeAcceptAllAuthorization(), nop), |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 121 | std::domain_error); |
| 122 | |
| 123 | BOOST_CHECK_THROW(dispatcher.addNotificationStream("stream/3"), std::domain_error); |
| 124 | } |
| 125 | |
Junxiao Shi | d97c953 | 2017-04-27 16:17:04 +0000 | [diff] [blame] | 126 | BOOST_AUTO_TEST_CASE(AddRemoveTopPrefix) |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 127 | { |
| 128 | std::map<std::string, size_t> nCallbackCalled; |
| 129 | dispatcher |
| 130 | .addControlCommand<VoidParameters>("test/1", makeAcceptAllAuthorization(), |
Davide Pesavento | cb385e3 | 2024-12-27 15:22:02 -0500 | [diff] [blame^] | 131 | [] (auto&&...) { return true; }, |
| 132 | [&nCallbackCalled] (auto&&...) { ++nCallbackCalled["test/1"]; }); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 133 | |
| 134 | dispatcher |
| 135 | .addControlCommand<VoidParameters>("test/2", makeAcceptAllAuthorization(), |
Davide Pesavento | cb385e3 | 2024-12-27 15:22:02 -0500 | [diff] [blame^] | 136 | [] (auto&&...) { return true; }, |
| 137 | [&nCallbackCalled] (auto&&...) { ++nCallbackCalled["test/2"]; }); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 138 | |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 139 | face.receive(*makeInterest("/root/1/test/1/%80%00")); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 140 | advanceClocks(1_ms); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 141 | BOOST_CHECK_EQUAL(nCallbackCalled["test/1"], 0); |
| 142 | BOOST_CHECK_EQUAL(nCallbackCalled["test/2"], 0); |
| 143 | |
| 144 | dispatcher.addTopPrefix("/root/1"); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 145 | advanceClocks(1_ms); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 146 | |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 147 | face.receive(*makeInterest("/root/1/test/1/%80%00")); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 148 | advanceClocks(1_ms); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 149 | BOOST_CHECK_EQUAL(nCallbackCalled["test/1"], 1); |
| 150 | BOOST_CHECK_EQUAL(nCallbackCalled["test/2"], 0); |
| 151 | |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 152 | face.receive(*makeInterest("/root/1/test/2/%80%00")); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 153 | advanceClocks(1_ms); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 154 | BOOST_CHECK_EQUAL(nCallbackCalled["test/1"], 1); |
| 155 | BOOST_CHECK_EQUAL(nCallbackCalled["test/2"], 1); |
| 156 | |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 157 | face.receive(*makeInterest("/root/2/test/1/%80%00")); |
| 158 | face.receive(*makeInterest("/root/2/test/2/%80%00")); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 159 | advanceClocks(1_ms); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 160 | BOOST_CHECK_EQUAL(nCallbackCalled["test/1"], 1); |
| 161 | BOOST_CHECK_EQUAL(nCallbackCalled["test/2"], 1); |
| 162 | |
| 163 | dispatcher.addTopPrefix("/root/2"); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 164 | advanceClocks(1_ms); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 165 | |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 166 | face.receive(*makeInterest("/root/1/test/1/%80%00")); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 167 | advanceClocks(1_ms); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 168 | BOOST_CHECK_EQUAL(nCallbackCalled["test/1"], 2); |
| 169 | |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 170 | face.receive(*makeInterest("/root/2/test/1/%80%00")); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 171 | advanceClocks(1_ms); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 172 | BOOST_CHECK_EQUAL(nCallbackCalled["test/1"], 3); |
| 173 | |
| 174 | dispatcher.removeTopPrefix("/root/1"); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 175 | advanceClocks(1_ms); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 176 | |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 177 | face.receive(*makeInterest("/root/1/test/1/%80%00")); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 178 | advanceClocks(1_ms); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 179 | BOOST_CHECK_EQUAL(nCallbackCalled["test/1"], 3); |
| 180 | |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 181 | face.receive(*makeInterest("/root/2/test/1/%80%00")); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 182 | advanceClocks(1_ms); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 183 | BOOST_CHECK_EQUAL(nCallbackCalled["test/1"], 4); |
| 184 | } |
| 185 | |
Davide Pesavento | cb385e3 | 2024-12-27 15:22:02 -0500 | [diff] [blame^] | 186 | BOOST_AUTO_TEST_CASE(ControlCommandOld, |
| 187 | * ut::description("test old-style ControlCommand registration")) |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 188 | { |
| 189 | size_t nCallbackCalled = 0; |
Davide Pesavento | cb385e3 | 2024-12-27 15:22:02 -0500 | [diff] [blame^] | 190 | auto handler = [&nCallbackCalled] (auto&&...) { ++nCallbackCalled; }; |
| 191 | dispatcher.addControlCommand<VoidParameters>("test", makeTestAuthorization(), |
| 192 | [] (auto&&...) { return true; }, std::move(handler)); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 193 | |
| 194 | dispatcher.addTopPrefix("/root"); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 195 | advanceClocks(1_ms); |
Davide Pesavento | cb385e3 | 2024-12-27 15:22:02 -0500 | [diff] [blame^] | 196 | BOOST_REQUIRE_EQUAL(face.sentData.size(), 0); |
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/test/%80%00")); // returns 403 |
| 199 | face.receive(*makeInterest("/root/test/%80%00/invalid")); // returns 403 |
| 200 | face.receive(*makeInterest("/root/test/%80%00/silent")); // silently ignored |
| 201 | face.receive(*makeInterest("/root/test/.../invalid")); // silently ignored (wrong format) |
| 202 | face.receive(*makeInterest("/root/test/.../valid")); // silently ignored (wrong format) |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 203 | advanceClocks(1_ms, 20); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 204 | BOOST_CHECK_EQUAL(nCallbackCalled, 0); |
Davide Pesavento | cb385e3 | 2024-12-27 15:22:02 -0500 | [diff] [blame^] | 205 | BOOST_REQUIRE_EQUAL(face.sentData.size(), 2); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 206 | |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 207 | BOOST_CHECK_EQUAL(face.sentData[0].getContentType(), tlv::ContentType_Blob); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 208 | BOOST_CHECK_EQUAL(ControlResponse(face.sentData[0].getContent().blockFromValue()).getCode(), 403); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 209 | BOOST_CHECK_EQUAL(face.sentData[1].getContentType(), tlv::ContentType_Blob); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 210 | BOOST_CHECK_EQUAL(ControlResponse(face.sentData[1].getContent().blockFromValue()).getCode(), 403); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 211 | |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 212 | face.receive(*makeInterest("/root/test/%80%00/valid")); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 213 | advanceClocks(1_ms, 10); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 214 | BOOST_CHECK_EQUAL(nCallbackCalled, 1); |
| 215 | } |
| 216 | |
Davide Pesavento | cb385e3 | 2024-12-27 15:22:02 -0500 | [diff] [blame^] | 217 | BOOST_AUTO_TEST_CASE(ControlCommandNew, |
| 218 | * ut::description("test new-style ControlCommand registration")) |
| 219 | { |
| 220 | size_t nHandlerCalled = 0; |
| 221 | auto handler = [&nHandlerCalled] (auto&&...) { ++nHandlerCalled; }; |
| 222 | |
| 223 | // test addControlCommand() |
| 224 | dispatcher.addControlCommand<nfd::FaceCreateCommand>(makeAcceptAllAuthorization(), handler); |
| 225 | dispatcher.addControlCommand<nfd::FaceUpdateCommand>(makeAcceptAllAuthorization(), handler); |
| 226 | dispatcher.addControlCommand<nfd::FaceDestroyCommand>(makeAcceptAllAuthorization(), handler); |
| 227 | dispatcher.addControlCommand<nfd::FibAddNextHopCommand>(makeAcceptAllAuthorization(), handler); |
| 228 | dispatcher.addControlCommand<nfd::FibRemoveNextHopCommand>(makeAcceptAllAuthorization(), handler); |
| 229 | dispatcher.addControlCommand<nfd::CsConfigCommand>(makeAcceptAllAuthorization(), handler); |
| 230 | dispatcher.addControlCommand<nfd::CsEraseCommand>(makeAcceptAllAuthorization(), handler); |
| 231 | dispatcher.addControlCommand<nfd::StrategyChoiceSetCommand>(makeAcceptAllAuthorization(), handler); |
| 232 | dispatcher.addControlCommand<nfd::StrategyChoiceUnsetCommand>(makeAcceptAllAuthorization(), handler); |
| 233 | dispatcher.addControlCommand<nfd::RibRegisterCommand>(makeAcceptAllAuthorization(), handler); |
| 234 | dispatcher.addControlCommand<nfd::RibUnregisterCommand>(makeAcceptAllAuthorization(), handler); |
| 235 | |
| 236 | BOOST_CHECK_THROW(dispatcher.addControlCommand<nfd::CsConfigCommand>(makeAcceptAllAuthorization(), |
| 237 | [] (auto&&...) {}), |
| 238 | std::out_of_range); |
| 239 | |
| 240 | dispatcher.addTopPrefix("/root"); |
| 241 | advanceClocks(1_ms); |
| 242 | BOOST_REQUIRE_EQUAL(face.sentData.size(), 0); |
| 243 | |
| 244 | BOOST_CHECK_THROW(dispatcher.addControlCommand<nfd::CsConfigCommand>(makeAcceptAllAuthorization(), |
| 245 | [] (auto&&...) {}), |
| 246 | std::domain_error); |
| 247 | |
| 248 | // we pick FaceDestroyCommand as an example for the following tests |
| 249 | |
| 250 | // malformed request (missing ControlParameters) => silently ignored |
| 251 | auto baseName = Name("/root").append(nfd::FaceDestroyCommand::getName()); |
| 252 | auto interest = makeInterest(baseName); |
| 253 | face.receive(*interest); |
| 254 | advanceClocks(1_ms); |
| 255 | BOOST_CHECK_EQUAL(nHandlerCalled, 0); |
| 256 | BOOST_CHECK_EQUAL(face.sentData.size(), 0); |
| 257 | |
| 258 | // ControlParameters present but invalid (missing required field) => reply with error 400 |
| 259 | nfd::ControlParameters params; |
| 260 | interest->setName(Name(baseName).append(params.wireEncode())); |
| 261 | face.receive(*interest); |
| 262 | advanceClocks(1_ms); |
| 263 | BOOST_CHECK_EQUAL(nHandlerCalled, 0); |
| 264 | BOOST_REQUIRE_EQUAL(face.sentData.size(), 1); |
| 265 | BOOST_CHECK_EQUAL(face.sentData[0].getContentType(), tlv::ContentType_Blob); |
| 266 | BOOST_CHECK_EQUAL(ControlResponse(face.sentData[0].getContent().blockFromValue()).getCode(), 400); |
| 267 | |
| 268 | // valid request |
| 269 | params.setFaceId(42); |
| 270 | interest->setName(Name(baseName).append(params.wireEncode())); |
| 271 | face.receive(*interest); |
| 272 | advanceClocks(1_ms); |
| 273 | BOOST_CHECK_EQUAL(nHandlerCalled, 1); |
| 274 | BOOST_CHECK_EQUAL(face.sentData.size(), 1); |
| 275 | } |
| 276 | |
| 277 | BOOST_AUTO_TEST_CASE(ControlCommandResponse) |
| 278 | { |
| 279 | auto handler = [] (const Name& prefix, const Interest& interest, |
| 280 | const ControlParameters&, const CommandContinuation& done) { |
| 281 | BOOST_CHECK_EQUAL(prefix, "/root"); |
| 282 | BOOST_CHECK_EQUAL(interest.getName().getPrefix(3), |
| 283 | Name("/root").append(nfd::CsConfigCommand::getName())); |
| 284 | done(ControlResponse(42, "the answer")); |
| 285 | }; |
| 286 | |
| 287 | // use CsConfigCommand as an example |
| 288 | dispatcher.addControlCommand<nfd::CsConfigCommand>(makeAcceptAllAuthorization(), handler); |
| 289 | dispatcher.addTopPrefix("/root"); |
| 290 | advanceClocks(1_ms); |
| 291 | BOOST_REQUIRE_EQUAL(face.sentData.size(), 0); |
| 292 | |
| 293 | nfd::ControlParameters params; |
| 294 | auto interest = makeInterest(Name("/root") |
| 295 | .append(nfd::CsConfigCommand::getName()) |
| 296 | .append(params.wireEncode())); |
| 297 | face.receive(*interest); |
| 298 | advanceClocks(1_ms, 10); |
| 299 | |
| 300 | BOOST_REQUIRE_EQUAL(face.sentData.size(), 1); |
| 301 | BOOST_CHECK_EQUAL(face.sentData[0].getContentType(), tlv::ContentType_Blob); |
| 302 | ControlResponse resp(face.sentData[0].getContent().blockFromValue()); |
| 303 | BOOST_CHECK_EQUAL(resp.getCode(), 42); |
| 304 | BOOST_CHECK_EQUAL(resp.getText(), "the answer"); |
| 305 | } |
| 306 | |
Junxiao Shi | d97c953 | 2017-04-27 16:17:04 +0000 | [diff] [blame] | 307 | class StatefulParameters : public mgmt::ControlParameters |
| 308 | { |
| 309 | public: |
| 310 | explicit |
| 311 | StatefulParameters(const Block& wire) |
| 312 | { |
| 313 | wireDecode(wire); |
| 314 | } |
| 315 | |
| 316 | Block |
| 317 | wireEncode() const final |
| 318 | { |
Davide Pesavento | fbea4fc | 2022-02-08 07:26:04 -0500 | [diff] [blame] | 319 | return {}; |
Junxiao Shi | d97c953 | 2017-04-27 16:17:04 +0000 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | void |
Davide Pesavento | 3c34ec1 | 2021-03-28 21:50:06 -0400 | [diff] [blame] | 323 | wireDecode(const Block&) final |
Junxiao Shi | d97c953 | 2017-04-27 16:17:04 +0000 | [diff] [blame] | 324 | { |
| 325 | m_state = EXPECTED_STATE; |
| 326 | } |
| 327 | |
| 328 | bool |
| 329 | check() const |
| 330 | { |
| 331 | return m_state == EXPECTED_STATE; |
| 332 | } |
| 333 | |
| 334 | private: |
| 335 | static constexpr int EXPECTED_STATE = 12602; |
| 336 | int m_state = 0; |
| 337 | }; |
| 338 | |
Davide Pesavento | 0c52603 | 2024-01-31 21:14:01 -0500 | [diff] [blame] | 339 | BOOST_AUTO_TEST_CASE(ControlCommandAsyncAuthorization, |
| 340 | * ut::description("test for bug #4059")) |
Junxiao Shi | d97c953 | 2017-04-27 16:17:04 +0000 | [diff] [blame] | 341 | { |
| 342 | AcceptContinuation authorizationAccept; |
Davide Pesavento | 54dfc4a | 2024-12-26 17:30:41 -0500 | [diff] [blame] | 343 | auto authorization = [&authorizationAccept] (const Name&, const Interest&, const ControlParameters*, |
| 344 | AcceptContinuation accept, RejectContinuation) { |
| 345 | authorizationAccept = std::move(accept); |
| 346 | }; |
Junxiao Shi | d97c953 | 2017-04-27 16:17:04 +0000 | [diff] [blame] | 347 | |
Davide Pesavento | 54dfc4a | 2024-12-26 17:30:41 -0500 | [diff] [blame] | 348 | auto validateParameters = [] (const ControlParameters& params) { |
| 349 | return dynamic_cast<const StatefulParameters&>(params).check(); |
| 350 | }; |
Junxiao Shi | d97c953 | 2017-04-27 16:17:04 +0000 | [diff] [blame] | 351 | |
| 352 | size_t nCallbackCalled = 0; |
Davide Pesavento | 3c34ec1 | 2021-03-28 21:50:06 -0400 | [diff] [blame] | 353 | dispatcher.addControlCommand<StatefulParameters>("test", authorization, validateParameters, |
Davide Pesavento | cb385e3 | 2024-12-27 15:22:02 -0500 | [diff] [blame^] | 354 | [&nCallbackCalled] (auto&&...) { ++nCallbackCalled; }); |
Junxiao Shi | d97c953 | 2017-04-27 16:17:04 +0000 | [diff] [blame] | 355 | |
| 356 | dispatcher.addTopPrefix("/root"); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 357 | advanceClocks(1_ms); |
Junxiao Shi | d97c953 | 2017-04-27 16:17:04 +0000 | [diff] [blame] | 358 | |
| 359 | face.receive(*makeInterest("/root/test/%80%00")); |
| 360 | BOOST_CHECK_EQUAL(nCallbackCalled, 0); |
| 361 | BOOST_REQUIRE(authorizationAccept != nullptr); |
| 362 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 363 | advanceClocks(1_ms); |
Junxiao Shi | d97c953 | 2017-04-27 16:17:04 +0000 | [diff] [blame] | 364 | authorizationAccept(""); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 365 | advanceClocks(1_ms); |
Junxiao Shi | d97c953 | 2017-04-27 16:17:04 +0000 | [diff] [blame] | 366 | BOOST_CHECK_EQUAL(nCallbackCalled, 1); |
| 367 | } |
| 368 | |
| 369 | BOOST_AUTO_TEST_CASE(StatusDataset) |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 370 | { |
Davide Pesavento | fbea4fc | 2022-02-08 07:26:04 -0500 | [diff] [blame] | 371 | const Block smallBlock({0x81, 0x01, 0x01}); |
Davide Pesavento | 3c34ec1 | 2021-03-28 21:50:06 -0400 | [diff] [blame] | 372 | const Block largeBlock = [] { |
| 373 | Block b(129, std::make_shared<const Buffer>(3000)); |
| 374 | b.encode(); |
| 375 | return b; |
| 376 | }(); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 377 | |
| 378 | dispatcher.addStatusDataset("test/small", |
| 379 | makeTestAuthorization(), |
Davide Pesavento | 3c34ec1 | 2021-03-28 21:50:06 -0400 | [diff] [blame] | 380 | [&smallBlock] (const Name&, const Interest&, |
Davide Pesavento | b10024c | 2017-09-22 01:36:44 -0400 | [diff] [blame] | 381 | StatusDatasetContext& context) { |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 382 | context.append(smallBlock); |
| 383 | context.append(smallBlock); |
| 384 | context.append(smallBlock); |
| 385 | context.end(); |
| 386 | }); |
| 387 | |
| 388 | dispatcher.addStatusDataset("test/large", |
| 389 | makeTestAuthorization(), |
Davide Pesavento | 3c34ec1 | 2021-03-28 21:50:06 -0400 | [diff] [blame] | 390 | [&largeBlock] (const Name&, const Interest&, |
Davide Pesavento | b10024c | 2017-09-22 01:36:44 -0400 | [diff] [blame] | 391 | StatusDatasetContext& context) { |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 392 | context.append(largeBlock); |
| 393 | context.append(largeBlock); |
| 394 | context.append(largeBlock); |
| 395 | context.end(); |
| 396 | }); |
| 397 | |
| 398 | dispatcher.addStatusDataset("test/reject", |
| 399 | makeTestAuthorization(), |
Davide Pesavento | 3c34ec1 | 2021-03-28 21:50:06 -0400 | [diff] [blame] | 400 | [] (const Name&, const Interest&, StatusDatasetContext& context) { |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 401 | context.reject(); |
| 402 | }); |
| 403 | |
| 404 | dispatcher.addTopPrefix("/root"); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 405 | advanceClocks(1_ms); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 406 | face.sentData.clear(); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 407 | |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 408 | face.receive(*makeInterest("/root/test/small/%80%00")); // returns 403 |
| 409 | face.receive(*makeInterest("/root/test/small/%80%00/invalid")); // returns 403 |
| 410 | face.receive(*makeInterest("/root/test/small/%80%00/silent")); // silently ignored |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 411 | advanceClocks(1_ms, 20); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 412 | |
Davide Pesavento | 3c34ec1 | 2021-03-28 21:50:06 -0400 | [diff] [blame] | 413 | BOOST_REQUIRE_EQUAL(face.sentData.size(), 2); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 414 | BOOST_CHECK_EQUAL(face.sentData[0].getContentType(), tlv::ContentType_Blob); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 415 | BOOST_CHECK_EQUAL(ControlResponse(face.sentData[0].getContent().blockFromValue()).getCode(), 403); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 416 | BOOST_CHECK_EQUAL(face.sentData[1].getContentType(), tlv::ContentType_Blob); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 417 | BOOST_CHECK_EQUAL(ControlResponse(face.sentData[1].getContent().blockFromValue()).getCode(), 403); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 418 | |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 419 | face.sentData.clear(); |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 420 | |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 421 | auto interestSmall = *makeInterest("/root/test/small/valid", true); |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 422 | face.receive(interestSmall); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 423 | advanceClocks(1_ms, 10); |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 424 | |
| 425 | // one data packet is generated and sent to both places |
Davide Pesavento | 3c34ec1 | 2021-03-28 21:50:06 -0400 | [diff] [blame] | 426 | BOOST_REQUIRE_EQUAL(face.sentData.size(), 1); |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 427 | BOOST_CHECK_EQUAL(storage.size(), 1); |
| 428 | |
| 429 | auto fetchedData = storage.find(interestSmall); |
| 430 | BOOST_REQUIRE(fetchedData != nullptr); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 431 | BOOST_CHECK_EQUAL(face.sentData[0].wireEncode(), fetchedData->wireEncode()); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 432 | |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 433 | face.receive(*makeInterest(Name("/root/test/small/valid").appendVersion(10))); // should be ignored |
| 434 | 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] | 435 | advanceClocks(1_ms, 10); |
Davide Pesavento | 3c34ec1 | 2021-03-28 21:50:06 -0400 | [diff] [blame] | 436 | BOOST_REQUIRE_EQUAL(face.sentData.size(), 1); |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 437 | BOOST_CHECK_EQUAL(storage.size(), 1); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 438 | |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 439 | Block content = face.sentData[0].getContent(); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 440 | BOOST_CHECK_NO_THROW(content.parse()); |
| 441 | |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 442 | BOOST_REQUIRE_EQUAL(content.elements().size(), 3); |
| 443 | BOOST_CHECK_EQUAL(content.elements()[0], smallBlock); |
| 444 | BOOST_CHECK_EQUAL(content.elements()[1], smallBlock); |
| 445 | BOOST_CHECK_EQUAL(content.elements()[2], smallBlock); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 446 | |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 447 | storage.erase("/", true); // clear the storage |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 448 | face.sentData.clear(); |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 449 | face.receive(*makeInterest("/root/test/large/valid")); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 450 | advanceClocks(1_ms, 10); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 451 | |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 452 | // two data packets are generated, the first one will be sent to both places |
| 453 | // while the second one will only be inserted into the in-memory storage |
Davide Pesavento | 3c34ec1 | 2021-03-28 21:50:06 -0400 | [diff] [blame] | 454 | BOOST_REQUIRE_EQUAL(face.sentData.size(), 1); |
| 455 | BOOST_REQUIRE_EQUAL(storage.size(), 2); |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 456 | |
| 457 | // segment0 should be sent through the face |
| 458 | const auto& component = face.sentData[0].getName().at(-1); |
| 459 | BOOST_CHECK(component.isSegment()); |
| 460 | BOOST_CHECK_EQUAL(component.toSegment(), 0); |
| 461 | |
| 462 | std::vector<Data> dataInStorage; |
| 463 | std::copy(storage.begin(), storage.end(), std::back_inserter(dataInStorage)); |
| 464 | |
| 465 | // the Data sent through the face should be the same as the first Data in the storage |
| 466 | BOOST_CHECK_EQUAL(face.sentData[0].getName(), dataInStorage[0].getName()); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 467 | BOOST_CHECK_EQUAL(face.sentData[0].getContent(), dataInStorage[0].getContent()); |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 468 | |
| 469 | content = [&dataInStorage] () -> Block { |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 470 | EncodingBuffer encoder; |
Davide Pesavento | 258d51a | 2022-02-27 21:26:28 -0500 | [diff] [blame] | 471 | size_t valueLength = encoder.prependBytes(dataInStorage[1].getContent().value_bytes()); |
| 472 | valueLength += encoder.prependBytes(dataInStorage[0].getContent().value_bytes()); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 473 | encoder.prependVarNumber(valueLength); |
| 474 | encoder.prependVarNumber(tlv::Content); |
| 475 | return encoder.block(); |
| 476 | }(); |
| 477 | |
| 478 | BOOST_CHECK_NO_THROW(content.parse()); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 479 | BOOST_REQUIRE_EQUAL(content.elements().size(), 3); |
| 480 | BOOST_CHECK_EQUAL(content.elements()[0], largeBlock); |
| 481 | BOOST_CHECK_EQUAL(content.elements()[1], largeBlock); |
| 482 | BOOST_CHECK_EQUAL(content.elements()[2], largeBlock); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 483 | |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 484 | storage.erase("/", true);// clear the storage |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 485 | face.sentData.clear(); |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 486 | face.receive(*makeInterest("/root/test/reject/%80%00/valid")); // returns nack |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 487 | advanceClocks(1_ms); |
Davide Pesavento | 3c34ec1 | 2021-03-28 21:50:06 -0400 | [diff] [blame] | 488 | |
| 489 | BOOST_REQUIRE_EQUAL(face.sentData.size(), 1); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 490 | BOOST_CHECK_EQUAL(face.sentData[0].getContentType(), tlv::ContentType_Nack); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 491 | BOOST_CHECK_EQUAL(ControlResponse(face.sentData[0].getContent().blockFromValue()).getCode(), 400); |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 492 | 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] | 493 | } |
| 494 | |
Junxiao Shi | d97c953 | 2017-04-27 16:17:04 +0000 | [diff] [blame] | 495 | BOOST_AUTO_TEST_CASE(NotificationStream) |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 496 | { |
Davide Pesavento | fbea4fc | 2022-02-08 07:26:04 -0500 | [diff] [blame] | 497 | const Block block({0x82, 0x01, 0x02}); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 498 | auto post = dispatcher.addNotificationStream("test"); |
| 499 | |
| 500 | post(block); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 501 | advanceClocks(1_ms); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 502 | BOOST_CHECK_EQUAL(face.sentData.size(), 0); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 503 | |
| 504 | dispatcher.addTopPrefix("/root"); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 505 | advanceClocks(1_ms); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 506 | face.sentData.clear(); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 507 | |
| 508 | post(block); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 509 | advanceClocks(1_ms); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 510 | BOOST_CHECK_EQUAL(face.sentData.size(), 1); |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 511 | BOOST_CHECK_EQUAL(storage.size(), 1); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 512 | |
| 513 | post(block); |
| 514 | post(block); |
| 515 | post(block); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 516 | advanceClocks(1_ms, 10); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 517 | |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 518 | BOOST_REQUIRE_EQUAL(face.sentData.size(), 4); |
Eric Newberry | c25e463 | 2021-02-11 10:48:11 -0800 | [diff] [blame] | 519 | BOOST_CHECK_EQUAL(face.sentData[0].getName(), "/root/test/seq=0"); |
| 520 | BOOST_CHECK_EQUAL(face.sentData[1].getName(), "/root/test/seq=1"); |
| 521 | BOOST_CHECK_EQUAL(face.sentData[2].getName(), "/root/test/seq=2"); |
| 522 | BOOST_CHECK_EQUAL(face.sentData[3].getName(), "/root/test/seq=3"); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 523 | |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 524 | BOOST_CHECK_EQUAL(face.sentData[0].getContent().blockFromValue(), block); |
| 525 | BOOST_CHECK_EQUAL(face.sentData[1].getContent().blockFromValue(), block); |
| 526 | BOOST_CHECK_EQUAL(face.sentData[2].getContent().blockFromValue(), block); |
| 527 | BOOST_CHECK_EQUAL(face.sentData[3].getContent().blockFromValue(), block); |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 528 | |
| 529 | // each version of notification will be sent to both places |
| 530 | std::vector<Data> dataInStorage; |
| 531 | std::copy(storage.begin(), storage.end(), std::back_inserter(dataInStorage)); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 532 | BOOST_REQUIRE_EQUAL(dataInStorage.size(), 4); |
Eric Newberry | c25e463 | 2021-02-11 10:48:11 -0800 | [diff] [blame] | 533 | BOOST_CHECK_EQUAL(dataInStorage[0].getName(), "/root/test/seq=0"); |
| 534 | BOOST_CHECK_EQUAL(dataInStorage[1].getName(), "/root/test/seq=1"); |
| 535 | BOOST_CHECK_EQUAL(dataInStorage[2].getName(), "/root/test/seq=2"); |
| 536 | BOOST_CHECK_EQUAL(dataInStorage[3].getName(), "/root/test/seq=3"); |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 537 | |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 538 | BOOST_CHECK_EQUAL(dataInStorage[0].getContent().blockFromValue(), block); |
| 539 | BOOST_CHECK_EQUAL(dataInStorage[1].getContent().blockFromValue(), block); |
| 540 | BOOST_CHECK_EQUAL(dataInStorage[2].getContent().blockFromValue(), block); |
| 541 | BOOST_CHECK_EQUAL(dataInStorage[3].getContent().blockFromValue(), block); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 542 | } |
| 543 | |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 544 | BOOST_AUTO_TEST_SUITE_END() // TestDispatcher |
| 545 | BOOST_AUTO_TEST_SUITE_END() // Mgmt |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 546 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 547 | } // namespace ndn::tests |