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