blob: ae6692712950489af62be64200c5b49410975774 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyev233750e2014-02-16 00:50:07 -08002/**
Alexander Afanasyevc169a812014-05-20 20:37:29 -04003 * Copyright (c) 2013-2014 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * 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.
Alexander Afanasyev233750e2014-02-16 00:50:07 -080020 */
21
Alexander Afanasyev233750e2014-02-16 00:50:07 -080022#include "encoding/encoding-buffer.hpp"
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070023#include "encoding/buffer-stream.hpp"
Alexander Afanasyev233750e2014-02-16 00:50:07 -080024
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070025#include "boost-test.hpp"
Junxiao Shiaf8eeea2014-03-31 20:10:56 -070026
Alexander Afanasyev233750e2014-02-16 00:50:07 -080027using namespace std;
28namespace ndn {
29
30BOOST_AUTO_TEST_SUITE(TestBlock)
31
Alexander Afanasyev21ef2392014-03-25 12:40:22 -070032BOOST_AUTO_TEST_CASE(TlvFromBuffer)
33{
34 static const uint8_t BUFFER[] = {
35 0x01, // == 1
36 0xfc, // == 252
37 0xfd, 0x00, 0xfd, // == 253
38 0xfe, 0x00, 0x01, 0x00, 0x00, // == 65536
39 0xff, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00 // == 4294967296LL
40 };
41
42 using namespace Tlv;
43
44 const uint8_t* begin;
45 uint64_t value;
46
47 begin = BUFFER;
48 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 1, value), true);
49 begin = BUFFER;
50 BOOST_CHECK_NO_THROW(readVarNumber(begin, begin + 1));
51 BOOST_CHECK_EQUAL(value, 1);
52
53 begin = BUFFER + 1;
54 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 1, value), true);
55 begin = BUFFER + 1;
56 BOOST_CHECK_NO_THROW(readVarNumber(begin, begin + 1));
57 BOOST_CHECK_EQUAL(value, 252);
58
59 begin = BUFFER + 2;
60 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 1, value), false);
61 begin = BUFFER + 2;
62 BOOST_CHECK_THROW(readVarNumber(begin, begin + 1), Error);
63
64 begin = BUFFER + 2;
65 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 2, value), false);
66 begin = BUFFER + 2;
67 BOOST_CHECK_THROW(readVarNumber(begin, begin + 2), Error);
68
69 begin = BUFFER + 2;
70 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 3, value), true);
71 begin = BUFFER + 2;
72 BOOST_CHECK_NO_THROW(readVarNumber(begin, begin + 3));
73 BOOST_CHECK_EQUAL(value, 253);
74
75
76 begin = BUFFER + 5;
77 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 1, value), false);
78 begin = BUFFER + 5;
79 BOOST_CHECK_THROW(readVarNumber(begin, begin + 1), Error);
80
81 begin = BUFFER + 5;
82 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 4, value), false);
83 begin = BUFFER + 5;
84 BOOST_CHECK_THROW(readVarNumber(begin, begin + 4), Error);
85
86 begin = BUFFER + 5;
87 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 5, value), true);
88 begin = BUFFER + 5;
89 BOOST_CHECK_NO_THROW(readVarNumber(begin, begin + 5));
90 BOOST_CHECK_EQUAL(value, 65536);
91
92 begin = BUFFER + 10;
93 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 1, value), false);
94 begin = BUFFER + 10;
95 BOOST_CHECK_THROW(readVarNumber(begin, begin + 1), Error);
96
97 begin = BUFFER + 10;
98 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 8, value), false);
99 begin = BUFFER + 10;
100 BOOST_CHECK_THROW(readVarNumber(begin, begin + 8), Error);
101
102 begin = BUFFER + 10;
103 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 9, value), true);
104 begin = BUFFER + 10;
105 BOOST_CHECK_NO_THROW(readVarNumber(begin, begin + 9));
106 BOOST_CHECK_EQUAL(value, 4294967296LL);
107}
108
109BOOST_AUTO_TEST_CASE(TlvFromStream)
110{
111 static const uint8_t BUFFER[] = {
112 0x01, // == 1
113 0xfc, // == 252
114 0xfd, 0x00, 0xfd, // == 253
115 0xfe, 0x00, 0x01, 0x00, 0x00, // == 65536
116 0xff, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00 // == 4294967296LL
117 };
118
119 using namespace Tlv;
120
121 typedef boost::iostreams::stream<boost::iostreams::array_source> ArrayStream;
122 typedef std::istream_iterator<uint8_t> Iterator;
123
124 Iterator end; // end of stream
125 uint64_t value;
126 {
127 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 1);
128 Iterator begin(stream);
129 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), true);
130 }
131 {
132 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 1);
133 Iterator begin(stream);
134 BOOST_CHECK_NO_THROW(readVarNumber(begin, end));
135 BOOST_CHECK_EQUAL(value, 1);
136 }
137
138 {
139 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 1, 1);
140 Iterator begin(stream);
141 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), true);
142 }
143 {
144 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 1, 1);
145 Iterator begin(stream);
146 BOOST_CHECK_NO_THROW(readVarNumber(begin, end));
147 BOOST_CHECK_EQUAL(value, 252);
148 }
149
150 {
151 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 2, 1);
152 Iterator begin(stream);
153 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), false);
154 }
155 {
156 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 2, 1);
157 Iterator begin(stream);
158 BOOST_CHECK_THROW(readVarNumber(begin, end), Error);
159 }
160
161 {
162 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 2, 2);
163 Iterator begin(stream);
164 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), false);
165 }
166 {
167 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 2, 2);
168 Iterator begin(stream);
169 BOOST_CHECK_THROW(readVarNumber(begin, end), Error);
170 }
171
172 {
173 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 2, 3);
174 Iterator begin(stream);
175 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), true);
176 }
177 {
178 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 2, 3);
179 Iterator begin(stream);
180 BOOST_CHECK_NO_THROW(readVarNumber(begin, end));
181 BOOST_CHECK_EQUAL(value, 253);
182 }
183
184 {
185 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 5, 1);
186 Iterator begin(stream);
187 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), false);
188 }
189 {
190 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 5, 1);
191 Iterator begin(stream);
192 BOOST_CHECK_THROW(readVarNumber(begin, end), Error);
193 }
194
195 {
196 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 5, 4);
197 Iterator begin(stream);
198 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), false);
199 }
200 {
201 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 5, 4);
202 Iterator begin(stream);
203 BOOST_CHECK_THROW(readVarNumber(begin, end), Error);
204 }
205
206 {
207 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 5, 5);
208 Iterator begin(stream);
209 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), true);
210 }
211 {
212 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 5, 5);
213 Iterator begin(stream);
214 BOOST_CHECK_NO_THROW(readVarNumber(begin, end));
215 BOOST_CHECK_EQUAL(value, 65536);
216 }
217
218 {
219 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 10, 1);
220 Iterator begin(stream);
221 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), false);
222 }
223 {
224 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 10, 1);
225 Iterator begin(stream);
226 BOOST_CHECK_THROW(readVarNumber(begin, end), Error);
227 }
228
229 {
230 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 10, 8);
231 Iterator begin(stream);
232 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), false);
233 }
234 {
235 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 10, 8);
236 Iterator begin(stream);
237 BOOST_CHECK_THROW(readVarNumber(begin, end), Error);
238 }
239
240 {
241 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 10, 9);
242 Iterator begin(stream);
243 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), true);
244 }
245 {
246 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 10, 9);
247 Iterator begin(stream);
248 BOOST_CHECK_NO_THROW(readVarNumber(begin, end));
249 BOOST_CHECK_EQUAL(value, 4294967296LL);
250 }
251}
252
253BOOST_AUTO_TEST_CASE(NonNegativeIntegerFromBuffer)
254{
255 static const uint8_t BUFFER[] = {
256 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01
257 };
258
259 using namespace Tlv;
260
261 const uint8_t* begin;
262 uint64_t value;
263
264 begin = BUFFER;
265 BOOST_CHECK_THROW(value = readNonNegativeInteger(1, begin, begin + 0), Error);
266 BOOST_CHECK_NO_THROW(value = readNonNegativeInteger(1, begin, begin + 1));
267 BOOST_CHECK_EQUAL(value, 1);
268
269 begin = BUFFER;
270 BOOST_CHECK_THROW(value = readNonNegativeInteger(2, begin, begin + 1), Error);
271 BOOST_CHECK_NO_THROW(value = readNonNegativeInteger(2, begin, begin + 2));
272 BOOST_CHECK_EQUAL(value, 257);
273
274 begin = BUFFER;
275 BOOST_CHECK_THROW(value = readNonNegativeInteger(4, begin, begin + 3), Error);
276 BOOST_CHECK_NO_THROW(value = readNonNegativeInteger(4, begin, begin + 4));
277 BOOST_CHECK_EQUAL(value, 16843009LL);
278
279 begin = BUFFER;
280 BOOST_CHECK_THROW(value = readNonNegativeInteger(8, begin, begin + 7), Error);
281 BOOST_CHECK_NO_THROW(value = readNonNegativeInteger(8, begin, begin + 8));
282 BOOST_CHECK_EQUAL(value, 72340172838076673LL);
283
284 begin = BUFFER;
285 BOOST_CHECK_THROW(value = readNonNegativeInteger(3, begin, begin + 3), Error);
286}
287
288BOOST_AUTO_TEST_CASE(NonNegativeIntegerFromStream)
289{
290 static const uint8_t BUFFER[] = {
291 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01
292 };
293
294 using namespace Tlv;
295
296 typedef boost::iostreams::stream<boost::iostreams::array_source> ArrayStream;
297 typedef std::istream_iterator<uint8_t> Iterator;
298
299 Iterator end; // end of stream
300 uint64_t value;
301 {
302 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 0);
303 Iterator begin(stream);
304 BOOST_CHECK_THROW(value = readNonNegativeInteger(1, begin, end), Error);
305 }
306 {
307 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 1);
308 Iterator begin(stream);
309 BOOST_CHECK_NO_THROW(value = readNonNegativeInteger(1, begin, end));
310 BOOST_CHECK_EQUAL(value, 1);
311 }
312
313 {
314 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 1);
315 Iterator begin(stream);
316 BOOST_CHECK_THROW(value = readNonNegativeInteger(2, begin, end), Error);
317 }
318 {
319 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 2);
320 Iterator begin(stream);
321 BOOST_CHECK_NO_THROW(value = readNonNegativeInteger(2, begin, end));
322 BOOST_CHECK_EQUAL(value, 257);
323 }
324
325 {
326 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 3);
327 Iterator begin(stream);
328 BOOST_CHECK_THROW(value = readNonNegativeInteger(4, begin, end), Error);
329 }
330 {
331 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 4);
332 Iterator begin(stream);
333 BOOST_CHECK_NO_THROW(value = readNonNegativeInteger(4, begin, end));
334 BOOST_CHECK_EQUAL(value, 16843009LL);
335 }
336
337 {
338 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 7);
339 Iterator begin(stream);
340 BOOST_CHECK_THROW(value = readNonNegativeInteger(8, begin, end), Error);
341 }
342 {
343 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 8);
344 Iterator begin(stream);
345 BOOST_CHECK_NO_THROW(value = readNonNegativeInteger(8, begin, end));
346 BOOST_CHECK_EQUAL(value, 72340172838076673LL);
347 }
348
349 {
350 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 3);
351 Iterator begin(stream);
352 BOOST_CHECK_THROW(value = readNonNegativeInteger(3, begin, end), Error);
353 }
354}
355
356BOOST_AUTO_TEST_CASE(BlockBasic)
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -0800357{
358 EncodingBuffer buffer;
359 EncodingEstimator estimator;
360 size_t s1, s2;
361
362 // VarNumber checks
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700363
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -0800364 s1 = buffer.prependVarNumber(252);
365 s2 = estimator.prependVarNumber(252);
366 BOOST_CHECK_EQUAL(buffer.size(), 1);
367 BOOST_CHECK_EQUAL(s1, 1);
368 BOOST_CHECK_EQUAL(s2, 1);
369 buffer = EncodingBuffer();
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700370
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -0800371 s1 = buffer.prependVarNumber(253);
372 s2 = estimator.prependVarNumber(253);
373 BOOST_CHECK_EQUAL(buffer.size(), 3);
374 BOOST_CHECK_EQUAL(s1, 3);
375 BOOST_CHECK_EQUAL(s2, 3);
376 buffer = EncodingBuffer();
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700377
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -0800378 s1 = buffer.prependVarNumber(255);
379 s2 = estimator.prependVarNumber(255);
380 BOOST_CHECK_EQUAL(buffer.size(), 3);
381 BOOST_CHECK_EQUAL(s1, 3);
382 BOOST_CHECK_EQUAL(s2, 3);
383 buffer = EncodingBuffer();
384
385 s1 = buffer.prependVarNumber(65535);
386 s2 = estimator.prependVarNumber(65535);
387 BOOST_CHECK_EQUAL(buffer.size(), 3);
388 BOOST_CHECK_EQUAL(s1, 3);
389 BOOST_CHECK_EQUAL(s2, 3);
390 buffer = EncodingBuffer();
391
392 s1 = buffer.prependVarNumber(65536);
393 s2 = estimator.prependVarNumber(65536);
394 BOOST_CHECK_EQUAL(buffer.size(), 5);
395 BOOST_CHECK_EQUAL(s1, 5);
396 BOOST_CHECK_EQUAL(s2, 5);
397 buffer = EncodingBuffer();
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700398
Alexander Afanasyev21ef2392014-03-25 12:40:22 -0700399 s1 = buffer.prependVarNumber(4294967295LL);
400 s2 = estimator.prependVarNumber(4294967295LL);
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -0800401 BOOST_CHECK_EQUAL(buffer.size(), 5);
402 BOOST_CHECK_EQUAL(s1, 5);
403 BOOST_CHECK_EQUAL(s2, 5);
404 buffer = EncodingBuffer();
405
Alexander Afanasyev21ef2392014-03-25 12:40:22 -0700406 s1 = buffer.prependVarNumber(4294967296LL);
407 s2 = estimator.prependVarNumber(4294967296LL);
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -0800408 BOOST_CHECK_EQUAL(buffer.size(), 9);
409 BOOST_CHECK_EQUAL(s1, 9);
410 BOOST_CHECK_EQUAL(s2, 9);
411 buffer = EncodingBuffer();
412
413 // nonNegativeInteger checks
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700414
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -0800415 s1 = buffer.prependNonNegativeInteger(252);
416 s2 = estimator.prependNonNegativeInteger(252);
417 BOOST_CHECK_EQUAL(buffer.size(), 1);
418 BOOST_CHECK_EQUAL(s1, 1);
419 BOOST_CHECK_EQUAL(s2, 1);
420 buffer = EncodingBuffer();
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700421
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -0800422 s1 = buffer.prependNonNegativeInteger(255);
423 s2 = estimator.prependNonNegativeInteger(255);
424 BOOST_CHECK_EQUAL(buffer.size(), 1);
425 BOOST_CHECK_EQUAL(s1, 1);
426 BOOST_CHECK_EQUAL(s2, 1);
427 buffer = EncodingBuffer();
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700428
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -0800429 s1 = buffer.prependNonNegativeInteger(256);
430 s2 = estimator.prependNonNegativeInteger(256);
431 BOOST_CHECK_EQUAL(buffer.size(), 2);
432 BOOST_CHECK_EQUAL(s1, 2);
433 BOOST_CHECK_EQUAL(s2, 2);
434 buffer = EncodingBuffer();
435
436 s1 = buffer.prependNonNegativeInteger(65535);
437 s2 = estimator.prependNonNegativeInteger(65535);
438 BOOST_CHECK_EQUAL(buffer.size(), 2);
439 BOOST_CHECK_EQUAL(s1, 2);
440 BOOST_CHECK_EQUAL(s2, 2);
441 buffer = EncodingBuffer();
442
443 s1 = buffer.prependNonNegativeInteger(65536);
444 s2 = estimator.prependNonNegativeInteger(65536);
445 BOOST_CHECK_EQUAL(buffer.size(), 4);
446 BOOST_CHECK_EQUAL(s1, 4);
447 BOOST_CHECK_EQUAL(s2, 4);
448 buffer = EncodingBuffer();
449
Alexander Afanasyev21ef2392014-03-25 12:40:22 -0700450 s1 = buffer.prependNonNegativeInteger(4294967295LL);
451 s2 = estimator.prependNonNegativeInteger(4294967295LL);
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -0800452 BOOST_CHECK_EQUAL(buffer.size(), 4);
453 BOOST_CHECK_EQUAL(s1, 4);
454 BOOST_CHECK_EQUAL(s2, 4);
455 buffer = EncodingBuffer();
456
Alexander Afanasyev21ef2392014-03-25 12:40:22 -0700457 s1 = buffer.prependNonNegativeInteger(4294967296LL);
458 s2 = estimator.prependNonNegativeInteger(4294967296LL);
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -0800459 BOOST_CHECK_EQUAL(buffer.size(), 8);
460 BOOST_CHECK_EQUAL(s1, 8);
461 BOOST_CHECK_EQUAL(s2, 8);
462 buffer = EncodingBuffer();
463}
464
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700465BOOST_AUTO_TEST_CASE(EncodingBufferToBlock)
Alexander Afanasyev233750e2014-02-16 00:50:07 -0800466{
467 uint8_t value[4];
468
Alexander Afanasyev15151312014-02-16 00:53:51 -0800469 EncodingBuffer buffer;
Alexander Afanasyev233750e2014-02-16 00:50:07 -0800470 size_t length = buffer.prependByteArray(value, sizeof(value));
471 buffer.prependVarNumber(length);
472 buffer.prependVarNumber(0xe0);
473
474 Block block;
475 BOOST_REQUIRE_NO_THROW(block = buffer.block());
476 BOOST_CHECK_EQUAL(block.type(), 0xe0);
477 BOOST_CHECK_EQUAL(block.value_size(), sizeof(value));
Alexander Afanasyev15151312014-02-16 00:53:51 -0800478
479 BOOST_REQUIRE_NO_THROW(block = Block(buffer));
480 BOOST_CHECK_EQUAL(block.type(), 0xe0);
481 BOOST_CHECK_EQUAL(block.value_size(), sizeof(value));
482}
483
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700484BOOST_AUTO_TEST_CASE(BlockToBuffer)
Alexander Afanasyev15151312014-02-16 00:53:51 -0800485{
486 shared_ptr<Buffer> buf = make_shared<Buffer>(10);
487 for (int i = 0; i < 10; i++) (*buf)[i] = i;
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700488
Alexander Afanasyev15151312014-02-16 00:53:51 -0800489 Block block(0xab, buf);
490 block.encode();
491
492 EncodingBuffer buffer(0,0);
493 BOOST_REQUIRE_NO_THROW(buffer = EncodingBuffer(block));
494 BOOST_CHECK_EQUAL(buffer.size(), 12);
495 BOOST_CHECK_EQUAL(buffer.capacity(), 12);
496
497 (*buf)[1] = 0xe0;
498 (*buf)[2] = 2;
499 BOOST_REQUIRE_NO_THROW(block = Block(buf, buf->begin() + 1, buf->begin() + 5));
500 BOOST_CHECK_EQUAL(block.type(), 0xe0);
501
502 BOOST_REQUIRE_NO_THROW(buffer = EncodingBuffer(block));
503 BOOST_CHECK_EQUAL(buffer.size(), 4);
504 BOOST_CHECK_EQUAL(buffer.capacity(), 10);
Alexander Afanasyev233750e2014-02-16 00:50:07 -0800505}
506
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700507BOOST_AUTO_TEST_CASE(FromBuffer)
508{
509 const uint8_t TEST_BUFFER[] = {0x00, 0x01, 0xfa, // ok
510 0x01, 0x01, 0xfb, // ok
511 0x03, 0x02, 0xff}; // not ok
512 BufferPtr buffer(new Buffer(TEST_BUFFER, sizeof(TEST_BUFFER)));
513
514 // using BufferPtr (avoids memory copy)
515 size_t offset = 0;
516 Block testBlock;
517 BOOST_CHECK(Block::fromBuffer(buffer, offset, testBlock));
518 BOOST_CHECK_EQUAL(testBlock.type(), 0);
519 BOOST_CHECK_EQUAL(testBlock.size(), 3);
520 BOOST_CHECK_EQUAL(testBlock.value_size(), 1);
521 BOOST_CHECK_EQUAL(*testBlock.wire(), 0x00);
522 BOOST_CHECK_EQUAL(*testBlock.value(), 0xfa);
523 offset += testBlock.size();
524
525 BOOST_CHECK(Block::fromBuffer(buffer, offset, testBlock));
526 BOOST_CHECK_EQUAL(testBlock.type(), 1);
527 BOOST_CHECK_EQUAL(testBlock.size(), 3);
528 BOOST_CHECK_EQUAL(testBlock.value_size(), 1);
529 BOOST_CHECK_EQUAL(*testBlock.wire(), 0x01);
530 BOOST_CHECK_EQUAL(*testBlock.value(), 0xfb);
531 offset += testBlock.size();
532
533 BOOST_CHECK(!Block::fromBuffer(buffer, offset, testBlock));
534
535 // just buffer, copies memory
536 offset = 0;
537 BOOST_CHECK(Block::fromBuffer(TEST_BUFFER + offset, sizeof(TEST_BUFFER) - offset, testBlock));
538 BOOST_CHECK_EQUAL(testBlock.type(), 0);
539 BOOST_CHECK_EQUAL(testBlock.size(), 3);
540 BOOST_CHECK_EQUAL(testBlock.value_size(), 1);
541 BOOST_CHECK_EQUAL(*testBlock.wire(), 0x00);
542 BOOST_CHECK_EQUAL(*testBlock.value(), 0xfa);
543 offset += testBlock.size();
544
545 BOOST_CHECK(Block::fromBuffer(TEST_BUFFER + offset, sizeof(TEST_BUFFER) - offset, testBlock));
546 BOOST_CHECK_EQUAL(testBlock.type(), 1);
547 BOOST_CHECK_EQUAL(testBlock.size(), 3);
548 BOOST_CHECK_EQUAL(testBlock.value_size(), 1);
549 BOOST_CHECK_EQUAL(*testBlock.wire(), 0x01);
550 BOOST_CHECK_EQUAL(*testBlock.value(), 0xfb);
551 offset += testBlock.size();
552
553 BOOST_CHECK(!Block::fromBuffer(TEST_BUFFER + offset, sizeof(TEST_BUFFER) - offset, testBlock));
554}
555
Alexander Afanasyev21ef2392014-03-25 12:40:22 -0700556BOOST_AUTO_TEST_CASE(BlockFromStream)
557{
558 const uint8_t TEST_BUFFER[] = {0x00, 0x01, 0xfa, // ok
559 0x01, 0x01, 0xfb, // ok
560 0x03, 0x02, 0xff}; // not ok
561
562 typedef boost::iostreams::stream<boost::iostreams::array_source> ArrayStream;
563 ArrayStream stream(reinterpret_cast<const char*>(TEST_BUFFER), sizeof(TEST_BUFFER));
564
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700565
Alexander Afanasyev21ef2392014-03-25 12:40:22 -0700566 Block testBlock;
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700567 BOOST_REQUIRE_NO_THROW(testBlock = Block::fromStream(stream));
Alexander Afanasyev21ef2392014-03-25 12:40:22 -0700568 BOOST_CHECK_EQUAL(testBlock.type(), 0);
569 BOOST_CHECK_EQUAL(testBlock.size(), 3);
570 BOOST_CHECK_EQUAL(testBlock.value_size(), 1);
571 BOOST_CHECK_EQUAL(*testBlock.wire(), 0x00);
572 BOOST_CHECK_EQUAL(*testBlock.value(), 0xfa);
573
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700574 BOOST_REQUIRE_NO_THROW(testBlock = Block::fromStream(stream));
Alexander Afanasyev21ef2392014-03-25 12:40:22 -0700575 BOOST_CHECK_EQUAL(testBlock.type(), 1);
576 BOOST_CHECK_EQUAL(testBlock.size(), 3);
577 BOOST_CHECK_EQUAL(testBlock.value_size(), 1);
578 BOOST_CHECK_EQUAL(*testBlock.wire(), 0x01);
579 BOOST_CHECK_EQUAL(*testBlock.value(), 0xfb);
580
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700581 BOOST_CHECK_THROW(Block::fromStream(stream), Tlv::Error);
Alexander Afanasyev21ef2392014-03-25 12:40:22 -0700582}
583
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700584BOOST_AUTO_TEST_CASE(Equality)
585{
586 BOOST_CONCEPT_ASSERT((boost::EqualityComparable<Block>));
587
588 Block a("\x08\x00", 2);
589 Block b("\x08\x00", 2);;
590 BOOST_CHECK_EQUAL(a == b, true);
591 BOOST_CHECK_EQUAL(a != b, false);
592
593 Block c("\x06\x00", 2);
594 Block d("\x08\x00", 2);;
595 BOOST_CHECK_EQUAL(c == d, false);
596 BOOST_CHECK_EQUAL(c != d, true);
597
598 Block e("\x06\x00", 2);
599 Block f("\x06\x01\xcc", 3);;
600 BOOST_CHECK_EQUAL(e == f, false);
601 BOOST_CHECK_EQUAL(e != f, true);
602}
603
Alexander Afanasyev233750e2014-02-16 00:50:07 -0800604BOOST_AUTO_TEST_SUITE_END()
605
606} // namespace ndn