blob: a4183f90cf3da5401932a9cb0bc6999183d30c2d [file] [log] [blame]
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
Alexander Afanasyev233750e2014-02-16 00:50:07 -08002/**
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07003 * Copyright (c) 2013-2014, Regents of the University of California.
4 * All rights reserved.
5 *
6 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
7 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
8 *
9 * This file licensed under New BSD License. See COPYING for detailed information about
10 * ndn-cxx library copyright, permissions, and redistribution restrictions.
Alexander Afanasyev233750e2014-02-16 00:50:07 -080011 */
12
Alexander Afanasyev233750e2014-02-16 00:50:07 -080013#include "encoding/encoding-buffer.hpp"
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070014#include "encoding/buffer-stream.hpp"
Alexander Afanasyev233750e2014-02-16 00:50:07 -080015
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070016#include "boost-test.hpp"
Junxiao Shiaf8eeea2014-03-31 20:10:56 -070017
Alexander Afanasyev233750e2014-02-16 00:50:07 -080018using namespace std;
19namespace ndn {
20
21BOOST_AUTO_TEST_SUITE(TestBlock)
22
Alexander Afanasyev21ef2392014-03-25 12:40:22 -070023BOOST_AUTO_TEST_CASE(TlvFromBuffer)
24{
25 static const uint8_t BUFFER[] = {
26 0x01, // == 1
27 0xfc, // == 252
28 0xfd, 0x00, 0xfd, // == 253
29 0xfe, 0x00, 0x01, 0x00, 0x00, // == 65536
30 0xff, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00 // == 4294967296LL
31 };
32
33 using namespace Tlv;
34
35 const uint8_t* begin;
36 uint64_t value;
37
38 begin = BUFFER;
39 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 1, value), true);
40 begin = BUFFER;
41 BOOST_CHECK_NO_THROW(readVarNumber(begin, begin + 1));
42 BOOST_CHECK_EQUAL(value, 1);
43
44 begin = BUFFER + 1;
45 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 1, value), true);
46 begin = BUFFER + 1;
47 BOOST_CHECK_NO_THROW(readVarNumber(begin, begin + 1));
48 BOOST_CHECK_EQUAL(value, 252);
49
50 begin = BUFFER + 2;
51 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 1, value), false);
52 begin = BUFFER + 2;
53 BOOST_CHECK_THROW(readVarNumber(begin, begin + 1), Error);
54
55 begin = BUFFER + 2;
56 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 2, value), false);
57 begin = BUFFER + 2;
58 BOOST_CHECK_THROW(readVarNumber(begin, begin + 2), Error);
59
60 begin = BUFFER + 2;
61 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 3, value), true);
62 begin = BUFFER + 2;
63 BOOST_CHECK_NO_THROW(readVarNumber(begin, begin + 3));
64 BOOST_CHECK_EQUAL(value, 253);
65
66
67 begin = BUFFER + 5;
68 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 1, value), false);
69 begin = BUFFER + 5;
70 BOOST_CHECK_THROW(readVarNumber(begin, begin + 1), Error);
71
72 begin = BUFFER + 5;
73 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 4, value), false);
74 begin = BUFFER + 5;
75 BOOST_CHECK_THROW(readVarNumber(begin, begin + 4), Error);
76
77 begin = BUFFER + 5;
78 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 5, value), true);
79 begin = BUFFER + 5;
80 BOOST_CHECK_NO_THROW(readVarNumber(begin, begin + 5));
81 BOOST_CHECK_EQUAL(value, 65536);
82
83 begin = BUFFER + 10;
84 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 1, value), false);
85 begin = BUFFER + 10;
86 BOOST_CHECK_THROW(readVarNumber(begin, begin + 1), Error);
87
88 begin = BUFFER + 10;
89 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 8, value), false);
90 begin = BUFFER + 10;
91 BOOST_CHECK_THROW(readVarNumber(begin, begin + 8), Error);
92
93 begin = BUFFER + 10;
94 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 9, value), true);
95 begin = BUFFER + 10;
96 BOOST_CHECK_NO_THROW(readVarNumber(begin, begin + 9));
97 BOOST_CHECK_EQUAL(value, 4294967296LL);
98}
99
100BOOST_AUTO_TEST_CASE(TlvFromStream)
101{
102 static const uint8_t BUFFER[] = {
103 0x01, // == 1
104 0xfc, // == 252
105 0xfd, 0x00, 0xfd, // == 253
106 0xfe, 0x00, 0x01, 0x00, 0x00, // == 65536
107 0xff, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00 // == 4294967296LL
108 };
109
110 using namespace Tlv;
111
112 typedef boost::iostreams::stream<boost::iostreams::array_source> ArrayStream;
113 typedef std::istream_iterator<uint8_t> Iterator;
114
115 Iterator end; // end of stream
116 uint64_t value;
117 {
118 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 1);
119 Iterator begin(stream);
120 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), true);
121 }
122 {
123 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 1);
124 Iterator begin(stream);
125 BOOST_CHECK_NO_THROW(readVarNumber(begin, end));
126 BOOST_CHECK_EQUAL(value, 1);
127 }
128
129 {
130 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 1, 1);
131 Iterator begin(stream);
132 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), true);
133 }
134 {
135 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 1, 1);
136 Iterator begin(stream);
137 BOOST_CHECK_NO_THROW(readVarNumber(begin, end));
138 BOOST_CHECK_EQUAL(value, 252);
139 }
140
141 {
142 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 2, 1);
143 Iterator begin(stream);
144 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), false);
145 }
146 {
147 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 2, 1);
148 Iterator begin(stream);
149 BOOST_CHECK_THROW(readVarNumber(begin, end), Error);
150 }
151
152 {
153 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 2, 2);
154 Iterator begin(stream);
155 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), false);
156 }
157 {
158 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 2, 2);
159 Iterator begin(stream);
160 BOOST_CHECK_THROW(readVarNumber(begin, end), Error);
161 }
162
163 {
164 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 2, 3);
165 Iterator begin(stream);
166 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), true);
167 }
168 {
169 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 2, 3);
170 Iterator begin(stream);
171 BOOST_CHECK_NO_THROW(readVarNumber(begin, end));
172 BOOST_CHECK_EQUAL(value, 253);
173 }
174
175 {
176 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 5, 1);
177 Iterator begin(stream);
178 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), false);
179 }
180 {
181 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 5, 1);
182 Iterator begin(stream);
183 BOOST_CHECK_THROW(readVarNumber(begin, end), Error);
184 }
185
186 {
187 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 5, 4);
188 Iterator begin(stream);
189 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), false);
190 }
191 {
192 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 5, 4);
193 Iterator begin(stream);
194 BOOST_CHECK_THROW(readVarNumber(begin, end), Error);
195 }
196
197 {
198 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 5, 5);
199 Iterator begin(stream);
200 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), true);
201 }
202 {
203 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 5, 5);
204 Iterator begin(stream);
205 BOOST_CHECK_NO_THROW(readVarNumber(begin, end));
206 BOOST_CHECK_EQUAL(value, 65536);
207 }
208
209 {
210 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 10, 1);
211 Iterator begin(stream);
212 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), false);
213 }
214 {
215 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 10, 1);
216 Iterator begin(stream);
217 BOOST_CHECK_THROW(readVarNumber(begin, end), Error);
218 }
219
220 {
221 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 10, 8);
222 Iterator begin(stream);
223 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), false);
224 }
225 {
226 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 10, 8);
227 Iterator begin(stream);
228 BOOST_CHECK_THROW(readVarNumber(begin, end), Error);
229 }
230
231 {
232 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 10, 9);
233 Iterator begin(stream);
234 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), true);
235 }
236 {
237 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 10, 9);
238 Iterator begin(stream);
239 BOOST_CHECK_NO_THROW(readVarNumber(begin, end));
240 BOOST_CHECK_EQUAL(value, 4294967296LL);
241 }
242}
243
244BOOST_AUTO_TEST_CASE(NonNegativeIntegerFromBuffer)
245{
246 static const uint8_t BUFFER[] = {
247 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01
248 };
249
250 using namespace Tlv;
251
252 const uint8_t* begin;
253 uint64_t value;
254
255 begin = BUFFER;
256 BOOST_CHECK_THROW(value = readNonNegativeInteger(1, begin, begin + 0), Error);
257 BOOST_CHECK_NO_THROW(value = readNonNegativeInteger(1, begin, begin + 1));
258 BOOST_CHECK_EQUAL(value, 1);
259
260 begin = BUFFER;
261 BOOST_CHECK_THROW(value = readNonNegativeInteger(2, begin, begin + 1), Error);
262 BOOST_CHECK_NO_THROW(value = readNonNegativeInteger(2, begin, begin + 2));
263 BOOST_CHECK_EQUAL(value, 257);
264
265 begin = BUFFER;
266 BOOST_CHECK_THROW(value = readNonNegativeInteger(4, begin, begin + 3), Error);
267 BOOST_CHECK_NO_THROW(value = readNonNegativeInteger(4, begin, begin + 4));
268 BOOST_CHECK_EQUAL(value, 16843009LL);
269
270 begin = BUFFER;
271 BOOST_CHECK_THROW(value = readNonNegativeInteger(8, begin, begin + 7), Error);
272 BOOST_CHECK_NO_THROW(value = readNonNegativeInteger(8, begin, begin + 8));
273 BOOST_CHECK_EQUAL(value, 72340172838076673LL);
274
275 begin = BUFFER;
276 BOOST_CHECK_THROW(value = readNonNegativeInteger(3, begin, begin + 3), Error);
277}
278
279BOOST_AUTO_TEST_CASE(NonNegativeIntegerFromStream)
280{
281 static const uint8_t BUFFER[] = {
282 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01
283 };
284
285 using namespace Tlv;
286
287 typedef boost::iostreams::stream<boost::iostreams::array_source> ArrayStream;
288 typedef std::istream_iterator<uint8_t> Iterator;
289
290 Iterator end; // end of stream
291 uint64_t value;
292 {
293 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 0);
294 Iterator begin(stream);
295 BOOST_CHECK_THROW(value = readNonNegativeInteger(1, begin, end), Error);
296 }
297 {
298 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 1);
299 Iterator begin(stream);
300 BOOST_CHECK_NO_THROW(value = readNonNegativeInteger(1, begin, end));
301 BOOST_CHECK_EQUAL(value, 1);
302 }
303
304 {
305 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 1);
306 Iterator begin(stream);
307 BOOST_CHECK_THROW(value = readNonNegativeInteger(2, begin, end), Error);
308 }
309 {
310 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 2);
311 Iterator begin(stream);
312 BOOST_CHECK_NO_THROW(value = readNonNegativeInteger(2, begin, end));
313 BOOST_CHECK_EQUAL(value, 257);
314 }
315
316 {
317 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 3);
318 Iterator begin(stream);
319 BOOST_CHECK_THROW(value = readNonNegativeInteger(4, begin, end), Error);
320 }
321 {
322 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 4);
323 Iterator begin(stream);
324 BOOST_CHECK_NO_THROW(value = readNonNegativeInteger(4, begin, end));
325 BOOST_CHECK_EQUAL(value, 16843009LL);
326 }
327
328 {
329 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 7);
330 Iterator begin(stream);
331 BOOST_CHECK_THROW(value = readNonNegativeInteger(8, begin, end), Error);
332 }
333 {
334 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 8);
335 Iterator begin(stream);
336 BOOST_CHECK_NO_THROW(value = readNonNegativeInteger(8, begin, end));
337 BOOST_CHECK_EQUAL(value, 72340172838076673LL);
338 }
339
340 {
341 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 3);
342 Iterator begin(stream);
343 BOOST_CHECK_THROW(value = readNonNegativeInteger(3, begin, end), Error);
344 }
345}
346
347BOOST_AUTO_TEST_CASE(BlockBasic)
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -0800348{
349 EncodingBuffer buffer;
350 EncodingEstimator estimator;
351 size_t s1, s2;
352
353 // VarNumber checks
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700354
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -0800355 s1 = buffer.prependVarNumber(252);
356 s2 = estimator.prependVarNumber(252);
357 BOOST_CHECK_EQUAL(buffer.size(), 1);
358 BOOST_CHECK_EQUAL(s1, 1);
359 BOOST_CHECK_EQUAL(s2, 1);
360 buffer = EncodingBuffer();
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700361
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -0800362 s1 = buffer.prependVarNumber(253);
363 s2 = estimator.prependVarNumber(253);
364 BOOST_CHECK_EQUAL(buffer.size(), 3);
365 BOOST_CHECK_EQUAL(s1, 3);
366 BOOST_CHECK_EQUAL(s2, 3);
367 buffer = EncodingBuffer();
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700368
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -0800369 s1 = buffer.prependVarNumber(255);
370 s2 = estimator.prependVarNumber(255);
371 BOOST_CHECK_EQUAL(buffer.size(), 3);
372 BOOST_CHECK_EQUAL(s1, 3);
373 BOOST_CHECK_EQUAL(s2, 3);
374 buffer = EncodingBuffer();
375
376 s1 = buffer.prependVarNumber(65535);
377 s2 = estimator.prependVarNumber(65535);
378 BOOST_CHECK_EQUAL(buffer.size(), 3);
379 BOOST_CHECK_EQUAL(s1, 3);
380 BOOST_CHECK_EQUAL(s2, 3);
381 buffer = EncodingBuffer();
382
383 s1 = buffer.prependVarNumber(65536);
384 s2 = estimator.prependVarNumber(65536);
385 BOOST_CHECK_EQUAL(buffer.size(), 5);
386 BOOST_CHECK_EQUAL(s1, 5);
387 BOOST_CHECK_EQUAL(s2, 5);
388 buffer = EncodingBuffer();
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700389
Alexander Afanasyev21ef2392014-03-25 12:40:22 -0700390 s1 = buffer.prependVarNumber(4294967295LL);
391 s2 = estimator.prependVarNumber(4294967295LL);
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -0800392 BOOST_CHECK_EQUAL(buffer.size(), 5);
393 BOOST_CHECK_EQUAL(s1, 5);
394 BOOST_CHECK_EQUAL(s2, 5);
395 buffer = EncodingBuffer();
396
Alexander Afanasyev21ef2392014-03-25 12:40:22 -0700397 s1 = buffer.prependVarNumber(4294967296LL);
398 s2 = estimator.prependVarNumber(4294967296LL);
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -0800399 BOOST_CHECK_EQUAL(buffer.size(), 9);
400 BOOST_CHECK_EQUAL(s1, 9);
401 BOOST_CHECK_EQUAL(s2, 9);
402 buffer = EncodingBuffer();
403
404 // nonNegativeInteger checks
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700405
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -0800406 s1 = buffer.prependNonNegativeInteger(252);
407 s2 = estimator.prependNonNegativeInteger(252);
408 BOOST_CHECK_EQUAL(buffer.size(), 1);
409 BOOST_CHECK_EQUAL(s1, 1);
410 BOOST_CHECK_EQUAL(s2, 1);
411 buffer = EncodingBuffer();
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700412
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -0800413 s1 = buffer.prependNonNegativeInteger(255);
414 s2 = estimator.prependNonNegativeInteger(255);
415 BOOST_CHECK_EQUAL(buffer.size(), 1);
416 BOOST_CHECK_EQUAL(s1, 1);
417 BOOST_CHECK_EQUAL(s2, 1);
418 buffer = EncodingBuffer();
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700419
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -0800420 s1 = buffer.prependNonNegativeInteger(256);
421 s2 = estimator.prependNonNegativeInteger(256);
422 BOOST_CHECK_EQUAL(buffer.size(), 2);
423 BOOST_CHECK_EQUAL(s1, 2);
424 BOOST_CHECK_EQUAL(s2, 2);
425 buffer = EncodingBuffer();
426
427 s1 = buffer.prependNonNegativeInteger(65535);
428 s2 = estimator.prependNonNegativeInteger(65535);
429 BOOST_CHECK_EQUAL(buffer.size(), 2);
430 BOOST_CHECK_EQUAL(s1, 2);
431 BOOST_CHECK_EQUAL(s2, 2);
432 buffer = EncodingBuffer();
433
434 s1 = buffer.prependNonNegativeInteger(65536);
435 s2 = estimator.prependNonNegativeInteger(65536);
436 BOOST_CHECK_EQUAL(buffer.size(), 4);
437 BOOST_CHECK_EQUAL(s1, 4);
438 BOOST_CHECK_EQUAL(s2, 4);
439 buffer = EncodingBuffer();
440
Alexander Afanasyev21ef2392014-03-25 12:40:22 -0700441 s1 = buffer.prependNonNegativeInteger(4294967295LL);
442 s2 = estimator.prependNonNegativeInteger(4294967295LL);
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -0800443 BOOST_CHECK_EQUAL(buffer.size(), 4);
444 BOOST_CHECK_EQUAL(s1, 4);
445 BOOST_CHECK_EQUAL(s2, 4);
446 buffer = EncodingBuffer();
447
Alexander Afanasyev21ef2392014-03-25 12:40:22 -0700448 s1 = buffer.prependNonNegativeInteger(4294967296LL);
449 s2 = estimator.prependNonNegativeInteger(4294967296LL);
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -0800450 BOOST_CHECK_EQUAL(buffer.size(), 8);
451 BOOST_CHECK_EQUAL(s1, 8);
452 BOOST_CHECK_EQUAL(s2, 8);
453 buffer = EncodingBuffer();
454}
455
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700456BOOST_AUTO_TEST_CASE(EncodingBufferToBlock)
Alexander Afanasyev233750e2014-02-16 00:50:07 -0800457{
458 uint8_t value[4];
459
Alexander Afanasyev15151312014-02-16 00:53:51 -0800460 EncodingBuffer buffer;
Alexander Afanasyev233750e2014-02-16 00:50:07 -0800461 size_t length = buffer.prependByteArray(value, sizeof(value));
462 buffer.prependVarNumber(length);
463 buffer.prependVarNumber(0xe0);
464
465 Block block;
466 BOOST_REQUIRE_NO_THROW(block = buffer.block());
467 BOOST_CHECK_EQUAL(block.type(), 0xe0);
468 BOOST_CHECK_EQUAL(block.value_size(), sizeof(value));
Alexander Afanasyev15151312014-02-16 00:53:51 -0800469
470 BOOST_REQUIRE_NO_THROW(block = Block(buffer));
471 BOOST_CHECK_EQUAL(block.type(), 0xe0);
472 BOOST_CHECK_EQUAL(block.value_size(), sizeof(value));
473}
474
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700475BOOST_AUTO_TEST_CASE(BlockToBuffer)
Alexander Afanasyev15151312014-02-16 00:53:51 -0800476{
477 shared_ptr<Buffer> buf = make_shared<Buffer>(10);
478 for (int i = 0; i < 10; i++) (*buf)[i] = i;
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700479
Alexander Afanasyev15151312014-02-16 00:53:51 -0800480 Block block(0xab, buf);
481 block.encode();
482
483 EncodingBuffer buffer(0,0);
484 BOOST_REQUIRE_NO_THROW(buffer = EncodingBuffer(block));
485 BOOST_CHECK_EQUAL(buffer.size(), 12);
486 BOOST_CHECK_EQUAL(buffer.capacity(), 12);
487
488 (*buf)[1] = 0xe0;
489 (*buf)[2] = 2;
490 BOOST_REQUIRE_NO_THROW(block = Block(buf, buf->begin() + 1, buf->begin() + 5));
491 BOOST_CHECK_EQUAL(block.type(), 0xe0);
492
493 BOOST_REQUIRE_NO_THROW(buffer = EncodingBuffer(block));
494 BOOST_CHECK_EQUAL(buffer.size(), 4);
495 BOOST_CHECK_EQUAL(buffer.capacity(), 10);
Alexander Afanasyev233750e2014-02-16 00:50:07 -0800496}
497
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700498BOOST_AUTO_TEST_CASE(FromBuffer)
499{
500 const uint8_t TEST_BUFFER[] = {0x00, 0x01, 0xfa, // ok
501 0x01, 0x01, 0xfb, // ok
502 0x03, 0x02, 0xff}; // not ok
503 BufferPtr buffer(new Buffer(TEST_BUFFER, sizeof(TEST_BUFFER)));
504
505 // using BufferPtr (avoids memory copy)
506 size_t offset = 0;
507 Block testBlock;
508 BOOST_CHECK(Block::fromBuffer(buffer, offset, testBlock));
509 BOOST_CHECK_EQUAL(testBlock.type(), 0);
510 BOOST_CHECK_EQUAL(testBlock.size(), 3);
511 BOOST_CHECK_EQUAL(testBlock.value_size(), 1);
512 BOOST_CHECK_EQUAL(*testBlock.wire(), 0x00);
513 BOOST_CHECK_EQUAL(*testBlock.value(), 0xfa);
514 offset += testBlock.size();
515
516 BOOST_CHECK(Block::fromBuffer(buffer, offset, testBlock));
517 BOOST_CHECK_EQUAL(testBlock.type(), 1);
518 BOOST_CHECK_EQUAL(testBlock.size(), 3);
519 BOOST_CHECK_EQUAL(testBlock.value_size(), 1);
520 BOOST_CHECK_EQUAL(*testBlock.wire(), 0x01);
521 BOOST_CHECK_EQUAL(*testBlock.value(), 0xfb);
522 offset += testBlock.size();
523
524 BOOST_CHECK(!Block::fromBuffer(buffer, offset, testBlock));
525
526 // just buffer, copies memory
527 offset = 0;
528 BOOST_CHECK(Block::fromBuffer(TEST_BUFFER + offset, sizeof(TEST_BUFFER) - offset, testBlock));
529 BOOST_CHECK_EQUAL(testBlock.type(), 0);
530 BOOST_CHECK_EQUAL(testBlock.size(), 3);
531 BOOST_CHECK_EQUAL(testBlock.value_size(), 1);
532 BOOST_CHECK_EQUAL(*testBlock.wire(), 0x00);
533 BOOST_CHECK_EQUAL(*testBlock.value(), 0xfa);
534 offset += testBlock.size();
535
536 BOOST_CHECK(Block::fromBuffer(TEST_BUFFER + offset, sizeof(TEST_BUFFER) - offset, testBlock));
537 BOOST_CHECK_EQUAL(testBlock.type(), 1);
538 BOOST_CHECK_EQUAL(testBlock.size(), 3);
539 BOOST_CHECK_EQUAL(testBlock.value_size(), 1);
540 BOOST_CHECK_EQUAL(*testBlock.wire(), 0x01);
541 BOOST_CHECK_EQUAL(*testBlock.value(), 0xfb);
542 offset += testBlock.size();
543
544 BOOST_CHECK(!Block::fromBuffer(TEST_BUFFER + offset, sizeof(TEST_BUFFER) - offset, testBlock));
545}
546
Alexander Afanasyev21ef2392014-03-25 12:40:22 -0700547BOOST_AUTO_TEST_CASE(BlockFromStream)
548{
549 const uint8_t TEST_BUFFER[] = {0x00, 0x01, 0xfa, // ok
550 0x01, 0x01, 0xfb, // ok
551 0x03, 0x02, 0xff}; // not ok
552
553 typedef boost::iostreams::stream<boost::iostreams::array_source> ArrayStream;
554 ArrayStream stream(reinterpret_cast<const char*>(TEST_BUFFER), sizeof(TEST_BUFFER));
555
556 Block testBlock;
557 BOOST_REQUIRE_NO_THROW(testBlock = Block(stream));
558 BOOST_CHECK_EQUAL(testBlock.type(), 0);
559 BOOST_CHECK_EQUAL(testBlock.size(), 3);
560 BOOST_CHECK_EQUAL(testBlock.value_size(), 1);
561 BOOST_CHECK_EQUAL(*testBlock.wire(), 0x00);
562 BOOST_CHECK_EQUAL(*testBlock.value(), 0xfa);
563
564 BOOST_REQUIRE_NO_THROW(testBlock = Block(stream));
565 BOOST_CHECK_EQUAL(testBlock.type(), 1);
566 BOOST_CHECK_EQUAL(testBlock.size(), 3);
567 BOOST_CHECK_EQUAL(testBlock.value_size(), 1);
568 BOOST_CHECK_EQUAL(*testBlock.wire(), 0x01);
569 BOOST_CHECK_EQUAL(*testBlock.value(), 0xfb);
570
571 BOOST_CHECK_THROW(testBlock = Block(stream), Tlv::Error);
572}
573
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700574BOOST_AUTO_TEST_CASE(Equality)
575{
576 BOOST_CONCEPT_ASSERT((boost::EqualityComparable<Block>));
577
578 Block a("\x08\x00", 2);
579 Block b("\x08\x00", 2);;
580 BOOST_CHECK_EQUAL(a == b, true);
581 BOOST_CHECK_EQUAL(a != b, false);
582
583 Block c("\x06\x00", 2);
584 Block d("\x08\x00", 2);;
585 BOOST_CHECK_EQUAL(c == d, false);
586 BOOST_CHECK_EQUAL(c != d, true);
587
588 Block e("\x06\x00", 2);
589 Block f("\x06\x01\xcc", 3);;
590 BOOST_CHECK_EQUAL(e == f, false);
591 BOOST_CHECK_EQUAL(e != f, true);
592}
593
Alexander Afanasyev233750e2014-02-16 00:50:07 -0800594BOOST_AUTO_TEST_SUITE_END()
595
596} // namespace ndn