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