Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | 233750e | 2014-02-16 00:50:07 -0800 | [diff] [blame] | 2 | /** |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2014 Regents of the University of California. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 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. |
Alexander Afanasyev | 233750e | 2014-02-16 00:50:07 -0800 | [diff] [blame] | 20 | */ |
| 21 | |
Alexander Afanasyev | 8b1674a | 2014-05-15 00:58:43 -0700 | [diff] [blame] | 22 | |
Alexander Afanasyev | 233750e | 2014-02-16 00:50:07 -0800 | [diff] [blame] | 23 | #include "encoding/encoding-buffer.hpp" |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 24 | #include "encoding/buffer-stream.hpp" |
Alexander Afanasyev | 233750e | 2014-02-16 00:50:07 -0800 | [diff] [blame] | 25 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 26 | #include "boost-test.hpp" |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 27 | |
Alexander Afanasyev | 233750e | 2014-02-16 00:50:07 -0800 | [diff] [blame] | 28 | using namespace std; |
| 29 | namespace ndn { |
| 30 | |
| 31 | BOOST_AUTO_TEST_SUITE(TestBlock) |
| 32 | |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 33 | BOOST_AUTO_TEST_CASE(TlvFromBuffer) |
| 34 | { |
| 35 | static const uint8_t BUFFER[] = { |
| 36 | 0x01, // == 1 |
| 37 | 0xfc, // == 252 |
| 38 | 0xfd, 0x00, 0xfd, // == 253 |
| 39 | 0xfe, 0x00, 0x01, 0x00, 0x00, // == 65536 |
| 40 | 0xff, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00 // == 4294967296LL |
| 41 | }; |
| 42 | |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 43 | using namespace tlv; |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 44 | |
| 45 | const uint8_t* begin; |
| 46 | uint64_t value; |
| 47 | |
| 48 | begin = BUFFER; |
| 49 | BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 1, value), true); |
| 50 | begin = BUFFER; |
| 51 | BOOST_CHECK_NO_THROW(readVarNumber(begin, begin + 1)); |
| 52 | BOOST_CHECK_EQUAL(value, 1); |
| 53 | |
| 54 | begin = BUFFER + 1; |
| 55 | BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 1, value), true); |
| 56 | begin = BUFFER + 1; |
| 57 | BOOST_CHECK_NO_THROW(readVarNumber(begin, begin + 1)); |
| 58 | BOOST_CHECK_EQUAL(value, 252); |
| 59 | |
| 60 | begin = BUFFER + 2; |
| 61 | BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 1, value), false); |
| 62 | begin = BUFFER + 2; |
| 63 | BOOST_CHECK_THROW(readVarNumber(begin, begin + 1), Error); |
| 64 | |
| 65 | begin = BUFFER + 2; |
| 66 | BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 2, value), false); |
| 67 | begin = BUFFER + 2; |
| 68 | BOOST_CHECK_THROW(readVarNumber(begin, begin + 2), Error); |
| 69 | |
| 70 | begin = BUFFER + 2; |
| 71 | BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 3, value), true); |
| 72 | begin = BUFFER + 2; |
| 73 | BOOST_CHECK_NO_THROW(readVarNumber(begin, begin + 3)); |
| 74 | BOOST_CHECK_EQUAL(value, 253); |
| 75 | |
| 76 | |
| 77 | begin = BUFFER + 5; |
| 78 | BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 1, value), false); |
| 79 | begin = BUFFER + 5; |
| 80 | BOOST_CHECK_THROW(readVarNumber(begin, begin + 1), Error); |
| 81 | |
| 82 | begin = BUFFER + 5; |
| 83 | BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 4, value), false); |
| 84 | begin = BUFFER + 5; |
| 85 | BOOST_CHECK_THROW(readVarNumber(begin, begin + 4), Error); |
| 86 | |
| 87 | begin = BUFFER + 5; |
| 88 | BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 5, value), true); |
| 89 | begin = BUFFER + 5; |
| 90 | BOOST_CHECK_NO_THROW(readVarNumber(begin, begin + 5)); |
| 91 | BOOST_CHECK_EQUAL(value, 65536); |
| 92 | |
| 93 | begin = BUFFER + 10; |
| 94 | BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 1, value), false); |
| 95 | begin = BUFFER + 10; |
| 96 | BOOST_CHECK_THROW(readVarNumber(begin, begin + 1), Error); |
| 97 | |
| 98 | begin = BUFFER + 10; |
| 99 | BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 8, value), false); |
| 100 | begin = BUFFER + 10; |
| 101 | BOOST_CHECK_THROW(readVarNumber(begin, begin + 8), Error); |
| 102 | |
| 103 | begin = BUFFER + 10; |
| 104 | BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 9, value), true); |
| 105 | begin = BUFFER + 10; |
| 106 | BOOST_CHECK_NO_THROW(readVarNumber(begin, begin + 9)); |
| 107 | BOOST_CHECK_EQUAL(value, 4294967296LL); |
| 108 | } |
| 109 | |
| 110 | BOOST_AUTO_TEST_CASE(TlvFromStream) |
| 111 | { |
| 112 | static const uint8_t BUFFER[] = { |
| 113 | 0x01, // == 1 |
| 114 | 0xfc, // == 252 |
| 115 | 0xfd, 0x00, 0xfd, // == 253 |
| 116 | 0xfe, 0x00, 0x01, 0x00, 0x00, // == 65536 |
| 117 | 0xff, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00 // == 4294967296LL |
| 118 | }; |
| 119 | |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 120 | using namespace tlv; |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 121 | |
| 122 | typedef boost::iostreams::stream<boost::iostreams::array_source> ArrayStream; |
| 123 | typedef std::istream_iterator<uint8_t> Iterator; |
| 124 | |
| 125 | Iterator end; // end of stream |
| 126 | uint64_t value; |
| 127 | { |
| 128 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 1); |
| 129 | Iterator begin(stream); |
| 130 | BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), true); |
| 131 | } |
| 132 | { |
| 133 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 1); |
| 134 | Iterator begin(stream); |
| 135 | BOOST_CHECK_NO_THROW(readVarNumber(begin, end)); |
| 136 | BOOST_CHECK_EQUAL(value, 1); |
| 137 | } |
| 138 | |
| 139 | { |
| 140 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 1, 1); |
| 141 | Iterator begin(stream); |
| 142 | BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), true); |
| 143 | } |
| 144 | { |
| 145 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 1, 1); |
| 146 | Iterator begin(stream); |
| 147 | BOOST_CHECK_NO_THROW(readVarNumber(begin, end)); |
| 148 | BOOST_CHECK_EQUAL(value, 252); |
| 149 | } |
| 150 | |
| 151 | { |
| 152 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 2, 1); |
| 153 | Iterator begin(stream); |
| 154 | BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), false); |
| 155 | } |
| 156 | { |
| 157 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 2, 1); |
| 158 | Iterator begin(stream); |
| 159 | BOOST_CHECK_THROW(readVarNumber(begin, end), Error); |
| 160 | } |
| 161 | |
| 162 | { |
| 163 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 2, 2); |
| 164 | Iterator begin(stream); |
| 165 | BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), false); |
| 166 | } |
| 167 | { |
| 168 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 2, 2); |
| 169 | Iterator begin(stream); |
| 170 | BOOST_CHECK_THROW(readVarNumber(begin, end), Error); |
| 171 | } |
| 172 | |
| 173 | { |
| 174 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 2, 3); |
| 175 | Iterator begin(stream); |
| 176 | BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), true); |
| 177 | } |
| 178 | { |
| 179 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 2, 3); |
| 180 | Iterator begin(stream); |
| 181 | BOOST_CHECK_NO_THROW(readVarNumber(begin, end)); |
| 182 | BOOST_CHECK_EQUAL(value, 253); |
| 183 | } |
| 184 | |
| 185 | { |
| 186 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 5, 1); |
| 187 | Iterator begin(stream); |
| 188 | BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), false); |
| 189 | } |
| 190 | { |
| 191 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 5, 1); |
| 192 | Iterator begin(stream); |
| 193 | BOOST_CHECK_THROW(readVarNumber(begin, end), Error); |
| 194 | } |
| 195 | |
| 196 | { |
| 197 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 5, 4); |
| 198 | Iterator begin(stream); |
| 199 | BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), false); |
| 200 | } |
| 201 | { |
| 202 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 5, 4); |
| 203 | Iterator begin(stream); |
| 204 | BOOST_CHECK_THROW(readVarNumber(begin, end), Error); |
| 205 | } |
| 206 | |
| 207 | { |
| 208 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 5, 5); |
| 209 | Iterator begin(stream); |
| 210 | BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), true); |
| 211 | } |
| 212 | { |
| 213 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 5, 5); |
| 214 | Iterator begin(stream); |
| 215 | BOOST_CHECK_NO_THROW(readVarNumber(begin, end)); |
| 216 | BOOST_CHECK_EQUAL(value, 65536); |
| 217 | } |
| 218 | |
| 219 | { |
| 220 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 10, 1); |
| 221 | Iterator begin(stream); |
| 222 | BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), false); |
| 223 | } |
| 224 | { |
| 225 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 10, 1); |
| 226 | Iterator begin(stream); |
| 227 | BOOST_CHECK_THROW(readVarNumber(begin, end), Error); |
| 228 | } |
| 229 | |
| 230 | { |
| 231 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 10, 8); |
| 232 | Iterator begin(stream); |
| 233 | BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), false); |
| 234 | } |
| 235 | { |
| 236 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 10, 8); |
| 237 | Iterator begin(stream); |
| 238 | BOOST_CHECK_THROW(readVarNumber(begin, end), Error); |
| 239 | } |
| 240 | |
| 241 | { |
| 242 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 10, 9); |
| 243 | Iterator begin(stream); |
| 244 | BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), true); |
| 245 | } |
| 246 | { |
| 247 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 10, 9); |
| 248 | Iterator begin(stream); |
| 249 | BOOST_CHECK_NO_THROW(readVarNumber(begin, end)); |
| 250 | BOOST_CHECK_EQUAL(value, 4294967296LL); |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | BOOST_AUTO_TEST_CASE(NonNegativeIntegerFromBuffer) |
| 255 | { |
| 256 | static const uint8_t BUFFER[] = { |
| 257 | 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 |
| 258 | }; |
| 259 | |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 260 | using namespace tlv; |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 261 | |
| 262 | const uint8_t* begin; |
| 263 | uint64_t value; |
| 264 | |
| 265 | begin = BUFFER; |
| 266 | BOOST_CHECK_THROW(value = readNonNegativeInteger(1, begin, begin + 0), Error); |
| 267 | BOOST_CHECK_NO_THROW(value = readNonNegativeInteger(1, begin, begin + 1)); |
| 268 | BOOST_CHECK_EQUAL(value, 1); |
| 269 | |
| 270 | begin = BUFFER; |
| 271 | BOOST_CHECK_THROW(value = readNonNegativeInteger(2, begin, begin + 1), Error); |
| 272 | BOOST_CHECK_NO_THROW(value = readNonNegativeInteger(2, begin, begin + 2)); |
| 273 | BOOST_CHECK_EQUAL(value, 257); |
| 274 | |
| 275 | begin = BUFFER; |
| 276 | BOOST_CHECK_THROW(value = readNonNegativeInteger(4, begin, begin + 3), Error); |
| 277 | BOOST_CHECK_NO_THROW(value = readNonNegativeInteger(4, begin, begin + 4)); |
| 278 | BOOST_CHECK_EQUAL(value, 16843009LL); |
| 279 | |
| 280 | begin = BUFFER; |
| 281 | BOOST_CHECK_THROW(value = readNonNegativeInteger(8, begin, begin + 7), Error); |
| 282 | BOOST_CHECK_NO_THROW(value = readNonNegativeInteger(8, begin, begin + 8)); |
| 283 | BOOST_CHECK_EQUAL(value, 72340172838076673LL); |
| 284 | |
| 285 | begin = BUFFER; |
| 286 | BOOST_CHECK_THROW(value = readNonNegativeInteger(3, begin, begin + 3), Error); |
| 287 | } |
| 288 | |
| 289 | BOOST_AUTO_TEST_CASE(NonNegativeIntegerFromStream) |
| 290 | { |
| 291 | static const uint8_t BUFFER[] = { |
| 292 | 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 |
| 293 | }; |
| 294 | |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 295 | using namespace tlv; |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 296 | |
| 297 | typedef boost::iostreams::stream<boost::iostreams::array_source> ArrayStream; |
| 298 | typedef std::istream_iterator<uint8_t> Iterator; |
| 299 | |
| 300 | Iterator end; // end of stream |
| 301 | uint64_t value; |
| 302 | { |
| 303 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 0); |
| 304 | Iterator begin(stream); |
| 305 | BOOST_CHECK_THROW(value = readNonNegativeInteger(1, begin, end), Error); |
| 306 | } |
| 307 | { |
| 308 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 1); |
| 309 | Iterator begin(stream); |
| 310 | BOOST_CHECK_NO_THROW(value = readNonNegativeInteger(1, begin, end)); |
| 311 | BOOST_CHECK_EQUAL(value, 1); |
| 312 | } |
| 313 | |
| 314 | { |
| 315 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 1); |
| 316 | Iterator begin(stream); |
| 317 | BOOST_CHECK_THROW(value = readNonNegativeInteger(2, begin, end), Error); |
| 318 | } |
| 319 | { |
| 320 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 2); |
| 321 | Iterator begin(stream); |
| 322 | BOOST_CHECK_NO_THROW(value = readNonNegativeInteger(2, begin, end)); |
| 323 | BOOST_CHECK_EQUAL(value, 257); |
| 324 | } |
| 325 | |
| 326 | { |
| 327 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 3); |
| 328 | Iterator begin(stream); |
| 329 | BOOST_CHECK_THROW(value = readNonNegativeInteger(4, begin, end), Error); |
| 330 | } |
| 331 | { |
| 332 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 4); |
| 333 | Iterator begin(stream); |
| 334 | BOOST_CHECK_NO_THROW(value = readNonNegativeInteger(4, begin, end)); |
| 335 | BOOST_CHECK_EQUAL(value, 16843009LL); |
| 336 | } |
| 337 | |
| 338 | { |
| 339 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 7); |
| 340 | Iterator begin(stream); |
| 341 | BOOST_CHECK_THROW(value = readNonNegativeInteger(8, begin, end), Error); |
| 342 | } |
| 343 | { |
| 344 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 8); |
| 345 | Iterator begin(stream); |
| 346 | BOOST_CHECK_NO_THROW(value = readNonNegativeInteger(8, begin, end)); |
| 347 | BOOST_CHECK_EQUAL(value, 72340172838076673LL); |
| 348 | } |
| 349 | |
| 350 | { |
| 351 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 3); |
| 352 | Iterator begin(stream); |
| 353 | BOOST_CHECK_THROW(value = readNonNegativeInteger(3, begin, end), Error); |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | BOOST_AUTO_TEST_CASE(BlockBasic) |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 358 | { |
| 359 | EncodingBuffer buffer; |
| 360 | EncodingEstimator estimator; |
| 361 | size_t s1, s2; |
| 362 | |
| 363 | // VarNumber checks |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 364 | |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 365 | s1 = buffer.prependVarNumber(252); |
| 366 | s2 = estimator.prependVarNumber(252); |
| 367 | BOOST_CHECK_EQUAL(buffer.size(), 1); |
| 368 | BOOST_CHECK_EQUAL(s1, 1); |
| 369 | BOOST_CHECK_EQUAL(s2, 1); |
| 370 | buffer = EncodingBuffer(); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 371 | |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 372 | s1 = buffer.prependVarNumber(253); |
| 373 | s2 = estimator.prependVarNumber(253); |
| 374 | BOOST_CHECK_EQUAL(buffer.size(), 3); |
| 375 | BOOST_CHECK_EQUAL(s1, 3); |
| 376 | BOOST_CHECK_EQUAL(s2, 3); |
| 377 | buffer = EncodingBuffer(); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 378 | |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 379 | s1 = buffer.prependVarNumber(255); |
| 380 | s2 = estimator.prependVarNumber(255); |
| 381 | BOOST_CHECK_EQUAL(buffer.size(), 3); |
| 382 | BOOST_CHECK_EQUAL(s1, 3); |
| 383 | BOOST_CHECK_EQUAL(s2, 3); |
| 384 | buffer = EncodingBuffer(); |
| 385 | |
| 386 | s1 = buffer.prependVarNumber(65535); |
| 387 | s2 = estimator.prependVarNumber(65535); |
| 388 | BOOST_CHECK_EQUAL(buffer.size(), 3); |
| 389 | BOOST_CHECK_EQUAL(s1, 3); |
| 390 | BOOST_CHECK_EQUAL(s2, 3); |
| 391 | buffer = EncodingBuffer(); |
| 392 | |
| 393 | s1 = buffer.prependVarNumber(65536); |
| 394 | s2 = estimator.prependVarNumber(65536); |
| 395 | BOOST_CHECK_EQUAL(buffer.size(), 5); |
| 396 | BOOST_CHECK_EQUAL(s1, 5); |
| 397 | BOOST_CHECK_EQUAL(s2, 5); |
| 398 | buffer = EncodingBuffer(); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 399 | |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 400 | s1 = buffer.prependVarNumber(4294967295LL); |
| 401 | s2 = estimator.prependVarNumber(4294967295LL); |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 402 | BOOST_CHECK_EQUAL(buffer.size(), 5); |
| 403 | BOOST_CHECK_EQUAL(s1, 5); |
| 404 | BOOST_CHECK_EQUAL(s2, 5); |
| 405 | buffer = EncodingBuffer(); |
| 406 | |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 407 | s1 = buffer.prependVarNumber(4294967296LL); |
| 408 | s2 = estimator.prependVarNumber(4294967296LL); |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 409 | BOOST_CHECK_EQUAL(buffer.size(), 9); |
| 410 | BOOST_CHECK_EQUAL(s1, 9); |
| 411 | BOOST_CHECK_EQUAL(s2, 9); |
| 412 | buffer = EncodingBuffer(); |
| 413 | |
| 414 | // nonNegativeInteger checks |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 415 | |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 416 | s1 = buffer.prependNonNegativeInteger(252); |
| 417 | s2 = estimator.prependNonNegativeInteger(252); |
| 418 | BOOST_CHECK_EQUAL(buffer.size(), 1); |
| 419 | BOOST_CHECK_EQUAL(s1, 1); |
| 420 | BOOST_CHECK_EQUAL(s2, 1); |
| 421 | buffer = EncodingBuffer(); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 422 | |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 423 | s1 = buffer.prependNonNegativeInteger(255); |
| 424 | s2 = estimator.prependNonNegativeInteger(255); |
| 425 | BOOST_CHECK_EQUAL(buffer.size(), 1); |
| 426 | BOOST_CHECK_EQUAL(s1, 1); |
| 427 | BOOST_CHECK_EQUAL(s2, 1); |
| 428 | buffer = EncodingBuffer(); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 429 | |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 430 | s1 = buffer.prependNonNegativeInteger(256); |
| 431 | s2 = estimator.prependNonNegativeInteger(256); |
| 432 | BOOST_CHECK_EQUAL(buffer.size(), 2); |
| 433 | BOOST_CHECK_EQUAL(s1, 2); |
| 434 | BOOST_CHECK_EQUAL(s2, 2); |
| 435 | buffer = EncodingBuffer(); |
| 436 | |
| 437 | s1 = buffer.prependNonNegativeInteger(65535); |
| 438 | s2 = estimator.prependNonNegativeInteger(65535); |
| 439 | BOOST_CHECK_EQUAL(buffer.size(), 2); |
| 440 | BOOST_CHECK_EQUAL(s1, 2); |
| 441 | BOOST_CHECK_EQUAL(s2, 2); |
| 442 | buffer = EncodingBuffer(); |
| 443 | |
| 444 | s1 = buffer.prependNonNegativeInteger(65536); |
| 445 | s2 = estimator.prependNonNegativeInteger(65536); |
| 446 | BOOST_CHECK_EQUAL(buffer.size(), 4); |
| 447 | BOOST_CHECK_EQUAL(s1, 4); |
| 448 | BOOST_CHECK_EQUAL(s2, 4); |
| 449 | buffer = EncodingBuffer(); |
| 450 | |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 451 | s1 = buffer.prependNonNegativeInteger(4294967295LL); |
| 452 | s2 = estimator.prependNonNegativeInteger(4294967295LL); |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 453 | BOOST_CHECK_EQUAL(buffer.size(), 4); |
| 454 | BOOST_CHECK_EQUAL(s1, 4); |
| 455 | BOOST_CHECK_EQUAL(s2, 4); |
| 456 | buffer = EncodingBuffer(); |
| 457 | |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 458 | s1 = buffer.prependNonNegativeInteger(4294967296LL); |
| 459 | s2 = estimator.prependNonNegativeInteger(4294967296LL); |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 460 | BOOST_CHECK_EQUAL(buffer.size(), 8); |
| 461 | BOOST_CHECK_EQUAL(s1, 8); |
| 462 | BOOST_CHECK_EQUAL(s2, 8); |
| 463 | buffer = EncodingBuffer(); |
| 464 | } |
| 465 | |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 466 | BOOST_AUTO_TEST_CASE(EncodingBufferToBlock) |
Alexander Afanasyev | 233750e | 2014-02-16 00:50:07 -0800 | [diff] [blame] | 467 | { |
| 468 | uint8_t value[4]; |
| 469 | |
Alexander Afanasyev | 1515131 | 2014-02-16 00:53:51 -0800 | [diff] [blame] | 470 | EncodingBuffer buffer; |
Alexander Afanasyev | 233750e | 2014-02-16 00:50:07 -0800 | [diff] [blame] | 471 | size_t length = buffer.prependByteArray(value, sizeof(value)); |
| 472 | buffer.prependVarNumber(length); |
| 473 | buffer.prependVarNumber(0xe0); |
| 474 | |
| 475 | Block block; |
| 476 | BOOST_REQUIRE_NO_THROW(block = buffer.block()); |
| 477 | BOOST_CHECK_EQUAL(block.type(), 0xe0); |
| 478 | BOOST_CHECK_EQUAL(block.value_size(), sizeof(value)); |
Alexander Afanasyev | 1515131 | 2014-02-16 00:53:51 -0800 | [diff] [blame] | 479 | |
| 480 | BOOST_REQUIRE_NO_THROW(block = Block(buffer)); |
| 481 | BOOST_CHECK_EQUAL(block.type(), 0xe0); |
| 482 | BOOST_CHECK_EQUAL(block.value_size(), sizeof(value)); |
| 483 | } |
| 484 | |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 485 | BOOST_AUTO_TEST_CASE(BlockToBuffer) |
Alexander Afanasyev | 1515131 | 2014-02-16 00:53:51 -0800 | [diff] [blame] | 486 | { |
| 487 | shared_ptr<Buffer> buf = make_shared<Buffer>(10); |
| 488 | for (int i = 0; i < 10; i++) (*buf)[i] = i; |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 489 | |
Alexander Afanasyev | 1515131 | 2014-02-16 00:53:51 -0800 | [diff] [blame] | 490 | Block block(0xab, buf); |
| 491 | block.encode(); |
| 492 | |
| 493 | EncodingBuffer buffer(0,0); |
| 494 | BOOST_REQUIRE_NO_THROW(buffer = EncodingBuffer(block)); |
| 495 | BOOST_CHECK_EQUAL(buffer.size(), 12); |
| 496 | BOOST_CHECK_EQUAL(buffer.capacity(), 12); |
| 497 | |
| 498 | (*buf)[1] = 0xe0; |
| 499 | (*buf)[2] = 2; |
| 500 | BOOST_REQUIRE_NO_THROW(block = Block(buf, buf->begin() + 1, buf->begin() + 5)); |
| 501 | BOOST_CHECK_EQUAL(block.type(), 0xe0); |
| 502 | |
| 503 | BOOST_REQUIRE_NO_THROW(buffer = EncodingBuffer(block)); |
| 504 | BOOST_CHECK_EQUAL(buffer.size(), 4); |
| 505 | BOOST_CHECK_EQUAL(buffer.capacity(), 10); |
Alexander Afanasyev | 233750e | 2014-02-16 00:50:07 -0800 | [diff] [blame] | 506 | } |
| 507 | |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 508 | BOOST_AUTO_TEST_CASE(FromBuffer) |
| 509 | { |
| 510 | const uint8_t TEST_BUFFER[] = {0x00, 0x01, 0xfa, // ok |
| 511 | 0x01, 0x01, 0xfb, // ok |
| 512 | 0x03, 0x02, 0xff}; // not ok |
| 513 | BufferPtr buffer(new Buffer(TEST_BUFFER, sizeof(TEST_BUFFER))); |
| 514 | |
| 515 | // using BufferPtr (avoids memory copy) |
| 516 | size_t offset = 0; |
| 517 | Block testBlock; |
| 518 | BOOST_CHECK(Block::fromBuffer(buffer, offset, testBlock)); |
| 519 | BOOST_CHECK_EQUAL(testBlock.type(), 0); |
| 520 | BOOST_CHECK_EQUAL(testBlock.size(), 3); |
| 521 | BOOST_CHECK_EQUAL(testBlock.value_size(), 1); |
| 522 | BOOST_CHECK_EQUAL(*testBlock.wire(), 0x00); |
| 523 | BOOST_CHECK_EQUAL(*testBlock.value(), 0xfa); |
| 524 | offset += testBlock.size(); |
| 525 | |
| 526 | BOOST_CHECK(Block::fromBuffer(buffer, offset, testBlock)); |
| 527 | BOOST_CHECK_EQUAL(testBlock.type(), 1); |
| 528 | BOOST_CHECK_EQUAL(testBlock.size(), 3); |
| 529 | BOOST_CHECK_EQUAL(testBlock.value_size(), 1); |
| 530 | BOOST_CHECK_EQUAL(*testBlock.wire(), 0x01); |
| 531 | BOOST_CHECK_EQUAL(*testBlock.value(), 0xfb); |
| 532 | offset += testBlock.size(); |
| 533 | |
| 534 | BOOST_CHECK(!Block::fromBuffer(buffer, offset, testBlock)); |
| 535 | |
| 536 | // just buffer, copies memory |
| 537 | offset = 0; |
| 538 | BOOST_CHECK(Block::fromBuffer(TEST_BUFFER + offset, sizeof(TEST_BUFFER) - offset, testBlock)); |
| 539 | BOOST_CHECK_EQUAL(testBlock.type(), 0); |
| 540 | BOOST_CHECK_EQUAL(testBlock.size(), 3); |
| 541 | BOOST_CHECK_EQUAL(testBlock.value_size(), 1); |
| 542 | BOOST_CHECK_EQUAL(*testBlock.wire(), 0x00); |
| 543 | BOOST_CHECK_EQUAL(*testBlock.value(), 0xfa); |
| 544 | offset += testBlock.size(); |
| 545 | |
| 546 | BOOST_CHECK(Block::fromBuffer(TEST_BUFFER + offset, sizeof(TEST_BUFFER) - offset, testBlock)); |
| 547 | BOOST_CHECK_EQUAL(testBlock.type(), 1); |
| 548 | BOOST_CHECK_EQUAL(testBlock.size(), 3); |
| 549 | BOOST_CHECK_EQUAL(testBlock.value_size(), 1); |
| 550 | BOOST_CHECK_EQUAL(*testBlock.wire(), 0x01); |
| 551 | BOOST_CHECK_EQUAL(*testBlock.value(), 0xfb); |
| 552 | offset += testBlock.size(); |
| 553 | |
| 554 | BOOST_CHECK(!Block::fromBuffer(TEST_BUFFER + offset, sizeof(TEST_BUFFER) - offset, testBlock)); |
| 555 | } |
| 556 | |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 557 | BOOST_AUTO_TEST_CASE(BlockFromStream) |
| 558 | { |
| 559 | const uint8_t TEST_BUFFER[] = {0x00, 0x01, 0xfa, // ok |
| 560 | 0x01, 0x01, 0xfb, // ok |
| 561 | 0x03, 0x02, 0xff}; // not ok |
| 562 | |
| 563 | typedef boost::iostreams::stream<boost::iostreams::array_source> ArrayStream; |
| 564 | ArrayStream stream(reinterpret_cast<const char*>(TEST_BUFFER), sizeof(TEST_BUFFER)); |
| 565 | |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 566 | |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 567 | Block testBlock; |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 568 | BOOST_REQUIRE_NO_THROW(testBlock = Block::fromStream(stream)); |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 569 | BOOST_CHECK_EQUAL(testBlock.type(), 0); |
| 570 | BOOST_CHECK_EQUAL(testBlock.size(), 3); |
| 571 | BOOST_CHECK_EQUAL(testBlock.value_size(), 1); |
| 572 | BOOST_CHECK_EQUAL(*testBlock.wire(), 0x00); |
| 573 | BOOST_CHECK_EQUAL(*testBlock.value(), 0xfa); |
| 574 | |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 575 | BOOST_REQUIRE_NO_THROW(testBlock = Block::fromStream(stream)); |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 576 | BOOST_CHECK_EQUAL(testBlock.type(), 1); |
| 577 | BOOST_CHECK_EQUAL(testBlock.size(), 3); |
| 578 | BOOST_CHECK_EQUAL(testBlock.value_size(), 1); |
| 579 | BOOST_CHECK_EQUAL(*testBlock.wire(), 0x01); |
| 580 | BOOST_CHECK_EQUAL(*testBlock.value(), 0xfb); |
| 581 | |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 582 | BOOST_CHECK_THROW(Block::fromStream(stream), tlv::Error); |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 583 | } |
| 584 | |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 585 | BOOST_AUTO_TEST_CASE(Equality) |
| 586 | { |
| 587 | BOOST_CONCEPT_ASSERT((boost::EqualityComparable<Block>)); |
| 588 | |
| 589 | Block a("\x08\x00", 2); |
| 590 | Block b("\x08\x00", 2);; |
| 591 | BOOST_CHECK_EQUAL(a == b, true); |
| 592 | BOOST_CHECK_EQUAL(a != b, false); |
| 593 | |
| 594 | Block c("\x06\x00", 2); |
| 595 | Block d("\x08\x00", 2);; |
| 596 | BOOST_CHECK_EQUAL(c == d, false); |
| 597 | BOOST_CHECK_EQUAL(c != d, true); |
| 598 | |
| 599 | Block e("\x06\x00", 2); |
| 600 | Block f("\x06\x01\xcc", 3);; |
| 601 | BOOST_CHECK_EQUAL(e == f, false); |
| 602 | BOOST_CHECK_EQUAL(e != f, true); |
| 603 | } |
| 604 | |
Alexander Afanasyev | 233750e | 2014-02-16 00:50:07 -0800 | [diff] [blame] | 605 | BOOST_AUTO_TEST_SUITE_END() |
| 606 | |
| 607 | } // namespace ndn |