Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 0ba6d64 | 2017-07-17 00:53:22 +0000 | [diff] [blame] | 2 | /* |
Davide Pesavento | a599d2a | 2022-02-16 18:52:43 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2022, Regents of the University of California, |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [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/face-manager.hpp" |
Junxiao Shi | ea47bde | 2017-01-26 17:49:16 +0000 | [diff] [blame] | 27 | #include "face/protocol-factory.hpp" |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 28 | |
Davide Pesavento | 78ddcab | 2019-02-28 22:00:03 -0500 | [diff] [blame] | 29 | #include "manager-common-fixture.hpp" |
| 30 | #include "tests/daemon/face/dummy-face.hpp" |
| 31 | #include "tests/daemon/face/dummy-transport.hpp" |
Davide Pesavento | 97210d5 | 2016-10-14 15:45:48 +0200 | [diff] [blame] | 32 | |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 33 | #include <ndn-cxx/encoding/tlv.hpp> |
Davide Pesavento | 7a294d4 | 2017-02-21 21:46:44 -0500 | [diff] [blame] | 34 | #include <ndn-cxx/encoding/tlv-nfd.hpp> |
Junxiao Shi | 25c6ce4 | 2016-09-09 13:49:59 +0000 | [diff] [blame] | 35 | #include <ndn-cxx/mgmt/nfd/channel-status.hpp> |
Davide Pesavento | 2818132 | 2018-11-08 16:44:50 -0500 | [diff] [blame] | 36 | #include <ndn-cxx/mgmt/nfd/face-event-notification.hpp> |
| 37 | #include <ndn-cxx/mgmt/nfd/face-query-filter.hpp> |
| 38 | #include <ndn-cxx/mgmt/nfd/face-status.hpp> |
Junxiao Shi | 0ba6d64 | 2017-07-17 00:53:22 +0000 | [diff] [blame] | 39 | #include <ndn-cxx/net/network-monitor-stub.hpp> |
Davide Pesavento | b8bd5ee | 2019-02-03 22:53:40 -0500 | [diff] [blame] | 40 | #include <ndn-cxx/util/random.hpp> |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 41 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame^] | 42 | namespace nfd::tests { |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 43 | |
Davide Pesavento | 78ddcab | 2019-02-28 22:00:03 -0500 | [diff] [blame] | 44 | class FaceManagerFixture : public ManagerFixtureWithAuthenticator |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 45 | { |
| 46 | public: |
| 47 | FaceManagerFixture() |
Davide Pesavento | a4abfb0 | 2019-10-06 16:02:56 -0400 | [diff] [blame] | 48 | : m_faceSystem(m_faceTable, make_shared<ndn::net::NetworkMonitorStub>(0)) |
Junxiao Shi | ea47bde | 2017-01-26 17:49:16 +0000 | [diff] [blame] | 49 | , m_manager(m_faceSystem, m_dispatcher, *m_authenticator) |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 50 | { |
Junxiao Shi | 9ddf1b5 | 2016-08-22 03:58:55 +0000 | [diff] [blame] | 51 | setTopPrefix(); |
Yanbiao Li | df846e5 | 2016-01-30 21:53:47 -0800 | [diff] [blame] | 52 | setPrivilege("faces"); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | public: |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 56 | enum AddFaceFlags { |
| 57 | REMOVE_LAST_NOTIFICATION = 1 << 0, |
| 58 | SET_SCOPE_LOCAL = 1 << 1, |
| 59 | SET_URI_TEST = 1 << 2, |
Davide Pesavento | 6837d4f | 2017-07-15 13:01:03 -0400 | [diff] [blame] | 60 | RANDOMIZE_COUNTERS = 1 << 3, |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 61 | }; |
| 62 | |
| 63 | /** \brief adds a face to the FaceTable |
| 64 | * \param options bitwise OR'ed AddFaceFlags |
| 65 | */ |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 66 | shared_ptr<Face> |
Davide Pesavento | 6837d4f | 2017-07-15 13:01:03 -0400 | [diff] [blame] | 67 | addFace(unsigned int flags = 0) |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 68 | { |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 69 | std::string uri = "dummy://"; |
| 70 | ndn::nfd::FaceScope scope = ndn::nfd::FACE_SCOPE_NON_LOCAL; |
| 71 | |
Davide Pesavento | 2818132 | 2018-11-08 16:44:50 -0500 | [diff] [blame] | 72 | if (flags & SET_SCOPE_LOCAL) { |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 73 | scope = ndn::nfd::FACE_SCOPE_LOCAL; |
| 74 | } |
Davide Pesavento | 2818132 | 2018-11-08 16:44:50 -0500 | [diff] [blame] | 75 | if (flags & SET_URI_TEST) { |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 76 | uri = "test://"; |
| 77 | } |
| 78 | |
| 79 | auto face = make_shared<DummyFace>(uri, uri, scope); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 80 | m_faceTable.add(face); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 81 | |
Davide Pesavento | 2818132 | 2018-11-08 16:44:50 -0500 | [diff] [blame] | 82 | if (flags & RANDOMIZE_COUNTERS) { |
| 83 | const auto& counters = face->getCounters(); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 84 | randomizeCounter(counters.nInInterests); |
| 85 | randomizeCounter(counters.nOutInterests); |
| 86 | randomizeCounter(counters.nInData); |
| 87 | randomizeCounter(counters.nOutData); |
| 88 | randomizeCounter(counters.nInNacks); |
| 89 | randomizeCounter(counters.nOutNacks); |
| 90 | randomizeCounter(counters.nInPackets); |
| 91 | randomizeCounter(counters.nOutPackets); |
| 92 | randomizeCounter(counters.nInBytes); |
| 93 | randomizeCounter(counters.nOutBytes); |
| 94 | } |
| 95 | |
Davide Pesavento | 2818132 | 2018-11-08 16:44:50 -0500 | [diff] [blame] | 96 | advanceClocks(1_ms, 10); // wait for notification posted |
| 97 | if (flags & REMOVE_LAST_NOTIFICATION) { |
Davide Pesavento | b93fb6c | 2020-04-12 14:10:45 -0400 | [diff] [blame] | 98 | BOOST_REQUIRE(!m_responses.empty()); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 99 | m_responses.pop_back(); |
| 100 | } |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 101 | |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 102 | return face; |
| 103 | } |
| 104 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 105 | private: |
| 106 | template<typename T> |
Davide Pesavento | 6837d4f | 2017-07-15 13:01:03 -0400 | [diff] [blame] | 107 | static void |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 108 | randomizeCounter(const T& counter) |
| 109 | { |
Davide Pesavento | 5f47aa6 | 2016-10-07 22:09:09 +0200 | [diff] [blame] | 110 | static std::uniform_int_distribution<typename T::rep> dist; |
Davide Pesavento | b8bd5ee | 2019-02-03 22:53:40 -0500 | [diff] [blame] | 111 | const_cast<T&>(counter).set(dist(ndn::random::getRandomNumberEngine())); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 114 | protected: |
Junxiao Shi | ea47bde | 2017-01-26 17:49:16 +0000 | [diff] [blame] | 115 | FaceSystem m_faceSystem; |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 116 | FaceManager m_manager; |
| 117 | }; |
| 118 | |
Davide Pesavento | 97210d5 | 2016-10-14 15:45:48 +0200 | [diff] [blame] | 119 | BOOST_AUTO_TEST_SUITE(Mgmt) |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 120 | BOOST_FIXTURE_TEST_SUITE(TestFaceManager, FaceManagerFixture) |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 121 | |
| 122 | BOOST_AUTO_TEST_SUITE(DestroyFace) |
| 123 | |
| 124 | BOOST_AUTO_TEST_CASE(Existing) |
| 125 | { |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 126 | auto addedFace = addFace(REMOVE_LAST_NOTIFICATION | SET_SCOPE_LOCAL); // clear notification for creation |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 127 | |
| 128 | auto parameters = ControlParameters().setFaceId(addedFace->getId()); |
Junxiao Shi | 8a1f170 | 2017-07-03 00:05:08 +0000 | [diff] [blame] | 129 | auto req = makeControlCommandRequest("/localhost/nfd/faces/destroy", parameters); |
| 130 | receiveInterest(req); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 131 | |
| 132 | BOOST_REQUIRE_EQUAL(m_responses.size(), 2); // one response and one notification |
| 133 | // notification is already tested, so ignore it |
| 134 | |
Junxiao Shi | 8a1f170 | 2017-07-03 00:05:08 +0000 | [diff] [blame] | 135 | BOOST_CHECK_EQUAL(checkResponse(1, req.getName(), makeResponse(200, "OK", parameters)), |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 136 | CheckResponseResult::OK); |
| 137 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 138 | BOOST_CHECK_EQUAL(addedFace->getId(), face::INVALID_FACEID); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | BOOST_AUTO_TEST_CASE(NonExisting) |
| 142 | { |
| 143 | auto parameters = ControlParameters().setFaceId(65535); |
Junxiao Shi | 8a1f170 | 2017-07-03 00:05:08 +0000 | [diff] [blame] | 144 | auto req = makeControlCommandRequest("/localhost/nfd/faces/destroy", parameters); |
| 145 | receiveInterest(req); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 146 | |
| 147 | BOOST_REQUIRE_EQUAL(m_responses.size(), 1); |
Junxiao Shi | 8a1f170 | 2017-07-03 00:05:08 +0000 | [diff] [blame] | 148 | BOOST_CHECK_EQUAL(checkResponse(0, req.getName(), makeResponse(200, "OK", parameters)), |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 149 | CheckResponseResult::OK); |
| 150 | } |
| 151 | |
| 152 | BOOST_AUTO_TEST_SUITE_END() // DestroyFace |
| 153 | |
Davide Pesavento | 97210d5 | 2016-10-14 15:45:48 +0200 | [diff] [blame] | 154 | BOOST_AUTO_TEST_SUITE(Datasets) |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 155 | |
| 156 | BOOST_AUTO_TEST_CASE(FaceDataset) |
| 157 | { |
Davide Pesavento | 2818132 | 2018-11-08 16:44:50 -0500 | [diff] [blame] | 158 | const size_t nEntries = 32; |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 159 | for (size_t i = 0; i < nEntries; ++i) { |
| 160 | addFace(REMOVE_LAST_NOTIFICATION | SET_URI_TEST | RANDOMIZE_COUNTERS); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Davide Pesavento | 2818132 | 2018-11-08 16:44:50 -0500 | [diff] [blame] | 163 | receiveInterest(Interest("/localhost/nfd/faces/list").setCanBePrefix(true)); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 164 | |
Davide Pesavento | 6837d4f | 2017-07-15 13:01:03 -0400 | [diff] [blame] | 165 | Block content = concatenateResponses(); |
| 166 | content.parse(); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 167 | BOOST_REQUIRE_EQUAL(content.elements().size(), nEntries); |
| 168 | |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 169 | std::set<FaceId> faceIds; |
| 170 | for (size_t idx = 0; idx < nEntries; ++idx) { |
Davide Pesavento | 6837d4f | 2017-07-15 13:01:03 -0400 | [diff] [blame] | 171 | ndn::nfd::FaceStatus decodedStatus(content.elements()[idx]); |
Junxiao Shi | da93f1f | 2015-11-11 06:13:16 -0700 | [diff] [blame] | 172 | BOOST_CHECK(m_faceTable.get(decodedStatus.getFaceId()) != nullptr); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 173 | faceIds.insert(decodedStatus.getFaceId()); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 174 | } |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 175 | BOOST_CHECK_EQUAL(faceIds.size(), nEntries); |
Davide Pesavento | 2818132 | 2018-11-08 16:44:50 -0500 | [diff] [blame] | 176 | |
| 177 | ndn::nfd::FaceStatus status(content.elements().front()); |
| 178 | const Face* face = m_faceTable.get(status.getFaceId()); |
| 179 | BOOST_REQUIRE(face != nullptr); |
| 180 | |
| 181 | // check face properties |
| 182 | BOOST_CHECK_EQUAL(status.getRemoteUri(), face->getRemoteUri().toString()); |
| 183 | BOOST_CHECK_EQUAL(status.getLocalUri(), face->getLocalUri().toString()); |
| 184 | BOOST_CHECK_EQUAL(status.hasExpirationPeriod(), |
| 185 | face->getExpirationTime() != time::steady_clock::time_point::max()); |
| 186 | BOOST_CHECK_EQUAL(status.getFaceScope(), face->getScope()); |
| 187 | BOOST_CHECK_EQUAL(status.getFacePersistency(), face->getPersistency()); |
| 188 | BOOST_CHECK_EQUAL(status.getLinkType(), face->getLinkType()); |
| 189 | |
| 190 | // check link service properties |
| 191 | BOOST_CHECK_EQUAL(status.hasBaseCongestionMarkingInterval(), false); |
| 192 | BOOST_CHECK_EQUAL(status.hasDefaultCongestionThreshold(), false); |
| 193 | BOOST_CHECK_EQUAL(status.getFlags(), 0); |
| 194 | |
| 195 | // check transport properties |
| 196 | BOOST_CHECK_EQUAL(status.hasMtu(), true); |
| 197 | BOOST_CHECK_EQUAL(status.getMtu(), ndn::MAX_NDN_PACKET_SIZE); |
| 198 | |
| 199 | // check counters |
| 200 | BOOST_CHECK_EQUAL(status.getNInInterests(), face->getCounters().nInInterests); |
| 201 | BOOST_CHECK_EQUAL(status.getNInData(), face->getCounters().nInData); |
| 202 | BOOST_CHECK_EQUAL(status.getNInNacks(), face->getCounters().nInNacks); |
| 203 | BOOST_CHECK_EQUAL(status.getNOutInterests(), face->getCounters().nOutInterests); |
| 204 | BOOST_CHECK_EQUAL(status.getNOutData(), face->getCounters().nOutData); |
| 205 | BOOST_CHECK_EQUAL(status.getNOutNacks(), face->getCounters().nOutNacks); |
| 206 | BOOST_CHECK_EQUAL(status.getNInBytes(), face->getCounters().nInBytes); |
| 207 | BOOST_CHECK_EQUAL(status.getNOutBytes(), face->getCounters().nOutBytes); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | BOOST_AUTO_TEST_CASE(FaceQuery) |
| 211 | { |
Davide Pesavento | 6837d4f | 2017-07-15 13:01:03 -0400 | [diff] [blame] | 212 | using ndn::nfd::FaceQueryFilter; |
| 213 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 214 | auto face1 = addFace(REMOVE_LAST_NOTIFICATION); // dummy:// |
| 215 | auto face2 = addFace(REMOVE_LAST_NOTIFICATION | SET_SCOPE_LOCAL); // dummy://, local |
| 216 | auto face3 = addFace(REMOVE_LAST_NOTIFICATION | SET_URI_TEST); // test:// |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 217 | |
Davide Pesavento | deb5427 | 2022-03-11 18:51:05 -0500 | [diff] [blame] | 218 | auto generateQuery = [] (const auto& filter) { |
| 219 | return Interest(Name("/localhost/nfd/faces/query").append(tlv::GenericNameComponent, filter.wireEncode())) |
Davide Pesavento | 2818132 | 2018-11-08 16:44:50 -0500 | [diff] [blame] | 220 | .setCanBePrefix(true); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 221 | }; |
| 222 | |
Davide Pesavento | 2818132 | 2018-11-08 16:44:50 -0500 | [diff] [blame] | 223 | auto schemeQuery = generateQuery(FaceQueryFilter().setUriScheme("dummy")); |
| 224 | auto idQuery = generateQuery(FaceQueryFilter().setFaceId(face1->getId())); |
| 225 | auto scopeQuery = generateQuery(FaceQueryFilter().setFaceScope(ndn::nfd::FACE_SCOPE_NON_LOCAL)); |
Davide Pesavento | 6837d4f | 2017-07-15 13:01:03 -0400 | [diff] [blame] | 226 | auto invalidQueryName = Name("/localhost/nfd/faces/query") |
Davide Pesavento | deb5427 | 2022-03-11 18:51:05 -0500 | [diff] [blame] | 227 | .append(tlv::GenericNameComponent, ndn::makeStringBlock(tlv::Content, "invalid")); |
Davide Pesavento | 2818132 | 2018-11-08 16:44:50 -0500 | [diff] [blame] | 228 | auto invalidQuery = Interest(invalidQueryName).setCanBePrefix(true); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 229 | |
Davide Pesavento | 2818132 | 2018-11-08 16:44:50 -0500 | [diff] [blame] | 230 | receiveInterest(schemeQuery); // face1 and face2 expected |
| 231 | receiveInterest(idQuery); // face1 expected |
| 232 | receiveInterest(scopeQuery); // face1 and face3 expected |
| 233 | receiveInterest(invalidQuery); // nack expected |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 234 | |
| 235 | BOOST_REQUIRE_EQUAL(m_responses.size(), 4); |
| 236 | |
| 237 | Block content; |
| 238 | ndn::nfd::FaceStatus status; |
| 239 | |
| 240 | content = m_responses[0].getContent(); |
Davide Pesavento | 6837d4f | 2017-07-15 13:01:03 -0400 | [diff] [blame] | 241 | content.parse(); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 242 | BOOST_CHECK_EQUAL(content.elements().size(), 2); // face1 and face2 |
Davide Pesavento | 6837d4f | 2017-07-15 13:01:03 -0400 | [diff] [blame] | 243 | status.wireDecode(content.elements()[0]); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 244 | BOOST_CHECK_EQUAL(face1->getId(), status.getFaceId()); |
Davide Pesavento | 6837d4f | 2017-07-15 13:01:03 -0400 | [diff] [blame] | 245 | status.wireDecode(content.elements()[1]); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 246 | BOOST_CHECK_EQUAL(face2->getId(), status.getFaceId()); |
| 247 | |
| 248 | content = m_responses[1].getContent(); |
Davide Pesavento | 6837d4f | 2017-07-15 13:01:03 -0400 | [diff] [blame] | 249 | content.parse(); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 250 | BOOST_CHECK_EQUAL(content.elements().size(), 1); // face1 |
Davide Pesavento | 6837d4f | 2017-07-15 13:01:03 -0400 | [diff] [blame] | 251 | status.wireDecode(content.elements()[0]); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 252 | BOOST_CHECK_EQUAL(face1->getId(), status.getFaceId()); |
| 253 | |
| 254 | content = m_responses[2].getContent(); |
Davide Pesavento | 6837d4f | 2017-07-15 13:01:03 -0400 | [diff] [blame] | 255 | content.parse(); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 256 | BOOST_CHECK_EQUAL(content.elements().size(), 2); // face1 and face3 |
Davide Pesavento | 6837d4f | 2017-07-15 13:01:03 -0400 | [diff] [blame] | 257 | status.wireDecode(content.elements()[0]); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 258 | BOOST_CHECK_EQUAL(face1->getId(), status.getFaceId()); |
Davide Pesavento | 6837d4f | 2017-07-15 13:01:03 -0400 | [diff] [blame] | 259 | status.wireDecode(content.elements()[1]); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 260 | BOOST_CHECK_EQUAL(face3->getId(), status.getFaceId()); |
| 261 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 262 | ControlResponse expectedResponse(400, "Malformed filter"); // nack, 400, malformed filter |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 263 | BOOST_CHECK_EQUAL(checkResponse(3, invalidQueryName, expectedResponse, tlv::ContentType_Nack), |
| 264 | CheckResponseResult::OK); |
| 265 | } |
| 266 | |
Davide Pesavento | 8fd15e6 | 2017-04-06 19:58:54 -0400 | [diff] [blame] | 267 | class TestChannel : public face::Channel |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 268 | { |
| 269 | public: |
Davide Pesavento | 97210d5 | 2016-10-14 15:45:48 +0200 | [diff] [blame] | 270 | explicit |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 271 | TestChannel(const std::string& uri) |
| 272 | { |
| 273 | setUri(FaceUri(uri)); |
| 274 | } |
Davide Pesavento | c19408d | 2017-04-08 00:42:55 -0400 | [diff] [blame] | 275 | |
| 276 | bool |
| 277 | isListening() const final |
| 278 | { |
| 279 | return false; |
| 280 | } |
| 281 | |
| 282 | size_t |
| 283 | size() const final |
| 284 | { |
| 285 | return 0; |
| 286 | } |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 287 | }; |
| 288 | |
Davide Pesavento | e5eebad | 2017-04-06 20:23:26 -0400 | [diff] [blame] | 289 | class TestProtocolFactory : public face::ProtocolFactory |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 290 | { |
| 291 | public: |
Davide Pesavento | d27841b | 2018-11-13 00:22:24 -0500 | [diff] [blame] | 292 | using ProtocolFactory::ProtocolFactory; |
Junxiao Shi | 0ba6d64 | 2017-07-17 00:53:22 +0000 | [diff] [blame] | 293 | |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 294 | shared_ptr<TestChannel> |
| 295 | addChannel(const std::string& channelUri) |
| 296 | { |
| 297 | auto channel = make_shared<TestChannel>(channelUri); |
| 298 | m_channels.push_back(channel); |
| 299 | return channel; |
| 300 | } |
| 301 | |
| 302 | private: |
Davide Pesavento | d27841b | 2018-11-13 00:22:24 -0500 | [diff] [blame] | 303 | std::vector<shared_ptr<const face::Channel>> |
| 304 | doGetChannels() const final |
| 305 | { |
| 306 | return m_channels; |
| 307 | } |
| 308 | |
| 309 | private: |
Davide Pesavento | 8fd15e6 | 2017-04-06 19:58:54 -0400 | [diff] [blame] | 310 | std::vector<shared_ptr<const face::Channel>> m_channels; |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 311 | }; |
| 312 | |
| 313 | BOOST_AUTO_TEST_CASE(ChannelDataset) |
| 314 | { |
Davide Pesavento | 2818132 | 2018-11-08 16:44:50 -0500 | [diff] [blame] | 315 | m_faceSystem.m_factories["test"] = make_unique<TestProtocolFactory>(m_faceSystem.makePFCtorParams()); |
Davide Pesavento | b5eee20 | 2017-09-21 23:59:22 -0400 | [diff] [blame] | 316 | auto factory = static_cast<TestProtocolFactory*>(m_faceSystem.getFactoryById("test")); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 317 | |
Davide Pesavento | 2818132 | 2018-11-08 16:44:50 -0500 | [diff] [blame] | 318 | const size_t nEntries = 42; |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 319 | std::map<std::string, shared_ptr<TestChannel>> addedChannels; |
Eric Newberry | 1b4ba05 | 2016-10-07 23:04:07 -0700 | [diff] [blame] | 320 | for (size_t i = 0; i < nEntries; i++) { |
Davide Pesavento | 6837d4f | 2017-07-15 13:01:03 -0400 | [diff] [blame] | 321 | auto channel = factory->addChannel("test" + to_string(i) + "://"); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 322 | addedChannels[channel->getUri().toString()] = channel; |
| 323 | } |
| 324 | |
Davide Pesavento | 2818132 | 2018-11-08 16:44:50 -0500 | [diff] [blame] | 325 | receiveInterest(Interest("/localhost/nfd/faces/channels").setCanBePrefix(true)); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 326 | |
Davide Pesavento | 6837d4f | 2017-07-15 13:01:03 -0400 | [diff] [blame] | 327 | Block content = concatenateResponses(); |
| 328 | content.parse(); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 329 | BOOST_REQUIRE_EQUAL(content.elements().size(), nEntries); |
| 330 | |
| 331 | for (size_t idx = 0; idx < nEntries; ++idx) { |
Davide Pesavento | 6837d4f | 2017-07-15 13:01:03 -0400 | [diff] [blame] | 332 | ndn::nfd::ChannelStatus decodedStatus(content.elements()[idx]); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 333 | BOOST_CHECK(addedChannels.find(decodedStatus.getLocalUri()) != addedChannels.end()); |
| 334 | } |
| 335 | } |
| 336 | |
Davide Pesavento | 97210d5 | 2016-10-14 15:45:48 +0200 | [diff] [blame] | 337 | BOOST_AUTO_TEST_SUITE_END() // Datasets |
| 338 | |
| 339 | BOOST_AUTO_TEST_SUITE(Notifications) |
| 340 | |
Eric Newberry | 1b4ba05 | 2016-10-07 23:04:07 -0700 | [diff] [blame] | 341 | BOOST_AUTO_TEST_CASE(FaceEventCreated) |
Davide Pesavento | 97210d5 | 2016-10-14 15:45:48 +0200 | [diff] [blame] | 342 | { |
Eric Newberry | 1b4ba05 | 2016-10-07 23:04:07 -0700 | [diff] [blame] | 343 | auto face = addFace(); // trigger FACE_EVENT_CREATED notification |
| 344 | BOOST_CHECK_NE(face->getId(), face::INVALID_FACEID); |
| 345 | FaceId faceId = face->getId(); |
| 346 | |
| 347 | BOOST_CHECK_EQUAL(m_manager.m_faceStateChangeConn.count(faceId), 1); |
Davide Pesavento | 97210d5 | 2016-10-14 15:45:48 +0200 | [diff] [blame] | 348 | |
| 349 | // check notification |
Eric Newberry | 1b4ba05 | 2016-10-07 23:04:07 -0700 | [diff] [blame] | 350 | BOOST_REQUIRE_EQUAL(m_responses.size(), 1); |
Davide Pesavento | 6837d4f | 2017-07-15 13:01:03 -0400 | [diff] [blame] | 351 | Block payload = m_responses.back().getContent().blockFromValue(); |
Eric Newberry | 1b4ba05 | 2016-10-07 23:04:07 -0700 | [diff] [blame] | 352 | BOOST_CHECK_EQUAL(payload.type(), ndn::tlv::nfd::FaceEventNotification); |
Davide Pesavento | 6837d4f | 2017-07-15 13:01:03 -0400 | [diff] [blame] | 353 | ndn::nfd::FaceEventNotification notification(payload); |
Eric Newberry | 1b4ba05 | 2016-10-07 23:04:07 -0700 | [diff] [blame] | 354 | BOOST_CHECK_EQUAL(notification.getKind(), ndn::nfd::FACE_EVENT_CREATED); |
| 355 | BOOST_CHECK_EQUAL(notification.getFaceId(), faceId); |
| 356 | BOOST_CHECK_EQUAL(notification.getRemoteUri(), face->getRemoteUri().toString()); |
| 357 | BOOST_CHECK_EQUAL(notification.getLocalUri(), face->getLocalUri().toString()); |
| 358 | BOOST_CHECK_EQUAL(notification.getFaceScope(), ndn::nfd::FACE_SCOPE_NON_LOCAL); |
| 359 | BOOST_CHECK_EQUAL(notification.getFacePersistency(), ndn::nfd::FACE_PERSISTENCY_PERSISTENT); |
| 360 | BOOST_CHECK_EQUAL(notification.getLinkType(), ndn::nfd::LinkType::LINK_TYPE_POINT_TO_POINT); |
Davide Pesavento | 6837d4f | 2017-07-15 13:01:03 -0400 | [diff] [blame] | 361 | BOOST_CHECK_EQUAL(notification.getFlags(), 0); |
Eric Newberry | 1b4ba05 | 2016-10-07 23:04:07 -0700 | [diff] [blame] | 362 | } |
Davide Pesavento | 97210d5 | 2016-10-14 15:45:48 +0200 | [diff] [blame] | 363 | |
Eric Newberry | 1b4ba05 | 2016-10-07 23:04:07 -0700 | [diff] [blame] | 364 | BOOST_AUTO_TEST_CASE(FaceEventDownUp) |
| 365 | { |
| 366 | auto face = addFace(); |
| 367 | BOOST_CHECK_NE(face->getId(), face::INVALID_FACEID); |
| 368 | FaceId faceId = face->getId(); |
| 369 | |
| 370 | // trigger FACE_EVENT_DOWN notification |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame^] | 371 | dynamic_cast<DummyTransport*>(face->getTransport())->setState(face::FaceState::DOWN); |
Davide Pesavento | 2818132 | 2018-11-08 16:44:50 -0500 | [diff] [blame] | 372 | advanceClocks(1_ms, 10); |
Eric Newberry | 1b4ba05 | 2016-10-07 23:04:07 -0700 | [diff] [blame] | 373 | BOOST_CHECK_EQUAL(face->getState(), face::FaceState::DOWN); |
Davide Pesavento | 97210d5 | 2016-10-14 15:45:48 +0200 | [diff] [blame] | 374 | |
| 375 | // check notification |
| 376 | { |
Davide Pesavento | 97210d5 | 2016-10-14 15:45:48 +0200 | [diff] [blame] | 377 | BOOST_REQUIRE_EQUAL(m_responses.size(), 2); |
Davide Pesavento | 6837d4f | 2017-07-15 13:01:03 -0400 | [diff] [blame] | 378 | Block payload = m_responses.back().getContent().blockFromValue(); |
Davide Pesavento | 97210d5 | 2016-10-14 15:45:48 +0200 | [diff] [blame] | 379 | BOOST_CHECK_EQUAL(payload.type(), ndn::tlv::nfd::FaceEventNotification); |
Davide Pesavento | 6837d4f | 2017-07-15 13:01:03 -0400 | [diff] [blame] | 380 | ndn::nfd::FaceEventNotification notification(payload); |
Eric Newberry | 1b4ba05 | 2016-10-07 23:04:07 -0700 | [diff] [blame] | 381 | BOOST_CHECK_EQUAL(notification.getKind(), ndn::nfd::FACE_EVENT_DOWN); |
Davide Pesavento | 97210d5 | 2016-10-14 15:45:48 +0200 | [diff] [blame] | 382 | BOOST_CHECK_EQUAL(notification.getFaceId(), faceId); |
Eric Newberry | 1b4ba05 | 2016-10-07 23:04:07 -0700 | [diff] [blame] | 383 | BOOST_CHECK_EQUAL(notification.getRemoteUri(), face->getRemoteUri().toString()); |
| 384 | BOOST_CHECK_EQUAL(notification.getLocalUri(), face->getLocalUri().toString()); |
Davide Pesavento | 97210d5 | 2016-10-14 15:45:48 +0200 | [diff] [blame] | 385 | BOOST_CHECK_EQUAL(notification.getFaceScope(), ndn::nfd::FACE_SCOPE_NON_LOCAL); |
| 386 | BOOST_CHECK_EQUAL(notification.getFacePersistency(), ndn::nfd::FACE_PERSISTENCY_PERSISTENT); |
| 387 | BOOST_CHECK_EQUAL(notification.getLinkType(), ndn::nfd::LinkType::LINK_TYPE_POINT_TO_POINT); |
Davide Pesavento | 6837d4f | 2017-07-15 13:01:03 -0400 | [diff] [blame] | 388 | BOOST_CHECK_EQUAL(notification.getFlags(), 0); |
Davide Pesavento | 97210d5 | 2016-10-14 15:45:48 +0200 | [diff] [blame] | 389 | } |
Eric Newberry | 1b4ba05 | 2016-10-07 23:04:07 -0700 | [diff] [blame] | 390 | |
| 391 | // trigger FACE_EVENT_UP notification |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame^] | 392 | dynamic_cast<DummyTransport*>(face->getTransport())->setState(face::FaceState::UP); |
Davide Pesavento | 2818132 | 2018-11-08 16:44:50 -0500 | [diff] [blame] | 393 | advanceClocks(1_ms, 10); |
Eric Newberry | 1b4ba05 | 2016-10-07 23:04:07 -0700 | [diff] [blame] | 394 | BOOST_CHECK_EQUAL(face->getState(), face::FaceState::UP); |
| 395 | |
| 396 | // check notification |
| 397 | { |
Eric Newberry | 1b4ba05 | 2016-10-07 23:04:07 -0700 | [diff] [blame] | 398 | BOOST_REQUIRE_EQUAL(m_responses.size(), 3); |
Davide Pesavento | 6837d4f | 2017-07-15 13:01:03 -0400 | [diff] [blame] | 399 | Block payload = m_responses.back().getContent().blockFromValue(); |
Eric Newberry | 1b4ba05 | 2016-10-07 23:04:07 -0700 | [diff] [blame] | 400 | BOOST_CHECK_EQUAL(payload.type(), ndn::tlv::nfd::FaceEventNotification); |
Davide Pesavento | 6837d4f | 2017-07-15 13:01:03 -0400 | [diff] [blame] | 401 | ndn::nfd::FaceEventNotification notification(payload); |
Eric Newberry | 1b4ba05 | 2016-10-07 23:04:07 -0700 | [diff] [blame] | 402 | BOOST_CHECK_EQUAL(notification.getKind(), ndn::nfd::FACE_EVENT_UP); |
| 403 | BOOST_CHECK_EQUAL(notification.getFaceId(), faceId); |
| 404 | BOOST_CHECK_EQUAL(notification.getRemoteUri(), face->getRemoteUri().toString()); |
| 405 | BOOST_CHECK_EQUAL(notification.getLocalUri(), face->getLocalUri().toString()); |
| 406 | BOOST_CHECK_EQUAL(notification.getFaceScope(), ndn::nfd::FACE_SCOPE_NON_LOCAL); |
| 407 | BOOST_CHECK_EQUAL(notification.getFacePersistency(), ndn::nfd::FACE_PERSISTENCY_PERSISTENT); |
| 408 | BOOST_CHECK_EQUAL(notification.getLinkType(), ndn::nfd::LinkType::LINK_TYPE_POINT_TO_POINT); |
Davide Pesavento | 6837d4f | 2017-07-15 13:01:03 -0400 | [diff] [blame] | 409 | BOOST_CHECK_EQUAL(notification.getFlags(), 0); |
Eric Newberry | 1b4ba05 | 2016-10-07 23:04:07 -0700 | [diff] [blame] | 410 | } |
| 411 | } |
| 412 | |
| 413 | BOOST_AUTO_TEST_CASE(FaceEventDestroyed) |
| 414 | { |
| 415 | auto face = addFace(); |
| 416 | BOOST_CHECK_NE(face->getId(), face::INVALID_FACEID); |
| 417 | FaceId faceId = face->getId(); |
| 418 | |
| 419 | BOOST_CHECK_EQUAL(m_manager.m_faceStateChangeConn.count(faceId), 1); |
| 420 | |
| 421 | face->close(); // trigger FaceDestroy FACE_EVENT_DESTROYED |
Davide Pesavento | 2818132 | 2018-11-08 16:44:50 -0500 | [diff] [blame] | 422 | advanceClocks(1_ms, 10); |
Eric Newberry | 1b4ba05 | 2016-10-07 23:04:07 -0700 | [diff] [blame] | 423 | |
| 424 | // check notification |
Eric Newberry | 1b4ba05 | 2016-10-07 23:04:07 -0700 | [diff] [blame] | 425 | BOOST_REQUIRE_EQUAL(m_responses.size(), 2); |
Davide Pesavento | 6837d4f | 2017-07-15 13:01:03 -0400 | [diff] [blame] | 426 | Block payload = m_responses.back().getContent().blockFromValue(); |
Eric Newberry | 1b4ba05 | 2016-10-07 23:04:07 -0700 | [diff] [blame] | 427 | BOOST_CHECK_EQUAL(payload.type(), ndn::tlv::nfd::FaceEventNotification); |
Davide Pesavento | 6837d4f | 2017-07-15 13:01:03 -0400 | [diff] [blame] | 428 | ndn::nfd::FaceEventNotification notification(payload); |
Eric Newberry | 1b4ba05 | 2016-10-07 23:04:07 -0700 | [diff] [blame] | 429 | BOOST_CHECK_EQUAL(notification.getKind(), ndn::nfd::FACE_EVENT_DESTROYED); |
| 430 | BOOST_CHECK_EQUAL(notification.getFaceId(), faceId); |
| 431 | BOOST_CHECK_EQUAL(notification.getRemoteUri(), face->getRemoteUri().toString()); |
| 432 | BOOST_CHECK_EQUAL(notification.getLocalUri(), face->getLocalUri().toString()); |
| 433 | BOOST_CHECK_EQUAL(notification.getFaceScope(), ndn::nfd::FACE_SCOPE_NON_LOCAL); |
| 434 | BOOST_CHECK_EQUAL(notification.getFacePersistency(), ndn::nfd::FACE_PERSISTENCY_PERSISTENT); |
| 435 | BOOST_CHECK_EQUAL(notification.getLinkType(), ndn::nfd::LinkType::LINK_TYPE_POINT_TO_POINT); |
Davide Pesavento | 6837d4f | 2017-07-15 13:01:03 -0400 | [diff] [blame] | 436 | BOOST_CHECK_EQUAL(notification.getFlags(), 0); |
Eric Newberry | 1b4ba05 | 2016-10-07 23:04:07 -0700 | [diff] [blame] | 437 | |
| 438 | BOOST_CHECK_EQUAL(face->getId(), face::INVALID_FACEID); |
| 439 | BOOST_CHECK_EQUAL(m_manager.m_faceStateChangeConn.count(faceId), 0); |
Davide Pesavento | 97210d5 | 2016-10-14 15:45:48 +0200 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | BOOST_AUTO_TEST_SUITE_END() // Notifications |
| 443 | |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 444 | BOOST_AUTO_TEST_SUITE_END() // TestFaceManager |
| 445 | BOOST_AUTO_TEST_SUITE_END() // Mgmt |
| 446 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame^] | 447 | } // namespace nfd::tests |