blob: a8c104b6389bd39d9e14a6fb125062e295eb641e [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 Afanasyevc169a812014-05-20 20:37:29 -04003 * Copyright (c) 2013-2014 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 Afanasyev8b1674a2014-05-15 00:58:43 -070022
Alexander Afanasyev233750e2014-02-16 00:50:07 -080023#include "encoding/encoding-buffer.hpp"
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070024#include "encoding/buffer-stream.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 -080028using namespace std;
29namespace ndn {
30
31BOOST_AUTO_TEST_SUITE(TestBlock)
32
Alexander Afanasyev21ef2392014-03-25 12:40:22 -070033BOOST_AUTO_TEST_CASE(BlockBasic)
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -080034{
35 EncodingBuffer buffer;
36 EncodingEstimator estimator;
37 size_t s1, s2;
38
39 // VarNumber checks
Alexander Afanasyev937aa782014-03-21 13:17:57 -070040
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -080041 s1 = buffer.prependVarNumber(252);
42 s2 = estimator.prependVarNumber(252);
43 BOOST_CHECK_EQUAL(buffer.size(), 1);
44 BOOST_CHECK_EQUAL(s1, 1);
45 BOOST_CHECK_EQUAL(s2, 1);
46 buffer = EncodingBuffer();
Alexander Afanasyev937aa782014-03-21 13:17:57 -070047
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -080048 s1 = buffer.prependVarNumber(253);
49 s2 = estimator.prependVarNumber(253);
50 BOOST_CHECK_EQUAL(buffer.size(), 3);
51 BOOST_CHECK_EQUAL(s1, 3);
52 BOOST_CHECK_EQUAL(s2, 3);
53 buffer = EncodingBuffer();
Alexander Afanasyev937aa782014-03-21 13:17:57 -070054
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -080055 s1 = buffer.prependVarNumber(255);
56 s2 = estimator.prependVarNumber(255);
57 BOOST_CHECK_EQUAL(buffer.size(), 3);
58 BOOST_CHECK_EQUAL(s1, 3);
59 BOOST_CHECK_EQUAL(s2, 3);
60 buffer = EncodingBuffer();
61
62 s1 = buffer.prependVarNumber(65535);
63 s2 = estimator.prependVarNumber(65535);
64 BOOST_CHECK_EQUAL(buffer.size(), 3);
65 BOOST_CHECK_EQUAL(s1, 3);
66 BOOST_CHECK_EQUAL(s2, 3);
67 buffer = EncodingBuffer();
68
69 s1 = buffer.prependVarNumber(65536);
70 s2 = estimator.prependVarNumber(65536);
71 BOOST_CHECK_EQUAL(buffer.size(), 5);
72 BOOST_CHECK_EQUAL(s1, 5);
73 BOOST_CHECK_EQUAL(s2, 5);
74 buffer = EncodingBuffer();
Alexander Afanasyev937aa782014-03-21 13:17:57 -070075
Alexander Afanasyev21ef2392014-03-25 12:40:22 -070076 s1 = buffer.prependVarNumber(4294967295LL);
77 s2 = estimator.prependVarNumber(4294967295LL);
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -080078 BOOST_CHECK_EQUAL(buffer.size(), 5);
79 BOOST_CHECK_EQUAL(s1, 5);
80 BOOST_CHECK_EQUAL(s2, 5);
81 buffer = EncodingBuffer();
82
Alexander Afanasyev21ef2392014-03-25 12:40:22 -070083 s1 = buffer.prependVarNumber(4294967296LL);
84 s2 = estimator.prependVarNumber(4294967296LL);
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -080085 BOOST_CHECK_EQUAL(buffer.size(), 9);
86 BOOST_CHECK_EQUAL(s1, 9);
87 BOOST_CHECK_EQUAL(s2, 9);
88 buffer = EncodingBuffer();
89
90 // nonNegativeInteger checks
Alexander Afanasyev937aa782014-03-21 13:17:57 -070091
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -080092 s1 = buffer.prependNonNegativeInteger(252);
93 s2 = estimator.prependNonNegativeInteger(252);
94 BOOST_CHECK_EQUAL(buffer.size(), 1);
95 BOOST_CHECK_EQUAL(s1, 1);
96 BOOST_CHECK_EQUAL(s2, 1);
97 buffer = EncodingBuffer();
Alexander Afanasyev937aa782014-03-21 13:17:57 -070098
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -080099 s1 = buffer.prependNonNegativeInteger(255);
100 s2 = estimator.prependNonNegativeInteger(255);
101 BOOST_CHECK_EQUAL(buffer.size(), 1);
102 BOOST_CHECK_EQUAL(s1, 1);
103 BOOST_CHECK_EQUAL(s2, 1);
104 buffer = EncodingBuffer();
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700105
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -0800106 s1 = buffer.prependNonNegativeInteger(256);
107 s2 = estimator.prependNonNegativeInteger(256);
108 BOOST_CHECK_EQUAL(buffer.size(), 2);
109 BOOST_CHECK_EQUAL(s1, 2);
110 BOOST_CHECK_EQUAL(s2, 2);
111 buffer = EncodingBuffer();
112
113 s1 = buffer.prependNonNegativeInteger(65535);
114 s2 = estimator.prependNonNegativeInteger(65535);
115 BOOST_CHECK_EQUAL(buffer.size(), 2);
116 BOOST_CHECK_EQUAL(s1, 2);
117 BOOST_CHECK_EQUAL(s2, 2);
118 buffer = EncodingBuffer();
119
120 s1 = buffer.prependNonNegativeInteger(65536);
121 s2 = estimator.prependNonNegativeInteger(65536);
122 BOOST_CHECK_EQUAL(buffer.size(), 4);
123 BOOST_CHECK_EQUAL(s1, 4);
124 BOOST_CHECK_EQUAL(s2, 4);
125 buffer = EncodingBuffer();
126
Alexander Afanasyev21ef2392014-03-25 12:40:22 -0700127 s1 = buffer.prependNonNegativeInteger(4294967295LL);
128 s2 = estimator.prependNonNegativeInteger(4294967295LL);
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -0800129 BOOST_CHECK_EQUAL(buffer.size(), 4);
130 BOOST_CHECK_EQUAL(s1, 4);
131 BOOST_CHECK_EQUAL(s2, 4);
132 buffer = EncodingBuffer();
133
Alexander Afanasyev21ef2392014-03-25 12:40:22 -0700134 s1 = buffer.prependNonNegativeInteger(4294967296LL);
135 s2 = estimator.prependNonNegativeInteger(4294967296LL);
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -0800136 BOOST_CHECK_EQUAL(buffer.size(), 8);
137 BOOST_CHECK_EQUAL(s1, 8);
138 BOOST_CHECK_EQUAL(s2, 8);
139 buffer = EncodingBuffer();
140}
141
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700142BOOST_AUTO_TEST_CASE(EncodingBufferToBlock)
Alexander Afanasyev233750e2014-02-16 00:50:07 -0800143{
144 uint8_t value[4];
145
Alexander Afanasyev15151312014-02-16 00:53:51 -0800146 EncodingBuffer buffer;
Alexander Afanasyev233750e2014-02-16 00:50:07 -0800147 size_t length = buffer.prependByteArray(value, sizeof(value));
148 buffer.prependVarNumber(length);
149 buffer.prependVarNumber(0xe0);
150
151 Block block;
152 BOOST_REQUIRE_NO_THROW(block = buffer.block());
153 BOOST_CHECK_EQUAL(block.type(), 0xe0);
154 BOOST_CHECK_EQUAL(block.value_size(), sizeof(value));
Alexander Afanasyev15151312014-02-16 00:53:51 -0800155
156 BOOST_REQUIRE_NO_THROW(block = Block(buffer));
157 BOOST_CHECK_EQUAL(block.type(), 0xe0);
158 BOOST_CHECK_EQUAL(block.value_size(), sizeof(value));
159}
160
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700161BOOST_AUTO_TEST_CASE(BlockToBuffer)
Alexander Afanasyev15151312014-02-16 00:53:51 -0800162{
163 shared_ptr<Buffer> buf = make_shared<Buffer>(10);
164 for (int i = 0; i < 10; i++) (*buf)[i] = i;
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700165
Alexander Afanasyev15151312014-02-16 00:53:51 -0800166 Block block(0xab, buf);
167 block.encode();
168
169 EncodingBuffer buffer(0,0);
170 BOOST_REQUIRE_NO_THROW(buffer = EncodingBuffer(block));
171 BOOST_CHECK_EQUAL(buffer.size(), 12);
172 BOOST_CHECK_EQUAL(buffer.capacity(), 12);
173
174 (*buf)[1] = 0xe0;
175 (*buf)[2] = 2;
176 BOOST_REQUIRE_NO_THROW(block = Block(buf, buf->begin() + 1, buf->begin() + 5));
177 BOOST_CHECK_EQUAL(block.type(), 0xe0);
178
179 BOOST_REQUIRE_NO_THROW(buffer = EncodingBuffer(block));
180 BOOST_CHECK_EQUAL(buffer.size(), 4);
181 BOOST_CHECK_EQUAL(buffer.capacity(), 10);
Alexander Afanasyev233750e2014-02-16 00:50:07 -0800182}
183
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700184BOOST_AUTO_TEST_CASE(FromBuffer)
185{
186 const uint8_t TEST_BUFFER[] = {0x00, 0x01, 0xfa, // ok
187 0x01, 0x01, 0xfb, // ok
188 0x03, 0x02, 0xff}; // not ok
189 BufferPtr buffer(new Buffer(TEST_BUFFER, sizeof(TEST_BUFFER)));
190
191 // using BufferPtr (avoids memory copy)
192 size_t offset = 0;
193 Block testBlock;
194 BOOST_CHECK(Block::fromBuffer(buffer, offset, testBlock));
195 BOOST_CHECK_EQUAL(testBlock.type(), 0);
196 BOOST_CHECK_EQUAL(testBlock.size(), 3);
197 BOOST_CHECK_EQUAL(testBlock.value_size(), 1);
198 BOOST_CHECK_EQUAL(*testBlock.wire(), 0x00);
199 BOOST_CHECK_EQUAL(*testBlock.value(), 0xfa);
200 offset += testBlock.size();
201
202 BOOST_CHECK(Block::fromBuffer(buffer, offset, testBlock));
203 BOOST_CHECK_EQUAL(testBlock.type(), 1);
204 BOOST_CHECK_EQUAL(testBlock.size(), 3);
205 BOOST_CHECK_EQUAL(testBlock.value_size(), 1);
206 BOOST_CHECK_EQUAL(*testBlock.wire(), 0x01);
207 BOOST_CHECK_EQUAL(*testBlock.value(), 0xfb);
208 offset += testBlock.size();
209
210 BOOST_CHECK(!Block::fromBuffer(buffer, offset, testBlock));
211
212 // just buffer, copies memory
213 offset = 0;
214 BOOST_CHECK(Block::fromBuffer(TEST_BUFFER + offset, sizeof(TEST_BUFFER) - offset, testBlock));
215 BOOST_CHECK_EQUAL(testBlock.type(), 0);
216 BOOST_CHECK_EQUAL(testBlock.size(), 3);
217 BOOST_CHECK_EQUAL(testBlock.value_size(), 1);
218 BOOST_CHECK_EQUAL(*testBlock.wire(), 0x00);
219 BOOST_CHECK_EQUAL(*testBlock.value(), 0xfa);
220 offset += testBlock.size();
221
222 BOOST_CHECK(Block::fromBuffer(TEST_BUFFER + offset, sizeof(TEST_BUFFER) - offset, testBlock));
223 BOOST_CHECK_EQUAL(testBlock.type(), 1);
224 BOOST_CHECK_EQUAL(testBlock.size(), 3);
225 BOOST_CHECK_EQUAL(testBlock.value_size(), 1);
226 BOOST_CHECK_EQUAL(*testBlock.wire(), 0x01);
227 BOOST_CHECK_EQUAL(*testBlock.value(), 0xfb);
228 offset += testBlock.size();
229
230 BOOST_CHECK(!Block::fromBuffer(TEST_BUFFER + offset, sizeof(TEST_BUFFER) - offset, testBlock));
231}
232
Alexander Afanasyev21ef2392014-03-25 12:40:22 -0700233BOOST_AUTO_TEST_CASE(BlockFromStream)
234{
235 const uint8_t TEST_BUFFER[] = {0x00, 0x01, 0xfa, // ok
236 0x01, 0x01, 0xfb, // ok
237 0x03, 0x02, 0xff}; // not ok
238
239 typedef boost::iostreams::stream<boost::iostreams::array_source> ArrayStream;
240 ArrayStream stream(reinterpret_cast<const char*>(TEST_BUFFER), sizeof(TEST_BUFFER));
241
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700242
Alexander Afanasyev21ef2392014-03-25 12:40:22 -0700243 Block testBlock;
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700244 BOOST_REQUIRE_NO_THROW(testBlock = Block::fromStream(stream));
Alexander Afanasyev21ef2392014-03-25 12:40:22 -0700245 BOOST_CHECK_EQUAL(testBlock.type(), 0);
246 BOOST_CHECK_EQUAL(testBlock.size(), 3);
247 BOOST_CHECK_EQUAL(testBlock.value_size(), 1);
248 BOOST_CHECK_EQUAL(*testBlock.wire(), 0x00);
249 BOOST_CHECK_EQUAL(*testBlock.value(), 0xfa);
250
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700251 BOOST_REQUIRE_NO_THROW(testBlock = Block::fromStream(stream));
Alexander Afanasyev21ef2392014-03-25 12:40:22 -0700252 BOOST_CHECK_EQUAL(testBlock.type(), 1);
253 BOOST_CHECK_EQUAL(testBlock.size(), 3);
254 BOOST_CHECK_EQUAL(testBlock.value_size(), 1);
255 BOOST_CHECK_EQUAL(*testBlock.wire(), 0x01);
256 BOOST_CHECK_EQUAL(*testBlock.value(), 0xfb);
257
Steve DiBenedetto54ce6682014-07-22 13:22:57 -0600258 BOOST_CHECK_THROW(Block::fromStream(stream), tlv::Error);
Alexander Afanasyev21ef2392014-03-25 12:40:22 -0700259}
260
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700261BOOST_AUTO_TEST_CASE(Equality)
262{
263 BOOST_CONCEPT_ASSERT((boost::EqualityComparable<Block>));
264
265 Block a("\x08\x00", 2);
266 Block b("\x08\x00", 2);;
267 BOOST_CHECK_EQUAL(a == b, true);
268 BOOST_CHECK_EQUAL(a != b, false);
269
270 Block c("\x06\x00", 2);
271 Block d("\x08\x00", 2);;
272 BOOST_CHECK_EQUAL(c == d, false);
273 BOOST_CHECK_EQUAL(c != d, true);
274
275 Block e("\x06\x00", 2);
276 Block f("\x06\x01\xcc", 3);;
277 BOOST_CHECK_EQUAL(e == f, false);
278 BOOST_CHECK_EQUAL(e != f, true);
279}
280
Alexander Afanasyev233750e2014-02-16 00:50:07 -0800281BOOST_AUTO_TEST_SUITE_END()
282
283} // namespace ndn