Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 5923bf8 | 2018-08-09 01:55:44 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame] | 3 | * Copyright (c) 2015-2022, Arizona Board of Regents. |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 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 | |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 20 | #include "tools/ping/client/ping.hpp" |
Davide Pesavento | 6677762 | 2020-10-09 18:46:03 -0400 | [diff] [blame] | 21 | #include "tools/ping/server/ping-server.hpp" |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 22 | |
Davide Pesavento | 6677762 | 2020-10-09 18:46:03 -0400 | [diff] [blame] | 23 | #include "tests/test-common.hpp" |
| 24 | #include "tests/io-fixture.hpp" |
| 25 | #include "tests/key-chain-fixture.hpp" |
| 26 | |
Davide Pesavento | 013de9b | 2016-09-01 12:06:56 +0000 | [diff] [blame] | 27 | #include <ndn-cxx/util/dummy-client-face.hpp> |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 28 | |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame] | 29 | namespace ndn::ping::tests { |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 30 | |
| 31 | using namespace ndn::tests; |
| 32 | |
Davide Pesavento | 6677762 | 2020-10-09 18:46:03 -0400 | [diff] [blame] | 33 | class PingIntegratedFixture : public IoFixture, public KeyChainFixture |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 34 | { |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame] | 35 | protected: |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 36 | PingIntegratedFixture() |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 37 | { |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame] | 38 | serverFace.onSendInterest.connect([this] (const auto& interest) { |
| 39 | receive(clientFace, interest); |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 40 | }); |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame] | 41 | clientFace.onSendInterest.connect([this] (const auto& interest) { |
| 42 | receive(serverFace, interest); |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 43 | }); |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame] | 44 | serverFace.onSendData.connect([this] (const auto& data) { |
| 45 | receive(clientFace, data); |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 46 | }); |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame] | 47 | clientFace.onSendData.connect([this] (const auto& data) { |
| 48 | receive(serverFace, data); |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 49 | }); |
| 50 | } |
| 51 | |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame] | 52 | template<typename Packet> |
| 53 | void |
| 54 | receive(util::DummyClientFace& face, const Packet& pkt) |
| 55 | { |
| 56 | m_io.post([=, &face] { |
| 57 | if (!wantLoss) { |
| 58 | face.receive(pkt); |
| 59 | } |
| 60 | }); |
| 61 | } |
| 62 | |
| 63 | void |
| 64 | onFinish() |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 65 | { |
Junxiao Shi | aa1b3c9 | 2016-07-14 14:56:53 +0000 | [diff] [blame] | 66 | serverFace.shutdown(); |
| 67 | clientFace.shutdown(); |
Davide Pesavento | 6677762 | 2020-10-09 18:46:03 -0400 | [diff] [blame] | 68 | m_io.stop(); |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 69 | } |
| 70 | |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame] | 71 | protected: |
| 72 | util::DummyClientFace serverFace{m_io, m_keyChain, {false, true}}; |
| 73 | util::DummyClientFace clientFace{m_io, m_keyChain, {false, true}}; |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 74 | std::unique_ptr<server::PingServer> server; |
| 75 | std::unique_ptr<client::Ping> client; |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame] | 76 | bool wantLoss = false; |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 77 | }; |
| 78 | |
Davide Pesavento | 013de9b | 2016-09-01 12:06:56 +0000 | [diff] [blame] | 79 | BOOST_AUTO_TEST_SUITE(Ping) |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame] | 80 | BOOST_FIXTURE_TEST_SUITE(TestIntegrated, PingIntegratedFixture) |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 81 | |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame] | 82 | BOOST_AUTO_TEST_CASE(Normal) |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 83 | { |
| 84 | server::Options serverOpts; |
| 85 | serverOpts.prefix = "ndn:/test-prefix"; |
Davide Pesavento | 5923bf8 | 2018-08-09 01:55:44 -0400 | [diff] [blame] | 86 | serverOpts.freshnessPeriod = 5_s; |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 87 | serverOpts.nMaxPings = 4; |
Davide Pesavento | 5923bf8 | 2018-08-09 01:55:44 -0400 | [diff] [blame] | 88 | serverOpts.wantTimestamp = false; |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 89 | serverOpts.payloadSize = 0; |
Davide Pesavento | 5923bf8 | 2018-08-09 01:55:44 -0400 | [diff] [blame] | 90 | server = make_unique<server::PingServer>(serverFace, m_keyChain, serverOpts); |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 91 | BOOST_REQUIRE_EQUAL(0, server->getNPings()); |
| 92 | server->start(); |
| 93 | |
| 94 | client::Options clientOpts; |
| 95 | clientOpts.prefix = "ndn:/test-prefix"; |
| 96 | clientOpts.shouldAllowStaleData = false; |
| 97 | clientOpts.shouldGenerateRandomSeq = false; |
| 98 | clientOpts.shouldPrintTimestamp = false; |
| 99 | clientOpts.nPings = 4; |
Davide Pesavento | 5923bf8 | 2018-08-09 01:55:44 -0400 | [diff] [blame] | 100 | clientOpts.interval = 100_ms; |
| 101 | clientOpts.timeout = 2_s; |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 102 | clientOpts.startSeq = 1000; |
Davide Pesavento | 5923bf8 | 2018-08-09 01:55:44 -0400 | [diff] [blame] | 103 | client = make_unique<client::Ping>(clientFace, clientOpts); |
Davide Pesavento | 6677762 | 2020-10-09 18:46:03 -0400 | [diff] [blame] | 104 | client->afterFinish.connect([this] { onFinish(); }); |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 105 | client->start(); |
| 106 | |
Davide Pesavento | 6677762 | 2020-10-09 18:46:03 -0400 | [diff] [blame] | 107 | advanceClocks(1_ms, 400); |
| 108 | m_io.run(); |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 109 | |
Davide Pesavento | 5923bf8 | 2018-08-09 01:55:44 -0400 | [diff] [blame] | 110 | BOOST_CHECK_EQUAL(4, server->getNPings()); |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 111 | } |
| 112 | |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame] | 113 | BOOST_AUTO_TEST_CASE(Timeout) |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 114 | { |
| 115 | wantLoss = true; |
| 116 | |
| 117 | server::Options serverOpts; |
| 118 | serverOpts.prefix = "ndn:/test-prefix"; |
Davide Pesavento | 5923bf8 | 2018-08-09 01:55:44 -0400 | [diff] [blame] | 119 | serverOpts.freshnessPeriod = 5_s; |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 120 | serverOpts.nMaxPings = 4; |
Davide Pesavento | 5923bf8 | 2018-08-09 01:55:44 -0400 | [diff] [blame] | 121 | serverOpts.wantTimestamp = false; |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 122 | serverOpts.payloadSize = 0; |
Davide Pesavento | 5923bf8 | 2018-08-09 01:55:44 -0400 | [diff] [blame] | 123 | server = make_unique<server::PingServer>(serverFace, m_keyChain, serverOpts); |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 124 | BOOST_REQUIRE_EQUAL(0, server->getNPings()); |
| 125 | server->start(); |
| 126 | |
| 127 | client::Options clientOpts; |
| 128 | clientOpts.prefix = "ndn:/test-prefix"; |
| 129 | clientOpts.shouldAllowStaleData = false; |
| 130 | clientOpts.shouldGenerateRandomSeq = false; |
| 131 | clientOpts.shouldPrintTimestamp = false; |
| 132 | clientOpts.nPings = 4; |
Davide Pesavento | 5923bf8 | 2018-08-09 01:55:44 -0400 | [diff] [blame] | 133 | clientOpts.interval = 100_ms; |
| 134 | clientOpts.timeout = 500_ms; |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 135 | clientOpts.startSeq = 1000; |
Davide Pesavento | 5923bf8 | 2018-08-09 01:55:44 -0400 | [diff] [blame] | 136 | client = make_unique<client::Ping>(clientFace, clientOpts); |
Davide Pesavento | 6677762 | 2020-10-09 18:46:03 -0400 | [diff] [blame] | 137 | client->afterFinish.connect([this] { onFinish(); }); |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 138 | client->start(); |
| 139 | |
Davide Pesavento | 6677762 | 2020-10-09 18:46:03 -0400 | [diff] [blame] | 140 | advanceClocks(1_ms, 1000); |
| 141 | m_io.run(); |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 142 | |
Davide Pesavento | 5923bf8 | 2018-08-09 01:55:44 -0400 | [diff] [blame] | 143 | BOOST_CHECK_EQUAL(0, server->getNPings()); |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Davide Pesavento | 013de9b | 2016-09-01 12:06:56 +0000 | [diff] [blame] | 146 | BOOST_AUTO_TEST_SUITE_END() // TestIntegrated |
| 147 | BOOST_AUTO_TEST_SUITE_END() // Ping |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 148 | |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame] | 149 | } // namespace ndn::ping::tests |