blob: cef62d680c55a78b50aeb079419d1ba2e9dd7c11 [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/*
Davide Pesavento66777622020-10-09 18:46:03 -04003 * Copyright (c) 2014-2020, 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 Pesavento66777622020-10-09 18:46:03 -040023#include "tests/io-fixture.hpp"
24
Davide Pesavento013de9b2016-09-01 12:06:56 +000025#include <ndn-cxx/util/dummy-client-face.hpp>
Eric Newberrya93680e2015-07-15 19:25:29 -070026
27namespace ndn {
28namespace ping {
29namespace client {
30namespace tests {
31
32using namespace ndn::tests;
33
Davide Pesavento013de9b2016-09-01 12:06:56 +000034BOOST_AUTO_TEST_SUITE(Ping)
35BOOST_AUTO_TEST_SUITE(TestPing)
36
37using ping::client::Ping;
Eric Newberrya93680e2015-07-15 19:25:29 -070038
Davide Pesavento66777622020-10-09 18:46:03 -040039BOOST_FIXTURE_TEST_CASE(Basic, IoFixture)
Eric Newberrya93680e2015-07-15 19:25:29 -070040{
Davide Pesavento66777622020-10-09 18:46:03 -040041 util::DummyClientFace face(m_io, {true, true});
Eric Newberrya93680e2015-07-15 19:25:29 -070042 Options pingOptions;
Davide Pesavento66777622020-10-09 18:46:03 -040043 pingOptions.prefix = "/test-prefix";
Teng Liang62884862016-03-31 18:15:36 -070044 pingOptions.shouldAllowStaleData = false;
45 pingOptions.shouldGenerateRandomSeq = false;
46 pingOptions.shouldPrintTimestamp = false;
47 pingOptions.nPings = 4;
Davide Pesavento66777622020-10-09 18:46:03 -040048 pingOptions.interval = 100_ms;
49 pingOptions.timeout = 2_s;
Teng Liang62884862016-03-31 18:15:36 -070050 pingOptions.startSeq = 1000;
Teng Liang62884862016-03-31 18:15:36 -070051 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
Davide Pesavento66777622020-10-09 18:46:03 -040076 this->advanceClocks(1_ms, 500);
Teng Liang62884862016-03-31 18:15:36 -070077 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
Davide Pesavento66777622020-10-09 18:46:03 -040083 face.receive(makeNack(face.sentInterests[1], lp::NackReason::DUPLICATE));
Eric Newberrya93680e2015-07-15 19:25:29 -070084
Junxiao Shi96192952019-05-22 15:45:12 +000085 data = makeData("/test-prefix/ping/1002");
86 data->setFreshnessPeriod(1_s);
87 face.receive(*data);
Teng Liang62884862016-03-31 18:15:36 -070088
Davide Pesavento66777622020-10-09 18:46:03 -040089 this->advanceClocks(100_ms, 20);
Teng Liang62884862016-03-31 18:15:36 -070090
Davide Pesavento66777622020-10-09 18:46:03 -040091 // /test-prefix/ping/1003 is unanswered and will timeout
Teng Liang62884862016-03-31 18:15:36 -070092
93 BOOST_CHECK_EQUAL(nFinishSignals, 1);
Eric Newberrya93680e2015-07-15 19:25:29 -070094}
95
Davide Pesavento013de9b2016-09-01 12:06:56 +000096BOOST_AUTO_TEST_SUITE_END() // TestPing
97BOOST_AUTO_TEST_SUITE_END() // Ping
Eric Newberrya93680e2015-07-15 19:25:29 -070098
99} // namespace tests
100} // namespace client
101} // namespace ping
102} // namespace ndn