blob: cdcd2626cd5407c851f39c8ecfa68e7c5e2a7f03 [file] [log] [blame]
Eric Newberry2f041d22018-06-03 18:02:31 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Davide Pesaventoe75861e2019-07-24 21:55:39 -04003 * Copyright (c) 2014-2019, 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 Pesaventoe75861e2019-07-24 21:55:39 -040029#include "tests/identity-management-fixture.hpp"
Eric Newberry2f041d22018-06-03 18:02:31 -070030
31#include <ndn-cxx/util/dummy-client-face.hpp>
32
33namespace ndn {
34namespace peek {
35namespace tests {
36
37using namespace ndn::tests;
38
Davide Pesavento87434be2019-07-25 19:04:23 -040039template<bool WANT_PREFIX_REG_REPLY = true>
Davide Pesaventoe75861e2019-07-24 21:55:39 -040040class NdnPokeFixture : public IdentityManagementTimeFixture
Eric Newberry2f041d22018-06-03 18:02:31 -070041{
42protected:
43 NdnPokeFixture()
Eric Newberry2f041d22018-06-03 18:02:31 -070044 {
Davide Pesaventoe75861e2019-07-24 21:55:39 -040045 m_keyChain.createIdentity("/test-id");
Eric Newberry2f041d22018-06-03 18:02:31 -070046 }
47
48 void
Davide Pesaventoe75861e2019-07-24 21:55:39 -040049 initialize(const PokeOptions& opts = makeDefaultOptions())
Eric Newberry2f041d22018-06-03 18:02:31 -070050 {
Davide Pesaventoe75861e2019-07-24 21:55:39 -040051 poke = make_unique<NdnPoke>(face, m_keyChain, payload, opts);
Eric Newberry2f041d22018-06-03 18:02:31 -070052 }
53
54 static PokeOptions
55 makeDefaultOptions()
56 {
57 PokeOptions opt;
Davide Pesaventoe75861e2019-07-24 21:55:39 -040058 opt.name = "/poke/test";
Eric Newberry2f041d22018-06-03 18:02:31 -070059 return opt;
60 }
61
62protected:
63 boost::asio::io_service io;
Davide Pesavento87434be2019-07-25 19:04:23 -040064 ndn::util::DummyClientFace face{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();
Eric Newberry2f041d22018-06-03 18:02:31 -070077 this->advanceClocks(io, 1_ms, 10);
78
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"));
Eric Newberry2f041d22018-06-03 18:02:31 -070084 this->advanceClocks(io, 1_ms, 10);
85 io.run();
86
Davide Pesavento87434be2019-07-25 19:04:23 -040087 BOOST_CHECK(poke->getResult() == NdnPoke::Result::DATA_SENT);
Eric Newberry2f041d22018-06-03 18:02:31 -070088 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 Pesaventoe75861e2019-07-24 21:55:39 -040092 BOOST_CHECK_EQUAL(face.sentData.back().getContentType(), tlv::ContentType_Blob);
93 BOOST_CHECK_EQUAL(face.sentData.back().getContent(), "150E48656C6C6F2C20776F726C64210A"_block);
Eric Newberry2f041d22018-06-03 18:02:31 -070094 BOOST_CHECK_EQUAL(face.sentData.back().getSignature().getType(), tlv::SignatureSha256WithEcdsa);
Eric Newberry468dbdf2018-06-20 23:58:55 -070095
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 Newberry2f041d22018-06-03 18:02:31 -070099}
100
Eric Newberry2f041d22018-06-03 18:02:31 -0700101BOOST_AUTO_TEST_CASE(FreshnessPeriod)
102{
103 auto options = makeDefaultOptions();
Davide Pesaventoe75861e2019-07-24 21:55:39 -0400104 options.freshnessPeriod = 1_s;
Eric Newberry2f041d22018-06-03 18:02:31 -0700105 initialize(options);
106
107 poke->start();
Eric Newberry2f041d22018-06-03 18:02:31 -0700108 this->advanceClocks(io, 1_ms, 10);
109
Junxiao Shi20d5a0b2018-08-14 09:26:11 -0600110 face.receive(*makeInterest("/poke/test"));
Eric Newberry2f041d22018-06-03 18:02:31 -0700111 this->advanceClocks(io, 1_ms, 10);
112 io.run();
113
Davide Pesavento87434be2019-07-25 19:04:23 -0400114 BOOST_CHECK(poke->getResult() == NdnPoke::Result::DATA_SENT);
Eric Newberry2f041d22018-06-03 18:02:31 -0700115 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 Pesaventoe75861e2019-07-24 21:55:39 -0400122BOOST_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 Pesavento87434be2019-07-25 19:04:23 -0400136 BOOST_CHECK(poke->getResult() == NdnPoke::Result::DATA_SENT);
Davide Pesaventoe75861e2019-07-24 21:55:39 -0400137 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 Newberry2f041d22018-06-03 18:02:31 -0700145BOOST_AUTO_TEST_CASE(DigestSha256)
146{
147 auto options = makeDefaultOptions();
148 options.signingInfo.setSha256Signing();
149 initialize(options);
150
151 poke->start();
Eric Newberry2f041d22018-06-03 18:02:31 -0700152 this->advanceClocks(io, 1_ms, 10);
153
Junxiao Shi20d5a0b2018-08-14 09:26:11 -0600154 face.receive(*makeInterest("/poke/test"));
Eric Newberry2f041d22018-06-03 18:02:31 -0700155 this->advanceClocks(io, 1_ms, 10);
156 io.run();
157
Davide Pesavento87434be2019-07-25 19:04:23 -0400158 BOOST_CHECK(poke->getResult() == NdnPoke::Result::DATA_SENT);
Eric Newberry2f041d22018-06-03 18:02:31 -0700159 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
166BOOST_AUTO_TEST_CASE(ForceData)
167{
168 auto options = makeDefaultOptions();
169 options.wantForceData = true;
170 initialize(options);
171
172 poke->start();
Eric Newberry2f041d22018-06-03 18:02:31 -0700173 this->advanceClocks(io, 1_ms, 10);
174
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);
180 BOOST_CHECK_EQUAL(face.sentData.back().getSignature().getType(), tlv::SignatureSha256WithEcdsa);
181}
182
Davide Pesaventoe75861e2019-07-24 21:55:39 -0400183BOOST_AUTO_TEST_CASE(Timeout)
Eric Newberry2f041d22018-06-03 18:02:31 -0700184{
Eric Newberry2f041d22018-06-03 18:02:31 -0700185 auto options = makeDefaultOptions();
Davide Pesaventoe75861e2019-07-24 21:55:39 -0400186 options.timeout = 4_s;
Eric Newberry2f041d22018-06-03 18:02:31 -0700187 initialize(options);
188
189 poke->start();
Davide Pesaventoe75861e2019-07-24 21:55:39 -0400190 this->advanceClocks(io, 1_ms, 10);
Eric Newberry2f041d22018-06-03 18:02:31 -0700191
Davide Pesaventoe75861e2019-07-24 21:55:39 -0400192 // 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 Pesavento94dff002019-07-24 23:35:05 -0400198 BOOST_CHECK(poke->getResult() == NdnPoke::Result::TIMEOUT);
Davide Pesaventoe75861e2019-07-24 21:55:39 -0400199 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 Pesavento87434be2019-07-25 19:04:23 -0400206BOOST_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 Pesaventoe75861e2019-07-24 21:55:39 -0400223BOOST_AUTO_TEST_CASE(OversizedPacket)
224{
225 payload << std::string(MAX_NDN_PACKET_SIZE, 'A');
226 initialize();
227
228 poke->start();
Eric Newberry2f041d22018-06-03 18:02:31 -0700229 this->advanceClocks(io, 1_ms, 10);
230
Junxiao Shi20d5a0b2018-08-14 09:26:11 -0600231 face.receive(*makeInterest("/poke/test"));
Eric Newberry2f041d22018-06-03 18:02:31 -0700232 BOOST_CHECK_THROW(face.processEvents(), Face::OversizedPacketError);
233
Davide Pesavento87434be2019-07-25 19:04:23 -0400234 // 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 Newberry2f041d22018-06-03 18:02:31 -0700236 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
237}
238
239BOOST_AUTO_TEST_SUITE_END() // TestNdnPoke
240BOOST_AUTO_TEST_SUITE_END() // Peek
241
242} // namespace tests
243} // namespace peek
244} // namespace ndn