Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | b6e276f | 2017-08-14 20:10:04 +0000 | [diff] [blame] | 2 | /* |
Teng Liang | 5b323d1 | 2018-01-31 18:50:45 -0700 | [diff] [blame] | 3 | * Copyright (c) 2013-2018 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" |
Teng Liang | 5b323d1 | 2018-01-31 18:50:45 -0700 | [diff] [blame] | 23 | #include "security/signature-sha256-with-rsa.hpp" |
Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 24 | |
| 25 | #include "boost-test.hpp" |
| 26 | |
| 27 | namespace ndn { |
| 28 | namespace lp { |
| 29 | namespace tests { |
| 30 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 31 | BOOST_AUTO_TEST_SUITE(Lp) |
| 32 | BOOST_AUTO_TEST_SUITE(TestPacket) |
Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 33 | |
| 34 | BOOST_AUTO_TEST_CASE(FieldAccess) |
| 35 | { |
| 36 | Packet packet; |
| 37 | |
Junxiao Shi | b6e276f | 2017-08-14 20:10:04 +0000 | [diff] [blame] | 38 | BOOST_CHECK(packet.empty()); |
Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 39 | BOOST_CHECK(!packet.has<FragIndexField>()); |
| 40 | BOOST_CHECK_EQUAL(0, packet.count<FragIndexField>()); |
Junxiao Shi | b6e276f | 2017-08-14 20:10:04 +0000 | [diff] [blame] | 41 | |
| 42 | packet.set<FragIndexField>(1234); |
| 43 | BOOST_CHECK(!packet.empty()); |
Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 44 | BOOST_CHECK(packet.has<FragIndexField>()); |
| 45 | BOOST_CHECK_THROW(packet.add<FragIndexField>(5678), std::length_error); |
| 46 | BOOST_CHECK_EQUAL(1, packet.count<FragIndexField>()); |
| 47 | BOOST_CHECK_EQUAL(1234, packet.get<FragIndexField>(0)); |
| 48 | BOOST_CHECK_THROW(packet.get<FragIndexField>(1), std::out_of_range); |
| 49 | BOOST_CHECK_THROW(packet.remove<FragIndexField>(1), std::out_of_range); |
Junxiao Shi | b6e276f | 2017-08-14 20:10:04 +0000 | [diff] [blame] | 50 | |
| 51 | packet.remove<FragIndexField>(0); |
Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 52 | BOOST_CHECK_EQUAL(0, packet.count<FragIndexField>()); |
Junxiao Shi | b6e276f | 2017-08-14 20:10:04 +0000 | [diff] [blame] | 53 | |
| 54 | packet.add<FragIndexField>(832); |
| 55 | std::vector<uint64_t> fragIndexes = packet.list<FragIndexField>(); |
Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 56 | BOOST_CHECK_EQUAL(1, fragIndexes.size()); |
| 57 | BOOST_CHECK_EQUAL(832, fragIndexes.at(0)); |
Junxiao Shi | b6e276f | 2017-08-14 20:10:04 +0000 | [diff] [blame] | 58 | |
| 59 | packet.clear<FragIndexField>(); |
Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 60 | BOOST_CHECK_EQUAL(0, packet.count<FragIndexField>()); |
Junxiao Shi | b6e276f | 2017-08-14 20:10:04 +0000 | [diff] [blame] | 61 | BOOST_CHECK(packet.empty()); |
Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | /// \todo test field access methods with a REPEATABLE field |
| 65 | |
| 66 | BOOST_AUTO_TEST_CASE(EncodeFragment) |
| 67 | { |
| 68 | static const uint8_t expectedBlock[] = { |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 69 | 0x64, 0x08, // LpPacket |
| 70 | 0x51, 0x02, // Sequence |
| 71 | 0x03, 0xe8, |
Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 72 | 0x50, 0x02, // Fragment |
| 73 | 0x03, 0xe8, |
| 74 | }; |
| 75 | |
| 76 | Buffer buf(2); |
| 77 | buf[0] = 0x03; |
| 78 | buf[1] = 0xe8; |
| 79 | |
| 80 | Packet packet; |
| 81 | 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] | 82 | BOOST_CHECK_NO_THROW(packet.add<SequenceField>(1000)); |
Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 83 | Block wire; |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 84 | BOOST_CHECK_NO_THROW(wire = packet.wireEncode()); |
Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 85 | BOOST_CHECK_EQUAL_COLLECTIONS(expectedBlock, expectedBlock + sizeof(expectedBlock), |
| 86 | wire.begin(), wire.end()); |
| 87 | } |
| 88 | |
| 89 | BOOST_AUTO_TEST_CASE(EncodeSubTlv) |
| 90 | { |
| 91 | static const uint8_t expectedBlock[] = { |
| 92 | 0x64, 0x09, // LpPacket |
| 93 | 0xfd, 0x03, 0x20, 0x05, // Nack |
| 94 | 0xfd, 0x03, 0x21, 0x01, // NackReason |
| 95 | 0x64, |
| 96 | }; |
| 97 | |
| 98 | NackHeader nack; |
| 99 | nack.setReason(NackReason::DUPLICATE); |
| 100 | |
| 101 | Packet packet; |
| 102 | BOOST_CHECK_NO_THROW(packet.add<NackField>(nack)); |
| 103 | Block wire; |
| 104 | BOOST_REQUIRE_NO_THROW(wire = packet.wireEncode()); |
| 105 | BOOST_CHECK_EQUAL_COLLECTIONS(expectedBlock, expectedBlock + sizeof(expectedBlock), |
| 106 | wire.begin(), wire.end()); |
| 107 | } |
| 108 | |
Teng Liang | 0296074 | 2017-10-24 00:36:45 -0700 | [diff] [blame] | 109 | BOOST_AUTO_TEST_CASE(EncodeZeroLengthTlv) |
| 110 | { |
| 111 | static const uint8_t expectedBlock[] = { |
| 112 | 0x64, 0x04, // LpPacket |
| 113 | 0xfd, 0x03, 0x4c, 0x00, // NonDiscovery |
| 114 | }; |
| 115 | |
| 116 | Packet packet1, packet2; |
| 117 | BOOST_CHECK_NO_THROW(packet1.set<NonDiscoveryField>(EmptyValue{})); |
| 118 | Block wire; |
| 119 | BOOST_REQUIRE_NO_THROW(wire = packet1.wireEncode()); |
| 120 | BOOST_CHECK_EQUAL_COLLECTIONS(expectedBlock, expectedBlock + sizeof(expectedBlock), |
| 121 | wire.begin(), wire.end()); |
| 122 | |
| 123 | BOOST_CHECK_NO_THROW(packet2.add<NonDiscoveryField>(EmptyValue{})); |
| 124 | BOOST_REQUIRE_NO_THROW(wire = packet2.wireEncode()); |
| 125 | BOOST_CHECK_EQUAL_COLLECTIONS(expectedBlock, expectedBlock + sizeof(expectedBlock), |
| 126 | wire.begin(), wire.end()); |
| 127 | } |
| 128 | |
Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 129 | BOOST_AUTO_TEST_CASE(EncodeSortOrder) |
| 130 | { |
| 131 | static const uint8_t expectedBlock[] = { |
Eric Newberry | 8422f57 | 2017-02-04 21:53:58 -0700 | [diff] [blame] | 132 | 0x64, 0x19, // LpPacket |
Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 133 | 0x52, 0x01, // FragIndex |
| 134 | 0x00, |
| 135 | 0x53, 0x01, // FragCount |
| 136 | 0x01, |
Eric Newberry | 8422f57 | 2017-02-04 21:53:58 -0700 | [diff] [blame] | 137 | 0xfd, 0x03, 0x44, 0x01, // Ack |
| 138 | 0x02, |
| 139 | 0xfd, 0x03, 0x44, 0x01, // Ack |
| 140 | 0x04, |
| 141 | 0xfd, 0x03, 0x44, 0x01, // Ack |
| 142 | 0x03, |
Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 143 | 0x50, 0x02, // Fragment |
| 144 | 0x03, 0xe8, |
| 145 | }; |
| 146 | |
| 147 | Buffer frag(2); |
| 148 | frag[0] = 0x03; |
| 149 | frag[1] = 0xe8; |
| 150 | |
| 151 | Packet packet; |
| 152 | BOOST_CHECK_NO_THROW(packet.add<FragmentField>(std::make_pair(frag.begin(), frag.end()))); |
| 153 | BOOST_CHECK_NO_THROW(packet.add<FragIndexField>(0)); |
Eric Newberry | 8422f57 | 2017-02-04 21:53:58 -0700 | [diff] [blame] | 154 | BOOST_CHECK_NO_THROW(packet.add<AckField>(2)); |
Eric Newberry | 2a89077 | 2017-06-26 12:06:15 -0700 | [diff] [blame] | 155 | BOOST_REQUIRE_NO_THROW(packet.wireEncode()); |
Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 156 | BOOST_CHECK_NO_THROW(packet.add<FragCountField>(1)); |
Eric Newberry | 2a89077 | 2017-06-26 12:06:15 -0700 | [diff] [blame] | 157 | BOOST_REQUIRE_NO_THROW(packet.wireEncode()); |
Eric Newberry | 8422f57 | 2017-02-04 21:53:58 -0700 | [diff] [blame] | 158 | BOOST_CHECK_NO_THROW(packet.add<AckField>(4)); |
Eric Newberry | 2a89077 | 2017-06-26 12:06:15 -0700 | [diff] [blame] | 159 | BOOST_REQUIRE_NO_THROW(packet.wireEncode()); |
Eric Newberry | 8422f57 | 2017-02-04 21:53:58 -0700 | [diff] [blame] | 160 | BOOST_CHECK_NO_THROW(packet.add<AckField>(3)); |
Eric Newberry | 2a89077 | 2017-06-26 12:06:15 -0700 | [diff] [blame] | 161 | BOOST_REQUIRE_NO_THROW(packet.wireEncode()); |
Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 162 | Block wire; |
| 163 | BOOST_REQUIRE_NO_THROW(wire = packet.wireEncode()); |
| 164 | BOOST_CHECK_EQUAL_COLLECTIONS(expectedBlock, expectedBlock + sizeof(expectedBlock), |
| 165 | wire.begin(), wire.end()); |
| 166 | } |
| 167 | |
| 168 | BOOST_AUTO_TEST_CASE(DecodeNormal) |
| 169 | { |
| 170 | static const uint8_t inputBlock[] = { |
| 171 | 0x64, 0x0a, // LpPacket |
| 172 | 0x52, 0x01, // FragIndex |
| 173 | 0x00, |
| 174 | 0x53, 0x01, // FragCount |
| 175 | 0x01, |
| 176 | 0x50, 0x02, // Fragment |
| 177 | 0x03, 0xe8, |
| 178 | }; |
| 179 | |
| 180 | Packet packet; |
| 181 | Block wire(inputBlock, sizeof(inputBlock)); |
| 182 | BOOST_CHECK_NO_THROW(packet.wireDecode(wire)); |
| 183 | BOOST_CHECK_EQUAL(1, packet.count<FragmentField>()); |
| 184 | BOOST_CHECK_EQUAL(1, packet.count<FragIndexField>()); |
| 185 | BOOST_CHECK_EQUAL(1, packet.count<FragCountField>()); |
| 186 | Buffer::const_iterator first, last; |
| 187 | BOOST_REQUIRE_NO_THROW(std::tie(first, last) = packet.get<FragmentField>(0)); |
| 188 | BOOST_CHECK_EQUAL(2, last - first); |
| 189 | BOOST_CHECK_EQUAL(0x03, *first); |
| 190 | BOOST_CHECK_EQUAL(0xe8, *(last - 1)); |
| 191 | BOOST_CHECK_EQUAL(0, packet.get<FragIndexField>(0)); |
| 192 | BOOST_CHECK_EQUAL(1, packet.get<FragCountField>(0)); |
| 193 | } |
| 194 | |
| 195 | BOOST_AUTO_TEST_CASE(DecodeIdle) |
| 196 | { |
| 197 | static const uint8_t inputBlock[] = { |
| 198 | 0x64, 0x06, // LpPacket |
| 199 | 0x52, 0x01, // FragIndex |
| 200 | 0x00, |
| 201 | 0x53, 0x01, // FragCount |
| 202 | 0x01, |
| 203 | }; |
| 204 | |
| 205 | Packet packet; |
| 206 | Block wire(inputBlock, sizeof(inputBlock)); |
| 207 | BOOST_CHECK_NO_THROW(packet.wireDecode(wire)); |
| 208 | BOOST_CHECK_EQUAL(0, packet.count<FragmentField>()); |
| 209 | BOOST_CHECK_EQUAL(1, packet.count<FragIndexField>()); |
| 210 | BOOST_CHECK_EQUAL(1, packet.count<FragCountField>()); |
| 211 | BOOST_CHECK_EQUAL(0, packet.get<FragIndexField>(0)); |
| 212 | BOOST_CHECK_EQUAL(1, packet.get<FragCountField>(0)); |
| 213 | } |
| 214 | |
| 215 | BOOST_AUTO_TEST_CASE(DecodeFragment) |
| 216 | { |
| 217 | static const uint8_t inputBlock[] = { |
| 218 | 0x64, 0x04, // LpPacket |
| 219 | 0x50, 0x02, // Fragment |
| 220 | 0x03, 0xe8, |
| 221 | }; |
| 222 | |
| 223 | Packet packet; |
| 224 | Block wire(inputBlock, sizeof(inputBlock)); |
| 225 | BOOST_CHECK_NO_THROW(packet.wireDecode(wire)); |
| 226 | BOOST_CHECK_EQUAL(1, packet.count<FragmentField>()); |
| 227 | BOOST_CHECK_EQUAL(0, packet.count<FragIndexField>()); |
| 228 | Buffer::const_iterator first, last; |
| 229 | BOOST_REQUIRE_NO_THROW(std::tie(first, last) = packet.get<FragmentField>(0)); |
| 230 | BOOST_CHECK_EQUAL(2, last - first); |
| 231 | BOOST_CHECK_EQUAL(0x03, *first); |
| 232 | BOOST_CHECK_EQUAL(0xe8, *(last - 1)); |
| 233 | } |
| 234 | |
Teng Liang | 0296074 | 2017-10-24 00:36:45 -0700 | [diff] [blame] | 235 | BOOST_AUTO_TEST_CASE(DecodeNonDiscoveryHeader) |
| 236 | { |
| 237 | static const uint8_t inputBlock[] = { |
| 238 | 0x64, 0x04, // LpPacket |
| 239 | 0xfd, 0x03, 0x4c, 0x00, // NonDiscovery |
| 240 | }; |
| 241 | |
| 242 | Packet packet; |
| 243 | Block wire(inputBlock, sizeof(inputBlock)); |
| 244 | BOOST_CHECK_NO_THROW(packet.wireDecode(wire)); |
| 245 | BOOST_CHECK_EQUAL(true, packet.has<NonDiscoveryField>()); |
| 246 | BOOST_CHECK_NO_THROW(packet.get<NonDiscoveryField>()); |
| 247 | } |
| 248 | |
Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 249 | BOOST_AUTO_TEST_CASE(DecodeEmpty) |
| 250 | { |
| 251 | static const uint8_t inputBlock[] = { |
| 252 | 0x64, 0x00, // LpPacket |
| 253 | }; |
| 254 | |
| 255 | Packet packet; |
| 256 | Block wire(inputBlock, sizeof(inputBlock)); |
| 257 | BOOST_CHECK_NO_THROW(packet.wireDecode(wire)); |
| 258 | BOOST_CHECK_EQUAL(0, packet.count<FragmentField>()); |
| 259 | BOOST_CHECK_EQUAL(0, packet.count<FragIndexField>()); |
Teng Liang | 0296074 | 2017-10-24 00:36:45 -0700 | [diff] [blame] | 260 | BOOST_CHECK_EQUAL(false, packet.has<NonDiscoveryField>()); |
Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | BOOST_AUTO_TEST_CASE(DecodeRepeatedNonRepeatableHeader) |
| 264 | { |
| 265 | static const uint8_t inputBlock[] = { |
| 266 | 0x64, 0x06, // LpPacket |
| 267 | 0x52, 0x01, // FragIndex |
| 268 | 0x00, |
| 269 | 0x52, 0x01, // FragIndex |
| 270 | 0x01, |
| 271 | }; |
| 272 | |
| 273 | Packet packet; |
| 274 | Block wire(inputBlock, sizeof(inputBlock)); |
| 275 | BOOST_CHECK_THROW(packet.wireDecode(wire), Packet::Error); |
| 276 | } |
| 277 | |
Eric Newberry | 8422f57 | 2017-02-04 21:53:58 -0700 | [diff] [blame] | 278 | BOOST_AUTO_TEST_CASE(DecodeRepeatedRepeatableHeader) |
| 279 | { |
| 280 | static const uint8_t inputBlock[] = { |
| 281 | 0x64, 0x0f, // LpPacket |
| 282 | 0xfd, 0x03, 0x44, 0x01, // Ack |
| 283 | 0x01, |
| 284 | 0xfd, 0x03, 0x44, 0x01, // Ack |
| 285 | 0x03, |
| 286 | 0xfd, 0x03, 0x44, 0x01, // Ack |
| 287 | 0x02, |
| 288 | }; |
| 289 | |
| 290 | Packet packet; |
| 291 | Block wire(inputBlock, sizeof(inputBlock)); |
| 292 | BOOST_CHECK_NO_THROW(packet.wireDecode(wire)); |
| 293 | BOOST_REQUIRE_EQUAL(packet.count<AckField>(), 3); |
| 294 | BOOST_CHECK_EQUAL(packet.get<AckField>(), 1); |
| 295 | BOOST_CHECK_EQUAL(packet.get<AckField>(0), 1); |
| 296 | BOOST_CHECK_EQUAL(packet.get<AckField>(1), 3); |
| 297 | BOOST_CHECK_EQUAL(packet.get<AckField>(2), 2); |
| 298 | } |
| 299 | |
Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 300 | BOOST_AUTO_TEST_CASE(DecodeRepeatedFragment) |
| 301 | { |
| 302 | static const uint8_t inputBlock[] = { |
| 303 | 0x64, 0x08, // LpPacket |
| 304 | 0x50, 0x02, // Fragment |
| 305 | 0x03, 0xe8, |
| 306 | 0x50, 0x02, // Fragment |
| 307 | 0x03, 0xe9, |
| 308 | }; |
| 309 | |
| 310 | Packet packet; |
| 311 | Block wire(inputBlock, sizeof(inputBlock)); |
| 312 | BOOST_CHECK_THROW(packet.wireDecode(wire), Packet::Error); |
| 313 | } |
| 314 | |
| 315 | BOOST_AUTO_TEST_CASE(DecodeWrongOrderAmongHeaders) |
| 316 | { |
| 317 | static const uint8_t inputBlock[] = { |
| 318 | 0x64, 0x0a, // LpPacket |
| 319 | 0x53, 0x01, // FragCount |
| 320 | 0x01, |
| 321 | 0x52, 0x01, // FragIndex |
| 322 | 0x00, |
| 323 | 0x50, 0x02, // Fragment |
| 324 | 0x03, 0xe8, |
| 325 | }; |
| 326 | |
| 327 | Packet packet; |
| 328 | Block wire(inputBlock, sizeof(inputBlock)); |
| 329 | BOOST_CHECK_THROW(packet.wireDecode(wire), Packet::Error); |
| 330 | } |
| 331 | |
| 332 | BOOST_AUTO_TEST_CASE(DecodeWrongOrderFragment) |
| 333 | { |
| 334 | static const uint8_t inputBlock[] = { |
| 335 | 0x64, 0x0a, // LpPacket |
| 336 | 0x52, 0x01, // FragIndex |
| 337 | 0x00, |
| 338 | 0x50, 0x02, // Fragment |
| 339 | 0x03, 0xe8, |
| 340 | 0x53, 0x01, // FragCount |
| 341 | 0x01, |
| 342 | }; |
| 343 | |
| 344 | Packet packet; |
| 345 | Block wire(inputBlock, sizeof(inputBlock)); |
| 346 | BOOST_CHECK_THROW(packet.wireDecode(wire), Packet::Error); |
| 347 | } |
| 348 | |
| 349 | BOOST_AUTO_TEST_CASE(DecodeIgnoredHeader) |
| 350 | { |
| 351 | static const uint8_t inputBlock[] = { |
| 352 | 0x64, 0x0c, // LpPacket |
| 353 | 0x52, 0x01, // FragIndex |
| 354 | 0x00, |
Eric Newberry | 3ed6247 | 2016-12-11 22:11:38 -0700 | [diff] [blame] | 355 | 0xfd, 0x03, 0x24, 0x01, // unknown TLV-TYPE 804 (ignored) |
Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 356 | 0x02, |
| 357 | 0x50, 0x02, // Fragment |
| 358 | 0x03, 0xe8, |
| 359 | }; |
| 360 | |
| 361 | Packet packet; |
| 362 | Block wire(inputBlock, sizeof(inputBlock)); |
| 363 | BOOST_CHECK_NO_THROW(packet.wireDecode(wire)); |
| 364 | BOOST_CHECK_EQUAL(1, packet.count<FragmentField>()); |
| 365 | BOOST_CHECK_EQUAL(1, packet.count<FragIndexField>()); |
| 366 | } |
| 367 | |
| 368 | BOOST_AUTO_TEST_CASE(DecodeUnrecognizedHeader) |
| 369 | { |
| 370 | static const uint8_t inputBlock[] = { |
| 371 | 0x64, 0x0c, // LpPacket |
| 372 | 0x52, 0x01, // FragIndex |
| 373 | 0x00, |
| 374 | 0xfd, 0x03, 0x22, 0x01, // unknown TLV-TYPE 802 (cannot ignore) |
| 375 | 0x02, |
| 376 | 0x50, 0x02, // Fragment |
| 377 | 0x03, 0xe8, |
| 378 | }; |
| 379 | |
| 380 | Packet packet; |
| 381 | Block wire(inputBlock, sizeof(inputBlock)); |
| 382 | BOOST_CHECK_THROW(packet.wireDecode(wire), Packet::Error); |
| 383 | } |
| 384 | |
| 385 | BOOST_AUTO_TEST_CASE(DecodeBareNetworkLayerPacket) |
| 386 | { |
| 387 | static const uint8_t inputBlock[] = { |
| 388 | 0x05, 0x0a, // Interest |
| 389 | 0x07, 0x02, // Name |
| 390 | 0x03, 0xe8, |
| 391 | 0x0a, 0x04, // Nonce |
| 392 | 0x01, 0x02, 0x03, 0x04, |
| 393 | }; |
| 394 | |
| 395 | Packet packet; |
| 396 | Block wire(inputBlock, sizeof(inputBlock)); |
| 397 | BOOST_CHECK_NO_THROW(packet.wireDecode(wire)); |
| 398 | BOOST_CHECK_EQUAL(1, packet.count<FragmentField>()); |
| 399 | |
Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 400 | Block encoded; |
| 401 | BOOST_CHECK_NO_THROW(encoded = packet.wireEncode()); |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 402 | BOOST_CHECK_EQUAL_COLLECTIONS(inputBlock, inputBlock + sizeof(inputBlock), |
Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 403 | encoded.begin(), encoded.end()); |
| 404 | } |
| 405 | |
Teng Liang | 5b323d1 | 2018-01-31 18:50:45 -0700 | [diff] [blame] | 406 | BOOST_AUTO_TEST_CASE(DecodePrefixAnnouncement) |
| 407 | { |
| 408 | static const uint8_t inputBlock[] = { |
| 409 | 0x64, 0x70, // LpPacket |
| 410 | 0xfd, 0x03, 0x50, 0x3a, // PrefixAnnouncement |
| 411 | 0x06, 0x38, // Data |
| 412 | 0x07, 0x29, 0x08, 0x0d, 0x73, 0x65, 0x6c, 0x66, 0x2d, 0x6c, |
| 413 | 0x65, 0x61, 0x72, 0x6e, 0x69, 0x6e, 0x67, 0x08, 0x03, 0x65, |
| 414 | 0x64, 0x75, 0x08, 0x02, 0x75, 0x61, 0x08, 0x02, 0x63, 0x73, |
| 415 | 0x08, 0x04, 0x6e, 0x65, 0x77, 0x73, 0x08, 0x05, 0xfd, 0x00, |
| 416 | 0x03, 0xa5, 0xfe, 0x14, 0x00, 0x15, 0x00, 0x16, 0x05, 0x1b, |
| 417 | 0x01, 0x01, 0x1c, 0x00, 0x17, 0x00, |
| 418 | 0x50, 0x30, // Fragment |
| 419 | 0x06, 0x2e, // Data |
| 420 | 0x07, 0x1f, 0x08, 0x03, 0x65, 0x64, 0x75, 0x08, 0x02, 0x75, |
| 421 | 0x61, 0x08, 0x02, 0x63, 0x73, 0x08, 0x04, 0x6e, 0x65, 0x77, |
| 422 | 0x73, 0x08, 0x0a, 0x69, 0x6e, 0x64, 0x65, 0x78, 0x2e, 0x68, |
| 423 | 0x74, 0x6d, 0x6c, 0x14, 0x00, 0x15, 0x00, 0x16, 0x05, 0x1b, |
| 424 | 0x01, 0x01, 0x1c, 0x00, 0x17, 0x00, |
| 425 | }; |
| 426 | |
| 427 | Data data1("/edu/ua/cs/news/index.html"); |
| 428 | ndn::SignatureSha256WithRsa fakeSignature; |
| 429 | fakeSignature.setValue(ndn::encoding::makeEmptyBlock(ndn::tlv::SignatureValue)); |
| 430 | data1.setSignature(fakeSignature); |
| 431 | |
| 432 | Block wire; |
| 433 | wire = data1.wireEncode(); |
| 434 | Packet packet; |
| 435 | BOOST_CHECK_NO_THROW(packet.wireDecode(wire)); |
| 436 | |
| 437 | Name name("/self-learning/edu/ua/cs/news"); |
| 438 | name.appendVersion(239102); |
| 439 | Data data2(name); |
| 440 | fakeSignature.setValue(ndn::encoding::makeEmptyBlock(ndn::tlv::SignatureValue)); |
| 441 | data2.setSignature(fakeSignature); |
| 442 | data2.wireEncode(); |
| 443 | |
| 444 | PrefixAnnouncement pa; |
| 445 | pa.setData(make_shared<Data>(data2)); |
| 446 | |
| 447 | BOOST_CHECK_NO_THROW(packet.add<PrefixAnnouncementField>(pa)); |
| 448 | Block encoded; |
| 449 | BOOST_CHECK_NO_THROW(encoded = packet.wireEncode()); |
| 450 | BOOST_CHECK_EQUAL_COLLECTIONS(inputBlock, inputBlock + sizeof(inputBlock), |
| 451 | encoded.begin(), encoded.end()); |
| 452 | } |
| 453 | |
Eric Newberry | 43bf6bb | 2015-10-09 16:12:09 -0700 | [diff] [blame] | 454 | BOOST_AUTO_TEST_CASE(DecodeUnrecognizedTlvType) |
| 455 | { |
| 456 | Packet packet; |
| 457 | Block wire = encoding::makeEmptyBlock(ndn::tlv::Name); |
| 458 | BOOST_CHECK_THROW(packet.wireDecode(wire), Packet::Error); |
| 459 | } |
| 460 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 461 | BOOST_AUTO_TEST_SUITE_END() // TestPacket |
| 462 | BOOST_AUTO_TEST_SUITE_END() // Lp |
Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 463 | |
| 464 | } // namespace tests |
| 465 | } // namespace lp |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 466 | } // namespace ndn |