blob: 7fcf0fdeead28838c8e5ecfc56dab33a88e21f84 [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 Afanasyev74633892015-02-08 18:08:46 -08003 * Copyright (c) 2013-2015 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"
Joao Pereira7476ebf2015-07-07 14:54:39 -040024#include "encoding/block-helpers.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
Alexander Afanasyev233750e2014-02-16 00:50:07 -080028namespace ndn {
Alexander Afanasyev74633892015-02-08 18:08:46 -080029namespace tests {
Alexander Afanasyev233750e2014-02-16 00:50:07 -080030
Spyridon Mastorakis429634f2015-02-19 17:35:33 -080031BOOST_AUTO_TEST_SUITE(EncodingBlock)
Alexander Afanasyev233750e2014-02-16 00:50:07 -080032
Alexander Afanasyev74633892015-02-08 18:08:46 -080033class BasicBlockFixture
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -080034{
Alexander Afanasyev74633892015-02-08 18:08:46 -080035public:
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -080036 EncodingBuffer buffer;
37 EncodingEstimator estimator;
Alexander Afanasyev74633892015-02-08 18:08:46 -080038};
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -080039
Alexander Afanasyev74633892015-02-08 18:08:46 -080040BOOST_FIXTURE_TEST_SUITE(Basic, BasicBlockFixture)
Alexander Afanasyev937aa782014-03-21 13:17:57 -070041
Alexander Afanasyev74633892015-02-08 18:08:46 -080042BOOST_AUTO_TEST_CASE(VarNumberOneByte1)
43{
44 size_t s1 = buffer.prependVarNumber(252);
45 size_t s2 = estimator.prependVarNumber(252);
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -080046 BOOST_CHECK_EQUAL(buffer.size(), 1);
47 BOOST_CHECK_EQUAL(s1, 1);
48 BOOST_CHECK_EQUAL(s2, 1);
Alexander Afanasyev74633892015-02-08 18:08:46 -080049}
Alexander Afanasyev937aa782014-03-21 13:17:57 -070050
Alexander Afanasyev74633892015-02-08 18:08:46 -080051BOOST_AUTO_TEST_CASE(VarNumberOneByte2)
52{
53 size_t s1 = buffer.prependVarNumber(253);
54 size_t s2 = estimator.prependVarNumber(253);
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -080055 BOOST_CHECK_EQUAL(buffer.size(), 3);
56 BOOST_CHECK_EQUAL(s1, 3);
57 BOOST_CHECK_EQUAL(s2, 3);
Alexander Afanasyev74633892015-02-08 18:08:46 -080058}
Alexander Afanasyev937aa782014-03-21 13:17:57 -070059
Alexander Afanasyev74633892015-02-08 18:08:46 -080060BOOST_AUTO_TEST_CASE(VarNumberThreeBytes1)
61{
62 size_t s1 = buffer.prependVarNumber(255);
63 size_t s2 = estimator.prependVarNumber(255);
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -080064 BOOST_CHECK_EQUAL(buffer.size(), 3);
65 BOOST_CHECK_EQUAL(s1, 3);
66 BOOST_CHECK_EQUAL(s2, 3);
Alexander Afanasyev74633892015-02-08 18:08:46 -080067}
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -080068
Alexander Afanasyev74633892015-02-08 18:08:46 -080069BOOST_AUTO_TEST_CASE(VarNumberThreeBytes2)
70{
71 size_t s1 = buffer.prependVarNumber(65535);
72 size_t s2 = estimator.prependVarNumber(65535);
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -080073 BOOST_CHECK_EQUAL(buffer.size(), 3);
74 BOOST_CHECK_EQUAL(s1, 3);
75 BOOST_CHECK_EQUAL(s2, 3);
Alexander Afanasyev74633892015-02-08 18:08:46 -080076}
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -080077
Alexander Afanasyev74633892015-02-08 18:08:46 -080078BOOST_AUTO_TEST_CASE(VarNumberFiveBytes1)
79{
80 size_t s1 = buffer.prependVarNumber(65536);
81 size_t s2 = estimator.prependVarNumber(65536);
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -080082 BOOST_CHECK_EQUAL(buffer.size(), 5);
83 BOOST_CHECK_EQUAL(s1, 5);
84 BOOST_CHECK_EQUAL(s2, 5);
Alexander Afanasyev74633892015-02-08 18:08:46 -080085}
Alexander Afanasyev937aa782014-03-21 13:17:57 -070086
Alexander Afanasyev74633892015-02-08 18:08:46 -080087BOOST_AUTO_TEST_CASE(VarNumberFiveBytes2)
88{
89 size_t s1 = buffer.prependVarNumber(4294967295LL);
90 size_t s2 = estimator.prependVarNumber(4294967295LL);
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -080091 BOOST_CHECK_EQUAL(buffer.size(), 5);
92 BOOST_CHECK_EQUAL(s1, 5);
93 BOOST_CHECK_EQUAL(s2, 5);
Alexander Afanasyev74633892015-02-08 18:08:46 -080094}
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -080095
Alexander Afanasyev74633892015-02-08 18:08:46 -080096BOOST_AUTO_TEST_CASE(VarNumberNineBytes)
97{
98 size_t s1 = buffer.prependVarNumber(4294967296LL);
99 size_t s2 = estimator.prependVarNumber(4294967296LL);
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -0800100 BOOST_CHECK_EQUAL(buffer.size(), 9);
101 BOOST_CHECK_EQUAL(s1, 9);
102 BOOST_CHECK_EQUAL(s2, 9);
Alexander Afanasyev74633892015-02-08 18:08:46 -0800103}
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -0800104
Alexander Afanasyev74633892015-02-08 18:08:46 -0800105BOOST_AUTO_TEST_CASE(NonNegativeNumberOneByte1)
106{
107 size_t s1 = buffer.prependNonNegativeInteger(252);
108 size_t s2 = estimator.prependNonNegativeInteger(252);
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -0800109 BOOST_CHECK_EQUAL(buffer.size(), 1);
110 BOOST_CHECK_EQUAL(s1, 1);
111 BOOST_CHECK_EQUAL(s2, 1);
Alexander Afanasyev74633892015-02-08 18:08:46 -0800112}
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700113
Alexander Afanasyev74633892015-02-08 18:08:46 -0800114BOOST_AUTO_TEST_CASE(NonNegativeNumberOneByte2)
115{
116 size_t s1 = buffer.prependNonNegativeInteger(255);
117 size_t s2 = estimator.prependNonNegativeInteger(255);
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -0800118 BOOST_CHECK_EQUAL(buffer.size(), 1);
119 BOOST_CHECK_EQUAL(s1, 1);
120 BOOST_CHECK_EQUAL(s2, 1);
Alexander Afanasyev74633892015-02-08 18:08:46 -0800121}
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700122
Alexander Afanasyev74633892015-02-08 18:08:46 -0800123BOOST_AUTO_TEST_CASE(NonNegativeNumberTwoBytes1)
124{
125 size_t s1 = buffer.prependNonNegativeInteger(256);
126 size_t s2 = estimator.prependNonNegativeInteger(256);
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -0800127 BOOST_CHECK_EQUAL(buffer.size(), 2);
128 BOOST_CHECK_EQUAL(s1, 2);
129 BOOST_CHECK_EQUAL(s2, 2);
Alexander Afanasyev74633892015-02-08 18:08:46 -0800130}
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -0800131
Alexander Afanasyev74633892015-02-08 18:08:46 -0800132BOOST_AUTO_TEST_CASE(NonNegativeNumberTwoBytes2)
133{
134 size_t s1 = buffer.prependNonNegativeInteger(65535);
135 size_t s2 = estimator.prependNonNegativeInteger(65535);
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -0800136 BOOST_CHECK_EQUAL(buffer.size(), 2);
137 BOOST_CHECK_EQUAL(s1, 2);
138 BOOST_CHECK_EQUAL(s2, 2);
Alexander Afanasyev74633892015-02-08 18:08:46 -0800139}
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -0800140
Alexander Afanasyev74633892015-02-08 18:08:46 -0800141BOOST_AUTO_TEST_CASE(NonNegativeNumberFourBytes1)
142{
143 size_t s1 = buffer.prependNonNegativeInteger(65536);
144 size_t s2 = estimator.prependNonNegativeInteger(65536);
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -0800145 BOOST_CHECK_EQUAL(buffer.size(), 4);
146 BOOST_CHECK_EQUAL(s1, 4);
147 BOOST_CHECK_EQUAL(s2, 4);
Alexander Afanasyev74633892015-02-08 18:08:46 -0800148}
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -0800149
Alexander Afanasyev74633892015-02-08 18:08:46 -0800150BOOST_AUTO_TEST_CASE(NonNegativeNumberFourBytes2)
151{
152 size_t s1 = buffer.prependNonNegativeInteger(4294967295LL);
153 size_t s2 = estimator.prependNonNegativeInteger(4294967295LL);
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -0800154 BOOST_CHECK_EQUAL(buffer.size(), 4);
155 BOOST_CHECK_EQUAL(s1, 4);
156 BOOST_CHECK_EQUAL(s2, 4);
Alexander Afanasyev74633892015-02-08 18:08:46 -0800157}
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -0800158
Alexander Afanasyev74633892015-02-08 18:08:46 -0800159BOOST_AUTO_TEST_CASE(NonNegativeNumberEightBytes)
160{
161 size_t s1 = buffer.prependNonNegativeInteger(4294967296LL);
162 size_t s2 = estimator.prependNonNegativeInteger(4294967296LL);
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -0800163 BOOST_CHECK_EQUAL(buffer.size(), 8);
164 BOOST_CHECK_EQUAL(s1, 8);
165 BOOST_CHECK_EQUAL(s2, 8);
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -0800166}
167
Alexander Afanasyev74633892015-02-08 18:08:46 -0800168BOOST_AUTO_TEST_SUITE_END() // Basic
169
Alexander Afanasyev4448d292015-08-09 20:11:37 -0700170BOOST_AUTO_TEST_CASE(BlockFromBlock)
171{
172 static uint8_t buffer[] = {0x80, 0x06, 0x81, 0x01, 0x01, 0x82, 0x01, 0x01};
173 Block block(buffer, sizeof(buffer));
174
175 Block derivedBlock(block, block.begin(), block.end());
176 BOOST_CHECK_EQUAL(derivedBlock.wire(), block.wire()); // pointers should match
177 BOOST_CHECK(derivedBlock == block); // blocks should match
178
179 derivedBlock = Block(block, block.begin() + 2, block.begin() + 5);
180 BOOST_CHECK(derivedBlock.begin() == block.begin() + 2);
181 BOOST_CHECK(derivedBlock == Block(buffer + 2, 3));
182
183 Buffer otherBuffer(buffer, sizeof(buffer));
184 BOOST_CHECK_THROW(Block(block, otherBuffer.begin(), block.end()), Block::Error);
185 BOOST_CHECK_THROW(Block(block, block.begin(), otherBuffer.end()), Block::Error);
186 BOOST_CHECK_THROW(Block(block, otherBuffer.begin(), otherBuffer.end()), Block::Error);
187}
188
Eric Newberry6235aac2015-08-13 16:03:47 -0700189BOOST_AUTO_TEST_CASE(BlockFromBlockCopyOnWriteModifyOrig)
190{
191 static uint8_t buffer[] = {
192 0x05, 0x0b, 0x07, 0x03, 0x01, 0x02, 0x03, 0x0a, 0x04, 0x04, 0x05, 0x06, 0x07,
193 };
194
195 Block block1(buffer, sizeof(buffer));
196
197 Block block2(block1, block1.begin(), block1.end());
198 auto buf2 = block2.getBuffer();
199
200 block1.parse();
201 block1.remove(tlv::Name);
202 block1.encode();
203
204 block2.parse();
205
206 BOOST_CHECK_EQUAL_COLLECTIONS(block2.begin(), block2.end(), buffer, buffer + sizeof(buffer));
207 BOOST_CHECK_EQUAL(buf2, block2.getBuffer());
208}
209
210BOOST_AUTO_TEST_CASE(BlockFromBlockCopyOnWriteModifyCopy)
211{
212 static uint8_t buffer[] = {
213 0x05, 0x0b, 0x07, 0x03, 0x01, 0x02, 0x03, 0x0a, 0x04, 0x04, 0x05, 0x06, 0x07,
214 };
215
216 Block block1(buffer, sizeof(buffer));
217 auto buf1 = block1.getBuffer();
218
219 Block block2(block1, block1.begin(), block1.end());
220
221 block2.parse();
222 block2.remove(tlv::Name);
223 block2.encode();
224
225 block1.parse();
226
227 BOOST_CHECK_EQUAL_COLLECTIONS(block1.begin(), block1.end(), buffer, buffer + sizeof(buffer));
228 BOOST_CHECK_EQUAL(buf1, block1.getBuffer());
229}
230
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700231BOOST_AUTO_TEST_CASE(EncodingBufferToBlock)
Alexander Afanasyev233750e2014-02-16 00:50:07 -0800232{
233 uint8_t value[4];
234
Alexander Afanasyev15151312014-02-16 00:53:51 -0800235 EncodingBuffer buffer;
Alexander Afanasyev233750e2014-02-16 00:50:07 -0800236 size_t length = buffer.prependByteArray(value, sizeof(value));
237 buffer.prependVarNumber(length);
238 buffer.prependVarNumber(0xe0);
239
240 Block block;
241 BOOST_REQUIRE_NO_THROW(block = buffer.block());
242 BOOST_CHECK_EQUAL(block.type(), 0xe0);
243 BOOST_CHECK_EQUAL(block.value_size(), sizeof(value));
Alexander Afanasyev15151312014-02-16 00:53:51 -0800244
245 BOOST_REQUIRE_NO_THROW(block = Block(buffer));
246 BOOST_CHECK_EQUAL(block.type(), 0xe0);
247 BOOST_CHECK_EQUAL(block.value_size(), sizeof(value));
248}
249
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700250BOOST_AUTO_TEST_CASE(BlockToBuffer)
Alexander Afanasyev15151312014-02-16 00:53:51 -0800251{
252 shared_ptr<Buffer> buf = make_shared<Buffer>(10);
253 for (int i = 0; i < 10; i++) (*buf)[i] = i;
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700254
Alexander Afanasyev15151312014-02-16 00:53:51 -0800255 Block block(0xab, buf);
256 block.encode();
257
Alexander Afanasyev74633892015-02-08 18:08:46 -0800258 {
259 BOOST_REQUIRE_NO_THROW(EncodingBuffer(block));
260 EncodingBuffer buffer(block);
261 BOOST_CHECK_EQUAL(buffer.size(), 12);
262 BOOST_CHECK_EQUAL(buffer.capacity(), 12);
263 }
Alexander Afanasyev15151312014-02-16 00:53:51 -0800264
265 (*buf)[1] = 0xe0;
266 (*buf)[2] = 2;
267 BOOST_REQUIRE_NO_THROW(block = Block(buf, buf->begin() + 1, buf->begin() + 5));
268 BOOST_CHECK_EQUAL(block.type(), 0xe0);
269
Alexander Afanasyev74633892015-02-08 18:08:46 -0800270 {
271 BOOST_REQUIRE_NO_THROW(EncodingBuffer(block));
272 EncodingBuffer buffer(block);
273 BOOST_CHECK_EQUAL(buffer.size(), 4);
274 BOOST_CHECK_EQUAL(buffer.capacity(), 10);
275 }
Alexander Afanasyev233750e2014-02-16 00:50:07 -0800276}
277
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700278BOOST_AUTO_TEST_CASE(FromBuffer)
279{
280 const uint8_t TEST_BUFFER[] = {0x00, 0x01, 0xfa, // ok
281 0x01, 0x01, 0xfb, // ok
282 0x03, 0x02, 0xff}; // not ok
283 BufferPtr buffer(new Buffer(TEST_BUFFER, sizeof(TEST_BUFFER)));
284
285 // using BufferPtr (avoids memory copy)
286 size_t offset = 0;
Junxiao Shi02a4bf32015-02-21 21:07:46 -0700287 bool isOk = false;
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700288 Block testBlock;
Junxiao Shi02a4bf32015-02-21 21:07:46 -0700289 std::tie(isOk, testBlock) = Block::fromBuffer(buffer, offset);
290 BOOST_CHECK(isOk);
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700291 BOOST_CHECK_EQUAL(testBlock.type(), 0);
292 BOOST_CHECK_EQUAL(testBlock.size(), 3);
293 BOOST_CHECK_EQUAL(testBlock.value_size(), 1);
294 BOOST_CHECK_EQUAL(*testBlock.wire(), 0x00);
295 BOOST_CHECK_EQUAL(*testBlock.value(), 0xfa);
296 offset += testBlock.size();
297
Junxiao Shi02a4bf32015-02-21 21:07:46 -0700298 std::tie(isOk, testBlock) = Block::fromBuffer(buffer, offset);
299 BOOST_CHECK(isOk);
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700300 BOOST_CHECK_EQUAL(testBlock.type(), 1);
301 BOOST_CHECK_EQUAL(testBlock.size(), 3);
302 BOOST_CHECK_EQUAL(testBlock.value_size(), 1);
303 BOOST_CHECK_EQUAL(*testBlock.wire(), 0x01);
304 BOOST_CHECK_EQUAL(*testBlock.value(), 0xfb);
305 offset += testBlock.size();
306
Junxiao Shi02a4bf32015-02-21 21:07:46 -0700307 std::tie(isOk, testBlock) = Block::fromBuffer(buffer, offset);
308 BOOST_CHECK(!isOk);
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700309
310 // just buffer, copies memory
311 offset = 0;
Junxiao Shi02a4bf32015-02-21 21:07:46 -0700312 std::tie(isOk, testBlock) = Block::fromBuffer(TEST_BUFFER + offset,
313 sizeof(TEST_BUFFER) - offset);
314 BOOST_CHECK(isOk);
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700315 BOOST_CHECK_EQUAL(testBlock.type(), 0);
316 BOOST_CHECK_EQUAL(testBlock.size(), 3);
317 BOOST_CHECK_EQUAL(testBlock.value_size(), 1);
318 BOOST_CHECK_EQUAL(*testBlock.wire(), 0x00);
319 BOOST_CHECK_EQUAL(*testBlock.value(), 0xfa);
320 offset += testBlock.size();
321
Junxiao Shi02a4bf32015-02-21 21:07:46 -0700322 std::tie(isOk, testBlock) = Block::fromBuffer(TEST_BUFFER + offset,
323 sizeof(TEST_BUFFER) - offset);
324 BOOST_CHECK(isOk);
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700325 BOOST_CHECK_EQUAL(testBlock.type(), 1);
326 BOOST_CHECK_EQUAL(testBlock.size(), 3);
327 BOOST_CHECK_EQUAL(testBlock.value_size(), 1);
328 BOOST_CHECK_EQUAL(*testBlock.wire(), 0x01);
329 BOOST_CHECK_EQUAL(*testBlock.value(), 0xfb);
330 offset += testBlock.size();
331
Junxiao Shi02a4bf32015-02-21 21:07:46 -0700332 std::tie(isOk, testBlock) = Block::fromBuffer(TEST_BUFFER + offset,
333 sizeof(TEST_BUFFER) - offset);
334 BOOST_CHECK(!isOk);
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700335}
336
Junxiao Shif0da7892015-04-04 22:16:16 -0700337BOOST_AUTO_TEST_CASE(FromStream)
Alexander Afanasyev21ef2392014-03-25 12:40:22 -0700338{
339 const uint8_t TEST_BUFFER[] = {0x00, 0x01, 0xfa, // ok
340 0x01, 0x01, 0xfb, // ok
341 0x03, 0x02, 0xff}; // not ok
342
343 typedef boost::iostreams::stream<boost::iostreams::array_source> ArrayStream;
344 ArrayStream stream(reinterpret_cast<const char*>(TEST_BUFFER), sizeof(TEST_BUFFER));
345
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700346
Alexander Afanasyev21ef2392014-03-25 12:40:22 -0700347 Block testBlock;
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700348 BOOST_REQUIRE_NO_THROW(testBlock = Block::fromStream(stream));
Alexander Afanasyev21ef2392014-03-25 12:40:22 -0700349 BOOST_CHECK_EQUAL(testBlock.type(), 0);
350 BOOST_CHECK_EQUAL(testBlock.size(), 3);
351 BOOST_CHECK_EQUAL(testBlock.value_size(), 1);
352 BOOST_CHECK_EQUAL(*testBlock.wire(), 0x00);
353 BOOST_CHECK_EQUAL(*testBlock.value(), 0xfa);
354
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700355 BOOST_REQUIRE_NO_THROW(testBlock = Block::fromStream(stream));
Alexander Afanasyev21ef2392014-03-25 12:40:22 -0700356 BOOST_CHECK_EQUAL(testBlock.type(), 1);
357 BOOST_CHECK_EQUAL(testBlock.size(), 3);
358 BOOST_CHECK_EQUAL(testBlock.value_size(), 1);
359 BOOST_CHECK_EQUAL(*testBlock.wire(), 0x01);
360 BOOST_CHECK_EQUAL(*testBlock.value(), 0xfb);
361
Steve DiBenedetto54ce6682014-07-22 13:22:57 -0600362 BOOST_CHECK_THROW(Block::fromStream(stream), tlv::Error);
Alexander Afanasyev21ef2392014-03-25 12:40:22 -0700363}
364
Junxiao Shif0da7892015-04-04 22:16:16 -0700365BOOST_AUTO_TEST_CASE(FromStreamWhitespace) // Bug 2728
366{
367 uint8_t PACKET[] = {
368 0x06, 0x20, // Data
369 0x07, 0x11, // Name
370 0x08, 0x05, 0x68, 0x65, 0x6c, 0x6c, 0x6f, // NameComponent 'hello'
371 0x08, 0x01, 0x31, // NameComponent '1'
372 0x08, 0x05, 0x77, 0x6f, 0x72, 0x6c, 0x64, // NameComponent 'world'
373 0x14, 0x00, // MetaInfo empty
374 0x15, 0x00, // Content empty
375 0x16, 0x05, // SignatureInfo
376 0x1b, 0x01, 0x01, // SignatureType RSA
377 0x1c, 0x00, // KeyLocator empty
378 0x17, 0x00 // SignatureValue empty
379 };
380 // TLV-LENGTH of <Data> is 0x20 which happens to be ASCII whitespace
381
382 std::stringstream stream;
383 stream.write(reinterpret_cast<const char*>(PACKET), sizeof(PACKET));
384 stream.seekg(0);
385
386 Block block = Block::fromStream(stream);
387 BOOST_CHECK_NO_THROW(block.parse());
388}
389
390BOOST_AUTO_TEST_CASE(FromStreamZeroLength) // Bug 2729
391{
392 uint8_t BUFFER[] = { 0x07, 0x00 }; // TLV-LENGTH is zero
393
394 std::stringstream stream;
395 stream.write(reinterpret_cast<const char*>(BUFFER), sizeof(BUFFER));
396 stream.seekg(0);
397
398 Block block;
399 BOOST_CHECK_NO_THROW(block = Block::fromStream(stream));
400 BOOST_CHECK_EQUAL(block.type(), 0x07);
401 BOOST_CHECK_EQUAL(block.value_size(), 0);
402}
403
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700404BOOST_AUTO_TEST_CASE(Equality)
405{
406 BOOST_CONCEPT_ASSERT((boost::EqualityComparable<Block>));
407
408 Block a("\x08\x00", 2);
409 Block b("\x08\x00", 2);;
410 BOOST_CHECK_EQUAL(a == b, true);
411 BOOST_CHECK_EQUAL(a != b, false);
412
413 Block c("\x06\x00", 2);
414 Block d("\x08\x00", 2);;
415 BOOST_CHECK_EQUAL(c == d, false);
416 BOOST_CHECK_EQUAL(c != d, true);
417
418 Block e("\x06\x00", 2);
419 Block f("\x06\x01\xcc", 3);;
420 BOOST_CHECK_EQUAL(e == f, false);
421 BOOST_CHECK_EQUAL(e != f, true);
422}
423
Joao Pereira7476ebf2015-07-07 14:54:39 -0400424BOOST_AUTO_TEST_CASE(InsertBeginning)
425{
426 Block masterBlock(tlv::Name);
427 Block firstBlock = makeStringBlock(tlv::NameComponent, "firstName");
428 Block secondBlock = makeStringBlock(tlv::NameComponent, "secondName");
429 Block thirdBlock = makeStringBlock(tlv::NameComponent, "thirdName");
430
431 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 0);
432 masterBlock.push_back(secondBlock);
433 masterBlock.push_back(thirdBlock);
434 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 2);
435 Block::element_const_iterator it = masterBlock.find(tlv::NameComponent);
436 BOOST_CHECK_EQUAL(*it == secondBlock, true);
437
438 it = masterBlock.insert(it, firstBlock);
439
440 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 3);
441 BOOST_CHECK_EQUAL(*(it + 1) == secondBlock, true);
442 BOOST_CHECK_EQUAL(*(masterBlock.elements_begin()) == firstBlock, true);
443}
444
445BOOST_AUTO_TEST_CASE(InsertEnd)
446{
447 Block masterBlock(tlv::Name);
448 Block firstBlock = makeStringBlock(tlv::NameComponent, "firstName");
449 Block secondBlock = makeStringBlock(tlv::NameComponent, "secondName");
450 Block thirdBlock = makeStringBlock(tlv::NameComponent, "thirdName");
451
452 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 0);
453 masterBlock.push_back(firstBlock);
454 masterBlock.push_back(secondBlock);
455 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 2);
456 Block::element_const_iterator it = masterBlock.elements_end();
457 BOOST_CHECK_EQUAL(*(it - 1) == secondBlock, true);
458
459 it = masterBlock.insert(it, thirdBlock);
460
461 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 3);
462 BOOST_CHECK_EQUAL(*(it - 1) == secondBlock, true);
463 BOOST_CHECK_EQUAL(*(masterBlock.elements_end() - 1) == thirdBlock, true);
464}
465
466BOOST_AUTO_TEST_CASE(InsertMiddle)
467{
468 Block masterBlock(tlv::Name);
469 Block firstBlock = makeStringBlock(tlv::NameComponent, "firstName");
470 Block secondBlock = makeStringBlock(tlv::NameComponent, "secondName");
471 Block thirdBlock = makeStringBlock(tlv::NameComponent, "thirdName");
472
473 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 0);
474 masterBlock.push_back(firstBlock);
475 masterBlock.push_back(thirdBlock);
476 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 2);
477 Block::element_const_iterator it = masterBlock.find(tlv::NameComponent);
478 BOOST_CHECK_EQUAL(*it == firstBlock, true);
479
480 it = masterBlock.insert(it+1, secondBlock);
481
482 BOOST_CHECK_EQUAL(*it == secondBlock, true);
483 BOOST_CHECK_EQUAL(*(it + 1) == thirdBlock, true);
484 BOOST_CHECK_EQUAL(*(it - 1) == firstBlock, true);
485}
486
487BOOST_AUTO_TEST_CASE(EraseSingleElement)
488{
489 Block masterBlock(tlv::Name);
490 Block firstBlock = makeStringBlock(tlv::NameComponent, "firstName");
491 Block secondBlock = makeStringBlock(tlv::NameComponent, "secondName");
492 Block thirdBlock = makeStringBlock(tlv::NameComponent, "thirdName");
493
494 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 0);
495 masterBlock.push_back(firstBlock);
496 masterBlock.push_back(secondBlock);
497 masterBlock.push_back(thirdBlock);
498 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 3);
499 Block::element_const_iterator it = masterBlock.find(tlv::NameComponent);
500 it++;
501 BOOST_CHECK_EQUAL(*it == secondBlock, true);
502
503 it = masterBlock.erase(it);
504
505 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 2);
506 BOOST_CHECK_EQUAL(*(it) == thirdBlock, true);
507 BOOST_CHECK_EQUAL(*(it - 1) == firstBlock, true);
508}
509
510BOOST_AUTO_TEST_CASE(EraseRange)
511{
512 Block masterBlock(tlv::Name);
513 Block firstBlock = makeStringBlock(tlv::NameComponent, "firstName");
514 Block secondBlock = makeStringBlock(tlv::NameComponent, "secondName");
515 Block thirdBlock = makeStringBlock(tlv::NameComponent, "thirdName");
516 Block fourthBlock = makeStringBlock(tlv::NameComponent, "fourthName");
517 Block fifthBlock = makeStringBlock(tlv::NameComponent, "fifthName");
518 Block sixthBlock = makeStringBlock(tlv::NameComponent, "sixthName");
519
520 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 0);
521 masterBlock.push_back(firstBlock);
522 masterBlock.push_back(secondBlock);
523 masterBlock.push_back(thirdBlock);
524 masterBlock.push_back(fourthBlock);
525 masterBlock.push_back(fifthBlock);
526 masterBlock.push_back(sixthBlock);
527 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 6);
528 Block::element_const_iterator itStart = masterBlock.find(tlv::NameComponent);
529 itStart++;
530 Block::element_const_iterator itEnd = itStart + 3;
531 BOOST_CHECK_EQUAL(*itStart == secondBlock, true);
532 BOOST_CHECK_EQUAL(*itEnd == fifthBlock, true);
533
534 Block::element_const_iterator newIt = masterBlock.erase(itStart, itEnd);
535
536 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 3);
537 BOOST_CHECK_EQUAL(*(newIt) == fifthBlock, true);
538 BOOST_CHECK_EQUAL(*(newIt - 1) == firstBlock, true);
539}
540
Eric Newberrya3d9fc02015-07-26 10:55:44 -0700541BOOST_AUTO_TEST_CASE(Remove)
542{
543 Block block(tlv::Data);
544 block.push_back(makeNonNegativeIntegerBlock(tlv::ContentType, 0));
545 block.push_back(makeNonNegativeIntegerBlock(tlv::FreshnessPeriod, 123));
546 block.push_back(makeStringBlock(tlv::Name, "ndn:/test-prefix"));
547 block.push_back(makeNonNegativeIntegerBlock(tlv::ContentType, 2));
548 block.push_back(makeNonNegativeIntegerBlock(tlv::ContentType, 1));
549
550 BOOST_CHECK_EQUAL(5, block.elements_size());
551 BOOST_REQUIRE_NO_THROW(block.remove(tlv::ContentType));
552 BOOST_CHECK_EQUAL(2, block.elements_size());
553
554 Block::element_container elements = block.elements();
555
556 BOOST_CHECK_EQUAL(tlv::FreshnessPeriod, elements[0].type());
557 BOOST_CHECK_EQUAL(123, readNonNegativeInteger(elements[0]));
558 BOOST_CHECK_EQUAL(tlv::Name, elements[1].type());
559 BOOST_CHECK(readString(elements[1]).compare("ndn:/test-prefix") == 0);
560}
561
Alexander Afanasyev233750e2014-02-16 00:50:07 -0800562BOOST_AUTO_TEST_SUITE_END()
563
Alexander Afanasyev74633892015-02-08 18:08:46 -0800564} // namespace tests
Alexander Afanasyev233750e2014-02-16 00:50:07 -0800565} // namespace ndn