Davide Pesavento | 248d6bd | 2014-03-09 10:24:08 +0100 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Spyridon Mastorakis | d0381c0 | 2015-02-19 10:29:41 -0800 | [diff] [blame] | 3 | * Copyright (c) 2014-2015, Regents of the University of California, |
| 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. |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 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/>. |
Spyridon Mastorakis | d0381c0 | 2015-02-19 10:29:41 -0800 | [diff] [blame] | 24 | */ |
Davide Pesavento | 248d6bd | 2014-03-09 10:24:08 +0100 | [diff] [blame] | 25 | |
| 26 | #include "core/network-interface.hpp" |
| 27 | #include "tests/test-common.hpp" |
| 28 | |
Davide Pesavento | 248d6bd | 2014-03-09 10:24:08 +0100 | [diff] [blame] | 29 | namespace nfd { |
| 30 | namespace tests { |
| 31 | |
Spyridon Mastorakis | d0381c0 | 2015-02-19 10:29:41 -0800 | [diff] [blame] | 32 | BOOST_FIXTURE_TEST_SUITE(TestNetworkInterface, BaseFixture) |
Davide Pesavento | 248d6bd | 2014-03-09 10:24:08 +0100 | [diff] [blame] | 33 | |
Alexander Afanasyev | 70aaf8a | 2014-12-13 00:44:22 -0800 | [diff] [blame] | 34 | BOOST_AUTO_TEST_CASE(ListRealNetworkInterfaces) |
Davide Pesavento | 248d6bd | 2014-03-09 10:24:08 +0100 | [diff] [blame] | 35 | { |
Davide Pesavento | b499a60 | 2014-11-18 22:36:56 +0100 | [diff] [blame] | 36 | std::vector<NetworkInterfaceInfo> netifs; |
Davide Pesavento | 248d6bd | 2014-03-09 10:24:08 +0100 | [diff] [blame] | 37 | BOOST_CHECK_NO_THROW(netifs = listNetworkInterfaces()); |
| 38 | |
Davide Pesavento | b499a60 | 2014-11-18 22:36:56 +0100 | [diff] [blame] | 39 | for (const auto& netif : netifs) { |
| 40 | BOOST_TEST_MESSAGE(netif.index << ": " << netif.name); |
| 41 | BOOST_TEST_MESSAGE("\tether " << netif.etherAddress); |
| 42 | for (const auto& address : netif.ipv4Addresses) |
Davide Pesavento | 248d6bd | 2014-03-09 10:24:08 +0100 | [diff] [blame] | 43 | BOOST_TEST_MESSAGE("\tinet " << address); |
Davide Pesavento | b499a60 | 2014-11-18 22:36:56 +0100 | [diff] [blame] | 44 | for (const auto& address : netif.ipv6Addresses) |
Davide Pesavento | 248d6bd | 2014-03-09 10:24:08 +0100 | [diff] [blame] | 45 | BOOST_TEST_MESSAGE("\tinet6 " << address); |
Davide Pesavento | b499a60 | 2014-11-18 22:36:56 +0100 | [diff] [blame] | 46 | BOOST_TEST_MESSAGE("\tloopback : " << netif.isLoopback()); |
| 47 | BOOST_TEST_MESSAGE("\tmulticast : " << netif.isMulticastCapable()); |
| 48 | BOOST_TEST_MESSAGE("\tup : " << netif.isUp()); |
Davide Pesavento | 248d6bd | 2014-03-09 10:24:08 +0100 | [diff] [blame] | 49 | } |
| 50 | } |
| 51 | |
Alexander Afanasyev | 70aaf8a | 2014-12-13 00:44:22 -0800 | [diff] [blame] | 52 | class FakeNetworkInterfaceFixture : public BaseFixture |
| 53 | { |
| 54 | public: |
| 55 | FakeNetworkInterfaceFixture() |
| 56 | { |
| 57 | using namespace boost::asio::ip; |
| 58 | |
| 59 | auto fakeInterfaces = make_shared<std::vector<NetworkInterfaceInfo>>(); |
| 60 | |
| 61 | fakeInterfaces->push_back( |
| 62 | NetworkInterfaceInfo {0, "lo0", |
| 63 | ethernet::Address(), |
| 64 | {address_v4::from_string("127.0.0.1")}, |
| 65 | {address_v6::from_string("fe80::1")}, |
| 66 | address_v4::from_string("127.255.255.255"), |
| 67 | IFF_LOOPBACK | IFF_UP}); |
| 68 | fakeInterfaces->push_back( |
| 69 | NetworkInterfaceInfo {1, "eth0", |
| 70 | ethernet::Address::fromString("3e:15:c2:8b:65:00"), |
| 71 | {address_v4::from_string("192.168.2.1")}, |
| 72 | {}, |
| 73 | address_v4::from_string("192.168.2.255"), |
| 74 | 0}); |
| 75 | fakeInterfaces->push_back( |
| 76 | NetworkInterfaceInfo {2, "eth1", |
| 77 | ethernet::Address::fromString("3e:15:c2:8b:65:00"), |
| 78 | {address_v4::from_string("198.51.100.1")}, |
| 79 | {address_v6::from_string("2001:db8::1")}, |
| 80 | address_v4::from_string("198.51.100.255"), |
| 81 | IFF_MULTICAST | IFF_BROADCAST | IFF_UP}); |
| 82 | |
| 83 | setDebugNetworkInterfaces(fakeInterfaces); |
| 84 | } |
| 85 | |
| 86 | ~FakeNetworkInterfaceFixture() |
| 87 | { |
| 88 | setDebugNetworkInterfaces(nullptr); |
| 89 | } |
| 90 | }; |
| 91 | |
| 92 | BOOST_FIXTURE_TEST_CASE(ListFakeNetworkInterfaces, FakeNetworkInterfaceFixture) |
| 93 | { |
| 94 | std::vector<NetworkInterfaceInfo> netifs; |
| 95 | BOOST_CHECK_NO_THROW(netifs = listNetworkInterfaces()); |
| 96 | |
| 97 | BOOST_REQUIRE_EQUAL(netifs.size(), 3); |
| 98 | |
| 99 | BOOST_CHECK_EQUAL(netifs[0].index, 0); |
| 100 | BOOST_CHECK_EQUAL(netifs[1].index, 1); |
| 101 | BOOST_CHECK_EQUAL(netifs[2].index, 2); |
| 102 | |
| 103 | BOOST_CHECK_EQUAL(netifs[0].name, "lo0"); |
| 104 | BOOST_CHECK_EQUAL(netifs[1].name, "eth0"); |
| 105 | BOOST_CHECK_EQUAL(netifs[2].name, "eth1"); |
| 106 | |
| 107 | // no real value of testing other parameters |
| 108 | } |
| 109 | |
Davide Pesavento | 248d6bd | 2014-03-09 10:24:08 +0100 | [diff] [blame] | 110 | BOOST_AUTO_TEST_SUITE_END() |
| 111 | |
| 112 | } // namespace tests |
| 113 | } // namespace nfd |