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