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 | /* |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2017, 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" |
Davide Pesavento | 5f47aa6 | 2016-10-07 22:09:09 +0200 | [diff] [blame] | 27 | #include "core/random.hpp" |
Junxiao Shi | ea47bde | 2017-01-26 17:49:16 +0000 | [diff] [blame] | 28 | #include "face/protocol-factory.hpp" |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 29 | |
Davide Pesavento | 97210d5 | 2016-10-14 15:45:48 +0200 | [diff] [blame] | 30 | #include "nfd-manager-common-fixture.hpp" |
| 31 | #include "../face/dummy-face.hpp" |
Eric Newberry | 1b4ba05 | 2016-10-07 23:04:07 -0700 | [diff] [blame] | 32 | #include "../face/dummy-transport.hpp" |
Davide Pesavento | 97210d5 | 2016-10-14 15:45:48 +0200 | [diff] [blame] | 33 | |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 34 | #include <ndn-cxx/encoding/tlv.hpp> |
Davide Pesavento | 7a294d4 | 2017-02-21 21:46:44 -0500 | [diff] [blame] | 35 | #include <ndn-cxx/encoding/tlv-nfd.hpp> |
Junxiao Shi | 25c6ce4 | 2016-09-09 13:49:59 +0000 | [diff] [blame] | 36 | #include <ndn-cxx/mgmt/nfd/channel-status.hpp> |
| 37 | #include <ndn-cxx/mgmt/nfd/face-event-notification.hpp> |
Junxiao Shi | 0ba6d64 | 2017-07-17 00:53:22 +0000 | [diff] [blame^] | 38 | #include <ndn-cxx/net/network-monitor-stub.hpp> |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 39 | |
| 40 | namespace nfd { |
| 41 | namespace tests { |
| 42 | |
Yanbiao Li | df846e5 | 2016-01-30 21:53:47 -0800 | [diff] [blame] | 43 | class FaceManagerFixture : public NfdManagerCommonFixture |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 44 | { |
| 45 | public: |
| 46 | FaceManagerFixture() |
| 47 | : m_faceTable(m_forwarder.getFaceTable()) |
Junxiao Shi | ea47bde | 2017-01-26 17:49:16 +0000 | [diff] [blame] | 48 | , m_faceSystem(m_faceTable) |
| 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, |
| 60 | RANDOMIZE_COUNTERS = 1 << 3 |
| 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> |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 67 | addFace(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 | |
| 72 | if ((flags & SET_SCOPE_LOCAL) != 0) { |
| 73 | scope = ndn::nfd::FACE_SCOPE_LOCAL; |
| 74 | } |
| 75 | if ((flags & SET_URI_TEST) != 0) { |
| 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 | |
| 82 | if ((flags & RANDOMIZE_COUNTERS) != 0) { |
| 83 | const face::FaceCounters& counters = face->getCounters(); |
| 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 | |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 96 | advanceClocks(time::milliseconds(1), 10); // wait for notification posted |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 97 | if ((flags & REMOVE_LAST_NOTIFICATION) != 0) { |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 98 | m_responses.pop_back(); |
| 99 | } |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 100 | |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 101 | return face; |
| 102 | } |
| 103 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 104 | private: |
| 105 | template<typename T> |
| 106 | static typename std::enable_if<std::is_base_of<SimpleCounter, T>::value>::type |
| 107 | randomizeCounter(const T& counter) |
| 108 | { |
Davide Pesavento | 5f47aa6 | 2016-10-07 22:09:09 +0200 | [diff] [blame] | 109 | static std::uniform_int_distribution<typename T::rep> dist; |
| 110 | const_cast<T&>(counter).set(dist(getGlobalRng())); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 111 | } |
| 112 | |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 113 | protected: |
| 114 | FaceTable& m_faceTable; |
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 | { |
| 158 | size_t nEntries = 303; |
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 | |
Junxiao Shi | 8a1f170 | 2017-07-03 00:05:08 +0000 | [diff] [blame] | 163 | receiveInterest(Interest("/localhost/nfd/faces/list")); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 164 | |
| 165 | Block content; |
| 166 | BOOST_CHECK_NO_THROW(content = concatenateResponses()); |
| 167 | BOOST_CHECK_NO_THROW(content.parse()); |
| 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) { |
| 172 | BOOST_TEST_MESSAGE("processing element: " << idx); |
| 173 | |
| 174 | ndn::nfd::FaceStatus decodedStatus; |
| 175 | BOOST_REQUIRE_NO_THROW(decodedStatus.wireDecode(content.elements()[idx])); |
Junxiao Shi | da93f1f | 2015-11-11 06:13:16 -0700 | [diff] [blame] | 176 | BOOST_CHECK(m_faceTable.get(decodedStatus.getFaceId()) != nullptr); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 177 | faceIds.insert(decodedStatus.getFaceId()); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | BOOST_CHECK_EQUAL(faceIds.size(), nEntries); |
Junxiao Shi | da93f1f | 2015-11-11 06:13:16 -0700 | [diff] [blame] | 181 | // TODO#3325 check dataset contents including counter values |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | BOOST_AUTO_TEST_CASE(FaceQuery) |
| 185 | { |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 186 | auto face1 = addFace(REMOVE_LAST_NOTIFICATION); // dummy:// |
| 187 | auto face2 = addFace(REMOVE_LAST_NOTIFICATION | SET_SCOPE_LOCAL); // dummy://, local |
| 188 | auto face3 = addFace(REMOVE_LAST_NOTIFICATION | SET_URI_TEST); // test:// |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 189 | |
| 190 | auto generateQueryName = [] (const ndn::nfd::FaceQueryFilter& filter) { |
| 191 | return Name("/localhost/nfd/faces/query").append(filter.wireEncode()); |
| 192 | }; |
| 193 | |
| 194 | auto querySchemeName = |
| 195 | generateQueryName(ndn::nfd::FaceQueryFilter().setUriScheme("dummy")); |
| 196 | auto queryIdName = |
| 197 | generateQueryName(ndn::nfd::FaceQueryFilter().setFaceId(face1->getId())); |
| 198 | auto queryScopeName = |
| 199 | generateQueryName(ndn::nfd::FaceQueryFilter().setFaceScope(ndn::nfd::FACE_SCOPE_NON_LOCAL)); |
| 200 | auto invalidQueryName = |
| 201 | Name("/localhost/nfd/faces/query").append(ndn::makeStringBlock(tlv::Content, "invalid")); |
| 202 | |
Junxiao Shi | 8a1f170 | 2017-07-03 00:05:08 +0000 | [diff] [blame] | 203 | receiveInterest(Interest(querySchemeName)); // face1 and face2 expected |
| 204 | receiveInterest(Interest(queryIdName)); // face1 expected |
| 205 | receiveInterest(Interest(queryScopeName)); // face1 and face3 expected |
| 206 | receiveInterest(Interest(invalidQueryName)); // nack expected |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 207 | |
| 208 | BOOST_REQUIRE_EQUAL(m_responses.size(), 4); |
| 209 | |
| 210 | Block content; |
| 211 | ndn::nfd::FaceStatus status; |
| 212 | |
| 213 | content = m_responses[0].getContent(); |
| 214 | BOOST_CHECK_NO_THROW(content.parse()); |
| 215 | BOOST_CHECK_EQUAL(content.elements().size(), 2); // face1 and face2 |
| 216 | BOOST_CHECK_NO_THROW(status.wireDecode(content.elements()[0])); |
| 217 | BOOST_CHECK_EQUAL(face1->getId(), status.getFaceId()); |
| 218 | BOOST_CHECK_NO_THROW(status.wireDecode(content.elements()[1])); |
| 219 | BOOST_CHECK_EQUAL(face2->getId(), status.getFaceId()); |
| 220 | |
| 221 | content = m_responses[1].getContent(); |
| 222 | BOOST_CHECK_NO_THROW(content.parse()); |
| 223 | BOOST_CHECK_EQUAL(content.elements().size(), 1); // face1 |
| 224 | BOOST_CHECK_NO_THROW(status.wireDecode(content.elements()[0])); |
| 225 | BOOST_CHECK_EQUAL(face1->getId(), status.getFaceId()); |
| 226 | |
| 227 | content = m_responses[2].getContent(); |
| 228 | BOOST_CHECK_NO_THROW(content.parse()); |
| 229 | BOOST_CHECK_EQUAL(content.elements().size(), 2); // face1 and face3 |
| 230 | BOOST_CHECK_NO_THROW(status.wireDecode(content.elements()[0])); |
| 231 | BOOST_CHECK_EQUAL(face1->getId(), status.getFaceId()); |
| 232 | BOOST_CHECK_NO_THROW(status.wireDecode(content.elements()[1])); |
| 233 | BOOST_CHECK_EQUAL(face3->getId(), status.getFaceId()); |
| 234 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 235 | ControlResponse expectedResponse(400, "Malformed filter"); // nack, 400, malformed filter |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 236 | BOOST_CHECK_EQUAL(checkResponse(3, invalidQueryName, expectedResponse, tlv::ContentType_Nack), |
| 237 | CheckResponseResult::OK); |
| 238 | } |
| 239 | |
Davide Pesavento | 8fd15e6 | 2017-04-06 19:58:54 -0400 | [diff] [blame] | 240 | class TestChannel : public face::Channel |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 241 | { |
| 242 | public: |
Davide Pesavento | 97210d5 | 2016-10-14 15:45:48 +0200 | [diff] [blame] | 243 | explicit |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 244 | TestChannel(const std::string& uri) |
| 245 | { |
| 246 | setUri(FaceUri(uri)); |
| 247 | } |
Davide Pesavento | c19408d | 2017-04-08 00:42:55 -0400 | [diff] [blame] | 248 | |
| 249 | bool |
| 250 | isListening() const final |
| 251 | { |
| 252 | return false; |
| 253 | } |
| 254 | |
| 255 | size_t |
| 256 | size() const final |
| 257 | { |
| 258 | return 0; |
| 259 | } |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 260 | }; |
| 261 | |
Davide Pesavento | e5eebad | 2017-04-06 20:23:26 -0400 | [diff] [blame] | 262 | class TestProtocolFactory : public face::ProtocolFactory |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 263 | { |
| 264 | public: |
Junxiao Shi | 0ba6d64 | 2017-07-17 00:53:22 +0000 | [diff] [blame^] | 265 | TestProtocolFactory(const CtorParams& params) |
| 266 | : ProtocolFactory(params) |
| 267 | { |
| 268 | } |
| 269 | |
Junxiao Shi | c344304 | 2017-01-26 17:21:35 +0000 | [diff] [blame] | 270 | void |
| 271 | processConfig(OptionalConfigSection configSection, |
| 272 | FaceSystem::ConfigContext& context) final |
| 273 | { |
| 274 | } |
| 275 | |
| 276 | void |
Eric Newberry | 78e32b0 | 2017-04-01 14:34:44 +0000 | [diff] [blame] | 277 | createFace(const FaceUri& remoteUri, |
| 278 | const ndn::optional<FaceUri>& localUri, |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 279 | ndn::nfd::FacePersistency persistency, |
Eric Newberry | f40551a | 2016-09-05 15:41:16 -0700 | [diff] [blame] | 280 | bool wantLocalFieldsEnabled, |
Davide Pesavento | 8fd15e6 | 2017-04-06 19:58:54 -0400 | [diff] [blame] | 281 | const face::FaceCreatedCallback& onCreated, |
| 282 | const face::FaceCreationFailedCallback& onConnectFailed) final |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 283 | { |
| 284 | } |
| 285 | |
Davide Pesavento | 8fd15e6 | 2017-04-06 19:58:54 -0400 | [diff] [blame] | 286 | std::vector<shared_ptr<const face::Channel>> |
Davide Pesavento | 97210d5 | 2016-10-14 15:45:48 +0200 | [diff] [blame] | 287 | getChannels() const final |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 288 | { |
| 289 | return m_channels; |
| 290 | } |
| 291 | |
| 292 | public: |
| 293 | shared_ptr<TestChannel> |
| 294 | addChannel(const std::string& channelUri) |
| 295 | { |
| 296 | auto channel = make_shared<TestChannel>(channelUri); |
| 297 | m_channels.push_back(channel); |
| 298 | return channel; |
| 299 | } |
| 300 | |
| 301 | private: |
Davide Pesavento | 8fd15e6 | 2017-04-06 19:58:54 -0400 | [diff] [blame] | 302 | std::vector<shared_ptr<const face::Channel>> m_channels; |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 303 | }; |
| 304 | |
| 305 | BOOST_AUTO_TEST_CASE(ChannelDataset) |
| 306 | { |
Junxiao Shi | 0ba6d64 | 2017-07-17 00:53:22 +0000 | [diff] [blame^] | 307 | m_manager.m_faceSystem.m_factories["test"] = |
| 308 | make_unique<TestProtocolFactory>(m_faceSystem.makePFCtorParams()); |
Junxiao Shi | b47247d | 2017-01-24 15:09:16 +0000 | [diff] [blame] | 309 | auto factory = static_cast<TestProtocolFactory*>(m_manager.m_faceSystem.getFactoryById("test")); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 310 | |
| 311 | std::map<std::string, shared_ptr<TestChannel>> addedChannels; |
| 312 | size_t nEntries = 404; |
Eric Newberry | 1b4ba05 | 2016-10-07 23:04:07 -0700 | [diff] [blame] | 313 | for (size_t i = 0; i < nEntries; i++) { |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 314 | auto channel = factory->addChannel("test" + boost::lexical_cast<std::string>(i) + "://"); |
| 315 | addedChannels[channel->getUri().toString()] = channel; |
| 316 | } |
| 317 | |
Junxiao Shi | 8a1f170 | 2017-07-03 00:05:08 +0000 | [diff] [blame] | 318 | receiveInterest(Interest("/localhost/nfd/faces/channels")); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 319 | |
| 320 | Block content; |
| 321 | BOOST_CHECK_NO_THROW(content = concatenateResponses()); |
| 322 | BOOST_CHECK_NO_THROW(content.parse()); |
| 323 | BOOST_REQUIRE_EQUAL(content.elements().size(), nEntries); |
| 324 | |
| 325 | for (size_t idx = 0; idx < nEntries; ++idx) { |
| 326 | BOOST_TEST_MESSAGE("processing element: " << idx); |
| 327 | |
| 328 | ndn::nfd::ChannelStatus decodedStatus; |
| 329 | BOOST_CHECK_NO_THROW(decodedStatus.wireDecode(content.elements()[idx])); |
| 330 | BOOST_CHECK(addedChannels.find(decodedStatus.getLocalUri()) != addedChannels.end()); |
| 331 | } |
| 332 | } |
| 333 | |
Davide Pesavento | 97210d5 | 2016-10-14 15:45:48 +0200 | [diff] [blame] | 334 | BOOST_AUTO_TEST_SUITE_END() // Datasets |
| 335 | |
| 336 | BOOST_AUTO_TEST_SUITE(Notifications) |
| 337 | |
Eric Newberry | 1b4ba05 | 2016-10-07 23:04:07 -0700 | [diff] [blame] | 338 | BOOST_AUTO_TEST_CASE(FaceEventCreated) |
Davide Pesavento | 97210d5 | 2016-10-14 15:45:48 +0200 | [diff] [blame] | 339 | { |
Eric Newberry | 1b4ba05 | 2016-10-07 23:04:07 -0700 | [diff] [blame] | 340 | auto face = addFace(); // trigger FACE_EVENT_CREATED notification |
| 341 | BOOST_CHECK_NE(face->getId(), face::INVALID_FACEID); |
| 342 | FaceId faceId = face->getId(); |
| 343 | |
| 344 | BOOST_CHECK_EQUAL(m_manager.m_faceStateChangeConn.count(faceId), 1); |
Davide Pesavento | 97210d5 | 2016-10-14 15:45:48 +0200 | [diff] [blame] | 345 | |
| 346 | // check notification |
Eric Newberry | 1b4ba05 | 2016-10-07 23:04:07 -0700 | [diff] [blame] | 347 | Block payload; |
| 348 | ndn::nfd::FaceEventNotification notification; |
| 349 | BOOST_REQUIRE_EQUAL(m_responses.size(), 1); |
| 350 | BOOST_CHECK_NO_THROW(payload = m_responses.back().getContent().blockFromValue()); |
| 351 | BOOST_CHECK_EQUAL(payload.type(), ndn::tlv::nfd::FaceEventNotification); |
| 352 | BOOST_CHECK_NO_THROW(notification.wireDecode(payload)); |
| 353 | BOOST_CHECK_EQUAL(notification.getKind(), ndn::nfd::FACE_EVENT_CREATED); |
| 354 | BOOST_CHECK_EQUAL(notification.getFaceId(), faceId); |
| 355 | BOOST_CHECK_EQUAL(notification.getRemoteUri(), face->getRemoteUri().toString()); |
| 356 | BOOST_CHECK_EQUAL(notification.getLocalUri(), face->getLocalUri().toString()); |
| 357 | BOOST_CHECK_EQUAL(notification.getFaceScope(), ndn::nfd::FACE_SCOPE_NON_LOCAL); |
| 358 | BOOST_CHECK_EQUAL(notification.getFacePersistency(), ndn::nfd::FACE_PERSISTENCY_PERSISTENT); |
| 359 | BOOST_CHECK_EQUAL(notification.getLinkType(), ndn::nfd::LinkType::LINK_TYPE_POINT_TO_POINT); |
| 360 | BOOST_CHECK_EQUAL(notification.getFlags(), 0x0); |
| 361 | } |
Davide Pesavento | 97210d5 | 2016-10-14 15:45:48 +0200 | [diff] [blame] | 362 | |
Eric Newberry | 1b4ba05 | 2016-10-07 23:04:07 -0700 | [diff] [blame] | 363 | BOOST_AUTO_TEST_CASE(FaceEventDownUp) |
| 364 | { |
| 365 | auto face = addFace(); |
| 366 | BOOST_CHECK_NE(face->getId(), face::INVALID_FACEID); |
| 367 | FaceId faceId = face->getId(); |
| 368 | |
| 369 | // trigger FACE_EVENT_DOWN notification |
| 370 | dynamic_cast<face::tests::DummyTransport*>(face->getTransport())->setState(face::FaceState::DOWN); |
Davide Pesavento | 97210d5 | 2016-10-14 15:45:48 +0200 | [diff] [blame] | 371 | advanceClocks(time::milliseconds(1), 10); |
Eric Newberry | 1b4ba05 | 2016-10-07 23:04:07 -0700 | [diff] [blame] | 372 | BOOST_CHECK_EQUAL(face->getState(), face::FaceState::DOWN); |
Davide Pesavento | 97210d5 | 2016-10-14 15:45:48 +0200 | [diff] [blame] | 373 | |
| 374 | // check notification |
| 375 | { |
| 376 | Block payload; |
| 377 | ndn::nfd::FaceEventNotification notification; |
| 378 | BOOST_REQUIRE_EQUAL(m_responses.size(), 2); |
Eric Newberry | 1b4ba05 | 2016-10-07 23:04:07 -0700 | [diff] [blame] | 379 | BOOST_CHECK_NO_THROW(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); |
| 381 | BOOST_CHECK_NO_THROW(notification.wireDecode(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); |
Eric Newberry | 1b4ba05 | 2016-10-07 23:04:07 -0700 | [diff] [blame] | 389 | BOOST_CHECK_EQUAL(notification.getFlags(), 0x0); |
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); |
| 394 | advanceClocks(time::milliseconds(1), 10); |
| 395 | BOOST_CHECK_EQUAL(face->getState(), face::FaceState::UP); |
| 396 | |
| 397 | // check notification |
| 398 | { |
| 399 | Block payload; |
| 400 | ndn::nfd::FaceEventNotification notification; |
| 401 | BOOST_REQUIRE_EQUAL(m_responses.size(), 3); |
| 402 | BOOST_CHECK_NO_THROW(payload = m_responses.back().getContent().blockFromValue()); |
| 403 | BOOST_CHECK_EQUAL(payload.type(), ndn::tlv::nfd::FaceEventNotification); |
| 404 | BOOST_CHECK_NO_THROW(notification.wireDecode(payload)); |
| 405 | BOOST_CHECK_EQUAL(notification.getKind(), ndn::nfd::FACE_EVENT_UP); |
| 406 | BOOST_CHECK_EQUAL(notification.getFaceId(), faceId); |
| 407 | BOOST_CHECK_EQUAL(notification.getRemoteUri(), face->getRemoteUri().toString()); |
| 408 | BOOST_CHECK_EQUAL(notification.getLocalUri(), face->getLocalUri().toString()); |
| 409 | BOOST_CHECK_EQUAL(notification.getFaceScope(), ndn::nfd::FACE_SCOPE_NON_LOCAL); |
| 410 | BOOST_CHECK_EQUAL(notification.getFacePersistency(), ndn::nfd::FACE_PERSISTENCY_PERSISTENT); |
| 411 | BOOST_CHECK_EQUAL(notification.getLinkType(), ndn::nfd::LinkType::LINK_TYPE_POINT_TO_POINT); |
| 412 | BOOST_CHECK_EQUAL(notification.getFlags(), 0x0); |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | BOOST_AUTO_TEST_CASE(FaceEventDestroyed) |
| 417 | { |
| 418 | auto face = addFace(); |
| 419 | BOOST_CHECK_NE(face->getId(), face::INVALID_FACEID); |
| 420 | FaceId faceId = face->getId(); |
| 421 | |
| 422 | BOOST_CHECK_EQUAL(m_manager.m_faceStateChangeConn.count(faceId), 1); |
| 423 | |
| 424 | face->close(); // trigger FaceDestroy FACE_EVENT_DESTROYED |
| 425 | advanceClocks(time::milliseconds(1), 10); |
| 426 | |
| 427 | // check notification |
| 428 | Block payload; |
| 429 | ndn::nfd::FaceEventNotification notification; |
| 430 | BOOST_REQUIRE_EQUAL(m_responses.size(), 2); |
| 431 | BOOST_CHECK_NO_THROW(payload = m_responses.back().getContent().blockFromValue()); |
| 432 | BOOST_CHECK_EQUAL(payload.type(), ndn::tlv::nfd::FaceEventNotification); |
| 433 | BOOST_CHECK_NO_THROW(notification.wireDecode(payload)); |
| 434 | BOOST_CHECK_EQUAL(notification.getKind(), ndn::nfd::FACE_EVENT_DESTROYED); |
| 435 | BOOST_CHECK_EQUAL(notification.getFaceId(), faceId); |
| 436 | BOOST_CHECK_EQUAL(notification.getRemoteUri(), face->getRemoteUri().toString()); |
| 437 | BOOST_CHECK_EQUAL(notification.getLocalUri(), face->getLocalUri().toString()); |
| 438 | BOOST_CHECK_EQUAL(notification.getFaceScope(), ndn::nfd::FACE_SCOPE_NON_LOCAL); |
| 439 | BOOST_CHECK_EQUAL(notification.getFacePersistency(), ndn::nfd::FACE_PERSISTENCY_PERSISTENT); |
| 440 | BOOST_CHECK_EQUAL(notification.getLinkType(), ndn::nfd::LinkType::LINK_TYPE_POINT_TO_POINT); |
| 441 | BOOST_CHECK_EQUAL(notification.getFlags(), 0x0); |
| 442 | |
| 443 | BOOST_CHECK_EQUAL(face->getId(), face::INVALID_FACEID); |
| 444 | BOOST_CHECK_EQUAL(m_manager.m_faceStateChangeConn.count(faceId), 0); |
Davide Pesavento | 97210d5 | 2016-10-14 15:45:48 +0200 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | BOOST_AUTO_TEST_SUITE_END() // Notifications |
| 448 | |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 449 | BOOST_AUTO_TEST_SUITE_END() // TestFaceManager |
| 450 | BOOST_AUTO_TEST_SUITE_END() // Mgmt |
| 451 | |
| 452 | } // namespace tests |
| 453 | } // namespace nfd |