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 | 0c52603 | 2024-01-31 21:14:01 -0500 | [diff] [blame] | 3 | * Copyright (c) 2013-2024 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" |
| 23 | #include "ndn-cxx/mgmt/nfd/control-parameters.hpp" |
| 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 | { |
| 88 | BOOST_CHECK_NO_THROW(dispatcher |
| 89 | .addControlCommand<VoidParameters>("test/1", makeAcceptAllAuthorization(), |
Davide Pesavento | 152ef44 | 2023-04-22 02:02:29 -0400 | [diff] [blame] | 90 | std::bind([] { return true; }), |
| 91 | std::bind([]{}))); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 92 | BOOST_CHECK_NO_THROW(dispatcher |
| 93 | .addControlCommand<VoidParameters>("test/2", makeAcceptAllAuthorization(), |
Davide Pesavento | 152ef44 | 2023-04-22 02:02:29 -0400 | [diff] [blame] | 94 | std::bind([] { return true; }), |
| 95 | std::bind([]{}))); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 96 | |
| 97 | BOOST_CHECK_THROW(dispatcher |
| 98 | .addControlCommand<VoidParameters>("test", makeAcceptAllAuthorization(), |
Davide Pesavento | 152ef44 | 2023-04-22 02:02:29 -0400 | [diff] [blame] | 99 | std::bind([] { return true; }), |
| 100 | std::bind([]{})), |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 101 | std::out_of_range); |
| 102 | |
| 103 | BOOST_CHECK_NO_THROW(dispatcher.addStatusDataset("status/1", |
Davide Pesavento | 152ef44 | 2023-04-22 02:02:29 -0400 | [diff] [blame] | 104 | makeAcceptAllAuthorization(), std::bind([]{}))); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 105 | BOOST_CHECK_NO_THROW(dispatcher.addStatusDataset("status/2", |
Davide Pesavento | 152ef44 | 2023-04-22 02:02:29 -0400 | [diff] [blame] | 106 | makeAcceptAllAuthorization(), std::bind([]{}))); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 107 | BOOST_CHECK_THROW(dispatcher.addStatusDataset("status", |
Davide Pesavento | 152ef44 | 2023-04-22 02:02:29 -0400 | [diff] [blame] | 108 | makeAcceptAllAuthorization(), std::bind([]{})), |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 109 | std::out_of_range); |
| 110 | |
| 111 | BOOST_CHECK_NO_THROW(dispatcher.addNotificationStream("stream/1")); |
| 112 | BOOST_CHECK_NO_THROW(dispatcher.addNotificationStream("stream/2")); |
| 113 | BOOST_CHECK_THROW(dispatcher.addNotificationStream("stream"), std::out_of_range); |
| 114 | |
| 115 | |
| 116 | BOOST_CHECK_NO_THROW(dispatcher.addTopPrefix("/root/1")); |
| 117 | BOOST_CHECK_NO_THROW(dispatcher.addTopPrefix("/root/2")); |
| 118 | BOOST_CHECK_THROW(dispatcher.addTopPrefix("/root"), std::out_of_range); |
| 119 | |
| 120 | BOOST_CHECK_THROW(dispatcher |
| 121 | .addControlCommand<VoidParameters>("test/3", makeAcceptAllAuthorization(), |
Davide Pesavento | 152ef44 | 2023-04-22 02:02:29 -0400 | [diff] [blame] | 122 | std::bind([] { return true; }), |
| 123 | std::bind([]{})), |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 124 | std::domain_error); |
| 125 | |
| 126 | BOOST_CHECK_THROW(dispatcher.addStatusDataset("status/3", |
Davide Pesavento | 152ef44 | 2023-04-22 02:02:29 -0400 | [diff] [blame] | 127 | makeAcceptAllAuthorization(), std::bind([]{})), |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 128 | std::domain_error); |
| 129 | |
| 130 | BOOST_CHECK_THROW(dispatcher.addNotificationStream("stream/3"), std::domain_error); |
| 131 | } |
| 132 | |
Junxiao Shi | d97c953 | 2017-04-27 16:17:04 +0000 | [diff] [blame] | 133 | BOOST_AUTO_TEST_CASE(AddRemoveTopPrefix) |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 134 | { |
| 135 | std::map<std::string, size_t> nCallbackCalled; |
| 136 | dispatcher |
| 137 | .addControlCommand<VoidParameters>("test/1", makeAcceptAllAuthorization(), |
Davide Pesavento | 152ef44 | 2023-04-22 02:02:29 -0400 | [diff] [blame] | 138 | std::bind([] { return true; }), |
| 139 | std::bind([&nCallbackCalled] { ++nCallbackCalled["test/1"]; })); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 140 | |
| 141 | dispatcher |
| 142 | .addControlCommand<VoidParameters>("test/2", makeAcceptAllAuthorization(), |
Davide Pesavento | 152ef44 | 2023-04-22 02:02:29 -0400 | [diff] [blame] | 143 | std::bind([] { return true; }), |
| 144 | std::bind([&nCallbackCalled] { ++nCallbackCalled["test/2"]; })); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 145 | |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 146 | face.receive(*makeInterest("/root/1/test/1/%80%00")); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 147 | advanceClocks(1_ms); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 148 | BOOST_CHECK_EQUAL(nCallbackCalled["test/1"], 0); |
| 149 | BOOST_CHECK_EQUAL(nCallbackCalled["test/2"], 0); |
| 150 | |
| 151 | dispatcher.addTopPrefix("/root/1"); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 152 | advanceClocks(1_ms); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 153 | |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 154 | face.receive(*makeInterest("/root/1/test/1/%80%00")); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 155 | advanceClocks(1_ms); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 156 | BOOST_CHECK_EQUAL(nCallbackCalled["test/1"], 1); |
| 157 | BOOST_CHECK_EQUAL(nCallbackCalled["test/2"], 0); |
| 158 | |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 159 | face.receive(*makeInterest("/root/1/test/2/%80%00")); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 160 | advanceClocks(1_ms); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 161 | BOOST_CHECK_EQUAL(nCallbackCalled["test/1"], 1); |
| 162 | BOOST_CHECK_EQUAL(nCallbackCalled["test/2"], 1); |
| 163 | |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 164 | face.receive(*makeInterest("/root/2/test/1/%80%00")); |
| 165 | face.receive(*makeInterest("/root/2/test/2/%80%00")); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 166 | advanceClocks(1_ms); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 167 | BOOST_CHECK_EQUAL(nCallbackCalled["test/1"], 1); |
| 168 | BOOST_CHECK_EQUAL(nCallbackCalled["test/2"], 1); |
| 169 | |
| 170 | dispatcher.addTopPrefix("/root/2"); |
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 | |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 173 | face.receive(*makeInterest("/root/1/test/1/%80%00")); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 174 | advanceClocks(1_ms); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 175 | BOOST_CHECK_EQUAL(nCallbackCalled["test/1"], 2); |
| 176 | |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 177 | face.receive(*makeInterest("/root/2/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 | |
| 181 | dispatcher.removeTopPrefix("/root/1"); |
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 | |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 184 | face.receive(*makeInterest("/root/1/test/1/%80%00")); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 185 | advanceClocks(1_ms); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 186 | BOOST_CHECK_EQUAL(nCallbackCalled["test/1"], 3); |
| 187 | |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 188 | face.receive(*makeInterest("/root/2/test/1/%80%00")); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 189 | advanceClocks(1_ms); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 190 | BOOST_CHECK_EQUAL(nCallbackCalled["test/1"], 4); |
| 191 | } |
| 192 | |
Junxiao Shi | d97c953 | 2017-04-27 16:17:04 +0000 | [diff] [blame] | 193 | BOOST_AUTO_TEST_CASE(ControlCommand) |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 194 | { |
| 195 | size_t nCallbackCalled = 0; |
| 196 | dispatcher |
| 197 | .addControlCommand<VoidParameters>("test", |
| 198 | makeTestAuthorization(), |
Davide Pesavento | 152ef44 | 2023-04-22 02:02:29 -0400 | [diff] [blame] | 199 | std::bind([] { return true; }), |
| 200 | std::bind([&nCallbackCalled] { ++nCallbackCalled; })); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 201 | |
| 202 | dispatcher.addTopPrefix("/root"); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 203 | advanceClocks(1_ms); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 204 | face.sentData.clear(); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 205 | |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 206 | face.receive(*makeInterest("/root/test/%80%00")); // returns 403 |
| 207 | face.receive(*makeInterest("/root/test/%80%00/invalid")); // returns 403 |
| 208 | face.receive(*makeInterest("/root/test/%80%00/silent")); // silently ignored |
| 209 | face.receive(*makeInterest("/root/test/.../invalid")); // silently ignored (wrong format) |
| 210 | face.receive(*makeInterest("/root/test/.../valid")); // silently ignored (wrong format) |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 211 | advanceClocks(1_ms, 20); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 212 | BOOST_CHECK_EQUAL(nCallbackCalled, 0); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 213 | BOOST_CHECK_EQUAL(face.sentData.size(), 2); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 214 | |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 215 | BOOST_CHECK_EQUAL(face.sentData[0].getContentType(), tlv::ContentType_Blob); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 216 | BOOST_CHECK_EQUAL(ControlResponse(face.sentData[0].getContent().blockFromValue()).getCode(), 403); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 217 | BOOST_CHECK_EQUAL(face.sentData[1].getContentType(), tlv::ContentType_Blob); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 218 | BOOST_CHECK_EQUAL(ControlResponse(face.sentData[1].getContent().blockFromValue()).getCode(), 403); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 219 | |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 220 | face.receive(*makeInterest("/root/test/%80%00/valid")); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 221 | advanceClocks(1_ms, 10); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 222 | BOOST_CHECK_EQUAL(nCallbackCalled, 1); |
| 223 | } |
| 224 | |
Junxiao Shi | d97c953 | 2017-04-27 16:17:04 +0000 | [diff] [blame] | 225 | class StatefulParameters : public mgmt::ControlParameters |
| 226 | { |
| 227 | public: |
| 228 | explicit |
| 229 | StatefulParameters(const Block& wire) |
| 230 | { |
| 231 | wireDecode(wire); |
| 232 | } |
| 233 | |
| 234 | Block |
| 235 | wireEncode() const final |
| 236 | { |
Davide Pesavento | fbea4fc | 2022-02-08 07:26:04 -0500 | [diff] [blame] | 237 | return {}; |
Junxiao Shi | d97c953 | 2017-04-27 16:17:04 +0000 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | void |
Davide Pesavento | 3c34ec1 | 2021-03-28 21:50:06 -0400 | [diff] [blame] | 241 | wireDecode(const Block&) final |
Junxiao Shi | d97c953 | 2017-04-27 16:17:04 +0000 | [diff] [blame] | 242 | { |
| 243 | m_state = EXPECTED_STATE; |
| 244 | } |
| 245 | |
| 246 | bool |
| 247 | check() const |
| 248 | { |
| 249 | return m_state == EXPECTED_STATE; |
| 250 | } |
| 251 | |
| 252 | private: |
| 253 | static constexpr int EXPECTED_STATE = 12602; |
| 254 | int m_state = 0; |
| 255 | }; |
| 256 | |
Davide Pesavento | 0c52603 | 2024-01-31 21:14:01 -0500 | [diff] [blame] | 257 | BOOST_AUTO_TEST_CASE(ControlCommandAsyncAuthorization, |
| 258 | * ut::description("test for bug #4059")) |
Junxiao Shi | d97c953 | 2017-04-27 16:17:04 +0000 | [diff] [blame] | 259 | { |
| 260 | AcceptContinuation authorizationAccept; |
Davide Pesavento | 54dfc4a | 2024-12-26 17:30:41 -0500 | [diff] [blame^] | 261 | auto authorization = [&authorizationAccept] (const Name&, const Interest&, const ControlParameters*, |
| 262 | AcceptContinuation accept, RejectContinuation) { |
| 263 | authorizationAccept = std::move(accept); |
| 264 | }; |
Junxiao Shi | d97c953 | 2017-04-27 16:17:04 +0000 | [diff] [blame] | 265 | |
Davide Pesavento | 54dfc4a | 2024-12-26 17:30:41 -0500 | [diff] [blame^] | 266 | auto validateParameters = [] (const ControlParameters& params) { |
| 267 | return dynamic_cast<const StatefulParameters&>(params).check(); |
| 268 | }; |
Junxiao Shi | d97c953 | 2017-04-27 16:17:04 +0000 | [diff] [blame] | 269 | |
| 270 | size_t nCallbackCalled = 0; |
Davide Pesavento | 3c34ec1 | 2021-03-28 21:50:06 -0400 | [diff] [blame] | 271 | dispatcher.addControlCommand<StatefulParameters>("test", authorization, validateParameters, |
Davide Pesavento | 152ef44 | 2023-04-22 02:02:29 -0400 | [diff] [blame] | 272 | std::bind([&nCallbackCalled] { ++nCallbackCalled; })); |
Junxiao Shi | d97c953 | 2017-04-27 16:17:04 +0000 | [diff] [blame] | 273 | |
| 274 | dispatcher.addTopPrefix("/root"); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 275 | advanceClocks(1_ms); |
Junxiao Shi | d97c953 | 2017-04-27 16:17:04 +0000 | [diff] [blame] | 276 | |
| 277 | face.receive(*makeInterest("/root/test/%80%00")); |
| 278 | BOOST_CHECK_EQUAL(nCallbackCalled, 0); |
| 279 | BOOST_REQUIRE(authorizationAccept != nullptr); |
| 280 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 281 | advanceClocks(1_ms); |
Junxiao Shi | d97c953 | 2017-04-27 16:17:04 +0000 | [diff] [blame] | 282 | authorizationAccept(""); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 283 | advanceClocks(1_ms); |
Junxiao Shi | d97c953 | 2017-04-27 16:17:04 +0000 | [diff] [blame] | 284 | BOOST_CHECK_EQUAL(nCallbackCalled, 1); |
| 285 | } |
| 286 | |
| 287 | BOOST_AUTO_TEST_CASE(StatusDataset) |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 288 | { |
Davide Pesavento | fbea4fc | 2022-02-08 07:26:04 -0500 | [diff] [blame] | 289 | const Block smallBlock({0x81, 0x01, 0x01}); |
Davide Pesavento | 3c34ec1 | 2021-03-28 21:50:06 -0400 | [diff] [blame] | 290 | const Block largeBlock = [] { |
| 291 | Block b(129, std::make_shared<const Buffer>(3000)); |
| 292 | b.encode(); |
| 293 | return b; |
| 294 | }(); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 295 | |
| 296 | dispatcher.addStatusDataset("test/small", |
| 297 | makeTestAuthorization(), |
Davide Pesavento | 3c34ec1 | 2021-03-28 21:50:06 -0400 | [diff] [blame] | 298 | [&smallBlock] (const Name&, const Interest&, |
Davide Pesavento | b10024c | 2017-09-22 01:36:44 -0400 | [diff] [blame] | 299 | StatusDatasetContext& context) { |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 300 | context.append(smallBlock); |
| 301 | context.append(smallBlock); |
| 302 | context.append(smallBlock); |
| 303 | context.end(); |
| 304 | }); |
| 305 | |
| 306 | dispatcher.addStatusDataset("test/large", |
| 307 | makeTestAuthorization(), |
Davide Pesavento | 3c34ec1 | 2021-03-28 21:50:06 -0400 | [diff] [blame] | 308 | [&largeBlock] (const Name&, const Interest&, |
Davide Pesavento | b10024c | 2017-09-22 01:36:44 -0400 | [diff] [blame] | 309 | StatusDatasetContext& context) { |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 310 | context.append(largeBlock); |
| 311 | context.append(largeBlock); |
| 312 | context.append(largeBlock); |
| 313 | context.end(); |
| 314 | }); |
| 315 | |
| 316 | dispatcher.addStatusDataset("test/reject", |
| 317 | makeTestAuthorization(), |
Davide Pesavento | 3c34ec1 | 2021-03-28 21:50:06 -0400 | [diff] [blame] | 318 | [] (const Name&, const Interest&, StatusDatasetContext& context) { |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 319 | context.reject(); |
| 320 | }); |
| 321 | |
| 322 | dispatcher.addTopPrefix("/root"); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 323 | advanceClocks(1_ms); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 324 | face.sentData.clear(); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 325 | |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 326 | face.receive(*makeInterest("/root/test/small/%80%00")); // returns 403 |
| 327 | face.receive(*makeInterest("/root/test/small/%80%00/invalid")); // returns 403 |
| 328 | face.receive(*makeInterest("/root/test/small/%80%00/silent")); // silently ignored |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 329 | advanceClocks(1_ms, 20); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 330 | |
Davide Pesavento | 3c34ec1 | 2021-03-28 21:50:06 -0400 | [diff] [blame] | 331 | BOOST_REQUIRE_EQUAL(face.sentData.size(), 2); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 332 | BOOST_CHECK_EQUAL(face.sentData[0].getContentType(), tlv::ContentType_Blob); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 333 | BOOST_CHECK_EQUAL(ControlResponse(face.sentData[0].getContent().blockFromValue()).getCode(), 403); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 334 | BOOST_CHECK_EQUAL(face.sentData[1].getContentType(), tlv::ContentType_Blob); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 335 | BOOST_CHECK_EQUAL(ControlResponse(face.sentData[1].getContent().blockFromValue()).getCode(), 403); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 336 | |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 337 | face.sentData.clear(); |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 338 | |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 339 | auto interestSmall = *makeInterest("/root/test/small/valid", true); |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 340 | face.receive(interestSmall); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 341 | advanceClocks(1_ms, 10); |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 342 | |
| 343 | // one data packet is generated and sent to both places |
Davide Pesavento | 3c34ec1 | 2021-03-28 21:50:06 -0400 | [diff] [blame] | 344 | BOOST_REQUIRE_EQUAL(face.sentData.size(), 1); |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 345 | BOOST_CHECK_EQUAL(storage.size(), 1); |
| 346 | |
| 347 | auto fetchedData = storage.find(interestSmall); |
| 348 | BOOST_REQUIRE(fetchedData != nullptr); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 349 | BOOST_CHECK_EQUAL(face.sentData[0].wireEncode(), fetchedData->wireEncode()); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 350 | |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 351 | face.receive(*makeInterest(Name("/root/test/small/valid").appendVersion(10))); // should be ignored |
| 352 | 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] | 353 | advanceClocks(1_ms, 10); |
Davide Pesavento | 3c34ec1 | 2021-03-28 21:50:06 -0400 | [diff] [blame] | 354 | BOOST_REQUIRE_EQUAL(face.sentData.size(), 1); |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 355 | BOOST_CHECK_EQUAL(storage.size(), 1); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 356 | |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 357 | Block content = face.sentData[0].getContent(); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 358 | BOOST_CHECK_NO_THROW(content.parse()); |
| 359 | |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 360 | BOOST_REQUIRE_EQUAL(content.elements().size(), 3); |
| 361 | BOOST_CHECK_EQUAL(content.elements()[0], smallBlock); |
| 362 | BOOST_CHECK_EQUAL(content.elements()[1], smallBlock); |
| 363 | BOOST_CHECK_EQUAL(content.elements()[2], smallBlock); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 364 | |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 365 | storage.erase("/", true); // clear the storage |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 366 | face.sentData.clear(); |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 367 | face.receive(*makeInterest("/root/test/large/valid")); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 368 | advanceClocks(1_ms, 10); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 369 | |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 370 | // two data packets are generated, the first one will be sent to both places |
| 371 | // 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] | 372 | BOOST_REQUIRE_EQUAL(face.sentData.size(), 1); |
| 373 | BOOST_REQUIRE_EQUAL(storage.size(), 2); |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 374 | |
| 375 | // segment0 should be sent through the face |
| 376 | const auto& component = face.sentData[0].getName().at(-1); |
| 377 | BOOST_CHECK(component.isSegment()); |
| 378 | BOOST_CHECK_EQUAL(component.toSegment(), 0); |
| 379 | |
| 380 | std::vector<Data> dataInStorage; |
| 381 | std::copy(storage.begin(), storage.end(), std::back_inserter(dataInStorage)); |
| 382 | |
| 383 | // the Data sent through the face should be the same as the first Data in the storage |
| 384 | BOOST_CHECK_EQUAL(face.sentData[0].getName(), dataInStorage[0].getName()); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 385 | BOOST_CHECK_EQUAL(face.sentData[0].getContent(), dataInStorage[0].getContent()); |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 386 | |
| 387 | content = [&dataInStorage] () -> Block { |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 388 | EncodingBuffer encoder; |
Davide Pesavento | 258d51a | 2022-02-27 21:26:28 -0500 | [diff] [blame] | 389 | size_t valueLength = encoder.prependBytes(dataInStorage[1].getContent().value_bytes()); |
| 390 | valueLength += encoder.prependBytes(dataInStorage[0].getContent().value_bytes()); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 391 | encoder.prependVarNumber(valueLength); |
| 392 | encoder.prependVarNumber(tlv::Content); |
| 393 | return encoder.block(); |
| 394 | }(); |
| 395 | |
| 396 | BOOST_CHECK_NO_THROW(content.parse()); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 397 | BOOST_REQUIRE_EQUAL(content.elements().size(), 3); |
| 398 | BOOST_CHECK_EQUAL(content.elements()[0], largeBlock); |
| 399 | BOOST_CHECK_EQUAL(content.elements()[1], largeBlock); |
| 400 | BOOST_CHECK_EQUAL(content.elements()[2], largeBlock); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 401 | |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 402 | storage.erase("/", true);// clear the storage |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 403 | face.sentData.clear(); |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 404 | face.receive(*makeInterest("/root/test/reject/%80%00/valid")); // returns nack |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 405 | advanceClocks(1_ms); |
Davide Pesavento | 3c34ec1 | 2021-03-28 21:50:06 -0400 | [diff] [blame] | 406 | |
| 407 | BOOST_REQUIRE_EQUAL(face.sentData.size(), 1); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 408 | BOOST_CHECK_EQUAL(face.sentData[0].getContentType(), tlv::ContentType_Nack); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 409 | BOOST_CHECK_EQUAL(ControlResponse(face.sentData[0].getContent().blockFromValue()).getCode(), 400); |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 410 | 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] | 411 | } |
| 412 | |
Junxiao Shi | d97c953 | 2017-04-27 16:17:04 +0000 | [diff] [blame] | 413 | BOOST_AUTO_TEST_CASE(NotificationStream) |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 414 | { |
Davide Pesavento | fbea4fc | 2022-02-08 07:26:04 -0500 | [diff] [blame] | 415 | const Block block({0x82, 0x01, 0x02}); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 416 | auto post = dispatcher.addNotificationStream("test"); |
| 417 | |
| 418 | post(block); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 419 | advanceClocks(1_ms); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 420 | BOOST_CHECK_EQUAL(face.sentData.size(), 0); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 421 | |
| 422 | dispatcher.addTopPrefix("/root"); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 423 | advanceClocks(1_ms); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 424 | face.sentData.clear(); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 425 | |
| 426 | post(block); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 427 | advanceClocks(1_ms); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 428 | BOOST_CHECK_EQUAL(face.sentData.size(), 1); |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 429 | BOOST_CHECK_EQUAL(storage.size(), 1); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 430 | |
| 431 | post(block); |
| 432 | post(block); |
| 433 | post(block); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 434 | advanceClocks(1_ms, 10); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 435 | |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 436 | BOOST_REQUIRE_EQUAL(face.sentData.size(), 4); |
Eric Newberry | c25e463 | 2021-02-11 10:48:11 -0800 | [diff] [blame] | 437 | BOOST_CHECK_EQUAL(face.sentData[0].getName(), "/root/test/seq=0"); |
| 438 | BOOST_CHECK_EQUAL(face.sentData[1].getName(), "/root/test/seq=1"); |
| 439 | BOOST_CHECK_EQUAL(face.sentData[2].getName(), "/root/test/seq=2"); |
| 440 | BOOST_CHECK_EQUAL(face.sentData[3].getName(), "/root/test/seq=3"); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 441 | |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 442 | BOOST_CHECK_EQUAL(face.sentData[0].getContent().blockFromValue(), block); |
| 443 | BOOST_CHECK_EQUAL(face.sentData[1].getContent().blockFromValue(), block); |
| 444 | BOOST_CHECK_EQUAL(face.sentData[2].getContent().blockFromValue(), block); |
| 445 | BOOST_CHECK_EQUAL(face.sentData[3].getContent().blockFromValue(), block); |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 446 | |
| 447 | // each version of notification will be sent to both places |
| 448 | std::vector<Data> dataInStorage; |
| 449 | std::copy(storage.begin(), storage.end(), std::back_inserter(dataInStorage)); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 450 | BOOST_REQUIRE_EQUAL(dataInStorage.size(), 4); |
Eric Newberry | c25e463 | 2021-02-11 10:48:11 -0800 | [diff] [blame] | 451 | BOOST_CHECK_EQUAL(dataInStorage[0].getName(), "/root/test/seq=0"); |
| 452 | BOOST_CHECK_EQUAL(dataInStorage[1].getName(), "/root/test/seq=1"); |
| 453 | BOOST_CHECK_EQUAL(dataInStorage[2].getName(), "/root/test/seq=2"); |
| 454 | BOOST_CHECK_EQUAL(dataInStorage[3].getName(), "/root/test/seq=3"); |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 455 | |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 456 | BOOST_CHECK_EQUAL(dataInStorage[0].getContent().blockFromValue(), block); |
| 457 | BOOST_CHECK_EQUAL(dataInStorage[1].getContent().blockFromValue(), block); |
| 458 | BOOST_CHECK_EQUAL(dataInStorage[2].getContent().blockFromValue(), block); |
| 459 | BOOST_CHECK_EQUAL(dataInStorage[3].getContent().blockFromValue(), block); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 460 | } |
| 461 | |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 462 | BOOST_AUTO_TEST_SUITE_END() // TestDispatcher |
| 463 | BOOST_AUTO_TEST_SUITE_END() // Mgmt |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 464 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 465 | } // namespace ndn::tests |