blob: 8d4e78debe9068b455abb6d840e552f9eb451bff [file] [log] [blame]
Eric Newberry261dbc22015-07-22 23:18:18 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shib6e276f2017-08-14 20:10:04 +00002/*
Eric Newberry8422f572017-02-04 21:53:58 -07003 * Copyright (c) 2013-2017 Regents of the University of California.
Eric Newberry261dbc22015-07-22 23:18:18 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6 *
7 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20 */
21
22#include "lp/packet.hpp"
23
24#include "boost-test.hpp"
25
26namespace ndn {
27namespace lp {
28namespace tests {
29
Davide Pesaventoeee3e822016-11-26 19:19:34 +010030BOOST_AUTO_TEST_SUITE(Lp)
31BOOST_AUTO_TEST_SUITE(TestPacket)
Eric Newberry261dbc22015-07-22 23:18:18 -070032
33BOOST_AUTO_TEST_CASE(FieldAccess)
34{
35 Packet packet;
36
Junxiao Shib6e276f2017-08-14 20:10:04 +000037 BOOST_CHECK(packet.empty());
Eric Newberry261dbc22015-07-22 23:18:18 -070038 BOOST_CHECK(!packet.has<FragIndexField>());
39 BOOST_CHECK_EQUAL(0, packet.count<FragIndexField>());
Junxiao Shib6e276f2017-08-14 20:10:04 +000040
41 packet.set<FragIndexField>(1234);
42 BOOST_CHECK(!packet.empty());
Eric Newberry261dbc22015-07-22 23:18:18 -070043 BOOST_CHECK(packet.has<FragIndexField>());
44 BOOST_CHECK_THROW(packet.add<FragIndexField>(5678), std::length_error);
45 BOOST_CHECK_EQUAL(1, packet.count<FragIndexField>());
46 BOOST_CHECK_EQUAL(1234, packet.get<FragIndexField>(0));
47 BOOST_CHECK_THROW(packet.get<FragIndexField>(1), std::out_of_range);
48 BOOST_CHECK_THROW(packet.remove<FragIndexField>(1), std::out_of_range);
Junxiao Shib6e276f2017-08-14 20:10:04 +000049
50 packet.remove<FragIndexField>(0);
Eric Newberry261dbc22015-07-22 23:18:18 -070051 BOOST_CHECK_EQUAL(0, packet.count<FragIndexField>());
Junxiao Shib6e276f2017-08-14 20:10:04 +000052
53 packet.add<FragIndexField>(832);
54 std::vector<uint64_t> fragIndexes = packet.list<FragIndexField>();
Eric Newberry261dbc22015-07-22 23:18:18 -070055 BOOST_CHECK_EQUAL(1, fragIndexes.size());
56 BOOST_CHECK_EQUAL(832, fragIndexes.at(0));
Junxiao Shib6e276f2017-08-14 20:10:04 +000057
58 packet.clear<FragIndexField>();
Eric Newberry261dbc22015-07-22 23:18:18 -070059 BOOST_CHECK_EQUAL(0, packet.count<FragIndexField>());
Junxiao Shib6e276f2017-08-14 20:10:04 +000060 BOOST_CHECK(packet.empty());
Eric Newberry261dbc22015-07-22 23:18:18 -070061}
62
63/// \todo test field access methods with a REPEATABLE field
64
65BOOST_AUTO_TEST_CASE(EncodeFragment)
66{
67 static const uint8_t expectedBlock[] = {
Eric Newberry83872fd2015-08-06 17:01:24 -070068 0x64, 0x08, // LpPacket
69 0x51, 0x02, // Sequence
70 0x03, 0xe8,
Eric Newberry261dbc22015-07-22 23:18:18 -070071 0x50, 0x02, // Fragment
72 0x03, 0xe8,
73 };
74
75 Buffer buf(2);
76 buf[0] = 0x03;
77 buf[1] = 0xe8;
78
79 Packet packet;
80 BOOST_CHECK_NO_THROW(packet.add<FragmentField>(std::make_pair(buf.begin(), buf.end())));
Eric Newberry83872fd2015-08-06 17:01:24 -070081 BOOST_CHECK_NO_THROW(packet.add<SequenceField>(1000));
Eric Newberry261dbc22015-07-22 23:18:18 -070082 Block wire;
Eric Newberry83872fd2015-08-06 17:01:24 -070083 BOOST_CHECK_NO_THROW(wire = packet.wireEncode());
Eric Newberry261dbc22015-07-22 23:18:18 -070084 BOOST_CHECK_EQUAL_COLLECTIONS(expectedBlock, expectedBlock + sizeof(expectedBlock),
85 wire.begin(), wire.end());
86}
87
88BOOST_AUTO_TEST_CASE(EncodeSubTlv)
89{
90 static const uint8_t expectedBlock[] = {
91 0x64, 0x09, // LpPacket
92 0xfd, 0x03, 0x20, 0x05, // Nack
93 0xfd, 0x03, 0x21, 0x01, // NackReason
94 0x64,
95 };
96
97 NackHeader nack;
98 nack.setReason(NackReason::DUPLICATE);
99
100 Packet packet;
101 BOOST_CHECK_NO_THROW(packet.add<NackField>(nack));
102 Block wire;
103 BOOST_REQUIRE_NO_THROW(wire = packet.wireEncode());
104 BOOST_CHECK_EQUAL_COLLECTIONS(expectedBlock, expectedBlock + sizeof(expectedBlock),
105 wire.begin(), wire.end());
106}
107
108BOOST_AUTO_TEST_CASE(EncodeSortOrder)
109{
110 static const uint8_t expectedBlock[] = {
Eric Newberry8422f572017-02-04 21:53:58 -0700111 0x64, 0x19, // LpPacket
Eric Newberry261dbc22015-07-22 23:18:18 -0700112 0x52, 0x01, // FragIndex
113 0x00,
114 0x53, 0x01, // FragCount
115 0x01,
Eric Newberry8422f572017-02-04 21:53:58 -0700116 0xfd, 0x03, 0x44, 0x01, // Ack
117 0x02,
118 0xfd, 0x03, 0x44, 0x01, // Ack
119 0x04,
120 0xfd, 0x03, 0x44, 0x01, // Ack
121 0x03,
Eric Newberry261dbc22015-07-22 23:18:18 -0700122 0x50, 0x02, // Fragment
123 0x03, 0xe8,
124 };
125
126 Buffer frag(2);
127 frag[0] = 0x03;
128 frag[1] = 0xe8;
129
130 Packet packet;
131 BOOST_CHECK_NO_THROW(packet.add<FragmentField>(std::make_pair(frag.begin(), frag.end())));
132 BOOST_CHECK_NO_THROW(packet.add<FragIndexField>(0));
Eric Newberry8422f572017-02-04 21:53:58 -0700133 BOOST_CHECK_NO_THROW(packet.add<AckField>(2));
Eric Newberry2a890772017-06-26 12:06:15 -0700134 BOOST_REQUIRE_NO_THROW(packet.wireEncode());
Eric Newberry261dbc22015-07-22 23:18:18 -0700135 BOOST_CHECK_NO_THROW(packet.add<FragCountField>(1));
Eric Newberry2a890772017-06-26 12:06:15 -0700136 BOOST_REQUIRE_NO_THROW(packet.wireEncode());
Eric Newberry8422f572017-02-04 21:53:58 -0700137 BOOST_CHECK_NO_THROW(packet.add<AckField>(4));
Eric Newberry2a890772017-06-26 12:06:15 -0700138 BOOST_REQUIRE_NO_THROW(packet.wireEncode());
Eric Newberry8422f572017-02-04 21:53:58 -0700139 BOOST_CHECK_NO_THROW(packet.add<AckField>(3));
Eric Newberry2a890772017-06-26 12:06:15 -0700140 BOOST_REQUIRE_NO_THROW(packet.wireEncode());
Eric Newberry261dbc22015-07-22 23:18:18 -0700141 Block wire;
142 BOOST_REQUIRE_NO_THROW(wire = packet.wireEncode());
143 BOOST_CHECK_EQUAL_COLLECTIONS(expectedBlock, expectedBlock + sizeof(expectedBlock),
144 wire.begin(), wire.end());
145}
146
147BOOST_AUTO_TEST_CASE(DecodeNormal)
148{
149 static const uint8_t inputBlock[] = {
150 0x64, 0x0a, // LpPacket
151 0x52, 0x01, // FragIndex
152 0x00,
153 0x53, 0x01, // FragCount
154 0x01,
155 0x50, 0x02, // Fragment
156 0x03, 0xe8,
157 };
158
159 Packet packet;
160 Block wire(inputBlock, sizeof(inputBlock));
161 BOOST_CHECK_NO_THROW(packet.wireDecode(wire));
162 BOOST_CHECK_EQUAL(1, packet.count<FragmentField>());
163 BOOST_CHECK_EQUAL(1, packet.count<FragIndexField>());
164 BOOST_CHECK_EQUAL(1, packet.count<FragCountField>());
165 Buffer::const_iterator first, last;
166 BOOST_REQUIRE_NO_THROW(std::tie(first, last) = packet.get<FragmentField>(0));
167 BOOST_CHECK_EQUAL(2, last - first);
168 BOOST_CHECK_EQUAL(0x03, *first);
169 BOOST_CHECK_EQUAL(0xe8, *(last - 1));
170 BOOST_CHECK_EQUAL(0, packet.get<FragIndexField>(0));
171 BOOST_CHECK_EQUAL(1, packet.get<FragCountField>(0));
172}
173
174BOOST_AUTO_TEST_CASE(DecodeIdle)
175{
176 static const uint8_t inputBlock[] = {
177 0x64, 0x06, // LpPacket
178 0x52, 0x01, // FragIndex
179 0x00,
180 0x53, 0x01, // FragCount
181 0x01,
182 };
183
184 Packet packet;
185 Block wire(inputBlock, sizeof(inputBlock));
186 BOOST_CHECK_NO_THROW(packet.wireDecode(wire));
187 BOOST_CHECK_EQUAL(0, packet.count<FragmentField>());
188 BOOST_CHECK_EQUAL(1, packet.count<FragIndexField>());
189 BOOST_CHECK_EQUAL(1, packet.count<FragCountField>());
190 BOOST_CHECK_EQUAL(0, packet.get<FragIndexField>(0));
191 BOOST_CHECK_EQUAL(1, packet.get<FragCountField>(0));
192}
193
194BOOST_AUTO_TEST_CASE(DecodeFragment)
195{
196 static const uint8_t inputBlock[] = {
197 0x64, 0x04, // LpPacket
198 0x50, 0x02, // Fragment
199 0x03, 0xe8,
200 };
201
202 Packet packet;
203 Block wire(inputBlock, sizeof(inputBlock));
204 BOOST_CHECK_NO_THROW(packet.wireDecode(wire));
205 BOOST_CHECK_EQUAL(1, packet.count<FragmentField>());
206 BOOST_CHECK_EQUAL(0, packet.count<FragIndexField>());
207 Buffer::const_iterator first, last;
208 BOOST_REQUIRE_NO_THROW(std::tie(first, last) = packet.get<FragmentField>(0));
209 BOOST_CHECK_EQUAL(2, last - first);
210 BOOST_CHECK_EQUAL(0x03, *first);
211 BOOST_CHECK_EQUAL(0xe8, *(last - 1));
212}
213
214BOOST_AUTO_TEST_CASE(DecodeEmpty)
215{
216 static const uint8_t inputBlock[] = {
217 0x64, 0x00, // LpPacket
218 };
219
220 Packet packet;
221 Block wire(inputBlock, sizeof(inputBlock));
222 BOOST_CHECK_NO_THROW(packet.wireDecode(wire));
223 BOOST_CHECK_EQUAL(0, packet.count<FragmentField>());
224 BOOST_CHECK_EQUAL(0, packet.count<FragIndexField>());
225}
226
227BOOST_AUTO_TEST_CASE(DecodeRepeatedNonRepeatableHeader)
228{
229 static const uint8_t inputBlock[] = {
230 0x64, 0x06, // LpPacket
231 0x52, 0x01, // FragIndex
232 0x00,
233 0x52, 0x01, // FragIndex
234 0x01,
235 };
236
237 Packet packet;
238 Block wire(inputBlock, sizeof(inputBlock));
239 BOOST_CHECK_THROW(packet.wireDecode(wire), Packet::Error);
240}
241
Eric Newberry8422f572017-02-04 21:53:58 -0700242BOOST_AUTO_TEST_CASE(DecodeRepeatedRepeatableHeader)
243{
244 static const uint8_t inputBlock[] = {
245 0x64, 0x0f, // LpPacket
246 0xfd, 0x03, 0x44, 0x01, // Ack
247 0x01,
248 0xfd, 0x03, 0x44, 0x01, // Ack
249 0x03,
250 0xfd, 0x03, 0x44, 0x01, // Ack
251 0x02,
252 };
253
254 Packet packet;
255 Block wire(inputBlock, sizeof(inputBlock));
256 BOOST_CHECK_NO_THROW(packet.wireDecode(wire));
257 BOOST_REQUIRE_EQUAL(packet.count<AckField>(), 3);
258 BOOST_CHECK_EQUAL(packet.get<AckField>(), 1);
259 BOOST_CHECK_EQUAL(packet.get<AckField>(0), 1);
260 BOOST_CHECK_EQUAL(packet.get<AckField>(1), 3);
261 BOOST_CHECK_EQUAL(packet.get<AckField>(2), 2);
262}
263
Eric Newberry261dbc22015-07-22 23:18:18 -0700264BOOST_AUTO_TEST_CASE(DecodeRepeatedFragment)
265{
266 static const uint8_t inputBlock[] = {
267 0x64, 0x08, // LpPacket
268 0x50, 0x02, // Fragment
269 0x03, 0xe8,
270 0x50, 0x02, // Fragment
271 0x03, 0xe9,
272 };
273
274 Packet packet;
275 Block wire(inputBlock, sizeof(inputBlock));
276 BOOST_CHECK_THROW(packet.wireDecode(wire), Packet::Error);
277}
278
279BOOST_AUTO_TEST_CASE(DecodeWrongOrderAmongHeaders)
280{
281 static const uint8_t inputBlock[] = {
282 0x64, 0x0a, // LpPacket
283 0x53, 0x01, // FragCount
284 0x01,
285 0x52, 0x01, // FragIndex
286 0x00,
287 0x50, 0x02, // Fragment
288 0x03, 0xe8,
289 };
290
291 Packet packet;
292 Block wire(inputBlock, sizeof(inputBlock));
293 BOOST_CHECK_THROW(packet.wireDecode(wire), Packet::Error);
294}
295
296BOOST_AUTO_TEST_CASE(DecodeWrongOrderFragment)
297{
298 static const uint8_t inputBlock[] = {
299 0x64, 0x0a, // LpPacket
300 0x52, 0x01, // FragIndex
301 0x00,
302 0x50, 0x02, // Fragment
303 0x03, 0xe8,
304 0x53, 0x01, // FragCount
305 0x01,
306 };
307
308 Packet packet;
309 Block wire(inputBlock, sizeof(inputBlock));
310 BOOST_CHECK_THROW(packet.wireDecode(wire), Packet::Error);
311}
312
313BOOST_AUTO_TEST_CASE(DecodeIgnoredHeader)
314{
315 static const uint8_t inputBlock[] = {
316 0x64, 0x0c, // LpPacket
317 0x52, 0x01, // FragIndex
318 0x00,
Eric Newberry3ed62472016-12-11 22:11:38 -0700319 0xfd, 0x03, 0x24, 0x01, // unknown TLV-TYPE 804 (ignored)
Eric Newberry261dbc22015-07-22 23:18:18 -0700320 0x02,
321 0x50, 0x02, // Fragment
322 0x03, 0xe8,
323 };
324
325 Packet packet;
326 Block wire(inputBlock, sizeof(inputBlock));
327 BOOST_CHECK_NO_THROW(packet.wireDecode(wire));
328 BOOST_CHECK_EQUAL(1, packet.count<FragmentField>());
329 BOOST_CHECK_EQUAL(1, packet.count<FragIndexField>());
330}
331
332BOOST_AUTO_TEST_CASE(DecodeUnrecognizedHeader)
333{
334 static const uint8_t inputBlock[] = {
335 0x64, 0x0c, // LpPacket
336 0x52, 0x01, // FragIndex
337 0x00,
338 0xfd, 0x03, 0x22, 0x01, // unknown TLV-TYPE 802 (cannot ignore)
339 0x02,
340 0x50, 0x02, // Fragment
341 0x03, 0xe8,
342 };
343
344 Packet packet;
345 Block wire(inputBlock, sizeof(inputBlock));
346 BOOST_CHECK_THROW(packet.wireDecode(wire), Packet::Error);
347}
348
349BOOST_AUTO_TEST_CASE(DecodeBareNetworkLayerPacket)
350{
351 static const uint8_t inputBlock[] = {
352 0x05, 0x0a, // Interest
353 0x07, 0x02, // Name
354 0x03, 0xe8,
355 0x0a, 0x04, // Nonce
356 0x01, 0x02, 0x03, 0x04,
357 };
358
359 Packet packet;
360 Block wire(inputBlock, sizeof(inputBlock));
361 BOOST_CHECK_NO_THROW(packet.wireDecode(wire));
362 BOOST_CHECK_EQUAL(1, packet.count<FragmentField>());
363
Eric Newberry261dbc22015-07-22 23:18:18 -0700364 Block encoded;
365 BOOST_CHECK_NO_THROW(encoded = packet.wireEncode());
Eric Newberry83872fd2015-08-06 17:01:24 -0700366 BOOST_CHECK_EQUAL_COLLECTIONS(inputBlock, inputBlock + sizeof(inputBlock),
Eric Newberry261dbc22015-07-22 23:18:18 -0700367 encoded.begin(), encoded.end());
368}
369
Eric Newberry43bf6bb2015-10-09 16:12:09 -0700370BOOST_AUTO_TEST_CASE(DecodeUnrecognizedTlvType)
371{
372 Packet packet;
373 Block wire = encoding::makeEmptyBlock(ndn::tlv::Name);
374 BOOST_CHECK_THROW(packet.wireDecode(wire), Packet::Error);
375}
376
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100377BOOST_AUTO_TEST_SUITE_END() // TestPacket
378BOOST_AUTO_TEST_SUITE_END() // Lp
Eric Newberry261dbc22015-07-22 23:18:18 -0700379
380} // namespace tests
381} // namespace lp
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100382} // namespace ndn