151 lines
4.4 KiB
C++
151 lines
4.4 KiB
C++
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
|
|
/*
|
|
* Copyright (c) 2015-2023, Arizona Board of Regents.
|
|
*
|
|
* This file is part of ndn-tools (Named Data Networking Essential Tools).
|
|
* See AUTHORS.md for complete list of ndn-tools authors and contributors.
|
|
*
|
|
* ndn-tools is free software: you can redistribute it and/or modify it under the terms
|
|
* of the GNU General Public License as published by the Free Software Foundation,
|
|
* either version 3 of the License, or (at your option) any later version.
|
|
*
|
|
* ndn-tools is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
|
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
|
* PURPOSE. See the GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License along with
|
|
* ndn-tools, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#include "tools/ping/client/ping.hpp"
|
|
#include "tools/ping/server/ping-server.hpp"
|
|
|
|
#include "tests/test-common.hpp"
|
|
#include "tests/io-fixture.hpp"
|
|
#include "tests/key-chain-fixture.hpp"
|
|
|
|
#include <ndn-cxx/util/dummy-client-face.hpp>
|
|
#include <boost/asio/post.hpp>
|
|
|
|
namespace ndn::ping::tests {
|
|
|
|
using namespace ndn::tests;
|
|
|
|
class PingIntegratedFixture : public IoFixture, public KeyChainFixture
|
|
{
|
|
protected:
|
|
PingIntegratedFixture()
|
|
{
|
|
serverFace.onSendInterest.connect([this] (const auto& interest) {
|
|
receive(clientFace, interest);
|
|
});
|
|
clientFace.onSendInterest.connect([this] (const auto& interest) {
|
|
receive(serverFace, interest);
|
|
});
|
|
serverFace.onSendData.connect([this] (const auto& data) {
|
|
receive(clientFace, data);
|
|
});
|
|
clientFace.onSendData.connect([this] (const auto& data) {
|
|
receive(serverFace, data);
|
|
});
|
|
}
|
|
|
|
template<typename Packet>
|
|
void
|
|
receive(DummyClientFace& face, const Packet& pkt)
|
|
{
|
|
boost::asio::post(m_io, [=, &face] {
|
|
if (!wantLoss) {
|
|
face.receive(pkt);
|
|
}
|
|
});
|
|
}
|
|
|
|
void
|
|
onFinish()
|
|
{
|
|
serverFace.shutdown();
|
|
clientFace.shutdown();
|
|
m_io.stop();
|
|
}
|
|
|
|
protected:
|
|
DummyClientFace serverFace{m_io, m_keyChain, {false, true}};
|
|
DummyClientFace clientFace{m_io, m_keyChain, {false, true}};
|
|
std::unique_ptr<server::PingServer> server;
|
|
std::unique_ptr<client::Ping> client;
|
|
bool wantLoss = false;
|
|
};
|
|
|
|
BOOST_AUTO_TEST_SUITE(Ping)
|
|
BOOST_FIXTURE_TEST_SUITE(TestIntegrated, PingIntegratedFixture)
|
|
|
|
BOOST_AUTO_TEST_CASE(Normal)
|
|
{
|
|
server::Options serverOpts;
|
|
serverOpts.prefix = "ndn:/test-prefix";
|
|
serverOpts.freshnessPeriod = 5_s;
|
|
serverOpts.nMaxPings = 4;
|
|
serverOpts.wantTimestamp = false;
|
|
serverOpts.payloadSize = 0;
|
|
server = make_unique<server::PingServer>(serverFace, m_keyChain, serverOpts);
|
|
BOOST_REQUIRE_EQUAL(0, server->getNPings());
|
|
server->start();
|
|
|
|
client::Options clientOpts;
|
|
clientOpts.prefix = "ndn:/test-prefix";
|
|
clientOpts.shouldAllowStaleData = false;
|
|
clientOpts.shouldGenerateRandomSeq = false;
|
|
clientOpts.shouldPrintTimestamp = false;
|
|
clientOpts.nPings = 4;
|
|
clientOpts.interval = 100_ms;
|
|
clientOpts.timeout = 2_s;
|
|
clientOpts.startSeq = 1000;
|
|
client = make_unique<client::Ping>(clientFace, clientOpts);
|
|
client->afterFinish.connect([this] { onFinish(); });
|
|
client->start();
|
|
|
|
advanceClocks(1_ms, 400);
|
|
m_io.run();
|
|
|
|
BOOST_CHECK_EQUAL(4, server->getNPings());
|
|
}
|
|
|
|
BOOST_AUTO_TEST_CASE(Timeout)
|
|
{
|
|
wantLoss = true;
|
|
|
|
server::Options serverOpts;
|
|
serverOpts.prefix = "ndn:/test-prefix";
|
|
serverOpts.freshnessPeriod = 5_s;
|
|
serverOpts.nMaxPings = 4;
|
|
serverOpts.wantTimestamp = false;
|
|
serverOpts.payloadSize = 0;
|
|
server = make_unique<server::PingServer>(serverFace, m_keyChain, serverOpts);
|
|
BOOST_REQUIRE_EQUAL(0, server->getNPings());
|
|
server->start();
|
|
|
|
client::Options clientOpts;
|
|
clientOpts.prefix = "ndn:/test-prefix";
|
|
clientOpts.shouldAllowStaleData = false;
|
|
clientOpts.shouldGenerateRandomSeq = false;
|
|
clientOpts.shouldPrintTimestamp = false;
|
|
clientOpts.nPings = 4;
|
|
clientOpts.interval = 100_ms;
|
|
clientOpts.timeout = 500_ms;
|
|
clientOpts.startSeq = 1000;
|
|
client = make_unique<client::Ping>(clientFace, clientOpts);
|
|
client->afterFinish.connect([this] { onFinish(); });
|
|
client->start();
|
|
|
|
advanceClocks(1_ms, 1000);
|
|
m_io.run();
|
|
|
|
BOOST_CHECK_EQUAL(0, server->getNPings());
|
|
}
|
|
|
|
BOOST_AUTO_TEST_SUITE_END() // TestIntegrated
|
|
BOOST_AUTO_TEST_SUITE_END() // Ping
|
|
|
|
} // namespace ndn::ping::tests
|