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