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