Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 2 | /* |
Alexander Afanasyev | 5f1820e | 2017-01-04 18:12:42 -0800 | [diff] [blame] | 3 | * Copyright (c) 2013-2017 Regents of the University of California. |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 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" |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 25 | |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 26 | #include <boost/iostreams/stream.hpp> |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 27 | #include <boost/iostreams/device/array.hpp> |
Alexander Afanasyev | 5f1820e | 2017-01-04 18:12:42 -0800 | [diff] [blame] | 28 | #include <boost/lexical_cast.hpp> |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 29 | |
| 30 | namespace ndn { |
| 31 | namespace tlv { |
| 32 | namespace tests { |
| 33 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 34 | BOOST_AUTO_TEST_SUITE(Encoding) |
| 35 | BOOST_AUTO_TEST_SUITE(TestTlv) |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 36 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 37 | using ArrayStream = boost::iostreams::stream<boost::iostreams::array_source>; |
| 38 | using Iterator = std::istream_iterator<uint8_t>; |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 39 | |
| 40 | BOOST_AUTO_TEST_SUITE(VarNumber) |
| 41 | |
| 42 | static 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 | |
| 50 | BOOST_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 | |
| 59 | BOOST_AUTO_TEST_CASE(Write) |
| 60 | { |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 61 | std::ostringstream os; |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 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 | |
| 77 | BOOST_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 | |
| 144 | BOOST_AUTO_TEST_CASE(ReadFromStream) |
| 145 | { |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 146 | Iterator end; // end of stream |
| 147 | uint64_t value; |
| 148 | { |
| 149 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 1); |
| 150 | Iterator begin(stream); |
| 151 | BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), true); |
| 152 | } |
| 153 | { |
| 154 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 1); |
| 155 | Iterator begin(stream); |
| 156 | BOOST_CHECK_NO_THROW(readVarNumber(begin, end)); |
| 157 | BOOST_CHECK_EQUAL(value, 1); |
| 158 | } |
| 159 | |
| 160 | { |
| 161 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 1, 1); |
| 162 | Iterator begin(stream); |
| 163 | BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), true); |
| 164 | } |
| 165 | { |
| 166 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 1, 1); |
| 167 | Iterator begin(stream); |
| 168 | BOOST_CHECK_NO_THROW(readVarNumber(begin, end)); |
| 169 | BOOST_CHECK_EQUAL(value, 252); |
| 170 | } |
| 171 | |
| 172 | { |
| 173 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 2, 1); |
| 174 | Iterator begin(stream); |
| 175 | BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), false); |
| 176 | } |
| 177 | { |
| 178 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 2, 1); |
| 179 | Iterator begin(stream); |
| 180 | BOOST_CHECK_THROW(readVarNumber(begin, end), Error); |
| 181 | } |
| 182 | |
| 183 | { |
| 184 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 2, 2); |
| 185 | Iterator begin(stream); |
| 186 | BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), false); |
| 187 | } |
| 188 | { |
| 189 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 2, 2); |
| 190 | Iterator begin(stream); |
| 191 | BOOST_CHECK_THROW(readVarNumber(begin, end), Error); |
| 192 | } |
| 193 | |
| 194 | { |
| 195 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 2, 3); |
| 196 | Iterator begin(stream); |
| 197 | BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), true); |
| 198 | } |
| 199 | { |
| 200 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 2, 3); |
| 201 | Iterator begin(stream); |
| 202 | BOOST_CHECK_NO_THROW(readVarNumber(begin, end)); |
| 203 | BOOST_CHECK_EQUAL(value, 253); |
| 204 | } |
| 205 | |
| 206 | { |
| 207 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 5, 1); |
| 208 | Iterator begin(stream); |
| 209 | BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), false); |
| 210 | } |
| 211 | { |
| 212 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 5, 1); |
| 213 | Iterator begin(stream); |
| 214 | BOOST_CHECK_THROW(readVarNumber(begin, end), Error); |
| 215 | } |
| 216 | |
| 217 | { |
| 218 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 5, 4); |
| 219 | Iterator begin(stream); |
| 220 | BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), false); |
| 221 | } |
| 222 | { |
| 223 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 5, 4); |
| 224 | Iterator begin(stream); |
| 225 | BOOST_CHECK_THROW(readVarNumber(begin, end), Error); |
| 226 | } |
| 227 | |
| 228 | { |
| 229 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 5, 5); |
| 230 | Iterator begin(stream); |
| 231 | BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), true); |
| 232 | } |
| 233 | { |
| 234 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 5, 5); |
| 235 | Iterator begin(stream); |
| 236 | BOOST_CHECK_NO_THROW(readVarNumber(begin, end)); |
| 237 | BOOST_CHECK_EQUAL(value, 65536); |
| 238 | } |
| 239 | |
| 240 | { |
| 241 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 10, 1); |
| 242 | Iterator begin(stream); |
| 243 | BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), false); |
| 244 | } |
| 245 | { |
| 246 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 10, 1); |
| 247 | Iterator begin(stream); |
| 248 | BOOST_CHECK_THROW(readVarNumber(begin, end), Error); |
| 249 | } |
| 250 | |
| 251 | { |
| 252 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 10, 8); |
| 253 | Iterator begin(stream); |
| 254 | BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), false); |
| 255 | } |
| 256 | { |
| 257 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 10, 8); |
| 258 | Iterator begin(stream); |
| 259 | BOOST_CHECK_THROW(readVarNumber(begin, end), Error); |
| 260 | } |
| 261 | |
| 262 | { |
| 263 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 10, 9); |
| 264 | Iterator begin(stream); |
| 265 | BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), true); |
| 266 | } |
| 267 | { |
| 268 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 10, 9); |
| 269 | Iterator begin(stream); |
| 270 | BOOST_CHECK_NO_THROW(readVarNumber(begin, end)); |
| 271 | BOOST_CHECK_EQUAL(value, 4294967296LL); |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | BOOST_AUTO_TEST_SUITE_END() // VarNumber |
| 276 | |
| 277 | BOOST_AUTO_TEST_SUITE(NonNegativeInteger) |
| 278 | |
| 279 | static const uint8_t BUFFER[] = { |
| 280 | 0x01, // 1 |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 281 | 0xff, // 255 |
| 282 | 0x01, 0x02, // 258 |
| 283 | 0x01, 0x01, 0x01, 0x02, // 16843010LL |
| 284 | 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02 // 72340172838076674LL |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 285 | }; |
| 286 | |
| 287 | BOOST_AUTO_TEST_CASE(SizeOf) |
| 288 | { |
| 289 | BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(1), 1); |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 290 | BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(253), 1); |
| 291 | BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(255), 1); |
| 292 | BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(256), 2); |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 293 | BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(65536), 4); |
| 294 | BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(16843009LL), 4); |
| 295 | BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(4294967296LL), 8); |
| 296 | BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(72340172838076673LL), 8); |
| 297 | } |
| 298 | |
| 299 | BOOST_AUTO_TEST_CASE(Write) |
| 300 | { |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 301 | std::ostringstream os; |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 302 | |
| 303 | writeNonNegativeInteger(os, 1); |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 304 | writeNonNegativeInteger(os, 255); |
| 305 | writeNonNegativeInteger(os, 258); |
| 306 | writeNonNegativeInteger(os, 16843010LL); |
| 307 | writeNonNegativeInteger(os, 72340172838076674LL); |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 308 | |
| 309 | std::string buffer = os.str(); |
| 310 | const uint8_t* actual = reinterpret_cast<const uint8_t*>(buffer.c_str()); |
| 311 | |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 312 | BOOST_CHECK_EQUAL_COLLECTIONS(BUFFER, BUFFER + sizeof(BUFFER), |
| 313 | actual, actual + sizeof(BUFFER)); |
| 314 | } |
| 315 | |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 316 | BOOST_AUTO_TEST_CASE(ReadFromBuffer) |
| 317 | { |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 318 | const uint8_t* begin = nullptr; |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 319 | |
| 320 | begin = BUFFER; |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 321 | BOOST_CHECK_EQUAL(readNonNegativeInteger(1, begin, begin + 1), 1); |
| 322 | BOOST_CHECK_EQUAL(begin, BUFFER + 1); |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 323 | |
| 324 | begin = BUFFER + 1; |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 325 | BOOST_CHECK_EQUAL(readNonNegativeInteger(1, begin, begin + 1), 255); |
| 326 | BOOST_CHECK_EQUAL(begin, BUFFER + 2); |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 327 | |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 328 | begin = BUFFER + 2; |
| 329 | BOOST_CHECK_EQUAL(readNonNegativeInteger(2, begin, begin + 2), 258); |
| 330 | BOOST_CHECK_EQUAL(begin, BUFFER + 4); |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 331 | |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 332 | begin = BUFFER + 4; |
| 333 | BOOST_CHECK_EQUAL(readNonNegativeInteger(4, begin, begin + 4), 16843010LL); |
| 334 | BOOST_CHECK_EQUAL(begin, BUFFER + 8); |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 335 | |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 336 | begin = BUFFER + 8; |
| 337 | BOOST_CHECK_EQUAL(readNonNegativeInteger(8, begin, begin + 8), 72340172838076674LL); |
| 338 | BOOST_CHECK_EQUAL(begin, BUFFER + 16); |
| 339 | |
| 340 | // invalid size |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 341 | begin = BUFFER; |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 342 | BOOST_CHECK_THROW(readNonNegativeInteger(3, begin, begin + 3), Error); |
| 343 | |
| 344 | // available buffer smaller than size |
| 345 | begin = BUFFER; |
| 346 | BOOST_CHECK_THROW(readNonNegativeInteger(1, begin, begin + 0), Error); |
| 347 | begin = BUFFER; |
| 348 | BOOST_CHECK_THROW(readNonNegativeInteger(2, begin, begin + 1), Error); |
| 349 | begin = BUFFER; |
| 350 | BOOST_CHECK_THROW(readNonNegativeInteger(4, begin, begin + 3), Error); |
| 351 | begin = BUFFER; |
| 352 | BOOST_CHECK_THROW(readNonNegativeInteger(8, begin, begin + 7), Error); |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | BOOST_AUTO_TEST_CASE(ReadFromStream) |
| 356 | { |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 357 | Iterator end; // end of stream |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 358 | |
| 359 | { |
| 360 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER), sizeof(BUFFER)); |
| 361 | Iterator begin(stream); |
| 362 | BOOST_CHECK_EQUAL(readNonNegativeInteger(1, begin, end), 1); |
| 363 | BOOST_CHECK_EQUAL(readNonNegativeInteger(1, begin, end), 255); |
| 364 | BOOST_CHECK_EQUAL(readNonNegativeInteger(2, begin, end), 258); |
| 365 | BOOST_CHECK_EQUAL(readNonNegativeInteger(4, begin, end), 16843010LL); |
| 366 | BOOST_CHECK_EQUAL(readNonNegativeInteger(8, begin, end), 72340172838076674LL); |
| 367 | BOOST_CHECK(begin == end); |
| 368 | } |
| 369 | |
| 370 | // invalid size |
| 371 | { |
| 372 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 3); |
| 373 | Iterator begin(stream); |
| 374 | BOOST_CHECK_THROW(readNonNegativeInteger(3, begin, end), Error); |
| 375 | } |
| 376 | |
| 377 | // available buffer smaller than size |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 378 | { |
| 379 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 0); |
| 380 | Iterator begin(stream); |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 381 | BOOST_CHECK_THROW(readNonNegativeInteger(1, begin, end), Error); |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 382 | } |
| 383 | { |
| 384 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 1); |
| 385 | Iterator begin(stream); |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 386 | BOOST_CHECK_THROW(readNonNegativeInteger(2, begin, end), Error); |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 387 | } |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 388 | { |
| 389 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 3); |
| 390 | Iterator begin(stream); |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 391 | BOOST_CHECK_THROW(readNonNegativeInteger(4, begin, end), Error); |
| 392 | } |
| 393 | { |
| 394 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 7); |
| 395 | Iterator begin(stream); |
| 396 | BOOST_CHECK_THROW(readNonNegativeInteger(8, begin, end), Error); |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 397 | } |
| 398 | } |
| 399 | |
| 400 | BOOST_AUTO_TEST_SUITE_END() // NonNegativeInteger |
| 401 | |
Alexander Afanasyev | 5f1820e | 2017-01-04 18:12:42 -0800 | [diff] [blame] | 402 | BOOST_AUTO_TEST_SUITE(PrintHelpers) |
| 403 | |
| 404 | BOOST_AUTO_TEST_CASE(PrintSignatureTypeValue) |
| 405 | { |
| 406 | SignatureTypeValue value = DigestSha256; |
| 407 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(value), "DigestSha256"); |
| 408 | |
| 409 | value = SignatureSha256WithRsa; |
| 410 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(value), "SignatureSha256WithRsa"); |
| 411 | |
| 412 | value = SignatureSha256WithEcdsa; |
| 413 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(value), "SignatureSha256WithEcdsa"); |
| 414 | |
| 415 | value = static_cast<SignatureTypeValue>(-1); |
| 416 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(value), "Unknown Signature Type"); |
| 417 | } |
| 418 | |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 419 | BOOST_AUTO_TEST_SUITE_END() // PrintHelpers |
Alexander Afanasyev | 5f1820e | 2017-01-04 18:12:42 -0800 | [diff] [blame] | 420 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 421 | BOOST_AUTO_TEST_SUITE_END() // TestTlv |
| 422 | BOOST_AUTO_TEST_SUITE_END() // Encoding |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 423 | |
| 424 | } // namespace tests |
| 425 | } // namespace tlv |
| 426 | } // namespace ndn |