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