Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2013-2017 Regents of the University of California. |
| 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 6 | * |
| 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 20 | */ |
| 21 | |
| 22 | #include "net/network-monitor-stub.hpp" |
| 23 | |
| 24 | #include "boost-test.hpp" |
| 25 | |
| 26 | namespace ndn { |
| 27 | namespace net { |
| 28 | namespace tests { |
| 29 | |
| 30 | BOOST_AUTO_TEST_SUITE(Net) |
| 31 | BOOST_AUTO_TEST_SUITE(TestNetworkMonitorStub) |
| 32 | |
| 33 | BOOST_AUTO_TEST_CASE(Capabilities) |
| 34 | { |
| 35 | NetworkMonitorStub stub(NetworkMonitor::CAP_ENUM | NetworkMonitor::CAP_IF_ADD_REMOVE); |
| 36 | BOOST_CHECK_EQUAL(stub.getCapabilities(), |
| 37 | NetworkMonitor::CAP_ENUM | NetworkMonitor::CAP_IF_ADD_REMOVE); |
| 38 | } |
| 39 | |
| 40 | class StubFixture |
| 41 | { |
| 42 | public: |
| 43 | StubFixture() |
| 44 | : stub(~0) |
| 45 | { |
| 46 | stub.onEnumerationCompleted.connect([this] { signals.push_back("EnumerationCompleted"); }); |
| 47 | stub.onInterfaceAdded.connect([this] (shared_ptr<const NetworkInterface> netif) { |
| 48 | signals.push_back("InterfaceAdded " + netif->getName()); }); |
| 49 | stub.onInterfaceRemoved.connect([this] (shared_ptr<const NetworkInterface> netif) { |
| 50 | signals.push_back("InterfaceRemoved " + netif->getName()); }); |
| 51 | } |
| 52 | |
| 53 | public: |
| 54 | NetworkMonitorStub stub; |
| 55 | std::vector<std::string> signals; |
| 56 | }; |
| 57 | |
| 58 | BOOST_FIXTURE_TEST_CASE(AddInterface, StubFixture) |
| 59 | { |
| 60 | BOOST_CHECK_EQUAL(stub.listNetworkInterfaces().size(), 0); |
| 61 | BOOST_CHECK(stub.getNetworkInterface("eth1") == nullptr); |
| 62 | |
| 63 | shared_ptr<NetworkInterface> if1 = stub.makeNetworkInterface(); |
| 64 | if1->setIndex(13697); |
| 65 | if1->setName("eth1"); |
| 66 | stub.addInterface(if1); |
| 67 | if1.reset(); |
| 68 | BOOST_REQUIRE(stub.getNetworkInterface("eth1") != nullptr); |
| 69 | BOOST_CHECK_EQUAL(stub.getNetworkInterface("eth1")->getIndex(), 13697); |
| 70 | BOOST_CHECK_EQUAL(stub.listNetworkInterfaces().at(0)->getIndex(), 13697); |
| 71 | BOOST_REQUIRE_EQUAL(signals.size(), 1); |
| 72 | BOOST_CHECK_EQUAL(signals.back(), "InterfaceAdded eth1"); |
| 73 | |
| 74 | shared_ptr<NetworkInterface> if1b = stub.makeNetworkInterface(); |
| 75 | if1b->setIndex(3280); |
| 76 | if1b->setName("eth1"); |
| 77 | BOOST_CHECK_THROW(stub.addInterface(if1b), std::invalid_argument); |
| 78 | BOOST_CHECK(stub.getNetworkInterface("eth1") != nullptr); |
| 79 | BOOST_CHECK_EQUAL(stub.getNetworkInterface("eth1")->getIndex(), 13697); |
| 80 | BOOST_CHECK_EQUAL(stub.listNetworkInterfaces().at(0)->getIndex(), 13697); |
| 81 | BOOST_CHECK_EQUAL(signals.size(), 1); |
| 82 | |
| 83 | stub.emitEnumerationCompleted(); |
| 84 | BOOST_REQUIRE_EQUAL(signals.size(), 2); |
| 85 | BOOST_CHECK_EQUAL(signals.back(), "EnumerationCompleted"); |
| 86 | |
| 87 | shared_ptr<NetworkInterface> if2 = stub.makeNetworkInterface(); |
| 88 | if2->setIndex(19243); |
| 89 | if2->setName("eth2"); |
| 90 | stub.addInterface(if2); |
| 91 | if2.reset(); |
| 92 | BOOST_REQUIRE(stub.getNetworkInterface("eth2") != nullptr); |
| 93 | BOOST_CHECK_EQUAL(stub.getNetworkInterface("eth2")->getIndex(), 19243); |
| 94 | BOOST_CHECK_EQUAL(stub.listNetworkInterfaces().size(), 2); |
| 95 | BOOST_REQUIRE_EQUAL(signals.size(), 3); |
| 96 | BOOST_CHECK_EQUAL(signals.back(), "InterfaceAdded eth2"); |
| 97 | } |
| 98 | |
| 99 | BOOST_FIXTURE_TEST_CASE(RemoveInterface, StubFixture) |
| 100 | { |
| 101 | shared_ptr<NetworkInterface> if1 = stub.makeNetworkInterface(); |
| 102 | if1->setIndex(13697); |
| 103 | if1->setName("eth1"); |
| 104 | stub.addInterface(if1); |
| 105 | |
| 106 | stub.emitEnumerationCompleted(); |
| 107 | BOOST_REQUIRE_EQUAL(signals.size(), 2); |
| 108 | BOOST_CHECK_EQUAL(signals.back(), "EnumerationCompleted"); |
| 109 | |
| 110 | stub.removeInterface("eth1"); |
| 111 | BOOST_CHECK(stub.getNetworkInterface("eth1") == nullptr); |
| 112 | BOOST_CHECK_EQUAL(stub.listNetworkInterfaces().size(), 0); |
| 113 | BOOST_REQUIRE_EQUAL(signals.size(), 3); |
| 114 | BOOST_CHECK_EQUAL(signals.back(), "InterfaceRemoved eth1"); |
| 115 | |
| 116 | stub.removeInterface("eth2"); // non-existent |
| 117 | BOOST_CHECK_EQUAL(signals.size(), 3); |
| 118 | } |
| 119 | |
| 120 | BOOST_AUTO_TEST_SUITE_END() // TestNetworkMonitorStub |
| 121 | BOOST_AUTO_TEST_SUITE_END() // Net |
| 122 | |
| 123 | } // namespace tests |
| 124 | } // namespace net |
| 125 | } // namespace ndn |