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