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 | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame^] | 39 | class NdnPokeFixture : public IdentityManagementTimeFixture |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 40 | { |
| 41 | protected: |
| 42 | NdnPokeFixture() |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 43 | { |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame^] | 44 | m_keyChain.createIdentity("/test-id"); |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | void |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame^] | 48 | initialize(const PokeOptions& opts = makeDefaultOptions()) |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 49 | { |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame^] | 50 | poke = make_unique<NdnPoke>(face, m_keyChain, payload, opts); |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | static PokeOptions |
| 54 | makeDefaultOptions() |
| 55 | { |
| 56 | PokeOptions opt; |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame^] | 57 | opt.name = "/poke/test"; |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 58 | return opt; |
| 59 | } |
| 60 | |
| 61 | protected: |
| 62 | boost::asio::io_service io; |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame^] | 63 | ndn::util::DummyClientFace face{io, m_keyChain, {true, true}}; |
| 64 | std::stringstream payload{"Hello, world!\n"}; |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 65 | unique_ptr<NdnPoke> poke; |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 66 | }; |
| 67 | |
| 68 | BOOST_AUTO_TEST_SUITE(Peek) |
| 69 | BOOST_FIXTURE_TEST_SUITE(TestNdnPoke, NdnPokeFixture) |
| 70 | |
| 71 | BOOST_AUTO_TEST_CASE(Basic) |
| 72 | { |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame^] | 73 | initialize(); |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 74 | |
| 75 | poke->start(); |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 76 | this->advanceClocks(io, 1_ms, 10); |
| 77 | |
Eric Newberry | 468dbdf | 2018-06-20 23:58:55 -0700 | [diff] [blame] | 78 | // Check for prefix registration |
| 79 | BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1); |
| 80 | BOOST_CHECK_EQUAL(face.sentInterests.front().getName().getPrefix(4), "/localhost/nfd/rib/register"); |
| 81 | |
Junxiao Shi | 20d5a0b | 2018-08-14 09:26:11 -0600 | [diff] [blame] | 82 | face.receive(*makeInterest("/poke/test")); |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 83 | this->advanceClocks(io, 1_ms, 10); |
| 84 | io.run(); |
| 85 | |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame^] | 86 | BOOST_CHECK(poke->didSendData()); |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 87 | BOOST_REQUIRE_EQUAL(face.sentData.size(), 1); |
| 88 | BOOST_CHECK_EQUAL(face.sentData.back().getName(), "/poke/test"); |
| 89 | BOOST_CHECK(!face.sentData.back().getFinalBlock()); |
| 90 | BOOST_CHECK_EQUAL(face.sentData.back().getFreshnessPeriod(), 0_ms); |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame^] | 91 | BOOST_CHECK_EQUAL(face.sentData.back().getContentType(), tlv::ContentType_Blob); |
| 92 | BOOST_CHECK_EQUAL(face.sentData.back().getContent(), "150E48656C6C6F2C20776F726C64210A"_block); |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 93 | BOOST_CHECK_EQUAL(face.sentData.back().getSignature().getType(), tlv::SignatureSha256WithEcdsa); |
Eric Newberry | 468dbdf | 2018-06-20 23:58:55 -0700 | [diff] [blame] | 94 | |
| 95 | // Check for prefix unregistration |
| 96 | BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 2); // One for registration, one for unregistration |
| 97 | 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] | 98 | } |
| 99 | |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 100 | BOOST_AUTO_TEST_CASE(FreshnessPeriod) |
| 101 | { |
| 102 | auto options = makeDefaultOptions(); |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame^] | 103 | options.freshnessPeriod = 1_s; |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 104 | initialize(options); |
| 105 | |
| 106 | poke->start(); |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 107 | this->advanceClocks(io, 1_ms, 10); |
| 108 | |
Junxiao Shi | 20d5a0b | 2018-08-14 09:26:11 -0600 | [diff] [blame] | 109 | face.receive(*makeInterest("/poke/test")); |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 110 | this->advanceClocks(io, 1_ms, 10); |
| 111 | io.run(); |
| 112 | |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame^] | 113 | BOOST_CHECK(poke->didSendData()); |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 114 | BOOST_REQUIRE_EQUAL(face.sentData.size(), 1); |
| 115 | BOOST_CHECK_EQUAL(face.sentData.back().getName(), "/poke/test"); |
| 116 | BOOST_CHECK(!face.sentData.back().getFinalBlock()); |
| 117 | BOOST_CHECK_EQUAL(face.sentData.back().getFreshnessPeriod(), 1_s); |
| 118 | BOOST_CHECK_EQUAL(face.sentData.back().getSignature().getType(), tlv::SignatureSha256WithEcdsa); |
| 119 | } |
| 120 | |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame^] | 121 | BOOST_AUTO_TEST_CASE(FinalBlockId) |
| 122 | { |
| 123 | auto options = makeDefaultOptions(); |
| 124 | options.name = "/poke/test/123"; |
| 125 | options.wantFinalBlockId = true; |
| 126 | initialize(options); |
| 127 | |
| 128 | poke->start(); |
| 129 | this->advanceClocks(io, 1_ms, 10); |
| 130 | |
| 131 | face.receive(*makeInterest(options.name)); |
| 132 | this->advanceClocks(io, 1_ms, 10); |
| 133 | io.run(); |
| 134 | |
| 135 | BOOST_CHECK(poke->didSendData()); |
| 136 | BOOST_REQUIRE_EQUAL(face.sentData.size(), 1); |
| 137 | BOOST_CHECK_EQUAL(face.sentData.back().getName(), options.name); |
| 138 | BOOST_REQUIRE(face.sentData.back().getFinalBlock()); |
| 139 | BOOST_CHECK_EQUAL(*(face.sentData.back().getFinalBlock()), name::Component("123")); |
| 140 | BOOST_CHECK_EQUAL(face.sentData.back().getFreshnessPeriod(), 0_ms); |
| 141 | BOOST_CHECK_EQUAL(face.sentData.back().getSignature().getType(), tlv::SignatureSha256WithEcdsa); |
| 142 | } |
| 143 | |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 144 | BOOST_AUTO_TEST_CASE(DigestSha256) |
| 145 | { |
| 146 | auto options = makeDefaultOptions(); |
| 147 | options.signingInfo.setSha256Signing(); |
| 148 | initialize(options); |
| 149 | |
| 150 | poke->start(); |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 151 | this->advanceClocks(io, 1_ms, 10); |
| 152 | |
Junxiao Shi | 20d5a0b | 2018-08-14 09:26:11 -0600 | [diff] [blame] | 153 | face.receive(*makeInterest("/poke/test")); |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 154 | this->advanceClocks(io, 1_ms, 10); |
| 155 | io.run(); |
| 156 | |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame^] | 157 | BOOST_CHECK(poke->didSendData()); |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 158 | BOOST_REQUIRE_EQUAL(face.sentData.size(), 1); |
| 159 | BOOST_CHECK_EQUAL(face.sentData.back().getName(), "/poke/test"); |
| 160 | BOOST_CHECK(!face.sentData.back().getFinalBlock()); |
| 161 | BOOST_CHECK_EQUAL(face.sentData.back().getFreshnessPeriod(), 0_ms); |
| 162 | BOOST_CHECK_EQUAL(face.sentData.back().getSignature().getType(), tlv::DigestSha256); |
| 163 | } |
| 164 | |
| 165 | BOOST_AUTO_TEST_CASE(ForceData) |
| 166 | { |
| 167 | auto options = makeDefaultOptions(); |
| 168 | options.wantForceData = true; |
| 169 | initialize(options); |
| 170 | |
| 171 | poke->start(); |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 172 | this->advanceClocks(io, 1_ms, 10); |
| 173 | |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame^] | 174 | BOOST_CHECK(poke->didSendData()); |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 175 | BOOST_REQUIRE_EQUAL(face.sentData.size(), 1); |
| 176 | BOOST_CHECK_EQUAL(face.sentData.back().getName(), "/poke/test"); |
| 177 | BOOST_CHECK(!face.sentData.back().getFinalBlock()); |
| 178 | BOOST_CHECK_EQUAL(face.sentData.back().getFreshnessPeriod(), 0_ms); |
| 179 | BOOST_CHECK_EQUAL(face.sentData.back().getSignature().getType(), tlv::SignatureSha256WithEcdsa); |
| 180 | } |
| 181 | |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame^] | 182 | BOOST_AUTO_TEST_CASE(Timeout) |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 183 | { |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 184 | auto options = makeDefaultOptions(); |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame^] | 185 | options.timeout = 4_s; |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 186 | initialize(options); |
| 187 | |
| 188 | poke->start(); |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame^] | 189 | this->advanceClocks(io, 1_ms, 10); |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 190 | |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame^] | 191 | // Check for prefix registration |
| 192 | BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1); |
| 193 | BOOST_CHECK_EQUAL(face.sentInterests.front().getName().getPrefix(4), "/localhost/nfd/rib/register"); |
| 194 | |
| 195 | this->advanceClocks(io, 1_s, 4); |
| 196 | |
| 197 | BOOST_CHECK(!poke->didSendData()); |
| 198 | BOOST_CHECK_EQUAL(face.sentData.size(), 0); |
| 199 | |
| 200 | // Check for prefix unregistration |
| 201 | BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 2); // One for registration, one for unregistration |
| 202 | BOOST_CHECK_EQUAL(face.sentInterests.back().getName().getPrefix(4), "/localhost/nfd/rib/unregister"); |
| 203 | } |
| 204 | |
| 205 | BOOST_AUTO_TEST_CASE(OversizedPacket) |
| 206 | { |
| 207 | payload << std::string(MAX_NDN_PACKET_SIZE, 'A'); |
| 208 | initialize(); |
| 209 | |
| 210 | poke->start(); |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 211 | this->advanceClocks(io, 1_ms, 10); |
| 212 | |
Junxiao Shi | 20d5a0b | 2018-08-14 09:26:11 -0600 | [diff] [blame] | 213 | face.receive(*makeInterest("/poke/test")); |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 214 | BOOST_CHECK_THROW(face.processEvents(), Face::OversizedPacketError); |
| 215 | |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame^] | 216 | // No point in checking didSendData() here. The exception is thrown from processEvents(), |
| 217 | // not from put(), so didSendData() is still set to true even though no packets are sent. |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 218 | BOOST_CHECK_EQUAL(face.sentData.size(), 0); |
| 219 | } |
| 220 | |
| 221 | BOOST_AUTO_TEST_SUITE_END() // TestNdnPoke |
| 222 | BOOST_AUTO_TEST_SUITE_END() // Peek |
| 223 | |
| 224 | } // namespace tests |
| 225 | } // namespace peek |
| 226 | } // namespace ndn |