blob: d6252f902eb7d3487681589936ad27e1121f70d7 [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
83 Interest interest("/poke/test");
84 face.receive(interest);
85 this->advanceClocks(io, 1_ms, 10);
86 io.run();
87
88 BOOST_CHECK(poke->wasDataSent());
89 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
90 BOOST_CHECK_EQUAL(face.sentData.back().getName(), "/poke/test");
91 BOOST_CHECK(!face.sentData.back().getFinalBlock());
92 BOOST_CHECK_EQUAL(face.sentData.back().getFreshnessPeriod(), 0_ms);
93 BOOST_CHECK_EQUAL(face.sentData.back().getSignature().getType(), tlv::SignatureSha256WithEcdsa);
94}
95
96BOOST_AUTO_TEST_CASE(FinalBlockId)
97{
98 auto options = makeDefaultOptions();
99 options.prefixName = "/poke/test/123";
100 options.wantLastAsFinalBlockId = true;
101 initialize(options);
102
103 poke->start();
104
105 this->advanceClocks(io, 1_ms, 10);
106
107 Interest interest("/poke/test/123");
108 face.receive(interest);
109 this->advanceClocks(io, 1_ms, 10);
110 io.run();
111
112 BOOST_CHECK(poke->wasDataSent());
113 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
114 BOOST_CHECK_EQUAL(face.sentData.back().getName(), "/poke/test/123");
115 BOOST_REQUIRE(face.sentData.back().getFinalBlock());
116 BOOST_CHECK_EQUAL(*(face.sentData.back().getFinalBlock()), name::Component("123"));
117 BOOST_CHECK_EQUAL(face.sentData.back().getFreshnessPeriod(), 0_ms);
118 BOOST_CHECK_EQUAL(face.sentData.back().getSignature().getType(), tlv::SignatureSha256WithEcdsa);
119}
120
121BOOST_AUTO_TEST_CASE(FreshnessPeriod)
122{
123 auto options = makeDefaultOptions();
124 options.freshnessPeriod = make_optional<time::milliseconds>(1_s);
125 initialize(options);
126
127 poke->start();
128
129 this->advanceClocks(io, 1_ms, 10);
130
131 Interest interest("/poke/test");
132 face.receive(interest);
133 this->advanceClocks(io, 1_ms, 10);
134 io.run();
135
136 BOOST_CHECK(poke->wasDataSent());
137 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
138 BOOST_CHECK_EQUAL(face.sentData.back().getName(), "/poke/test");
139 BOOST_CHECK(!face.sentData.back().getFinalBlock());
140 BOOST_CHECK_EQUAL(face.sentData.back().getFreshnessPeriod(), 1_s);
141 BOOST_CHECK_EQUAL(face.sentData.back().getSignature().getType(), tlv::SignatureSha256WithEcdsa);
142}
143
144BOOST_AUTO_TEST_CASE(DigestSha256)
145{
146 auto options = makeDefaultOptions();
147 options.signingInfo.setSha256Signing();
148 initialize(options);
149
150 poke->start();
151
152 this->advanceClocks(io, 1_ms, 10);
153
154 Interest interest("/poke/test");
155 face.receive(interest);
156 this->advanceClocks(io, 1_ms, 10);
157 io.run();
158
159 BOOST_CHECK(poke->wasDataSent());
160 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
161 BOOST_CHECK_EQUAL(face.sentData.back().getName(), "/poke/test");
162 BOOST_CHECK(!face.sentData.back().getFinalBlock());
163 BOOST_CHECK_EQUAL(face.sentData.back().getFreshnessPeriod(), 0_ms);
164 BOOST_CHECK_EQUAL(face.sentData.back().getSignature().getType(), tlv::DigestSha256);
165}
166
167BOOST_AUTO_TEST_CASE(ForceData)
168{
169 auto options = makeDefaultOptions();
170 options.wantForceData = true;
171 initialize(options);
172
173 poke->start();
174
175 this->advanceClocks(io, 1_ms, 10);
176
177 BOOST_CHECK(poke->wasDataSent());
178 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
179 BOOST_CHECK_EQUAL(face.sentData.back().getName(), "/poke/test");
180 BOOST_CHECK(!face.sentData.back().getFinalBlock());
181 BOOST_CHECK_EQUAL(face.sentData.back().getFreshnessPeriod(), 0_ms);
182 BOOST_CHECK_EQUAL(face.sentData.back().getSignature().getType(), tlv::SignatureSha256WithEcdsa);
183}
184
185BOOST_AUTO_TEST_CASE(ExceedMaxPacketSize)
186{
187 for (size_t i = 0; i < MAX_NDN_PACKET_SIZE; i++) {
188 inData << "A";
189 }
190
191 auto options = makeDefaultOptions();
192 initialize(options);
193
194 poke->start();
195
196 this->advanceClocks(io, 1_ms, 10);
197
198 Interest interest("/poke/test");
199 face.receive(interest);
200 BOOST_CHECK_THROW(face.processEvents(), Face::OversizedPacketError);
201
202 // We can't check wasDataSent() correctly here because it will be set to true, even if put failed
203 // due to the packet being oversized.
204 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
205}
206
207BOOST_AUTO_TEST_SUITE_END() // TestNdnPoke
208BOOST_AUTO_TEST_SUITE_END() // Peek
209
210} // namespace tests
211} // namespace peek
212} // namespace ndn