blob: fb6f1ccab36bffbef41a0da0766130307e773634 [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"
22#include <ndn-cxx/util/dummy-client-face.hpp>
23
24#include "tests/test-common.hpp"
Alexander Afanasyev1e7a7b22015-08-26 15:35:26 -070025#include "../identity-management-time-fixture.hpp"
Eric Newberrya93680e2015-07-15 19:25:29 -070026
27namespace ndn {
28namespace ping {
29namespace tests {
30
31using namespace ndn::tests;
32
Alexander Afanasyev1e7a7b22015-08-26 15:35:26 -070033class PingIntegratedFixture : public IdentityManagementTimeFixture
Eric Newberrya93680e2015-07-15 19:25:29 -070034{
35public:
36 PingIntegratedFixture()
Junxiao Shiaa1b3c92016-07-14 14:56:53 +000037 : serverFace(io, m_keyChain, {false, true})
38 , clientFace(io, m_keyChain, {false, true})
Eric Newberrya93680e2015-07-15 19:25:29 -070039 , wantLoss(false)
40 {
Junxiao Shiaa1b3c92016-07-14 14:56:53 +000041 serverFace.onSendInterest.connect([this] (const Interest& interest) {
42 io.post([=] { if (!wantLoss) { clientFace.receive(interest); } });
Eric Newberrya93680e2015-07-15 19:25:29 -070043 });
Junxiao Shiaa1b3c92016-07-14 14:56:53 +000044 clientFace.onSendInterest.connect([this] (const Interest& interest) {
45 io.post([=] { if (!wantLoss) { serverFace.receive(interest); } });
Eric Newberrya93680e2015-07-15 19:25:29 -070046 });
Junxiao Shiaa1b3c92016-07-14 14:56:53 +000047 serverFace.onSendData.connect([this] (const Data& data) {
48 io.post([=] { if (!wantLoss) { clientFace.receive(data); } });
Eric Newberrya93680e2015-07-15 19:25:29 -070049 });
Junxiao Shiaa1b3c92016-07-14 14:56:53 +000050 clientFace.onSendData.connect([this] (const Data& data) {
51 io.post([=] { if (!wantLoss) { serverFace.receive(data); } });
Eric Newberrya93680e2015-07-15 19:25:29 -070052 });
53 }
54
Teng Liang62884862016-03-31 18:15:36 -070055 void onFinish()
Eric Newberrya93680e2015-07-15 19:25:29 -070056 {
Junxiao Shiaa1b3c92016-07-14 14:56:53 +000057 serverFace.shutdown();
58 clientFace.shutdown();
Teng Liang62884862016-03-31 18:15:36 -070059 io.stop();
Eric Newberrya93680e2015-07-15 19:25:29 -070060 }
61
62public:
63 boost::asio::io_service io;
Junxiao Shiaa1b3c92016-07-14 14:56:53 +000064 util::DummyClientFace serverFace;
65 util::DummyClientFace clientFace;
Eric Newberrya93680e2015-07-15 19:25:29 -070066 std::unique_ptr<server::PingServer> server;
67 std::unique_ptr<client::Ping> client;
Eric Newberrya93680e2015-07-15 19:25:29 -070068 bool wantLoss;
69};
70
71BOOST_AUTO_TEST_SUITE(PingIntegrated)
72
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
137BOOST_AUTO_TEST_SUITE_END()
138
139} // namespace tests
140} // namespace ping
141} // namespace ndn