blob: eafc52bc02fe3024ea51ef77a6707b57889f3507 [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 Pesavento66777622020-10-09 18:46:03 -04003 * Copyright (c) 2015-2020, 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
29namespace ndn {
30namespace ping {
31namespace tests {
32
33using namespace ndn::tests;
34
Davide Pesavento66777622020-10-09 18:46:03 -040035class PingIntegratedFixture : public IoFixture, public KeyChainFixture
Eric Newberrya93680e2015-07-15 19:25:29 -070036{
37public:
38 PingIntegratedFixture()
Davide Pesavento66777622020-10-09 18:46:03 -040039 : serverFace(m_io, m_keyChain, {false, true})
40 , clientFace(m_io, m_keyChain, {false, true})
Eric Newberrya93680e2015-07-15 19:25:29 -070041 , wantLoss(false)
42 {
Junxiao Shiaa1b3c92016-07-14 14:56:53 +000043 serverFace.onSendInterest.connect([this] (const Interest& interest) {
Davide Pesavento66777622020-10-09 18:46:03 -040044 m_io.post([=] { if (!wantLoss) { clientFace.receive(interest); } });
Eric Newberrya93680e2015-07-15 19:25:29 -070045 });
Junxiao Shiaa1b3c92016-07-14 14:56:53 +000046 clientFace.onSendInterest.connect([this] (const Interest& interest) {
Davide Pesavento66777622020-10-09 18:46:03 -040047 m_io.post([=] { if (!wantLoss) { serverFace.receive(interest); } });
Eric Newberrya93680e2015-07-15 19:25:29 -070048 });
Junxiao Shiaa1b3c92016-07-14 14:56:53 +000049 serverFace.onSendData.connect([this] (const Data& data) {
Davide Pesavento66777622020-10-09 18:46:03 -040050 m_io.post([=] { if (!wantLoss) { clientFace.receive(data); } });
Eric Newberrya93680e2015-07-15 19:25:29 -070051 });
Junxiao Shiaa1b3c92016-07-14 14:56:53 +000052 clientFace.onSendData.connect([this] (const Data& data) {
Davide Pesavento66777622020-10-09 18:46:03 -040053 m_io.post([=] { if (!wantLoss) { serverFace.receive(data); } });
Eric Newberrya93680e2015-07-15 19:25:29 -070054 });
55 }
56
Teng Liang62884862016-03-31 18:15:36 -070057 void onFinish()
Eric Newberrya93680e2015-07-15 19:25:29 -070058 {
Junxiao Shiaa1b3c92016-07-14 14:56:53 +000059 serverFace.shutdown();
60 clientFace.shutdown();
Davide Pesavento66777622020-10-09 18:46:03 -040061 m_io.stop();
Eric Newberrya93680e2015-07-15 19:25:29 -070062 }
63
64public:
Junxiao Shiaa1b3c92016-07-14 14:56:53 +000065 util::DummyClientFace serverFace;
66 util::DummyClientFace clientFace;
Eric Newberrya93680e2015-07-15 19:25:29 -070067 std::unique_ptr<server::PingServer> server;
68 std::unique_ptr<client::Ping> client;
Eric Newberrya93680e2015-07-15 19:25:29 -070069 bool wantLoss;
70};
71
Davide Pesavento013de9b2016-09-01 12:06:56 +000072BOOST_AUTO_TEST_SUITE(Ping)
73BOOST_AUTO_TEST_SUITE(TestIntegrated)
Eric Newberrya93680e2015-07-15 19:25:29 -070074
75BOOST_FIXTURE_TEST_CASE(Normal, PingIntegratedFixture)
76{
77 server::Options serverOpts;
78 serverOpts.prefix = "ndn:/test-prefix";
Davide Pesavento5923bf82018-08-09 01:55:44 -040079 serverOpts.freshnessPeriod = 5_s;
Eric Newberrya93680e2015-07-15 19:25:29 -070080 serverOpts.nMaxPings = 4;
Davide Pesavento5923bf82018-08-09 01:55:44 -040081 serverOpts.wantTimestamp = false;
Eric Newberrya93680e2015-07-15 19:25:29 -070082 serverOpts.payloadSize = 0;
Davide Pesavento5923bf82018-08-09 01:55:44 -040083 server = make_unique<server::PingServer>(serverFace, m_keyChain, serverOpts);
Eric Newberrya93680e2015-07-15 19:25:29 -070084 BOOST_REQUIRE_EQUAL(0, server->getNPings());
85 server->start();
86
87 client::Options clientOpts;
88 clientOpts.prefix = "ndn:/test-prefix";
89 clientOpts.shouldAllowStaleData = false;
90 clientOpts.shouldGenerateRandomSeq = false;
91 clientOpts.shouldPrintTimestamp = false;
92 clientOpts.nPings = 4;
Davide Pesavento5923bf82018-08-09 01:55:44 -040093 clientOpts.interval = 100_ms;
94 clientOpts.timeout = 2_s;
Eric Newberrya93680e2015-07-15 19:25:29 -070095 clientOpts.startSeq = 1000;
Davide Pesavento5923bf82018-08-09 01:55:44 -040096 client = make_unique<client::Ping>(clientFace, clientOpts);
Davide Pesavento66777622020-10-09 18:46:03 -040097 client->afterFinish.connect([this] { onFinish(); });
Eric Newberrya93680e2015-07-15 19:25:29 -070098 client->start();
99
Davide Pesavento66777622020-10-09 18:46:03 -0400100 advanceClocks(1_ms, 400);
101 m_io.run();
Eric Newberrya93680e2015-07-15 19:25:29 -0700102
Davide Pesavento5923bf82018-08-09 01:55:44 -0400103 BOOST_CHECK_EQUAL(4, server->getNPings());
Eric Newberrya93680e2015-07-15 19:25:29 -0700104}
105
106BOOST_FIXTURE_TEST_CASE(Timeout, PingIntegratedFixture)
107{
108 wantLoss = true;
109
110 server::Options serverOpts;
111 serverOpts.prefix = "ndn:/test-prefix";
Davide Pesavento5923bf82018-08-09 01:55:44 -0400112 serverOpts.freshnessPeriod = 5_s;
Eric Newberrya93680e2015-07-15 19:25:29 -0700113 serverOpts.nMaxPings = 4;
Davide Pesavento5923bf82018-08-09 01:55:44 -0400114 serverOpts.wantTimestamp = false;
Eric Newberrya93680e2015-07-15 19:25:29 -0700115 serverOpts.payloadSize = 0;
Davide Pesavento5923bf82018-08-09 01:55:44 -0400116 server = make_unique<server::PingServer>(serverFace, m_keyChain, serverOpts);
Eric Newberrya93680e2015-07-15 19:25:29 -0700117 BOOST_REQUIRE_EQUAL(0, server->getNPings());
118 server->start();
119
120 client::Options clientOpts;
121 clientOpts.prefix = "ndn:/test-prefix";
122 clientOpts.shouldAllowStaleData = false;
123 clientOpts.shouldGenerateRandomSeq = false;
124 clientOpts.shouldPrintTimestamp = false;
125 clientOpts.nPings = 4;
Davide Pesavento5923bf82018-08-09 01:55:44 -0400126 clientOpts.interval = 100_ms;
127 clientOpts.timeout = 500_ms;
Eric Newberrya93680e2015-07-15 19:25:29 -0700128 clientOpts.startSeq = 1000;
Davide Pesavento5923bf82018-08-09 01:55:44 -0400129 client = make_unique<client::Ping>(clientFace, clientOpts);
Davide Pesavento66777622020-10-09 18:46:03 -0400130 client->afterFinish.connect([this] { onFinish(); });
Eric Newberrya93680e2015-07-15 19:25:29 -0700131 client->start();
132
Davide Pesavento66777622020-10-09 18:46:03 -0400133 advanceClocks(1_ms, 1000);
134 m_io.run();
Eric Newberrya93680e2015-07-15 19:25:29 -0700135
Davide Pesavento5923bf82018-08-09 01:55:44 -0400136 BOOST_CHECK_EQUAL(0, server->getNPings());
Eric Newberrya93680e2015-07-15 19:25:29 -0700137}
138
Davide Pesavento013de9b2016-09-01 12:06:56 +0000139BOOST_AUTO_TEST_SUITE_END() // TestIntegrated
140BOOST_AUTO_TEST_SUITE_END() // Ping
Eric Newberrya93680e2015-07-15 19:25:29 -0700141
142} // namespace tests
143} // namespace ping
144} // namespace ndn