Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 5923bf8 | 2018-08-09 01:55:44 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | 5748e82 | 2024-01-26 18:40:22 -0500 | [diff] [blame] | 3 | * Copyright (c) 2015-2024, Arizona Board of Regents. |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-tools (Named Data Networking Essential Tools). |
| 6 | * See AUTHORS.md for complete list of ndn-tools authors and contributors. |
| 7 | * |
| 8 | * ndn-tools is free software: you can redistribute it and/or modify it under the terms |
| 9 | * of the GNU General Public License as published by the Free Software Foundation, |
| 10 | * either version 3 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * ndn-tools is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 13 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 | * PURPOSE. See the GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along with |
| 17 | * ndn-tools, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 20 | #include "tools/ping/client/ping.hpp" |
Davide Pesavento | 6677762 | 2020-10-09 18:46:03 -0400 | [diff] [blame] | 21 | #include "tools/ping/server/ping-server.hpp" |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 22 | |
Davide Pesavento | 6677762 | 2020-10-09 18:46:03 -0400 | [diff] [blame] | 23 | #include "tests/test-common.hpp" |
| 24 | #include "tests/io-fixture.hpp" |
| 25 | #include "tests/key-chain-fixture.hpp" |
| 26 | |
Davide Pesavento | 013de9b | 2016-09-01 12:06:56 +0000 | [diff] [blame] | 27 | #include <ndn-cxx/util/dummy-client-face.hpp> |
Davide Pesavento | 2ab04a2 | 2023-09-15 22:08:22 -0400 | [diff] [blame] | 28 | #include <boost/asio/post.hpp> |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 29 | |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame] | 30 | namespace ndn::ping::tests { |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 31 | |
| 32 | using namespace ndn::tests; |
| 33 | |
Davide Pesavento | 6677762 | 2020-10-09 18:46:03 -0400 | [diff] [blame] | 34 | class PingIntegratedFixture : public IoFixture, public KeyChainFixture |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 35 | { |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame] | 36 | protected: |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 37 | PingIntegratedFixture() |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 38 | { |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame] | 39 | serverFace.onSendInterest.connect([this] (const auto& interest) { |
| 40 | receive(clientFace, interest); |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 41 | }); |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame] | 42 | clientFace.onSendInterest.connect([this] (const auto& interest) { |
| 43 | receive(serverFace, interest); |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 44 | }); |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame] | 45 | serverFace.onSendData.connect([this] (const auto& data) { |
| 46 | receive(clientFace, data); |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 47 | }); |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame] | 48 | clientFace.onSendData.connect([this] (const auto& data) { |
| 49 | receive(serverFace, data); |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 50 | }); |
| 51 | } |
| 52 | |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame] | 53 | template<typename Packet> |
| 54 | void |
Junxiao Shi | 869d73e | 2023-08-10 22:52:26 +0000 | [diff] [blame] | 55 | receive(DummyClientFace& face, const Packet& pkt) |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame] | 56 | { |
Davide Pesavento | 5748e82 | 2024-01-26 18:40:22 -0500 | [diff] [blame] | 57 | boost::asio::post(m_io, [&face, pkt, wantLoss = wantLoss] { |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame] | 58 | if (!wantLoss) { |
| 59 | face.receive(pkt); |
| 60 | } |
| 61 | }); |
| 62 | } |
| 63 | |
| 64 | void |
| 65 | onFinish() |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 66 | { |
Junxiao Shi | aa1b3c9 | 2016-07-14 14:56:53 +0000 | [diff] [blame] | 67 | serverFace.shutdown(); |
| 68 | clientFace.shutdown(); |
Davide Pesavento | 6677762 | 2020-10-09 18:46:03 -0400 | [diff] [blame] | 69 | m_io.stop(); |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame] | 72 | protected: |
Junxiao Shi | 869d73e | 2023-08-10 22:52:26 +0000 | [diff] [blame] | 73 | DummyClientFace serverFace{m_io, m_keyChain, {false, true}}; |
| 74 | DummyClientFace clientFace{m_io, m_keyChain, {false, true}}; |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 75 | std::unique_ptr<server::PingServer> server; |
| 76 | std::unique_ptr<client::Ping> client; |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame] | 77 | bool wantLoss = false; |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 78 | }; |
| 79 | |
Davide Pesavento | 013de9b | 2016-09-01 12:06:56 +0000 | [diff] [blame] | 80 | BOOST_AUTO_TEST_SUITE(Ping) |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame] | 81 | BOOST_FIXTURE_TEST_SUITE(TestIntegrated, PingIntegratedFixture) |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 82 | |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame] | 83 | BOOST_AUTO_TEST_CASE(Normal) |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 84 | { |
| 85 | server::Options serverOpts; |
| 86 | serverOpts.prefix = "ndn:/test-prefix"; |
Davide Pesavento | 5923bf8 | 2018-08-09 01:55:44 -0400 | [diff] [blame] | 87 | serverOpts.freshnessPeriod = 5_s; |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 88 | serverOpts.nMaxPings = 4; |
Davide Pesavento | 5923bf8 | 2018-08-09 01:55:44 -0400 | [diff] [blame] | 89 | serverOpts.wantTimestamp = false; |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 90 | serverOpts.payloadSize = 0; |
Davide Pesavento | 5748e82 | 2024-01-26 18:40:22 -0500 | [diff] [blame] | 91 | server = std::make_unique<server::PingServer>(serverFace, m_keyChain, serverOpts); |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 92 | BOOST_REQUIRE_EQUAL(0, server->getNPings()); |
| 93 | server->start(); |
| 94 | |
| 95 | client::Options clientOpts; |
| 96 | clientOpts.prefix = "ndn:/test-prefix"; |
| 97 | clientOpts.shouldAllowStaleData = false; |
| 98 | clientOpts.shouldGenerateRandomSeq = false; |
| 99 | clientOpts.shouldPrintTimestamp = false; |
| 100 | clientOpts.nPings = 4; |
Davide Pesavento | 5923bf8 | 2018-08-09 01:55:44 -0400 | [diff] [blame] | 101 | clientOpts.interval = 100_ms; |
| 102 | clientOpts.timeout = 2_s; |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 103 | clientOpts.startSeq = 1000; |
Davide Pesavento | 5748e82 | 2024-01-26 18:40:22 -0500 | [diff] [blame] | 104 | client = std::make_unique<client::Ping>(clientFace, clientOpts); |
Davide Pesavento | 6677762 | 2020-10-09 18:46:03 -0400 | [diff] [blame] | 105 | client->afterFinish.connect([this] { onFinish(); }); |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 106 | client->start(); |
| 107 | |
Davide Pesavento | 6677762 | 2020-10-09 18:46:03 -0400 | [diff] [blame] | 108 | advanceClocks(1_ms, 400); |
| 109 | m_io.run(); |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 110 | |
Davide Pesavento | 5923bf8 | 2018-08-09 01:55:44 -0400 | [diff] [blame] | 111 | BOOST_CHECK_EQUAL(4, server->getNPings()); |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame] | 114 | BOOST_AUTO_TEST_CASE(Timeout) |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 115 | { |
| 116 | wantLoss = true; |
| 117 | |
| 118 | server::Options serverOpts; |
| 119 | serverOpts.prefix = "ndn:/test-prefix"; |
Davide Pesavento | 5923bf8 | 2018-08-09 01:55:44 -0400 | [diff] [blame] | 120 | serverOpts.freshnessPeriod = 5_s; |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 121 | serverOpts.nMaxPings = 4; |
Davide Pesavento | 5923bf8 | 2018-08-09 01:55:44 -0400 | [diff] [blame] | 122 | serverOpts.wantTimestamp = false; |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 123 | serverOpts.payloadSize = 0; |
Davide Pesavento | 5748e82 | 2024-01-26 18:40:22 -0500 | [diff] [blame] | 124 | server = std::make_unique<server::PingServer>(serverFace, m_keyChain, serverOpts); |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 125 | BOOST_REQUIRE_EQUAL(0, server->getNPings()); |
| 126 | server->start(); |
| 127 | |
| 128 | client::Options clientOpts; |
| 129 | clientOpts.prefix = "ndn:/test-prefix"; |
| 130 | clientOpts.shouldAllowStaleData = false; |
| 131 | clientOpts.shouldGenerateRandomSeq = false; |
| 132 | clientOpts.shouldPrintTimestamp = false; |
| 133 | clientOpts.nPings = 4; |
Davide Pesavento | 5923bf8 | 2018-08-09 01:55:44 -0400 | [diff] [blame] | 134 | clientOpts.interval = 100_ms; |
| 135 | clientOpts.timeout = 500_ms; |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 136 | clientOpts.startSeq = 1000; |
Davide Pesavento | 5748e82 | 2024-01-26 18:40:22 -0500 | [diff] [blame] | 137 | client = std::make_unique<client::Ping>(clientFace, clientOpts); |
Davide Pesavento | 6677762 | 2020-10-09 18:46:03 -0400 | [diff] [blame] | 138 | client->afterFinish.connect([this] { onFinish(); }); |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 139 | client->start(); |
| 140 | |
Davide Pesavento | 6677762 | 2020-10-09 18:46:03 -0400 | [diff] [blame] | 141 | advanceClocks(1_ms, 1000); |
| 142 | m_io.run(); |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 143 | |
Davide Pesavento | 5923bf8 | 2018-08-09 01:55:44 -0400 | [diff] [blame] | 144 | BOOST_CHECK_EQUAL(0, server->getNPings()); |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 145 | } |
| 146 | |
Davide Pesavento | 013de9b | 2016-09-01 12:06:56 +0000 | [diff] [blame] | 147 | BOOST_AUTO_TEST_SUITE_END() // TestIntegrated |
| 148 | BOOST_AUTO_TEST_SUITE_END() // Ping |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 149 | |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame] | 150 | } // namespace ndn::ping::tests |