blob: caad9f8168328988e205bdc42dc2da16b7f5b163 [file] [log] [blame]
Eric Newberrya93680e2015-07-15 19:25:29 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento5923bf82018-08-09 01:55:44 -04002/*
Davide Pesavento5748e822024-01-26 18:40:22 -05003 * Copyright (c) 2015-2024, Arizona Board of Regents.
Eric Newberrya93680e2015-07-15 19:25:29 -07004 *
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 Newberrya93680e2015-07-15 19:25:29 -070020#include "tools/ping/client/ping.hpp"
Davide Pesavento66777622020-10-09 18:46:03 -040021#include "tools/ping/server/ping-server.hpp"
Eric Newberrya93680e2015-07-15 19:25:29 -070022
Davide Pesavento66777622020-10-09 18:46:03 -040023#include "tests/test-common.hpp"
24#include "tests/io-fixture.hpp"
25#include "tests/key-chain-fixture.hpp"
26
Davide Pesavento013de9b2016-09-01 12:06:56 +000027#include <ndn-cxx/util/dummy-client-face.hpp>
Davide Pesavento2ab04a22023-09-15 22:08:22 -040028#include <boost/asio/post.hpp>
Eric Newberrya93680e2015-07-15 19:25:29 -070029
Davide Pesaventob3570c62022-02-19 19:19:00 -050030namespace ndn::ping::tests {
Eric Newberrya93680e2015-07-15 19:25:29 -070031
32using namespace ndn::tests;
33
Davide Pesavento66777622020-10-09 18:46:03 -040034class PingIntegratedFixture : public IoFixture, public KeyChainFixture
Eric Newberrya93680e2015-07-15 19:25:29 -070035{
Davide Pesaventob3570c62022-02-19 19:19:00 -050036protected:
Eric Newberrya93680e2015-07-15 19:25:29 -070037 PingIntegratedFixture()
Eric Newberrya93680e2015-07-15 19:25:29 -070038 {
Davide Pesaventob3570c62022-02-19 19:19:00 -050039 serverFace.onSendInterest.connect([this] (const auto& interest) {
40 receive(clientFace, interest);
Eric Newberrya93680e2015-07-15 19:25:29 -070041 });
Davide Pesaventob3570c62022-02-19 19:19:00 -050042 clientFace.onSendInterest.connect([this] (const auto& interest) {
43 receive(serverFace, interest);
Eric Newberrya93680e2015-07-15 19:25:29 -070044 });
Davide Pesaventob3570c62022-02-19 19:19:00 -050045 serverFace.onSendData.connect([this] (const auto& data) {
46 receive(clientFace, data);
Eric Newberrya93680e2015-07-15 19:25:29 -070047 });
Davide Pesaventob3570c62022-02-19 19:19:00 -050048 clientFace.onSendData.connect([this] (const auto& data) {
49 receive(serverFace, data);
Eric Newberrya93680e2015-07-15 19:25:29 -070050 });
51 }
52
Davide Pesaventob3570c62022-02-19 19:19:00 -050053 template<typename Packet>
54 void
Junxiao Shi869d73e2023-08-10 22:52:26 +000055 receive(DummyClientFace& face, const Packet& pkt)
Davide Pesaventob3570c62022-02-19 19:19:00 -050056 {
Davide Pesavento5748e822024-01-26 18:40:22 -050057 boost::asio::post(m_io, [&face, pkt, wantLoss = wantLoss] {
Davide Pesaventob3570c62022-02-19 19:19:00 -050058 if (!wantLoss) {
59 face.receive(pkt);
60 }
61 });
62 }
63
64 void
65 onFinish()
Eric Newberrya93680e2015-07-15 19:25:29 -070066 {
Junxiao Shiaa1b3c92016-07-14 14:56:53 +000067 serverFace.shutdown();
68 clientFace.shutdown();
Davide Pesavento66777622020-10-09 18:46:03 -040069 m_io.stop();
Eric Newberrya93680e2015-07-15 19:25:29 -070070 }
71
Davide Pesaventob3570c62022-02-19 19:19:00 -050072protected:
Junxiao Shi869d73e2023-08-10 22:52:26 +000073 DummyClientFace serverFace{m_io, m_keyChain, {false, true}};
74 DummyClientFace clientFace{m_io, m_keyChain, {false, true}};
Eric Newberrya93680e2015-07-15 19:25:29 -070075 std::unique_ptr<server::PingServer> server;
76 std::unique_ptr<client::Ping> client;
Davide Pesaventob3570c62022-02-19 19:19:00 -050077 bool wantLoss = false;
Eric Newberrya93680e2015-07-15 19:25:29 -070078};
79
Davide Pesavento013de9b2016-09-01 12:06:56 +000080BOOST_AUTO_TEST_SUITE(Ping)
Davide Pesaventob3570c62022-02-19 19:19:00 -050081BOOST_FIXTURE_TEST_SUITE(TestIntegrated, PingIntegratedFixture)
Eric Newberrya93680e2015-07-15 19:25:29 -070082
Davide Pesaventob3570c62022-02-19 19:19:00 -050083BOOST_AUTO_TEST_CASE(Normal)
Eric Newberrya93680e2015-07-15 19:25:29 -070084{
85 server::Options serverOpts;
86 serverOpts.prefix = "ndn:/test-prefix";
Davide Pesavento5923bf82018-08-09 01:55:44 -040087 serverOpts.freshnessPeriod = 5_s;
Eric Newberrya93680e2015-07-15 19:25:29 -070088 serverOpts.nMaxPings = 4;
Davide Pesavento5923bf82018-08-09 01:55:44 -040089 serverOpts.wantTimestamp = false;
Eric Newberrya93680e2015-07-15 19:25:29 -070090 serverOpts.payloadSize = 0;
Davide Pesavento5748e822024-01-26 18:40:22 -050091 server = std::make_unique<server::PingServer>(serverFace, m_keyChain, serverOpts);
Eric Newberrya93680e2015-07-15 19:25:29 -070092 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 Pesavento5923bf82018-08-09 01:55:44 -0400101 clientOpts.interval = 100_ms;
102 clientOpts.timeout = 2_s;
Eric Newberrya93680e2015-07-15 19:25:29 -0700103 clientOpts.startSeq = 1000;
Davide Pesavento5748e822024-01-26 18:40:22 -0500104 client = std::make_unique<client::Ping>(clientFace, clientOpts);
Davide Pesavento66777622020-10-09 18:46:03 -0400105 client->afterFinish.connect([this] { onFinish(); });
Eric Newberrya93680e2015-07-15 19:25:29 -0700106 client->start();
107
Davide Pesavento66777622020-10-09 18:46:03 -0400108 advanceClocks(1_ms, 400);
109 m_io.run();
Eric Newberrya93680e2015-07-15 19:25:29 -0700110
Davide Pesavento5923bf82018-08-09 01:55:44 -0400111 BOOST_CHECK_EQUAL(4, server->getNPings());
Eric Newberrya93680e2015-07-15 19:25:29 -0700112}
113
Davide Pesaventob3570c62022-02-19 19:19:00 -0500114BOOST_AUTO_TEST_CASE(Timeout)
Eric Newberrya93680e2015-07-15 19:25:29 -0700115{
116 wantLoss = true;
117
118 server::Options serverOpts;
119 serverOpts.prefix = "ndn:/test-prefix";
Davide Pesavento5923bf82018-08-09 01:55:44 -0400120 serverOpts.freshnessPeriod = 5_s;
Eric Newberrya93680e2015-07-15 19:25:29 -0700121 serverOpts.nMaxPings = 4;
Davide Pesavento5923bf82018-08-09 01:55:44 -0400122 serverOpts.wantTimestamp = false;
Eric Newberrya93680e2015-07-15 19:25:29 -0700123 serverOpts.payloadSize = 0;
Davide Pesavento5748e822024-01-26 18:40:22 -0500124 server = std::make_unique<server::PingServer>(serverFace, m_keyChain, serverOpts);
Eric Newberrya93680e2015-07-15 19:25:29 -0700125 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 Pesavento5923bf82018-08-09 01:55:44 -0400134 clientOpts.interval = 100_ms;
135 clientOpts.timeout = 500_ms;
Eric Newberrya93680e2015-07-15 19:25:29 -0700136 clientOpts.startSeq = 1000;
Davide Pesavento5748e822024-01-26 18:40:22 -0500137 client = std::make_unique<client::Ping>(clientFace, clientOpts);
Davide Pesavento66777622020-10-09 18:46:03 -0400138 client->afterFinish.connect([this] { onFinish(); });
Eric Newberrya93680e2015-07-15 19:25:29 -0700139 client->start();
140
Davide Pesavento66777622020-10-09 18:46:03 -0400141 advanceClocks(1_ms, 1000);
142 m_io.run();
Eric Newberrya93680e2015-07-15 19:25:29 -0700143
Davide Pesavento5923bf82018-08-09 01:55:44 -0400144 BOOST_CHECK_EQUAL(0, server->getNPings());
Eric Newberrya93680e2015-07-15 19:25:29 -0700145}
146
Davide Pesavento013de9b2016-09-01 12:06:56 +0000147BOOST_AUTO_TEST_SUITE_END() // TestIntegrated
148BOOST_AUTO_TEST_SUITE_END() // Ping
Eric Newberrya93680e2015-07-15 19:25:29 -0700149
Davide Pesaventob3570c62022-02-19 19:19:00 -0500150} // namespace ndn::ping::tests