Junxiao Shi | f2bfb44 | 2018-01-05 12:34:57 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2022, Regents of the University of California, |
Junxiao Shi | f2bfb44 | 2018-01-05 12:34:57 +0000 | [diff] [blame] | 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis. |
| 10 | * |
| 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 24 | */ |
| 25 | |
| 26 | #include "mgmt/cs-manager.hpp" |
Davide Pesavento | 2818132 | 2018-11-08 16:44:50 -0500 | [diff] [blame] | 27 | |
Davide Pesavento | 78ddcab | 2019-02-28 22:00:03 -0500 | [diff] [blame] | 28 | #include "manager-common-fixture.hpp" |
Davide Pesavento | 2818132 | 2018-11-08 16:44:50 -0500 | [diff] [blame] | 29 | |
Junxiao Shi | f2bfb44 | 2018-01-05 12:34:57 +0000 | [diff] [blame] | 30 | #include <ndn-cxx/mgmt/nfd/cs-info.hpp> |
| 31 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 32 | namespace nfd::tests { |
Junxiao Shi | f2bfb44 | 2018-01-05 12:34:57 +0000 | [diff] [blame] | 33 | |
Davide Pesavento | 78ddcab | 2019-02-28 22:00:03 -0500 | [diff] [blame] | 34 | class CsManagerFixture : public ManagerFixtureWithAuthenticator |
Junxiao Shi | f2bfb44 | 2018-01-05 12:34:57 +0000 | [diff] [blame] | 35 | { |
| 36 | public: |
| 37 | CsManagerFixture() |
| 38 | : m_cs(m_forwarder.getCs()) |
| 39 | , m_fwCnt(const_cast<ForwarderCounters&>(m_forwarder.getCounters())) |
| 40 | , m_manager(m_cs, m_fwCnt, m_dispatcher, *m_authenticator) |
| 41 | { |
| 42 | setTopPrefix(); |
Junxiao Shi | c9b5e01 | 2018-02-07 15:04:18 +0000 | [diff] [blame] | 43 | setPrivilege("cs"); |
Junxiao Shi | f2bfb44 | 2018-01-05 12:34:57 +0000 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | protected: |
| 47 | Cs& m_cs; |
| 48 | ForwarderCounters& m_fwCnt; |
| 49 | CsManager m_manager; |
| 50 | }; |
| 51 | |
| 52 | BOOST_AUTO_TEST_SUITE(Mgmt) |
| 53 | BOOST_FIXTURE_TEST_SUITE(TestCsManager, CsManagerFixture) |
| 54 | |
Junxiao Shi | c9b5e01 | 2018-02-07 15:04:18 +0000 | [diff] [blame] | 55 | BOOST_AUTO_TEST_CASE(Config) |
| 56 | { |
| 57 | using ndn::nfd::CsFlagBit; |
| 58 | const Name cmdPrefix("/localhost/nfd/cs/config"); |
| 59 | |
| 60 | // setup initial CS config |
| 61 | m_cs.setLimit(22129); |
| 62 | m_cs.enableAdmit(false); |
| 63 | m_cs.enableServe(true); |
| 64 | |
| 65 | // send empty cs/config command |
Davide Pesavento | 20cafa8 | 2022-07-25 01:15:03 -0400 | [diff] [blame] | 66 | auto req = makeControlCommandRequest(cmdPrefix); |
Junxiao Shi | c9b5e01 | 2018-02-07 15:04:18 +0000 | [diff] [blame] | 67 | receiveInterest(req); |
| 68 | |
| 69 | // response shall reflect current config |
| 70 | ControlParameters body; |
| 71 | body.setCapacity(22129); |
| 72 | body.setFlagBit(CsFlagBit::BIT_CS_ENABLE_ADMIT, false, false); |
| 73 | body.setFlagBit(CsFlagBit::BIT_CS_ENABLE_SERVE, true, false); |
| 74 | BOOST_CHECK_EQUAL(checkResponse(0, req.getName(), |
| 75 | ControlResponse(200, "OK").setBody(body.wireEncode())), |
| 76 | CheckResponseResult::OK); |
| 77 | |
| 78 | // send filled cs/config command |
| 79 | ControlParameters parameters; |
| 80 | parameters.setCapacity(18609); |
| 81 | parameters.setFlagBit(CsFlagBit::BIT_CS_ENABLE_ADMIT, true); |
| 82 | parameters.setFlagBit(CsFlagBit::BIT_CS_ENABLE_SERVE, false); |
| 83 | req = makeControlCommandRequest(cmdPrefix, parameters); |
| 84 | receiveInterest(req); |
| 85 | |
| 86 | // response shall reflect updated config |
| 87 | body.setCapacity(18609); |
| 88 | body.setFlagBit(CsFlagBit::BIT_CS_ENABLE_ADMIT, true, false); |
| 89 | body.setFlagBit(CsFlagBit::BIT_CS_ENABLE_SERVE, false, false); |
| 90 | BOOST_CHECK_EQUAL(checkResponse(1, req.getName(), |
| 91 | ControlResponse(200, "OK").setBody(body.wireEncode())), |
| 92 | CheckResponseResult::OK); |
| 93 | |
| 94 | // CS shall have updated config |
| 95 | BOOST_CHECK_EQUAL(m_cs.getLimit(), 18609); |
| 96 | BOOST_CHECK_EQUAL(m_cs.shouldAdmit(), true); |
| 97 | BOOST_CHECK_EQUAL(m_cs.shouldServe(), false); |
| 98 | } |
| 99 | |
Junxiao Shi | bd72431 | 2018-04-17 02:23:48 +0000 | [diff] [blame] | 100 | BOOST_AUTO_TEST_CASE(Erase) |
| 101 | { |
| 102 | m_cs.setLimit(CsManager::ERASE_LIMIT * 5); |
| 103 | m_cs.insert(*makeData("/B/C/1")); |
| 104 | m_cs.insert(*makeData("/B/C/2")); |
| 105 | m_cs.insert(*makeData("/B/D/3")); |
| 106 | m_cs.insert(*makeData("/B/D/4")); |
| 107 | for (size_t i = 0; i < CsManager::ERASE_LIMIT - 1; ++i) { |
| 108 | m_cs.insert(*makeData(Name("/E").appendSequenceNumber(i))); |
| 109 | } |
| 110 | for (size_t i = 0; i < CsManager::ERASE_LIMIT; ++i) { |
| 111 | m_cs.insert(*makeData(Name("/F").appendSequenceNumber(i))); |
| 112 | } |
| 113 | for (size_t i = 0; i < CsManager::ERASE_LIMIT + 1; ++i) { |
| 114 | m_cs.insert(*makeData(Name("/G").appendSequenceNumber(i))); |
| 115 | } |
| 116 | for (size_t i = 0; i < CsManager::ERASE_LIMIT + 1; ++i) { |
| 117 | m_cs.insert(*makeData(Name("/H").appendSequenceNumber(i))); |
| 118 | } |
| 119 | const Name cmdPrefix("/localhost/nfd/cs/erase"); |
| 120 | |
| 121 | // requested Name matches no Data |
| 122 | auto req = makeControlCommandRequest(cmdPrefix, |
| 123 | ControlParameters().setName("/A").setCount(1)); |
| 124 | receiveInterest(req); |
| 125 | |
| 126 | // response should include zero as actual Count |
| 127 | ControlParameters body; |
| 128 | body.setName("/A"); |
| 129 | body.setCount(0); |
| 130 | BOOST_CHECK_EQUAL(checkResponse(0, req.getName(), |
| 131 | ControlResponse(200, "OK").setBody(body.wireEncode())), |
| 132 | CheckResponseResult::OK); |
| 133 | |
| 134 | // requested Count is less than erase limit |
| 135 | req = makeControlCommandRequest(cmdPrefix, |
| 136 | ControlParameters().setName("/B").setCount(3)); |
| 137 | receiveInterest(req); |
| 138 | |
| 139 | // response should include actual Count and omit Capacity |
| 140 | body.setName("/B"); |
| 141 | body.setCount(3); |
| 142 | BOOST_CHECK_EQUAL(checkResponse(1, req.getName(), |
| 143 | ControlResponse(200, "OK").setBody(body.wireEncode())), |
| 144 | CheckResponseResult::OK); |
| 145 | |
| 146 | // requested Count equals erase limit |
| 147 | req = makeControlCommandRequest(cmdPrefix, |
| 148 | ControlParameters().setName("/E").setCount(CsManager::ERASE_LIMIT)); |
| 149 | receiveInterest(req); |
| 150 | |
| 151 | // response should include actual Count and omit Capacity |
| 152 | body.setName("/E"); |
| 153 | body.setCount(CsManager::ERASE_LIMIT - 1); |
| 154 | BOOST_CHECK_EQUAL(checkResponse(2, req.getName(), |
| 155 | ControlResponse(200, "OK").setBody(body.wireEncode())), |
| 156 | CheckResponseResult::OK); |
| 157 | |
| 158 | // requested Count exceeds erase limit, but there are no more Data |
| 159 | req = makeControlCommandRequest(cmdPrefix, |
| 160 | ControlParameters().setName("/F").setCount(CsManager::ERASE_LIMIT + 1)); |
| 161 | receiveInterest(req); |
| 162 | |
| 163 | // response should include actual Count and omit Capacity |
| 164 | body.setName("/F"); |
| 165 | body.setCount(CsManager::ERASE_LIMIT); |
| 166 | BOOST_CHECK_EQUAL(checkResponse(3, req.getName(), |
| 167 | ControlResponse(200, "OK").setBody(body.wireEncode())), |
| 168 | CheckResponseResult::OK); |
| 169 | |
| 170 | // requested Count exceeds erase limit, and there are more Data |
| 171 | req = makeControlCommandRequest(cmdPrefix, |
| 172 | ControlParameters().setName("/G").setCount(CsManager::ERASE_LIMIT + 1)); |
| 173 | receiveInterest(req); |
| 174 | |
| 175 | // response should include both actual Count and Capacity |
| 176 | body.setName("/G"); |
| 177 | body.setCount(CsManager::ERASE_LIMIT); |
| 178 | body.setCapacity(CsManager::ERASE_LIMIT); |
| 179 | BOOST_CHECK_EQUAL(checkResponse(4, req.getName(), |
| 180 | ControlResponse(200, "OK").setBody(body.wireEncode())), |
| 181 | CheckResponseResult::OK); |
| 182 | |
| 183 | // request omit Count, which implies "no limit" aka exceeds erase limit |
| 184 | req = makeControlCommandRequest(cmdPrefix, |
| 185 | ControlParameters().setName("/H")); |
| 186 | receiveInterest(req); |
| 187 | |
| 188 | // response should include both actual Count and Capacity since there are more Data |
| 189 | body.setName("/H"); |
| 190 | body.setCount(CsManager::ERASE_LIMIT); |
| 191 | body.setCapacity(CsManager::ERASE_LIMIT); |
| 192 | BOOST_CHECK_EQUAL(checkResponse(5, req.getName(), |
| 193 | ControlResponse(200, "OK").setBody(body.wireEncode())), |
| 194 | CheckResponseResult::OK); |
| 195 | |
| 196 | // one Data each under /A, /G, /H remain, all other Data are erased |
| 197 | BOOST_CHECK_EQUAL(m_cs.size(), 3); |
| 198 | } |
| 199 | |
Junxiao Shi | f2bfb44 | 2018-01-05 12:34:57 +0000 | [diff] [blame] | 200 | BOOST_AUTO_TEST_CASE(Info) |
| 201 | { |
Junxiao Shi | 26667e1 | 2018-02-08 20:01:18 +0000 | [diff] [blame] | 202 | m_cs.setLimit(2681); |
Davide Pesavento | 2818132 | 2018-11-08 16:44:50 -0500 | [diff] [blame] | 203 | for (uint64_t i = 0; i < 310; ++i) { |
Junxiao Shi | 26667e1 | 2018-02-08 20:01:18 +0000 | [diff] [blame] | 204 | m_cs.insert(*makeData(Name("/Q8H4oi4g").appendSequenceNumber(i))); |
| 205 | } |
| 206 | m_cs.enableAdmit(false); |
| 207 | m_cs.enableServe(true); |
Junxiao Shi | f2bfb44 | 2018-01-05 12:34:57 +0000 | [diff] [blame] | 208 | m_fwCnt.nCsHits.set(362); |
| 209 | m_fwCnt.nCsMisses.set(1493); |
| 210 | |
Junxiao Shi | 9d72785 | 2019-05-14 13:44:22 -0600 | [diff] [blame] | 211 | receiveInterest(*makeInterest("/localhost/nfd/cs/info", true)); |
Junxiao Shi | f2bfb44 | 2018-01-05 12:34:57 +0000 | [diff] [blame] | 212 | Block dataset = concatenateResponses(); |
| 213 | dataset.parse(); |
| 214 | BOOST_REQUIRE_EQUAL(dataset.elements_size(), 1); |
| 215 | |
| 216 | ndn::nfd::CsInfo info(*dataset.elements_begin()); |
Junxiao Shi | 26667e1 | 2018-02-08 20:01:18 +0000 | [diff] [blame] | 217 | BOOST_CHECK_EQUAL(info.getCapacity(), 2681); |
| 218 | BOOST_CHECK_EQUAL(info.getEnableAdmit(), false); |
| 219 | BOOST_CHECK_EQUAL(info.getEnableServe(), true); |
| 220 | BOOST_CHECK_EQUAL(info.getNEntries(), 310); |
Junxiao Shi | f2bfb44 | 2018-01-05 12:34:57 +0000 | [diff] [blame] | 221 | BOOST_CHECK_EQUAL(info.getNHits(), 362); |
| 222 | BOOST_CHECK_EQUAL(info.getNMisses(), 1493); |
| 223 | } |
| 224 | |
| 225 | BOOST_AUTO_TEST_SUITE_END() // TestCsManager |
| 226 | BOOST_AUTO_TEST_SUITE_END() // Mgmt |
| 227 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 228 | } // namespace nfd::tests |