Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2017, Regents of the University of California, |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -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 "face/ethernet-factory.hpp" |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 27 | #include "face/face.hpp" |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 28 | |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 29 | #include "factory-test-common.hpp" |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 30 | #include "ethernet-fixture.hpp" |
| 31 | #include "face-system-fixture.hpp" |
| 32 | #include <boost/algorithm/string/replace.hpp> |
| 33 | #include <boost/range/algorithm/count_if.hpp> |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 34 | |
| 35 | namespace nfd { |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 36 | namespace face { |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 37 | namespace tests { |
| 38 | |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 39 | using namespace nfd::tests; |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 40 | |
| 41 | BOOST_AUTO_TEST_SUITE(Face) |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 42 | BOOST_FIXTURE_TEST_SUITE(TestEthernetFactory, EthernetFixture) |
| 43 | |
| 44 | using face::Face; |
| 45 | |
| 46 | class EthernetConfigFixture : public EthernetFixture |
| 47 | , public FaceSystemFixture |
| 48 | { |
| 49 | public: |
| 50 | std::vector<const Face*> |
Teng Liang | bfea575 | 2017-03-29 04:51:10 +0000 | [diff] [blame] | 51 | listEtherMcastFaces(ndn::nfd::LinkType linkType = ndn::nfd::LINK_TYPE_MULTI_ACCESS) const |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 52 | { |
Teng Liang | bfea575 | 2017-03-29 04:51:10 +0000 | [diff] [blame] | 53 | return this->listFacesByScheme("ether", linkType); |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | size_t |
Teng Liang | bfea575 | 2017-03-29 04:51:10 +0000 | [diff] [blame] | 57 | countEtherMcastFaces(ndn::nfd::LinkType linkType = ndn::nfd::LINK_TYPE_MULTI_ACCESS) const |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 58 | { |
Teng Liang | bfea575 | 2017-03-29 04:51:10 +0000 | [diff] [blame] | 59 | return this->listEtherMcastFaces(linkType).size(); |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 60 | } |
| 61 | }; |
| 62 | |
| 63 | BOOST_FIXTURE_TEST_SUITE(ProcessConfig, EthernetConfigFixture) |
| 64 | |
| 65 | BOOST_AUTO_TEST_CASE(Normal) |
| 66 | { |
| 67 | SKIP_IF_ETHERNET_NETIF_COUNT_LT(1); |
| 68 | |
| 69 | const std::string CONFIG = R"CONFIG( |
| 70 | face_system |
| 71 | { |
| 72 | ether |
| 73 | { |
| 74 | mcast yes |
| 75 | mcast_group 01:00:5E:00:17:AA |
Teng Liang | bfea575 | 2017-03-29 04:51:10 +0000 | [diff] [blame] | 76 | mcast_ad_hoc no |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 77 | whitelist |
| 78 | { |
| 79 | * |
| 80 | } |
| 81 | blacklist |
| 82 | { |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | )CONFIG"; |
| 87 | |
Junxiao Shi | 1b65ca1 | 2017-01-21 23:04:41 +0000 | [diff] [blame] | 88 | parseConfig(CONFIG, true); |
| 89 | parseConfig(CONFIG, false); |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 90 | |
| 91 | BOOST_CHECK_EQUAL(this->countEtherMcastFaces(), netifs.size()); |
| 92 | } |
| 93 | |
Teng Liang | bfea575 | 2017-03-29 04:51:10 +0000 | [diff] [blame] | 94 | BOOST_AUTO_TEST_CASE(EnableDisableMcast) |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 95 | { |
Teng Liang | bfea575 | 2017-03-29 04:51:10 +0000 | [diff] [blame] | 96 | const std::string CONFIG_WITH_MCAST = R"CONFIG( |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 97 | face_system |
| 98 | { |
Teng Liang | bfea575 | 2017-03-29 04:51:10 +0000 | [diff] [blame] | 99 | ether |
| 100 | { |
| 101 | mcast yes |
| 102 | } |
| 103 | } |
| 104 | )CONFIG"; |
| 105 | const std::string CONFIG_WITHOUT_MCAST = R"CONFIG( |
| 106 | face_system |
| 107 | { |
| 108 | ether |
| 109 | { |
| 110 | mcast no |
| 111 | } |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 112 | } |
| 113 | )CONFIG"; |
| 114 | |
Teng Liang | bfea575 | 2017-03-29 04:51:10 +0000 | [diff] [blame] | 115 | parseConfig(CONFIG_WITHOUT_MCAST, false); |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 116 | BOOST_CHECK_EQUAL(this->countEtherMcastFaces(), 0); |
Teng Liang | bfea575 | 2017-03-29 04:51:10 +0000 | [diff] [blame] | 117 | |
| 118 | SKIP_IF_ETHERNET_NETIF_COUNT_LT(1); |
| 119 | |
| 120 | parseConfig(CONFIG_WITH_MCAST, false); |
| 121 | g_io.poll(); |
| 122 | BOOST_CHECK_EQUAL(this->countEtherMcastFaces(), netifs.size()); |
| 123 | |
| 124 | parseConfig(CONFIG_WITHOUT_MCAST, false); |
| 125 | g_io.poll(); |
| 126 | BOOST_CHECK_EQUAL(this->countEtherMcastFaces(), 0); |
| 127 | } |
| 128 | |
| 129 | BOOST_AUTO_TEST_CASE(McastAdHoc) |
| 130 | { |
| 131 | SKIP_IF_ETHERNET_NETIF_COUNT_LT(1); |
| 132 | |
| 133 | const std::string CONFIG = R"CONFIG( |
| 134 | face_system |
| 135 | { |
| 136 | ether |
| 137 | { |
| 138 | mcast_ad_hoc yes |
| 139 | } |
| 140 | } |
| 141 | )CONFIG"; |
| 142 | |
| 143 | parseConfig(CONFIG, false); |
| 144 | BOOST_CHECK_EQUAL(this->countEtherMcastFaces(ndn::nfd::LINK_TYPE_AD_HOC), netifs.size()); |
| 145 | } |
| 146 | |
| 147 | BOOST_AUTO_TEST_CASE(ChangeMcastGroup) |
| 148 | { |
| 149 | SKIP_IF_ETHERNET_NETIF_COUNT_LT(1); |
| 150 | |
| 151 | const std::string CONFIG1 = R"CONFIG( |
| 152 | face_system |
| 153 | { |
| 154 | ether |
| 155 | { |
Davide Pesavento | 43ff2a9 | 2017-05-18 19:50:57 -0400 | [diff] [blame] | 156 | mcast_group 01:00:5e:90:10:01 |
Teng Liang | bfea575 | 2017-03-29 04:51:10 +0000 | [diff] [blame] | 157 | } |
| 158 | } |
| 159 | )CONFIG"; |
| 160 | const std::string CONFIG2 = R"CONFIG( |
| 161 | face_system |
| 162 | { |
| 163 | ether |
| 164 | { |
Davide Pesavento | 43ff2a9 | 2017-05-18 19:50:57 -0400 | [diff] [blame] | 165 | mcast_group 01:00:5e:90:10:02 |
Teng Liang | bfea575 | 2017-03-29 04:51:10 +0000 | [diff] [blame] | 166 | } |
| 167 | } |
| 168 | )CONFIG"; |
| 169 | |
| 170 | parseConfig(CONFIG1, false); |
| 171 | auto etherMcastFaces = this->listEtherMcastFaces(); |
| 172 | BOOST_REQUIRE_EQUAL(etherMcastFaces.size(), netifs.size()); |
| 173 | BOOST_CHECK_EQUAL(etherMcastFaces.front()->getRemoteUri(), |
Davide Pesavento | 43ff2a9 | 2017-05-18 19:50:57 -0400 | [diff] [blame] | 174 | FaceUri(ethernet::Address{0x01, 0x00, 0x5e, 0x90, 0x10, 0x01})); |
Teng Liang | bfea575 | 2017-03-29 04:51:10 +0000 | [diff] [blame] | 175 | |
| 176 | parseConfig(CONFIG2, false); |
| 177 | g_io.poll(); |
| 178 | etherMcastFaces = this->listEtherMcastFaces(); |
| 179 | BOOST_REQUIRE_EQUAL(etherMcastFaces.size(), netifs.size()); |
| 180 | BOOST_CHECK_EQUAL(etherMcastFaces.front()->getRemoteUri(), |
Davide Pesavento | 43ff2a9 | 2017-05-18 19:50:57 -0400 | [diff] [blame] | 181 | FaceUri(ethernet::Address{0x01, 0x00, 0x5e, 0x90, 0x10, 0x02})); |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | BOOST_AUTO_TEST_CASE(Whitelist) |
| 185 | { |
| 186 | SKIP_IF_ETHERNET_NETIF_COUNT_LT(1); |
| 187 | |
| 188 | std::string CONFIG = R"CONFIG( |
| 189 | face_system |
| 190 | { |
| 191 | ether |
| 192 | { |
| 193 | whitelist |
| 194 | { |
| 195 | ifname %ifname |
| 196 | } |
| 197 | } |
| 198 | } |
| 199 | )CONFIG"; |
| 200 | boost::replace_first(CONFIG, "%ifname", netifs.front().name); |
| 201 | |
Junxiao Shi | 1b65ca1 | 2017-01-21 23:04:41 +0000 | [diff] [blame] | 202 | parseConfig(CONFIG, false); |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 203 | auto etherMcastFaces = this->listEtherMcastFaces(); |
| 204 | BOOST_REQUIRE_EQUAL(etherMcastFaces.size(), 1); |
| 205 | BOOST_CHECK_EQUAL(etherMcastFaces.front()->getLocalUri().getHost(), netifs.front().name); |
| 206 | } |
| 207 | |
| 208 | BOOST_AUTO_TEST_CASE(Blacklist) |
| 209 | { |
| 210 | SKIP_IF_ETHERNET_NETIF_COUNT_LT(1); |
| 211 | |
| 212 | std::string CONFIG = R"CONFIG( |
| 213 | face_system |
| 214 | { |
| 215 | ether |
| 216 | { |
| 217 | blacklist |
| 218 | { |
| 219 | ifname %ifname |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | )CONFIG"; |
| 224 | boost::replace_first(CONFIG, "%ifname", netifs.front().name); |
| 225 | |
Junxiao Shi | 1b65ca1 | 2017-01-21 23:04:41 +0000 | [diff] [blame] | 226 | parseConfig(CONFIG, false); |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 227 | auto etherMcastFaces = this->listEtherMcastFaces(); |
| 228 | BOOST_CHECK_EQUAL(etherMcastFaces.size(), netifs.size() - 1); |
| 229 | BOOST_CHECK_EQUAL(boost::count_if(etherMcastFaces, [=] (const Face* face) { |
| 230 | return face->getLocalUri().getHost() == netifs.front().name; |
| 231 | }), 0); |
| 232 | } |
| 233 | |
Teng Liang | bfea575 | 2017-03-29 04:51:10 +0000 | [diff] [blame] | 234 | BOOST_AUTO_TEST_CASE(Omitted) |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 235 | { |
Teng Liang | bfea575 | 2017-03-29 04:51:10 +0000 | [diff] [blame] | 236 | const std::string CONFIG = R"CONFIG( |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 237 | face_system |
| 238 | { |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 239 | } |
| 240 | )CONFIG"; |
| 241 | |
Teng Liang | bfea575 | 2017-03-29 04:51:10 +0000 | [diff] [blame] | 242 | parseConfig(CONFIG, true); |
| 243 | parseConfig(CONFIG, false); |
| 244 | |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 245 | BOOST_CHECK_EQUAL(this->countEtherMcastFaces(), 0); |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | BOOST_AUTO_TEST_CASE(BadMcast) |
| 249 | { |
| 250 | const std::string CONFIG = R"CONFIG( |
| 251 | face_system |
| 252 | { |
| 253 | ether |
| 254 | { |
| 255 | mcast hello |
| 256 | } |
| 257 | } |
| 258 | )CONFIG"; |
| 259 | |
| 260 | BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error); |
| 261 | BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error); |
| 262 | } |
| 263 | |
| 264 | BOOST_AUTO_TEST_CASE(BadMcastGroup) |
| 265 | { |
| 266 | const std::string CONFIG = R"CONFIG( |
| 267 | face_system |
| 268 | { |
| 269 | ether |
| 270 | { |
| 271 | mcast yes |
| 272 | mcast_group hello |
| 273 | } |
| 274 | } |
| 275 | )CONFIG"; |
| 276 | |
| 277 | BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error); |
| 278 | BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error); |
| 279 | } |
| 280 | |
| 281 | BOOST_AUTO_TEST_CASE(UnicastMcastGroup) |
| 282 | { |
| 283 | const std::string CONFIG = R"CONFIG( |
| 284 | face_system |
| 285 | { |
| 286 | ether |
| 287 | { |
| 288 | mcast yes |
Davide Pesavento | 43ff2a9 | 2017-05-18 19:50:57 -0400 | [diff] [blame] | 289 | mcast_group 00:00:5e:00:53:5e |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 290 | } |
| 291 | } |
| 292 | )CONFIG"; |
| 293 | |
| 294 | BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error); |
| 295 | BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error); |
| 296 | } |
| 297 | |
| 298 | BOOST_AUTO_TEST_CASE(UnknownOption) |
| 299 | { |
| 300 | const std::string CONFIG = R"CONFIG( |
| 301 | face_system |
| 302 | { |
| 303 | ether |
| 304 | { |
| 305 | hello |
| 306 | } |
| 307 | } |
| 308 | )CONFIG"; |
| 309 | |
| 310 | BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error); |
| 311 | BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error); |
| 312 | } |
| 313 | |
| 314 | BOOST_AUTO_TEST_SUITE_END() // ProcessConfig |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 315 | |
| 316 | BOOST_AUTO_TEST_CASE(GetChannels) |
| 317 | { |
| 318 | EthernetFactory factory; |
| 319 | |
| 320 | auto channels = factory.getChannels(); |
| 321 | BOOST_CHECK_EQUAL(channels.empty(), true); |
| 322 | } |
| 323 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 324 | BOOST_AUTO_TEST_CASE(UnsupportedFaceCreate) |
| 325 | { |
| 326 | EthernetFactory factory; |
| 327 | |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 328 | createFace(factory, |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 329 | FaceUri("ether://[00:00:5e:00:53:5e]"), |
Eric Newberry | 78e32b0 | 2017-04-01 14:34:44 +0000 | [diff] [blame] | 330 | {}, |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 331 | ndn::nfd::FACE_PERSISTENCY_PERSISTENT, |
Eric Newberry | f40551a | 2016-09-05 15:41:16 -0700 | [diff] [blame] | 332 | false, |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 333 | {CreateFaceExpectedResult::FAILURE, 406, |
| 334 | "Creation of unicast Ethernet faces requires a LocalUri with dev:// scheme"}); |
| 335 | |
| 336 | createFace(factory, |
| 337 | FaceUri("ether://[00:00:5e:00:53:5e]"), |
| 338 | FaceUri("udp4://127.0.0.1:20071"), |
| 339 | ndn::nfd::FACE_PERSISTENCY_PERSISTENT, |
| 340 | false, |
| 341 | {CreateFaceExpectedResult::FAILURE, 406, |
| 342 | "Creation of unicast Ethernet faces requires a LocalUri with dev:// scheme"}); |
| 343 | |
| 344 | createFace(factory, |
| 345 | FaceUri("ether://[00:00:5e:00:53:5e]"), |
| 346 | FaceUri("dev://eth0"), |
| 347 | ndn::nfd::FACE_PERSISTENCY_ON_DEMAND, |
| 348 | false, |
| 349 | {CreateFaceExpectedResult::FAILURE, 406, |
| 350 | "Outgoing Ethernet faces do not support on-demand persistency"}); |
| 351 | |
| 352 | createFace(factory, |
| 353 | FaceUri("ether://[01:00:5e:90:10:5e]"), |
| 354 | FaceUri("dev://eth0"), |
| 355 | ndn::nfd::FACE_PERSISTENCY_PERSISTENT, |
| 356 | false, |
| 357 | {CreateFaceExpectedResult::FAILURE, 406, |
| 358 | "Cannot create multicast Ethernet faces"}); |
| 359 | |
| 360 | createFace(factory, |
| 361 | FaceUri("ether://[00:00:5e:00:53:5e]"), |
| 362 | FaceUri("dev://eth0"), |
| 363 | ndn::nfd::FACE_PERSISTENCY_PERSISTENT, |
| 364 | true, |
| 365 | {CreateFaceExpectedResult::FAILURE, 406, |
| 366 | "Local fields can only be enabled on faces with local scope"}); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | BOOST_AUTO_TEST_SUITE_END() // TestEthernetFactory |
| 370 | BOOST_AUTO_TEST_SUITE_END() // Face |
| 371 | |
| 372 | } // namespace tests |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 373 | } // namespace face |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 374 | } // namespace nfd |