blob: b8e2164d033df508445ef1a99a4d0f3eff72720c [file] [log] [blame]
Eric Newberrya93680e2015-07-15 19:25:29 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014-2015, Arizona Board of Regents.
4 *
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()
37 : serverFace(util::makeDummyClientFace(io, {false, true}))
38 , clientFace(util::makeDummyClientFace(io, {false, true}))
39 , numResponses(0)
40 , wantLoss(false)
41 {
42 serverFace->onSendInterest.connect([this] (const Interest& interest) {
43 io.post([=] { if (!wantLoss) { clientFace->receive(interest); } });
44 });
45 clientFace->onSendInterest.connect([this] (const Interest& interest) {
46 io.post([=] { if (!wantLoss) { serverFace->receive(interest); } });
47 });
48 serverFace->onSendData.connect([this] (const Data& data) {
49 io.post([=] { if (!wantLoss) { clientFace->receive(data); } });
50 });
51 clientFace->onSendData.connect([this] (const Data& data) {
52 io.post([=] { if (!wantLoss) { serverFace->receive(data); } });
53 });
54 }
55
56 void onTimeout(uint64_t seq)
57 {
58 numResponses++;
59 if (numResponses == maxResponses) {
60 serverFace->shutdown();
61 clientFace->shutdown();
62 io.stop();
63 }
64 }
65
66 void onData(uint64_t seq)
67 {
68 numResponses++;
69 if (numResponses == maxResponses) {
70 serverFace->shutdown();
71 clientFace->shutdown();
72 io.stop();
73 }
74 }
75
76public:
77 boost::asio::io_service io;
78 shared_ptr<util::DummyClientFace> serverFace;
79 shared_ptr<util::DummyClientFace> clientFace;
80 std::unique_ptr<server::PingServer> server;
81 std::unique_ptr<client::Ping> client;
82 int maxResponses;
83 int numResponses;
84 bool wantLoss;
85};
86
87BOOST_AUTO_TEST_SUITE(PingIntegrated)
88
89BOOST_FIXTURE_TEST_CASE(Normal, PingIntegratedFixture)
90{
91 server::Options serverOpts;
92 serverOpts.prefix = "ndn:/test-prefix";
93 serverOpts.freshnessPeriod = time::milliseconds(5000);
94 serverOpts.nMaxPings = 4;
95 serverOpts.shouldPrintTimestamp = false;
96 serverOpts.payloadSize = 0;
Alexander Afanasyev1e7a7b22015-08-26 15:35:26 -070097 server.reset(new server::PingServer(*serverFace, m_keyChain, serverOpts));
Eric Newberrya93680e2015-07-15 19:25:29 -070098 BOOST_REQUIRE_EQUAL(0, server->getNPings());
99 server->start();
100
101 client::Options clientOpts;
102 clientOpts.prefix = "ndn:/test-prefix";
103 clientOpts.shouldAllowStaleData = false;
104 clientOpts.shouldGenerateRandomSeq = false;
105 clientOpts.shouldPrintTimestamp = false;
106 clientOpts.nPings = 4;
107 clientOpts.interval = time::milliseconds(100);
108 clientOpts.timeout = time::milliseconds(2000);
109 clientOpts.startSeq = 1000;
110 client.reset(new client::Ping(*clientFace, clientOpts));
111 client->afterResponse.connect(bind(&PingIntegratedFixture::onData, this, _1));
112 client->afterTimeout.connect(bind(&PingIntegratedFixture::onTimeout, this, _1));
113 maxResponses = 4;
114 client->start();
115
116 this->advanceClocks(io, time::milliseconds(1), 400);
117 io.run();
118
119 BOOST_REQUIRE_EQUAL(4, server->getNPings());
120}
121
122BOOST_FIXTURE_TEST_CASE(Timeout, PingIntegratedFixture)
123{
124 wantLoss = true;
125
126 server::Options serverOpts;
127 serverOpts.prefix = "ndn:/test-prefix";
128 serverOpts.freshnessPeriod = time::milliseconds(5000);
129 serverOpts.nMaxPings = 4;
130 serverOpts.shouldPrintTimestamp = false;
131 serverOpts.payloadSize = 0;
Alexander Afanasyev1e7a7b22015-08-26 15:35:26 -0700132 server.reset(new server::PingServer(*serverFace, m_keyChain, serverOpts));
Eric Newberrya93680e2015-07-15 19:25:29 -0700133 BOOST_REQUIRE_EQUAL(0, server->getNPings());
134 server->start();
135
136 client::Options clientOpts;
137 clientOpts.prefix = "ndn:/test-prefix";
138 clientOpts.shouldAllowStaleData = false;
139 clientOpts.shouldGenerateRandomSeq = false;
140 clientOpts.shouldPrintTimestamp = false;
141 clientOpts.nPings = 4;
142 clientOpts.interval = time::milliseconds(100);
143 clientOpts.timeout = time::milliseconds(500);
144 clientOpts.startSeq = 1000;
145 client.reset(new client::Ping(*clientFace, clientOpts));
146 numResponses = 0;
147 maxResponses = 4;
148 client->afterResponse.connect(bind(&PingIntegratedFixture::onData, this, _1));
149 client->afterTimeout.connect(bind(&PingIntegratedFixture::onTimeout, this, _1));
150 client->start();
151
152 this->advanceClocks(io, time::milliseconds(1), 1000);
153 io.run();
154
155 BOOST_REQUIRE_EQUAL(0, server->getNPings());
156}
157
158BOOST_AUTO_TEST_SUITE_END()
159
160} // namespace tests
161} // namespace ping
162} // namespace ndn