blob: ac7ff516b161f6c5dc28168e1c695ec2edd46939 [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"
Alexander Afanasyev233750e2014-02-16 00:50:07 -080024
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070025#include "boost-test.hpp"
Junxiao Shiaf8eeea2014-03-31 20:10:56 -070026
Alexander Afanasyev233750e2014-02-16 00:50:07 -080027namespace ndn {
Alexander Afanasyev74633892015-02-08 18:08:46 -080028namespace tests {
Alexander Afanasyev233750e2014-02-16 00:50:07 -080029
Spyridon Mastorakis429634f2015-02-19 17:35:33 -080030BOOST_AUTO_TEST_SUITE(EncodingBlock)
Alexander Afanasyev233750e2014-02-16 00:50:07 -080031
Alexander Afanasyev74633892015-02-08 18:08:46 -080032class BasicBlockFixture
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -080033{
Alexander Afanasyev74633892015-02-08 18:08:46 -080034public:
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -080035 EncodingBuffer buffer;
36 EncodingEstimator estimator;
Alexander Afanasyev74633892015-02-08 18:08:46 -080037};
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -080038
Alexander Afanasyev74633892015-02-08 18:08:46 -080039BOOST_FIXTURE_TEST_SUITE(Basic, BasicBlockFixture)
Alexander Afanasyev937aa782014-03-21 13:17:57 -070040
Alexander Afanasyev74633892015-02-08 18:08:46 -080041BOOST_AUTO_TEST_CASE(VarNumberOneByte1)
42{
43 size_t s1 = buffer.prependVarNumber(252);
44 size_t s2 = estimator.prependVarNumber(252);
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -080045 BOOST_CHECK_EQUAL(buffer.size(), 1);
46 BOOST_CHECK_EQUAL(s1, 1);
47 BOOST_CHECK_EQUAL(s2, 1);
Alexander Afanasyev74633892015-02-08 18:08:46 -080048}
Alexander Afanasyev937aa782014-03-21 13:17:57 -070049
Alexander Afanasyev74633892015-02-08 18:08:46 -080050BOOST_AUTO_TEST_CASE(VarNumberOneByte2)
51{
52 size_t s1 = buffer.prependVarNumber(253);
53 size_t s2 = estimator.prependVarNumber(253);
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -080054 BOOST_CHECK_EQUAL(buffer.size(), 3);
55 BOOST_CHECK_EQUAL(s1, 3);
56 BOOST_CHECK_EQUAL(s2, 3);
Alexander Afanasyev74633892015-02-08 18:08:46 -080057}
Alexander Afanasyev937aa782014-03-21 13:17:57 -070058
Alexander Afanasyev74633892015-02-08 18:08:46 -080059BOOST_AUTO_TEST_CASE(VarNumberThreeBytes1)
60{
61 size_t s1 = buffer.prependVarNumber(255);
62 size_t s2 = estimator.prependVarNumber(255);
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -080063 BOOST_CHECK_EQUAL(buffer.size(), 3);
64 BOOST_CHECK_EQUAL(s1, 3);
65 BOOST_CHECK_EQUAL(s2, 3);
Alexander Afanasyev74633892015-02-08 18:08:46 -080066}
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -080067
Alexander Afanasyev74633892015-02-08 18:08:46 -080068BOOST_AUTO_TEST_CASE(VarNumberThreeBytes2)
69{
70 size_t s1 = buffer.prependVarNumber(65535);
71 size_t s2 = estimator.prependVarNumber(65535);
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -080072 BOOST_CHECK_EQUAL(buffer.size(), 3);
73 BOOST_CHECK_EQUAL(s1, 3);
74 BOOST_CHECK_EQUAL(s2, 3);
Alexander Afanasyev74633892015-02-08 18:08:46 -080075}
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -080076
Alexander Afanasyev74633892015-02-08 18:08:46 -080077BOOST_AUTO_TEST_CASE(VarNumberFiveBytes1)
78{
79 size_t s1 = buffer.prependVarNumber(65536);
80 size_t s2 = estimator.prependVarNumber(65536);
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -080081 BOOST_CHECK_EQUAL(buffer.size(), 5);
82 BOOST_CHECK_EQUAL(s1, 5);
83 BOOST_CHECK_EQUAL(s2, 5);
Alexander Afanasyev74633892015-02-08 18:08:46 -080084}
Alexander Afanasyev937aa782014-03-21 13:17:57 -070085
Alexander Afanasyev74633892015-02-08 18:08:46 -080086BOOST_AUTO_TEST_CASE(VarNumberFiveBytes2)
87{
88 size_t s1 = buffer.prependVarNumber(4294967295LL);
89 size_t s2 = estimator.prependVarNumber(4294967295LL);
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -080090 BOOST_CHECK_EQUAL(buffer.size(), 5);
91 BOOST_CHECK_EQUAL(s1, 5);
92 BOOST_CHECK_EQUAL(s2, 5);
Alexander Afanasyev74633892015-02-08 18:08:46 -080093}
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -080094
Alexander Afanasyev74633892015-02-08 18:08:46 -080095BOOST_AUTO_TEST_CASE(VarNumberNineBytes)
96{
97 size_t s1 = buffer.prependVarNumber(4294967296LL);
98 size_t s2 = estimator.prependVarNumber(4294967296LL);
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -080099 BOOST_CHECK_EQUAL(buffer.size(), 9);
100 BOOST_CHECK_EQUAL(s1, 9);
101 BOOST_CHECK_EQUAL(s2, 9);
Alexander Afanasyev74633892015-02-08 18:08:46 -0800102}
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -0800103
Alexander Afanasyev74633892015-02-08 18:08:46 -0800104BOOST_AUTO_TEST_CASE(NonNegativeNumberOneByte1)
105{
106 size_t s1 = buffer.prependNonNegativeInteger(252);
107 size_t s2 = estimator.prependNonNegativeInteger(252);
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -0800108 BOOST_CHECK_EQUAL(buffer.size(), 1);
109 BOOST_CHECK_EQUAL(s1, 1);
110 BOOST_CHECK_EQUAL(s2, 1);
Alexander Afanasyev74633892015-02-08 18:08:46 -0800111}
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700112
Alexander Afanasyev74633892015-02-08 18:08:46 -0800113BOOST_AUTO_TEST_CASE(NonNegativeNumberOneByte2)
114{
115 size_t s1 = buffer.prependNonNegativeInteger(255);
116 size_t s2 = estimator.prependNonNegativeInteger(255);
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -0800117 BOOST_CHECK_EQUAL(buffer.size(), 1);
118 BOOST_CHECK_EQUAL(s1, 1);
119 BOOST_CHECK_EQUAL(s2, 1);
Alexander Afanasyev74633892015-02-08 18:08:46 -0800120}
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700121
Alexander Afanasyev74633892015-02-08 18:08:46 -0800122BOOST_AUTO_TEST_CASE(NonNegativeNumberTwoBytes1)
123{
124 size_t s1 = buffer.prependNonNegativeInteger(256);
125 size_t s2 = estimator.prependNonNegativeInteger(256);
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -0800126 BOOST_CHECK_EQUAL(buffer.size(), 2);
127 BOOST_CHECK_EQUAL(s1, 2);
128 BOOST_CHECK_EQUAL(s2, 2);
Alexander Afanasyev74633892015-02-08 18:08:46 -0800129}
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -0800130
Alexander Afanasyev74633892015-02-08 18:08:46 -0800131BOOST_AUTO_TEST_CASE(NonNegativeNumberTwoBytes2)
132{
133 size_t s1 = buffer.prependNonNegativeInteger(65535);
134 size_t s2 = estimator.prependNonNegativeInteger(65535);
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -0800135 BOOST_CHECK_EQUAL(buffer.size(), 2);
136 BOOST_CHECK_EQUAL(s1, 2);
137 BOOST_CHECK_EQUAL(s2, 2);
Alexander Afanasyev74633892015-02-08 18:08:46 -0800138}
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -0800139
Alexander Afanasyev74633892015-02-08 18:08:46 -0800140BOOST_AUTO_TEST_CASE(NonNegativeNumberFourBytes1)
141{
142 size_t s1 = buffer.prependNonNegativeInteger(65536);
143 size_t s2 = estimator.prependNonNegativeInteger(65536);
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -0800144 BOOST_CHECK_EQUAL(buffer.size(), 4);
145 BOOST_CHECK_EQUAL(s1, 4);
146 BOOST_CHECK_EQUAL(s2, 4);
Alexander Afanasyev74633892015-02-08 18:08:46 -0800147}
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -0800148
Alexander Afanasyev74633892015-02-08 18:08:46 -0800149BOOST_AUTO_TEST_CASE(NonNegativeNumberFourBytes2)
150{
151 size_t s1 = buffer.prependNonNegativeInteger(4294967295LL);
152 size_t s2 = estimator.prependNonNegativeInteger(4294967295LL);
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -0800153 BOOST_CHECK_EQUAL(buffer.size(), 4);
154 BOOST_CHECK_EQUAL(s1, 4);
155 BOOST_CHECK_EQUAL(s2, 4);
Alexander Afanasyev74633892015-02-08 18:08:46 -0800156}
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -0800157
Alexander Afanasyev74633892015-02-08 18:08:46 -0800158BOOST_AUTO_TEST_CASE(NonNegativeNumberEightBytes)
159{
160 size_t s1 = buffer.prependNonNegativeInteger(4294967296LL);
161 size_t s2 = estimator.prependNonNegativeInteger(4294967296LL);
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -0800162 BOOST_CHECK_EQUAL(buffer.size(), 8);
163 BOOST_CHECK_EQUAL(s1, 8);
164 BOOST_CHECK_EQUAL(s2, 8);
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -0800165}
166
Alexander Afanasyev74633892015-02-08 18:08:46 -0800167BOOST_AUTO_TEST_SUITE_END() // Basic
168
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700169BOOST_AUTO_TEST_CASE(EncodingBufferToBlock)
Alexander Afanasyev233750e2014-02-16 00:50:07 -0800170{
171 uint8_t value[4];
172
Alexander Afanasyev15151312014-02-16 00:53:51 -0800173 EncodingBuffer buffer;
Alexander Afanasyev233750e2014-02-16 00:50:07 -0800174 size_t length = buffer.prependByteArray(value, sizeof(value));
175 buffer.prependVarNumber(length);
176 buffer.prependVarNumber(0xe0);
177
178 Block block;
179 BOOST_REQUIRE_NO_THROW(block = buffer.block());
180 BOOST_CHECK_EQUAL(block.type(), 0xe0);
181 BOOST_CHECK_EQUAL(block.value_size(), sizeof(value));
Alexander Afanasyev15151312014-02-16 00:53:51 -0800182
183 BOOST_REQUIRE_NO_THROW(block = Block(buffer));
184 BOOST_CHECK_EQUAL(block.type(), 0xe0);
185 BOOST_CHECK_EQUAL(block.value_size(), sizeof(value));
186}
187
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700188BOOST_AUTO_TEST_CASE(BlockToBuffer)
Alexander Afanasyev15151312014-02-16 00:53:51 -0800189{
190 shared_ptr<Buffer> buf = make_shared<Buffer>(10);
191 for (int i = 0; i < 10; i++) (*buf)[i] = i;
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700192
Alexander Afanasyev15151312014-02-16 00:53:51 -0800193 Block block(0xab, buf);
194 block.encode();
195
Alexander Afanasyev74633892015-02-08 18:08:46 -0800196 {
197 BOOST_REQUIRE_NO_THROW(EncodingBuffer(block));
198 EncodingBuffer buffer(block);
199 BOOST_CHECK_EQUAL(buffer.size(), 12);
200 BOOST_CHECK_EQUAL(buffer.capacity(), 12);
201 }
Alexander Afanasyev15151312014-02-16 00:53:51 -0800202
203 (*buf)[1] = 0xe0;
204 (*buf)[2] = 2;
205 BOOST_REQUIRE_NO_THROW(block = Block(buf, buf->begin() + 1, buf->begin() + 5));
206 BOOST_CHECK_EQUAL(block.type(), 0xe0);
207
Alexander Afanasyev74633892015-02-08 18:08:46 -0800208 {
209 BOOST_REQUIRE_NO_THROW(EncodingBuffer(block));
210 EncodingBuffer buffer(block);
211 BOOST_CHECK_EQUAL(buffer.size(), 4);
212 BOOST_CHECK_EQUAL(buffer.capacity(), 10);
213 }
Alexander Afanasyev233750e2014-02-16 00:50:07 -0800214}
215
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700216BOOST_AUTO_TEST_CASE(FromBuffer)
217{
218 const uint8_t TEST_BUFFER[] = {0x00, 0x01, 0xfa, // ok
219 0x01, 0x01, 0xfb, // ok
220 0x03, 0x02, 0xff}; // not ok
221 BufferPtr buffer(new Buffer(TEST_BUFFER, sizeof(TEST_BUFFER)));
222
223 // using BufferPtr (avoids memory copy)
224 size_t offset = 0;
Junxiao Shi02a4bf32015-02-21 21:07:46 -0700225 bool isOk = false;
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700226 Block testBlock;
Junxiao Shi02a4bf32015-02-21 21:07:46 -0700227 std::tie(isOk, testBlock) = Block::fromBuffer(buffer, offset);
228 BOOST_CHECK(isOk);
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700229 BOOST_CHECK_EQUAL(testBlock.type(), 0);
230 BOOST_CHECK_EQUAL(testBlock.size(), 3);
231 BOOST_CHECK_EQUAL(testBlock.value_size(), 1);
232 BOOST_CHECK_EQUAL(*testBlock.wire(), 0x00);
233 BOOST_CHECK_EQUAL(*testBlock.value(), 0xfa);
234 offset += testBlock.size();
235
Junxiao Shi02a4bf32015-02-21 21:07:46 -0700236 std::tie(isOk, testBlock) = Block::fromBuffer(buffer, offset);
237 BOOST_CHECK(isOk);
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700238 BOOST_CHECK_EQUAL(testBlock.type(), 1);
239 BOOST_CHECK_EQUAL(testBlock.size(), 3);
240 BOOST_CHECK_EQUAL(testBlock.value_size(), 1);
241 BOOST_CHECK_EQUAL(*testBlock.wire(), 0x01);
242 BOOST_CHECK_EQUAL(*testBlock.value(), 0xfb);
243 offset += testBlock.size();
244
Junxiao Shi02a4bf32015-02-21 21:07:46 -0700245 std::tie(isOk, testBlock) = Block::fromBuffer(buffer, offset);
246 BOOST_CHECK(!isOk);
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700247
248 // just buffer, copies memory
249 offset = 0;
Junxiao Shi02a4bf32015-02-21 21:07:46 -0700250 std::tie(isOk, testBlock) = Block::fromBuffer(TEST_BUFFER + offset,
251 sizeof(TEST_BUFFER) - offset);
252 BOOST_CHECK(isOk);
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700253 BOOST_CHECK_EQUAL(testBlock.type(), 0);
254 BOOST_CHECK_EQUAL(testBlock.size(), 3);
255 BOOST_CHECK_EQUAL(testBlock.value_size(), 1);
256 BOOST_CHECK_EQUAL(*testBlock.wire(), 0x00);
257 BOOST_CHECK_EQUAL(*testBlock.value(), 0xfa);
258 offset += testBlock.size();
259
Junxiao Shi02a4bf32015-02-21 21:07:46 -0700260 std::tie(isOk, testBlock) = Block::fromBuffer(TEST_BUFFER + offset,
261 sizeof(TEST_BUFFER) - offset);
262 BOOST_CHECK(isOk);
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700263 BOOST_CHECK_EQUAL(testBlock.type(), 1);
264 BOOST_CHECK_EQUAL(testBlock.size(), 3);
265 BOOST_CHECK_EQUAL(testBlock.value_size(), 1);
266 BOOST_CHECK_EQUAL(*testBlock.wire(), 0x01);
267 BOOST_CHECK_EQUAL(*testBlock.value(), 0xfb);
268 offset += testBlock.size();
269
Junxiao Shi02a4bf32015-02-21 21:07:46 -0700270 std::tie(isOk, testBlock) = Block::fromBuffer(TEST_BUFFER + offset,
271 sizeof(TEST_BUFFER) - offset);
272 BOOST_CHECK(!isOk);
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700273}
274
Alexander Afanasyev21ef2392014-03-25 12:40:22 -0700275BOOST_AUTO_TEST_CASE(BlockFromStream)
276{
277 const uint8_t TEST_BUFFER[] = {0x00, 0x01, 0xfa, // ok
278 0x01, 0x01, 0xfb, // ok
279 0x03, 0x02, 0xff}; // not ok
280
281 typedef boost::iostreams::stream<boost::iostreams::array_source> ArrayStream;
282 ArrayStream stream(reinterpret_cast<const char*>(TEST_BUFFER), sizeof(TEST_BUFFER));
283
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700284
Alexander Afanasyev21ef2392014-03-25 12:40:22 -0700285 Block testBlock;
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700286 BOOST_REQUIRE_NO_THROW(testBlock = Block::fromStream(stream));
Alexander Afanasyev21ef2392014-03-25 12:40:22 -0700287 BOOST_CHECK_EQUAL(testBlock.type(), 0);
288 BOOST_CHECK_EQUAL(testBlock.size(), 3);
289 BOOST_CHECK_EQUAL(testBlock.value_size(), 1);
290 BOOST_CHECK_EQUAL(*testBlock.wire(), 0x00);
291 BOOST_CHECK_EQUAL(*testBlock.value(), 0xfa);
292
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700293 BOOST_REQUIRE_NO_THROW(testBlock = Block::fromStream(stream));
Alexander Afanasyev21ef2392014-03-25 12:40:22 -0700294 BOOST_CHECK_EQUAL(testBlock.type(), 1);
295 BOOST_CHECK_EQUAL(testBlock.size(), 3);
296 BOOST_CHECK_EQUAL(testBlock.value_size(), 1);
297 BOOST_CHECK_EQUAL(*testBlock.wire(), 0x01);
298 BOOST_CHECK_EQUAL(*testBlock.value(), 0xfb);
299
Steve DiBenedetto54ce6682014-07-22 13:22:57 -0600300 BOOST_CHECK_THROW(Block::fromStream(stream), tlv::Error);
Alexander Afanasyev21ef2392014-03-25 12:40:22 -0700301}
302
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700303BOOST_AUTO_TEST_CASE(Equality)
304{
305 BOOST_CONCEPT_ASSERT((boost::EqualityComparable<Block>));
306
307 Block a("\x08\x00", 2);
308 Block b("\x08\x00", 2);;
309 BOOST_CHECK_EQUAL(a == b, true);
310 BOOST_CHECK_EQUAL(a != b, false);
311
312 Block c("\x06\x00", 2);
313 Block d("\x08\x00", 2);;
314 BOOST_CHECK_EQUAL(c == d, false);
315 BOOST_CHECK_EQUAL(c != d, true);
316
317 Block e("\x06\x00", 2);
318 Block f("\x06\x01\xcc", 3);;
319 BOOST_CHECK_EQUAL(e == f, false);
320 BOOST_CHECK_EQUAL(e != f, true);
321}
322
Alexander Afanasyev233750e2014-02-16 00:50:07 -0800323BOOST_AUTO_TEST_SUITE_END()
324
Alexander Afanasyev74633892015-02-08 18:08:46 -0800325} // namespace tests
Alexander Afanasyev233750e2014-02-16 00:50:07 -0800326} // namespace ndn