blob: e9e8c7679976eece14d178886f272bd44673abfe [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;
225 Block testBlock;
226 BOOST_CHECK(Block::fromBuffer(buffer, offset, testBlock));
227 BOOST_CHECK_EQUAL(testBlock.type(), 0);
228 BOOST_CHECK_EQUAL(testBlock.size(), 3);
229 BOOST_CHECK_EQUAL(testBlock.value_size(), 1);
230 BOOST_CHECK_EQUAL(*testBlock.wire(), 0x00);
231 BOOST_CHECK_EQUAL(*testBlock.value(), 0xfa);
232 offset += testBlock.size();
233
234 BOOST_CHECK(Block::fromBuffer(buffer, offset, testBlock));
235 BOOST_CHECK_EQUAL(testBlock.type(), 1);
236 BOOST_CHECK_EQUAL(testBlock.size(), 3);
237 BOOST_CHECK_EQUAL(testBlock.value_size(), 1);
238 BOOST_CHECK_EQUAL(*testBlock.wire(), 0x01);
239 BOOST_CHECK_EQUAL(*testBlock.value(), 0xfb);
240 offset += testBlock.size();
241
242 BOOST_CHECK(!Block::fromBuffer(buffer, offset, testBlock));
243
244 // just buffer, copies memory
245 offset = 0;
246 BOOST_CHECK(Block::fromBuffer(TEST_BUFFER + offset, sizeof(TEST_BUFFER) - offset, testBlock));
247 BOOST_CHECK_EQUAL(testBlock.type(), 0);
248 BOOST_CHECK_EQUAL(testBlock.size(), 3);
249 BOOST_CHECK_EQUAL(testBlock.value_size(), 1);
250 BOOST_CHECK_EQUAL(*testBlock.wire(), 0x00);
251 BOOST_CHECK_EQUAL(*testBlock.value(), 0xfa);
252 offset += testBlock.size();
253
254 BOOST_CHECK(Block::fromBuffer(TEST_BUFFER + offset, sizeof(TEST_BUFFER) - offset, testBlock));
255 BOOST_CHECK_EQUAL(testBlock.type(), 1);
256 BOOST_CHECK_EQUAL(testBlock.size(), 3);
257 BOOST_CHECK_EQUAL(testBlock.value_size(), 1);
258 BOOST_CHECK_EQUAL(*testBlock.wire(), 0x01);
259 BOOST_CHECK_EQUAL(*testBlock.value(), 0xfb);
260 offset += testBlock.size();
261
262 BOOST_CHECK(!Block::fromBuffer(TEST_BUFFER + offset, sizeof(TEST_BUFFER) - offset, testBlock));
263}
264
Alexander Afanasyev21ef2392014-03-25 12:40:22 -0700265BOOST_AUTO_TEST_CASE(BlockFromStream)
266{
267 const uint8_t TEST_BUFFER[] = {0x00, 0x01, 0xfa, // ok
268 0x01, 0x01, 0xfb, // ok
269 0x03, 0x02, 0xff}; // not ok
270
271 typedef boost::iostreams::stream<boost::iostreams::array_source> ArrayStream;
272 ArrayStream stream(reinterpret_cast<const char*>(TEST_BUFFER), sizeof(TEST_BUFFER));
273
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700274
Alexander Afanasyev21ef2392014-03-25 12:40:22 -0700275 Block testBlock;
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700276 BOOST_REQUIRE_NO_THROW(testBlock = Block::fromStream(stream));
Alexander Afanasyev21ef2392014-03-25 12:40:22 -0700277 BOOST_CHECK_EQUAL(testBlock.type(), 0);
278 BOOST_CHECK_EQUAL(testBlock.size(), 3);
279 BOOST_CHECK_EQUAL(testBlock.value_size(), 1);
280 BOOST_CHECK_EQUAL(*testBlock.wire(), 0x00);
281 BOOST_CHECK_EQUAL(*testBlock.value(), 0xfa);
282
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700283 BOOST_REQUIRE_NO_THROW(testBlock = Block::fromStream(stream));
Alexander Afanasyev21ef2392014-03-25 12:40:22 -0700284 BOOST_CHECK_EQUAL(testBlock.type(), 1);
285 BOOST_CHECK_EQUAL(testBlock.size(), 3);
286 BOOST_CHECK_EQUAL(testBlock.value_size(), 1);
287 BOOST_CHECK_EQUAL(*testBlock.wire(), 0x01);
288 BOOST_CHECK_EQUAL(*testBlock.value(), 0xfb);
289
Steve DiBenedetto54ce6682014-07-22 13:22:57 -0600290 BOOST_CHECK_THROW(Block::fromStream(stream), tlv::Error);
Alexander Afanasyev21ef2392014-03-25 12:40:22 -0700291}
292
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700293BOOST_AUTO_TEST_CASE(Equality)
294{
295 BOOST_CONCEPT_ASSERT((boost::EqualityComparable<Block>));
296
297 Block a("\x08\x00", 2);
298 Block b("\x08\x00", 2);;
299 BOOST_CHECK_EQUAL(a == b, true);
300 BOOST_CHECK_EQUAL(a != b, false);
301
302 Block c("\x06\x00", 2);
303 Block d("\x08\x00", 2);;
304 BOOST_CHECK_EQUAL(c == d, false);
305 BOOST_CHECK_EQUAL(c != d, true);
306
307 Block e("\x06\x00", 2);
308 Block f("\x06\x01\xcc", 3);;
309 BOOST_CHECK_EQUAL(e == f, false);
310 BOOST_CHECK_EQUAL(e != f, true);
311}
312
Alexander Afanasyev233750e2014-02-16 00:50:07 -0800313BOOST_AUTO_TEST_SUITE_END()
314
Alexander Afanasyev74633892015-02-08 18:08:46 -0800315} // namespace tests
Alexander Afanasyev233750e2014-02-16 00:50:07 -0800316} // namespace ndn