blob: 50c04c7595e6ca63ae0a08c7364646b16d22979d [file] [log] [blame]
Davide Pesavento2bf35a62017-04-02 00:41:06 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento74daf742018-11-23 18:14:13 -05002/*
Davide Pesavento47ce2ee2023-05-09 01:33:33 -04003 * Copyright (c) 2013-2023 Regents of the University of California.
Davide Pesavento2bf35a62017-04-02 00:41:06 -04004 *
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
Davide Pesavento7e780642018-11-24 15:51:34 -050022#include "ndn-cxx/net/network-monitor.hpp"
Davide Pesavento2bf35a62017-04-02 00:41:06 -040023
Davide Pesavento7e780642018-11-24 15:51:34 -050024#include "tests/boost-test.hpp"
25
Davide Pesavento2f46d652023-11-09 23:40:01 -050026#include <boost/asio/io_context.hpp>
27#include <boost/asio/post.hpp>
Davide Pesavento2bf35a62017-04-02 00:41:06 -040028
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040029namespace ndn::tests {
30
31using namespace ndn::net;
Davide Pesavento2bf35a62017-04-02 00:41:06 -040032
Junxiao Shi25467942017-06-30 02:53:14 +000033BOOST_AUTO_TEST_SUITE(Net)
Davide Pesavento2bf35a62017-04-02 00:41:06 -040034BOOST_AUTO_TEST_SUITE(TestNetworkMonitor)
35
Junxiao Shif4c1eb32017-05-30 17:05:10 +000036#define NM_REQUIRE_CAP(capability) \
37 do { \
38 if ((nm->getCapabilities() & NetworkMonitor::CAP_ ## capability) == 0) { \
39 BOOST_WARN_MESSAGE(false, "skipping assertions that require " #capability " capability"); \
40 return; \
41 } \
42 } while (false)
43
Davide Pesavento2bf35a62017-04-02 00:41:06 -040044BOOST_AUTO_TEST_CASE(DestructWithoutRun)
45{
Davide Pesavento2f46d652023-11-09 23:40:01 -050046 boost::asio::io_context io;
Davide Pesavento2bf35a62017-04-02 00:41:06 -040047 auto nm = make_unique<NetworkMonitor>(io);
48 nm.reset();
49 BOOST_CHECK(true); // if we got this far, the test passed
50}
51
52BOOST_AUTO_TEST_CASE(DestructWhileEnumerating)
53{
Davide Pesavento2f46d652023-11-09 23:40:01 -050054 boost::asio::io_context io;
Davide Pesavento2bf35a62017-04-02 00:41:06 -040055 auto nm = make_unique<NetworkMonitor>(io);
Junxiao Shif4c1eb32017-05-30 17:05:10 +000056 NM_REQUIRE_CAP(ENUM);
Davide Pesavento2bf35a62017-04-02 00:41:06 -040057
Junxiao Shi2dc416d2017-07-03 04:46:16 +000058 nm->onInterfaceAdded.connect([&] (const shared_ptr<const NetworkInterface>&) {
Davide Pesavento2f46d652023-11-09 23:40:01 -050059 boost::asio::post(io, [&] { nm.reset(); });
Davide Pesavento2bf35a62017-04-02 00:41:06 -040060 });
61 nm->onEnumerationCompleted.connect([&] {
Davide Pesavento2bf35a62017-04-02 00:41:06 -040062 // make sure the test case terminates even if we have zero interfaces
Davide Pesavento2f46d652023-11-09 23:40:01 -050063 boost::asio::post(io, [&] { nm.reset(); });
Davide Pesavento2bf35a62017-04-02 00:41:06 -040064 });
65
66 io.run();
Davide Pesavento2bf35a62017-04-02 00:41:06 -040067 BOOST_CHECK(true); // if we got this far, the test passed
68}
69
70BOOST_AUTO_TEST_SUITE_END() // TestNetworkMonitor
Junxiao Shi25467942017-06-30 02:53:14 +000071BOOST_AUTO_TEST_SUITE_END() // Net
Davide Pesavento2bf35a62017-04-02 00:41:06 -040072
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040073} // namespace ndn::tests