blob: 599f37c70106d200f4d0c7a9c2dfa7175bf7b496 [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/**
Davide Pesaventoeee3e822016-11-26 19:19:34 +01003 * Copyright (c) 2013-2016 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
Davide Pesaventoeee3e822016-11-26 19:19:34 +010022#include "encoding/block.hpp"
Joao Pereira7476ebf2015-07-07 14:54:39 -040023#include "encoding/block-helpers.hpp"
Davide Pesaventoeee3e822016-11-26 19:19:34 +010024#include "encoding/encoding-buffer.hpp"
Alexander Afanasyev233750e2014-02-16 00:50:07 -080025
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070026#include "boost-test.hpp"
Junxiao Shiaf8eeea2014-03-31 20:10:56 -070027
Davide Pesaventoeee3e822016-11-26 19:19:34 +010028#include <boost/iostreams/device/array.hpp>
29#include <boost/iostreams/stream.hpp>
30
Alexander Afanasyev233750e2014-02-16 00:50:07 -080031namespace ndn {
Alexander Afanasyev74633892015-02-08 18:08:46 -080032namespace tests {
Alexander Afanasyev233750e2014-02-16 00:50:07 -080033
Davide Pesaventoeee3e822016-11-26 19:19:34 +010034BOOST_AUTO_TEST_SUITE(Encoding)
35BOOST_AUTO_TEST_SUITE(TestBlock)
Alexander Afanasyev233750e2014-02-16 00:50:07 -080036
Alexander Afanasyev74633892015-02-08 18:08:46 -080037class BasicBlockFixture
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -080038{
Alexander Afanasyev74633892015-02-08 18:08:46 -080039public:
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -080040 EncodingBuffer buffer;
41 EncodingEstimator estimator;
Alexander Afanasyev74633892015-02-08 18:08:46 -080042};
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -080043
Alexander Afanasyev74633892015-02-08 18:08:46 -080044BOOST_FIXTURE_TEST_SUITE(Basic, BasicBlockFixture)
Alexander Afanasyev937aa782014-03-21 13:17:57 -070045
Alexander Afanasyev74633892015-02-08 18:08:46 -080046BOOST_AUTO_TEST_CASE(VarNumberOneByte1)
47{
48 size_t s1 = buffer.prependVarNumber(252);
49 size_t s2 = estimator.prependVarNumber(252);
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -080050 BOOST_CHECK_EQUAL(buffer.size(), 1);
51 BOOST_CHECK_EQUAL(s1, 1);
52 BOOST_CHECK_EQUAL(s2, 1);
Alexander Afanasyev74633892015-02-08 18:08:46 -080053}
Alexander Afanasyev937aa782014-03-21 13:17:57 -070054
Alexander Afanasyev74633892015-02-08 18:08:46 -080055BOOST_AUTO_TEST_CASE(VarNumberOneByte2)
56{
57 size_t s1 = buffer.prependVarNumber(253);
58 size_t s2 = estimator.prependVarNumber(253);
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -080059 BOOST_CHECK_EQUAL(buffer.size(), 3);
60 BOOST_CHECK_EQUAL(s1, 3);
61 BOOST_CHECK_EQUAL(s2, 3);
Alexander Afanasyev74633892015-02-08 18:08:46 -080062}
Alexander Afanasyev937aa782014-03-21 13:17:57 -070063
Alexander Afanasyev74633892015-02-08 18:08:46 -080064BOOST_AUTO_TEST_CASE(VarNumberThreeBytes1)
65{
66 size_t s1 = buffer.prependVarNumber(255);
67 size_t s2 = estimator.prependVarNumber(255);
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -080068 BOOST_CHECK_EQUAL(buffer.size(), 3);
69 BOOST_CHECK_EQUAL(s1, 3);
70 BOOST_CHECK_EQUAL(s2, 3);
Alexander Afanasyev74633892015-02-08 18:08:46 -080071}
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -080072
Alexander Afanasyev74633892015-02-08 18:08:46 -080073BOOST_AUTO_TEST_CASE(VarNumberThreeBytes2)
74{
75 size_t s1 = buffer.prependVarNumber(65535);
76 size_t s2 = estimator.prependVarNumber(65535);
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -080077 BOOST_CHECK_EQUAL(buffer.size(), 3);
78 BOOST_CHECK_EQUAL(s1, 3);
79 BOOST_CHECK_EQUAL(s2, 3);
Alexander Afanasyev74633892015-02-08 18:08:46 -080080}
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -080081
Alexander Afanasyev74633892015-02-08 18:08:46 -080082BOOST_AUTO_TEST_CASE(VarNumberFiveBytes1)
83{
84 size_t s1 = buffer.prependVarNumber(65536);
85 size_t s2 = estimator.prependVarNumber(65536);
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -080086 BOOST_CHECK_EQUAL(buffer.size(), 5);
87 BOOST_CHECK_EQUAL(s1, 5);
88 BOOST_CHECK_EQUAL(s2, 5);
Alexander Afanasyev74633892015-02-08 18:08:46 -080089}
Alexander Afanasyev937aa782014-03-21 13:17:57 -070090
Alexander Afanasyev74633892015-02-08 18:08:46 -080091BOOST_AUTO_TEST_CASE(VarNumberFiveBytes2)
92{
93 size_t s1 = buffer.prependVarNumber(4294967295LL);
94 size_t s2 = estimator.prependVarNumber(4294967295LL);
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -080095 BOOST_CHECK_EQUAL(buffer.size(), 5);
96 BOOST_CHECK_EQUAL(s1, 5);
97 BOOST_CHECK_EQUAL(s2, 5);
Alexander Afanasyev74633892015-02-08 18:08:46 -080098}
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -080099
Alexander Afanasyev74633892015-02-08 18:08:46 -0800100BOOST_AUTO_TEST_CASE(VarNumberNineBytes)
101{
102 size_t s1 = buffer.prependVarNumber(4294967296LL);
103 size_t s2 = estimator.prependVarNumber(4294967296LL);
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -0800104 BOOST_CHECK_EQUAL(buffer.size(), 9);
105 BOOST_CHECK_EQUAL(s1, 9);
106 BOOST_CHECK_EQUAL(s2, 9);
Alexander Afanasyev74633892015-02-08 18:08:46 -0800107}
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -0800108
Alexander Afanasyev74633892015-02-08 18:08:46 -0800109BOOST_AUTO_TEST_CASE(NonNegativeNumberOneByte1)
110{
111 size_t s1 = buffer.prependNonNegativeInteger(252);
112 size_t s2 = estimator.prependNonNegativeInteger(252);
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -0800113 BOOST_CHECK_EQUAL(buffer.size(), 1);
114 BOOST_CHECK_EQUAL(s1, 1);
115 BOOST_CHECK_EQUAL(s2, 1);
Alexander Afanasyev74633892015-02-08 18:08:46 -0800116}
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700117
Alexander Afanasyev74633892015-02-08 18:08:46 -0800118BOOST_AUTO_TEST_CASE(NonNegativeNumberOneByte2)
119{
120 size_t s1 = buffer.prependNonNegativeInteger(255);
121 size_t s2 = estimator.prependNonNegativeInteger(255);
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -0800122 BOOST_CHECK_EQUAL(buffer.size(), 1);
123 BOOST_CHECK_EQUAL(s1, 1);
124 BOOST_CHECK_EQUAL(s2, 1);
Alexander Afanasyev74633892015-02-08 18:08:46 -0800125}
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700126
Alexander Afanasyev74633892015-02-08 18:08:46 -0800127BOOST_AUTO_TEST_CASE(NonNegativeNumberTwoBytes1)
128{
129 size_t s1 = buffer.prependNonNegativeInteger(256);
130 size_t s2 = estimator.prependNonNegativeInteger(256);
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -0800131 BOOST_CHECK_EQUAL(buffer.size(), 2);
132 BOOST_CHECK_EQUAL(s1, 2);
133 BOOST_CHECK_EQUAL(s2, 2);
Alexander Afanasyev74633892015-02-08 18:08:46 -0800134}
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -0800135
Alexander Afanasyev74633892015-02-08 18:08:46 -0800136BOOST_AUTO_TEST_CASE(NonNegativeNumberTwoBytes2)
137{
138 size_t s1 = buffer.prependNonNegativeInteger(65535);
139 size_t s2 = estimator.prependNonNegativeInteger(65535);
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -0800140 BOOST_CHECK_EQUAL(buffer.size(), 2);
141 BOOST_CHECK_EQUAL(s1, 2);
142 BOOST_CHECK_EQUAL(s2, 2);
Alexander Afanasyev74633892015-02-08 18:08:46 -0800143}
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -0800144
Alexander Afanasyev74633892015-02-08 18:08:46 -0800145BOOST_AUTO_TEST_CASE(NonNegativeNumberFourBytes1)
146{
147 size_t s1 = buffer.prependNonNegativeInteger(65536);
148 size_t s2 = estimator.prependNonNegativeInteger(65536);
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -0800149 BOOST_CHECK_EQUAL(buffer.size(), 4);
150 BOOST_CHECK_EQUAL(s1, 4);
151 BOOST_CHECK_EQUAL(s2, 4);
Alexander Afanasyev74633892015-02-08 18:08:46 -0800152}
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -0800153
Alexander Afanasyev74633892015-02-08 18:08:46 -0800154BOOST_AUTO_TEST_CASE(NonNegativeNumberFourBytes2)
155{
156 size_t s1 = buffer.prependNonNegativeInteger(4294967295LL);
157 size_t s2 = estimator.prependNonNegativeInteger(4294967295LL);
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -0800158 BOOST_CHECK_EQUAL(buffer.size(), 4);
159 BOOST_CHECK_EQUAL(s1, 4);
160 BOOST_CHECK_EQUAL(s2, 4);
Alexander Afanasyev74633892015-02-08 18:08:46 -0800161}
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -0800162
Alexander Afanasyev74633892015-02-08 18:08:46 -0800163BOOST_AUTO_TEST_CASE(NonNegativeNumberEightBytes)
164{
165 size_t s1 = buffer.prependNonNegativeInteger(4294967296LL);
166 size_t s2 = estimator.prependNonNegativeInteger(4294967296LL);
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -0800167 BOOST_CHECK_EQUAL(buffer.size(), 8);
168 BOOST_CHECK_EQUAL(s1, 8);
169 BOOST_CHECK_EQUAL(s2, 8);
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -0800170}
171
Alexander Afanasyev74633892015-02-08 18:08:46 -0800172BOOST_AUTO_TEST_SUITE_END() // Basic
173
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100174BOOST_AUTO_TEST_SUITE(Construction)
175
176BOOST_AUTO_TEST_CASE(FromBlock)
Alexander Afanasyev4448d292015-08-09 20:11:37 -0700177{
178 static uint8_t buffer[] = {0x80, 0x06, 0x81, 0x01, 0x01, 0x82, 0x01, 0x01};
179 Block block(buffer, sizeof(buffer));
180
181 Block derivedBlock(block, block.begin(), block.end());
182 BOOST_CHECK_EQUAL(derivedBlock.wire(), block.wire()); // pointers should match
183 BOOST_CHECK(derivedBlock == block); // blocks should match
184
185 derivedBlock = Block(block, block.begin() + 2, block.begin() + 5);
186 BOOST_CHECK(derivedBlock.begin() == block.begin() + 2);
187 BOOST_CHECK(derivedBlock == Block(buffer + 2, 3));
188
189 Buffer otherBuffer(buffer, sizeof(buffer));
190 BOOST_CHECK_THROW(Block(block, otherBuffer.begin(), block.end()), Block::Error);
191 BOOST_CHECK_THROW(Block(block, block.begin(), otherBuffer.end()), Block::Error);
192 BOOST_CHECK_THROW(Block(block, otherBuffer.begin(), otherBuffer.end()), Block::Error);
193}
194
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100195BOOST_AUTO_TEST_CASE(FromBlockCopyOnWriteModifyOriginal)
Eric Newberry6235aac2015-08-13 16:03:47 -0700196{
197 static uint8_t buffer[] = {
198 0x05, 0x0b, 0x07, 0x03, 0x01, 0x02, 0x03, 0x0a, 0x04, 0x04, 0x05, 0x06, 0x07,
199 };
200
201 Block block1(buffer, sizeof(buffer));
202
203 Block block2(block1, block1.begin(), block1.end());
204 auto buf2 = block2.getBuffer();
205
206 block1.parse();
207 block1.remove(tlv::Name);
208 block1.encode();
209
210 block2.parse();
211
212 BOOST_CHECK_EQUAL_COLLECTIONS(block2.begin(), block2.end(), buffer, buffer + sizeof(buffer));
213 BOOST_CHECK_EQUAL(buf2, block2.getBuffer());
214}
215
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100216BOOST_AUTO_TEST_CASE(FromBlockCopyOnWriteModifyCopy)
Eric Newberry6235aac2015-08-13 16:03:47 -0700217{
218 static uint8_t buffer[] = {
219 0x05, 0x0b, 0x07, 0x03, 0x01, 0x02, 0x03, 0x0a, 0x04, 0x04, 0x05, 0x06, 0x07,
220 };
221
222 Block block1(buffer, sizeof(buffer));
223 auto buf1 = block1.getBuffer();
224
225 Block block2(block1, block1.begin(), block1.end());
226
227 block2.parse();
228 block2.remove(tlv::Name);
229 block2.encode();
230
231 block1.parse();
232
233 BOOST_CHECK_EQUAL_COLLECTIONS(block1.begin(), block1.end(), buffer, buffer + sizeof(buffer));
234 BOOST_CHECK_EQUAL(buf1, block1.getBuffer());
235}
236
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100237BOOST_AUTO_TEST_CASE(FromEncodingBuffer)
Alexander Afanasyev233750e2014-02-16 00:50:07 -0800238{
239 uint8_t value[4];
240
Alexander Afanasyev15151312014-02-16 00:53:51 -0800241 EncodingBuffer buffer;
Alexander Afanasyev233750e2014-02-16 00:50:07 -0800242 size_t length = buffer.prependByteArray(value, sizeof(value));
243 buffer.prependVarNumber(length);
244 buffer.prependVarNumber(0xe0);
245
246 Block block;
247 BOOST_REQUIRE_NO_THROW(block = buffer.block());
248 BOOST_CHECK_EQUAL(block.type(), 0xe0);
249 BOOST_CHECK_EQUAL(block.value_size(), sizeof(value));
Alexander Afanasyev15151312014-02-16 00:53:51 -0800250
251 BOOST_REQUIRE_NO_THROW(block = Block(buffer));
252 BOOST_CHECK_EQUAL(block.type(), 0xe0);
253 BOOST_CHECK_EQUAL(block.value_size(), sizeof(value));
254}
255
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100256BOOST_AUTO_TEST_CASE(ToEncodingBuffer)
Alexander Afanasyev15151312014-02-16 00:53:51 -0800257{
258 shared_ptr<Buffer> buf = make_shared<Buffer>(10);
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100259 for (int i = 0; i < 10; i++)
260 (*buf)[i] = i;
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700261
Alexander Afanasyev15151312014-02-16 00:53:51 -0800262 Block block(0xab, buf);
263 block.encode();
264
Alexander Afanasyev74633892015-02-08 18:08:46 -0800265 {
266 BOOST_REQUIRE_NO_THROW(EncodingBuffer(block));
267 EncodingBuffer buffer(block);
268 BOOST_CHECK_EQUAL(buffer.size(), 12);
269 BOOST_CHECK_EQUAL(buffer.capacity(), 12);
270 }
Alexander Afanasyev15151312014-02-16 00:53:51 -0800271
272 (*buf)[1] = 0xe0;
273 (*buf)[2] = 2;
274 BOOST_REQUIRE_NO_THROW(block = Block(buf, buf->begin() + 1, buf->begin() + 5));
275 BOOST_CHECK_EQUAL(block.type(), 0xe0);
276
Alexander Afanasyev74633892015-02-08 18:08:46 -0800277 {
278 BOOST_REQUIRE_NO_THROW(EncodingBuffer(block));
279 EncodingBuffer buffer(block);
280 BOOST_CHECK_EQUAL(buffer.size(), 4);
281 BOOST_CHECK_EQUAL(buffer.capacity(), 10);
282 }
Alexander Afanasyev233750e2014-02-16 00:50:07 -0800283}
284
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700285BOOST_AUTO_TEST_CASE(FromBuffer)
286{
287 const uint8_t TEST_BUFFER[] = {0x00, 0x01, 0xfa, // ok
288 0x01, 0x01, 0xfb, // ok
289 0x03, 0x02, 0xff}; // not ok
290 BufferPtr buffer(new Buffer(TEST_BUFFER, sizeof(TEST_BUFFER)));
291
292 // using BufferPtr (avoids memory copy)
293 size_t offset = 0;
Junxiao Shi02a4bf32015-02-21 21:07:46 -0700294 bool isOk = false;
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700295 Block testBlock;
Junxiao Shi02a4bf32015-02-21 21:07:46 -0700296 std::tie(isOk, testBlock) = Block::fromBuffer(buffer, offset);
297 BOOST_CHECK(isOk);
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700298 BOOST_CHECK_EQUAL(testBlock.type(), 0);
299 BOOST_CHECK_EQUAL(testBlock.size(), 3);
300 BOOST_CHECK_EQUAL(testBlock.value_size(), 1);
301 BOOST_CHECK_EQUAL(*testBlock.wire(), 0x00);
302 BOOST_CHECK_EQUAL(*testBlock.value(), 0xfa);
303 offset += testBlock.size();
304
Junxiao Shi02a4bf32015-02-21 21:07:46 -0700305 std::tie(isOk, testBlock) = Block::fromBuffer(buffer, offset);
306 BOOST_CHECK(isOk);
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700307 BOOST_CHECK_EQUAL(testBlock.type(), 1);
308 BOOST_CHECK_EQUAL(testBlock.size(), 3);
309 BOOST_CHECK_EQUAL(testBlock.value_size(), 1);
310 BOOST_CHECK_EQUAL(*testBlock.wire(), 0x01);
311 BOOST_CHECK_EQUAL(*testBlock.value(), 0xfb);
312 offset += testBlock.size();
313
Junxiao Shi02a4bf32015-02-21 21:07:46 -0700314 std::tie(isOk, testBlock) = Block::fromBuffer(buffer, offset);
315 BOOST_CHECK(!isOk);
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700316
317 // just buffer, copies memory
318 offset = 0;
Junxiao Shi02a4bf32015-02-21 21:07:46 -0700319 std::tie(isOk, testBlock) = Block::fromBuffer(TEST_BUFFER + offset,
320 sizeof(TEST_BUFFER) - offset);
321 BOOST_CHECK(isOk);
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700322 BOOST_CHECK_EQUAL(testBlock.type(), 0);
323 BOOST_CHECK_EQUAL(testBlock.size(), 3);
324 BOOST_CHECK_EQUAL(testBlock.value_size(), 1);
325 BOOST_CHECK_EQUAL(*testBlock.wire(), 0x00);
326 BOOST_CHECK_EQUAL(*testBlock.value(), 0xfa);
327 offset += testBlock.size();
328
Junxiao Shi02a4bf32015-02-21 21:07:46 -0700329 std::tie(isOk, testBlock) = Block::fromBuffer(TEST_BUFFER + offset,
330 sizeof(TEST_BUFFER) - offset);
331 BOOST_CHECK(isOk);
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700332 BOOST_CHECK_EQUAL(testBlock.type(), 1);
333 BOOST_CHECK_EQUAL(testBlock.size(), 3);
334 BOOST_CHECK_EQUAL(testBlock.value_size(), 1);
335 BOOST_CHECK_EQUAL(*testBlock.wire(), 0x01);
336 BOOST_CHECK_EQUAL(*testBlock.value(), 0xfb);
337 offset += testBlock.size();
338
Junxiao Shi02a4bf32015-02-21 21:07:46 -0700339 std::tie(isOk, testBlock) = Block::fromBuffer(TEST_BUFFER + offset,
340 sizeof(TEST_BUFFER) - offset);
341 BOOST_CHECK(!isOk);
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700342}
343
Junxiao Shif0da7892015-04-04 22:16:16 -0700344BOOST_AUTO_TEST_CASE(FromStream)
Alexander Afanasyev21ef2392014-03-25 12:40:22 -0700345{
346 const uint8_t TEST_BUFFER[] = {0x00, 0x01, 0xfa, // ok
347 0x01, 0x01, 0xfb, // ok
348 0x03, 0x02, 0xff}; // not ok
349
350 typedef boost::iostreams::stream<boost::iostreams::array_source> ArrayStream;
351 ArrayStream stream(reinterpret_cast<const char*>(TEST_BUFFER), sizeof(TEST_BUFFER));
352
353 Block testBlock;
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700354 BOOST_REQUIRE_NO_THROW(testBlock = Block::fromStream(stream));
Alexander Afanasyev21ef2392014-03-25 12:40:22 -0700355 BOOST_CHECK_EQUAL(testBlock.type(), 0);
356 BOOST_CHECK_EQUAL(testBlock.size(), 3);
357 BOOST_CHECK_EQUAL(testBlock.value_size(), 1);
358 BOOST_CHECK_EQUAL(*testBlock.wire(), 0x00);
359 BOOST_CHECK_EQUAL(*testBlock.value(), 0xfa);
360
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700361 BOOST_REQUIRE_NO_THROW(testBlock = Block::fromStream(stream));
Alexander Afanasyev21ef2392014-03-25 12:40:22 -0700362 BOOST_CHECK_EQUAL(testBlock.type(), 1);
363 BOOST_CHECK_EQUAL(testBlock.size(), 3);
364 BOOST_CHECK_EQUAL(testBlock.value_size(), 1);
365 BOOST_CHECK_EQUAL(*testBlock.wire(), 0x01);
366 BOOST_CHECK_EQUAL(*testBlock.value(), 0xfb);
367
Steve DiBenedetto54ce6682014-07-22 13:22:57 -0600368 BOOST_CHECK_THROW(Block::fromStream(stream), tlv::Error);
Alexander Afanasyev21ef2392014-03-25 12:40:22 -0700369}
370
Junxiao Shif0da7892015-04-04 22:16:16 -0700371BOOST_AUTO_TEST_CASE(FromStreamWhitespace) // Bug 2728
372{
373 uint8_t PACKET[] = {
374 0x06, 0x20, // Data
375 0x07, 0x11, // Name
376 0x08, 0x05, 0x68, 0x65, 0x6c, 0x6c, 0x6f, // NameComponent 'hello'
377 0x08, 0x01, 0x31, // NameComponent '1'
378 0x08, 0x05, 0x77, 0x6f, 0x72, 0x6c, 0x64, // NameComponent 'world'
379 0x14, 0x00, // MetaInfo empty
380 0x15, 0x00, // Content empty
381 0x16, 0x05, // SignatureInfo
382 0x1b, 0x01, 0x01, // SignatureType RSA
383 0x1c, 0x00, // KeyLocator empty
384 0x17, 0x00 // SignatureValue empty
385 };
386 // TLV-LENGTH of <Data> is 0x20 which happens to be ASCII whitespace
387
388 std::stringstream stream;
389 stream.write(reinterpret_cast<const char*>(PACKET), sizeof(PACKET));
390 stream.seekg(0);
391
392 Block block = Block::fromStream(stream);
393 BOOST_CHECK_NO_THROW(block.parse());
394}
395
396BOOST_AUTO_TEST_CASE(FromStreamZeroLength) // Bug 2729
397{
398 uint8_t BUFFER[] = { 0x07, 0x00 }; // TLV-LENGTH is zero
399
400 std::stringstream stream;
401 stream.write(reinterpret_cast<const char*>(BUFFER), sizeof(BUFFER));
402 stream.seekg(0);
403
404 Block block;
405 BOOST_CHECK_NO_THROW(block = Block::fromStream(stream));
406 BOOST_CHECK_EQUAL(block.type(), 0x07);
407 BOOST_CHECK_EQUAL(block.value_size(), 0);
408}
409
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100410BOOST_AUTO_TEST_SUITE_END() // Construction
411
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700412BOOST_AUTO_TEST_CASE(Equality)
413{
414 BOOST_CONCEPT_ASSERT((boost::EqualityComparable<Block>));
415
416 Block a("\x08\x00", 2);
417 Block b("\x08\x00", 2);;
418 BOOST_CHECK_EQUAL(a == b, true);
419 BOOST_CHECK_EQUAL(a != b, false);
420
421 Block c("\x06\x00", 2);
422 Block d("\x08\x00", 2);;
423 BOOST_CHECK_EQUAL(c == d, false);
424 BOOST_CHECK_EQUAL(c != d, true);
425
426 Block e("\x06\x00", 2);
427 Block f("\x06\x01\xcc", 3);;
428 BOOST_CHECK_EQUAL(e == f, false);
429 BOOST_CHECK_EQUAL(e != f, true);
430}
431
Joao Pereira7476ebf2015-07-07 14:54:39 -0400432BOOST_AUTO_TEST_CASE(InsertBeginning)
433{
434 Block masterBlock(tlv::Name);
435 Block firstBlock = makeStringBlock(tlv::NameComponent, "firstName");
436 Block secondBlock = makeStringBlock(tlv::NameComponent, "secondName");
437 Block thirdBlock = makeStringBlock(tlv::NameComponent, "thirdName");
438
439 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 0);
440 masterBlock.push_back(secondBlock);
441 masterBlock.push_back(thirdBlock);
442 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 2);
443 Block::element_const_iterator it = masterBlock.find(tlv::NameComponent);
444 BOOST_CHECK_EQUAL(*it == secondBlock, true);
445
446 it = masterBlock.insert(it, firstBlock);
447
448 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 3);
449 BOOST_CHECK_EQUAL(*(it + 1) == secondBlock, true);
450 BOOST_CHECK_EQUAL(*(masterBlock.elements_begin()) == firstBlock, true);
451}
452
453BOOST_AUTO_TEST_CASE(InsertEnd)
454{
455 Block masterBlock(tlv::Name);
456 Block firstBlock = makeStringBlock(tlv::NameComponent, "firstName");
457 Block secondBlock = makeStringBlock(tlv::NameComponent, "secondName");
458 Block thirdBlock = makeStringBlock(tlv::NameComponent, "thirdName");
459
460 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 0);
461 masterBlock.push_back(firstBlock);
462 masterBlock.push_back(secondBlock);
463 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 2);
464 Block::element_const_iterator it = masterBlock.elements_end();
465 BOOST_CHECK_EQUAL(*(it - 1) == secondBlock, true);
466
467 it = masterBlock.insert(it, thirdBlock);
468
469 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 3);
470 BOOST_CHECK_EQUAL(*(it - 1) == secondBlock, true);
471 BOOST_CHECK_EQUAL(*(masterBlock.elements_end() - 1) == thirdBlock, true);
472}
473
474BOOST_AUTO_TEST_CASE(InsertMiddle)
475{
476 Block masterBlock(tlv::Name);
477 Block firstBlock = makeStringBlock(tlv::NameComponent, "firstName");
478 Block secondBlock = makeStringBlock(tlv::NameComponent, "secondName");
479 Block thirdBlock = makeStringBlock(tlv::NameComponent, "thirdName");
480
481 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 0);
482 masterBlock.push_back(firstBlock);
483 masterBlock.push_back(thirdBlock);
484 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 2);
485 Block::element_const_iterator it = masterBlock.find(tlv::NameComponent);
486 BOOST_CHECK_EQUAL(*it == firstBlock, true);
487
488 it = masterBlock.insert(it+1, secondBlock);
489
490 BOOST_CHECK_EQUAL(*it == secondBlock, true);
491 BOOST_CHECK_EQUAL(*(it + 1) == thirdBlock, true);
492 BOOST_CHECK_EQUAL(*(it - 1) == firstBlock, true);
493}
494
495BOOST_AUTO_TEST_CASE(EraseSingleElement)
496{
497 Block masterBlock(tlv::Name);
498 Block firstBlock = makeStringBlock(tlv::NameComponent, "firstName");
499 Block secondBlock = makeStringBlock(tlv::NameComponent, "secondName");
500 Block thirdBlock = makeStringBlock(tlv::NameComponent, "thirdName");
501
502 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 0);
503 masterBlock.push_back(firstBlock);
504 masterBlock.push_back(secondBlock);
505 masterBlock.push_back(thirdBlock);
506 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 3);
507 Block::element_const_iterator it = masterBlock.find(tlv::NameComponent);
508 it++;
509 BOOST_CHECK_EQUAL(*it == secondBlock, true);
510
511 it = masterBlock.erase(it);
512
513 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 2);
514 BOOST_CHECK_EQUAL(*(it) == thirdBlock, true);
515 BOOST_CHECK_EQUAL(*(it - 1) == firstBlock, true);
516}
517
518BOOST_AUTO_TEST_CASE(EraseRange)
519{
520 Block masterBlock(tlv::Name);
521 Block firstBlock = makeStringBlock(tlv::NameComponent, "firstName");
522 Block secondBlock = makeStringBlock(tlv::NameComponent, "secondName");
523 Block thirdBlock = makeStringBlock(tlv::NameComponent, "thirdName");
524 Block fourthBlock = makeStringBlock(tlv::NameComponent, "fourthName");
525 Block fifthBlock = makeStringBlock(tlv::NameComponent, "fifthName");
526 Block sixthBlock = makeStringBlock(tlv::NameComponent, "sixthName");
527
528 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 0);
529 masterBlock.push_back(firstBlock);
530 masterBlock.push_back(secondBlock);
531 masterBlock.push_back(thirdBlock);
532 masterBlock.push_back(fourthBlock);
533 masterBlock.push_back(fifthBlock);
534 masterBlock.push_back(sixthBlock);
535 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 6);
536 Block::element_const_iterator itStart = masterBlock.find(tlv::NameComponent);
537 itStart++;
538 Block::element_const_iterator itEnd = itStart + 3;
539 BOOST_CHECK_EQUAL(*itStart == secondBlock, true);
540 BOOST_CHECK_EQUAL(*itEnd == fifthBlock, true);
541
542 Block::element_const_iterator newIt = masterBlock.erase(itStart, itEnd);
543
544 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 3);
545 BOOST_CHECK_EQUAL(*(newIt) == fifthBlock, true);
546 BOOST_CHECK_EQUAL(*(newIt - 1) == firstBlock, true);
547}
548
Eric Newberrya3d9fc02015-07-26 10:55:44 -0700549BOOST_AUTO_TEST_CASE(Remove)
550{
551 Block block(tlv::Data);
552 block.push_back(makeNonNegativeIntegerBlock(tlv::ContentType, 0));
553 block.push_back(makeNonNegativeIntegerBlock(tlv::FreshnessPeriod, 123));
554 block.push_back(makeStringBlock(tlv::Name, "ndn:/test-prefix"));
555 block.push_back(makeNonNegativeIntegerBlock(tlv::ContentType, 2));
556 block.push_back(makeNonNegativeIntegerBlock(tlv::ContentType, 1));
557
558 BOOST_CHECK_EQUAL(5, block.elements_size());
559 BOOST_REQUIRE_NO_THROW(block.remove(tlv::ContentType));
560 BOOST_CHECK_EQUAL(2, block.elements_size());
561
562 Block::element_container elements = block.elements();
563
564 BOOST_CHECK_EQUAL(tlv::FreshnessPeriod, elements[0].type());
565 BOOST_CHECK_EQUAL(123, readNonNegativeInteger(elements[0]));
566 BOOST_CHECK_EQUAL(tlv::Name, elements[1].type());
567 BOOST_CHECK(readString(elements[1]).compare("ndn:/test-prefix") == 0);
568}
569
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100570BOOST_AUTO_TEST_SUITE_END() // TestBlock
571BOOST_AUTO_TEST_SUITE_END() // Encoding
Alexander Afanasyev233750e2014-02-16 00:50:07 -0800572
Alexander Afanasyev74633892015-02-08 18:08:46 -0800573} // namespace tests
Alexander Afanasyev233750e2014-02-16 00:50:07 -0800574} // namespace ndn