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 | |
Davide Pesavento | 0e13ed3 | 2024-12-28 15:01:25 -0500 | [diff] [blame] | 41 | class VoidParameters : public ControlParametersBase |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 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 | 0e13ed3 | 2024-12-28 15:01:25 -0500 | [diff] [blame] | 67 | return [] (const Name&, const Interest& interest, const ControlParametersBase*, |
Davide Pesavento | 3c34ec1 | 2021-03-28 21:50:06 -0400 | [diff] [blame] | 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); |
jaczhi | 82d8ed0 | 2025-01-07 15:51:02 -0800 | [diff] [blame^] | 235 | dispatcher.addControlCommand<nfd::RibAnnounceCommand>(makeAcceptAllAuthorization(), handler); |
Davide Pesavento | cb385e3 | 2024-12-27 15:22:02 -0500 | [diff] [blame] | 236 | |
| 237 | BOOST_CHECK_THROW(dispatcher.addControlCommand<nfd::CsConfigCommand>(makeAcceptAllAuthorization(), |
| 238 | [] (auto&&...) {}), |
| 239 | std::out_of_range); |
| 240 | |
| 241 | dispatcher.addTopPrefix("/root"); |
| 242 | advanceClocks(1_ms); |
| 243 | BOOST_REQUIRE_EQUAL(face.sentData.size(), 0); |
| 244 | |
| 245 | BOOST_CHECK_THROW(dispatcher.addControlCommand<nfd::CsConfigCommand>(makeAcceptAllAuthorization(), |
| 246 | [] (auto&&...) {}), |
| 247 | std::domain_error); |
| 248 | |
| 249 | // we pick FaceDestroyCommand as an example for the following tests |
| 250 | |
| 251 | // malformed request (missing ControlParameters) => silently ignored |
| 252 | auto baseName = Name("/root").append(nfd::FaceDestroyCommand::getName()); |
| 253 | auto interest = makeInterest(baseName); |
| 254 | face.receive(*interest); |
| 255 | advanceClocks(1_ms); |
| 256 | BOOST_CHECK_EQUAL(nHandlerCalled, 0); |
| 257 | BOOST_CHECK_EQUAL(face.sentData.size(), 0); |
| 258 | |
| 259 | // ControlParameters present but invalid (missing required field) => reply with error 400 |
| 260 | nfd::ControlParameters params; |
| 261 | interest->setName(Name(baseName).append(params.wireEncode())); |
| 262 | face.receive(*interest); |
| 263 | advanceClocks(1_ms); |
| 264 | BOOST_CHECK_EQUAL(nHandlerCalled, 0); |
| 265 | BOOST_REQUIRE_EQUAL(face.sentData.size(), 1); |
| 266 | BOOST_CHECK_EQUAL(face.sentData[0].getContentType(), tlv::ContentType_Blob); |
| 267 | BOOST_CHECK_EQUAL(ControlResponse(face.sentData[0].getContent().blockFromValue()).getCode(), 400); |
| 268 | |
| 269 | // valid request |
| 270 | params.setFaceId(42); |
| 271 | interest->setName(Name(baseName).append(params.wireEncode())); |
| 272 | face.receive(*interest); |
| 273 | advanceClocks(1_ms); |
| 274 | BOOST_CHECK_EQUAL(nHandlerCalled, 1); |
| 275 | BOOST_CHECK_EQUAL(face.sentData.size(), 1); |
| 276 | } |
| 277 | |
| 278 | BOOST_AUTO_TEST_CASE(ControlCommandResponse) |
| 279 | { |
| 280 | auto handler = [] (const Name& prefix, const Interest& interest, |
Davide Pesavento | 0e13ed3 | 2024-12-28 15:01:25 -0500 | [diff] [blame] | 281 | const ControlParametersBase&, const CommandContinuation& done) { |
Davide Pesavento | cb385e3 | 2024-12-27 15:22:02 -0500 | [diff] [blame] | 282 | BOOST_CHECK_EQUAL(prefix, "/root"); |
| 283 | BOOST_CHECK_EQUAL(interest.getName().getPrefix(3), |
| 284 | Name("/root").append(nfd::CsConfigCommand::getName())); |
| 285 | done(ControlResponse(42, "the answer")); |
| 286 | }; |
| 287 | |
| 288 | // use CsConfigCommand as an example |
| 289 | dispatcher.addControlCommand<nfd::CsConfigCommand>(makeAcceptAllAuthorization(), handler); |
| 290 | dispatcher.addTopPrefix("/root"); |
| 291 | advanceClocks(1_ms); |
| 292 | BOOST_REQUIRE_EQUAL(face.sentData.size(), 0); |
| 293 | |
| 294 | nfd::ControlParameters params; |
| 295 | auto interest = makeInterest(Name("/root") |
| 296 | .append(nfd::CsConfigCommand::getName()) |
| 297 | .append(params.wireEncode())); |
| 298 | face.receive(*interest); |
| 299 | advanceClocks(1_ms, 10); |
| 300 | |
| 301 | BOOST_REQUIRE_EQUAL(face.sentData.size(), 1); |
| 302 | BOOST_CHECK_EQUAL(face.sentData[0].getContentType(), tlv::ContentType_Blob); |
| 303 | ControlResponse resp(face.sentData[0].getContent().blockFromValue()); |
| 304 | BOOST_CHECK_EQUAL(resp.getCode(), 42); |
| 305 | BOOST_CHECK_EQUAL(resp.getText(), "the answer"); |
| 306 | } |
| 307 | |
Davide Pesavento | 0e13ed3 | 2024-12-28 15:01:25 -0500 | [diff] [blame] | 308 | class StatefulParameters : public ControlParametersBase |
Junxiao Shi | d97c953 | 2017-04-27 16:17:04 +0000 | [diff] [blame] | 309 | { |
| 310 | public: |
| 311 | explicit |
| 312 | StatefulParameters(const Block& wire) |
| 313 | { |
| 314 | wireDecode(wire); |
| 315 | } |
| 316 | |
| 317 | Block |
| 318 | wireEncode() const final |
| 319 | { |
Davide Pesavento | fbea4fc | 2022-02-08 07:26:04 -0500 | [diff] [blame] | 320 | return {}; |
Junxiao Shi | d97c953 | 2017-04-27 16:17:04 +0000 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | void |
Davide Pesavento | 3c34ec1 | 2021-03-28 21:50:06 -0400 | [diff] [blame] | 324 | wireDecode(const Block&) final |
Junxiao Shi | d97c953 | 2017-04-27 16:17:04 +0000 | [diff] [blame] | 325 | { |
| 326 | m_state = EXPECTED_STATE; |
| 327 | } |
| 328 | |
| 329 | bool |
| 330 | check() const |
| 331 | { |
| 332 | return m_state == EXPECTED_STATE; |
| 333 | } |
| 334 | |
| 335 | private: |
| 336 | static constexpr int EXPECTED_STATE = 12602; |
| 337 | int m_state = 0; |
| 338 | }; |
| 339 | |
Davide Pesavento | 0c52603 | 2024-01-31 21:14:01 -0500 | [diff] [blame] | 340 | BOOST_AUTO_TEST_CASE(ControlCommandAsyncAuthorization, |
| 341 | * ut::description("test for bug #4059")) |
Junxiao Shi | d97c953 | 2017-04-27 16:17:04 +0000 | [diff] [blame] | 342 | { |
| 343 | AcceptContinuation authorizationAccept; |
Davide Pesavento | 0e13ed3 | 2024-12-28 15:01:25 -0500 | [diff] [blame] | 344 | auto authorization = [&authorizationAccept] (const Name&, const Interest&, const ControlParametersBase*, |
Davide Pesavento | 54dfc4a | 2024-12-26 17:30:41 -0500 | [diff] [blame] | 345 | AcceptContinuation accept, RejectContinuation) { |
| 346 | authorizationAccept = std::move(accept); |
| 347 | }; |
Junxiao Shi | d97c953 | 2017-04-27 16:17:04 +0000 | [diff] [blame] | 348 | |
Davide Pesavento | 0e13ed3 | 2024-12-28 15:01:25 -0500 | [diff] [blame] | 349 | auto validateParameters = [] (const ControlParametersBase& params) { |
Davide Pesavento | 54dfc4a | 2024-12-26 17:30:41 -0500 | [diff] [blame] | 350 | return dynamic_cast<const StatefulParameters&>(params).check(); |
| 351 | }; |
Junxiao Shi | d97c953 | 2017-04-27 16:17:04 +0000 | [diff] [blame] | 352 | |
| 353 | size_t nCallbackCalled = 0; |
Davide Pesavento | 3c34ec1 | 2021-03-28 21:50:06 -0400 | [diff] [blame] | 354 | dispatcher.addControlCommand<StatefulParameters>("test", authorization, validateParameters, |
Davide Pesavento | cb385e3 | 2024-12-27 15:22:02 -0500 | [diff] [blame] | 355 | [&nCallbackCalled] (auto&&...) { ++nCallbackCalled; }); |
Junxiao Shi | d97c953 | 2017-04-27 16:17:04 +0000 | [diff] [blame] | 356 | |
| 357 | dispatcher.addTopPrefix("/root"); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 358 | advanceClocks(1_ms); |
Junxiao Shi | d97c953 | 2017-04-27 16:17:04 +0000 | [diff] [blame] | 359 | |
| 360 | face.receive(*makeInterest("/root/test/%80%00")); |
| 361 | BOOST_CHECK_EQUAL(nCallbackCalled, 0); |
| 362 | BOOST_REQUIRE(authorizationAccept != nullptr); |
| 363 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 364 | advanceClocks(1_ms); |
Junxiao Shi | d97c953 | 2017-04-27 16:17:04 +0000 | [diff] [blame] | 365 | authorizationAccept(""); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 366 | advanceClocks(1_ms); |
Junxiao Shi | d97c953 | 2017-04-27 16:17:04 +0000 | [diff] [blame] | 367 | BOOST_CHECK_EQUAL(nCallbackCalled, 1); |
| 368 | } |
| 369 | |
| 370 | BOOST_AUTO_TEST_CASE(StatusDataset) |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 371 | { |
Davide Pesavento | fbea4fc | 2022-02-08 07:26:04 -0500 | [diff] [blame] | 372 | const Block smallBlock({0x81, 0x01, 0x01}); |
Davide Pesavento | 3c34ec1 | 2021-03-28 21:50:06 -0400 | [diff] [blame] | 373 | const Block largeBlock = [] { |
| 374 | Block b(129, std::make_shared<const Buffer>(3000)); |
| 375 | b.encode(); |
| 376 | return b; |
| 377 | }(); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 378 | |
| 379 | dispatcher.addStatusDataset("test/small", |
| 380 | makeTestAuthorization(), |
Davide Pesavento | 3c34ec1 | 2021-03-28 21:50:06 -0400 | [diff] [blame] | 381 | [&smallBlock] (const Name&, const Interest&, |
Davide Pesavento | b10024c | 2017-09-22 01:36:44 -0400 | [diff] [blame] | 382 | StatusDatasetContext& context) { |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 383 | context.append(smallBlock); |
| 384 | context.append(smallBlock); |
| 385 | context.append(smallBlock); |
| 386 | context.end(); |
| 387 | }); |
| 388 | |
| 389 | dispatcher.addStatusDataset("test/large", |
| 390 | makeTestAuthorization(), |
Davide Pesavento | 3c34ec1 | 2021-03-28 21:50:06 -0400 | [diff] [blame] | 391 | [&largeBlock] (const Name&, const Interest&, |
Davide Pesavento | b10024c | 2017-09-22 01:36:44 -0400 | [diff] [blame] | 392 | StatusDatasetContext& context) { |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 393 | context.append(largeBlock); |
| 394 | context.append(largeBlock); |
| 395 | context.append(largeBlock); |
| 396 | context.end(); |
| 397 | }); |
| 398 | |
| 399 | dispatcher.addStatusDataset("test/reject", |
| 400 | makeTestAuthorization(), |
Davide Pesavento | 3c34ec1 | 2021-03-28 21:50:06 -0400 | [diff] [blame] | 401 | [] (const Name&, const Interest&, StatusDatasetContext& context) { |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 402 | context.reject(); |
| 403 | }); |
| 404 | |
| 405 | dispatcher.addTopPrefix("/root"); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 406 | advanceClocks(1_ms); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 407 | face.sentData.clear(); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 408 | |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 409 | face.receive(*makeInterest("/root/test/small/%80%00")); // returns 403 |
| 410 | face.receive(*makeInterest("/root/test/small/%80%00/invalid")); // returns 403 |
| 411 | face.receive(*makeInterest("/root/test/small/%80%00/silent")); // silently ignored |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 412 | advanceClocks(1_ms, 20); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 413 | |
Davide Pesavento | 3c34ec1 | 2021-03-28 21:50:06 -0400 | [diff] [blame] | 414 | BOOST_REQUIRE_EQUAL(face.sentData.size(), 2); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 415 | BOOST_CHECK_EQUAL(face.sentData[0].getContentType(), tlv::ContentType_Blob); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 416 | BOOST_CHECK_EQUAL(ControlResponse(face.sentData[0].getContent().blockFromValue()).getCode(), 403); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 417 | BOOST_CHECK_EQUAL(face.sentData[1].getContentType(), tlv::ContentType_Blob); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 418 | BOOST_CHECK_EQUAL(ControlResponse(face.sentData[1].getContent().blockFromValue()).getCode(), 403); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 419 | |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 420 | face.sentData.clear(); |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 421 | |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 422 | auto interestSmall = *makeInterest("/root/test/small/valid", true); |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 423 | face.receive(interestSmall); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 424 | advanceClocks(1_ms, 10); |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 425 | |
| 426 | // one data packet is generated and sent to both places |
Davide Pesavento | 3c34ec1 | 2021-03-28 21:50:06 -0400 | [diff] [blame] | 427 | BOOST_REQUIRE_EQUAL(face.sentData.size(), 1); |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 428 | BOOST_CHECK_EQUAL(storage.size(), 1); |
| 429 | |
| 430 | auto fetchedData = storage.find(interestSmall); |
| 431 | BOOST_REQUIRE(fetchedData != nullptr); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 432 | BOOST_CHECK_EQUAL(face.sentData[0].wireEncode(), fetchedData->wireEncode()); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 433 | |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 434 | face.receive(*makeInterest(Name("/root/test/small/valid").appendVersion(10))); // should be ignored |
| 435 | 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] | 436 | advanceClocks(1_ms, 10); |
Davide Pesavento | 3c34ec1 | 2021-03-28 21:50:06 -0400 | [diff] [blame] | 437 | BOOST_REQUIRE_EQUAL(face.sentData.size(), 1); |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 438 | BOOST_CHECK_EQUAL(storage.size(), 1); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 439 | |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 440 | Block content = face.sentData[0].getContent(); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 441 | BOOST_CHECK_NO_THROW(content.parse()); |
| 442 | |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 443 | BOOST_REQUIRE_EQUAL(content.elements().size(), 3); |
| 444 | BOOST_CHECK_EQUAL(content.elements()[0], smallBlock); |
| 445 | BOOST_CHECK_EQUAL(content.elements()[1], smallBlock); |
| 446 | BOOST_CHECK_EQUAL(content.elements()[2], smallBlock); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 447 | |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 448 | storage.erase("/", true); // clear the storage |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 449 | face.sentData.clear(); |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 450 | face.receive(*makeInterest("/root/test/large/valid")); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 451 | advanceClocks(1_ms, 10); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 452 | |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 453 | // two data packets are generated, the first one will be sent to both places |
| 454 | // 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] | 455 | BOOST_REQUIRE_EQUAL(face.sentData.size(), 1); |
| 456 | BOOST_REQUIRE_EQUAL(storage.size(), 2); |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 457 | |
| 458 | // segment0 should be sent through the face |
| 459 | const auto& component = face.sentData[0].getName().at(-1); |
| 460 | BOOST_CHECK(component.isSegment()); |
| 461 | BOOST_CHECK_EQUAL(component.toSegment(), 0); |
| 462 | |
| 463 | std::vector<Data> dataInStorage; |
| 464 | std::copy(storage.begin(), storage.end(), std::back_inserter(dataInStorage)); |
| 465 | |
| 466 | // the Data sent through the face should be the same as the first Data in the storage |
| 467 | BOOST_CHECK_EQUAL(face.sentData[0].getName(), dataInStorage[0].getName()); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 468 | BOOST_CHECK_EQUAL(face.sentData[0].getContent(), dataInStorage[0].getContent()); |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 469 | |
| 470 | content = [&dataInStorage] () -> Block { |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 471 | EncodingBuffer encoder; |
Davide Pesavento | 258d51a | 2022-02-27 21:26:28 -0500 | [diff] [blame] | 472 | size_t valueLength = encoder.prependBytes(dataInStorage[1].getContent().value_bytes()); |
| 473 | valueLength += encoder.prependBytes(dataInStorage[0].getContent().value_bytes()); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 474 | encoder.prependVarNumber(valueLength); |
| 475 | encoder.prependVarNumber(tlv::Content); |
| 476 | return encoder.block(); |
| 477 | }(); |
| 478 | |
| 479 | BOOST_CHECK_NO_THROW(content.parse()); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 480 | BOOST_REQUIRE_EQUAL(content.elements().size(), 3); |
| 481 | BOOST_CHECK_EQUAL(content.elements()[0], largeBlock); |
| 482 | BOOST_CHECK_EQUAL(content.elements()[1], largeBlock); |
| 483 | BOOST_CHECK_EQUAL(content.elements()[2], largeBlock); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 484 | |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 485 | storage.erase("/", true);// clear the storage |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 486 | face.sentData.clear(); |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 487 | face.receive(*makeInterest("/root/test/reject/%80%00/valid")); // returns nack |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 488 | advanceClocks(1_ms); |
Davide Pesavento | 3c34ec1 | 2021-03-28 21:50:06 -0400 | [diff] [blame] | 489 | |
| 490 | BOOST_REQUIRE_EQUAL(face.sentData.size(), 1); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 491 | BOOST_CHECK_EQUAL(face.sentData[0].getContentType(), tlv::ContentType_Nack); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 492 | BOOST_CHECK_EQUAL(ControlResponse(face.sentData[0].getContent().blockFromValue()).getCode(), 400); |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 493 | 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] | 494 | } |
| 495 | |
Junxiao Shi | d97c953 | 2017-04-27 16:17:04 +0000 | [diff] [blame] | 496 | BOOST_AUTO_TEST_CASE(NotificationStream) |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 497 | { |
Davide Pesavento | fbea4fc | 2022-02-08 07:26:04 -0500 | [diff] [blame] | 498 | const Block block({0x82, 0x01, 0x02}); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 499 | auto post = dispatcher.addNotificationStream("test"); |
| 500 | |
| 501 | post(block); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 502 | advanceClocks(1_ms); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 503 | BOOST_CHECK_EQUAL(face.sentData.size(), 0); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 504 | |
| 505 | dispatcher.addTopPrefix("/root"); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 506 | advanceClocks(1_ms); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 507 | face.sentData.clear(); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 508 | |
| 509 | post(block); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 510 | advanceClocks(1_ms); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 511 | BOOST_CHECK_EQUAL(face.sentData.size(), 1); |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 512 | BOOST_CHECK_EQUAL(storage.size(), 1); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 513 | |
| 514 | post(block); |
| 515 | post(block); |
| 516 | post(block); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 517 | advanceClocks(1_ms, 10); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 518 | |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 519 | BOOST_REQUIRE_EQUAL(face.sentData.size(), 4); |
Eric Newberry | c25e463 | 2021-02-11 10:48:11 -0800 | [diff] [blame] | 520 | BOOST_CHECK_EQUAL(face.sentData[0].getName(), "/root/test/seq=0"); |
| 521 | BOOST_CHECK_EQUAL(face.sentData[1].getName(), "/root/test/seq=1"); |
| 522 | BOOST_CHECK_EQUAL(face.sentData[2].getName(), "/root/test/seq=2"); |
| 523 | BOOST_CHECK_EQUAL(face.sentData[3].getName(), "/root/test/seq=3"); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 524 | |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 525 | BOOST_CHECK_EQUAL(face.sentData[0].getContent().blockFromValue(), block); |
| 526 | BOOST_CHECK_EQUAL(face.sentData[1].getContent().blockFromValue(), block); |
| 527 | BOOST_CHECK_EQUAL(face.sentData[2].getContent().blockFromValue(), block); |
| 528 | BOOST_CHECK_EQUAL(face.sentData[3].getContent().blockFromValue(), block); |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 529 | |
| 530 | // each version of notification will be sent to both places |
| 531 | std::vector<Data> dataInStorage; |
| 532 | std::copy(storage.begin(), storage.end(), std::back_inserter(dataInStorage)); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 533 | BOOST_REQUIRE_EQUAL(dataInStorage.size(), 4); |
Eric Newberry | c25e463 | 2021-02-11 10:48:11 -0800 | [diff] [blame] | 534 | BOOST_CHECK_EQUAL(dataInStorage[0].getName(), "/root/test/seq=0"); |
| 535 | BOOST_CHECK_EQUAL(dataInStorage[1].getName(), "/root/test/seq=1"); |
| 536 | BOOST_CHECK_EQUAL(dataInStorage[2].getName(), "/root/test/seq=2"); |
| 537 | BOOST_CHECK_EQUAL(dataInStorage[3].getName(), "/root/test/seq=3"); |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 538 | |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 539 | BOOST_CHECK_EQUAL(dataInStorage[0].getContent().blockFromValue(), block); |
| 540 | BOOST_CHECK_EQUAL(dataInStorage[1].getContent().blockFromValue(), block); |
| 541 | BOOST_CHECK_EQUAL(dataInStorage[2].getContent().blockFromValue(), block); |
| 542 | BOOST_CHECK_EQUAL(dataInStorage[3].getContent().blockFromValue(), block); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 543 | } |
| 544 | |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 545 | BOOST_AUTO_TEST_SUITE_END() // TestDispatcher |
| 546 | BOOST_AUTO_TEST_SUITE_END() // Mgmt |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 547 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 548 | } // namespace ndn::tests |