blob: 84c13e5e2035853cd78bcf4abb44889da3508fba [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/*
3 * Copyright (c) 2015-2018, 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
20#include "tools/ping/server/ping-server.hpp"
21#include "tools/ping/client/ping.hpp"
Eric Newberrya93680e2015-07-15 19:25:29 -070022
Davide Pesavento013de9b2016-09-01 12:06:56 +000023#include "tests/identity-management-fixture.hpp"
24#include <ndn-cxx/util/dummy-client-face.hpp>
Eric Newberrya93680e2015-07-15 19:25:29 -070025
26namespace ndn {
27namespace ping {
28namespace tests {
29
30using namespace ndn::tests;
31
Alexander Afanasyev1e7a7b22015-08-26 15:35:26 -070032class PingIntegratedFixture : public IdentityManagementTimeFixture
Eric Newberrya93680e2015-07-15 19:25:29 -070033{
34public:
35 PingIntegratedFixture()
Junxiao Shiaa1b3c92016-07-14 14:56:53 +000036 : serverFace(io, m_keyChain, {false, true})
37 , clientFace(io, m_keyChain, {false, true})
Eric Newberrya93680e2015-07-15 19:25:29 -070038 , wantLoss(false)
39 {
Junxiao Shiaa1b3c92016-07-14 14:56:53 +000040 serverFace.onSendInterest.connect([this] (const Interest& interest) {
41 io.post([=] { if (!wantLoss) { clientFace.receive(interest); } });
Eric Newberrya93680e2015-07-15 19:25:29 -070042 });
Junxiao Shiaa1b3c92016-07-14 14:56:53 +000043 clientFace.onSendInterest.connect([this] (const Interest& interest) {
44 io.post([=] { if (!wantLoss) { serverFace.receive(interest); } });
Eric Newberrya93680e2015-07-15 19:25:29 -070045 });
Junxiao Shiaa1b3c92016-07-14 14:56:53 +000046 serverFace.onSendData.connect([this] (const Data& data) {
47 io.post([=] { if (!wantLoss) { clientFace.receive(data); } });
Eric Newberrya93680e2015-07-15 19:25:29 -070048 });
Junxiao Shiaa1b3c92016-07-14 14:56:53 +000049 clientFace.onSendData.connect([this] (const Data& data) {
50 io.post([=] { if (!wantLoss) { serverFace.receive(data); } });
Eric Newberrya93680e2015-07-15 19:25:29 -070051 });
52 }
53
Teng Liang62884862016-03-31 18:15:36 -070054 void onFinish()
Eric Newberrya93680e2015-07-15 19:25:29 -070055 {
Junxiao Shiaa1b3c92016-07-14 14:56:53 +000056 serverFace.shutdown();
57 clientFace.shutdown();
Teng Liang62884862016-03-31 18:15:36 -070058 io.stop();
Eric Newberrya93680e2015-07-15 19:25:29 -070059 }
60
61public:
62 boost::asio::io_service io;
Junxiao Shiaa1b3c92016-07-14 14:56:53 +000063 util::DummyClientFace serverFace;
64 util::DummyClientFace clientFace;
Eric Newberrya93680e2015-07-15 19:25:29 -070065 std::unique_ptr<server::PingServer> server;
66 std::unique_ptr<client::Ping> client;
Eric Newberrya93680e2015-07-15 19:25:29 -070067 bool wantLoss;
68};
69
Davide Pesavento013de9b2016-09-01 12:06:56 +000070BOOST_AUTO_TEST_SUITE(Ping)
71BOOST_AUTO_TEST_SUITE(TestIntegrated)
Eric Newberrya93680e2015-07-15 19:25:29 -070072
73BOOST_FIXTURE_TEST_CASE(Normal, PingIntegratedFixture)
74{
75 server::Options serverOpts;
76 serverOpts.prefix = "ndn:/test-prefix";
Davide Pesavento5923bf82018-08-09 01:55:44 -040077 serverOpts.freshnessPeriod = 5_s;
Eric Newberrya93680e2015-07-15 19:25:29 -070078 serverOpts.nMaxPings = 4;
Davide Pesavento5923bf82018-08-09 01:55:44 -040079 serverOpts.wantTimestamp = false;
Eric Newberrya93680e2015-07-15 19:25:29 -070080 serverOpts.payloadSize = 0;
Davide Pesavento5923bf82018-08-09 01:55:44 -040081 server = make_unique<server::PingServer>(serverFace, m_keyChain, serverOpts);
Eric Newberrya93680e2015-07-15 19:25:29 -070082 BOOST_REQUIRE_EQUAL(0, server->getNPings());
83 server->start();
84
85 client::Options clientOpts;
86 clientOpts.prefix = "ndn:/test-prefix";
87 clientOpts.shouldAllowStaleData = false;
88 clientOpts.shouldGenerateRandomSeq = false;
89 clientOpts.shouldPrintTimestamp = false;
90 clientOpts.nPings = 4;
Davide Pesavento5923bf82018-08-09 01:55:44 -040091 clientOpts.interval = 100_ms;
92 clientOpts.timeout = 2_s;
Eric Newberrya93680e2015-07-15 19:25:29 -070093 clientOpts.startSeq = 1000;
Davide Pesavento5923bf82018-08-09 01:55:44 -040094 client = make_unique<client::Ping>(clientFace, clientOpts);
Teng Liang62884862016-03-31 18:15:36 -070095 client->afterFinish.connect(bind(&PingIntegratedFixture::onFinish, this));
Eric Newberrya93680e2015-07-15 19:25:29 -070096 client->start();
97
Davide Pesavento5923bf82018-08-09 01:55:44 -040098 advanceClocks(io, 1_ms, 400);
Eric Newberrya93680e2015-07-15 19:25:29 -070099 io.run();
100
Davide Pesavento5923bf82018-08-09 01:55:44 -0400101 BOOST_CHECK_EQUAL(4, server->getNPings());
Eric Newberrya93680e2015-07-15 19:25:29 -0700102}
103
104BOOST_FIXTURE_TEST_CASE(Timeout, PingIntegratedFixture)
105{
106 wantLoss = true;
107
108 server::Options serverOpts;
109 serverOpts.prefix = "ndn:/test-prefix";
Davide Pesavento5923bf82018-08-09 01:55:44 -0400110 serverOpts.freshnessPeriod = 5_s;
Eric Newberrya93680e2015-07-15 19:25:29 -0700111 serverOpts.nMaxPings = 4;
Davide Pesavento5923bf82018-08-09 01:55:44 -0400112 serverOpts.wantTimestamp = false;
Eric Newberrya93680e2015-07-15 19:25:29 -0700113 serverOpts.payloadSize = 0;
Davide Pesavento5923bf82018-08-09 01:55:44 -0400114 server = make_unique<server::PingServer>(serverFace, m_keyChain, serverOpts);
Eric Newberrya93680e2015-07-15 19:25:29 -0700115 BOOST_REQUIRE_EQUAL(0, server->getNPings());
116 server->start();
117
118 client::Options clientOpts;
119 clientOpts.prefix = "ndn:/test-prefix";
120 clientOpts.shouldAllowStaleData = false;
121 clientOpts.shouldGenerateRandomSeq = false;
122 clientOpts.shouldPrintTimestamp = false;
123 clientOpts.nPings = 4;
Davide Pesavento5923bf82018-08-09 01:55:44 -0400124 clientOpts.interval = 100_ms;
125 clientOpts.timeout = 500_ms;
Eric Newberrya93680e2015-07-15 19:25:29 -0700126 clientOpts.startSeq = 1000;
Davide Pesavento5923bf82018-08-09 01:55:44 -0400127 client = make_unique<client::Ping>(clientFace, clientOpts);
Teng Liang62884862016-03-31 18:15:36 -0700128 client->afterFinish.connect(bind(&PingIntegratedFixture::onFinish, this));
Eric Newberrya93680e2015-07-15 19:25:29 -0700129 client->start();
130
Davide Pesavento5923bf82018-08-09 01:55:44 -0400131 advanceClocks(io, 1_ms, 1000);
Eric Newberrya93680e2015-07-15 19:25:29 -0700132 io.run();
133
Davide Pesavento5923bf82018-08-09 01:55:44 -0400134 BOOST_CHECK_EQUAL(0, server->getNPings());
Eric Newberrya93680e2015-07-15 19:25:29 -0700135}
136
Davide Pesavento013de9b2016-09-01 12:06:56 +0000137BOOST_AUTO_TEST_SUITE_END() // TestIntegrated
138BOOST_AUTO_TEST_SUITE_END() // Ping
Eric Newberrya93680e2015-07-15 19:25:29 -0700139
140} // namespace tests
141} // namespace ping
142} // namespace ndn