Davide Pesavento | 248d6bd | 2014-03-09 10:24:08 +0100 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014 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 | * |
| 10 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 11 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 12 | * |
| 13 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 14 | * of the GNU General Public License as published by the Free Software Foundation, |
| 15 | * either version 3 of the License, or (at your option) any later version. |
| 16 | * |
| 17 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 18 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 19 | * PURPOSE. See the GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License along with |
| 22 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 23 | **/ |
Davide Pesavento | 248d6bd | 2014-03-09 10:24:08 +0100 | [diff] [blame] | 24 | |
| 25 | #include "core/network-interface.hpp" |
| 26 | #include "tests/test-common.hpp" |
| 27 | |
Davide Pesavento | 248d6bd | 2014-03-09 10:24:08 +0100 | [diff] [blame] | 28 | namespace nfd { |
| 29 | namespace tests { |
| 30 | |
| 31 | BOOST_FIXTURE_TEST_SUITE(CoreNetworkInterface, BaseFixture) |
| 32 | |
Alexander Afanasyev | 70aaf8a | 2014-12-13 00:44:22 -0800 | [diff] [blame] | 33 | BOOST_AUTO_TEST_CASE(ListRealNetworkInterfaces) |
Davide Pesavento | 248d6bd | 2014-03-09 10:24:08 +0100 | [diff] [blame] | 34 | { |
Davide Pesavento | b499a60 | 2014-11-18 22:36:56 +0100 | [diff] [blame] | 35 | std::vector<NetworkInterfaceInfo> netifs; |
Davide Pesavento | 248d6bd | 2014-03-09 10:24:08 +0100 | [diff] [blame] | 36 | BOOST_CHECK_NO_THROW(netifs = listNetworkInterfaces()); |
| 37 | |
Davide Pesavento | b499a60 | 2014-11-18 22:36:56 +0100 | [diff] [blame] | 38 | for (const auto& netif : netifs) { |
| 39 | BOOST_TEST_MESSAGE(netif.index << ": " << netif.name); |
| 40 | BOOST_TEST_MESSAGE("\tether " << netif.etherAddress); |
| 41 | for (const auto& address : netif.ipv4Addresses) |
Davide Pesavento | 248d6bd | 2014-03-09 10:24:08 +0100 | [diff] [blame] | 42 | BOOST_TEST_MESSAGE("\tinet " << address); |
Davide Pesavento | b499a60 | 2014-11-18 22:36:56 +0100 | [diff] [blame] | 43 | for (const auto& address : netif.ipv6Addresses) |
Davide Pesavento | 248d6bd | 2014-03-09 10:24:08 +0100 | [diff] [blame] | 44 | BOOST_TEST_MESSAGE("\tinet6 " << address); |
Davide Pesavento | b499a60 | 2014-11-18 22:36:56 +0100 | [diff] [blame] | 45 | BOOST_TEST_MESSAGE("\tloopback : " << netif.isLoopback()); |
| 46 | BOOST_TEST_MESSAGE("\tmulticast : " << netif.isMulticastCapable()); |
| 47 | BOOST_TEST_MESSAGE("\tup : " << netif.isUp()); |
Davide Pesavento | 248d6bd | 2014-03-09 10:24:08 +0100 | [diff] [blame] | 48 | } |
| 49 | } |
| 50 | |
Alexander Afanasyev | 70aaf8a | 2014-12-13 00:44:22 -0800 | [diff] [blame] | 51 | class FakeNetworkInterfaceFixture : public BaseFixture |
| 52 | { |
| 53 | public: |
| 54 | FakeNetworkInterfaceFixture() |
| 55 | { |
| 56 | using namespace boost::asio::ip; |
| 57 | |
| 58 | auto fakeInterfaces = make_shared<std::vector<NetworkInterfaceInfo>>(); |
| 59 | |
| 60 | fakeInterfaces->push_back( |
| 61 | NetworkInterfaceInfo {0, "lo0", |
| 62 | ethernet::Address(), |
| 63 | {address_v4::from_string("127.0.0.1")}, |
| 64 | {address_v6::from_string("fe80::1")}, |
| 65 | address_v4::from_string("127.255.255.255"), |
| 66 | IFF_LOOPBACK | IFF_UP}); |
| 67 | fakeInterfaces->push_back( |
| 68 | NetworkInterfaceInfo {1, "eth0", |
| 69 | ethernet::Address::fromString("3e:15:c2:8b:65:00"), |
| 70 | {address_v4::from_string("192.168.2.1")}, |
| 71 | {}, |
| 72 | address_v4::from_string("192.168.2.255"), |
| 73 | 0}); |
| 74 | fakeInterfaces->push_back( |
| 75 | NetworkInterfaceInfo {2, "eth1", |
| 76 | ethernet::Address::fromString("3e:15:c2:8b:65:00"), |
| 77 | {address_v4::from_string("198.51.100.1")}, |
| 78 | {address_v6::from_string("2001:db8::1")}, |
| 79 | address_v4::from_string("198.51.100.255"), |
| 80 | IFF_MULTICAST | IFF_BROADCAST | IFF_UP}); |
| 81 | |
| 82 | setDebugNetworkInterfaces(fakeInterfaces); |
| 83 | } |
| 84 | |
| 85 | ~FakeNetworkInterfaceFixture() |
| 86 | { |
| 87 | setDebugNetworkInterfaces(nullptr); |
| 88 | } |
| 89 | }; |
| 90 | |
| 91 | BOOST_FIXTURE_TEST_CASE(ListFakeNetworkInterfaces, FakeNetworkInterfaceFixture) |
| 92 | { |
| 93 | std::vector<NetworkInterfaceInfo> netifs; |
| 94 | BOOST_CHECK_NO_THROW(netifs = listNetworkInterfaces()); |
| 95 | |
| 96 | BOOST_REQUIRE_EQUAL(netifs.size(), 3); |
| 97 | |
| 98 | BOOST_CHECK_EQUAL(netifs[0].index, 0); |
| 99 | BOOST_CHECK_EQUAL(netifs[1].index, 1); |
| 100 | BOOST_CHECK_EQUAL(netifs[2].index, 2); |
| 101 | |
| 102 | BOOST_CHECK_EQUAL(netifs[0].name, "lo0"); |
| 103 | BOOST_CHECK_EQUAL(netifs[1].name, "eth0"); |
| 104 | BOOST_CHECK_EQUAL(netifs[2].name, "eth1"); |
| 105 | |
| 106 | // no real value of testing other parameters |
| 107 | } |
| 108 | |
Davide Pesavento | 248d6bd | 2014-03-09 10:24:08 +0100 | [diff] [blame] | 109 | BOOST_AUTO_TEST_SUITE_END() |
| 110 | |
| 111 | } // namespace tests |
| 112 | } // namespace nfd |