blob: 4150e0cc5adabcd06b671bf9685b293fb7b9f86e [file] [log] [blame]
Eric Newberry261dbc22015-07-22 23:18:18 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2015 Regents of the University of California.
4 *
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
30BOOST_AUTO_TEST_SUITE(LpPacket)
31
32BOOST_AUTO_TEST_CASE(FieldAccess)
33{
34 Packet packet;
35
36 BOOST_CHECK(!packet.has<FragIndexField>());
37 BOOST_CHECK_EQUAL(0, packet.count<FragIndexField>());
38 BOOST_CHECK_NO_THROW(packet.set<FragIndexField>(1234));
39 BOOST_CHECK(packet.has<FragIndexField>());
40 BOOST_CHECK_THROW(packet.add<FragIndexField>(5678), std::length_error);
41 BOOST_CHECK_EQUAL(1, packet.count<FragIndexField>());
42 BOOST_CHECK_EQUAL(1234, packet.get<FragIndexField>(0));
43 BOOST_CHECK_THROW(packet.get<FragIndexField>(1), std::out_of_range);
44 BOOST_CHECK_THROW(packet.remove<FragIndexField>(1), std::out_of_range);
45 BOOST_CHECK_NO_THROW(packet.remove<FragIndexField>(0));
46 BOOST_CHECK_EQUAL(0, packet.count<FragIndexField>());
47 BOOST_CHECK_NO_THROW(packet.add<FragIndexField>(832));
48 std::vector<uint64_t> fragIndexes;
49 BOOST_REQUIRE_NO_THROW(fragIndexes = packet.list<FragIndexField>());
50 BOOST_CHECK_EQUAL(1, fragIndexes.size());
51 BOOST_CHECK_EQUAL(832, fragIndexes.at(0));
52 BOOST_CHECK_NO_THROW(packet.clear<FragIndexField>());
53 BOOST_CHECK_EQUAL(0, packet.count<FragIndexField>());
54}
55
56/// \todo test field access methods with a REPEATABLE field
57
58BOOST_AUTO_TEST_CASE(EncodeFragment)
59{
60 static const uint8_t expectedBlock[] = {
Eric Newberry83872fd2015-08-06 17:01:24 -070061 0x64, 0x08, // LpPacket
62 0x51, 0x02, // Sequence
63 0x03, 0xe8,
Eric Newberry261dbc22015-07-22 23:18:18 -070064 0x50, 0x02, // Fragment
65 0x03, 0xe8,
66 };
67
68 Buffer buf(2);
69 buf[0] = 0x03;
70 buf[1] = 0xe8;
71
72 Packet packet;
73 BOOST_CHECK_NO_THROW(packet.add<FragmentField>(std::make_pair(buf.begin(), buf.end())));
Eric Newberry83872fd2015-08-06 17:01:24 -070074 BOOST_CHECK_NO_THROW(packet.add<SequenceField>(1000));
Eric Newberry261dbc22015-07-22 23:18:18 -070075 Block wire;
Eric Newberry83872fd2015-08-06 17:01:24 -070076 BOOST_CHECK_NO_THROW(wire = packet.wireEncode());
Eric Newberry261dbc22015-07-22 23:18:18 -070077 BOOST_CHECK_EQUAL_COLLECTIONS(expectedBlock, expectedBlock + sizeof(expectedBlock),
78 wire.begin(), wire.end());
79}
80
81BOOST_AUTO_TEST_CASE(EncodeSubTlv)
82{
83 static const uint8_t expectedBlock[] = {
84 0x64, 0x09, // LpPacket
85 0xfd, 0x03, 0x20, 0x05, // Nack
86 0xfd, 0x03, 0x21, 0x01, // NackReason
87 0x64,
88 };
89
90 NackHeader nack;
91 nack.setReason(NackReason::DUPLICATE);
92
93 Packet packet;
94 BOOST_CHECK_NO_THROW(packet.add<NackField>(nack));
95 Block wire;
96 BOOST_REQUIRE_NO_THROW(wire = packet.wireEncode());
97 BOOST_CHECK_EQUAL_COLLECTIONS(expectedBlock, expectedBlock + sizeof(expectedBlock),
98 wire.begin(), wire.end());
99}
100
101BOOST_AUTO_TEST_CASE(EncodeSortOrder)
102{
103 static const uint8_t expectedBlock[] = {
104 0x64, 0x0a, // LpPacket
105 0x52, 0x01, // FragIndex
106 0x00,
107 0x53, 0x01, // FragCount
108 0x01,
109 0x50, 0x02, // Fragment
110 0x03, 0xe8,
111 };
112
113 Buffer frag(2);
114 frag[0] = 0x03;
115 frag[1] = 0xe8;
116
117 Packet packet;
118 BOOST_CHECK_NO_THROW(packet.add<FragmentField>(std::make_pair(frag.begin(), frag.end())));
119 BOOST_CHECK_NO_THROW(packet.add<FragIndexField>(0));
120 BOOST_CHECK_NO_THROW(packet.add<FragCountField>(1));
121 Block wire;
122 BOOST_REQUIRE_NO_THROW(wire = packet.wireEncode());
123 BOOST_CHECK_EQUAL_COLLECTIONS(expectedBlock, expectedBlock + sizeof(expectedBlock),
124 wire.begin(), wire.end());
125}
126
127BOOST_AUTO_TEST_CASE(DecodeNormal)
128{
129 static const uint8_t inputBlock[] = {
130 0x64, 0x0a, // LpPacket
131 0x52, 0x01, // FragIndex
132 0x00,
133 0x53, 0x01, // FragCount
134 0x01,
135 0x50, 0x02, // Fragment
136 0x03, 0xe8,
137 };
138
139 Packet packet;
140 Block wire(inputBlock, sizeof(inputBlock));
141 BOOST_CHECK_NO_THROW(packet.wireDecode(wire));
142 BOOST_CHECK_EQUAL(1, packet.count<FragmentField>());
143 BOOST_CHECK_EQUAL(1, packet.count<FragIndexField>());
144 BOOST_CHECK_EQUAL(1, packet.count<FragCountField>());
145 Buffer::const_iterator first, last;
146 BOOST_REQUIRE_NO_THROW(std::tie(first, last) = packet.get<FragmentField>(0));
147 BOOST_CHECK_EQUAL(2, last - first);
148 BOOST_CHECK_EQUAL(0x03, *first);
149 BOOST_CHECK_EQUAL(0xe8, *(last - 1));
150 BOOST_CHECK_EQUAL(0, packet.get<FragIndexField>(0));
151 BOOST_CHECK_EQUAL(1, packet.get<FragCountField>(0));
152}
153
154BOOST_AUTO_TEST_CASE(DecodeIdle)
155{
156 static const uint8_t inputBlock[] = {
157 0x64, 0x06, // LpPacket
158 0x52, 0x01, // FragIndex
159 0x00,
160 0x53, 0x01, // FragCount
161 0x01,
162 };
163
164 Packet packet;
165 Block wire(inputBlock, sizeof(inputBlock));
166 BOOST_CHECK_NO_THROW(packet.wireDecode(wire));
167 BOOST_CHECK_EQUAL(0, packet.count<FragmentField>());
168 BOOST_CHECK_EQUAL(1, packet.count<FragIndexField>());
169 BOOST_CHECK_EQUAL(1, packet.count<FragCountField>());
170 BOOST_CHECK_EQUAL(0, packet.get<FragIndexField>(0));
171 BOOST_CHECK_EQUAL(1, packet.get<FragCountField>(0));
172}
173
174BOOST_AUTO_TEST_CASE(DecodeFragment)
175{
176 static const uint8_t inputBlock[] = {
177 0x64, 0x04, // LpPacket
178 0x50, 0x02, // Fragment
179 0x03, 0xe8,
180 };
181
182 Packet packet;
183 Block wire(inputBlock, sizeof(inputBlock));
184 BOOST_CHECK_NO_THROW(packet.wireDecode(wire));
185 BOOST_CHECK_EQUAL(1, packet.count<FragmentField>());
186 BOOST_CHECK_EQUAL(0, packet.count<FragIndexField>());
187 Buffer::const_iterator first, last;
188 BOOST_REQUIRE_NO_THROW(std::tie(first, last) = packet.get<FragmentField>(0));
189 BOOST_CHECK_EQUAL(2, last - first);
190 BOOST_CHECK_EQUAL(0x03, *first);
191 BOOST_CHECK_EQUAL(0xe8, *(last - 1));
192}
193
194BOOST_AUTO_TEST_CASE(DecodeEmpty)
195{
196 static const uint8_t inputBlock[] = {
197 0x64, 0x00, // LpPacket
198 };
199
200 Packet packet;
201 Block wire(inputBlock, sizeof(inputBlock));
202 BOOST_CHECK_NO_THROW(packet.wireDecode(wire));
203 BOOST_CHECK_EQUAL(0, packet.count<FragmentField>());
204 BOOST_CHECK_EQUAL(0, packet.count<FragIndexField>());
205}
206
207BOOST_AUTO_TEST_CASE(DecodeRepeatedNonRepeatableHeader)
208{
209 static const uint8_t inputBlock[] = {
210 0x64, 0x06, // LpPacket
211 0x52, 0x01, // FragIndex
212 0x00,
213 0x52, 0x01, // FragIndex
214 0x01,
215 };
216
217 Packet packet;
218 Block wire(inputBlock, sizeof(inputBlock));
219 BOOST_CHECK_THROW(packet.wireDecode(wire), Packet::Error);
220}
221
222BOOST_AUTO_TEST_CASE(DecodeRepeatedFragment)
223{
224 static const uint8_t inputBlock[] = {
225 0x64, 0x08, // LpPacket
226 0x50, 0x02, // Fragment
227 0x03, 0xe8,
228 0x50, 0x02, // Fragment
229 0x03, 0xe9,
230 };
231
232 Packet packet;
233 Block wire(inputBlock, sizeof(inputBlock));
234 BOOST_CHECK_THROW(packet.wireDecode(wire), Packet::Error);
235}
236
237BOOST_AUTO_TEST_CASE(DecodeWrongOrderAmongHeaders)
238{
239 static const uint8_t inputBlock[] = {
240 0x64, 0x0a, // LpPacket
241 0x53, 0x01, // FragCount
242 0x01,
243 0x52, 0x01, // FragIndex
244 0x00,
245 0x50, 0x02, // Fragment
246 0x03, 0xe8,
247 };
248
249 Packet packet;
250 Block wire(inputBlock, sizeof(inputBlock));
251 BOOST_CHECK_THROW(packet.wireDecode(wire), Packet::Error);
252}
253
254BOOST_AUTO_TEST_CASE(DecodeWrongOrderFragment)
255{
256 static const uint8_t inputBlock[] = {
257 0x64, 0x0a, // LpPacket
258 0x52, 0x01, // FragIndex
259 0x00,
260 0x50, 0x02, // Fragment
261 0x03, 0xe8,
262 0x53, 0x01, // FragCount
263 0x01,
264 };
265
266 Packet packet;
267 Block wire(inputBlock, sizeof(inputBlock));
268 BOOST_CHECK_THROW(packet.wireDecode(wire), Packet::Error);
269}
270
271BOOST_AUTO_TEST_CASE(DecodeIgnoredHeader)
272{
273 static const uint8_t inputBlock[] = {
274 0x64, 0x0c, // LpPacket
275 0x52, 0x01, // FragIndex
276 0x00,
277 0xfd, 0x03, 0x23, 0x01, // unknown TLV-TYPE 803 (ignored)
278 0x02,
279 0x50, 0x02, // Fragment
280 0x03, 0xe8,
281 };
282
283 Packet packet;
284 Block wire(inputBlock, sizeof(inputBlock));
285 BOOST_CHECK_NO_THROW(packet.wireDecode(wire));
286 BOOST_CHECK_EQUAL(1, packet.count<FragmentField>());
287 BOOST_CHECK_EQUAL(1, packet.count<FragIndexField>());
288}
289
290BOOST_AUTO_TEST_CASE(DecodeUnrecognizedHeader)
291{
292 static const uint8_t inputBlock[] = {
293 0x64, 0x0c, // LpPacket
294 0x52, 0x01, // FragIndex
295 0x00,
296 0xfd, 0x03, 0x22, 0x01, // unknown TLV-TYPE 802 (cannot ignore)
297 0x02,
298 0x50, 0x02, // Fragment
299 0x03, 0xe8,
300 };
301
302 Packet packet;
303 Block wire(inputBlock, sizeof(inputBlock));
304 BOOST_CHECK_THROW(packet.wireDecode(wire), Packet::Error);
305}
306
307BOOST_AUTO_TEST_CASE(DecodeBareNetworkLayerPacket)
308{
309 static const uint8_t inputBlock[] = {
310 0x05, 0x0a, // Interest
311 0x07, 0x02, // Name
312 0x03, 0xe8,
313 0x0a, 0x04, // Nonce
314 0x01, 0x02, 0x03, 0x04,
315 };
316
317 Packet packet;
318 Block wire(inputBlock, sizeof(inputBlock));
319 BOOST_CHECK_NO_THROW(packet.wireDecode(wire));
320 BOOST_CHECK_EQUAL(1, packet.count<FragmentField>());
321
Eric Newberry261dbc22015-07-22 23:18:18 -0700322 Block encoded;
323 BOOST_CHECK_NO_THROW(encoded = packet.wireEncode());
Eric Newberry83872fd2015-08-06 17:01:24 -0700324 BOOST_CHECK_EQUAL_COLLECTIONS(inputBlock, inputBlock + sizeof(inputBlock),
Eric Newberry261dbc22015-07-22 23:18:18 -0700325 encoded.begin(), encoded.end());
326}
327
328BOOST_AUTO_TEST_SUITE_END()
329
330} // namespace tests
331} // namespace lp
332} // namespace ndn