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 | /* |
Eric Newberry | a3c8bd1 | 2020-05-15 17:27:07 -0700 | [diff] [blame] | 3 | * Copyright (c) 2013-2020 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 | |
| 28 | namespace ndn { |
| 29 | namespace lp { |
| 30 | namespace tests { |
| 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; |
Junxiao Shi | 974b81a | 2018-04-21 01:37:03 +0000 | [diff] [blame] | 98 | packet.add<FragmentField>(std::make_pair(buf.begin(), buf.end())); |
| 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; |
Junxiao Shi | c53df03 | 2019-01-14 23:33:25 +0000 | [diff] [blame] | 167 | packet.add<FragmentField>(std::make_pair(frag.begin(), frag.end())); |
| 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; |
| 200 | Block wire(inputBlock, sizeof(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); |
Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 216 | Buffer::const_iterator first, last; |
Junxiao Shi | c53df03 | 2019-01-14 23:33:25 +0000 | [diff] [blame] | 217 | std::tie(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; |
| 234 | Block wire(inputBlock, sizeof(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; |
| 252 | Block wire(inputBlock, sizeof(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>()); |
| 256 | Buffer::const_iterator first, last; |
Junxiao Shi | c53df03 | 2019-01-14 23:33:25 +0000 | [diff] [blame] | 257 | std::tie(first, last) = packet.get<FragmentField>(0); |
Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 258 | BOOST_CHECK_EQUAL(2, last - first); |
| 259 | BOOST_CHECK_EQUAL(0x03, *first); |
| 260 | BOOST_CHECK_EQUAL(0xe8, *(last - 1)); |
| 261 | } |
| 262 | |
Teng Liang | 0296074 | 2017-10-24 00:36:45 -0700 | [diff] [blame] | 263 | BOOST_AUTO_TEST_CASE(DecodeNonDiscoveryHeader) |
| 264 | { |
| 265 | static const uint8_t inputBlock[] = { |
| 266 | 0x64, 0x04, // LpPacket |
| 267 | 0xfd, 0x03, 0x4c, 0x00, // NonDiscovery |
| 268 | }; |
| 269 | |
| 270 | Packet packet; |
| 271 | Block wire(inputBlock, sizeof(inputBlock)); |
Junxiao Shi | c53df03 | 2019-01-14 23:33:25 +0000 | [diff] [blame] | 272 | packet.wireDecode(wire); |
Teng Liang | 0296074 | 2017-10-24 00:36:45 -0700 | [diff] [blame] | 273 | BOOST_CHECK_EQUAL(true, packet.has<NonDiscoveryField>()); |
Junxiao Shi | c53df03 | 2019-01-14 23:33:25 +0000 | [diff] [blame] | 274 | packet.get<NonDiscoveryField>(); |
Teng Liang | 0296074 | 2017-10-24 00:36:45 -0700 | [diff] [blame] | 275 | } |
| 276 | |
Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 277 | BOOST_AUTO_TEST_CASE(DecodeEmpty) |
| 278 | { |
| 279 | static const uint8_t inputBlock[] = { |
| 280 | 0x64, 0x00, // LpPacket |
| 281 | }; |
| 282 | |
| 283 | Packet packet; |
| 284 | Block wire(inputBlock, sizeof(inputBlock)); |
Junxiao Shi | c53df03 | 2019-01-14 23:33:25 +0000 | [diff] [blame] | 285 | packet.wireDecode(wire); |
Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 286 | BOOST_CHECK_EQUAL(0, packet.count<FragmentField>()); |
| 287 | BOOST_CHECK_EQUAL(0, packet.count<FragIndexField>()); |
Teng Liang | 0296074 | 2017-10-24 00:36:45 -0700 | [diff] [blame] | 288 | BOOST_CHECK_EQUAL(false, packet.has<NonDiscoveryField>()); |
Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | BOOST_AUTO_TEST_CASE(DecodeRepeatedNonRepeatableHeader) |
| 292 | { |
| 293 | static const uint8_t inputBlock[] = { |
| 294 | 0x64, 0x06, // LpPacket |
| 295 | 0x52, 0x01, // FragIndex |
| 296 | 0x00, |
| 297 | 0x52, 0x01, // FragIndex |
| 298 | 0x01, |
| 299 | }; |
| 300 | |
| 301 | Packet packet; |
| 302 | Block wire(inputBlock, sizeof(inputBlock)); |
| 303 | BOOST_CHECK_THROW(packet.wireDecode(wire), Packet::Error); |
| 304 | } |
| 305 | |
| 306 | BOOST_AUTO_TEST_CASE(DecodeRepeatedFragment) |
| 307 | { |
| 308 | static const uint8_t inputBlock[] = { |
| 309 | 0x64, 0x08, // LpPacket |
| 310 | 0x50, 0x02, // Fragment |
| 311 | 0x03, 0xe8, |
| 312 | 0x50, 0x02, // Fragment |
| 313 | 0x03, 0xe9, |
| 314 | }; |
| 315 | |
| 316 | Packet packet; |
| 317 | Block wire(inputBlock, sizeof(inputBlock)); |
| 318 | BOOST_CHECK_THROW(packet.wireDecode(wire), Packet::Error); |
| 319 | } |
| 320 | |
| 321 | BOOST_AUTO_TEST_CASE(DecodeWrongOrderAmongHeaders) |
| 322 | { |
| 323 | static const uint8_t inputBlock[] = { |
| 324 | 0x64, 0x0a, // LpPacket |
| 325 | 0x53, 0x01, // FragCount |
| 326 | 0x01, |
| 327 | 0x52, 0x01, // FragIndex |
| 328 | 0x00, |
| 329 | 0x50, 0x02, // Fragment |
| 330 | 0x03, 0xe8, |
| 331 | }; |
| 332 | |
| 333 | Packet packet; |
| 334 | Block wire(inputBlock, sizeof(inputBlock)); |
| 335 | BOOST_CHECK_THROW(packet.wireDecode(wire), Packet::Error); |
| 336 | } |
| 337 | |
| 338 | BOOST_AUTO_TEST_CASE(DecodeWrongOrderFragment) |
| 339 | { |
| 340 | static const uint8_t inputBlock[] = { |
| 341 | 0x64, 0x0a, // LpPacket |
| 342 | 0x52, 0x01, // FragIndex |
| 343 | 0x00, |
| 344 | 0x50, 0x02, // Fragment |
| 345 | 0x03, 0xe8, |
| 346 | 0x53, 0x01, // FragCount |
| 347 | 0x01, |
| 348 | }; |
| 349 | |
| 350 | Packet packet; |
| 351 | Block wire(inputBlock, sizeof(inputBlock)); |
| 352 | BOOST_CHECK_THROW(packet.wireDecode(wire), Packet::Error); |
| 353 | } |
| 354 | |
| 355 | BOOST_AUTO_TEST_CASE(DecodeIgnoredHeader) |
| 356 | { |
| 357 | static const uint8_t inputBlock[] = { |
| 358 | 0x64, 0x0c, // LpPacket |
| 359 | 0x52, 0x01, // FragIndex |
| 360 | 0x00, |
Eric Newberry | 3ed6247 | 2016-12-11 22:11:38 -0700 | [diff] [blame] | 361 | 0xfd, 0x03, 0x24, 0x01, // unknown TLV-TYPE 804 (ignored) |
Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 362 | 0x02, |
| 363 | 0x50, 0x02, // Fragment |
| 364 | 0x03, 0xe8, |
| 365 | }; |
| 366 | |
| 367 | Packet packet; |
| 368 | Block wire(inputBlock, sizeof(inputBlock)); |
Junxiao Shi | c53df03 | 2019-01-14 23:33:25 +0000 | [diff] [blame] | 369 | packet.wireDecode(wire); |
Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 370 | BOOST_CHECK_EQUAL(1, packet.count<FragmentField>()); |
| 371 | BOOST_CHECK_EQUAL(1, packet.count<FragIndexField>()); |
| 372 | } |
| 373 | |
| 374 | BOOST_AUTO_TEST_CASE(DecodeUnrecognizedHeader) |
| 375 | { |
| 376 | static const uint8_t inputBlock[] = { |
| 377 | 0x64, 0x0c, // LpPacket |
| 378 | 0x52, 0x01, // FragIndex |
| 379 | 0x00, |
| 380 | 0xfd, 0x03, 0x22, 0x01, // unknown TLV-TYPE 802 (cannot ignore) |
| 381 | 0x02, |
| 382 | 0x50, 0x02, // Fragment |
| 383 | 0x03, 0xe8, |
| 384 | }; |
| 385 | |
| 386 | Packet packet; |
| 387 | Block wire(inputBlock, sizeof(inputBlock)); |
| 388 | BOOST_CHECK_THROW(packet.wireDecode(wire), Packet::Error); |
| 389 | } |
| 390 | |
| 391 | BOOST_AUTO_TEST_CASE(DecodeBareNetworkLayerPacket) |
| 392 | { |
| 393 | static const uint8_t inputBlock[] = { |
| 394 | 0x05, 0x0a, // Interest |
| 395 | 0x07, 0x02, // Name |
| 396 | 0x03, 0xe8, |
| 397 | 0x0a, 0x04, // Nonce |
| 398 | 0x01, 0x02, 0x03, 0x04, |
| 399 | }; |
| 400 | |
| 401 | Packet packet; |
| 402 | Block wire(inputBlock, sizeof(inputBlock)); |
Junxiao Shi | c53df03 | 2019-01-14 23:33:25 +0000 | [diff] [blame] | 403 | packet.wireDecode(wire); |
Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 404 | BOOST_CHECK_EQUAL(1, packet.count<FragmentField>()); |
| 405 | |
Junxiao Shi | c53df03 | 2019-01-14 23:33:25 +0000 | [diff] [blame] | 406 | Block encoded = packet.wireEncode(); |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 407 | BOOST_CHECK_EQUAL_COLLECTIONS(inputBlock, inputBlock + sizeof(inputBlock), |
Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 408 | encoded.begin(), encoded.end()); |
| 409 | } |
| 410 | |
Junxiao Shi | c53df03 | 2019-01-14 23:33:25 +0000 | [diff] [blame] | 411 | BOOST_AUTO_TEST_CASE(DecodeSeqNum) |
| 412 | { |
| 413 | Packet packet; |
| 414 | |
| 415 | // Sequence number is fixed-width, not NonNegativeInteger |
| 416 | packet.wireDecode("640A 5104A4A5A6A7 5002FFFF"_block); |
| 417 | BOOST_CHECK_THROW(packet.get<SequenceField>(), ndn::tlv::Error); |
| 418 | |
| 419 | packet.wireDecode("640E 5108A0A1A2A3A4A5A6A7 5002FFFF"_block); |
| 420 | BOOST_CHECK_EQUAL(packet.get<SequenceField>(), 0xA0A1A2A3A4A5A6A7); |
| 421 | } |
| 422 | |
Eric Newberry | 43bf6bb | 2015-10-09 16:12:09 -0700 | [diff] [blame] | 423 | BOOST_AUTO_TEST_CASE(DecodeUnrecognizedTlvType) |
| 424 | { |
| 425 | Packet packet; |
| 426 | Block wire = encoding::makeEmptyBlock(ndn::tlv::Name); |
| 427 | BOOST_CHECK_THROW(packet.wireDecode(wire), Packet::Error); |
| 428 | } |
| 429 | |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 430 | BOOST_FIXTURE_TEST_CASE(DecodePrefixAnnouncement, ndn::tests::KeyChainFixture) |
Teng Liang | e3ecad7 | 2018-08-28 21:12:53 -0700 | [diff] [blame] | 431 | { |
| 432 | // Construct Data which prefix announcement is attached to |
Davide Pesavento | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 433 | auto data0 = ndn::tests::makeData("/edu/ua/cs/news/index.html"); |
Teng Liang | e3ecad7 | 2018-08-28 21:12:53 -0700 | [diff] [blame] | 434 | |
Teng Liang | e3ecad7 | 2018-08-28 21:12:53 -0700 | [diff] [blame] | 435 | Packet packet0; |
Davide Pesavento | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 436 | packet0.wireDecode(data0->wireEncode()); |
Teng Liang | e3ecad7 | 2018-08-28 21:12:53 -0700 | [diff] [blame] | 437 | |
| 438 | // Construct Prefix Announcement |
| 439 | PrefixAnnouncement pa; |
| 440 | pa.setAnnouncedName("/net/example"); |
| 441 | pa.setExpiration(5_min); |
| 442 | pa.setValidityPeriod(security::ValidityPeriod(time::fromIsoString("20181030T000000"), |
| 443 | time::fromIsoString("20181124T235959"))); |
| 444 | pa.toData(m_keyChain, signingWithSha256(), 1); |
| 445 | PrefixAnnouncementHeader pah0(pa); |
Junxiao Shi | c53df03 | 2019-01-14 23:33:25 +0000 | [diff] [blame] | 446 | packet0.add<PrefixAnnouncementField>(pah0); |
| 447 | Block encoded = packet0.wireEncode(); |
Teng Liang | e3ecad7 | 2018-08-28 21:12:53 -0700 | [diff] [blame] | 448 | |
| 449 | // check decoding |
| 450 | Packet packet1; |
Junxiao Shi | c53df03 | 2019-01-14 23:33:25 +0000 | [diff] [blame] | 451 | packet1.wireDecode(encoded); |
Teng Liang | e3ecad7 | 2018-08-28 21:12:53 -0700 | [diff] [blame] | 452 | BOOST_CHECK_EQUAL(true, packet1.has<PrefixAnnouncementField>()); |
Junxiao Shi | c53df03 | 2019-01-14 23:33:25 +0000 | [diff] [blame] | 453 | PrefixAnnouncementHeader pah1 = packet1.get<PrefixAnnouncementField>(); |
Teng Liang | e3ecad7 | 2018-08-28 21:12:53 -0700 | [diff] [blame] | 454 | BOOST_CHECK_EQUAL(pah1.getPrefixAnn()->getAnnouncedName(), "/net/example"); |
| 455 | } |
| 456 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 457 | BOOST_AUTO_TEST_SUITE_END() // TestPacket |
| 458 | BOOST_AUTO_TEST_SUITE_END() // Lp |
Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 459 | |
| 460 | } // namespace tests |
| 461 | } // namespace lp |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 462 | } // namespace ndn |