Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 0d82d04 | 2017-07-07 06:15:27 +0000 | [diff] [blame] | 2 | /* |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2022, Regents of the University of California, |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [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 | |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 26 | #ifndef NFD_TESTS_DAEMON_FACE_ETHERNET_FIXTURE_HPP |
| 27 | #define NFD_TESTS_DAEMON_FACE_ETHERNET_FIXTURE_HPP |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 28 | |
Davide Pesavento | 6bd6d0b | 2017-03-25 15:16:40 -0400 | [diff] [blame] | 29 | #include "face/multicast-ethernet-transport.hpp" |
| 30 | #include "face/unicast-ethernet-transport.hpp" |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 31 | |
Davide Pesavento | 3dade00 | 2019-03-19 11:29:56 -0600 | [diff] [blame] | 32 | #include "tests/daemon/limited-io.hpp" |
Davide Pesavento | 22fba35 | 2017-10-17 15:53:51 -0400 | [diff] [blame] | 33 | #include "test-netif.hpp" |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 34 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 35 | namespace nfd::tests { |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 36 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 37 | using face::EthernetTransport; |
| 38 | using face::MulticastEthernetTransport; |
| 39 | using face::UnicastEthernetTransport; |
Davide Pesavento | 1816d4b | 2017-07-02 12:20:48 -0400 | [diff] [blame] | 40 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 41 | /** |
| 42 | * \brief Fixture providing a list of EthernetTransport-capable network interfaces. |
Junxiao Shi | 84d62cb | 2017-07-12 16:15:18 +0000 | [diff] [blame] | 43 | */ |
Davide Pesavento | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame] | 44 | class EthernetFixture : public virtual GlobalIoFixture |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 45 | { |
| 46 | protected: |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 47 | EthernetFixture() |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 48 | { |
Junxiao Shi | 84d62cb | 2017-07-12 16:15:18 +0000 | [diff] [blame] | 49 | for (const auto& netif : collectNetworkInterfaces()) { |
| 50 | if (!netif->isLoopback() && netif->isUp()) { |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 51 | try { |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 52 | MulticastEthernetTransport t(*netif, ethernet::getBroadcastAddress(), |
| 53 | ndn::nfd::LINK_TYPE_MULTI_ACCESS); |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 54 | netifs.push_back(netif); |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 55 | } |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 56 | catch (const EthernetTransport::Error&) { |
| 57 | // ignore |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 58 | } |
| 59 | } |
| 60 | } |
Junxiao Shi | fbdd5ce | 2018-11-26 15:10:51 +0000 | [diff] [blame] | 61 | if (!netifs.empty()) { |
Eric Newberry | feeb918 | 2020-03-29 00:28:41 -0700 | [diff] [blame] | 62 | defaultNetif = const_pointer_cast<ndn::net::NetworkInterface>(netifs.front()); |
Junxiao Shi | fbdd5ce | 2018-11-26 15:10:51 +0000 | [diff] [blame] | 63 | } |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 64 | } |
| 65 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 66 | /** \brief Returns the first running interface. |
Eric Newberry | feeb918 | 2020-03-29 00:28:41 -0700 | [diff] [blame] | 67 | */ |
| 68 | shared_ptr<ndn::net::NetworkInterface> |
| 69 | getRunningNetif() const |
| 70 | { |
| 71 | for (const auto& netif : netifs) { |
| 72 | if (netif->getState() == ndn::net::InterfaceState::RUNNING) { |
| 73 | return const_pointer_cast<ndn::net::NetworkInterface>(netif); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | return nullptr; |
| 78 | } |
| 79 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 80 | /** \brief Create a UnicastEthernetTransport. |
Junxiao Shi | 84d62cb | 2017-07-12 16:15:18 +0000 | [diff] [blame] | 81 | */ |
Davide Pesavento | 6bd6d0b | 2017-03-25 15:16:40 -0400 | [diff] [blame] | 82 | void |
Eric Newberry | feeb918 | 2020-03-29 00:28:41 -0700 | [diff] [blame] | 83 | initializeUnicast(shared_ptr<ndn::net::NetworkInterface> netif = nullptr, |
| 84 | ndn::nfd::FacePersistency persistency = ndn::nfd::FACE_PERSISTENCY_PERSISTENT, |
Davide Pesavento | 1816d4b | 2017-07-02 12:20:48 -0400 | [diff] [blame] | 85 | ethernet::Address remoteAddr = {0x00, 0x00, 0x5e, 0x00, 0x53, 0x5e}) |
Davide Pesavento | 6bd6d0b | 2017-03-25 15:16:40 -0400 | [diff] [blame] | 86 | { |
Eric Newberry | feeb918 | 2020-03-29 00:28:41 -0700 | [diff] [blame] | 87 | if (!netif) { |
| 88 | netif = defaultNetif; |
| 89 | } |
| 90 | |
Junxiao Shi | fbdd5ce | 2018-11-26 15:10:51 +0000 | [diff] [blame] | 91 | localEp = netif->getName(); |
Davide Pesavento | 6bd6d0b | 2017-03-25 15:16:40 -0400 | [diff] [blame] | 92 | remoteEp = remoteAddr; |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 93 | transport = make_unique<UnicastEthernetTransport>(*netif, remoteEp, persistency, 2_s); |
Davide Pesavento | 6bd6d0b | 2017-03-25 15:16:40 -0400 | [diff] [blame] | 94 | } |
| 95 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 96 | /** \brief Create a MulticastEthernetTransport. |
Junxiao Shi | 84d62cb | 2017-07-12 16:15:18 +0000 | [diff] [blame] | 97 | */ |
Davide Pesavento | 6bd6d0b | 2017-03-25 15:16:40 -0400 | [diff] [blame] | 98 | void |
Eric Newberry | feeb918 | 2020-03-29 00:28:41 -0700 | [diff] [blame] | 99 | initializeMulticast(shared_ptr<ndn::net::NetworkInterface> netif = nullptr, |
| 100 | ndn::nfd::LinkType linkType = ndn::nfd::LINK_TYPE_MULTI_ACCESS, |
Davide Pesavento | 1816d4b | 2017-07-02 12:20:48 -0400 | [diff] [blame] | 101 | ethernet::Address mcastGroup = {0x01, 0x00, 0x5e, 0x90, 0x10, 0x5e}) |
Davide Pesavento | 6bd6d0b | 2017-03-25 15:16:40 -0400 | [diff] [blame] | 102 | { |
Eric Newberry | feeb918 | 2020-03-29 00:28:41 -0700 | [diff] [blame] | 103 | if (!netif) { |
| 104 | netif = defaultNetif; |
| 105 | } |
| 106 | |
Junxiao Shi | fbdd5ce | 2018-11-26 15:10:51 +0000 | [diff] [blame] | 107 | localEp = netif->getName(); |
Davide Pesavento | 6bd6d0b | 2017-03-25 15:16:40 -0400 | [diff] [blame] | 108 | remoteEp = mcastGroup; |
Junxiao Shi | fbdd5ce | 2018-11-26 15:10:51 +0000 | [diff] [blame] | 109 | transport = make_unique<MulticastEthernetTransport>(*netif, remoteEp, linkType); |
Davide Pesavento | 6bd6d0b | 2017-03-25 15:16:40 -0400 | [diff] [blame] | 110 | } |
| 111 | |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 112 | protected: |
Davide Pesavento | 1816d4b | 2017-07-02 12:20:48 -0400 | [diff] [blame] | 113 | LimitedIo limitedIo; |
Davide Pesavento | 6bd6d0b | 2017-03-25 15:16:40 -0400 | [diff] [blame] | 114 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 115 | /** \brief EthernetTransport-capable network interfaces. |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 116 | */ |
Junxiao Shi | 84d62cb | 2017-07-12 16:15:18 +0000 | [diff] [blame] | 117 | std::vector<shared_ptr<const ndn::net::NetworkInterface>> netifs; |
| 118 | |
Eric Newberry | feeb918 | 2020-03-29 00:28:41 -0700 | [diff] [blame] | 119 | shared_ptr<ndn::net::NetworkInterface> defaultNetif; |
Junxiao Shi | 84d62cb | 2017-07-12 16:15:18 +0000 | [diff] [blame] | 120 | unique_ptr<EthernetTransport> transport; |
| 121 | std::string localEp; |
| 122 | ethernet::Address remoteEp; |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 123 | }; |
| 124 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 125 | } // namespace nfd::tests |
| 126 | |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 127 | #define SKIP_IF_ETHERNET_NETIF_COUNT_LT(n) \ |
| 128 | do { \ |
| 129 | if (this->netifs.size() < (n)) { \ |
| 130 | BOOST_WARN_MESSAGE(false, "skipping assertions that require " #n \ |
| 131 | " or more EthernetTransport-capable network interfaces"); \ |
| 132 | return; \ |
| 133 | } \ |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 134 | } while (false) |
| 135 | |
Eric Newberry | feeb918 | 2020-03-29 00:28:41 -0700 | [diff] [blame] | 136 | #define SKIP_IF_NO_RUNNING_ETHERNET_NETIF() \ |
| 137 | do { \ |
| 138 | if (!this->getRunningNetif()) { \ |
| 139 | BOOST_WARN_MESSAGE(false, "skipping assertions that require a running " \ |
| 140 | "EthernetTransport-capable network interface"); \ |
| 141 | return; \ |
| 142 | } \ |
| 143 | } while (false) |
| 144 | |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 145 | #endif // NFD_TESTS_DAEMON_FACE_ETHERNET_FIXTURE_HPP |