blob: 9e929aa216b0caba96cc66adda6a6435d5b4a756 [file] [log] [blame]
Eric Newberrya93680e2015-07-15 19:25:29 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Teng Liang62884862016-03-31 18:15:36 -07003 * Copyright (c) 2014-2016, 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";
77 serverOpts.freshnessPeriod = time::milliseconds(5000);
78 serverOpts.nMaxPings = 4;
79 serverOpts.shouldPrintTimestamp = false;
80 serverOpts.payloadSize = 0;
Junxiao Shiaa1b3c92016-07-14 14:56:53 +000081 server.reset(new 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;
91 clientOpts.interval = time::milliseconds(100);
92 clientOpts.timeout = time::milliseconds(2000);
93 clientOpts.startSeq = 1000;
Junxiao Shiaa1b3c92016-07-14 14:56:53 +000094 client.reset(new 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
98 this->advanceClocks(io, time::milliseconds(1), 400);
99 io.run();
100
101 BOOST_REQUIRE_EQUAL(4, server->getNPings());
102}
103
104BOOST_FIXTURE_TEST_CASE(Timeout, PingIntegratedFixture)
105{
106 wantLoss = true;
107
108 server::Options serverOpts;
109 serverOpts.prefix = "ndn:/test-prefix";
110 serverOpts.freshnessPeriod = time::milliseconds(5000);
111 serverOpts.nMaxPings = 4;
112 serverOpts.shouldPrintTimestamp = false;
113 serverOpts.payloadSize = 0;
Junxiao Shiaa1b3c92016-07-14 14:56:53 +0000114 server.reset(new 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;
124 clientOpts.interval = time::milliseconds(100);
125 clientOpts.timeout = time::milliseconds(500);
126 clientOpts.startSeq = 1000;
Junxiao Shiaa1b3c92016-07-14 14:56:53 +0000127 client.reset(new 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
131 this->advanceClocks(io, time::milliseconds(1), 1000);
132 io.run();
133
134 BOOST_REQUIRE_EQUAL(0, server->getNPings());
135}
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