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