Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Eric Newberry | 8422f57 | 2017-02-04 21:53:58 -0700 | [diff] [blame] | 3 | * Copyright (c) 2013-2017 Regents of the University of California. |
Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -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 "lp/packet.hpp" |
| 23 | |
| 24 | #include "boost-test.hpp" |
| 25 | |
| 26 | namespace ndn { |
| 27 | namespace lp { |
| 28 | namespace tests { |
| 29 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 30 | BOOST_AUTO_TEST_SUITE(Lp) |
| 31 | BOOST_AUTO_TEST_SUITE(TestPacket) |
Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 32 | |
| 33 | BOOST_AUTO_TEST_CASE(FieldAccess) |
| 34 | { |
| 35 | Packet packet; |
| 36 | |
| 37 | BOOST_CHECK(!packet.has<FragIndexField>()); |
| 38 | BOOST_CHECK_EQUAL(0, packet.count<FragIndexField>()); |
| 39 | BOOST_CHECK_NO_THROW(packet.set<FragIndexField>(1234)); |
| 40 | BOOST_CHECK(packet.has<FragIndexField>()); |
| 41 | BOOST_CHECK_THROW(packet.add<FragIndexField>(5678), std::length_error); |
| 42 | BOOST_CHECK_EQUAL(1, packet.count<FragIndexField>()); |
| 43 | BOOST_CHECK_EQUAL(1234, packet.get<FragIndexField>(0)); |
| 44 | BOOST_CHECK_THROW(packet.get<FragIndexField>(1), std::out_of_range); |
| 45 | BOOST_CHECK_THROW(packet.remove<FragIndexField>(1), std::out_of_range); |
| 46 | BOOST_CHECK_NO_THROW(packet.remove<FragIndexField>(0)); |
| 47 | BOOST_CHECK_EQUAL(0, packet.count<FragIndexField>()); |
| 48 | BOOST_CHECK_NO_THROW(packet.add<FragIndexField>(832)); |
| 49 | std::vector<uint64_t> fragIndexes; |
| 50 | BOOST_REQUIRE_NO_THROW(fragIndexes = packet.list<FragIndexField>()); |
| 51 | BOOST_CHECK_EQUAL(1, fragIndexes.size()); |
| 52 | BOOST_CHECK_EQUAL(832, fragIndexes.at(0)); |
| 53 | BOOST_CHECK_NO_THROW(packet.clear<FragIndexField>()); |
| 54 | BOOST_CHECK_EQUAL(0, packet.count<FragIndexField>()); |
| 55 | } |
| 56 | |
| 57 | /// \todo test field access methods with a REPEATABLE field |
| 58 | |
| 59 | BOOST_AUTO_TEST_CASE(EncodeFragment) |
| 60 | { |
| 61 | static const uint8_t expectedBlock[] = { |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 62 | 0x64, 0x08, // LpPacket |
| 63 | 0x51, 0x02, // Sequence |
| 64 | 0x03, 0xe8, |
Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 65 | 0x50, 0x02, // Fragment |
| 66 | 0x03, 0xe8, |
| 67 | }; |
| 68 | |
| 69 | Buffer buf(2); |
| 70 | buf[0] = 0x03; |
| 71 | buf[1] = 0xe8; |
| 72 | |
| 73 | Packet packet; |
| 74 | BOOST_CHECK_NO_THROW(packet.add<FragmentField>(std::make_pair(buf.begin(), buf.end()))); |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 75 | BOOST_CHECK_NO_THROW(packet.add<SequenceField>(1000)); |
Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 76 | Block wire; |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 77 | BOOST_CHECK_NO_THROW(wire = packet.wireEncode()); |
Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 78 | BOOST_CHECK_EQUAL_COLLECTIONS(expectedBlock, expectedBlock + sizeof(expectedBlock), |
| 79 | wire.begin(), wire.end()); |
| 80 | } |
| 81 | |
| 82 | BOOST_AUTO_TEST_CASE(EncodeSubTlv) |
| 83 | { |
| 84 | static const uint8_t expectedBlock[] = { |
| 85 | 0x64, 0x09, // LpPacket |
| 86 | 0xfd, 0x03, 0x20, 0x05, // Nack |
| 87 | 0xfd, 0x03, 0x21, 0x01, // NackReason |
| 88 | 0x64, |
| 89 | }; |
| 90 | |
| 91 | NackHeader nack; |
| 92 | nack.setReason(NackReason::DUPLICATE); |
| 93 | |
| 94 | Packet packet; |
| 95 | BOOST_CHECK_NO_THROW(packet.add<NackField>(nack)); |
| 96 | Block wire; |
| 97 | BOOST_REQUIRE_NO_THROW(wire = packet.wireEncode()); |
| 98 | BOOST_CHECK_EQUAL_COLLECTIONS(expectedBlock, expectedBlock + sizeof(expectedBlock), |
| 99 | wire.begin(), wire.end()); |
| 100 | } |
| 101 | |
| 102 | BOOST_AUTO_TEST_CASE(EncodeSortOrder) |
| 103 | { |
| 104 | static const uint8_t expectedBlock[] = { |
Eric Newberry | 8422f57 | 2017-02-04 21:53:58 -0700 | [diff] [blame] | 105 | 0x64, 0x19, // LpPacket |
Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 106 | 0x52, 0x01, // FragIndex |
| 107 | 0x00, |
| 108 | 0x53, 0x01, // FragCount |
| 109 | 0x01, |
Eric Newberry | 8422f57 | 2017-02-04 21:53:58 -0700 | [diff] [blame] | 110 | 0xfd, 0x03, 0x44, 0x01, // Ack |
| 111 | 0x02, |
| 112 | 0xfd, 0x03, 0x44, 0x01, // Ack |
| 113 | 0x04, |
| 114 | 0xfd, 0x03, 0x44, 0x01, // Ack |
| 115 | 0x03, |
Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 116 | 0x50, 0x02, // Fragment |
| 117 | 0x03, 0xe8, |
| 118 | }; |
| 119 | |
| 120 | Buffer frag(2); |
| 121 | frag[0] = 0x03; |
| 122 | frag[1] = 0xe8; |
| 123 | |
| 124 | Packet packet; |
| 125 | BOOST_CHECK_NO_THROW(packet.add<FragmentField>(std::make_pair(frag.begin(), frag.end()))); |
| 126 | BOOST_CHECK_NO_THROW(packet.add<FragIndexField>(0)); |
Eric Newberry | 8422f57 | 2017-02-04 21:53:58 -0700 | [diff] [blame] | 127 | BOOST_CHECK_NO_THROW(packet.add<AckField>(2)); |
Eric Newberry | 2a89077 | 2017-06-26 12:06:15 -0700 | [diff] [blame^] | 128 | BOOST_REQUIRE_NO_THROW(packet.wireEncode()); |
Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 129 | BOOST_CHECK_NO_THROW(packet.add<FragCountField>(1)); |
Eric Newberry | 2a89077 | 2017-06-26 12:06:15 -0700 | [diff] [blame^] | 130 | BOOST_REQUIRE_NO_THROW(packet.wireEncode()); |
Eric Newberry | 8422f57 | 2017-02-04 21:53:58 -0700 | [diff] [blame] | 131 | BOOST_CHECK_NO_THROW(packet.add<AckField>(4)); |
Eric Newberry | 2a89077 | 2017-06-26 12:06:15 -0700 | [diff] [blame^] | 132 | BOOST_REQUIRE_NO_THROW(packet.wireEncode()); |
Eric Newberry | 8422f57 | 2017-02-04 21:53:58 -0700 | [diff] [blame] | 133 | BOOST_CHECK_NO_THROW(packet.add<AckField>(3)); |
Eric Newberry | 2a89077 | 2017-06-26 12:06:15 -0700 | [diff] [blame^] | 134 | BOOST_REQUIRE_NO_THROW(packet.wireEncode()); |
Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 135 | Block wire; |
| 136 | BOOST_REQUIRE_NO_THROW(wire = packet.wireEncode()); |
| 137 | BOOST_CHECK_EQUAL_COLLECTIONS(expectedBlock, expectedBlock + sizeof(expectedBlock), |
| 138 | wire.begin(), wire.end()); |
| 139 | } |
| 140 | |
| 141 | BOOST_AUTO_TEST_CASE(DecodeNormal) |
| 142 | { |
| 143 | static const uint8_t inputBlock[] = { |
| 144 | 0x64, 0x0a, // LpPacket |
| 145 | 0x52, 0x01, // FragIndex |
| 146 | 0x00, |
| 147 | 0x53, 0x01, // FragCount |
| 148 | 0x01, |
| 149 | 0x50, 0x02, // Fragment |
| 150 | 0x03, 0xe8, |
| 151 | }; |
| 152 | |
| 153 | Packet packet; |
| 154 | Block wire(inputBlock, sizeof(inputBlock)); |
| 155 | BOOST_CHECK_NO_THROW(packet.wireDecode(wire)); |
| 156 | BOOST_CHECK_EQUAL(1, packet.count<FragmentField>()); |
| 157 | BOOST_CHECK_EQUAL(1, packet.count<FragIndexField>()); |
| 158 | BOOST_CHECK_EQUAL(1, packet.count<FragCountField>()); |
| 159 | Buffer::const_iterator first, last; |
| 160 | BOOST_REQUIRE_NO_THROW(std::tie(first, last) = packet.get<FragmentField>(0)); |
| 161 | BOOST_CHECK_EQUAL(2, last - first); |
| 162 | BOOST_CHECK_EQUAL(0x03, *first); |
| 163 | BOOST_CHECK_EQUAL(0xe8, *(last - 1)); |
| 164 | BOOST_CHECK_EQUAL(0, packet.get<FragIndexField>(0)); |
| 165 | BOOST_CHECK_EQUAL(1, packet.get<FragCountField>(0)); |
| 166 | } |
| 167 | |
| 168 | BOOST_AUTO_TEST_CASE(DecodeIdle) |
| 169 | { |
| 170 | static const uint8_t inputBlock[] = { |
| 171 | 0x64, 0x06, // LpPacket |
| 172 | 0x52, 0x01, // FragIndex |
| 173 | 0x00, |
| 174 | 0x53, 0x01, // FragCount |
| 175 | 0x01, |
| 176 | }; |
| 177 | |
| 178 | Packet packet; |
| 179 | Block wire(inputBlock, sizeof(inputBlock)); |
| 180 | BOOST_CHECK_NO_THROW(packet.wireDecode(wire)); |
| 181 | BOOST_CHECK_EQUAL(0, packet.count<FragmentField>()); |
| 182 | BOOST_CHECK_EQUAL(1, packet.count<FragIndexField>()); |
| 183 | BOOST_CHECK_EQUAL(1, packet.count<FragCountField>()); |
| 184 | BOOST_CHECK_EQUAL(0, packet.get<FragIndexField>(0)); |
| 185 | BOOST_CHECK_EQUAL(1, packet.get<FragCountField>(0)); |
| 186 | } |
| 187 | |
| 188 | BOOST_AUTO_TEST_CASE(DecodeFragment) |
| 189 | { |
| 190 | static const uint8_t inputBlock[] = { |
| 191 | 0x64, 0x04, // LpPacket |
| 192 | 0x50, 0x02, // Fragment |
| 193 | 0x03, 0xe8, |
| 194 | }; |
| 195 | |
| 196 | Packet packet; |
| 197 | Block wire(inputBlock, sizeof(inputBlock)); |
| 198 | BOOST_CHECK_NO_THROW(packet.wireDecode(wire)); |
| 199 | BOOST_CHECK_EQUAL(1, packet.count<FragmentField>()); |
| 200 | BOOST_CHECK_EQUAL(0, packet.count<FragIndexField>()); |
| 201 | Buffer::const_iterator first, last; |
| 202 | BOOST_REQUIRE_NO_THROW(std::tie(first, last) = packet.get<FragmentField>(0)); |
| 203 | BOOST_CHECK_EQUAL(2, last - first); |
| 204 | BOOST_CHECK_EQUAL(0x03, *first); |
| 205 | BOOST_CHECK_EQUAL(0xe8, *(last - 1)); |
| 206 | } |
| 207 | |
| 208 | BOOST_AUTO_TEST_CASE(DecodeEmpty) |
| 209 | { |
| 210 | static const uint8_t inputBlock[] = { |
| 211 | 0x64, 0x00, // LpPacket |
| 212 | }; |
| 213 | |
| 214 | Packet packet; |
| 215 | Block wire(inputBlock, sizeof(inputBlock)); |
| 216 | BOOST_CHECK_NO_THROW(packet.wireDecode(wire)); |
| 217 | BOOST_CHECK_EQUAL(0, packet.count<FragmentField>()); |
| 218 | BOOST_CHECK_EQUAL(0, packet.count<FragIndexField>()); |
| 219 | } |
| 220 | |
| 221 | BOOST_AUTO_TEST_CASE(DecodeRepeatedNonRepeatableHeader) |
| 222 | { |
| 223 | static const uint8_t inputBlock[] = { |
| 224 | 0x64, 0x06, // LpPacket |
| 225 | 0x52, 0x01, // FragIndex |
| 226 | 0x00, |
| 227 | 0x52, 0x01, // FragIndex |
| 228 | 0x01, |
| 229 | }; |
| 230 | |
| 231 | Packet packet; |
| 232 | Block wire(inputBlock, sizeof(inputBlock)); |
| 233 | BOOST_CHECK_THROW(packet.wireDecode(wire), Packet::Error); |
| 234 | } |
| 235 | |
Eric Newberry | 8422f57 | 2017-02-04 21:53:58 -0700 | [diff] [blame] | 236 | BOOST_AUTO_TEST_CASE(DecodeRepeatedRepeatableHeader) |
| 237 | { |
| 238 | static const uint8_t inputBlock[] = { |
| 239 | 0x64, 0x0f, // LpPacket |
| 240 | 0xfd, 0x03, 0x44, 0x01, // Ack |
| 241 | 0x01, |
| 242 | 0xfd, 0x03, 0x44, 0x01, // Ack |
| 243 | 0x03, |
| 244 | 0xfd, 0x03, 0x44, 0x01, // Ack |
| 245 | 0x02, |
| 246 | }; |
| 247 | |
| 248 | Packet packet; |
| 249 | Block wire(inputBlock, sizeof(inputBlock)); |
| 250 | BOOST_CHECK_NO_THROW(packet.wireDecode(wire)); |
| 251 | BOOST_REQUIRE_EQUAL(packet.count<AckField>(), 3); |
| 252 | BOOST_CHECK_EQUAL(packet.get<AckField>(), 1); |
| 253 | BOOST_CHECK_EQUAL(packet.get<AckField>(0), 1); |
| 254 | BOOST_CHECK_EQUAL(packet.get<AckField>(1), 3); |
| 255 | BOOST_CHECK_EQUAL(packet.get<AckField>(2), 2); |
| 256 | } |
| 257 | |
Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 258 | BOOST_AUTO_TEST_CASE(DecodeRepeatedFragment) |
| 259 | { |
| 260 | static const uint8_t inputBlock[] = { |
| 261 | 0x64, 0x08, // LpPacket |
| 262 | 0x50, 0x02, // Fragment |
| 263 | 0x03, 0xe8, |
| 264 | 0x50, 0x02, // Fragment |
| 265 | 0x03, 0xe9, |
| 266 | }; |
| 267 | |
| 268 | Packet packet; |
| 269 | Block wire(inputBlock, sizeof(inputBlock)); |
| 270 | BOOST_CHECK_THROW(packet.wireDecode(wire), Packet::Error); |
| 271 | } |
| 272 | |
| 273 | BOOST_AUTO_TEST_CASE(DecodeWrongOrderAmongHeaders) |
| 274 | { |
| 275 | static const uint8_t inputBlock[] = { |
| 276 | 0x64, 0x0a, // LpPacket |
| 277 | 0x53, 0x01, // FragCount |
| 278 | 0x01, |
| 279 | 0x52, 0x01, // FragIndex |
| 280 | 0x00, |
| 281 | 0x50, 0x02, // Fragment |
| 282 | 0x03, 0xe8, |
| 283 | }; |
| 284 | |
| 285 | Packet packet; |
| 286 | Block wire(inputBlock, sizeof(inputBlock)); |
| 287 | BOOST_CHECK_THROW(packet.wireDecode(wire), Packet::Error); |
| 288 | } |
| 289 | |
| 290 | BOOST_AUTO_TEST_CASE(DecodeWrongOrderFragment) |
| 291 | { |
| 292 | static const uint8_t inputBlock[] = { |
| 293 | 0x64, 0x0a, // LpPacket |
| 294 | 0x52, 0x01, // FragIndex |
| 295 | 0x00, |
| 296 | 0x50, 0x02, // Fragment |
| 297 | 0x03, 0xe8, |
| 298 | 0x53, 0x01, // FragCount |
| 299 | 0x01, |
| 300 | }; |
| 301 | |
| 302 | Packet packet; |
| 303 | Block wire(inputBlock, sizeof(inputBlock)); |
| 304 | BOOST_CHECK_THROW(packet.wireDecode(wire), Packet::Error); |
| 305 | } |
| 306 | |
| 307 | BOOST_AUTO_TEST_CASE(DecodeIgnoredHeader) |
| 308 | { |
| 309 | static const uint8_t inputBlock[] = { |
| 310 | 0x64, 0x0c, // LpPacket |
| 311 | 0x52, 0x01, // FragIndex |
| 312 | 0x00, |
Eric Newberry | 3ed6247 | 2016-12-11 22:11:38 -0700 | [diff] [blame] | 313 | 0xfd, 0x03, 0x24, 0x01, // unknown TLV-TYPE 804 (ignored) |
Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 314 | 0x02, |
| 315 | 0x50, 0x02, // Fragment |
| 316 | 0x03, 0xe8, |
| 317 | }; |
| 318 | |
| 319 | Packet packet; |
| 320 | Block wire(inputBlock, sizeof(inputBlock)); |
| 321 | BOOST_CHECK_NO_THROW(packet.wireDecode(wire)); |
| 322 | BOOST_CHECK_EQUAL(1, packet.count<FragmentField>()); |
| 323 | BOOST_CHECK_EQUAL(1, packet.count<FragIndexField>()); |
| 324 | } |
| 325 | |
| 326 | BOOST_AUTO_TEST_CASE(DecodeUnrecognizedHeader) |
| 327 | { |
| 328 | static const uint8_t inputBlock[] = { |
| 329 | 0x64, 0x0c, // LpPacket |
| 330 | 0x52, 0x01, // FragIndex |
| 331 | 0x00, |
| 332 | 0xfd, 0x03, 0x22, 0x01, // unknown TLV-TYPE 802 (cannot ignore) |
| 333 | 0x02, |
| 334 | 0x50, 0x02, // Fragment |
| 335 | 0x03, 0xe8, |
| 336 | }; |
| 337 | |
| 338 | Packet packet; |
| 339 | Block wire(inputBlock, sizeof(inputBlock)); |
| 340 | BOOST_CHECK_THROW(packet.wireDecode(wire), Packet::Error); |
| 341 | } |
| 342 | |
| 343 | BOOST_AUTO_TEST_CASE(DecodeBareNetworkLayerPacket) |
| 344 | { |
| 345 | static const uint8_t inputBlock[] = { |
| 346 | 0x05, 0x0a, // Interest |
| 347 | 0x07, 0x02, // Name |
| 348 | 0x03, 0xe8, |
| 349 | 0x0a, 0x04, // Nonce |
| 350 | 0x01, 0x02, 0x03, 0x04, |
| 351 | }; |
| 352 | |
| 353 | Packet packet; |
| 354 | Block wire(inputBlock, sizeof(inputBlock)); |
| 355 | BOOST_CHECK_NO_THROW(packet.wireDecode(wire)); |
| 356 | BOOST_CHECK_EQUAL(1, packet.count<FragmentField>()); |
| 357 | |
Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 358 | Block encoded; |
| 359 | BOOST_CHECK_NO_THROW(encoded = packet.wireEncode()); |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 360 | BOOST_CHECK_EQUAL_COLLECTIONS(inputBlock, inputBlock + sizeof(inputBlock), |
Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 361 | encoded.begin(), encoded.end()); |
| 362 | } |
| 363 | |
Eric Newberry | 43bf6bb | 2015-10-09 16:12:09 -0700 | [diff] [blame] | 364 | BOOST_AUTO_TEST_CASE(DecodeUnrecognizedTlvType) |
| 365 | { |
| 366 | Packet packet; |
| 367 | Block wire = encoding::makeEmptyBlock(ndn::tlv::Name); |
| 368 | BOOST_CHECK_THROW(packet.wireDecode(wire), Packet::Error); |
| 369 | } |
| 370 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 371 | BOOST_AUTO_TEST_SUITE_END() // TestPacket |
| 372 | BOOST_AUTO_TEST_SUITE_END() // Lp |
Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 373 | |
| 374 | } // namespace tests |
| 375 | } // namespace lp |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 376 | } // namespace ndn |