util: NetworkMonitor: fine-grained signals on interface/address changes

Change-Id: I60d4cc6d673b920ba81d57502977f0340be0da48
Refs: #3353
diff --git a/tests/integrated/network-monitor.README.md b/tests/integrated/network-monitor.README.md
new file mode 100644
index 0000000..b99f26c
--- /dev/null
+++ b/tests/integrated/network-monitor.README.md
@@ -0,0 +1,42 @@
+## NetworkMonitor test
+
+These instructions are only for Linux.
+
+Run the network-monitor integrated test binary, e.g.:
+```
+./build/tests/integrated/network-monitor
+```
+Note: sudo is not required.
+
+You should see an `onInterfaceAdded` message for each ethernet and loopback
+network interface present on the system, followed by an `onAddressAdded`
+message for each IPv4/IPv6 address on each interface. Finally,
+`onEnumerationCompleted` is printed, along with a summary of all interfaces
+discovered thus far.
+
+[The following commands assume eth0 is the name of an ethernet interface
+on the machine. If your interfaces are named differently, replace eth0
+with the name of any ethernet interface that you have available.]
+
+Command | Expected output
+--------|----------------
+sudo ip link add link eth0 name nmtest0 type vlan id 42 | `nmtest0: onInterfaceAdded`
+sudo ip link set dev nmtest0 mtu 1342 | `nmtest0: onMtuChanged <old_mtu> -> 1342` (`old_mtu` is most likely 1500)
+sudo ip link set dev nmtest0 up | `nmtest0: onStateChanged down -> <new_state>` (`new_state` is one of: running, dormant, no-carrier)
+sudo ip address add 198.51.100.100/24 dev nmtest0 | `nmtest0: onAddressAdded 198.51.100.100/24`
+sudo ip address del 198.51.100.100/24 dev nmtest0 | `nmtest0: onAddressRemoved 198.51.100.100/24`
+sudo ip address add 2001:db8::1/80 dev nmtest0 | `nmtest0: onAddressAdded 2001:db8::1/80`
+sudo ip address del 2001:db8::1/80 dev nmtest0 | `nmtest0: onAddressRemoved 2001:db8::1/80`
+sudo ip link delete dev nmtest0 | `nmtest0: onInterfaceRemoved`
+
+If you unplug the ethernet cable from your network card, you should see:
+```
+eth0: onStateChanged running -> no-carrier
+nmtest0: onStateChanged running -> no-carrier
+```
+
+Plugging the cable back in should produce the following messages:
+```
+eth0: onStateChanged no-carrier -> running
+nmtest0: onStateChanged no-carrier -> running
+```
diff --git a/tests/integrated/network-monitor.cpp b/tests/integrated/network-monitor.cpp
index cbc0e8a..3b4a516 100644
--- a/tests/integrated/network-monitor.cpp
+++ b/tests/integrated/network-monitor.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2013-2016 Regents of the University of California.
+ * Copyright (c) 2013-2017 Regents of the University of California.
  *
  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
  *
@@ -25,6 +25,8 @@
 
 #include "util/network-monitor.hpp"
 
+#include "util/network-address.hpp"
+#include "util/network-interface.hpp"
 #include "util/time.hpp"
 
 #include "boost-test.hpp"
