blob: 3b4a51613fb6f5475e2503468129fd9c183d7b9f [file] [log] [blame]
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Davide Pesavento2bf35a62017-04-02 00:41:06 -04003 * Copyright (c) 2013-2017 Regents of the University of California.
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -08004 *
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#define BOOST_TEST_MAIN 1
23#define BOOST_TEST_DYN_LINK 1
24#define BOOST_TEST_MODULE ndn-cxx Integrated Tests (Network Monitor)
25
26#include "util/network-monitor.hpp"
27
Davide Pesavento2bf35a62017-04-02 00:41:06 -040028#include "util/network-address.hpp"
29#include "util/network-interface.hpp"
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -080030#include "util/time.hpp"
31
Davide Pesavento537dc3a2016-02-18 19:35:26 +010032#include "boost-test.hpp"
33
34#include <boost/asio/io_service.hpp>
35#include <iostream>
36
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -080037namespace ndn {
38namespace util {
Davide Pesavento2bf35a62017-04-02 00:41:06 -040039namespace tests {
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -080040
Davide Pesavento2bf35a62017-04-02 00:41:06 -040041BOOST_AUTO_TEST_SUITE(Util)
42BOOST_AUTO_TEST_SUITE(TestNetworkMonitor)
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -080043
Davide Pesavento2bf35a62017-04-02 00:41:06 -040044static std::ostream&
45logEvent(const shared_ptr<NetworkInterface>& ni = nullptr, std::ostream& os = std::cout)
46{
47 os << '[' << time::toIsoString(time::system_clock::now()) << "] ";
48 if (ni != nullptr)
49 os << ni->getName() << ": ";
50 return os;
51}
52
53BOOST_AUTO_TEST_CASE(Signals)
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -080054{
55 boost::asio::io_service io;
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -080056 NetworkMonitor monitor(io);
57
58 monitor.onNetworkStateChanged.connect([] {
Davide Pesavento2bf35a62017-04-02 00:41:06 -040059 logEvent() << "onNetworkStateChanged" << std::endl;
60 });
61
62 monitor.onEnumerationCompleted.connect([&monitor] {
63 logEvent() << "onEnumerationCompleted" << std::endl;
64 for (const auto& ni : monitor.listNetworkInterfaces()) {
65 std::cout << *ni;
66 }
67 });
68
69 monitor.onInterfaceAdded.connect([] (const shared_ptr<NetworkInterface>& ni) {
70 logEvent(ni) << "onInterfaceAdded\n" << *ni;
71
72 ni->onAddressAdded.connect([ni] (const NetworkAddress& address) {
73 logEvent(ni) << "onAddressAdded " << address << std::endl;
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -080074 });
75
Davide Pesavento2bf35a62017-04-02 00:41:06 -040076 ni->onAddressRemoved.connect([ni] (const NetworkAddress& address) {
77 logEvent(ni) << "onAddressRemoved " << address << std::endl;
78 });
79
80 ni->onStateChanged.connect([ni] (InterfaceState oldState, InterfaceState newState) {
81 logEvent(ni) << "onStateChanged " << oldState << " -> " << newState << std::endl;
82 });
83
84 ni->onMtuChanged.connect([ni] (uint32_t oldMtu, uint32_t newMtu) {
85 logEvent(ni) << "onMtuChanged " << oldMtu << " -> " << newMtu << std::endl;
86 });
87 }); // monitor.onInterfaceAdded.connect
88
89 monitor.onInterfaceRemoved.connect([] (const shared_ptr<NetworkInterface>& ni) {
90 logEvent(ni) << "onInterfaceRemoved" << std::endl;
91 });
92
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -080093 io.run();
94}
95
Davide Pesavento2bf35a62017-04-02 00:41:06 -040096BOOST_AUTO_TEST_SUITE_END() // TestNetworkMonitor
97BOOST_AUTO_TEST_SUITE_END() // Util
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -080098
Davide Pesavento2bf35a62017-04-02 00:41:06 -040099} // namespace tests
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -0800100} // namespace util
101} // namespace ndn