blob: 3f9a59b328734c2c9b88faf1efdaada10d39e116 [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 Pesaventoe75861e2019-07-24 21:55:39 -040039class NdnPokeFixture : public IdentityManagementTimeFixture
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:
62 boost::asio::io_service io;
Davide Pesaventoe75861e2019-07-24 21:55:39 -040063 ndn::util::DummyClientFace face{io, m_keyChain, {true, true}};
64 std::stringstream payload{"Hello, world!\n"};
Eric Newberry2f041d22018-06-03 18:02:31 -070065 unique_ptr<NdnPoke> poke;
Eric Newberry2f041d22018-06-03 18:02:31 -070066};
67
68BOOST_AUTO_TEST_SUITE(Peek)
69BOOST_FIXTURE_TEST_SUITE(TestNdnPoke, NdnPokeFixture)
70
71BOOST_AUTO_TEST_CASE(Basic)
72{
Davide Pesaventoe75861e2019-07-24 21:55:39 -040073 initialize();
Eric Newberry2f041d22018-06-03 18:02:31 -070074
75 poke->start();
Eric Newberry2f041d22018-06-03 18:02:31 -070076 this->advanceClocks(io, 1_ms, 10);
77
Eric Newberry468dbdf2018-06-20 23:58:55 -070078 // 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 Shi20d5a0b2018-08-14 09:26:11 -060082 face.receive(*makeInterest("/poke/test"));
Eric Newberry2f041d22018-06-03 18:02:31 -070083 this->advanceClocks(io, 1_ms, 10);
84 io.run();
85
Davide Pesaventoe75861e2019-07-24 21:55:39 -040086 BOOST_CHECK(poke->didSendData());
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);
Eric Newberry2f041d22018-06-03 18:02:31 -070093 BOOST_CHECK_EQUAL(face.sentData.back().getSignature().getType(), 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
Eric Newberry2f041d22018-06-03 18:02:31 -0700100BOOST_AUTO_TEST_CASE(FreshnessPeriod)
101{
102 auto options = makeDefaultOptions();
Davide Pesaventoe75861e2019-07-24 21:55:39 -0400103 options.freshnessPeriod = 1_s;
Eric Newberry2f041d22018-06-03 18:02:31 -0700104 initialize(options);
105
106 poke->start();
Eric Newberry2f041d22018-06-03 18:02:31 -0700107 this->advanceClocks(io, 1_ms, 10);
108
Junxiao Shi20d5a0b2018-08-14 09:26:11 -0600109 face.receive(*makeInterest("/poke/test"));
Eric Newberry2f041d22018-06-03 18:02:31 -0700110 this->advanceClocks(io, 1_ms, 10);
111 io.run();
112
Davide Pesaventoe75861e2019-07-24 21:55:39 -0400113 BOOST_CHECK(poke->didSendData());
Eric Newberry2f041d22018-06-03 18:02:31 -0700114 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 Pesaventoe75861e2019-07-24 21:55:39 -0400121BOOST_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 Newberry2f041d22018-06-03 18:02:31 -0700144BOOST_AUTO_TEST_CASE(DigestSha256)
145{
146 auto options = makeDefaultOptions();
147 options.signingInfo.setSha256Signing();
148 initialize(options);
149
150 poke->start();
Eric Newberry2f041d22018-06-03 18:02:31 -0700151 this->advanceClocks(io, 1_ms, 10);
152
Junxiao Shi20d5a0b2018-08-14 09:26:11 -0600153 face.receive(*makeInterest("/poke/test"));
Eric Newberry2f041d22018-06-03 18:02:31 -0700154 this->advanceClocks(io, 1_ms, 10);
155 io.run();
156
Davide Pesaventoe75861e2019-07-24 21:55:39 -0400157 BOOST_CHECK(poke->didSendData());
Eric Newberry2f041d22018-06-03 18:02:31 -0700158 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
165BOOST_AUTO_TEST_CASE(ForceData)
166{
167 auto options = makeDefaultOptions();
168 options.wantForceData = true;
169 initialize(options);
170
171 poke->start();
Eric Newberry2f041d22018-06-03 18:02:31 -0700172 this->advanceClocks(io, 1_ms, 10);
173
Davide Pesaventoe75861e2019-07-24 21:55:39 -0400174 BOOST_CHECK(poke->didSendData());
Eric Newberry2f041d22018-06-03 18:02:31 -0700175 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 Pesaventoe75861e2019-07-24 21:55:39 -0400182BOOST_AUTO_TEST_CASE(Timeout)
Eric Newberry2f041d22018-06-03 18:02:31 -0700183{
Eric Newberry2f041d22018-06-03 18:02:31 -0700184 auto options = makeDefaultOptions();
Davide Pesaventoe75861e2019-07-24 21:55:39 -0400185 options.timeout = 4_s;
Eric Newberry2f041d22018-06-03 18:02:31 -0700186 initialize(options);
187
188 poke->start();
Davide Pesaventoe75861e2019-07-24 21:55:39 -0400189 this->advanceClocks(io, 1_ms, 10);
Eric Newberry2f041d22018-06-03 18:02:31 -0700190
Davide Pesaventoe75861e2019-07-24 21:55:39 -0400191 // 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
205BOOST_AUTO_TEST_CASE(OversizedPacket)
206{
207 payload << std::string(MAX_NDN_PACKET_SIZE, 'A');
208 initialize();
209
210 poke->start();
Eric Newberry2f041d22018-06-03 18:02:31 -0700211 this->advanceClocks(io, 1_ms, 10);
212
Junxiao Shi20d5a0b2018-08-14 09:26:11 -0600213 face.receive(*makeInterest("/poke/test"));
Eric Newberry2f041d22018-06-03 18:02:31 -0700214 BOOST_CHECK_THROW(face.processEvents(), Face::OversizedPacketError);
215
Davide Pesaventoe75861e2019-07-24 21:55:39 -0400216 // 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 Newberry2f041d22018-06-03 18:02:31 -0700218 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
219}
220
221BOOST_AUTO_TEST_SUITE_END() // TestNdnPoke
222BOOST_AUTO_TEST_SUITE_END() // Peek
223
224} // namespace tests
225} // namespace peek
226} // namespace ndn