@@ -34,25 +36,66 @@
 
 namespace ndn {
 namespace util {
+namespace tests {
 
-BOOST_AUTO_TEST_SUITE(UtilNetworkMonitor)
+BOOST_AUTO_TEST_SUITE(Util)
+BOOST_AUTO_TEST_SUITE(TestNetworkMonitor)
 
-BOOST_AUTO_TEST_CASE(Basic)
+static std::ostream&
+logEvent(const shared_ptr<NetworkInterface>& ni = nullptr, std::ostream& os = std::cout)
+{
+  os << '[' << time::toIsoString(time::system_clock::now()) << "] ";
+  if (ni != nullptr)
+    os << ni->getName() << ": ";
+  return os;
+}
+
+BOOST_AUTO_TEST_CASE(Signals)
 {
   boost::asio::io_service io;
-  BOOST_REQUIRE_NO_THROW((NetworkMonitor(io)));
-
   NetworkMonitor monitor(io);
 
   monitor.onNetworkStateChanged.connect([] {
-      std::cout << time::toString(time::system_clock::now())
-                << "\tReceived network state change event" << std::endl;
+    logEvent() << "onNetworkStateChanged" << std::endl;
+  });
+
+  monitor.onEnumerationCompleted.connect([&monitor] {
+    logEvent() << "onEnumerationCompleted" << std::endl;
+    for (const auto& ni : monitor.listNetworkInterfaces()) {
+      std::cout << *ni;
+    }
+  });
+
+  monitor.onInterfaceAdded.connect([] (const shared_ptr<NetworkInterface>& ni) {
+    logEvent(ni) << "onInterfaceAdded\n" << *ni;
+
+    ni->onAddressAdded.connect([ni] (const NetworkAddress& address) {
+      logEvent(ni) << "onAddressAdded " << address << std::endl;
     });
 
+    ni->onAddressRemoved.connect([ni] (const NetworkAddress& address) {
+      logEvent(ni) << "onAddressRemoved " << address << std::endl;
+    });
+
+    ni->onStateChanged.connect([ni] (InterfaceState oldState, InterfaceState newState) {
+      logEvent(ni) << "onStateChanged " << oldState << " -> " << newState << std::endl;
+    });
+
+    ni->onMtuChanged.connect([ni] (uint32_t oldMtu, uint32_t newMtu) {
+      logEvent(ni) << "onMtuChanged " << oldMtu << " -> " << newMtu << std::endl;
+    });
+  }); // monitor.onInterfaceAdded.connect
+
+  monitor.onInterfaceRemoved.connect([] (const shared_ptr<NetworkInterface>& ni) {
+    logEvent(ni) << "onInterfaceRemoved" << std::endl;
+  });
+
   io.run();
 }
 
-BOOST_AUTO_TEST_SUITE_END()
+BOOST_AUTO_TEST_SUITE_END() // TestNetworkMonitor
+BOOST_AUTO_TEST_SUITE_END() // Util
 
+} // namespace tests
 } // namespace util
 } // namespace ndn
diff --git a/tests/unit-tests/util/network-monitor.t.cpp b/tests/unit-tests/util/network-monitor.t.cpp
new file mode 100644
index 0000000..bac8750
--- /dev/null
+++ b/tests/unit-tests/util/network-monitor.t.cpp
@@ -0,0 +1,67 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2013-2017 Regents of the University of California.
+ *
+ * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
+ *
+ * ndn-cxx library is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later version.
+ *
+ * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License and GNU Lesser
+ * General Public License along with ndn-cxx, e.g., in COPYING.md file.  If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
+ */
+
+#include "util/network-monitor.hpp"
+
+#include "boost-test.hpp"
+#include <boost/asio/io_service.hpp>
+
+namespace ndn {
+namespace util {
+namespace tests {
+
+BOOST_AUTO_TEST_SUITE(Util)
+BOOST_AUTO_TEST_SUITE(TestNetworkMonitor)
+
+BOOST_AUTO_TEST_CASE(DestructWithoutRun)
+{
+  boost::asio::io_service io;
+  auto nm = make_unique<NetworkMonitor>(io);
+  nm.reset();
+  BOOST_CHECK(true); // if we got this far, the test passed
+}
+
+BOOST_AUTO_TEST_CASE(DestructWhileEnumerating)
+{
+#ifdef __linux__
+  boost::asio::io_service io;
+  auto nm = make_unique<NetworkMonitor>(io);
+
+  nm->onInterfaceAdded.connect([&] (const shared_ptr<NetworkInterface>&) {
+    io.post([&] { nm.reset(); });
+  });
+  nm->onEnumerationCompleted.connect([&] {
+    BOOST_CHECK_EQUAL(nm->listNetworkInterfaces().size(), 0);
+    // make sure the test case terminates even if we have zero interfaces
+    io.post([&] { nm.reset(); });
+  });
+
+  io.run();
+#endif
+  BOOST_CHECK(true); // if we got this far, the test passed
+}
+
+BOOST_AUTO_TEST_SUITE_END() // TestNetworkMonitor
+BOOST_AUTO_TEST_SUITE_END() // Util
+
+} // namespace tests
+} // namespace util
+} // namespace ndn