Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2014-2018, Regents of the University of California, |
| 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" |
| 29 | |
| 30 | #include <ndn-cxx/util/dummy-client-face.hpp> |
| 31 | |
| 32 | namespace ndn { |
| 33 | namespace peek { |
| 34 | namespace tests { |
| 35 | |
| 36 | using namespace ndn::tests; |
| 37 | |
| 38 | class NdnPokeFixture : public UnitTestTimeFixture |
| 39 | { |
| 40 | protected: |
| 41 | NdnPokeFixture() |
| 42 | : face(io, keyChain, {true, true}) |
| 43 | { |
| 44 | keyChain.createIdentity("/test-id"); |
| 45 | |
| 46 | inData << "Hello world!"; |
| 47 | } |
| 48 | |
| 49 | void |
| 50 | initialize(const PokeOptions& opts) |
| 51 | { |
| 52 | poke = make_unique<NdnPoke>(face, keyChain, inData, opts); |
| 53 | } |
| 54 | |
| 55 | static PokeOptions |
| 56 | makeDefaultOptions() |
| 57 | { |
| 58 | PokeOptions opt; |
| 59 | opt.prefixName = "/poke/test"; |
| 60 | return opt; |
| 61 | } |
| 62 | |
| 63 | protected: |
| 64 | boost::asio::io_service io; |
| 65 | ndn::util::DummyClientFace face; |
| 66 | KeyChain keyChain; |
| 67 | unique_ptr<NdnPoke> poke; |
| 68 | std::stringstream inData; |
| 69 | }; |
| 70 | |
| 71 | BOOST_AUTO_TEST_SUITE(Peek) |
| 72 | BOOST_FIXTURE_TEST_SUITE(TestNdnPoke, NdnPokeFixture) |
| 73 | |
| 74 | BOOST_AUTO_TEST_CASE(Basic) |
| 75 | { |
| 76 | auto options = makeDefaultOptions(); |
| 77 | initialize(options); |
| 78 | |
| 79 | poke->start(); |
| 80 | |
| 81 | this->advanceClocks(io, 1_ms, 10); |
| 82 | |
Eric Newberry | 468dbdf | 2018-06-20 23:58:55 -0700 | [diff] [blame] | 83 | // Check for prefix registration |
| 84 | BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1); |
| 85 | BOOST_CHECK_EQUAL(face.sentInterests.front().getName().getPrefix(4), "/localhost/nfd/rib/register"); |
| 86 | |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 87 | Interest interest("/poke/test"); |
| 88 | face.receive(interest); |
| 89 | this->advanceClocks(io, 1_ms, 10); |
| 90 | io.run(); |
| 91 | |
| 92 | BOOST_CHECK(poke->wasDataSent()); |
| 93 | BOOST_REQUIRE_EQUAL(face.sentData.size(), 1); |
| 94 | BOOST_CHECK_EQUAL(face.sentData.back().getName(), "/poke/test"); |
| 95 | BOOST_CHECK(!face.sentData.back().getFinalBlock()); |
| 96 | BOOST_CHECK_EQUAL(face.sentData.back().getFreshnessPeriod(), 0_ms); |
| 97 | BOOST_CHECK_EQUAL(face.sentData.back().getSignature().getType(), tlv::SignatureSha256WithEcdsa); |
Eric Newberry | 468dbdf | 2018-06-20 23:58:55 -0700 | [diff] [blame] | 98 | |
| 99 | // Check for prefix unregistration |
| 100 | BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 2); // One for registration, one for unregistration |
| 101 | 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] | 102 | } |
| 103 | |
| 104 | BOOST_AUTO_TEST_CASE(FinalBlockId) |
| 105 | { |
| 106 | auto options = makeDefaultOptions(); |
| 107 | options.prefixName = "/poke/test/123"; |
| 108 | options.wantLastAsFinalBlockId = true; |
| 109 | initialize(options); |
| 110 | |
| 111 | poke->start(); |
| 112 | |
| 113 | this->advanceClocks(io, 1_ms, 10); |
| 114 | |
| 115 | Interest interest("/poke/test/123"); |
| 116 | face.receive(interest); |
| 117 | this->advanceClocks(io, 1_ms, 10); |
| 118 | io.run(); |
| 119 | |
| 120 | BOOST_CHECK(poke->wasDataSent()); |
| 121 | BOOST_REQUIRE_EQUAL(face.sentData.size(), 1); |
| 122 | BOOST_CHECK_EQUAL(face.sentData.back().getName(), "/poke/test/123"); |
| 123 | BOOST_REQUIRE(face.sentData.back().getFinalBlock()); |
| 124 | BOOST_CHECK_EQUAL(*(face.sentData.back().getFinalBlock()), name::Component("123")); |
| 125 | BOOST_CHECK_EQUAL(face.sentData.back().getFreshnessPeriod(), 0_ms); |
| 126 | BOOST_CHECK_EQUAL(face.sentData.back().getSignature().getType(), tlv::SignatureSha256WithEcdsa); |
| 127 | } |
| 128 | |
| 129 | BOOST_AUTO_TEST_CASE(FreshnessPeriod) |
| 130 | { |
| 131 | auto options = makeDefaultOptions(); |
| 132 | options.freshnessPeriod = make_optional<time::milliseconds>(1_s); |
| 133 | initialize(options); |
| 134 | |
| 135 | poke->start(); |
| 136 | |
| 137 | this->advanceClocks(io, 1_ms, 10); |
| 138 | |
| 139 | Interest interest("/poke/test"); |
| 140 | face.receive(interest); |
| 141 | this->advanceClocks(io, 1_ms, 10); |
| 142 | io.run(); |
| 143 | |
| 144 | BOOST_CHECK(poke->wasDataSent()); |
| 145 | BOOST_REQUIRE_EQUAL(face.sentData.size(), 1); |
| 146 | BOOST_CHECK_EQUAL(face.sentData.back().getName(), "/poke/test"); |
| 147 | BOOST_CHECK(!face.sentData.back().getFinalBlock()); |
| 148 | BOOST_CHECK_EQUAL(face.sentData.back().getFreshnessPeriod(), 1_s); |
| 149 | BOOST_CHECK_EQUAL(face.sentData.back().getSignature().getType(), tlv::SignatureSha256WithEcdsa); |
| 150 | } |
| 151 | |
| 152 | BOOST_AUTO_TEST_CASE(DigestSha256) |
| 153 | { |
| 154 | auto options = makeDefaultOptions(); |
| 155 | options.signingInfo.setSha256Signing(); |
| 156 | initialize(options); |
| 157 | |
| 158 | poke->start(); |
| 159 | |
| 160 | this->advanceClocks(io, 1_ms, 10); |
| 161 | |
| 162 | Interest interest("/poke/test"); |
| 163 | face.receive(interest); |
| 164 | this->advanceClocks(io, 1_ms, 10); |
| 165 | io.run(); |
| 166 | |
| 167 | BOOST_CHECK(poke->wasDataSent()); |
| 168 | BOOST_REQUIRE_EQUAL(face.sentData.size(), 1); |
| 169 | BOOST_CHECK_EQUAL(face.sentData.back().getName(), "/poke/test"); |
| 170 | BOOST_CHECK(!face.sentData.back().getFinalBlock()); |
| 171 | BOOST_CHECK_EQUAL(face.sentData.back().getFreshnessPeriod(), 0_ms); |
| 172 | BOOST_CHECK_EQUAL(face.sentData.back().getSignature().getType(), tlv::DigestSha256); |
| 173 | } |
| 174 | |
| 175 | BOOST_AUTO_TEST_CASE(ForceData) |
| 176 | { |
| 177 | auto options = makeDefaultOptions(); |
| 178 | options.wantForceData = true; |
| 179 | initialize(options); |
| 180 | |
| 181 | poke->start(); |
| 182 | |
| 183 | this->advanceClocks(io, 1_ms, 10); |
| 184 | |
| 185 | BOOST_CHECK(poke->wasDataSent()); |
| 186 | BOOST_REQUIRE_EQUAL(face.sentData.size(), 1); |
| 187 | BOOST_CHECK_EQUAL(face.sentData.back().getName(), "/poke/test"); |
| 188 | BOOST_CHECK(!face.sentData.back().getFinalBlock()); |
| 189 | BOOST_CHECK_EQUAL(face.sentData.back().getFreshnessPeriod(), 0_ms); |
| 190 | BOOST_CHECK_EQUAL(face.sentData.back().getSignature().getType(), tlv::SignatureSha256WithEcdsa); |
| 191 | } |
| 192 | |
| 193 | BOOST_AUTO_TEST_CASE(ExceedMaxPacketSize) |
| 194 | { |
| 195 | for (size_t i = 0; i < MAX_NDN_PACKET_SIZE; i++) { |
| 196 | inData << "A"; |
| 197 | } |
| 198 | |
| 199 | auto options = makeDefaultOptions(); |
| 200 | initialize(options); |
| 201 | |
| 202 | poke->start(); |
| 203 | |
| 204 | this->advanceClocks(io, 1_ms, 10); |
| 205 | |
| 206 | Interest interest("/poke/test"); |
| 207 | face.receive(interest); |
| 208 | BOOST_CHECK_THROW(face.processEvents(), Face::OversizedPacketError); |
| 209 | |
| 210 | // We can't check wasDataSent() correctly here because it will be set to true, even if put failed |
| 211 | // due to the packet being oversized. |
| 212 | BOOST_CHECK_EQUAL(face.sentData.size(), 0); |
| 213 | } |
| 214 | |
| 215 | BOOST_AUTO_TEST_SUITE_END() // TestNdnPoke |
| 216 | BOOST_AUTO_TEST_SUITE_END() // Peek |
| 217 | |
| 218 | } // namespace tests |
| 219 | } // namespace peek |
| 220 | } // namespace ndn |