blob: bed99581ab0f77701064c5b3551f5fb13a68bd20 [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"
14
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070015#include "boost-test.hpp"
Junxiao Shiaf8eeea2014-03-31 20:10:56 -070016
Alexander Afanasyev233750e2014-02-16 00:50:07 -080017using namespace std;
18namespace ndn {
19
20BOOST_AUTO_TEST_SUITE(TestBlock)
21
Alexander Afanasyev21ef2392014-03-25 12:40:22 -070022BOOST_AUTO_TEST_CASE(TlvFromBuffer)
23{
24 static const uint8_t BUFFER[] = {
25 0x01, // == 1
26 0xfc, // == 252
27 0xfd, 0x00, 0xfd, // == 253
28 0xfe, 0x00, 0x01, 0x00, 0x00, // == 65536
29 0xff, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00 // == 4294967296LL
30 };
31
32 using namespace Tlv;
33
34 const uint8_t* begin;
35 uint64_t value;
36
37 begin = BUFFER;
38 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 1, value), true);
39 begin = BUFFER;
40 BOOST_CHECK_NO_THROW(readVarNumber(begin, begin + 1));
41 BOOST_CHECK_EQUAL(value, 1);
42
43 begin = BUFFER + 1;
44 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 1, value), true);
45 begin = BUFFER + 1;
46 BOOST_CHECK_NO_THROW(readVarNumber(begin, begin + 1));
47 BOOST_CHECK_EQUAL(value, 252);
48
49 begin = BUFFER + 2;
50 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 1, value), false);
51 begin = BUFFER + 2;
52 BOOST_CHECK_THROW(readVarNumber(begin, begin + 1), Error);
53
54 begin = BUFFER + 2;
55 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 2, value), false);
56 begin = BUFFER + 2;
57 BOOST_CHECK_THROW(readVarNumber(begin, begin + 2), Error);
58
59 begin = BUFFER + 2;
60 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 3, value), true);
61 begin = BUFFER + 2;
62 BOOST_CHECK_NO_THROW(readVarNumber(begin, begin + 3));
63 BOOST_CHECK_EQUAL(value, 253);
64
65
66 begin = BUFFER + 5;
67 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 1, value), false);
68 begin = BUFFER + 5;
69 BOOST_CHECK_THROW(readVarNumber(begin, begin + 1), Error);
70
71 begin = BUFFER + 5;
72 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 4, value), false);
73 begin = BUFFER + 5;
74 BOOST_CHECK_THROW(readVarNumber(begin, begin + 4), Error);
75
76 begin = BUFFER + 5;
77 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 5, value), true);
78 begin = BUFFER + 5;
79 BOOST_CHECK_NO_THROW(readVarNumber(begin, begin + 5));
80 BOOST_CHECK_EQUAL(value, 65536);
81
82 begin = BUFFER + 10;
83 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 1, value), false);
84 begin = BUFFER + 10;
85 BOOST_CHECK_THROW(readVarNumber(begin, begin + 1), Error);
86
87 begin = BUFFER + 10;
88 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 8, value), false);
89 begin = BUFFER + 10;
90 BOOST_CHECK_THROW(readVarNumber(begin, begin + 8), Error);
91
92 begin = BUFFER + 10;
93 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 9, value), true);
94 begin = BUFFER + 10;
95 BOOST_CHECK_NO_THROW(readVarNumber(begin, begin + 9));
96 BOOST_CHECK_EQUAL(value, 4294967296LL);
97}
98
99BOOST_AUTO_TEST_CASE(TlvFromStream)
100{
101 static const uint8_t BUFFER[] = {
102 0x01, // == 1
103 0xfc, // == 252
104 0xfd, 0x00, 0xfd, // == 253
105 0xfe, 0x00, 0x01, 0x00, 0x00, // == 65536
106 0xff, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00 // == 4294967296LL
107 };
108
109 using namespace Tlv;
110
111 typedef boost::iostreams::stream<boost::iostreams::array_source> ArrayStream;
112 typedef std::istream_iterator<uint8_t> Iterator;
113
114 Iterator end; // end of stream
115 uint64_t value;
116 {
117 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 1);
118 Iterator begin(stream);
119 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), true);
120 }
121 {
122 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 1);
123 Iterator begin(stream);
124 BOOST_CHECK_NO_THROW(readVarNumber(begin, end));
125 BOOST_CHECK_EQUAL(value, 1);
126 }
127
128 {
129 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 1, 1);
130 Iterator begin(stream);
131 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), true);
132 }
133 {
134 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 1, 1);
135 Iterator begin(stream);
136 BOOST_CHECK_NO_THROW(readVarNumber(begin, end));
137 BOOST_CHECK_EQUAL(value, 252);
138 }
139
140 {
141 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 2, 1);
142 Iterator begin(stream);
143 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), false);
144 }
145 {
146 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 2, 1);
147 Iterator begin(stream);
148 BOOST_CHECK_THROW(readVarNumber(begin, end), Error);
149 }
150
151 {
152 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 2, 2);
153 Iterator begin(stream);
154 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), false);
155 }
156 {
157 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 2, 2);
158 Iterator begin(stream);
159 BOOST_CHECK_THROW(readVarNumber(begin, end), Error);
160 }
161
162 {
163 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 2, 3);
164 Iterator begin(stream);
165 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), true);
166 }
167 {
168 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 2, 3);
169 Iterator begin(stream);
170 BOOST_CHECK_NO_THROW(readVarNumber(begin, end));
171 BOOST_CHECK_EQUAL(value, 253);
172 }
173
174 {
175 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 5, 1);
176 Iterator begin(stream);
177 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), false);
178 }
179 {
180 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 5, 1);
181 Iterator begin(stream);
182 BOOST_CHECK_THROW(readVarNumber(begin, end), Error);
183 }
184
185 {
186 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 5, 4);
187 Iterator begin(stream);
188 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), false);
189 }
190 {
191 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 5, 4);
192 Iterator begin(stream);
193 BOOST_CHECK_THROW(readVarNumber(begin, end), Error);
194 }
195
196 {
197 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 5, 5);
198 Iterator begin(stream);
199 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), true);
200 }
201 {
202 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 5, 5);
203 Iterator begin(stream);
204 BOOST_CHECK_NO_THROW(readVarNumber(begin, end));
205 BOOST_CHECK_EQUAL(value, 65536);
206 }
207
208 {
209 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 10, 1);
210 Iterator begin(stream);
211 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), false);
212 }
213 {
214 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 10, 1);
215 Iterator begin(stream);
216 BOOST_CHECK_THROW(readVarNumber(begin, end), Error);
217 }
218
219 {
220 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 10, 8);
221 Iterator begin(stream);
222 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), false);
223 }
224 {
225 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 10, 8);
226 Iterator begin(stream);
227 BOOST_CHECK_THROW(readVarNumber(begin, end), Error);
228 }
229
230 {
231 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 10, 9);
232 Iterator begin(stream);
233 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), true);
234 }
235 {
236 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 10, 9);
237 Iterator begin(stream);
238 BOOST_CHECK_NO_THROW(readVarNumber(begin, end));
239 BOOST_CHECK_EQUAL(value, 4294967296LL);
240 }
241}
242
243BOOST_AUTO_TEST_CASE(NonNegativeIntegerFromBuffer)
244{
245 static const uint8_t BUFFER[] = {
246 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01
247 };
248
249 using namespace Tlv;
250
251 const uint8_t* begin;
252 uint64_t value;
253
254 begin = BUFFER;
255 BOOST_CHECK_THROW(value = readNonNegativeInteger(1, begin, begin + 0), Error);
256 BOOST_CHECK_NO_THROW(value = readNonNegativeInteger(1, begin, begin + 1));
257 BOOST_CHECK_EQUAL(value, 1);
258
259 begin = BUFFER;
260 BOOST_CHECK_THROW(value = readNonNegativeInteger(2, begin, begin + 1), Error);
261 BOOST_CHECK_NO_THROW(value = readNonNegativeInteger(2, begin, begin + 2));
262 BOOST_CHECK_EQUAL(value, 257);
263
264 begin = BUFFER;
265 BOOST_CHECK_THROW(value = readNonNegativeInteger(4, begin, begin + 3), Error);
266 BOOST_CHECK_NO_THROW(value = readNonNegativeInteger(4, begin, begin + 4));
267 BOOST_CHECK_EQUAL(value, 16843009LL);
268
269 begin = BUFFER;
270 BOOST_CHECK_THROW(value = readNonNegativeInteger(8, begin, begin + 7), Error);
271 BOOST_CHECK_NO_THROW(value = readNonNegativeInteger(8, begin, begin + 8));
272 BOOST_CHECK_EQUAL(value, 72340172838076673LL);
273
274 begin = BUFFER;
275 BOOST_CHECK_THROW(value = readNonNegativeInteger(3, begin, begin + 3), Error);
276}
277
278BOOST_AUTO_TEST_CASE(NonNegativeIntegerFromStream)
279{
280 static const uint8_t BUFFER[] = {
281 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01
282 };
283
284 using namespace Tlv;
285
286 typedef boost::iostreams::stream<boost::iostreams::array_source> ArrayStream;
287 typedef std::istream_iterator<uint8_t> Iterator;
288
289 Iterator end; // end of stream
290 uint64_t value;
291 {
292 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 0);
293 Iterator begin(stream);
294 BOOST_CHECK_THROW(value = readNonNegativeInteger(1, begin, end), Error);
295 }
296 {
297 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 1);
298 Iterator begin(stream);
299 BOOST_CHECK_NO_THROW(value = readNonNegativeInteger(1, begin, end));
300 BOOST_CHECK_EQUAL(value, 1);
301 }
302
303 {
304 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 1);
305 Iterator begin(stream);
306 BOOST_CHECK_THROW(value = readNonNegativeInteger(2, begin, end), Error);
307 }
308 {
309 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 2);
310 Iterator begin(stream);
311 BOOST_CHECK_NO_THROW(value = readNonNegativeInteger(2, begin, end));
312 BOOST_CHECK_EQUAL(value, 257);
313 }
314
315 {
316 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 3);
317 Iterator begin(stream);
318 BOOST_CHECK_THROW(value = readNonNegativeInteger(4, begin, end), Error);
319 }
320 {
321 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 4);
322 Iterator begin(stream);
323 BOOST_CHECK_NO_THROW(value = readNonNegativeInteger(4, begin, end));
324 BOOST_CHECK_EQUAL(value, 16843009LL);
325 }
326
327 {
328 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 7);
329 Iterator begin(stream);
330 BOOST_CHECK_THROW(value = readNonNegativeInteger(8, begin, end), Error);
331 }
332 {
333 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 8);
334 Iterator begin(stream);
335 BOOST_CHECK_NO_THROW(value = readNonNegativeInteger(8, begin, end));
336 BOOST_CHECK_EQUAL(value, 72340172838076673LL);
337 }
338
339 {
340 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 3);
341 Iterator begin(stream);
342 BOOST_CHECK_THROW(value = readNonNegativeInteger(3, begin, end), Error);
343 }
344}
345
346BOOST_AUTO_TEST_CASE(BlockBasic)
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -0800347{
348 EncodingBuffer buffer;
349 EncodingEstimator estimator;
350 size_t s1, s2;
351
352 // VarNumber checks
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700353
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -0800354 s1 = buffer.prependVarNumber(252);
355 s2 = estimator.prependVarNumber(252);
356 BOOST_CHECK_EQUAL(buffer.size(), 1);
357 BOOST_CHECK_EQUAL(s1, 1);
358 BOOST_CHECK_EQUAL(s2, 1);
359 buffer = EncodingBuffer();
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700360
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -0800361 s1 = buffer.prependVarNumber(253);
362 s2 = estimator.prependVarNumber(253);
363 BOOST_CHECK_EQUAL(buffer.size(), 3);
364 BOOST_CHECK_EQUAL(s1, 3);
365 BOOST_CHECK_EQUAL(s2, 3);
366 buffer = EncodingBuffer();
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700367
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -0800368 s1 = buffer.prependVarNumber(255);
369 s2 = estimator.prependVarNumber(255);
370 BOOST_CHECK_EQUAL(buffer.size(), 3);
371 BOOST_CHECK_EQUAL(s1, 3);
372 BOOST_CHECK_EQUAL(s2, 3);
373 buffer = EncodingBuffer();
374
375 s1 = buffer.prependVarNumber(65535);
376 s2 = estimator.prependVarNumber(65535);
377 BOOST_CHECK_EQUAL(buffer.size(), 3);
378 BOOST_CHECK_EQUAL(s1, 3);
379 BOOST_CHECK_EQUAL(s2, 3);
380 buffer = EncodingBuffer();
381
382 s1 = buffer.prependVarNumber(65536);
383 s2 = estimator.prependVarNumber(65536);
384 BOOST_CHECK_EQUAL(buffer.size(), 5);
385 BOOST_CHECK_EQUAL(s1, 5);
386 BOOST_CHECK_EQUAL(s2, 5);
387 buffer = EncodingBuffer();
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700388
Alexander Afanasyev21ef2392014-03-25 12:40:22 -0700389 s1 = buffer.prependVarNumber(4294967295LL);
390 s2 = estimator.prependVarNumber(4294967295LL);
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -0800391 BOOST_CHECK_EQUAL(buffer.size(), 5);
392 BOOST_CHECK_EQUAL(s1, 5);
393 BOOST_CHECK_EQUAL(s2, 5);
394 buffer = EncodingBuffer();
395
Alexander Afanasyev21ef2392014-03-25 12:40:22 -0700396 s1 = buffer.prependVarNumber(4294967296LL);
397 s2 = estimator.prependVarNumber(4294967296LL);
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -0800398 BOOST_CHECK_EQUAL(buffer.size(), 9);
399 BOOST_CHECK_EQUAL(s1, 9);
400 BOOST_CHECK_EQUAL(s2, 9);
401 buffer = EncodingBuffer();
402
403 // nonNegativeInteger checks
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700404
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -0800405 s1 = buffer.prependNonNegativeInteger(252);
406 s2 = estimator.prependNonNegativeInteger(252);
407 BOOST_CHECK_EQUAL(buffer.size(), 1);
408 BOOST_CHECK_EQUAL(s1, 1);
409 BOOST_CHECK_EQUAL(s2, 1);
410 buffer = EncodingBuffer();
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700411
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -0800412 s1 = buffer.prependNonNegativeInteger(255);
413 s2 = estimator.prependNonNegativeInteger(255);
414 BOOST_CHECK_EQUAL(buffer.size(), 1);
415 BOOST_CHECK_EQUAL(s1, 1);
416 BOOST_CHECK_EQUAL(s2, 1);
417 buffer = EncodingBuffer();
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700418
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -0800419 s1 = buffer.prependNonNegativeInteger(256);
420 s2 = estimator.prependNonNegativeInteger(256);
421 BOOST_CHECK_EQUAL(buffer.size(), 2);
422 BOOST_CHECK_EQUAL(s1, 2);
423 BOOST_CHECK_EQUAL(s2, 2);
424 buffer = EncodingBuffer();
425
426 s1 = buffer.prependNonNegativeInteger(65535);
427 s2 = estimator.prependNonNegativeInteger(65535);
428 BOOST_CHECK_EQUAL(buffer.size(), 2);
429 BOOST_CHECK_EQUAL(s1, 2);
430 BOOST_CHECK_EQUAL(s2, 2);
431 buffer = EncodingBuffer();
432
433 s1 = buffer.prependNonNegativeInteger(65536);
434 s2 = estimator.prependNonNegativeInteger(65536);
435 BOOST_CHECK_EQUAL(buffer.size(), 4);
436 BOOST_CHECK_EQUAL(s1, 4);
437 BOOST_CHECK_EQUAL(s2, 4);
438 buffer = EncodingBuffer();
439
Alexander Afanasyev21ef2392014-03-25 12:40:22 -0700440 s1 = buffer.prependNonNegativeInteger(4294967295LL);
441 s2 = estimator.prependNonNegativeInteger(4294967295LL);
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -0800442 BOOST_CHECK_EQUAL(buffer.size(), 4);
443 BOOST_CHECK_EQUAL(s1, 4);
444 BOOST_CHECK_EQUAL(s2, 4);
445 buffer = EncodingBuffer();
446
Alexander Afanasyev21ef2392014-03-25 12:40:22 -0700447 s1 = buffer.prependNonNegativeInteger(4294967296LL);
448 s2 = estimator.prependNonNegativeInteger(4294967296LL);
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -0800449 BOOST_CHECK_EQUAL(buffer.size(), 8);
450 BOOST_CHECK_EQUAL(s1, 8);
451 BOOST_CHECK_EQUAL(s2, 8);
452 buffer = EncodingBuffer();
453}
454
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700455BOOST_AUTO_TEST_CASE(EncodingBufferToBlock)
Alexander Afanasyev233750e2014-02-16 00:50:07 -0800456{
457 uint8_t value[4];
458
Alexander Afanasyev15151312014-02-16 00:53:51 -0800459 EncodingBuffer buffer;
Alexander Afanasyev233750e2014-02-16 00:50:07 -0800460 size_t length = buffer.prependByteArray(value, sizeof(value));
461 buffer.prependVarNumber(length);
462 buffer.prependVarNumber(0xe0);
463
464 Block block;
465 BOOST_REQUIRE_NO_THROW(block = buffer.block());
466 BOOST_CHECK_EQUAL(block.type(), 0xe0);
467 BOOST_CHECK_EQUAL(block.value_size(), sizeof(value));
Alexander Afanasyev15151312014-02-16 00:53:51 -0800468
469 BOOST_REQUIRE_NO_THROW(block = Block(buffer));
470 BOOST_CHECK_EQUAL(block.type(), 0xe0);
471 BOOST_CHECK_EQUAL(block.value_size(), sizeof(value));
472}
473
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700474BOOST_AUTO_TEST_CASE(BlockToBuffer)
Alexander Afanasyev15151312014-02-16 00:53:51 -0800475{
476 shared_ptr<Buffer> buf = make_shared<Buffer>(10);
477 for (int i = 0; i < 10; i++) (*buf)[i] = i;
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700478
Alexander Afanasyev15151312014-02-16 00:53:51 -0800479 Block block(0xab, buf);
480 block.encode();
481
482 EncodingBuffer buffer(0,0);
483 BOOST_REQUIRE_NO_THROW(buffer = EncodingBuffer(block));
484 BOOST_CHECK_EQUAL(buffer.size(), 12);
485 BOOST_CHECK_EQUAL(buffer.capacity(), 12);
486
487 (*buf)[1] = 0xe0;
488 (*buf)[2] = 2;
489 BOOST_REQUIRE_NO_THROW(block = Block(buf, buf->begin() + 1, buf->begin() + 5));
490 BOOST_CHECK_EQUAL(block.type(), 0xe0);
491
492 BOOST_REQUIRE_NO_THROW(buffer = EncodingBuffer(block));
493 BOOST_CHECK_EQUAL(buffer.size(), 4);
494 BOOST_CHECK_EQUAL(buffer.capacity(), 10);
Alexander Afanasyev233750e2014-02-16 00:50:07 -0800495}
496
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700497BOOST_AUTO_TEST_CASE(FromBuffer)
498{
499 const uint8_t TEST_BUFFER[] = {0x00, 0x01, 0xfa, // ok
500 0x01, 0x01, 0xfb, // ok
501 0x03, 0x02, 0xff}; // not ok
502 BufferPtr buffer(new Buffer(TEST_BUFFER, sizeof(TEST_BUFFER)));
503
504 // using BufferPtr (avoids memory copy)
505 size_t offset = 0;
506 Block testBlock;
507 BOOST_CHECK(Block::fromBuffer(buffer, offset, testBlock));
508 BOOST_CHECK_EQUAL(testBlock.type(), 0);
509 BOOST_CHECK_EQUAL(testBlock.size(), 3);
510 BOOST_CHECK_EQUAL(testBlock.value_size(), 1);
511 BOOST_CHECK_EQUAL(*testBlock.wire(), 0x00);
512 BOOST_CHECK_EQUAL(*testBlock.value(), 0xfa);
513 offset += testBlock.size();
514
515 BOOST_CHECK(Block::fromBuffer(buffer, offset, testBlock));
516 BOOST_CHECK_EQUAL(testBlock.type(), 1);
517 BOOST_CHECK_EQUAL(testBlock.size(), 3);
518 BOOST_CHECK_EQUAL(testBlock.value_size(), 1);
519 BOOST_CHECK_EQUAL(*testBlock.wire(), 0x01);
520 BOOST_CHECK_EQUAL(*testBlock.value(), 0xfb);
521 offset += testBlock.size();
522
523 BOOST_CHECK(!Block::fromBuffer(buffer, offset, testBlock));
524
525 // just buffer, copies memory
526 offset = 0;
527 BOOST_CHECK(Block::fromBuffer(TEST_BUFFER + offset, sizeof(TEST_BUFFER) - offset, testBlock));
528 BOOST_CHECK_EQUAL(testBlock.type(), 0);
529 BOOST_CHECK_EQUAL(testBlock.size(), 3);
530 BOOST_CHECK_EQUAL(testBlock.value_size(), 1);
531 BOOST_CHECK_EQUAL(*testBlock.wire(), 0x00);
532 BOOST_CHECK_EQUAL(*testBlock.value(), 0xfa);
533 offset += testBlock.size();
534
535 BOOST_CHECK(Block::fromBuffer(TEST_BUFFER + offset, sizeof(TEST_BUFFER) - offset, testBlock));
536 BOOST_CHECK_EQUAL(testBlock.type(), 1);
537 BOOST_CHECK_EQUAL(testBlock.size(), 3);
538 BOOST_CHECK_EQUAL(testBlock.value_size(), 1);
539 BOOST_CHECK_EQUAL(*testBlock.wire(), 0x01);
540 BOOST_CHECK_EQUAL(*testBlock.value(), 0xfb);
541 offset += testBlock.size();
542
543 BOOST_CHECK(!Block::fromBuffer(TEST_BUFFER + offset, sizeof(TEST_BUFFER) - offset, testBlock));
544}
545
Alexander Afanasyev21ef2392014-03-25 12:40:22 -0700546BOOST_AUTO_TEST_CASE(BlockFromStream)
547{
548 const uint8_t TEST_BUFFER[] = {0x00, 0x01, 0xfa, // ok
549 0x01, 0x01, 0xfb, // ok
550 0x03, 0x02, 0xff}; // not ok
551
552 typedef boost::iostreams::stream<boost::iostreams::array_source> ArrayStream;
553 ArrayStream stream(reinterpret_cast<const char*>(TEST_BUFFER), sizeof(TEST_BUFFER));
554
555 Block testBlock;
556 BOOST_REQUIRE_NO_THROW(testBlock = Block(stream));
557 BOOST_CHECK_EQUAL(testBlock.type(), 0);
558 BOOST_CHECK_EQUAL(testBlock.size(), 3);
559 BOOST_CHECK_EQUAL(testBlock.value_size(), 1);
560 BOOST_CHECK_EQUAL(*testBlock.wire(), 0x00);
561 BOOST_CHECK_EQUAL(*testBlock.value(), 0xfa);
562
563 BOOST_REQUIRE_NO_THROW(testBlock = Block(stream));
564 BOOST_CHECK_EQUAL(testBlock.type(), 1);
565 BOOST_CHECK_EQUAL(testBlock.size(), 3);
566 BOOST_CHECK_EQUAL(testBlock.value_size(), 1);
567 BOOST_CHECK_EQUAL(*testBlock.wire(), 0x01);
568 BOOST_CHECK_EQUAL(*testBlock.value(), 0xfb);
569
570 BOOST_CHECK_THROW(testBlock = Block(stream), Tlv::Error);
571}
572
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700573BOOST_AUTO_TEST_CASE(Equality)
574{
575 BOOST_CONCEPT_ASSERT((boost::EqualityComparable<Block>));
576
577 Block a("\x08\x00", 2);
578 Block b("\x08\x00", 2);;
579 BOOST_CHECK_EQUAL(a == b, true);
580 BOOST_CHECK_EQUAL(a != b, false);
581
582 Block c("\x06\x00", 2);
583 Block d("\x08\x00", 2);;
584 BOOST_CHECK_EQUAL(c == d, false);
585 BOOST_CHECK_EQUAL(c != d, true);
586
587 Block e("\x06\x00", 2);
588 Block f("\x06\x01\xcc", 3);;
589 BOOST_CHECK_EQUAL(e == f, false);
590 BOOST_CHECK_EQUAL(e != f, true);
591}
592
Alexander Afanasyev233750e2014-02-16 00:50:07 -0800593BOOST_AUTO_TEST_SUITE_END()
594
595} // namespace ndn