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> |
Junxiao Shi | 0ba6d64 | 2017-07-17 00:53:22 +0000 | [diff] [blame] | 37 | #include <ndn-cxx/net/network-monitor-stub.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, |
Davide Pesavento | 6837d4f | 2017-07-15 13:01:03 -0400 | [diff] [blame] | 59 | RANDOMIZE_COUNTERS = 1 << 3, |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 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> |
Davide Pesavento | 6837d4f | 2017-07-15 13:01:03 -0400 | [diff] [blame] | 66 | addFace(unsigned 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> |
Davide Pesavento | 6837d4f | 2017-07-15 13:01:03 -0400 | [diff] [blame] | 105 | static void |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 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 | { |
Davide Pesavento | 6837d4f | 2017-07-15 13:01:03 -0400 | [diff] [blame] | 157 | const 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 | |
Davide Pesavento | 6837d4f | 2017-07-15 13:01:03 -0400 | [diff] [blame] | 164 | Block content = concatenateResponses(); |
| 165 | content.parse(); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 166 | BOOST_REQUIRE_EQUAL(content.elements().size(), nEntries); |
| 167 | |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 168 | std::set<FaceId> faceIds; |
| 169 | for (size_t idx = 0; idx < nEntries; ++idx) { |
Davide Pesavento | 6837d4f | 2017-07-15 13:01:03 -0400 | [diff] [blame] | 170 | ndn::nfd::FaceStatus decodedStatus(content.elements()[idx]); |
Junxiao Shi | da93f1f | 2015-11-11 06:13:16 -0700 | [diff] [blame] | 171 | BOOST_CHECK(m_faceTable.get(decodedStatus.getFaceId()) != nullptr); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 172 | faceIds.insert(decodedStatus.getFaceId()); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | BOOST_CHECK_EQUAL(faceIds.size(), nEntries); |
Junxiao Shi | da93f1f | 2015-11-11 06:13:16 -0700 | [diff] [blame] | 176 | // TODO#3325 check dataset contents including counter values |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | BOOST_AUTO_TEST_CASE(FaceQuery) |
| 180 | { |
Davide Pesavento | 6837d4f | 2017-07-15 13:01:03 -0400 | [diff] [blame] | 181 | using ndn::nfd::FaceQueryFilter; |
| 182 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 183 | auto face1 = addFace(REMOVE_LAST_NOTIFICATION); // dummy:// |
| 184 | auto face2 = addFace(REMOVE_LAST_NOTIFICATION | SET_SCOPE_LOCAL); // dummy://, local |
| 185 | auto face3 = addFace(REMOVE_LAST_NOTIFICATION | SET_URI_TEST); // test:// |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 186 | |
Davide Pesavento | 6837d4f | 2017-07-15 13:01:03 -0400 | [diff] [blame] | 187 | auto generateQueryName = [] (const FaceQueryFilter& filter) { |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 188 | return Name("/localhost/nfd/faces/query").append(filter.wireEncode()); |
| 189 | }; |
| 190 | |
Davide Pesavento | 6837d4f | 2017-07-15 13:01:03 -0400 | [diff] [blame] | 191 | auto querySchemeName = generateQueryName(FaceQueryFilter().setUriScheme("dummy")); |
| 192 | auto queryIdName = generateQueryName(FaceQueryFilter().setFaceId(face1->getId())); |
| 193 | auto queryScopeName = generateQueryName(FaceQueryFilter().setFaceScope(ndn::nfd::FACE_SCOPE_NON_LOCAL)); |
| 194 | auto invalidQueryName = Name("/localhost/nfd/faces/query") |
| 195 | .append(ndn::makeStringBlock(tlv::Content, "invalid")); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 196 | |
Junxiao Shi | 8a1f170 | 2017-07-03 00:05:08 +0000 | [diff] [blame] | 197 | receiveInterest(Interest(querySchemeName)); // face1 and face2 expected |
| 198 | receiveInterest(Interest(queryIdName)); // face1 expected |
| 199 | receiveInterest(Interest(queryScopeName)); // face1 and face3 expected |
| 200 | receiveInterest(Interest(invalidQueryName)); // nack expected |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 201 | |
| 202 | BOOST_REQUIRE_EQUAL(m_responses.size(), 4); |
| 203 | |
| 204 | Block content; |
| 205 | ndn::nfd::FaceStatus status; |
| 206 | |
| 207 | content = m_responses[0].getContent(); |
Davide Pesavento | 6837d4f | 2017-07-15 13:01:03 -0400 | [diff] [blame] | 208 | content.parse(); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 209 | BOOST_CHECK_EQUAL(content.elements().size(), 2); // face1 and face2 |
Davide Pesavento | 6837d4f | 2017-07-15 13:01:03 -0400 | [diff] [blame] | 210 | status.wireDecode(content.elements()[0]); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 211 | BOOST_CHECK_EQUAL(face1->getId(), status.getFaceId()); |
Davide Pesavento | 6837d4f | 2017-07-15 13:01:03 -0400 | [diff] [blame] | 212 | status.wireDecode(content.elements()[1]); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 213 | BOOST_CHECK_EQUAL(face2->getId(), status.getFaceId()); |
| 214 | |
| 215 | content = m_responses[1].getContent(); |
Davide Pesavento | 6837d4f | 2017-07-15 13:01:03 -0400 | [diff] [blame] | 216 | content.parse(); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 217 | BOOST_CHECK_EQUAL(content.elements().size(), 1); // face1 |
Davide Pesavento | 6837d4f | 2017-07-15 13:01:03 -0400 | [diff] [blame] | 218 | status.wireDecode(content.elements()[0]); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 219 | BOOST_CHECK_EQUAL(face1->getId(), status.getFaceId()); |
| 220 | |
| 221 | content = m_responses[2].getContent(); |
Davide Pesavento | 6837d4f | 2017-07-15 13:01:03 -0400 | [diff] [blame] | 222 | content.parse(); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 223 | BOOST_CHECK_EQUAL(content.elements().size(), 2); // face1 and face3 |
Davide Pesavento | 6837d4f | 2017-07-15 13:01:03 -0400 | [diff] [blame] | 224 | status.wireDecode(content.elements()[0]); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 225 | BOOST_CHECK_EQUAL(face1->getId(), status.getFaceId()); |
Davide Pesavento | 6837d4f | 2017-07-15 13:01:03 -0400 | [diff] [blame] | 226 | status.wireDecode(content.elements()[1]); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 227 | BOOST_CHECK_EQUAL(face3->getId(), status.getFaceId()); |
| 228 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 229 | ControlResponse expectedResponse(400, "Malformed filter"); // nack, 400, malformed filter |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 230 | BOOST_CHECK_EQUAL(checkResponse(3, invalidQueryName, expectedResponse, tlv::ContentType_Nack), |
| 231 | CheckResponseResult::OK); |
| 232 | } |
| 233 | |
Davide Pesavento | 8fd15e6 | 2017-04-06 19:58:54 -0400 | [diff] [blame] | 234 | class TestChannel : public face::Channel |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 235 | { |
| 236 | public: |
Davide Pesavento | 97210d5 | 2016-10-14 15:45:48 +0200 | [diff] [blame] | 237 | explicit |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 238 | TestChannel(const std::string& uri) |
| 239 | { |
| 240 | setUri(FaceUri(uri)); |
| 241 | } |
Davide Pesavento | c19408d | 2017-04-08 00:42:55 -0400 | [diff] [blame] | 242 | |
| 243 | bool |
| 244 | isListening() const final |
| 245 | { |
| 246 | return false; |
| 247 | } |
| 248 | |
| 249 | size_t |
| 250 | size() const final |
| 251 | { |
| 252 | return 0; |
| 253 | } |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 254 | }; |
| 255 | |
Davide Pesavento | e5eebad | 2017-04-06 20:23:26 -0400 | [diff] [blame] | 256 | class TestProtocolFactory : public face::ProtocolFactory |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 257 | { |
| 258 | public: |
Junxiao Shi | 0ba6d64 | 2017-07-17 00:53:22 +0000 | [diff] [blame] | 259 | TestProtocolFactory(const CtorParams& params) |
| 260 | : ProtocolFactory(params) |
| 261 | { |
| 262 | } |
| 263 | |
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 | 944f38b | 2017-07-20 20:54:22 -0400 | [diff] [blame] | 271 | createFace(const CreateFaceParams& params, |
Davide Pesavento | 8fd15e6 | 2017-04-06 19:58:54 -0400 | [diff] [blame] | 272 | const face::FaceCreatedCallback& onCreated, |
| 273 | const face::FaceCreationFailedCallback& onConnectFailed) final |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 274 | { |
| 275 | } |
| 276 | |
Davide Pesavento | 8fd15e6 | 2017-04-06 19:58:54 -0400 | [diff] [blame] | 277 | std::vector<shared_ptr<const face::Channel>> |
Davide Pesavento | 97210d5 | 2016-10-14 15:45:48 +0200 | [diff] [blame] | 278 | getChannels() const final |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 279 | { |
| 280 | return m_channels; |
| 281 | } |
| 282 | |
| 283 | public: |
| 284 | shared_ptr<TestChannel> |
| 285 | addChannel(const std::string& channelUri) |
| 286 | { |
| 287 | auto channel = make_shared<TestChannel>(channelUri); |
| 288 | m_channels.push_back(channel); |
| 289 | return channel; |
| 290 | } |
| 291 | |
| 292 | private: |
Davide Pesavento | 8fd15e6 | 2017-04-06 19:58:54 -0400 | [diff] [blame] | 293 | std::vector<shared_ptr<const face::Channel>> m_channels; |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 294 | }; |
| 295 | |
| 296 | BOOST_AUTO_TEST_CASE(ChannelDataset) |
| 297 | { |
Junxiao Shi | 0ba6d64 | 2017-07-17 00:53:22 +0000 | [diff] [blame] | 298 | m_manager.m_faceSystem.m_factories["test"] = |
| 299 | make_unique<TestProtocolFactory>(m_faceSystem.makePFCtorParams()); |
Junxiao Shi | b47247d | 2017-01-24 15:09:16 +0000 | [diff] [blame] | 300 | auto factory = static_cast<TestProtocolFactory*>(m_manager.m_faceSystem.getFactoryById("test")); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 301 | |
Davide Pesavento | 6837d4f | 2017-07-15 13:01:03 -0400 | [diff] [blame] | 302 | const size_t nEntries = 404; |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 303 | std::map<std::string, shared_ptr<TestChannel>> addedChannels; |
Eric Newberry | 1b4ba05 | 2016-10-07 23:04:07 -0700 | [diff] [blame] | 304 | for (size_t i = 0; i < nEntries; i++) { |
Davide Pesavento | 6837d4f | 2017-07-15 13:01:03 -0400 | [diff] [blame] | 305 | auto channel = factory->addChannel("test" + to_string(i) + "://"); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 306 | addedChannels[channel->getUri().toString()] = channel; |
| 307 | } |
| 308 | |
Junxiao Shi | 8a1f170 | 2017-07-03 00:05:08 +0000 | [diff] [blame] | 309 | receiveInterest(Interest("/localhost/nfd/faces/channels")); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 310 | |
Davide Pesavento | 6837d4f | 2017-07-15 13:01:03 -0400 | [diff] [blame] | 311 | Block content = concatenateResponses(); |
| 312 | content.parse(); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 313 | BOOST_REQUIRE_EQUAL(content.elements().size(), nEntries); |
| 314 | |
| 315 | for (size_t idx = 0; idx < nEntries; ++idx) { |
Davide Pesavento | 6837d4f | 2017-07-15 13:01:03 -0400 | [diff] [blame] | 316 | ndn::nfd::ChannelStatus decodedStatus(content.elements()[idx]); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 317 | BOOST_CHECK(addedChannels.find(decodedStatus.getLocalUri()) != addedChannels.end()); |
| 318 | } |
| 319 | } |
| 320 | |
Davide Pesavento | 97210d5 | 2016-10-14 15:45:48 +0200 | [diff] [blame] | 321 | BOOST_AUTO_TEST_SUITE_END() // Datasets |
| 322 | |
| 323 | BOOST_AUTO_TEST_SUITE(Notifications) |
| 324 | |
Eric Newberry | 1b4ba05 | 2016-10-07 23:04:07 -0700 | [diff] [blame] | 325 | BOOST_AUTO_TEST_CASE(FaceEventCreated) |
Davide Pesavento | 97210d5 | 2016-10-14 15:45:48 +0200 | [diff] [blame] | 326 | { |
Eric Newberry | 1b4ba05 | 2016-10-07 23:04:07 -0700 | [diff] [blame] | 327 | auto face = addFace(); // trigger FACE_EVENT_CREATED notification |
| 328 | BOOST_CHECK_NE(face->getId(), face::INVALID_FACEID); |
| 329 | FaceId faceId = face->getId(); |
| 330 | |
| 331 | BOOST_CHECK_EQUAL(m_manager.m_faceStateChangeConn.count(faceId), 1); |
Davide Pesavento | 97210d5 | 2016-10-14 15:45:48 +0200 | [diff] [blame] | 332 | |
| 333 | // check notification |
Eric Newberry | 1b4ba05 | 2016-10-07 23:04:07 -0700 | [diff] [blame] | 334 | BOOST_REQUIRE_EQUAL(m_responses.size(), 1); |
Davide Pesavento | 6837d4f | 2017-07-15 13:01:03 -0400 | [diff] [blame] | 335 | Block payload = m_responses.back().getContent().blockFromValue(); |
Eric Newberry | 1b4ba05 | 2016-10-07 23:04:07 -0700 | [diff] [blame] | 336 | BOOST_CHECK_EQUAL(payload.type(), ndn::tlv::nfd::FaceEventNotification); |
Davide Pesavento | 6837d4f | 2017-07-15 13:01:03 -0400 | [diff] [blame] | 337 | ndn::nfd::FaceEventNotification notification(payload); |
Eric Newberry | 1b4ba05 | 2016-10-07 23:04:07 -0700 | [diff] [blame] | 338 | BOOST_CHECK_EQUAL(notification.getKind(), ndn::nfd::FACE_EVENT_CREATED); |
| 339 | BOOST_CHECK_EQUAL(notification.getFaceId(), faceId); |
| 340 | BOOST_CHECK_EQUAL(notification.getRemoteUri(), face->getRemoteUri().toString()); |
| 341 | BOOST_CHECK_EQUAL(notification.getLocalUri(), face->getLocalUri().toString()); |
| 342 | BOOST_CHECK_EQUAL(notification.getFaceScope(), ndn::nfd::FACE_SCOPE_NON_LOCAL); |
| 343 | BOOST_CHECK_EQUAL(notification.getFacePersistency(), ndn::nfd::FACE_PERSISTENCY_PERSISTENT); |
| 344 | 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] | 345 | BOOST_CHECK_EQUAL(notification.getFlags(), 0); |
Eric Newberry | 1b4ba05 | 2016-10-07 23:04:07 -0700 | [diff] [blame] | 346 | } |
Davide Pesavento | 97210d5 | 2016-10-14 15:45:48 +0200 | [diff] [blame] | 347 | |
Eric Newberry | 1b4ba05 | 2016-10-07 23:04:07 -0700 | [diff] [blame] | 348 | BOOST_AUTO_TEST_CASE(FaceEventDownUp) |
| 349 | { |
| 350 | auto face = addFace(); |
| 351 | BOOST_CHECK_NE(face->getId(), face::INVALID_FACEID); |
| 352 | FaceId faceId = face->getId(); |
| 353 | |
| 354 | // trigger FACE_EVENT_DOWN notification |
| 355 | dynamic_cast<face::tests::DummyTransport*>(face->getTransport())->setState(face::FaceState::DOWN); |
Davide Pesavento | 97210d5 | 2016-10-14 15:45:48 +0200 | [diff] [blame] | 356 | advanceClocks(time::milliseconds(1), 10); |
Eric Newberry | 1b4ba05 | 2016-10-07 23:04:07 -0700 | [diff] [blame] | 357 | BOOST_CHECK_EQUAL(face->getState(), face::FaceState::DOWN); |
Davide Pesavento | 97210d5 | 2016-10-14 15:45:48 +0200 | [diff] [blame] | 358 | |
| 359 | // check notification |
| 360 | { |
Davide Pesavento | 97210d5 | 2016-10-14 15:45:48 +0200 | [diff] [blame] | 361 | BOOST_REQUIRE_EQUAL(m_responses.size(), 2); |
Davide Pesavento | 6837d4f | 2017-07-15 13:01:03 -0400 | [diff] [blame] | 362 | Block payload = m_responses.back().getContent().blockFromValue(); |
Davide Pesavento | 97210d5 | 2016-10-14 15:45:48 +0200 | [diff] [blame] | 363 | BOOST_CHECK_EQUAL(payload.type(), ndn::tlv::nfd::FaceEventNotification); |
Davide Pesavento | 6837d4f | 2017-07-15 13:01:03 -0400 | [diff] [blame] | 364 | ndn::nfd::FaceEventNotification notification(payload); |
Eric Newberry | 1b4ba05 | 2016-10-07 23:04:07 -0700 | [diff] [blame] | 365 | BOOST_CHECK_EQUAL(notification.getKind(), ndn::nfd::FACE_EVENT_DOWN); |
Davide Pesavento | 97210d5 | 2016-10-14 15:45:48 +0200 | [diff] [blame] | 366 | BOOST_CHECK_EQUAL(notification.getFaceId(), faceId); |
Eric Newberry | 1b4ba05 | 2016-10-07 23:04:07 -0700 | [diff] [blame] | 367 | BOOST_CHECK_EQUAL(notification.getRemoteUri(), face->getRemoteUri().toString()); |
| 368 | BOOST_CHECK_EQUAL(notification.getLocalUri(), face->getLocalUri().toString()); |
Davide Pesavento | 97210d5 | 2016-10-14 15:45:48 +0200 | [diff] [blame] | 369 | BOOST_CHECK_EQUAL(notification.getFaceScope(), ndn::nfd::FACE_SCOPE_NON_LOCAL); |
| 370 | BOOST_CHECK_EQUAL(notification.getFacePersistency(), ndn::nfd::FACE_PERSISTENCY_PERSISTENT); |
| 371 | 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] | 372 | BOOST_CHECK_EQUAL(notification.getFlags(), 0); |
Davide Pesavento | 97210d5 | 2016-10-14 15:45:48 +0200 | [diff] [blame] | 373 | } |
Eric Newberry | 1b4ba05 | 2016-10-07 23:04:07 -0700 | [diff] [blame] | 374 | |
| 375 | // trigger FACE_EVENT_UP notification |
| 376 | dynamic_cast<face::tests::DummyTransport*>(face->getTransport())->setState(face::FaceState::UP); |
| 377 | advanceClocks(time::milliseconds(1), 10); |
| 378 | BOOST_CHECK_EQUAL(face->getState(), face::FaceState::UP); |
| 379 | |
| 380 | // check notification |
| 381 | { |
Eric Newberry | 1b4ba05 | 2016-10-07 23:04:07 -0700 | [diff] [blame] | 382 | BOOST_REQUIRE_EQUAL(m_responses.size(), 3); |
Davide Pesavento | 6837d4f | 2017-07-15 13:01:03 -0400 | [diff] [blame] | 383 | Block payload = m_responses.back().getContent().blockFromValue(); |
Eric Newberry | 1b4ba05 | 2016-10-07 23:04:07 -0700 | [diff] [blame] | 384 | BOOST_CHECK_EQUAL(payload.type(), ndn::tlv::nfd::FaceEventNotification); |
Davide Pesavento | 6837d4f | 2017-07-15 13:01:03 -0400 | [diff] [blame] | 385 | ndn::nfd::FaceEventNotification notification(payload); |
Eric Newberry | 1b4ba05 | 2016-10-07 23:04:07 -0700 | [diff] [blame] | 386 | BOOST_CHECK_EQUAL(notification.getKind(), ndn::nfd::FACE_EVENT_UP); |
| 387 | BOOST_CHECK_EQUAL(notification.getFaceId(), faceId); |
| 388 | BOOST_CHECK_EQUAL(notification.getRemoteUri(), face->getRemoteUri().toString()); |
| 389 | BOOST_CHECK_EQUAL(notification.getLocalUri(), face->getLocalUri().toString()); |
| 390 | BOOST_CHECK_EQUAL(notification.getFaceScope(), ndn::nfd::FACE_SCOPE_NON_LOCAL); |
| 391 | BOOST_CHECK_EQUAL(notification.getFacePersistency(), ndn::nfd::FACE_PERSISTENCY_PERSISTENT); |
| 392 | 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] | 393 | BOOST_CHECK_EQUAL(notification.getFlags(), 0); |
Eric Newberry | 1b4ba05 | 2016-10-07 23:04:07 -0700 | [diff] [blame] | 394 | } |
| 395 | } |
| 396 | |
| 397 | BOOST_AUTO_TEST_CASE(FaceEventDestroyed) |
| 398 | { |
| 399 | auto face = addFace(); |
| 400 | BOOST_CHECK_NE(face->getId(), face::INVALID_FACEID); |
| 401 | FaceId faceId = face->getId(); |
| 402 | |
| 403 | BOOST_CHECK_EQUAL(m_manager.m_faceStateChangeConn.count(faceId), 1); |
| 404 | |
| 405 | face->close(); // trigger FaceDestroy FACE_EVENT_DESTROYED |
| 406 | advanceClocks(time::milliseconds(1), 10); |
| 407 | |
| 408 | // check notification |
Eric Newberry | 1b4ba05 | 2016-10-07 23:04:07 -0700 | [diff] [blame] | 409 | BOOST_REQUIRE_EQUAL(m_responses.size(), 2); |
Davide Pesavento | 6837d4f | 2017-07-15 13:01:03 -0400 | [diff] [blame] | 410 | Block payload = m_responses.back().getContent().blockFromValue(); |
Eric Newberry | 1b4ba05 | 2016-10-07 23:04:07 -0700 | [diff] [blame] | 411 | BOOST_CHECK_EQUAL(payload.type(), ndn::tlv::nfd::FaceEventNotification); |
Davide Pesavento | 6837d4f | 2017-07-15 13:01:03 -0400 | [diff] [blame] | 412 | ndn::nfd::FaceEventNotification notification(payload); |
Eric Newberry | 1b4ba05 | 2016-10-07 23:04:07 -0700 | [diff] [blame] | 413 | BOOST_CHECK_EQUAL(notification.getKind(), ndn::nfd::FACE_EVENT_DESTROYED); |
| 414 | BOOST_CHECK_EQUAL(notification.getFaceId(), faceId); |
| 415 | BOOST_CHECK_EQUAL(notification.getRemoteUri(), face->getRemoteUri().toString()); |
| 416 | BOOST_CHECK_EQUAL(notification.getLocalUri(), face->getLocalUri().toString()); |
| 417 | BOOST_CHECK_EQUAL(notification.getFaceScope(), ndn::nfd::FACE_SCOPE_NON_LOCAL); |
| 418 | BOOST_CHECK_EQUAL(notification.getFacePersistency(), ndn::nfd::FACE_PERSISTENCY_PERSISTENT); |
| 419 | 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] | 420 | BOOST_CHECK_EQUAL(notification.getFlags(), 0); |
Eric Newberry | 1b4ba05 | 2016-10-07 23:04:07 -0700 | [diff] [blame] | 421 | |
| 422 | BOOST_CHECK_EQUAL(face->getId(), face::INVALID_FACEID); |
| 423 | BOOST_CHECK_EQUAL(m_manager.m_faceStateChangeConn.count(faceId), 0); |
Davide Pesavento | 97210d5 | 2016-10-14 15:45:48 +0200 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | BOOST_AUTO_TEST_SUITE_END() // Notifications |
| 427 | |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 428 | BOOST_AUTO_TEST_SUITE_END() // TestFaceManager |
| 429 | BOOST_AUTO_TEST_SUITE_END() // Mgmt |
| 430 | |
| 431 | } // namespace tests |
| 432 | } // namespace nfd |