blob: 67d7ddc700cf68c3fc27774983f085d7eb35aa71 [file] [log] [blame]
Eric Newberrya93680e2015-07-15 19:25:29 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi96192952019-05-22 15:45:12 +00002/*
3 * Copyright (c) 2014-2019, 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/client/ping.hpp"
Eric Newberrya93680e2015-07-15 19:25:29 -070021
22#include "tests/test-common.hpp"
Davide Pesavento013de9b2016-09-01 12:06:56 +000023#include <ndn-cxx/util/dummy-client-face.hpp>
Eric Newberrya93680e2015-07-15 19:25:29 -070024
25namespace ndn {
26namespace ping {
27namespace client {
28namespace tests {
29
30using namespace ndn::tests;
31
Davide Pesavento013de9b2016-09-01 12:06:56 +000032BOOST_AUTO_TEST_SUITE(Ping)
33BOOST_AUTO_TEST_SUITE(TestPing)
34
35using ping::client::Ping;
Eric Newberrya93680e2015-07-15 19:25:29 -070036
Teng Liang62884862016-03-31 18:15:36 -070037BOOST_FIXTURE_TEST_CASE(Basic, UnitTestTimeFixture)
Eric Newberrya93680e2015-07-15 19:25:29 -070038{
Eric Newberrya93680e2015-07-15 19:25:29 -070039 Options pingOptions;
Teng Liang62884862016-03-31 18:15:36 -070040 pingOptions.prefix = "ndn:/test-prefix";
41 pingOptions.shouldAllowStaleData = false;
42 pingOptions.shouldGenerateRandomSeq = false;
43 pingOptions.shouldPrintTimestamp = false;
44 pingOptions.nPings = 4;
45 pingOptions.interval = time::milliseconds(100);
46 pingOptions.timeout = time::milliseconds(2000);
47 pingOptions.startSeq = 1000;
Eric Newberrya93680e2015-07-15 19:25:29 -070048
Teng Liang62884862016-03-31 18:15:36 -070049 boost::asio::io_service io;
50 util::DummyClientFace face(io, {true, true});
51 Ping ping(face, pingOptions);
52
53 int nFinishSignals = 0;
54 std::vector<uint64_t> dataSeqs;
55 std::vector<uint64_t> nackSeqs;
56 std::vector<uint64_t> timeoutSeqs;
57
58 ping.afterData.connect(bind([&] (uint64_t seq) { dataSeqs.push_back(seq); }, _1));
59 ping.afterNack.connect(bind([&] (uint64_t seq) { nackSeqs.push_back(seq); }, _1));
60 ping.afterTimeout.connect(bind([&] (uint64_t seq) { timeoutSeqs.push_back(seq); }, _1));
61 ping.afterFinish.connect(bind([&] {
62 BOOST_REQUIRE_EQUAL(dataSeqs.size(), 2);
63 BOOST_REQUIRE_EQUAL(nackSeqs.size(), 1);
64 BOOST_REQUIRE_EQUAL(timeoutSeqs.size(), 1);
65
66 BOOST_CHECK_EQUAL(dataSeqs[0], 1000);
67 BOOST_CHECK_EQUAL(nackSeqs[0], 1001);
68 BOOST_CHECK_EQUAL(dataSeqs[1], 1002);
69 BOOST_CHECK_EQUAL(timeoutSeqs[0], 1003);
70
71 nFinishSignals++;
72 }));
73
Eric Newberrya93680e2015-07-15 19:25:29 -070074 ping.start();
75
Teng Liang62884862016-03-31 18:15:36 -070076 this->advanceClocks(io, time::milliseconds(1), 500);
77 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 4);
Eric Newberrya93680e2015-07-15 19:25:29 -070078
Junxiao Shi96192952019-05-22 15:45:12 +000079 auto data = makeData("/test-prefix/ping/1000");
80 data->setFreshnessPeriod(1_s);
81 face.receive(*data);
Eric Newberrya93680e2015-07-15 19:25:29 -070082
Teng Liang62884862016-03-31 18:15:36 -070083 lp::Nack nack(face.sentInterests[1]);
84 nack.setReason(lp::NackReason::DUPLICATE);
85 face.receive(nack);
Eric Newberrya93680e2015-07-15 19:25:29 -070086
Junxiao Shi96192952019-05-22 15:45:12 +000087 data = makeData("/test-prefix/ping/1002");
88 data->setFreshnessPeriod(1_s);
89 face.receive(*data);
Teng Liang62884862016-03-31 18:15:36 -070090
91 this->advanceClocks(io, time::milliseconds(100), 20);
92
93 // ndn:/test-prefix/ping/1003 is unanswered and will timeout
94
95 BOOST_CHECK_EQUAL(nFinishSignals, 1);
96
97 face.shutdown();
98 io.stop();
Eric Newberrya93680e2015-07-15 19:25:29 -070099}
100
Davide Pesavento013de9b2016-09-01 12:06:56 +0000101BOOST_AUTO_TEST_SUITE_END() // TestPing
102BOOST_AUTO_TEST_SUITE_END() // Ping
Eric Newberrya93680e2015-07-15 19:25:29 -0700103
104} // namespace tests
105} // namespace client
106} // namespace ping
107} // namespace ndn