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