Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Teng Liang | 6288486 | 2016-03-31 18:15:36 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014-2016, 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" |
| 22 | #include <ndn-cxx/util/dummy-client-face.hpp> |
| 23 | |
| 24 | #include "tests/test-common.hpp" |
Alexander Afanasyev | 1e7a7b2 | 2015-08-26 15:35:26 -0700 | [diff] [blame] | 25 | #include "../identity-management-time-fixture.hpp" |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 26 | |
| 27 | namespace ndn { |
| 28 | namespace ping { |
| 29 | namespace tests { |
| 30 | |
| 31 | using namespace ndn::tests; |
| 32 | |
Alexander Afanasyev | 1e7a7b2 | 2015-08-26 15:35:26 -0700 | [diff] [blame] | 33 | class PingIntegratedFixture : public IdentityManagementTimeFixture |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 34 | { |
| 35 | public: |
| 36 | PingIntegratedFixture() |
Junxiao Shi | aa1b3c9 | 2016-07-14 14:56:53 +0000 | [diff] [blame] | 37 | : serverFace(io, m_keyChain, {false, true}) |
| 38 | , clientFace(io, m_keyChain, {false, true}) |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 39 | , wantLoss(false) |
| 40 | { |
Junxiao Shi | aa1b3c9 | 2016-07-14 14:56:53 +0000 | [diff] [blame] | 41 | serverFace.onSendInterest.connect([this] (const Interest& interest) { |
| 42 | io.post([=] { if (!wantLoss) { clientFace.receive(interest); } }); |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 43 | }); |
Junxiao Shi | aa1b3c9 | 2016-07-14 14:56:53 +0000 | [diff] [blame] | 44 | clientFace.onSendInterest.connect([this] (const Interest& interest) { |
| 45 | io.post([=] { if (!wantLoss) { serverFace.receive(interest); } }); |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 46 | }); |
Junxiao Shi | aa1b3c9 | 2016-07-14 14:56:53 +0000 | [diff] [blame] | 47 | serverFace.onSendData.connect([this] (const Data& data) { |
| 48 | io.post([=] { if (!wantLoss) { clientFace.receive(data); } }); |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 49 | }); |
Junxiao Shi | aa1b3c9 | 2016-07-14 14:56:53 +0000 | [diff] [blame] | 50 | clientFace.onSendData.connect([this] (const Data& data) { |
| 51 | io.post([=] { if (!wantLoss) { serverFace.receive(data); } }); |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 52 | }); |
| 53 | } |
| 54 | |
Teng Liang | 6288486 | 2016-03-31 18:15:36 -0700 | [diff] [blame] | 55 | void onFinish() |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 56 | { |
Junxiao Shi | aa1b3c9 | 2016-07-14 14:56:53 +0000 | [diff] [blame] | 57 | serverFace.shutdown(); |
| 58 | clientFace.shutdown(); |
Teng Liang | 6288486 | 2016-03-31 18:15:36 -0700 | [diff] [blame] | 59 | io.stop(); |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | public: |
| 63 | boost::asio::io_service io; |
Junxiao Shi | aa1b3c9 | 2016-07-14 14:56:53 +0000 | [diff] [blame] | 64 | util::DummyClientFace serverFace; |
| 65 | util::DummyClientFace clientFace; |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 66 | std::unique_ptr<server::PingServer> server; |
| 67 | std::unique_ptr<client::Ping> client; |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 68 | bool wantLoss; |
| 69 | }; |
| 70 | |
| 71 | BOOST_AUTO_TEST_SUITE(PingIntegrated) |
| 72 | |
| 73 | BOOST_FIXTURE_TEST_CASE(Normal, PingIntegratedFixture) |
| 74 | { |
| 75 | server::Options serverOpts; |
| 76 | serverOpts.prefix = "ndn:/test-prefix"; |
| 77 | serverOpts.freshnessPeriod = time::milliseconds(5000); |
| 78 | serverOpts.nMaxPings = 4; |
| 79 | serverOpts.shouldPrintTimestamp = false; |
| 80 | serverOpts.payloadSize = 0; |
Junxiao Shi | aa1b3c9 | 2016-07-14 14:56:53 +0000 | [diff] [blame] | 81 | server.reset(new 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; |
| 91 | clientOpts.interval = time::milliseconds(100); |
| 92 | clientOpts.timeout = time::milliseconds(2000); |
| 93 | clientOpts.startSeq = 1000; |
Junxiao Shi | aa1b3c9 | 2016-07-14 14:56:53 +0000 | [diff] [blame] | 94 | client.reset(new 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 | |
| 98 | this->advanceClocks(io, time::milliseconds(1), 400); |
| 99 | io.run(); |
| 100 | |
| 101 | BOOST_REQUIRE_EQUAL(4, server->getNPings()); |
| 102 | } |
| 103 | |
| 104 | BOOST_FIXTURE_TEST_CASE(Timeout, PingIntegratedFixture) |
| 105 | { |
| 106 | wantLoss = true; |
| 107 | |
| 108 | server::Options serverOpts; |
| 109 | serverOpts.prefix = "ndn:/test-prefix"; |
| 110 | serverOpts.freshnessPeriod = time::milliseconds(5000); |
| 111 | serverOpts.nMaxPings = 4; |
| 112 | serverOpts.shouldPrintTimestamp = false; |
| 113 | serverOpts.payloadSize = 0; |
Junxiao Shi | aa1b3c9 | 2016-07-14 14:56:53 +0000 | [diff] [blame] | 114 | server.reset(new 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; |
| 124 | clientOpts.interval = time::milliseconds(100); |
| 125 | clientOpts.timeout = time::milliseconds(500); |
| 126 | clientOpts.startSeq = 1000; |
Junxiao Shi | aa1b3c9 | 2016-07-14 14:56:53 +0000 | [diff] [blame] | 127 | client.reset(new 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 | |
| 131 | this->advanceClocks(io, time::milliseconds(1), 1000); |
| 132 | io.run(); |
| 133 | |
| 134 | BOOST_REQUIRE_EQUAL(0, server->getNPings()); |
| 135 | } |
| 136 | |
| 137 | BOOST_AUTO_TEST_SUITE_END() |
| 138 | |
| 139 | } // namespace tests |
| 140 | } // namespace ping |
| 141 | } // namespace ndn |