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*> |
| 51 | listEtherMcastFaces() const |
| 52 | { |
| 53 | return this->listFacesByScheme("ether", ndn::nfd::LINK_TYPE_MULTI_ACCESS); |
| 54 | } |
| 55 | |
| 56 | size_t |
| 57 | countEtherMcastFaces() const |
| 58 | { |
| 59 | return this->listEtherMcastFaces().size(); |
| 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 |
| 76 | whitelist |
| 77 | { |
| 78 | * |
| 79 | } |
| 80 | blacklist |
| 81 | { |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | )CONFIG"; |
| 86 | |
Junxiao Shi | 1b65ca1 | 2017-01-21 23:04:41 +0000 | [diff] [blame] | 87 | parseConfig(CONFIG, true); |
| 88 | parseConfig(CONFIG, false); |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 89 | |
| 90 | BOOST_CHECK_EQUAL(this->countEtherMcastFaces(), netifs.size()); |
| 91 | } |
| 92 | |
| 93 | BOOST_AUTO_TEST_CASE(Omitted) |
| 94 | { |
| 95 | const std::string CONFIG = R"CONFIG( |
| 96 | face_system |
| 97 | { |
| 98 | } |
| 99 | )CONFIG"; |
| 100 | |
Junxiao Shi | 1b65ca1 | 2017-01-21 23:04:41 +0000 | [diff] [blame] | 101 | parseConfig(CONFIG, true); |
| 102 | parseConfig(CONFIG, false); |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 103 | |
| 104 | BOOST_CHECK_EQUAL(this->countEtherMcastFaces(), 0); |
| 105 | } |
| 106 | |
| 107 | BOOST_AUTO_TEST_CASE(Whitelist) |
| 108 | { |
| 109 | SKIP_IF_ETHERNET_NETIF_COUNT_LT(1); |
| 110 | |
| 111 | std::string CONFIG = R"CONFIG( |
| 112 | face_system |
| 113 | { |
| 114 | ether |
| 115 | { |
| 116 | whitelist |
| 117 | { |
| 118 | ifname %ifname |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | )CONFIG"; |
| 123 | boost::replace_first(CONFIG, "%ifname", netifs.front().name); |
| 124 | |
Junxiao Shi | 1b65ca1 | 2017-01-21 23:04:41 +0000 | [diff] [blame] | 125 | parseConfig(CONFIG, false); |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 126 | auto etherMcastFaces = this->listEtherMcastFaces(); |
| 127 | BOOST_REQUIRE_EQUAL(etherMcastFaces.size(), 1); |
| 128 | BOOST_CHECK_EQUAL(etherMcastFaces.front()->getLocalUri().getHost(), netifs.front().name); |
| 129 | } |
| 130 | |
| 131 | BOOST_AUTO_TEST_CASE(Blacklist) |
| 132 | { |
| 133 | SKIP_IF_ETHERNET_NETIF_COUNT_LT(1); |
| 134 | |
| 135 | std::string CONFIG = R"CONFIG( |
| 136 | face_system |
| 137 | { |
| 138 | ether |
| 139 | { |
| 140 | blacklist |
| 141 | { |
| 142 | ifname %ifname |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | )CONFIG"; |
| 147 | boost::replace_first(CONFIG, "%ifname", netifs.front().name); |
| 148 | |
Junxiao Shi | 1b65ca1 | 2017-01-21 23:04:41 +0000 | [diff] [blame] | 149 | parseConfig(CONFIG, false); |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 150 | auto etherMcastFaces = this->listEtherMcastFaces(); |
| 151 | BOOST_CHECK_EQUAL(etherMcastFaces.size(), netifs.size() - 1); |
| 152 | BOOST_CHECK_EQUAL(boost::count_if(etherMcastFaces, [=] (const Face* face) { |
| 153 | return face->getLocalUri().getHost() == netifs.front().name; |
| 154 | }), 0); |
| 155 | } |
| 156 | |
| 157 | BOOST_AUTO_TEST_CASE(EnableDisableMcast) |
| 158 | { |
| 159 | const std::string CONFIG_WITH_MCAST = R"CONFIG( |
| 160 | face_system |
| 161 | { |
| 162 | ether |
| 163 | { |
| 164 | mcast yes |
| 165 | } |
| 166 | } |
| 167 | )CONFIG"; |
| 168 | const std::string CONFIG_WITHOUT_MCAST = R"CONFIG( |
| 169 | face_system |
| 170 | { |
| 171 | ether |
| 172 | { |
| 173 | mcast no |
| 174 | } |
| 175 | } |
| 176 | )CONFIG"; |
| 177 | |
Junxiao Shi | 1b65ca1 | 2017-01-21 23:04:41 +0000 | [diff] [blame] | 178 | parseConfig(CONFIG_WITHOUT_MCAST, false); |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 179 | BOOST_CHECK_EQUAL(this->countEtherMcastFaces(), 0); |
| 180 | |
| 181 | SKIP_IF_ETHERNET_NETIF_COUNT_LT(1); |
| 182 | |
Junxiao Shi | 1b65ca1 | 2017-01-21 23:04:41 +0000 | [diff] [blame] | 183 | parseConfig(CONFIG_WITH_MCAST, false); |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 184 | g_io.poll(); |
| 185 | BOOST_CHECK_EQUAL(this->countEtherMcastFaces(), netifs.size()); |
| 186 | |
Junxiao Shi | 1b65ca1 | 2017-01-21 23:04:41 +0000 | [diff] [blame] | 187 | parseConfig(CONFIG_WITHOUT_MCAST, false); |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 188 | g_io.poll(); |
| 189 | BOOST_CHECK_EQUAL(this->countEtherMcastFaces(), 0); |
| 190 | } |
| 191 | |
| 192 | BOOST_AUTO_TEST_CASE(ChangeMcastGroup) |
| 193 | { |
| 194 | SKIP_IF_ETHERNET_NETIF_COUNT_LT(1); |
| 195 | |
| 196 | const std::string CONFIG1 = R"CONFIG( |
| 197 | face_system |
| 198 | { |
| 199 | ether |
| 200 | { |
| 201 | mcast_group 01:00:00:00:00:01 |
| 202 | } |
| 203 | } |
| 204 | )CONFIG"; |
| 205 | const std::string CONFIG2 = R"CONFIG( |
| 206 | face_system |
| 207 | { |
| 208 | ether |
| 209 | { |
| 210 | mcast_group 01:00:00:00:00:02 |
| 211 | } |
| 212 | } |
| 213 | )CONFIG"; |
| 214 | |
Junxiao Shi | 1b65ca1 | 2017-01-21 23:04:41 +0000 | [diff] [blame] | 215 | parseConfig(CONFIG1, false); |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 216 | auto etherMcastFaces = this->listEtherMcastFaces(); |
| 217 | BOOST_REQUIRE_EQUAL(etherMcastFaces.size(), netifs.size()); |
| 218 | BOOST_CHECK_EQUAL(etherMcastFaces.front()->getRemoteUri(), |
| 219 | FaceUri(ethernet::Address(0x01, 0x00, 0x00, 0x00, 0x00, 0x01))); |
| 220 | |
Junxiao Shi | 1b65ca1 | 2017-01-21 23:04:41 +0000 | [diff] [blame] | 221 | parseConfig(CONFIG2, false); |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 222 | g_io.poll(); |
| 223 | etherMcastFaces = this->listEtherMcastFaces(); |
| 224 | BOOST_REQUIRE_EQUAL(etherMcastFaces.size(), netifs.size()); |
| 225 | BOOST_CHECK_EQUAL(etherMcastFaces.front()->getRemoteUri(), |
| 226 | FaceUri(ethernet::Address(0x01, 0x00, 0x00, 0x00, 0x00, 0x02))); |
| 227 | } |
| 228 | |
| 229 | BOOST_AUTO_TEST_CASE(BadMcast) |
| 230 | { |
| 231 | const std::string CONFIG = R"CONFIG( |
| 232 | face_system |
| 233 | { |
| 234 | ether |
| 235 | { |
| 236 | mcast hello |
| 237 | } |
| 238 | } |
| 239 | )CONFIG"; |
| 240 | |
| 241 | BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error); |
| 242 | BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error); |
| 243 | } |
| 244 | |
| 245 | BOOST_AUTO_TEST_CASE(BadMcastGroup) |
| 246 | { |
| 247 | const std::string CONFIG = R"CONFIG( |
| 248 | face_system |
| 249 | { |
| 250 | ether |
| 251 | { |
| 252 | mcast yes |
| 253 | mcast_group 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(UnicastMcastGroup) |
| 263 | { |
| 264 | const std::string CONFIG = R"CONFIG( |
| 265 | face_system |
| 266 | { |
| 267 | ether |
| 268 | { |
| 269 | mcast yes |
| 270 | mcast_group 02:00:00:00:00:01 |
| 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(UnknownOption) |
| 280 | { |
| 281 | const std::string CONFIG = R"CONFIG( |
| 282 | face_system |
| 283 | { |
| 284 | ether |
| 285 | { |
| 286 | hello |
| 287 | } |
| 288 | } |
| 289 | )CONFIG"; |
| 290 | |
| 291 | BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error); |
| 292 | BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error); |
| 293 | } |
| 294 | |
| 295 | BOOST_AUTO_TEST_SUITE_END() // ProcessConfig |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 296 | |
| 297 | BOOST_AUTO_TEST_CASE(GetChannels) |
| 298 | { |
| 299 | EthernetFactory factory; |
| 300 | |
| 301 | auto channels = factory.getChannels(); |
| 302 | BOOST_CHECK_EQUAL(channels.empty(), true); |
| 303 | } |
| 304 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 305 | BOOST_AUTO_TEST_CASE(UnsupportedFaceCreate) |
| 306 | { |
| 307 | EthernetFactory factory; |
| 308 | |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 309 | createFace(factory, |
| 310 | FaceUri("ether://[08:00:27:01:01:01]"), |
| 311 | ndn::nfd::FACE_PERSISTENCY_PERMANENT, |
Eric Newberry | f40551a | 2016-09-05 15:41:16 -0700 | [diff] [blame] | 312 | false, |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 313 | {CreateFaceExpectedResult::FAILURE, 406, "Unsupported protocol"}); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 314 | |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 315 | createFace(factory, |
| 316 | FaceUri("ether://[08:00:27:01:01:01]"), |
| 317 | ndn::nfd::FACE_PERSISTENCY_ON_DEMAND, |
Eric Newberry | f40551a | 2016-09-05 15:41:16 -0700 | [diff] [blame] | 318 | false, |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 319 | {CreateFaceExpectedResult::FAILURE, 406, "Unsupported protocol"}); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 320 | |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 321 | createFace(factory, |
| 322 | FaceUri("ether://[08:00:27:01:01:01]"), |
| 323 | ndn::nfd::FACE_PERSISTENCY_PERSISTENT, |
Eric Newberry | f40551a | 2016-09-05 15:41:16 -0700 | [diff] [blame] | 324 | false, |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 325 | {CreateFaceExpectedResult::FAILURE, 406, "Unsupported protocol"}); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | BOOST_AUTO_TEST_SUITE_END() // TestEthernetFactory |
| 329 | BOOST_AUTO_TEST_SUITE_END() // Face |
| 330 | |
| 331 | } // namespace tests |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 332 | } // namespace face |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 333 | } // namespace nfd |