blob: 14fe3059f7cb156d5935d508a4a7bd3b09adcee5 [file] [log] [blame]
Eric Newberry2f041d22018-06-03 18:02:31 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Davide Pesaventob3570c62022-02-19 19:19:00 -05003 * Copyright (c) 2014-2022, Regents of the University of California,
Eric Newberry2f041d22018-06-03 18:02:31 -07004 * 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 Pesavento66777622020-10-09 18:46:03 -040029#include "tests/io-fixture.hpp"
30#include "tests/key-chain-fixture.hpp"
Eric Newberry2f041d22018-06-03 18:02:31 -070031
32#include <ndn-cxx/util/dummy-client-face.hpp>
33
Davide Pesaventob3570c62022-02-19 19:19:00 -050034namespace ndn::peek::tests {
Eric Newberry2f041d22018-06-03 18:02:31 -070035
36using namespace ndn::tests;
37
Davide Pesavento87434be2019-07-25 19:04:23 -040038template<bool WANT_PREFIX_REG_REPLY = true>
Davide Pesavento66777622020-10-09 18:46:03 -040039class NdnPokeFixture : public IoFixture, public KeyChainFixture
Eric Newberry2f041d22018-06-03 18:02:31 -070040{
41protected:
42 NdnPokeFixture()
Eric Newberry2f041d22018-06-03 18:02:31 -070043 {
Davide Pesaventoe75861e2019-07-24 21:55:39 -040044 m_keyChain.createIdentity("/test-id");
Eric Newberry2f041d22018-06-03 18:02:31 -070045 }
46
47 void
Davide Pesaventoe75861e2019-07-24 21:55:39 -040048 initialize(const PokeOptions& opts = makeDefaultOptions())
Eric Newberry2f041d22018-06-03 18:02:31 -070049 {
Davide Pesaventoe75861e2019-07-24 21:55:39 -040050 poke = make_unique<NdnPoke>(face, m_keyChain, payload, opts);
Eric Newberry2f041d22018-06-03 18:02:31 -070051 }
52
53 static PokeOptions
54 makeDefaultOptions()
55 {
56 PokeOptions opt;
Davide Pesaventoe75861e2019-07-24 21:55:39 -040057 opt.name = "/poke/test";
Eric Newberry2f041d22018-06-03 18:02:31 -070058 return opt;
59 }
60
61protected:
Davide Pesavento66777622020-10-09 18:46:03 -040062 ndn::util::DummyClientFace face{m_io, m_keyChain, {true, WANT_PREFIX_REG_REPLY}};
Davide Pesaventoe75861e2019-07-24 21:55:39 -040063 std::stringstream payload{"Hello, world!\n"};
Eric Newberry2f041d22018-06-03 18:02:31 -070064 unique_ptr<NdnPoke> poke;
Eric Newberry2f041d22018-06-03 18:02:31 -070065};
66
67BOOST_AUTO_TEST_SUITE(Peek)
Davide Pesavento87434be2019-07-25 19:04:23 -040068BOOST_FIXTURE_TEST_SUITE(TestNdnPoke, NdnPokeFixture<>)
Eric Newberry2f041d22018-06-03 18:02:31 -070069
70BOOST_AUTO_TEST_CASE(Basic)
71{
Davide Pesaventoe75861e2019-07-24 21:55:39 -040072 initialize();
Eric Newberry2f041d22018-06-03 18:02:31 -070073
74 poke->start();
Davide Pesavento66777622020-10-09 18:46:03 -040075 this->advanceClocks(1_ms, 10);
Eric Newberry2f041d22018-06-03 18:02:31 -070076
Eric Newberry468dbdf2018-06-20 23:58:55 -070077 // Check for prefix registration
78 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1);
79 BOOST_CHECK_EQUAL(face.sentInterests.front().getName().getPrefix(4), "/localhost/nfd/rib/register");
80
Junxiao Shi20d5a0b2018-08-14 09:26:11 -060081 face.receive(*makeInterest("/poke/test"));
Davide Pesavento66777622020-10-09 18:46:03 -040082 this->advanceClocks(1_ms, 10);
Eric Newberry2f041d22018-06-03 18:02:31 -070083
Davide Pesavento87434be2019-07-25 19:04:23 -040084 BOOST_CHECK(poke->getResult() == NdnPoke::Result::DATA_SENT);
Eric Newberry2f041d22018-06-03 18:02:31 -070085 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
86 BOOST_CHECK_EQUAL(face.sentData.back().getName(), "/poke/test");
87 BOOST_CHECK(!face.sentData.back().getFinalBlock());
88 BOOST_CHECK_EQUAL(face.sentData.back().getFreshnessPeriod(), 0_ms);
Davide Pesaventoe75861e2019-07-24 21:55:39 -040089 BOOST_CHECK_EQUAL(face.sentData.back().getContentType(), tlv::ContentType_Blob);
90 BOOST_CHECK_EQUAL(face.sentData.back().getContent(), "150E48656C6C6F2C20776F726C64210A"_block);
Davide Pesavento35275582020-07-27 18:02:03 -040091 BOOST_CHECK_EQUAL(face.sentData.back().getSignatureType(), tlv::SignatureSha256WithEcdsa);
Eric Newberry468dbdf2018-06-20 23:58:55 -070092
93 // Check for prefix unregistration
94 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 2); // One for registration, one for unregistration
95 BOOST_CHECK_EQUAL(face.sentInterests.back().getName().getPrefix(4), "/localhost/nfd/rib/unregister");
Eric Newberry2f041d22018-06-03 18:02:31 -070096}
97
Davide Pesaventoc5243b42019-07-26 13:30:16 -040098BOOST_AUTO_TEST_CASE(NoMatch)
99{
100 initialize();
101
102 poke->start();
Davide Pesavento66777622020-10-09 18:46:03 -0400103 this->advanceClocks(1_ms, 10);
Davide Pesaventoc5243b42019-07-26 13:30:16 -0400104
105 face.receive(*makeInterest("/poke/test/foo"));
Davide Pesavento66777622020-10-09 18:46:03 -0400106 this->advanceClocks(1_ms, 10);
Davide Pesaventoc5243b42019-07-26 13:30:16 -0400107
108 BOOST_CHECK(poke->getResult() == NdnPoke::Result::UNKNOWN);
109 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
110
111 face.receive(*makeInterest("/poke/test"));
Davide Pesavento66777622020-10-09 18:46:03 -0400112 this->advanceClocks(1_ms, 10);
Davide Pesaventoc5243b42019-07-26 13:30:16 -0400113
114 BOOST_CHECK(poke->getResult() == NdnPoke::Result::DATA_SENT);
115 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
116 BOOST_CHECK_EQUAL(face.sentData.back().getName(), "/poke/test");
117}
118
Eric Newberry2f041d22018-06-03 18:02:31 -0700119BOOST_AUTO_TEST_CASE(FreshnessPeriod)
120{
121 auto options = makeDefaultOptions();
Davide Pesaventoe75861e2019-07-24 21:55:39 -0400122 options.freshnessPeriod = 1_s;
Eric Newberry2f041d22018-06-03 18:02:31 -0700123 initialize(options);
124
125 poke->start();
Davide Pesavento66777622020-10-09 18:46:03 -0400126 this->advanceClocks(1_ms, 10);
Eric Newberry2f041d22018-06-03 18:02:31 -0700127
Junxiao Shi20d5a0b2018-08-14 09:26:11 -0600128 face.receive(*makeInterest("/poke/test"));
Davide Pesavento66777622020-10-09 18:46:03 -0400129 this->advanceClocks(1_ms, 10);
Eric Newberry2f041d22018-06-03 18:02:31 -0700130
Davide Pesavento87434be2019-07-25 19:04:23 -0400131 BOOST_CHECK(poke->getResult() == NdnPoke::Result::DATA_SENT);
Eric Newberry2f041d22018-06-03 18:02:31 -0700132 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
133 BOOST_CHECK_EQUAL(face.sentData.back().getName(), "/poke/test");
134 BOOST_CHECK(!face.sentData.back().getFinalBlock());
135 BOOST_CHECK_EQUAL(face.sentData.back().getFreshnessPeriod(), 1_s);
Davide Pesavento35275582020-07-27 18:02:03 -0400136 BOOST_CHECK_EQUAL(face.sentData.back().getSignatureType(), tlv::SignatureSha256WithEcdsa);
Eric Newberry2f041d22018-06-03 18:02:31 -0700137}
138
Davide Pesaventoe75861e2019-07-24 21:55:39 -0400139BOOST_AUTO_TEST_CASE(FinalBlockId)
140{
141 auto options = makeDefaultOptions();
142 options.name = "/poke/test/123";
143 options.wantFinalBlockId = true;
144 initialize(options);
145
146 poke->start();
Davide Pesavento66777622020-10-09 18:46:03 -0400147 this->advanceClocks(1_ms, 10);
Davide Pesaventoe75861e2019-07-24 21:55:39 -0400148
149 face.receive(*makeInterest(options.name));
Davide Pesavento66777622020-10-09 18:46:03 -0400150 this->advanceClocks(1_ms, 10);
Davide Pesaventoe75861e2019-07-24 21:55:39 -0400151
Davide Pesavento87434be2019-07-25 19:04:23 -0400152 BOOST_CHECK(poke->getResult() == NdnPoke::Result::DATA_SENT);
Davide Pesaventoe75861e2019-07-24 21:55:39 -0400153 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
154 BOOST_CHECK_EQUAL(face.sentData.back().getName(), options.name);
155 BOOST_REQUIRE(face.sentData.back().getFinalBlock());
156 BOOST_CHECK_EQUAL(*(face.sentData.back().getFinalBlock()), name::Component("123"));
157 BOOST_CHECK_EQUAL(face.sentData.back().getFreshnessPeriod(), 0_ms);
Davide Pesavento35275582020-07-27 18:02:03 -0400158 BOOST_CHECK_EQUAL(face.sentData.back().getSignatureType(), tlv::SignatureSha256WithEcdsa);
Davide Pesaventoe75861e2019-07-24 21:55:39 -0400159}
160
Eric Newberry2f041d22018-06-03 18:02:31 -0700161BOOST_AUTO_TEST_CASE(DigestSha256)
162{
163 auto options = makeDefaultOptions();
164 options.signingInfo.setSha256Signing();
165 initialize(options);
166
167 poke->start();
Davide Pesavento66777622020-10-09 18:46:03 -0400168 this->advanceClocks(1_ms, 10);
Eric Newberry2f041d22018-06-03 18:02:31 -0700169
Junxiao Shi20d5a0b2018-08-14 09:26:11 -0600170 face.receive(*makeInterest("/poke/test"));
Davide Pesavento66777622020-10-09 18:46:03 -0400171 this->advanceClocks(1_ms, 10);
Eric Newberry2f041d22018-06-03 18:02:31 -0700172
Davide Pesavento87434be2019-07-25 19:04:23 -0400173 BOOST_CHECK(poke->getResult() == NdnPoke::Result::DATA_SENT);
Eric Newberry2f041d22018-06-03 18:02:31 -0700174 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
175 BOOST_CHECK_EQUAL(face.sentData.back().getName(), "/poke/test");
176 BOOST_CHECK(!face.sentData.back().getFinalBlock());
177 BOOST_CHECK_EQUAL(face.sentData.back().getFreshnessPeriod(), 0_ms);
Davide Pesavento35275582020-07-27 18:02:03 -0400178 BOOST_CHECK_EQUAL(face.sentData.back().getSignatureType(), tlv::DigestSha256);
Eric Newberry2f041d22018-06-03 18:02:31 -0700179}
180
Davide Pesavento6a1396e2019-07-26 15:03:28 -0400181BOOST_AUTO_TEST_CASE(Unsolicited)
Eric Newberry2f041d22018-06-03 18:02:31 -0700182{
183 auto options = makeDefaultOptions();
Davide Pesavento6a1396e2019-07-26 15:03:28 -0400184 options.wantUnsolicited = true;
Eric Newberry2f041d22018-06-03 18:02:31 -0700185 initialize(options);
186
187 poke->start();
Davide Pesavento66777622020-10-09 18:46:03 -0400188 this->advanceClocks(1_ms, 10);
Eric Newberry2f041d22018-06-03 18:02:31 -0700189
Davide Pesavento87434be2019-07-25 19:04:23 -0400190 BOOST_CHECK(poke->getResult() == NdnPoke::Result::DATA_SENT);
Eric Newberry2f041d22018-06-03 18:02:31 -0700191 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
192 BOOST_CHECK_EQUAL(face.sentData.back().getName(), "/poke/test");
193 BOOST_CHECK(!face.sentData.back().getFinalBlock());
194 BOOST_CHECK_EQUAL(face.sentData.back().getFreshnessPeriod(), 0_ms);
Davide Pesavento35275582020-07-27 18:02:03 -0400195 BOOST_CHECK_EQUAL(face.sentData.back().getSignatureType(), tlv::SignatureSha256WithEcdsa);
Eric Newberry2f041d22018-06-03 18:02:31 -0700196}
197
Davide Pesaventoe75861e2019-07-24 21:55:39 -0400198BOOST_AUTO_TEST_CASE(Timeout)
Eric Newberry2f041d22018-06-03 18:02:31 -0700199{
Eric Newberry2f041d22018-06-03 18:02:31 -0700200 auto options = makeDefaultOptions();
Davide Pesaventoe75861e2019-07-24 21:55:39 -0400201 options.timeout = 4_s;
Eric Newberry2f041d22018-06-03 18:02:31 -0700202 initialize(options);
203
204 poke->start();
Davide Pesavento66777622020-10-09 18:46:03 -0400205 this->advanceClocks(1_ms, 10);
Eric Newberry2f041d22018-06-03 18:02:31 -0700206
Davide Pesaventoe75861e2019-07-24 21:55:39 -0400207 // Check for prefix registration
208 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1);
209 BOOST_CHECK_EQUAL(face.sentInterests.front().getName().getPrefix(4), "/localhost/nfd/rib/register");
210
Davide Pesavento66777622020-10-09 18:46:03 -0400211 this->advanceClocks(1_s, 4);
Davide Pesaventoe75861e2019-07-24 21:55:39 -0400212
Davide Pesavento94dff002019-07-24 23:35:05 -0400213 BOOST_CHECK(poke->getResult() == NdnPoke::Result::TIMEOUT);
Davide Pesaventoe75861e2019-07-24 21:55:39 -0400214 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
215
216 // Check for prefix unregistration
217 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 2); // One for registration, one for unregistration
218 BOOST_CHECK_EQUAL(face.sentInterests.back().getName().getPrefix(4), "/localhost/nfd/rib/unregister");
219}
220
Davide Pesavento87434be2019-07-25 19:04:23 -0400221BOOST_FIXTURE_TEST_CASE(PrefixRegTimeout, NdnPokeFixture<false>)
222{
223 initialize();
224
225 poke->start();
Davide Pesavento66777622020-10-09 18:46:03 -0400226 this->advanceClocks(1_ms, 10);
Davide Pesavento87434be2019-07-25 19:04:23 -0400227
228 // Check for prefix registration
229 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1);
230 BOOST_CHECK_EQUAL(face.sentInterests.front().getName().getPrefix(4), "/localhost/nfd/rib/register");
231
Davide Pesavento66777622020-10-09 18:46:03 -0400232 this->advanceClocks(1_s, 10);
Davide Pesavento87434be2019-07-25 19:04:23 -0400233
234 BOOST_CHECK(poke->getResult() == NdnPoke::Result::PREFIX_REG_FAIL);
235 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
236}
237
Davide Pesaventoe75861e2019-07-24 21:55:39 -0400238BOOST_AUTO_TEST_CASE(OversizedPacket)
239{
240 payload << std::string(MAX_NDN_PACKET_SIZE, 'A');
241 initialize();
242
243 poke->start();
Davide Pesavento66777622020-10-09 18:46:03 -0400244 this->advanceClocks(1_ms, 10);
Eric Newberry2f041d22018-06-03 18:02:31 -0700245
Junxiao Shi20d5a0b2018-08-14 09:26:11 -0600246 face.receive(*makeInterest("/poke/test"));
Eric Newberry2f041d22018-06-03 18:02:31 -0700247 BOOST_CHECK_THROW(face.processEvents(), Face::OversizedPacketError);
248
Davide Pesavento87434be2019-07-25 19:04:23 -0400249 // No point in checking getResult() here. The exception is thrown from processEvents(),
250 // not from put(), so the result is still DATA_SENT even though no packets were sent.
Eric Newberry2f041d22018-06-03 18:02:31 -0700251 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
252}
253
254BOOST_AUTO_TEST_SUITE_END() // TestNdnPoke
255BOOST_AUTO_TEST_SUITE_END() // Peek
256
Davide Pesaventob3570c62022-02-19 19:19:00 -0500257} // namespace ndn::peek::tests