blob: cc90ede149e489b268d747a81c78cc9f481efbdc [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 Afanasyev937aa782014-03-21 13:17:57 -0700170BOOST_AUTO_TEST_CASE(EncodingBufferToBlock)
Alexander Afanasyev233750e2014-02-16 00:50:07 -0800171{
172 uint8_t value[4];
173
Alexander Afanasyev15151312014-02-16 00:53:51 -0800174 EncodingBuffer buffer;
Alexander Afanasyev233750e2014-02-16 00:50:07 -0800175 size_t length = buffer.prependByteArray(value, sizeof(value));
176 buffer.prependVarNumber(length);
177 buffer.prependVarNumber(0xe0);
178
179 Block block;
180 BOOST_REQUIRE_NO_THROW(block = buffer.block());
181 BOOST_CHECK_EQUAL(block.type(), 0xe0);
182 BOOST_CHECK_EQUAL(block.value_size(), sizeof(value));
Alexander Afanasyev15151312014-02-16 00:53:51 -0800183
184 BOOST_REQUIRE_NO_THROW(block = Block(buffer));
185 BOOST_CHECK_EQUAL(block.type(), 0xe0);
186 BOOST_CHECK_EQUAL(block.value_size(), sizeof(value));
187}
188
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700189BOOST_AUTO_TEST_CASE(BlockToBuffer)
Alexander Afanasyev15151312014-02-16 00:53:51 -0800190{
191 shared_ptr<Buffer> buf = make_shared<Buffer>(10);
192 for (int i = 0; i < 10; i++) (*buf)[i] = i;
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700193
Alexander Afanasyev15151312014-02-16 00:53:51 -0800194 Block block(0xab, buf);
195 block.encode();
196
Alexander Afanasyev74633892015-02-08 18:08:46 -0800197 {
198 BOOST_REQUIRE_NO_THROW(EncodingBuffer(block));
199 EncodingBuffer buffer(block);
200 BOOST_CHECK_EQUAL(buffer.size(), 12);
201 BOOST_CHECK_EQUAL(buffer.capacity(), 12);
202 }
Alexander Afanasyev15151312014-02-16 00:53:51 -0800203
204 (*buf)[1] = 0xe0;
205 (*buf)[2] = 2;
206 BOOST_REQUIRE_NO_THROW(block = Block(buf, buf->begin() + 1, buf->begin() + 5));
207 BOOST_CHECK_EQUAL(block.type(), 0xe0);
208
Alexander Afanasyev74633892015-02-08 18:08:46 -0800209 {
210 BOOST_REQUIRE_NO_THROW(EncodingBuffer(block));
211 EncodingBuffer buffer(block);
212 BOOST_CHECK_EQUAL(buffer.size(), 4);
213 BOOST_CHECK_EQUAL(buffer.capacity(), 10);
214 }
Alexander Afanasyev233750e2014-02-16 00:50:07 -0800215}
216
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700217BOOST_AUTO_TEST_CASE(FromBuffer)
218{
219 const uint8_t TEST_BUFFER[] = {0x00, 0x01, 0xfa, // ok
220 0x01, 0x01, 0xfb, // ok
221 0x03, 0x02, 0xff}; // not ok
222 BufferPtr buffer(new Buffer(TEST_BUFFER, sizeof(TEST_BUFFER)));
223
224 // using BufferPtr (avoids memory copy)
225 size_t offset = 0;
Junxiao Shi02a4bf32015-02-21 21:07:46 -0700226 bool isOk = false;
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700227 Block testBlock;
Junxiao Shi02a4bf32015-02-21 21:07:46 -0700228 std::tie(isOk, testBlock) = Block::fromBuffer(buffer, offset);
229 BOOST_CHECK(isOk);
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700230 BOOST_CHECK_EQUAL(testBlock.type(), 0);
231 BOOST_CHECK_EQUAL(testBlock.size(), 3);
232 BOOST_CHECK_EQUAL(testBlock.value_size(), 1);
233 BOOST_CHECK_EQUAL(*testBlock.wire(), 0x00);
234 BOOST_CHECK_EQUAL(*testBlock.value(), 0xfa);
235 offset += testBlock.size();
236
Junxiao Shi02a4bf32015-02-21 21:07:46 -0700237 std::tie(isOk, testBlock) = Block::fromBuffer(buffer, offset);
238 BOOST_CHECK(isOk);
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700239 BOOST_CHECK_EQUAL(testBlock.type(), 1);
240 BOOST_CHECK_EQUAL(testBlock.size(), 3);
241 BOOST_CHECK_EQUAL(testBlock.value_size(), 1);
242 BOOST_CHECK_EQUAL(*testBlock.wire(), 0x01);
243 BOOST_CHECK_EQUAL(*testBlock.value(), 0xfb);
244 offset += testBlock.size();
245
Junxiao Shi02a4bf32015-02-21 21:07:46 -0700246 std::tie(isOk, testBlock) = Block::fromBuffer(buffer, offset);
247 BOOST_CHECK(!isOk);
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700248
249 // just buffer, copies memory
250 offset = 0;
Junxiao Shi02a4bf32015-02-21 21:07:46 -0700251 std::tie(isOk, testBlock) = Block::fromBuffer(TEST_BUFFER + offset,
252 sizeof(TEST_BUFFER) - offset);
253 BOOST_CHECK(isOk);
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700254 BOOST_CHECK_EQUAL(testBlock.type(), 0);
255 BOOST_CHECK_EQUAL(testBlock.size(), 3);
256 BOOST_CHECK_EQUAL(testBlock.value_size(), 1);
257 BOOST_CHECK_EQUAL(*testBlock.wire(), 0x00);
258 BOOST_CHECK_EQUAL(*testBlock.value(), 0xfa);
259 offset += testBlock.size();
260
Junxiao Shi02a4bf32015-02-21 21:07:46 -0700261 std::tie(isOk, testBlock) = Block::fromBuffer(TEST_BUFFER + offset,
262 sizeof(TEST_BUFFER) - offset);
263 BOOST_CHECK(isOk);
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700264 BOOST_CHECK_EQUAL(testBlock.type(), 1);
265 BOOST_CHECK_EQUAL(testBlock.size(), 3);
266 BOOST_CHECK_EQUAL(testBlock.value_size(), 1);
267 BOOST_CHECK_EQUAL(*testBlock.wire(), 0x01);
268 BOOST_CHECK_EQUAL(*testBlock.value(), 0xfb);
269 offset += testBlock.size();
270
Junxiao Shi02a4bf32015-02-21 21:07:46 -0700271 std::tie(isOk, testBlock) = Block::fromBuffer(TEST_BUFFER + offset,
272 sizeof(TEST_BUFFER) - offset);
273 BOOST_CHECK(!isOk);
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700274}
275
Junxiao Shif0da7892015-04-04 22:16:16 -0700276BOOST_AUTO_TEST_CASE(FromStream)
Alexander Afanasyev21ef2392014-03-25 12:40:22 -0700277{
278 const uint8_t TEST_BUFFER[] = {0x00, 0x01, 0xfa, // ok
279 0x01, 0x01, 0xfb, // ok
280 0x03, 0x02, 0xff}; // not ok
281
282 typedef boost::iostreams::stream<boost::iostreams::array_source> ArrayStream;
283 ArrayStream stream(reinterpret_cast<const char*>(TEST_BUFFER), sizeof(TEST_BUFFER));
284
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700285
Alexander Afanasyev21ef2392014-03-25 12:40:22 -0700286 Block testBlock;
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700287 BOOST_REQUIRE_NO_THROW(testBlock = Block::fromStream(stream));
Alexander Afanasyev21ef2392014-03-25 12:40:22 -0700288 BOOST_CHECK_EQUAL(testBlock.type(), 0);
289 BOOST_CHECK_EQUAL(testBlock.size(), 3);
290 BOOST_CHECK_EQUAL(testBlock.value_size(), 1);
291 BOOST_CHECK_EQUAL(*testBlock.wire(), 0x00);
292 BOOST_CHECK_EQUAL(*testBlock.value(), 0xfa);
293
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700294 BOOST_REQUIRE_NO_THROW(testBlock = Block::fromStream(stream));
Alexander Afanasyev21ef2392014-03-25 12:40:22 -0700295 BOOST_CHECK_EQUAL(testBlock.type(), 1);
296 BOOST_CHECK_EQUAL(testBlock.size(), 3);
297 BOOST_CHECK_EQUAL(testBlock.value_size(), 1);
298 BOOST_CHECK_EQUAL(*testBlock.wire(), 0x01);
299 BOOST_CHECK_EQUAL(*testBlock.value(), 0xfb);
300
Steve DiBenedetto54ce6682014-07-22 13:22:57 -0600301 BOOST_CHECK_THROW(Block::fromStream(stream), tlv::Error);
Alexander Afanasyev21ef2392014-03-25 12:40:22 -0700302}
303
Junxiao Shif0da7892015-04-04 22:16:16 -0700304BOOST_AUTO_TEST_CASE(FromStreamWhitespace) // Bug 2728
305{
306 uint8_t PACKET[] = {
307 0x06, 0x20, // Data
308 0x07, 0x11, // Name
309 0x08, 0x05, 0x68, 0x65, 0x6c, 0x6c, 0x6f, // NameComponent 'hello'
310 0x08, 0x01, 0x31, // NameComponent '1'
311 0x08, 0x05, 0x77, 0x6f, 0x72, 0x6c, 0x64, // NameComponent 'world'
312 0x14, 0x00, // MetaInfo empty
313 0x15, 0x00, // Content empty
314 0x16, 0x05, // SignatureInfo
315 0x1b, 0x01, 0x01, // SignatureType RSA
316 0x1c, 0x00, // KeyLocator empty
317 0x17, 0x00 // SignatureValue empty
318 };
319 // TLV-LENGTH of <Data> is 0x20 which happens to be ASCII whitespace
320
321 std::stringstream stream;
322 stream.write(reinterpret_cast<const char*>(PACKET), sizeof(PACKET));
323 stream.seekg(0);
324
325 Block block = Block::fromStream(stream);
326 BOOST_CHECK_NO_THROW(block.parse());
327}
328
329BOOST_AUTO_TEST_CASE(FromStreamZeroLength) // Bug 2729
330{
331 uint8_t BUFFER[] = { 0x07, 0x00 }; // TLV-LENGTH is zero
332
333 std::stringstream stream;
334 stream.write(reinterpret_cast<const char*>(BUFFER), sizeof(BUFFER));
335 stream.seekg(0);
336
337 Block block;
338 BOOST_CHECK_NO_THROW(block = Block::fromStream(stream));
339 BOOST_CHECK_EQUAL(block.type(), 0x07);
340 BOOST_CHECK_EQUAL(block.value_size(), 0);
341}
342
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700343BOOST_AUTO_TEST_CASE(Equality)
344{
345 BOOST_CONCEPT_ASSERT((boost::EqualityComparable<Block>));
346
347 Block a("\x08\x00", 2);
348 Block b("\x08\x00", 2);;
349 BOOST_CHECK_EQUAL(a == b, true);
350 BOOST_CHECK_EQUAL(a != b, false);
351
352 Block c("\x06\x00", 2);
353 Block d("\x08\x00", 2);;
354 BOOST_CHECK_EQUAL(c == d, false);
355 BOOST_CHECK_EQUAL(c != d, true);
356
357 Block e("\x06\x00", 2);
358 Block f("\x06\x01\xcc", 3);;
359 BOOST_CHECK_EQUAL(e == f, false);
360 BOOST_CHECK_EQUAL(e != f, true);
361}
362
Joao Pereira7476ebf2015-07-07 14:54:39 -0400363BOOST_AUTO_TEST_CASE(InsertBeginning)
364{
365 Block masterBlock(tlv::Name);
366 Block firstBlock = makeStringBlock(tlv::NameComponent, "firstName");
367 Block secondBlock = makeStringBlock(tlv::NameComponent, "secondName");
368 Block thirdBlock = makeStringBlock(tlv::NameComponent, "thirdName");
369
370 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 0);
371 masterBlock.push_back(secondBlock);
372 masterBlock.push_back(thirdBlock);
373 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 2);
374 Block::element_const_iterator it = masterBlock.find(tlv::NameComponent);
375 BOOST_CHECK_EQUAL(*it == secondBlock, true);
376
377 it = masterBlock.insert(it, firstBlock);
378
379 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 3);
380 BOOST_CHECK_EQUAL(*(it + 1) == secondBlock, true);
381 BOOST_CHECK_EQUAL(*(masterBlock.elements_begin()) == firstBlock, true);
382}
383
384BOOST_AUTO_TEST_CASE(InsertEnd)
385{
386 Block masterBlock(tlv::Name);
387 Block firstBlock = makeStringBlock(tlv::NameComponent, "firstName");
388 Block secondBlock = makeStringBlock(tlv::NameComponent, "secondName");
389 Block thirdBlock = makeStringBlock(tlv::NameComponent, "thirdName");
390
391 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 0);
392 masterBlock.push_back(firstBlock);
393 masterBlock.push_back(secondBlock);
394 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 2);
395 Block::element_const_iterator it = masterBlock.elements_end();
396 BOOST_CHECK_EQUAL(*(it - 1) == secondBlock, true);
397
398 it = masterBlock.insert(it, thirdBlock);
399
400 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 3);
401 BOOST_CHECK_EQUAL(*(it - 1) == secondBlock, true);
402 BOOST_CHECK_EQUAL(*(masterBlock.elements_end() - 1) == thirdBlock, true);
403}
404
405BOOST_AUTO_TEST_CASE(InsertMiddle)
406{
407 Block masterBlock(tlv::Name);
408 Block firstBlock = makeStringBlock(tlv::NameComponent, "firstName");
409 Block secondBlock = makeStringBlock(tlv::NameComponent, "secondName");
410 Block thirdBlock = makeStringBlock(tlv::NameComponent, "thirdName");
411
412 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 0);
413 masterBlock.push_back(firstBlock);
414 masterBlock.push_back(thirdBlock);
415 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 2);
416 Block::element_const_iterator it = masterBlock.find(tlv::NameComponent);
417 BOOST_CHECK_EQUAL(*it == firstBlock, true);
418
419 it = masterBlock.insert(it+1, secondBlock);
420
421 BOOST_CHECK_EQUAL(*it == secondBlock, true);
422 BOOST_CHECK_EQUAL(*(it + 1) == thirdBlock, true);
423 BOOST_CHECK_EQUAL(*(it - 1) == firstBlock, true);
424}
425
426BOOST_AUTO_TEST_CASE(EraseSingleElement)
427{
428 Block masterBlock(tlv::Name);
429 Block firstBlock = makeStringBlock(tlv::NameComponent, "firstName");
430 Block secondBlock = makeStringBlock(tlv::NameComponent, "secondName");
431 Block thirdBlock = makeStringBlock(tlv::NameComponent, "thirdName");
432
433 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 0);
434 masterBlock.push_back(firstBlock);
435 masterBlock.push_back(secondBlock);
436 masterBlock.push_back(thirdBlock);
437 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 3);
438 Block::element_const_iterator it = masterBlock.find(tlv::NameComponent);
439 it++;
440 BOOST_CHECK_EQUAL(*it == secondBlock, true);
441
442 it = masterBlock.erase(it);
443
444 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 2);
445 BOOST_CHECK_EQUAL(*(it) == thirdBlock, true);
446 BOOST_CHECK_EQUAL(*(it - 1) == firstBlock, true);
447}
448
449BOOST_AUTO_TEST_CASE(EraseRange)
450{
451 Block masterBlock(tlv::Name);
452 Block firstBlock = makeStringBlock(tlv::NameComponent, "firstName");
453 Block secondBlock = makeStringBlock(tlv::NameComponent, "secondName");
454 Block thirdBlock = makeStringBlock(tlv::NameComponent, "thirdName");
455 Block fourthBlock = makeStringBlock(tlv::NameComponent, "fourthName");
456 Block fifthBlock = makeStringBlock(tlv::NameComponent, "fifthName");
457 Block sixthBlock = makeStringBlock(tlv::NameComponent, "sixthName");
458
459 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 0);
460 masterBlock.push_back(firstBlock);
461 masterBlock.push_back(secondBlock);
462 masterBlock.push_back(thirdBlock);
463 masterBlock.push_back(fourthBlock);
464 masterBlock.push_back(fifthBlock);
465 masterBlock.push_back(sixthBlock);
466 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 6);
467 Block::element_const_iterator itStart = masterBlock.find(tlv::NameComponent);
468 itStart++;
469 Block::element_const_iterator itEnd = itStart + 3;
470 BOOST_CHECK_EQUAL(*itStart == secondBlock, true);
471 BOOST_CHECK_EQUAL(*itEnd == fifthBlock, true);
472
473 Block::element_const_iterator newIt = masterBlock.erase(itStart, itEnd);
474
475 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 3);
476 BOOST_CHECK_EQUAL(*(newIt) == fifthBlock, true);
477 BOOST_CHECK_EQUAL(*(newIt - 1) == firstBlock, true);
478}
479
Eric Newberrya3d9fc02015-07-26 10:55:44 -0700480BOOST_AUTO_TEST_CASE(Remove)
481{
482 Block block(tlv::Data);
483 block.push_back(makeNonNegativeIntegerBlock(tlv::ContentType, 0));
484 block.push_back(makeNonNegativeIntegerBlock(tlv::FreshnessPeriod, 123));
485 block.push_back(makeStringBlock(tlv::Name, "ndn:/test-prefix"));
486 block.push_back(makeNonNegativeIntegerBlock(tlv::ContentType, 2));
487 block.push_back(makeNonNegativeIntegerBlock(tlv::ContentType, 1));
488
489 BOOST_CHECK_EQUAL(5, block.elements_size());
490 BOOST_REQUIRE_NO_THROW(block.remove(tlv::ContentType));
491 BOOST_CHECK_EQUAL(2, block.elements_size());
492
493 Block::element_container elements = block.elements();
494
495 BOOST_CHECK_EQUAL(tlv::FreshnessPeriod, elements[0].type());
496 BOOST_CHECK_EQUAL(123, readNonNegativeInteger(elements[0]));
497 BOOST_CHECK_EQUAL(tlv::Name, elements[1].type());
498 BOOST_CHECK(readString(elements[1]).compare("ndn:/test-prefix") == 0);
499}
500
Alexander Afanasyev233750e2014-02-16 00:50:07 -0800501BOOST_AUTO_TEST_SUITE_END()
502
Alexander Afanasyev74633892015-02-08 18:08:46 -0800503} // namespace tests
Alexander Afanasyev233750e2014-02-16 00:50:07 -0800504} // namespace ndn