Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 3 | * Copyright (c) 2013-2016 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[] = { |
| 105 | 0x64, 0x0a, // LpPacket |
| 106 | 0x52, 0x01, // FragIndex |
| 107 | 0x00, |
| 108 | 0x53, 0x01, // FragCount |
| 109 | 0x01, |
| 110 | 0x50, 0x02, // Fragment |
| 111 | 0x03, 0xe8, |
| 112 | }; |
| 113 | |
| 114 | Buffer frag(2); |
| 115 | frag[0] = 0x03; |
| 116 | frag[1] = 0xe8; |
| 117 | |
| 118 | Packet packet; |
| 119 | BOOST_CHECK_NO_THROW(packet.add<FragmentField>(std::make_pair(frag.begin(), frag.end()))); |
| 120 | BOOST_CHECK_NO_THROW(packet.add<FragIndexField>(0)); |
| 121 | BOOST_CHECK_NO_THROW(packet.add<FragCountField>(1)); |
| 122 | Block wire; |
| 123 | BOOST_REQUIRE_NO_THROW(wire = packet.wireEncode()); |
| 124 | BOOST_CHECK_EQUAL_COLLECTIONS(expectedBlock, expectedBlock + sizeof(expectedBlock), |
| 125 | wire.begin(), wire.end()); |
| 126 | } |
| 127 | |
| 128 | BOOST_AUTO_TEST_CASE(DecodeNormal) |
| 129 | { |
| 130 | static const uint8_t inputBlock[] = { |
| 131 | 0x64, 0x0a, // LpPacket |
| 132 | 0x52, 0x01, // FragIndex |
| 133 | 0x00, |
| 134 | 0x53, 0x01, // FragCount |
| 135 | 0x01, |
| 136 | 0x50, 0x02, // Fragment |
| 137 | 0x03, 0xe8, |
| 138 | }; |
| 139 | |
| 140 | Packet packet; |
| 141 | Block wire(inputBlock, sizeof(inputBlock)); |
| 142 | BOOST_CHECK_NO_THROW(packet.wireDecode(wire)); |
| 143 | BOOST_CHECK_EQUAL(1, packet.count<FragmentField>()); |
| 144 | BOOST_CHECK_EQUAL(1, packet.count<FragIndexField>()); |
| 145 | BOOST_CHECK_EQUAL(1, packet.count<FragCountField>()); |
| 146 | Buffer::const_iterator first, last; |
| 147 | BOOST_REQUIRE_NO_THROW(std::tie(first, last) = packet.get<FragmentField>(0)); |
| 148 | BOOST_CHECK_EQUAL(2, last - first); |
| 149 | BOOST_CHECK_EQUAL(0x03, *first); |
| 150 | BOOST_CHECK_EQUAL(0xe8, *(last - 1)); |
| 151 | BOOST_CHECK_EQUAL(0, packet.get<FragIndexField>(0)); |
| 152 | BOOST_CHECK_EQUAL(1, packet.get<FragCountField>(0)); |
| 153 | } |
| 154 | |
| 155 | BOOST_AUTO_TEST_CASE(DecodeIdle) |
| 156 | { |
| 157 | static const uint8_t inputBlock[] = { |
| 158 | 0x64, 0x06, // LpPacket |
| 159 | 0x52, 0x01, // FragIndex |
| 160 | 0x00, |
| 161 | 0x53, 0x01, // FragCount |
| 162 | 0x01, |
| 163 | }; |
| 164 | |
| 165 | Packet packet; |
| 166 | Block wire(inputBlock, sizeof(inputBlock)); |
| 167 | BOOST_CHECK_NO_THROW(packet.wireDecode(wire)); |
| 168 | BOOST_CHECK_EQUAL(0, packet.count<FragmentField>()); |
| 169 | BOOST_CHECK_EQUAL(1, packet.count<FragIndexField>()); |
| 170 | BOOST_CHECK_EQUAL(1, packet.count<FragCountField>()); |
| 171 | BOOST_CHECK_EQUAL(0, packet.get<FragIndexField>(0)); |
| 172 | BOOST_CHECK_EQUAL(1, packet.get<FragCountField>(0)); |
| 173 | } |
| 174 | |
| 175 | BOOST_AUTO_TEST_CASE(DecodeFragment) |
| 176 | { |
| 177 | static const uint8_t inputBlock[] = { |
| 178 | 0x64, 0x04, // LpPacket |
| 179 | 0x50, 0x02, // Fragment |
| 180 | 0x03, 0xe8, |
| 181 | }; |
| 182 | |
| 183 | Packet packet; |
| 184 | Block wire(inputBlock, sizeof(inputBlock)); |
| 185 | BOOST_CHECK_NO_THROW(packet.wireDecode(wire)); |
| 186 | BOOST_CHECK_EQUAL(1, packet.count<FragmentField>()); |
| 187 | BOOST_CHECK_EQUAL(0, packet.count<FragIndexField>()); |
| 188 | Buffer::const_iterator first, last; |
| 189 | BOOST_REQUIRE_NO_THROW(std::tie(first, last) = packet.get<FragmentField>(0)); |
| 190 | BOOST_CHECK_EQUAL(2, last - first); |
| 191 | BOOST_CHECK_EQUAL(0x03, *first); |
| 192 | BOOST_CHECK_EQUAL(0xe8, *(last - 1)); |
| 193 | } |
| 194 | |
| 195 | BOOST_AUTO_TEST_CASE(DecodeEmpty) |
| 196 | { |
| 197 | static const uint8_t inputBlock[] = { |
| 198 | 0x64, 0x00, // LpPacket |
| 199 | }; |
| 200 | |
| 201 | Packet packet; |
| 202 | Block wire(inputBlock, sizeof(inputBlock)); |
| 203 | BOOST_CHECK_NO_THROW(packet.wireDecode(wire)); |
| 204 | BOOST_CHECK_EQUAL(0, packet.count<FragmentField>()); |
| 205 | BOOST_CHECK_EQUAL(0, packet.count<FragIndexField>()); |
| 206 | } |
| 207 | |
| 208 | BOOST_AUTO_TEST_CASE(DecodeRepeatedNonRepeatableHeader) |
| 209 | { |
| 210 | static const uint8_t inputBlock[] = { |
| 211 | 0x64, 0x06, // LpPacket |
| 212 | 0x52, 0x01, // FragIndex |
| 213 | 0x00, |
| 214 | 0x52, 0x01, // FragIndex |
| 215 | 0x01, |
| 216 | }; |
| 217 | |
| 218 | Packet packet; |
| 219 | Block wire(inputBlock, sizeof(inputBlock)); |
| 220 | BOOST_CHECK_THROW(packet.wireDecode(wire), Packet::Error); |
| 221 | } |
| 222 | |
| 223 | BOOST_AUTO_TEST_CASE(DecodeRepeatedFragment) |
| 224 | { |
| 225 | static const uint8_t inputBlock[] = { |
| 226 | 0x64, 0x08, // LpPacket |
| 227 | 0x50, 0x02, // Fragment |
| 228 | 0x03, 0xe8, |
| 229 | 0x50, 0x02, // Fragment |
| 230 | 0x03, 0xe9, |
| 231 | }; |
| 232 | |
| 233 | Packet packet; |
| 234 | Block wire(inputBlock, sizeof(inputBlock)); |
| 235 | BOOST_CHECK_THROW(packet.wireDecode(wire), Packet::Error); |
| 236 | } |
| 237 | |
| 238 | BOOST_AUTO_TEST_CASE(DecodeWrongOrderAmongHeaders) |
| 239 | { |
| 240 | static const uint8_t inputBlock[] = { |
| 241 | 0x64, 0x0a, // LpPacket |
| 242 | 0x53, 0x01, // FragCount |
| 243 | 0x01, |
| 244 | 0x52, 0x01, // FragIndex |
| 245 | 0x00, |
| 246 | 0x50, 0x02, // Fragment |
| 247 | 0x03, 0xe8, |
| 248 | }; |
| 249 | |
| 250 | Packet packet; |
| 251 | Block wire(inputBlock, sizeof(inputBlock)); |
| 252 | BOOST_CHECK_THROW(packet.wireDecode(wire), Packet::Error); |
| 253 | } |
| 254 | |
| 255 | BOOST_AUTO_TEST_CASE(DecodeWrongOrderFragment) |
| 256 | { |
| 257 | static const uint8_t inputBlock[] = { |
| 258 | 0x64, 0x0a, // LpPacket |
| 259 | 0x52, 0x01, // FragIndex |
| 260 | 0x00, |
| 261 | 0x50, 0x02, // Fragment |
| 262 | 0x03, 0xe8, |
| 263 | 0x53, 0x01, // FragCount |
| 264 | 0x01, |
| 265 | }; |
| 266 | |
| 267 | Packet packet; |
| 268 | Block wire(inputBlock, sizeof(inputBlock)); |
| 269 | BOOST_CHECK_THROW(packet.wireDecode(wire), Packet::Error); |
| 270 | } |
| 271 | |
| 272 | BOOST_AUTO_TEST_CASE(DecodeIgnoredHeader) |
| 273 | { |
| 274 | static const uint8_t inputBlock[] = { |
| 275 | 0x64, 0x0c, // LpPacket |
| 276 | 0x52, 0x01, // FragIndex |
| 277 | 0x00, |
Eric Newberry | 3ed6247 | 2016-12-11 22:11:38 -0700 | [diff] [blame] | 278 | 0xfd, 0x03, 0x24, 0x01, // unknown TLV-TYPE 804 (ignored) |
Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 279 | 0x02, |
| 280 | 0x50, 0x02, // Fragment |
| 281 | 0x03, 0xe8, |
| 282 | }; |
| 283 | |
| 284 | Packet packet; |
| 285 | Block wire(inputBlock, sizeof(inputBlock)); |
| 286 | BOOST_CHECK_NO_THROW(packet.wireDecode(wire)); |
| 287 | BOOST_CHECK_EQUAL(1, packet.count<FragmentField>()); |
| 288 | BOOST_CHECK_EQUAL(1, packet.count<FragIndexField>()); |
| 289 | } |
| 290 | |
| 291 | BOOST_AUTO_TEST_CASE(DecodeUnrecognizedHeader) |
| 292 | { |
| 293 | static const uint8_t inputBlock[] = { |
| 294 | 0x64, 0x0c, // LpPacket |
| 295 | 0x52, 0x01, // FragIndex |
| 296 | 0x00, |
| 297 | 0xfd, 0x03, 0x22, 0x01, // unknown TLV-TYPE 802 (cannot ignore) |
| 298 | 0x02, |
| 299 | 0x50, 0x02, // Fragment |
| 300 | 0x03, 0xe8, |
| 301 | }; |
| 302 | |
| 303 | Packet packet; |
| 304 | Block wire(inputBlock, sizeof(inputBlock)); |
| 305 | BOOST_CHECK_THROW(packet.wireDecode(wire), Packet::Error); |
| 306 | } |
| 307 | |
| 308 | BOOST_AUTO_TEST_CASE(DecodeBareNetworkLayerPacket) |
| 309 | { |
| 310 | static const uint8_t inputBlock[] = { |
| 311 | 0x05, 0x0a, // Interest |
| 312 | 0x07, 0x02, // Name |
| 313 | 0x03, 0xe8, |
| 314 | 0x0a, 0x04, // Nonce |
| 315 | 0x01, 0x02, 0x03, 0x04, |
| 316 | }; |
| 317 | |
| 318 | Packet packet; |
| 319 | Block wire(inputBlock, sizeof(inputBlock)); |
| 320 | BOOST_CHECK_NO_THROW(packet.wireDecode(wire)); |
| 321 | BOOST_CHECK_EQUAL(1, packet.count<FragmentField>()); |
| 322 | |
Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 323 | Block encoded; |
| 324 | BOOST_CHECK_NO_THROW(encoded = packet.wireEncode()); |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 325 | BOOST_CHECK_EQUAL_COLLECTIONS(inputBlock, inputBlock + sizeof(inputBlock), |
Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 326 | encoded.begin(), encoded.end()); |
| 327 | } |
| 328 | |
Eric Newberry | 43bf6bb | 2015-10-09 16:12:09 -0700 | [diff] [blame] | 329 | BOOST_AUTO_TEST_CASE(DecodeUnrecognizedTlvType) |
| 330 | { |
| 331 | Packet packet; |
| 332 | Block wire = encoding::makeEmptyBlock(ndn::tlv::Name); |
| 333 | BOOST_CHECK_THROW(packet.wireDecode(wire), Packet::Error); |
| 334 | } |
| 335 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 336 | BOOST_AUTO_TEST_SUITE_END() // TestPacket |
| 337 | BOOST_AUTO_TEST_SUITE_END() // Lp |
Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 338 | |
| 339 | } // namespace tests |
| 340 | } // namespace lp |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 341 | } // namespace ndn |