blob: f569f039b84a78c5260ebce0afb1e247345efee2 [file] [log] [blame]
Eric Newberry2f041d22018-06-03 18:02:31 -07001/* -*- 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
32namespace ndn {
33namespace peek {
34namespace tests {
35
36using namespace ndn::tests;
37
38class NdnPokeFixture : public UnitTestTimeFixture
39{
40protected:
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
63protected:
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
71BOOST_AUTO_TEST_SUITE(Peek)
72BOOST_FIXTURE_TEST_SUITE(TestNdnPoke, NdnPokeFixture)
73
74BOOST_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 Newberry468dbdf2018-06-20 23:58:55 -070083 // 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
Junxiao Shi20d5a0b2018-08-14 09:26:11 -060087 face.receive(*makeInterest("/poke/test"));
Eric Newberry2f041d22018-06-03 18:02:31 -070088 this->advanceClocks(io, 1_ms, 10);
89 io.run();
90
91 BOOST_CHECK(poke->wasDataSent());
92 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
93 BOOST_CHECK_EQUAL(face.sentData.back().getName(), "/poke/test");
94 BOOST_CHECK(!face.sentData.back().getFinalBlock());
95 BOOST_CHECK_EQUAL(face.sentData.back().getFreshnessPeriod(), 0_ms);
96 BOOST_CHECK_EQUAL(face.sentData.back().getSignature().getType(), tlv::SignatureSha256WithEcdsa);
Eric Newberry468dbdf2018-06-20 23:58:55 -070097
98 // Check for prefix unregistration
99 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 2); // One for registration, one for unregistration
100 BOOST_CHECK_EQUAL(face.sentInterests.back().getName().getPrefix(4), "/localhost/nfd/rib/unregister");
Eric Newberry2f041d22018-06-03 18:02:31 -0700101}
102
103BOOST_AUTO_TEST_CASE(FinalBlockId)
104{
105 auto options = makeDefaultOptions();
106 options.prefixName = "/poke/test/123";
107 options.wantLastAsFinalBlockId = true;
108 initialize(options);
109
110 poke->start();
111
112 this->advanceClocks(io, 1_ms, 10);
113
Junxiao Shi20d5a0b2018-08-14 09:26:11 -0600114 face.receive(*makeInterest("/poke/test/123"));
Eric Newberry2f041d22018-06-03 18:02:31 -0700115 this->advanceClocks(io, 1_ms, 10);
116 io.run();
117
118 BOOST_CHECK(poke->wasDataSent());
119 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
120 BOOST_CHECK_EQUAL(face.sentData.back().getName(), "/poke/test/123");
121 BOOST_REQUIRE(face.sentData.back().getFinalBlock());
122 BOOST_CHECK_EQUAL(*(face.sentData.back().getFinalBlock()), name::Component("123"));
123 BOOST_CHECK_EQUAL(face.sentData.back().getFreshnessPeriod(), 0_ms);
124 BOOST_CHECK_EQUAL(face.sentData.back().getSignature().getType(), tlv::SignatureSha256WithEcdsa);
125}
126
127BOOST_AUTO_TEST_CASE(FreshnessPeriod)
128{
129 auto options = makeDefaultOptions();
130 options.freshnessPeriod = make_optional<time::milliseconds>(1_s);
131 initialize(options);
132
133 poke->start();
134
135 this->advanceClocks(io, 1_ms, 10);
136
Junxiao Shi20d5a0b2018-08-14 09:26:11 -0600137 face.receive(*makeInterest("/poke/test"));
Eric Newberry2f041d22018-06-03 18:02:31 -0700138 this->advanceClocks(io, 1_ms, 10);
139 io.run();
140
141 BOOST_CHECK(poke->wasDataSent());
142 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
143 BOOST_CHECK_EQUAL(face.sentData.back().getName(), "/poke/test");
144 BOOST_CHECK(!face.sentData.back().getFinalBlock());
145 BOOST_CHECK_EQUAL(face.sentData.back().getFreshnessPeriod(), 1_s);
146 BOOST_CHECK_EQUAL(face.sentData.back().getSignature().getType(), tlv::SignatureSha256WithEcdsa);
147}
148
149BOOST_AUTO_TEST_CASE(DigestSha256)
150{
151 auto options = makeDefaultOptions();
152 options.signingInfo.setSha256Signing();
153 initialize(options);
154
155 poke->start();
156
157 this->advanceClocks(io, 1_ms, 10);
158
Junxiao Shi20d5a0b2018-08-14 09:26:11 -0600159 face.receive(*makeInterest("/poke/test"));
Eric Newberry2f041d22018-06-03 18:02:31 -0700160 this->advanceClocks(io, 1_ms, 10);
161 io.run();
162
163 BOOST_CHECK(poke->wasDataSent());
164 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
165 BOOST_CHECK_EQUAL(face.sentData.back().getName(), "/poke/test");
166 BOOST_CHECK(!face.sentData.back().getFinalBlock());
167 BOOST_CHECK_EQUAL(face.sentData.back().getFreshnessPeriod(), 0_ms);
168 BOOST_CHECK_EQUAL(face.sentData.back().getSignature().getType(), tlv::DigestSha256);
169}
170
171BOOST_AUTO_TEST_CASE(ForceData)
172{
173 auto options = makeDefaultOptions();
174 options.wantForceData = true;
175 initialize(options);
176
177 poke->start();
178
179 this->advanceClocks(io, 1_ms, 10);
180
181 BOOST_CHECK(poke->wasDataSent());
182 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
183 BOOST_CHECK_EQUAL(face.sentData.back().getName(), "/poke/test");
184 BOOST_CHECK(!face.sentData.back().getFinalBlock());
185 BOOST_CHECK_EQUAL(face.sentData.back().getFreshnessPeriod(), 0_ms);
186 BOOST_CHECK_EQUAL(face.sentData.back().getSignature().getType(), tlv::SignatureSha256WithEcdsa);
187}
188
189BOOST_AUTO_TEST_CASE(ExceedMaxPacketSize)
190{
191 for (size_t i = 0; i < MAX_NDN_PACKET_SIZE; i++) {
192 inData << "A";
193 }
194
195 auto options = makeDefaultOptions();
196 initialize(options);
197
198 poke->start();
199
200 this->advanceClocks(io, 1_ms, 10);
201
Junxiao Shi20d5a0b2018-08-14 09:26:11 -0600202 face.receive(*makeInterest("/poke/test"));
Eric Newberry2f041d22018-06-03 18:02:31 -0700203 BOOST_CHECK_THROW(face.processEvents(), Face::OversizedPacketError);
204
205 // We can't check wasDataSent() correctly here because it will be set to true, even if put failed
206 // due to the packet being oversized.
207 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
208}
209
210BOOST_AUTO_TEST_SUITE_END() // TestNdnPoke
211BOOST_AUTO_TEST_SUITE_END() // Peek
212
213} // namespace tests
214} // namespace peek
215} // namespace ndn