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