blob: 0b95ba3519fef638db86db1a4cf54f1f80208933 [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 Pesaventob3570c62022-02-19 19:19:00 -05003 * Copyright (c) 2015-2022, 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>
Eric Newberrya93680e2015-07-15 19:25:29 -070028
Davide Pesaventob3570c62022-02-19 19:19:00 -050029namespace ndn::ping::tests {
Eric Newberrya93680e2015-07-15 19:25:29 -070030
31using namespace ndn::tests;
32
Davide Pesavento66777622020-10-09 18:46:03 -040033class PingIntegratedFixture : public IoFixture, public KeyChainFixture
Eric Newberrya93680e2015-07-15 19:25:29 -070034{
Davide Pesaventob3570c62022-02-19 19:19:00 -050035protected:
Eric Newberrya93680e2015-07-15 19:25:29 -070036 PingIntegratedFixture()
Eric Newberrya93680e2015-07-15 19:25:29 -070037 {
Davide Pesaventob3570c62022-02-19 19:19:00 -050038 serverFace.onSendInterest.connect([this] (const auto& interest) {
39 receive(clientFace, interest);
Eric Newberrya93680e2015-07-15 19:25:29 -070040 });
Davide Pesaventob3570c62022-02-19 19:19:00 -050041 clientFace.onSendInterest.connect([this] (const auto& interest) {
42 receive(serverFace, interest);
Eric Newberrya93680e2015-07-15 19:25:29 -070043 });
Davide Pesaventob3570c62022-02-19 19:19:00 -050044 serverFace.onSendData.connect([this] (const auto& data) {
45 receive(clientFace, data);
Eric Newberrya93680e2015-07-15 19:25:29 -070046 });
Davide Pesaventob3570c62022-02-19 19:19:00 -050047 clientFace.onSendData.connect([this] (const auto& data) {
48 receive(serverFace, data);
Eric Newberrya93680e2015-07-15 19:25:29 -070049 });
50 }
51
Davide Pesaventob3570c62022-02-19 19:19:00 -050052 template<typename Packet>
53 void
54 receive(util::DummyClientFace& face, const Packet& pkt)
55 {
56 m_io.post([=, &face] {
57 if (!wantLoss) {
58 face.receive(pkt);
59 }
60 });
61 }
62
63 void
64 onFinish()
Eric Newberrya93680e2015-07-15 19:25:29 -070065 {
Junxiao Shiaa1b3c92016-07-14 14:56:53 +000066 serverFace.shutdown();
67 clientFace.shutdown();
Davide Pesavento66777622020-10-09 18:46:03 -040068 m_io.stop();
Eric Newberrya93680e2015-07-15 19:25:29 -070069 }
70
Davide Pesaventob3570c62022-02-19 19:19:00 -050071protected:
72 util::DummyClientFace serverFace{m_io, m_keyChain, {false, true}};
73 util::DummyClientFace clientFace{m_io, m_keyChain, {false, true}};
Eric Newberrya93680e2015-07-15 19:25:29 -070074 std::unique_ptr<server::PingServer> server;
75 std::unique_ptr<client::Ping> client;
Davide Pesaventob3570c62022-02-19 19:19:00 -050076 bool wantLoss = false;
Eric Newberrya93680e2015-07-15 19:25:29 -070077};
78
Davide Pesavento013de9b2016-09-01 12:06:56 +000079BOOST_AUTO_TEST_SUITE(Ping)
Davide Pesaventob3570c62022-02-19 19:19:00 -050080BOOST_FIXTURE_TEST_SUITE(TestIntegrated, PingIntegratedFixture)
Eric Newberrya93680e2015-07-15 19:25:29 -070081
Davide Pesaventob3570c62022-02-19 19:19:00 -050082BOOST_AUTO_TEST_CASE(Normal)
Eric Newberrya93680e2015-07-15 19:25:29 -070083{
84 server::Options serverOpts;
85 serverOpts.prefix = "ndn:/test-prefix";
Davide Pesavento5923bf82018-08-09 01:55:44 -040086 serverOpts.freshnessPeriod = 5_s;
Eric Newberrya93680e2015-07-15 19:25:29 -070087 serverOpts.nMaxPings = 4;
Davide Pesavento5923bf82018-08-09 01:55:44 -040088 serverOpts.wantTimestamp = false;
Eric Newberrya93680e2015-07-15 19:25:29 -070089 serverOpts.payloadSize = 0;
Davide Pesavento5923bf82018-08-09 01:55:44 -040090 server = make_unique<server::PingServer>(serverFace, m_keyChain, serverOpts);
Eric Newberrya93680e2015-07-15 19:25:29 -070091 BOOST_REQUIRE_EQUAL(0, server->getNPings());
92 server->start();
93
94 client::Options clientOpts;
95 clientOpts.prefix = "ndn:/test-prefix";
96 clientOpts.shouldAllowStaleData = false;
97 clientOpts.shouldGenerateRandomSeq = false;
98 clientOpts.shouldPrintTimestamp = false;
99 clientOpts.nPings = 4;
Davide Pesavento5923bf82018-08-09 01:55:44 -0400100 clientOpts.interval = 100_ms;
101 clientOpts.timeout = 2_s;
Eric Newberrya93680e2015-07-15 19:25:29 -0700102 clientOpts.startSeq = 1000;
Davide Pesavento5923bf82018-08-09 01:55:44 -0400103 client = make_unique<client::Ping>(clientFace, clientOpts);
Davide Pesavento66777622020-10-09 18:46:03 -0400104 client->afterFinish.connect([this] { onFinish(); });
Eric Newberrya93680e2015-07-15 19:25:29 -0700105 client->start();
106
Davide Pesavento66777622020-10-09 18:46:03 -0400107 advanceClocks(1_ms, 400);
108 m_io.run();
Eric Newberrya93680e2015-07-15 19:25:29 -0700109
Davide Pesavento5923bf82018-08-09 01:55:44 -0400110 BOOST_CHECK_EQUAL(4, server->getNPings());
Eric Newberrya93680e2015-07-15 19:25:29 -0700111}
112
Davide Pesaventob3570c62022-02-19 19:19:00 -0500113BOOST_AUTO_TEST_CASE(Timeout)
Eric Newberrya93680e2015-07-15 19:25:29 -0700114{
115 wantLoss = true;
116
117 server::Options serverOpts;
118 serverOpts.prefix = "ndn:/test-prefix";
Davide Pesavento5923bf82018-08-09 01:55:44 -0400119 serverOpts.freshnessPeriod = 5_s;
Eric Newberrya93680e2015-07-15 19:25:29 -0700120 serverOpts.nMaxPings = 4;
Davide Pesavento5923bf82018-08-09 01:55:44 -0400121 serverOpts.wantTimestamp = false;
Eric Newberrya93680e2015-07-15 19:25:29 -0700122 serverOpts.payloadSize = 0;
Davide Pesavento5923bf82018-08-09 01:55:44 -0400123 server = make_unique<server::PingServer>(serverFace, m_keyChain, serverOpts);
Eric Newberrya93680e2015-07-15 19:25:29 -0700124 BOOST_REQUIRE_EQUAL(0, server->getNPings());
125 server->start();
126
127 client::Options clientOpts;
128 clientOpts.prefix = "ndn:/test-prefix";
129 clientOpts.shouldAllowStaleData = false;
130 clientOpts.shouldGenerateRandomSeq = false;
131 clientOpts.shouldPrintTimestamp = false;
132 clientOpts.nPings = 4;
Davide Pesavento5923bf82018-08-09 01:55:44 -0400133 clientOpts.interval = 100_ms;
134 clientOpts.timeout = 500_ms;
Eric Newberrya93680e2015-07-15 19:25:29 -0700135 clientOpts.startSeq = 1000;
Davide Pesavento5923bf82018-08-09 01:55:44 -0400136 client = make_unique<client::Ping>(clientFace, clientOpts);
Davide Pesavento66777622020-10-09 18:46:03 -0400137 client->afterFinish.connect([this] { onFinish(); });
Eric Newberrya93680e2015-07-15 19:25:29 -0700138 client->start();
139
Davide Pesavento66777622020-10-09 18:46:03 -0400140 advanceClocks(1_ms, 1000);
141 m_io.run();
Eric Newberrya93680e2015-07-15 19:25:29 -0700142
Davide Pesavento5923bf82018-08-09 01:55:44 -0400143 BOOST_CHECK_EQUAL(0, server->getNPings());
Eric Newberrya93680e2015-07-15 19:25:29 -0700144}
145
Davide Pesavento013de9b2016-09-01 12:06:56 +0000146BOOST_AUTO_TEST_SUITE_END() // TestIntegrated
147BOOST_AUTO_TEST_SUITE_END() // Ping
Eric Newberrya93680e2015-07-15 19:25:29 -0700148
Davide Pesaventob3570c62022-02-19 19:19:00 -0500149} // namespace ndn::ping::tests