Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Yanbiao Li | 58ba3f9 | 2017-02-15 14:27:18 +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 | |
Yanbiao Li | 58ba3f9 | 2017-02-15 14:27:18 +0000 | [diff] [blame] | 26 | #include "mgmt/face-manager.hpp" |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 27 | #include "face-manager-command-fixture.hpp" |
| 28 | #include "nfd-manager-common-fixture.hpp" |
Junxiao Shi | b84e674 | 2016-07-19 13:16:22 +0000 | [diff] [blame] | 29 | |
| 30 | #include <thread> |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 31 | |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 32 | namespace nfd { |
| 33 | namespace tests { |
| 34 | |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 35 | BOOST_AUTO_TEST_SUITE(Mgmt) |
| 36 | BOOST_AUTO_TEST_SUITE(TestFaceManager) |
| 37 | |
| 38 | BOOST_FIXTURE_TEST_SUITE(CreateFace, BaseFixture) |
| 39 | |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 40 | class TcpFaceOnDemand |
| 41 | { |
| 42 | public: |
| 43 | ControlParameters |
| 44 | getParameters() |
| 45 | { |
| 46 | return ControlParameters() |
| 47 | .setUri("tcp4://127.0.0.1:26363") |
| 48 | .setFacePersistency(ndn::nfd::FACE_PERSISTENCY_ON_DEMAND); |
| 49 | } |
| 50 | }; |
| 51 | |
| 52 | class TcpFacePersistent |
| 53 | { |
| 54 | public: |
| 55 | ControlParameters |
| 56 | getParameters() |
| 57 | { |
| 58 | return ControlParameters() |
| 59 | .setUri("tcp4://127.0.0.1:26363") |
| 60 | .setFacePersistency(ndn::nfd::FACE_PERSISTENCY_PERSISTENT); |
| 61 | } |
| 62 | }; |
| 63 | |
| 64 | class TcpFacePermanent |
| 65 | { |
| 66 | public: |
| 67 | ControlParameters |
| 68 | getParameters() |
| 69 | { |
| 70 | return ControlParameters() |
| 71 | .setUri("tcp4://127.0.0.1:26363") |
| 72 | .setFacePersistency(ndn::nfd::FACE_PERSISTENCY_PERMANENT); |
| 73 | } |
| 74 | }; |
| 75 | |
| 76 | class UdpFaceOnDemand |
| 77 | { |
| 78 | public: |
| 79 | ControlParameters |
| 80 | getParameters() |
| 81 | { |
| 82 | return ControlParameters() |
| 83 | .setUri("udp4://127.0.0.1:26363") |
| 84 | .setFacePersistency(ndn::nfd::FACE_PERSISTENCY_ON_DEMAND); |
| 85 | } |
| 86 | }; |
| 87 | |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 88 | class UdpFacePersistent |
| 89 | { |
| 90 | public: |
| 91 | ControlParameters |
| 92 | getParameters() |
| 93 | { |
| 94 | return ControlParameters() |
| 95 | .setUri("udp4://127.0.0.1:26363") |
| 96 | .setFacePersistency(ndn::nfd::FACE_PERSISTENCY_PERSISTENT); |
| 97 | } |
| 98 | }; |
| 99 | |
| 100 | class UdpFacePermanent |
| 101 | { |
| 102 | public: |
| 103 | ControlParameters |
| 104 | getParameters() |
| 105 | { |
| 106 | return ControlParameters() |
| 107 | .setUri("udp4://127.0.0.1:26363") |
| 108 | .setFacePersistency(ndn::nfd::FACE_PERSISTENCY_PERMANENT); |
| 109 | } |
| 110 | }; |
| 111 | |
Eric Newberry | b5aa7f5 | 2016-09-03 20:36:12 -0700 | [diff] [blame] | 112 | class UdpFaceConnectToSelf // face that will cause afterCreateFaceFailure to be invoked |
| 113 | // fails because remote endpoint is prohibited |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 114 | { |
| 115 | public: |
Eric Newberry | b5aa7f5 | 2016-09-03 20:36:12 -0700 | [diff] [blame] | 116 | ControlParameters |
| 117 | getParameters() |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 118 | { |
Eric Newberry | b5aa7f5 | 2016-09-03 20:36:12 -0700 | [diff] [blame] | 119 | return ControlParameters() |
| 120 | .setUri("udp4://0.0.0.0:16363"); // cannot connect to self |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 121 | } |
| 122 | }; |
| 123 | |
Eric Newberry | f40551a | 2016-09-05 15:41:16 -0700 | [diff] [blame] | 124 | class LocalTcpFaceLocalFieldsEnabled |
| 125 | { |
| 126 | public: |
| 127 | ControlParameters |
| 128 | getParameters() |
| 129 | { |
| 130 | return ControlParameters() |
| 131 | .setUri("tcp4://127.0.0.1:26363") |
| 132 | .setFacePersistency(ndn::nfd::FACE_PERSISTENCY_PERSISTENT) |
| 133 | .setFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED, true); |
| 134 | } |
| 135 | }; |
| 136 | |
| 137 | class LocalTcpFaceLocalFieldsDisabled |
| 138 | { |
| 139 | public: |
| 140 | ControlParameters |
| 141 | getParameters() |
| 142 | { |
| 143 | return ControlParameters() |
| 144 | .setUri("tcp4://127.0.0.1:26363") |
| 145 | .setFacePersistency(ndn::nfd::FACE_PERSISTENCY_PERSISTENT) |
| 146 | .setFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED, false); |
| 147 | } |
| 148 | }; |
| 149 | |
| 150 | class NonLocalUdpFaceLocalFieldsEnabled // won't work because non-local scope |
| 151 | { |
| 152 | public: |
| 153 | ControlParameters |
| 154 | getParameters() |
| 155 | { |
| 156 | return ControlParameters() |
| 157 | .setUri("udp4://127.0.0.1:26363") |
| 158 | .setFacePersistency(ndn::nfd::FACE_PERSISTENCY_PERSISTENT) |
| 159 | .setFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED, true); |
| 160 | } |
| 161 | }; |
| 162 | |
| 163 | class NonLocalUdpFaceLocalFieldsDisabled |
| 164 | { |
| 165 | public: |
| 166 | ControlParameters |
| 167 | getParameters() |
| 168 | { |
| 169 | return ControlParameters() |
| 170 | .setUri("udp4://127.0.0.1:26363") |
| 171 | .setFacePersistency(ndn::nfd::FACE_PERSISTENCY_PERSISTENT) |
| 172 | .setFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED, false); |
| 173 | } |
| 174 | }; |
| 175 | |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 176 | namespace mpl = boost::mpl; |
| 177 | |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 178 | // pairs of CreateCommand and Success/Failure status |
Eric Newberry | b5aa7f5 | 2016-09-03 20:36:12 -0700 | [diff] [blame] | 179 | typedef mpl::vector<mpl::pair<TcpFaceOnDemand, CommandFailure<406>>, |
| 180 | mpl::pair<TcpFacePersistent, CommandSuccess>, |
| 181 | mpl::pair<TcpFacePermanent, CommandFailure<406>>, |
| 182 | mpl::pair<UdpFaceOnDemand, CommandFailure<406>>, |
| 183 | mpl::pair<UdpFacePersistent, CommandSuccess>, |
| 184 | mpl::pair<UdpFacePermanent, CommandSuccess>, |
Eric Newberry | f40551a | 2016-09-05 15:41:16 -0700 | [diff] [blame] | 185 | mpl::pair<UdpFaceConnectToSelf, CommandFailure<406>>, |
| 186 | mpl::pair<LocalTcpFaceLocalFieldsEnabled, CommandSuccess>, |
| 187 | mpl::pair<LocalTcpFaceLocalFieldsDisabled, CommandSuccess>, |
| 188 | mpl::pair<NonLocalUdpFaceLocalFieldsEnabled, CommandFailure<406>>, |
| 189 | mpl::pair<NonLocalUdpFaceLocalFieldsDisabled, CommandSuccess>> Faces; |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 190 | |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 191 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(NewFace, T, Faces, FaceManagerCommandFixture) |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 192 | { |
| 193 | typedef typename T::first FaceType; |
| 194 | typedef typename T::second CreateResult; |
| 195 | |
| 196 | Name commandName("/localhost/nfd/faces"); |
| 197 | commandName.append("create"); |
| 198 | commandName.append(FaceType().getParameters().wireEncode()); |
Junxiao Shi | b84e674 | 2016-07-19 13:16:22 +0000 | [diff] [blame] | 199 | auto command = makeInterest(commandName); |
Junxiao Shi | 221b6fe | 2016-07-14 18:21:56 +0000 | [diff] [blame] | 200 | m_keyChain.sign(*command); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 201 | |
| 202 | bool hasCallbackFired = false; |
Junxiao Shi | 221b6fe | 2016-07-14 18:21:56 +0000 | [diff] [blame] | 203 | this->node1.face.onSendData.connect([this, command, &hasCallbackFired] (const Data& response) { |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 204 | if (!command->getName().isPrefixOf(response.getName())) { |
| 205 | return; |
| 206 | } |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 207 | |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 208 | ControlResponse actual(response.getContent().blockFromValue()); |
| 209 | ControlResponse expected(CreateResult().getExpected()); |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 210 | BOOST_TEST_MESSAGE(actual.getText()); |
Yanbiao Li | 58ba3f9 | 2017-02-15 14:27:18 +0000 | [diff] [blame] | 211 | BOOST_CHECK_EQUAL(expected.getCode(), actual.getCode()); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 212 | |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 213 | if (actual.getBody().hasWire()) { |
| 214 | ControlParameters expectedParams(FaceType().getParameters()); |
| 215 | ControlParameters actualParams(actual.getBody()); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 216 | |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 217 | BOOST_CHECK(actualParams.hasFaceId()); |
| 218 | BOOST_CHECK_EQUAL(expectedParams.getFacePersistency(), actualParams.getFacePersistency()); |
| 219 | |
Eric Newberry | b5aa7f5 | 2016-09-03 20:36:12 -0700 | [diff] [blame] | 220 | if (actual.getCode() == 200) { |
| 221 | if (expectedParams.hasFlags()) { |
Eric Newberry | f40551a | 2016-09-05 15:41:16 -0700 | [diff] [blame] | 222 | BOOST_CHECK_EQUAL(expectedParams.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED), |
| 223 | actualParams.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED)); |
Eric Newberry | b5aa7f5 | 2016-09-03 20:36:12 -0700 | [diff] [blame] | 224 | } |
| 225 | else { |
Eric Newberry | f40551a | 2016-09-05 15:41:16 -0700 | [diff] [blame] | 226 | // local fields are disabled by default |
| 227 | BOOST_CHECK_EQUAL(actualParams.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED), false); |
Eric Newberry | b5aa7f5 | 2016-09-03 20:36:12 -0700 | [diff] [blame] | 228 | } |
| 229 | } |
| 230 | else { |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 231 | BOOST_CHECK_EQUAL(expectedParams.getUri(), actualParams.getUri()); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 232 | } |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 233 | } |
Eric Newberry | b5aa7f5 | 2016-09-03 20:36:12 -0700 | [diff] [blame] | 234 | |
| 235 | if (actual.getCode() != 200) { |
| 236 | // ensure face not created |
| 237 | FaceUri uri(FaceType().getParameters().getUri()); |
| 238 | auto& faceTable = this->node1.manager.m_faceTable; |
| 239 | BOOST_CHECK(std::none_of(faceTable.begin(), faceTable.end(), [uri] (Face& face) { |
| 240 | return face.getRemoteUri() == uri; |
| 241 | })); |
| 242 | } |
| 243 | |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 244 | hasCallbackFired = true; |
| 245 | }); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 246 | |
Junxiao Shi | 221b6fe | 2016-07-14 18:21:56 +0000 | [diff] [blame] | 247 | this->node1.face.receive(*command); |
Junxiao Shi | b84e674 | 2016-07-19 13:16:22 +0000 | [diff] [blame] | 248 | this->advanceClocks(time::milliseconds(1), 5); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 249 | |
| 250 | BOOST_CHECK(hasCallbackFired); |
| 251 | } |
| 252 | |
Yanbiao Li | 58ba3f9 | 2017-02-15 14:27:18 +0000 | [diff] [blame] | 253 | BOOST_FIXTURE_TEST_CASE(ExistingFace, FaceManagerCommandFixture) |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 254 | { |
Yanbiao Li | 58ba3f9 | 2017-02-15 14:27:18 +0000 | [diff] [blame] | 255 | using FaceType = UdpFacePersistent; |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 256 | |
| 257 | { |
| 258 | // create face |
| 259 | |
| 260 | Name commandName("/localhost/nfd/faces"); |
| 261 | commandName.append("create"); |
Yanbiao Li | 58ba3f9 | 2017-02-15 14:27:18 +0000 | [diff] [blame] | 262 | commandName.append(FaceType().getParameters().wireEncode()); |
Junxiao Shi | b84e674 | 2016-07-19 13:16:22 +0000 | [diff] [blame] | 263 | auto command = makeInterest(commandName); |
Junxiao Shi | 221b6fe | 2016-07-14 18:21:56 +0000 | [diff] [blame] | 264 | m_keyChain.sign(*command); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 265 | |
Junxiao Shi | 221b6fe | 2016-07-14 18:21:56 +0000 | [diff] [blame] | 266 | this->node1.face.receive(*command); |
Junxiao Shi | b84e674 | 2016-07-19 13:16:22 +0000 | [diff] [blame] | 267 | this->advanceClocks(time::milliseconds(1), 5); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 268 | } |
| 269 | |
Yanbiao Li | 58ba3f9 | 2017-02-15 14:27:18 +0000 | [diff] [blame] | 270 | // find the created face |
| 271 | auto foundFace = this->node1.findFaceByUri(FaceType().getParameters().getUri()); |
| 272 | BOOST_REQUIRE(foundFace != nullptr); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 273 | |
| 274 | { |
Yanbiao Li | 58ba3f9 | 2017-02-15 14:27:18 +0000 | [diff] [blame] | 275 | // re-create face |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 276 | |
| 277 | Name commandName("/localhost/nfd/faces"); |
| 278 | commandName.append("create"); |
| 279 | commandName.append(FaceType().getParameters().wireEncode()); |
Junxiao Shi | b84e674 | 2016-07-19 13:16:22 +0000 | [diff] [blame] | 280 | auto command = makeInterest(commandName); |
Junxiao Shi | 221b6fe | 2016-07-14 18:21:56 +0000 | [diff] [blame] | 281 | m_keyChain.sign(*command); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 282 | |
| 283 | bool hasCallbackFired = false; |
Junxiao Shi | b84e674 | 2016-07-19 13:16:22 +0000 | [diff] [blame] | 284 | this->node1.face.onSendData.connect( |
| 285 | [this, command, &hasCallbackFired, foundFace] (const Data& response) { |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 286 | if (!command->getName().isPrefixOf(response.getName())) { |
| 287 | return; |
| 288 | } |
| 289 | |
| 290 | ControlResponse actual(response.getContent().blockFromValue()); |
Yanbiao Li | 58ba3f9 | 2017-02-15 14:27:18 +0000 | [diff] [blame] | 291 | BOOST_REQUIRE_EQUAL(actual.getCode(), 409); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 292 | |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 293 | ControlParameters actualParams(actual.getBody()); |
Yanbiao Li | 58ba3f9 | 2017-02-15 14:27:18 +0000 | [diff] [blame] | 294 | BOOST_CHECK_EQUAL(foundFace->getId(), actualParams.getFaceId()); |
| 295 | BOOST_CHECK_EQUAL(foundFace->getRemoteUri().toString(), actualParams.getUri()); |
| 296 | BOOST_CHECK_EQUAL(foundFace->getPersistency(), actualParams.getFacePersistency()); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 297 | |
| 298 | hasCallbackFired = true; |
| 299 | }); |
| 300 | |
Junxiao Shi | 221b6fe | 2016-07-14 18:21:56 +0000 | [diff] [blame] | 301 | this->node1.face.receive(*command); |
Junxiao Shi | b84e674 | 2016-07-19 13:16:22 +0000 | [diff] [blame] | 302 | this->advanceClocks(time::milliseconds(1), 5); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 303 | |
| 304 | BOOST_CHECK(hasCallbackFired); |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | BOOST_AUTO_TEST_SUITE_END() // CreateFace |
| 309 | BOOST_AUTO_TEST_SUITE_END() // TestFaceManager |
| 310 | BOOST_AUTO_TEST_SUITE_END() // Mgmt |
| 311 | |
Weiwei Liu | f5aee94 | 2016-03-19 07:00:42 +0000 | [diff] [blame] | 312 | } // namespace tests |
| 313 | } // namespace nfd |