blob: 617b5165f9d011eb65c8f18cacd5b684594f7e2c [file] [log] [blame]
Eric Newberry2f041d22018-06-03 18:02:31 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Davide Pesavento35275582020-07-27 18:02:03 -04003 * Copyright (c) 2014-2020, 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
34namespace ndn {
35namespace peek {
36namespace tests {
37
38using namespace ndn::tests;
39
Davide Pesavento87434be2019-07-25 19:04:23 -040040template<bool WANT_PREFIX_REG_REPLY = true>
Davide Pesavento66777622020-10-09 18:46:03 -040041class NdnPokeFixture : public IoFixture, public KeyChainFixture
Eric Newberry2f041d22018-06-03 18:02:31 -070042{
43protected:
44 NdnPokeFixture()
Eric Newberry2f041d22018-06-03 18:02:31 -070045 {
Davide Pesaventoe75861e2019-07-24 21:55:39 -040046 m_keyChain.createIdentity("/test-id");
Eric Newberry2f041d22018-06-03 18:02:31 -070047 }
48
49 void
Davide Pesaventoe75861e2019-07-24 21:55:39 -040050 initialize(const PokeOptions& opts = makeDefaultOptions())
Eric Newberry2f041d22018-06-03 18:02:31 -070051 {
Davide Pesaventoe75861e2019-07-24 21:55:39 -040052 poke = make_unique<NdnPoke>(face, m_keyChain, payload, opts);
Eric Newberry2f041d22018-06-03 18:02:31 -070053 }
54
55 static PokeOptions
56 makeDefaultOptions()
57 {
58 PokeOptions opt;
Davide Pesaventoe75861e2019-07-24 21:55:39 -040059 opt.name = "/poke/test";
Eric Newberry2f041d22018-06-03 18:02:31 -070060 return opt;
61 }
62
63protected:
Davide Pesavento66777622020-10-09 18:46:03 -040064 ndn::util::DummyClientFace face{m_io, m_keyChain, {true, WANT_PREFIX_REG_REPLY}};
Davide Pesaventoe75861e2019-07-24 21:55:39 -040065 std::stringstream payload{"Hello, world!\n"};
Eric Newberry2f041d22018-06-03 18:02:31 -070066 unique_ptr<NdnPoke> poke;
Eric Newberry2f041d22018-06-03 18:02:31 -070067};
68
69BOOST_AUTO_TEST_SUITE(Peek)
Davide Pesavento87434be2019-07-25 19:04:23 -040070BOOST_FIXTURE_TEST_SUITE(TestNdnPoke, NdnPokeFixture<>)
Eric Newberry2f041d22018-06-03 18:02:31 -070071
72BOOST_AUTO_TEST_CASE(Basic)
73{
Davide Pesaventoe75861e2019-07-24 21:55:39 -040074 initialize();
Eric Newberry2f041d22018-06-03 18:02:31 -070075
76 poke->start();
Davide Pesavento66777622020-10-09 18:46:03 -040077 this->advanceClocks(1_ms, 10);
Eric Newberry2f041d22018-06-03 18:02:31 -070078
Eric Newberry468dbdf2018-06-20 23:58:55 -070079 // 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 Shi20d5a0b2018-08-14 09:26:11 -060083 face.receive(*makeInterest("/poke/test"));
Davide Pesavento66777622020-10-09 18:46:03 -040084 this->advanceClocks(1_ms, 10);
Eric Newberry2f041d22018-06-03 18:02:31 -070085
Davide Pesavento87434be2019-07-25 19:04:23 -040086 BOOST_CHECK(poke->getResult() == NdnPoke::Result::DATA_SENT);
Eric Newberry2f041d22018-06-03 18:02:31 -070087 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 Pesaventoe75861e2019-07-24 21:55:39 -040091 BOOST_CHECK_EQUAL(face.sentData.back().getContentType(), tlv::ContentType_Blob);
92 BOOST_CHECK_EQUAL(face.sentData.back().getContent(), "150E48656C6C6F2C20776F726C64210A"_block);
Davide Pesavento35275582020-07-27 18:02:03 -040093 BOOST_CHECK_EQUAL(face.sentData.back().getSignatureType(), tlv::SignatureSha256WithEcdsa);
Eric Newberry468dbdf2018-06-20 23:58:55 -070094
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 Newberry2f041d22018-06-03 18:02:31 -070098}
99
Davide Pesaventoc5243b42019-07-26 13:30:16 -0400100BOOST_AUTO_TEST_CASE(NoMatch)
101{
102 initialize();
103
104 poke->start();
Davide Pesavento66777622020-10-09 18:46:03 -0400105 this->advanceClocks(1_ms, 10);
Davide Pesaventoc5243b42019-07-26 13:30:16 -0400106
107 face.receive(*makeInterest("/poke/test/foo"));
Davide Pesavento66777622020-10-09 18:46:03 -0400108 this->advanceClocks(1_ms, 10);
Davide Pesaventoc5243b42019-07-26 13:30:16 -0400109
110 BOOST_CHECK(poke->getResult() == NdnPoke::Result::UNKNOWN);
111 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
112
113 face.receive(*makeInterest("/poke/test"));
Davide Pesavento66777622020-10-09 18:46:03 -0400114 this->advanceClocks(1_ms, 10);
Davide Pesaventoc5243b42019-07-26 13:30:16 -0400115
116 BOOST_CHECK(poke->getResult() == NdnPoke::Result::DATA_SENT);
117 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
118 BOOST_CHECK_EQUAL(face.sentData.back().getName(), "/poke/test");
119}
120
Eric Newberry2f041d22018-06-03 18:02:31 -0700121BOOST_AUTO_TEST_CASE(FreshnessPeriod)
122{
123 auto options = makeDefaultOptions();
Davide Pesaventoe75861e2019-07-24 21:55:39 -0400124 options.freshnessPeriod = 1_s;
Eric Newberry2f041d22018-06-03 18:02:31 -0700125 initialize(options);
126
127 poke->start();
Davide Pesavento66777622020-10-09 18:46:03 -0400128 this->advanceClocks(1_ms, 10);
Eric Newberry2f041d22018-06-03 18:02:31 -0700129
Junxiao Shi20d5a0b2018-08-14 09:26:11 -0600130 face.receive(*makeInterest("/poke/test"));
Davide Pesavento66777622020-10-09 18:46:03 -0400131 this->advanceClocks(1_ms, 10);
Eric Newberry2f041d22018-06-03 18:02:31 -0700132
Davide Pesavento87434be2019-07-25 19:04:23 -0400133 BOOST_CHECK(poke->getResult() == NdnPoke::Result::DATA_SENT);
Eric Newberry2f041d22018-06-03 18:02:31 -0700134 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
135 BOOST_CHECK_EQUAL(face.sentData.back().getName(), "/poke/test");
136 BOOST_CHECK(!face.sentData.back().getFinalBlock());
137 BOOST_CHECK_EQUAL(face.sentData.back().getFreshnessPeriod(), 1_s);
Davide Pesavento35275582020-07-27 18:02:03 -0400138 BOOST_CHECK_EQUAL(face.sentData.back().getSignatureType(), tlv::SignatureSha256WithEcdsa);
Eric Newberry2f041d22018-06-03 18:02:31 -0700139}
140
Davide Pesaventoe75861e2019-07-24 21:55:39 -0400141BOOST_AUTO_TEST_CASE(FinalBlockId)
142{
143 auto options = makeDefaultOptions();
144 options.name = "/poke/test/123";
145 options.wantFinalBlockId = true;
146 initialize(options);
147
148 poke->start();
Davide Pesavento66777622020-10-09 18:46:03 -0400149 this->advanceClocks(1_ms, 10);
Davide Pesaventoe75861e2019-07-24 21:55:39 -0400150
151 face.receive(*makeInterest(options.name));
Davide Pesavento66777622020-10-09 18:46:03 -0400152 this->advanceClocks(1_ms, 10);
Davide Pesaventoe75861e2019-07-24 21:55:39 -0400153
Davide Pesavento87434be2019-07-25 19:04:23 -0400154 BOOST_CHECK(poke->getResult() == NdnPoke::Result::DATA_SENT);
Davide Pesaventoe75861e2019-07-24 21:55:39 -0400155 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
156 BOOST_CHECK_EQUAL(face.sentData.back().getName(), options.name);
157 BOOST_REQUIRE(face.sentData.back().getFinalBlock());
158 BOOST_CHECK_EQUAL(*(face.sentData.back().getFinalBlock()), name::Component("123"));
159 BOOST_CHECK_EQUAL(face.sentData.back().getFreshnessPeriod(), 0_ms);
Davide Pesavento35275582020-07-27 18:02:03 -0400160 BOOST_CHECK_EQUAL(face.sentData.back().getSignatureType(), tlv::SignatureSha256WithEcdsa);
Davide Pesaventoe75861e2019-07-24 21:55:39 -0400161}
162
Eric Newberry2f041d22018-06-03 18:02:31 -0700163BOOST_AUTO_TEST_CASE(DigestSha256)
164{
165 auto options = makeDefaultOptions();
166 options.signingInfo.setSha256Signing();
167 initialize(options);
168
169 poke->start();
Davide Pesavento66777622020-10-09 18:46:03 -0400170 this->advanceClocks(1_ms, 10);
Eric Newberry2f041d22018-06-03 18:02:31 -0700171
Junxiao Shi20d5a0b2018-08-14 09:26:11 -0600172 face.receive(*makeInterest("/poke/test"));
Davide Pesavento66777622020-10-09 18:46:03 -0400173 this->advanceClocks(1_ms, 10);
Eric Newberry2f041d22018-06-03 18:02:31 -0700174
Davide Pesavento87434be2019-07-25 19:04:23 -0400175 BOOST_CHECK(poke->getResult() == NdnPoke::Result::DATA_SENT);
Eric Newberry2f041d22018-06-03 18:02:31 -0700176 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);
Davide Pesavento35275582020-07-27 18:02:03 -0400180 BOOST_CHECK_EQUAL(face.sentData.back().getSignatureType(), tlv::DigestSha256);
Eric Newberry2f041d22018-06-03 18:02:31 -0700181}
182
Davide Pesavento6a1396e2019-07-26 15:03:28 -0400183BOOST_AUTO_TEST_CASE(Unsolicited)
Eric Newberry2f041d22018-06-03 18:02:31 -0700184{
185 auto options = makeDefaultOptions();
Davide Pesavento6a1396e2019-07-26 15:03:28 -0400186 options.wantUnsolicited = true;
Eric Newberry2f041d22018-06-03 18:02:31 -0700187 initialize(options);
188
189 poke->start();
Davide Pesavento66777622020-10-09 18:46:03 -0400190 this->advanceClocks(1_ms, 10);
Eric Newberry2f041d22018-06-03 18:02:31 -0700191
Davide Pesavento87434be2019-07-25 19:04:23 -0400192 BOOST_CHECK(poke->getResult() == NdnPoke::Result::DATA_SENT);
Eric Newberry2f041d22018-06-03 18:02:31 -0700193 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
194 BOOST_CHECK_EQUAL(face.sentData.back().getName(), "/poke/test");
195 BOOST_CHECK(!face.sentData.back().getFinalBlock());
196 BOOST_CHECK_EQUAL(face.sentData.back().getFreshnessPeriod(), 0_ms);
Davide Pesavento35275582020-07-27 18:02:03 -0400197 BOOST_CHECK_EQUAL(face.sentData.back().getSignatureType(), tlv::SignatureSha256WithEcdsa);
Eric Newberry2f041d22018-06-03 18:02:31 -0700198}
199
Davide Pesaventoe75861e2019-07-24 21:55:39 -0400200BOOST_AUTO_TEST_CASE(Timeout)
Eric Newberry2f041d22018-06-03 18:02:31 -0700201{
Eric Newberry2f041d22018-06-03 18:02:31 -0700202 auto options = makeDefaultOptions();
Davide Pesaventoe75861e2019-07-24 21:55:39 -0400203 options.timeout = 4_s;
Eric Newberry2f041d22018-06-03 18:02:31 -0700204 initialize(options);
205
206 poke->start();
Davide Pesavento66777622020-10-09 18:46:03 -0400207 this->advanceClocks(1_ms, 10);
Eric Newberry2f041d22018-06-03 18:02:31 -0700208
Davide Pesaventoe75861e2019-07-24 21:55:39 -0400209 // Check for prefix registration
210 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1);
211 BOOST_CHECK_EQUAL(face.sentInterests.front().getName().getPrefix(4), "/localhost/nfd/rib/register");
212
Davide Pesavento66777622020-10-09 18:46:03 -0400213 this->advanceClocks(1_s, 4);
Davide Pesaventoe75861e2019-07-24 21:55:39 -0400214
Davide Pesavento94dff002019-07-24 23:35:05 -0400215 BOOST_CHECK(poke->getResult() == NdnPoke::Result::TIMEOUT);
Davide Pesaventoe75861e2019-07-24 21:55:39 -0400216 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
217
218 // Check for prefix unregistration
219 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 2); // One for registration, one for unregistration
220 BOOST_CHECK_EQUAL(face.sentInterests.back().getName().getPrefix(4), "/localhost/nfd/rib/unregister");
221}
222
Davide Pesavento87434be2019-07-25 19:04:23 -0400223BOOST_FIXTURE_TEST_CASE(PrefixRegTimeout, NdnPokeFixture<false>)
224{
225 initialize();
226
227 poke->start();
Davide Pesavento66777622020-10-09 18:46:03 -0400228 this->advanceClocks(1_ms, 10);
Davide Pesavento87434be2019-07-25 19:04:23 -0400229
230 // Check for prefix registration
231 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1);
232 BOOST_CHECK_EQUAL(face.sentInterests.front().getName().getPrefix(4), "/localhost/nfd/rib/register");
233
Davide Pesavento66777622020-10-09 18:46:03 -0400234 this->advanceClocks(1_s, 10);
Davide Pesavento87434be2019-07-25 19:04:23 -0400235
236 BOOST_CHECK(poke->getResult() == NdnPoke::Result::PREFIX_REG_FAIL);
237 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
238}
239
Davide Pesaventoe75861e2019-07-24 21:55:39 -0400240BOOST_AUTO_TEST_CASE(OversizedPacket)
241{
242 payload << std::string(MAX_NDN_PACKET_SIZE, 'A');
243 initialize();
244
245 poke->start();
Davide Pesavento66777622020-10-09 18:46:03 -0400246 this->advanceClocks(1_ms, 10);
Eric Newberry2f041d22018-06-03 18:02:31 -0700247
Junxiao Shi20d5a0b2018-08-14 09:26:11 -0600248 face.receive(*makeInterest("/poke/test"));
Eric Newberry2f041d22018-06-03 18:02:31 -0700249 BOOST_CHECK_THROW(face.processEvents(), Face::OversizedPacketError);
250
Davide Pesavento87434be2019-07-25 19:04:23 -0400251 // No point in checking getResult() here. The exception is thrown from processEvents(),
252 // not from put(), so the result is still DATA_SENT even though no packets were sent.
Eric Newberry2f041d22018-06-03 18:02:31 -0700253 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
254}
255
256BOOST_AUTO_TEST_SUITE_END() // TestNdnPoke
257BOOST_AUTO_TEST_SUITE_END() // Peek
258
259} // namespace tests
260} // namespace peek
261} // namespace ndn