Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2019, Regents of the University of California, |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis. |
| 10 | * |
| 11 | * This file is part of ndn-tools (Named Data Networking Essential Tools). |
| 12 | * See AUTHORS.md for complete list of ndn-tools authors and contributors. |
| 13 | * |
| 14 | * ndn-tools is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * ndn-tools is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * ndn-tools, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 24 | */ |
| 25 | |
| 26 | #include "tools/peek/ndnpoke/ndnpoke.hpp" |
| 27 | |
| 28 | #include "tests/test-common.hpp" |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame] | 29 | #include "tests/identity-management-fixture.hpp" |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 30 | |
| 31 | #include <ndn-cxx/util/dummy-client-face.hpp> |
| 32 | |
| 33 | namespace ndn { |
| 34 | namespace peek { |
| 35 | namespace tests { |
| 36 | |
| 37 | using namespace ndn::tests; |
| 38 | |
Davide Pesavento | 87434be | 2019-07-25 19:04:23 -0400 | [diff] [blame] | 39 | template<bool WANT_PREFIX_REG_REPLY = true> |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame] | 40 | class NdnPokeFixture : public IdentityManagementTimeFixture |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 41 | { |
| 42 | protected: |
| 43 | NdnPokeFixture() |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 44 | { |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame] | 45 | m_keyChain.createIdentity("/test-id"); |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | void |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame] | 49 | initialize(const PokeOptions& opts = makeDefaultOptions()) |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 50 | { |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame] | 51 | poke = make_unique<NdnPoke>(face, m_keyChain, payload, opts); |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | static PokeOptions |
| 55 | makeDefaultOptions() |
| 56 | { |
| 57 | PokeOptions opt; |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame] | 58 | opt.name = "/poke/test"; |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 59 | return opt; |
| 60 | } |
| 61 | |
| 62 | protected: |
| 63 | boost::asio::io_service io; |
Davide Pesavento | 87434be | 2019-07-25 19:04:23 -0400 | [diff] [blame] | 64 | ndn::util::DummyClientFace face{io, m_keyChain, {true, WANT_PREFIX_REG_REPLY}}; |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame] | 65 | std::stringstream payload{"Hello, world!\n"}; |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 66 | unique_ptr<NdnPoke> poke; |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 67 | }; |
| 68 | |
| 69 | BOOST_AUTO_TEST_SUITE(Peek) |
Davide Pesavento | 87434be | 2019-07-25 19:04:23 -0400 | [diff] [blame] | 70 | BOOST_FIXTURE_TEST_SUITE(TestNdnPoke, NdnPokeFixture<>) |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 71 | |
| 72 | BOOST_AUTO_TEST_CASE(Basic) |
| 73 | { |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame] | 74 | initialize(); |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 75 | |
| 76 | poke->start(); |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 77 | this->advanceClocks(io, 1_ms, 10); |
| 78 | |
Eric Newberry | 468dbdf | 2018-06-20 23:58:55 -0700 | [diff] [blame] | 79 | // Check for prefix registration |
| 80 | BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1); |
| 81 | BOOST_CHECK_EQUAL(face.sentInterests.front().getName().getPrefix(4), "/localhost/nfd/rib/register"); |
| 82 | |
Junxiao Shi | 20d5a0b | 2018-08-14 09:26:11 -0600 | [diff] [blame] | 83 | face.receive(*makeInterest("/poke/test")); |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 84 | this->advanceClocks(io, 1_ms, 10); |
| 85 | io.run(); |
| 86 | |
Davide Pesavento | 87434be | 2019-07-25 19:04:23 -0400 | [diff] [blame] | 87 | BOOST_CHECK(poke->getResult() == NdnPoke::Result::DATA_SENT); |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 88 | BOOST_REQUIRE_EQUAL(face.sentData.size(), 1); |
| 89 | BOOST_CHECK_EQUAL(face.sentData.back().getName(), "/poke/test"); |
| 90 | BOOST_CHECK(!face.sentData.back().getFinalBlock()); |
| 91 | BOOST_CHECK_EQUAL(face.sentData.back().getFreshnessPeriod(), 0_ms); |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame] | 92 | BOOST_CHECK_EQUAL(face.sentData.back().getContentType(), tlv::ContentType_Blob); |
| 93 | BOOST_CHECK_EQUAL(face.sentData.back().getContent(), "150E48656C6C6F2C20776F726C64210A"_block); |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 94 | BOOST_CHECK_EQUAL(face.sentData.back().getSignature().getType(), tlv::SignatureSha256WithEcdsa); |
Eric Newberry | 468dbdf | 2018-06-20 23:58:55 -0700 | [diff] [blame] | 95 | |
| 96 | // Check for prefix unregistration |
| 97 | BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 2); // One for registration, one for unregistration |
| 98 | BOOST_CHECK_EQUAL(face.sentInterests.back().getName().getPrefix(4), "/localhost/nfd/rib/unregister"); |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 101 | BOOST_AUTO_TEST_CASE(FreshnessPeriod) |
| 102 | { |
| 103 | auto options = makeDefaultOptions(); |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame] | 104 | options.freshnessPeriod = 1_s; |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 105 | initialize(options); |
| 106 | |
| 107 | poke->start(); |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 108 | this->advanceClocks(io, 1_ms, 10); |
| 109 | |
Junxiao Shi | 20d5a0b | 2018-08-14 09:26:11 -0600 | [diff] [blame] | 110 | face.receive(*makeInterest("/poke/test")); |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 111 | this->advanceClocks(io, 1_ms, 10); |
| 112 | io.run(); |
| 113 | |
Davide Pesavento | 87434be | 2019-07-25 19:04:23 -0400 | [diff] [blame] | 114 | BOOST_CHECK(poke->getResult() == NdnPoke::Result::DATA_SENT); |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 115 | BOOST_REQUIRE_EQUAL(face.sentData.size(), 1); |
| 116 | BOOST_CHECK_EQUAL(face.sentData.back().getName(), "/poke/test"); |
| 117 | BOOST_CHECK(!face.sentData.back().getFinalBlock()); |
| 118 | BOOST_CHECK_EQUAL(face.sentData.back().getFreshnessPeriod(), 1_s); |
| 119 | BOOST_CHECK_EQUAL(face.sentData.back().getSignature().getType(), tlv::SignatureSha256WithEcdsa); |
| 120 | } |
| 121 | |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame] | 122 | BOOST_AUTO_TEST_CASE(FinalBlockId) |
| 123 | { |
| 124 | auto options = makeDefaultOptions(); |
| 125 | options.name = "/poke/test/123"; |
| 126 | options.wantFinalBlockId = true; |
| 127 | initialize(options); |
| 128 | |
| 129 | poke->start(); |
| 130 | this->advanceClocks(io, 1_ms, 10); |
| 131 | |
| 132 | face.receive(*makeInterest(options.name)); |
| 133 | this->advanceClocks(io, 1_ms, 10); |
| 134 | io.run(); |
| 135 | |
Davide Pesavento | 87434be | 2019-07-25 19:04:23 -0400 | [diff] [blame] | 136 | BOOST_CHECK(poke->getResult() == NdnPoke::Result::DATA_SENT); |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame] | 137 | BOOST_REQUIRE_EQUAL(face.sentData.size(), 1); |
| 138 | BOOST_CHECK_EQUAL(face.sentData.back().getName(), options.name); |
| 139 | BOOST_REQUIRE(face.sentData.back().getFinalBlock()); |
| 140 | BOOST_CHECK_EQUAL(*(face.sentData.back().getFinalBlock()), name::Component("123")); |
| 141 | BOOST_CHECK_EQUAL(face.sentData.back().getFreshnessPeriod(), 0_ms); |
| 142 | BOOST_CHECK_EQUAL(face.sentData.back().getSignature().getType(), tlv::SignatureSha256WithEcdsa); |
| 143 | } |
| 144 | |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 145 | BOOST_AUTO_TEST_CASE(DigestSha256) |
| 146 | { |
| 147 | auto options = makeDefaultOptions(); |
| 148 | options.signingInfo.setSha256Signing(); |
| 149 | initialize(options); |
| 150 | |
| 151 | poke->start(); |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 152 | this->advanceClocks(io, 1_ms, 10); |
| 153 | |
Junxiao Shi | 20d5a0b | 2018-08-14 09:26:11 -0600 | [diff] [blame] | 154 | face.receive(*makeInterest("/poke/test")); |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 155 | this->advanceClocks(io, 1_ms, 10); |
| 156 | io.run(); |
| 157 | |
Davide Pesavento | 87434be | 2019-07-25 19:04:23 -0400 | [diff] [blame] | 158 | BOOST_CHECK(poke->getResult() == NdnPoke::Result::DATA_SENT); |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 159 | BOOST_REQUIRE_EQUAL(face.sentData.size(), 1); |
| 160 | BOOST_CHECK_EQUAL(face.sentData.back().getName(), "/poke/test"); |
| 161 | BOOST_CHECK(!face.sentData.back().getFinalBlock()); |
| 162 | BOOST_CHECK_EQUAL(face.sentData.back().getFreshnessPeriod(), 0_ms); |
| 163 | BOOST_CHECK_EQUAL(face.sentData.back().getSignature().getType(), tlv::DigestSha256); |
| 164 | } |
| 165 | |
| 166 | BOOST_AUTO_TEST_CASE(ForceData) |
| 167 | { |
| 168 | auto options = makeDefaultOptions(); |
| 169 | options.wantForceData = true; |
| 170 | initialize(options); |
| 171 | |
| 172 | poke->start(); |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 173 | this->advanceClocks(io, 1_ms, 10); |
| 174 | |
Davide Pesavento | 87434be | 2019-07-25 19:04:23 -0400 | [diff] [blame] | 175 | BOOST_CHECK(poke->getResult() == NdnPoke::Result::DATA_SENT); |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 176 | BOOST_REQUIRE_EQUAL(face.sentData.size(), 1); |
| 177 | BOOST_CHECK_EQUAL(face.sentData.back().getName(), "/poke/test"); |
| 178 | BOOST_CHECK(!face.sentData.back().getFinalBlock()); |
| 179 | BOOST_CHECK_EQUAL(face.sentData.back().getFreshnessPeriod(), 0_ms); |
| 180 | BOOST_CHECK_EQUAL(face.sentData.back().getSignature().getType(), tlv::SignatureSha256WithEcdsa); |
| 181 | } |
| 182 | |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame] | 183 | BOOST_AUTO_TEST_CASE(Timeout) |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 184 | { |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 185 | auto options = makeDefaultOptions(); |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame] | 186 | options.timeout = 4_s; |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 187 | initialize(options); |
| 188 | |
| 189 | poke->start(); |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame] | 190 | this->advanceClocks(io, 1_ms, 10); |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 191 | |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame] | 192 | // Check for prefix registration |
| 193 | BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1); |
| 194 | BOOST_CHECK_EQUAL(face.sentInterests.front().getName().getPrefix(4), "/localhost/nfd/rib/register"); |
| 195 | |
| 196 | this->advanceClocks(io, 1_s, 4); |
| 197 | |
Davide Pesavento | 94dff00 | 2019-07-24 23:35:05 -0400 | [diff] [blame^] | 198 | BOOST_CHECK(poke->getResult() == NdnPoke::Result::TIMEOUT); |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame] | 199 | BOOST_CHECK_EQUAL(face.sentData.size(), 0); |
| 200 | |
| 201 | // Check for prefix unregistration |
| 202 | BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 2); // One for registration, one for unregistration |
| 203 | BOOST_CHECK_EQUAL(face.sentInterests.back().getName().getPrefix(4), "/localhost/nfd/rib/unregister"); |
| 204 | } |
| 205 | |
Davide Pesavento | 87434be | 2019-07-25 19:04:23 -0400 | [diff] [blame] | 206 | BOOST_FIXTURE_TEST_CASE(PrefixRegTimeout, NdnPokeFixture<false>) |
| 207 | { |
| 208 | initialize(); |
| 209 | |
| 210 | poke->start(); |
| 211 | this->advanceClocks(io, 1_ms, 10); |
| 212 | |
| 213 | // Check for prefix registration |
| 214 | BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1); |
| 215 | BOOST_CHECK_EQUAL(face.sentInterests.front().getName().getPrefix(4), "/localhost/nfd/rib/register"); |
| 216 | |
| 217 | this->advanceClocks(io, 1_s, 10); |
| 218 | |
| 219 | BOOST_CHECK(poke->getResult() == NdnPoke::Result::PREFIX_REG_FAIL); |
| 220 | BOOST_CHECK_EQUAL(face.sentData.size(), 0); |
| 221 | } |
| 222 | |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame] | 223 | BOOST_AUTO_TEST_CASE(OversizedPacket) |
| 224 | { |
| 225 | payload << std::string(MAX_NDN_PACKET_SIZE, 'A'); |
| 226 | initialize(); |
| 227 | |
| 228 | poke->start(); |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 229 | this->advanceClocks(io, 1_ms, 10); |
| 230 | |
Junxiao Shi | 20d5a0b | 2018-08-14 09:26:11 -0600 | [diff] [blame] | 231 | face.receive(*makeInterest("/poke/test")); |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 232 | BOOST_CHECK_THROW(face.processEvents(), Face::OversizedPacketError); |
| 233 | |
Davide Pesavento | 87434be | 2019-07-25 19:04:23 -0400 | [diff] [blame] | 234 | // No point in checking getResult() here. The exception is thrown from processEvents(), |
| 235 | // not from put(), so the result is still DATA_SENT even though no packets were sent. |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 236 | BOOST_CHECK_EQUAL(face.sentData.size(), 0); |
| 237 | } |
| 238 | |
| 239 | BOOST_AUTO_TEST_SUITE_END() // TestNdnPoke |
| 240 | BOOST_AUTO_TEST_SUITE_END() // Peek |
| 241 | |
| 242 | } // namespace tests |
| 243 | } // namespace peek |
| 244 | } // namespace ndn |