blob: a00827194eef66c50506a2f6bfeac7b40c0edfd9 [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 Pesaventob3570c62022-02-19 19:19:00 -05003 * Copyright (c) 2014-2022, 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
Davide Pesaventob3570c62022-02-19 19:19:00 -050027namespace ndn::ping::client::tests {
Eric Newberrya93680e2015-07-15 19:25:29 -070028
29using namespace ndn::tests;
30
Davide Pesavento013de9b2016-09-01 12:06:56 +000031BOOST_AUTO_TEST_SUITE(Ping)
Davide Pesaventof8d9a532021-07-03 16:04:12 -040032BOOST_AUTO_TEST_SUITE(TestClient)
Davide Pesavento013de9b2016-09-01 12:06:56 +000033
Davide Pesaventof8d9a532021-07-03 16:04:12 -040034using client::Ping;
Eric Newberrya93680e2015-07-15 19:25:29 -070035
Davide Pesavento66777622020-10-09 18:46:03 -040036BOOST_FIXTURE_TEST_CASE(Basic, IoFixture)
Eric Newberrya93680e2015-07-15 19:25:29 -070037{
Davide Pesavento66777622020-10-09 18:46:03 -040038 util::DummyClientFace face(m_io, {true, true});
Eric Newberrya93680e2015-07-15 19:25:29 -070039 Options pingOptions;
Davide Pesavento66777622020-10-09 18:46:03 -040040 pingOptions.prefix = "/test-prefix";
Teng Liang62884862016-03-31 18:15:36 -070041 pingOptions.shouldAllowStaleData = false;
42 pingOptions.shouldGenerateRandomSeq = false;
43 pingOptions.shouldPrintTimestamp = false;
44 pingOptions.nPings = 4;
Davide Pesavento66777622020-10-09 18:46:03 -040045 pingOptions.interval = 100_ms;
46 pingOptions.timeout = 2_s;
Teng Liang62884862016-03-31 18:15:36 -070047 pingOptions.startSeq = 1000;
Teng Liang62884862016-03-31 18:15:36 -070048 Ping ping(face, pingOptions);
49
50 int nFinishSignals = 0;
51 std::vector<uint64_t> dataSeqs;
52 std::vector<uint64_t> nackSeqs;
53 std::vector<uint64_t> timeoutSeqs;
54
Davide Pesaventof8d9a532021-07-03 16:04:12 -040055 ping.afterData.connect([&] (uint64_t seq, auto&&...) { dataSeqs.push_back(seq); });
56 ping.afterNack.connect([&] (uint64_t seq, auto&&...) { nackSeqs.push_back(seq); });
57 ping.afterTimeout.connect([&] (uint64_t seq, auto&&...) { timeoutSeqs.push_back(seq); });
58 ping.afterFinish.connect([&] {
Teng Liang62884862016-03-31 18:15:36 -070059 BOOST_REQUIRE_EQUAL(dataSeqs.size(), 2);
60 BOOST_REQUIRE_EQUAL(nackSeqs.size(), 1);
61 BOOST_REQUIRE_EQUAL(timeoutSeqs.size(), 1);
62
63 BOOST_CHECK_EQUAL(dataSeqs[0], 1000);
64 BOOST_CHECK_EQUAL(nackSeqs[0], 1001);
65 BOOST_CHECK_EQUAL(dataSeqs[1], 1002);
66 BOOST_CHECK_EQUAL(timeoutSeqs[0], 1003);
67
68 nFinishSignals++;
Davide Pesaventof8d9a532021-07-03 16:04:12 -040069 });
Teng Liang62884862016-03-31 18:15:36 -070070
Eric Newberrya93680e2015-07-15 19:25:29 -070071 ping.start();
72
Davide Pesavento66777622020-10-09 18:46:03 -040073 this->advanceClocks(1_ms, 500);
Teng Liang62884862016-03-31 18:15:36 -070074 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 4);
Eric Newberrya93680e2015-07-15 19:25:29 -070075
Junxiao Shi96192952019-05-22 15:45:12 +000076 auto data = makeData("/test-prefix/ping/1000");
77 data->setFreshnessPeriod(1_s);
78 face.receive(*data);
Eric Newberrya93680e2015-07-15 19:25:29 -070079
Davide Pesavento66777622020-10-09 18:46:03 -040080 face.receive(makeNack(face.sentInterests[1], lp::NackReason::DUPLICATE));
Eric Newberrya93680e2015-07-15 19:25:29 -070081
Junxiao Shi96192952019-05-22 15:45:12 +000082 data = makeData("/test-prefix/ping/1002");
83 data->setFreshnessPeriod(1_s);
84 face.receive(*data);
Teng Liang62884862016-03-31 18:15:36 -070085
Davide Pesavento66777622020-10-09 18:46:03 -040086 this->advanceClocks(100_ms, 20);
Teng Liang62884862016-03-31 18:15:36 -070087
Davide Pesavento66777622020-10-09 18:46:03 -040088 // /test-prefix/ping/1003 is unanswered and will timeout
Teng Liang62884862016-03-31 18:15:36 -070089
90 BOOST_CHECK_EQUAL(nFinishSignals, 1);
Eric Newberrya93680e2015-07-15 19:25:29 -070091}
92
Davide Pesaventof8d9a532021-07-03 16:04:12 -040093BOOST_AUTO_TEST_SUITE_END() // TestClient
Davide Pesavento013de9b2016-09-01 12:06:56 +000094BOOST_AUTO_TEST_SUITE_END() // Ping
Eric Newberrya93680e2015-07-15 19:25:29 -070095
Davide Pesaventob3570c62022-02-19 19:19:00 -050096} // namespace ndn::ping::client::tests