blob: 7aeec58b5f7e98652b97cfcc077311474b5e3652 [file] [log] [blame]
Alexander Afanasyevd1de3972014-08-14 19:47:41 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2014 Regents of the University of California.
4 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6 *
7 * 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.
20 */
21
22#include "encoding/tlv.hpp"
23
24#include "boost-test.hpp"
25#include <boost/iostreams/stream.hpp>
26
27namespace ndn {
28namespace tlv {
29namespace tests {
30
31using std::ostringstream;
32
33BOOST_AUTO_TEST_SUITE(EncodingTlv)
34
35BOOST_AUTO_TEST_CASE(Exception)
36{
37 BOOST_CHECK_THROW(throw Error("Test"), Error);
38}
39
40BOOST_AUTO_TEST_SUITE(VarNumber)
41
42static const uint8_t BUFFER[] = {
43 0x01, // == 1
44 0xfc, // == 252
45 0xfd, 0x00, 0xfd, // == 253
46 0xfe, 0x00, 0x01, 0x00, 0x00, // == 65536
47 0xff, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00 // == 4294967296LL
48};
49
50BOOST_AUTO_TEST_CASE(SizeOf)
51{
52 BOOST_CHECK_EQUAL(sizeOfVarNumber(1), 1);
53 BOOST_CHECK_EQUAL(sizeOfVarNumber(252), 1);
54 BOOST_CHECK_EQUAL(sizeOfVarNumber(253), 3);
55 BOOST_CHECK_EQUAL(sizeOfVarNumber(65536), 5);
56 BOOST_CHECK_EQUAL(sizeOfVarNumber(4294967296LL), 9);
57}
58
59BOOST_AUTO_TEST_CASE(Write)
60{
61 ostringstream os;
62
63 writeVarNumber(os, 1);
64 writeVarNumber(os, 252);
65 writeVarNumber(os, 253);
66 writeVarNumber(os, 65536);
67 writeVarNumber(os, 4294967296LL);
68
69 std::string buffer = os.str();
70 const uint8_t* actual = reinterpret_cast<const uint8_t*>(buffer.c_str());
71
72 BOOST_CHECK_EQUAL(buffer.size(), sizeof(BUFFER));
73 BOOST_CHECK_EQUAL_COLLECTIONS(BUFFER, BUFFER + sizeof(BUFFER),
74 actual, actual + sizeof(BUFFER));
75}
76
77BOOST_AUTO_TEST_CASE(ReadFromBuffer)
78{
79 const uint8_t* begin;
80 uint64_t value;
81
82 begin = BUFFER;
83 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 1, value), true);
84 begin = BUFFER;
85 BOOST_CHECK_NO_THROW(readVarNumber(begin, begin + 1));
86 BOOST_CHECK_EQUAL(value, 1);
87
88 begin = BUFFER + 1;
89 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 1, value), true);
90 begin = BUFFER + 1;
91 BOOST_CHECK_NO_THROW(readVarNumber(begin, begin + 1));
92 BOOST_CHECK_EQUAL(value, 252);
93
94 begin = BUFFER + 2;
95 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 1, value), false);
96 begin = BUFFER + 2;
97 BOOST_CHECK_THROW(readVarNumber(begin, begin + 1), Error);
98
99 begin = BUFFER + 2;
100 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 2, value), false);
101 begin = BUFFER + 2;
102 BOOST_CHECK_THROW(readVarNumber(begin, begin + 2), Error);
103
104 begin = BUFFER + 2;
105 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 3, value), true);
106 begin = BUFFER + 2;
107 BOOST_CHECK_NO_THROW(readVarNumber(begin, begin + 3));
108 BOOST_CHECK_EQUAL(value, 253);
109
110
111 begin = BUFFER + 5;
112 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 1, value), false);
113 begin = BUFFER + 5;
114 BOOST_CHECK_THROW(readVarNumber(begin, begin + 1), Error);
115
116 begin = BUFFER + 5;
117 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 4, value), false);
118 begin = BUFFER + 5;
119 BOOST_CHECK_THROW(readVarNumber(begin, begin + 4), Error);
120
121 begin = BUFFER + 5;
122 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 5, value), true);
123 begin = BUFFER + 5;
124 BOOST_CHECK_NO_THROW(readVarNumber(begin, begin + 5));
125 BOOST_CHECK_EQUAL(value, 65536);
126
127 begin = BUFFER + 10;
128 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 1, value), false);
129 begin = BUFFER + 10;
130 BOOST_CHECK_THROW(readVarNumber(begin, begin + 1), Error);
131
132 begin = BUFFER + 10;
133 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 8, value), false);
134 begin = BUFFER + 10;
135 BOOST_CHECK_THROW(readVarNumber(begin, begin + 8), Error);
136
137 begin = BUFFER + 10;
138 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 9, value), true);
139 begin = BUFFER + 10;
140 BOOST_CHECK_NO_THROW(readVarNumber(begin, begin + 9));
141 BOOST_CHECK_EQUAL(value, 4294967296LL);
142}
143
144BOOST_AUTO_TEST_CASE(ReadFromStream)
145{
146 typedef boost::iostreams::stream<boost::iostreams::array_source> ArrayStream;
147 typedef std::istream_iterator<uint8_t> Iterator;
148
149 Iterator end; // end of stream
150 uint64_t value;
151 {
152 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 1);
153 Iterator begin(stream);
154 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), true);
155 }
156 {
157 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 1);
158 Iterator begin(stream);
159 BOOST_CHECK_NO_THROW(readVarNumber(begin, end));
160 BOOST_CHECK_EQUAL(value, 1);
161 }
162
163 {
164 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 1, 1);
165 Iterator begin(stream);
166 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), true);
167 }
168 {
169 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 1, 1);
170 Iterator begin(stream);
171 BOOST_CHECK_NO_THROW(readVarNumber(begin, end));
172 BOOST_CHECK_EQUAL(value, 252);
173 }
174
175 {
176 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 2, 1);
177 Iterator begin(stream);
178 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), false);
179 }
180 {
181 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 2, 1);
182 Iterator begin(stream);
183 BOOST_CHECK_THROW(readVarNumber(begin, end), Error);
184 }
185
186 {
187 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 2, 2);
188 Iterator begin(stream);
189 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), false);
190 }
191 {
192 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 2, 2);
193 Iterator begin(stream);
194 BOOST_CHECK_THROW(readVarNumber(begin, end), Error);
195 }
196
197 {
198 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 2, 3);
199 Iterator begin(stream);
200 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), true);
201 }
202 {
203 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 2, 3);
204 Iterator begin(stream);
205 BOOST_CHECK_NO_THROW(readVarNumber(begin, end));
206 BOOST_CHECK_EQUAL(value, 253);
207 }
208
209 {
210 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 5, 1);
211 Iterator begin(stream);
212 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), false);
213 }
214 {
215 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 5, 1);
216 Iterator begin(stream);
217 BOOST_CHECK_THROW(readVarNumber(begin, end), Error);
218 }
219
220 {
221 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 5, 4);
222 Iterator begin(stream);
223 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), false);
224 }
225 {
226 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 5, 4);
227 Iterator begin(stream);
228 BOOST_CHECK_THROW(readVarNumber(begin, end), Error);
229 }
230
231 {
232 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 5, 5);
233 Iterator begin(stream);
234 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), true);
235 }
236 {
237 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 5, 5);
238 Iterator begin(stream);
239 BOOST_CHECK_NO_THROW(readVarNumber(begin, end));
240 BOOST_CHECK_EQUAL(value, 65536);
241 }
242
243 {
244 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 10, 1);
245 Iterator begin(stream);
246 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), false);
247 }
248 {
249 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 10, 1);
250 Iterator begin(stream);
251 BOOST_CHECK_THROW(readVarNumber(begin, end), Error);
252 }
253
254 {
255 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 10, 8);
256 Iterator begin(stream);
257 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), false);
258 }
259 {
260 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 10, 8);
261 Iterator begin(stream);
262 BOOST_CHECK_THROW(readVarNumber(begin, end), Error);
263 }
264
265 {
266 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 10, 9);
267 Iterator begin(stream);
268 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), true);
269 }
270 {
271 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 10, 9);
272 Iterator begin(stream);
273 BOOST_CHECK_NO_THROW(readVarNumber(begin, end));
274 BOOST_CHECK_EQUAL(value, 4294967296LL);
275 }
276}
277
278BOOST_AUTO_TEST_SUITE_END() // VarNumber
279
280BOOST_AUTO_TEST_SUITE(NonNegativeInteger)
281
282static const uint8_t BUFFER[] = {
283 0x01, // 1
284 0x01, 0x01, // 257
285 0x01, 0x01, 0x01, 0x01, // 16843009LL
286 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 // 72340172838076673LL
287};
288
289BOOST_AUTO_TEST_CASE(SizeOf)
290{
291 BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(1), 1);
292 BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(252), 1);
293 BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(253), 2);
294 BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(257), 2);
295 BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(65536), 4);
296 BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(16843009LL), 4);
297 BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(4294967296LL), 8);
298 BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(72340172838076673LL), 8);
299}
300
301BOOST_AUTO_TEST_CASE(Write)
302{
303 ostringstream os;
304
305 writeNonNegativeInteger(os, 1);
306 writeNonNegativeInteger(os, 257);
307 writeNonNegativeInteger(os, 16843009LL);
308 writeNonNegativeInteger(os, 72340172838076673LL);
309
310 std::string buffer = os.str();
311 const uint8_t* actual = reinterpret_cast<const uint8_t*>(buffer.c_str());
312
313 BOOST_CHECK_EQUAL(buffer.size(), sizeof(BUFFER));
314 BOOST_CHECK_EQUAL_COLLECTIONS(BUFFER, BUFFER + sizeof(BUFFER),
315 actual, actual + sizeof(BUFFER));
316}
317
318
319BOOST_AUTO_TEST_CASE(ReadFromBuffer)
320{
321 const uint8_t* begin;
322 uint64_t value;
323
324 begin = BUFFER;
325 BOOST_CHECK_THROW(value = readNonNegativeInteger(1, begin, begin + 0), Error);
326 BOOST_CHECK_NO_THROW(value = readNonNegativeInteger(1, begin, begin + 1));
327 BOOST_CHECK_EQUAL(value, 1);
328
329 begin = BUFFER + 1;
330 BOOST_CHECK_THROW(value = readNonNegativeInteger(2, begin, begin + 1), Error);
331 BOOST_CHECK_NO_THROW(value = readNonNegativeInteger(2, begin, begin + 2));
332 BOOST_CHECK_EQUAL(value, 257);
333
334 begin = BUFFER + 3;
335 BOOST_CHECK_THROW(value = readNonNegativeInteger(4, begin, begin + 3), Error);
336 BOOST_CHECK_NO_THROW(value = readNonNegativeInteger(4, begin, begin + 4));
337 BOOST_CHECK_EQUAL(value, 16843009LL);
338
339 begin = BUFFER + 7;
340 BOOST_CHECK_THROW(value = readNonNegativeInteger(8, begin, begin + 7), Error);
341 BOOST_CHECK_NO_THROW(value = readNonNegativeInteger(8, begin, begin + 8));
342 BOOST_CHECK_EQUAL(value, 72340172838076673LL);
343
344 begin = BUFFER;
345 BOOST_CHECK_THROW(value = readNonNegativeInteger(3, begin, begin + 3), Error);
346}
347
348BOOST_AUTO_TEST_CASE(ReadFromStream)
349{
350 typedef boost::iostreams::stream<boost::iostreams::array_source> ArrayStream;
351 typedef std::istream_iterator<uint8_t> Iterator;
352
353 Iterator end; // end of stream
354 uint64_t value;
355 {
356 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 0);
357 Iterator begin(stream);
358 BOOST_CHECK_THROW(value = readNonNegativeInteger(1, begin, end), Error);
359 }
360 {
361 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 1);
362 Iterator begin(stream);
363 BOOST_CHECK_NO_THROW(value = readNonNegativeInteger(1, begin, end));
364 BOOST_CHECK_EQUAL(value, 1);
365 }
366
367 {
368 ArrayStream stream(reinterpret_cast<const char*>(BUFFER + 1), 1);
369 Iterator begin(stream);
370 BOOST_CHECK_THROW(value = readNonNegativeInteger(2, begin, end), Error);
371 }
372 {
373 ArrayStream stream(reinterpret_cast<const char*>(BUFFER + 1), 2);
374 Iterator begin(stream);
375 BOOST_CHECK_NO_THROW(value = readNonNegativeInteger(2, begin, end));
376 BOOST_CHECK_EQUAL(value, 257);
377 }
378
379 {
380 ArrayStream stream(reinterpret_cast<const char*>(BUFFER + 3), 3);
381 Iterator begin(stream);
382 BOOST_CHECK_THROW(value = readNonNegativeInteger(4, begin, end), Error);
383 }
384 {
385 ArrayStream stream(reinterpret_cast<const char*>(BUFFER + 3), 4);
386 Iterator begin(stream);
387 BOOST_CHECK_NO_THROW(value = readNonNegativeInteger(4, begin, end));
388 BOOST_CHECK_EQUAL(value, 16843009LL);
389 }
390
391 {
392 ArrayStream stream(reinterpret_cast<const char*>(BUFFER + 7), 7);
393 Iterator begin(stream);
394 BOOST_CHECK_THROW(value = readNonNegativeInteger(8, begin, end), Error);
395 }
396 {
397 ArrayStream stream(reinterpret_cast<const char*>(BUFFER + 7), 8);
398 Iterator begin(stream);
399 BOOST_CHECK_NO_THROW(value = readNonNegativeInteger(8, begin, end));
400 BOOST_CHECK_EQUAL(value, 72340172838076673LL);
401 }
402
403 {
404 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 3);
405 Iterator begin(stream);
406 BOOST_CHECK_THROW(value = readNonNegativeInteger(3, begin, end), Error);
407 }
408}
409
410BOOST_AUTO_TEST_SUITE_END() // NonNegativeInteger
411
412BOOST_AUTO_TEST_SUITE_END() // EncodingTlv
413
414} // namespace tests
415} // namespace tlv
416} // namespace ndn