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