Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | 9a63bf2 | 2023-11-11 17:12:51 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2023, Regents of the University of California, |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis. |
| 10 | * |
| 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 24 | */ |
| 25 | |
| 26 | #include "face/lp-fragmenter.hpp" |
| 27 | |
| 28 | #include "tests/test-common.hpp" |
| 29 | |
Davide Pesavento | 9a63bf2 | 2023-11-11 17:12:51 -0500 | [diff] [blame] | 30 | #include <ndn-cxx/lp/fields.hpp> |
| 31 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 32 | namespace nfd::tests { |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 33 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 34 | using namespace nfd::face; |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 35 | |
Davide Pesavento | b7e72c3 | 2020-10-02 20:00:03 -0400 | [diff] [blame] | 36 | class LpFragmenterFixture |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 37 | { |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 38 | protected: |
| 39 | LpFragmenter fragmenter{{}}; |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 40 | }; |
| 41 | |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 42 | BOOST_AUTO_TEST_SUITE(Face) |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 43 | BOOST_FIXTURE_TEST_SUITE(TestLpFragmenter, LpFragmenterFixture) |
| 44 | |
Davide Pesavento | b7e72c3 | 2020-10-02 20:00:03 -0400 | [diff] [blame] | 45 | BOOST_AUTO_TEST_CASE(SingleFragment) |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 46 | { |
Davide Pesavento | b7e72c3 | 2020-10-02 20:00:03 -0400 | [diff] [blame] | 47 | const size_t mtu = 256; |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 48 | |
| 49 | lp::Packet packet; |
| 50 | packet.add<lp::IncomingFaceIdField>(123); |
| 51 | |
Davide Pesavento | 5bafd2c | 2020-07-28 16:16:27 -0400 | [diff] [blame] | 52 | auto data = makeData("/test/data123"); |
Davide Pesavento | 5bafd2c | 2020-07-28 16:16:27 -0400 | [diff] [blame] | 53 | packet.add<lp::FragmentField>({data->wireEncode().begin(), data->wireEncode().end()}); |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 54 | |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 55 | auto [isOk, frags] = fragmenter.fragmentPacket(packet, mtu); |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 56 | BOOST_REQUIRE(isOk); |
| 57 | BOOST_REQUIRE_EQUAL(frags.size(), 1); |
Davide Pesavento | b7e72c3 | 2020-10-02 20:00:03 -0400 | [diff] [blame] | 58 | |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 59 | BOOST_CHECK(frags[0].has<lp::FragmentField>()); |
| 60 | BOOST_CHECK_EQUAL(frags[0].get<lp::IncomingFaceIdField>(), 123); |
| 61 | BOOST_CHECK(!frags[0].has<lp::FragIndexField>()); |
| 62 | BOOST_CHECK(!frags[0].has<lp::FragCountField>()); |
| 63 | BOOST_CHECK_LE(frags[0].wireEncode().size(), mtu); |
| 64 | |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 65 | auto [fragBegin, fragEnd] = frags[0].get<lp::FragmentField>(); |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 66 | BOOST_CHECK_EQUAL_COLLECTIONS(data->wireEncode().begin(), data->wireEncode().end(), |
| 67 | fragBegin, fragEnd); |
| 68 | } |
| 69 | |
Davide Pesavento | b7e72c3 | 2020-10-02 20:00:03 -0400 | [diff] [blame] | 70 | BOOST_AUTO_TEST_CASE(MultipleFragments) |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 71 | { |
Davide Pesavento | b7e72c3 | 2020-10-02 20:00:03 -0400 | [diff] [blame] | 72 | const size_t mtu = MIN_MTU; |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 73 | |
| 74 | lp::Packet packet; |
| 75 | packet.add<lp::IncomingFaceIdField>(123); |
| 76 | |
Davide Pesavento | 5bafd2c | 2020-07-28 16:16:27 -0400 | [diff] [blame] | 77 | auto data = makeData("/test/data123/123456789/987654321/123456789"); |
Davide Pesavento | 5bafd2c | 2020-07-28 16:16:27 -0400 | [diff] [blame] | 78 | packet.add<lp::FragmentField>({data->wireEncode().begin(), data->wireEncode().end()}); |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 79 | |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 80 | auto [isOk, frags] = fragmenter.fragmentPacket(packet, mtu); |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 81 | BOOST_REQUIRE(isOk); |
Eric Newberry | 812d615 | 2018-06-06 15:06:01 -0700 | [diff] [blame] | 82 | BOOST_REQUIRE_EQUAL(frags.size(), 5); |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 83 | |
Davide Pesavento | b7e72c3 | 2020-10-02 20:00:03 -0400 | [diff] [blame] | 84 | ndn::Buffer reassembledPayload(data->wireEncode().size()); |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 85 | |
| 86 | BOOST_CHECK(frags[0].has<lp::FragmentField>()); |
| 87 | BOOST_CHECK_EQUAL(frags[0].get<lp::IncomingFaceIdField>(), 123); |
| 88 | BOOST_CHECK_EQUAL(frags[0].get<lp::FragIndexField>(), 0); |
Eric Newberry | 812d615 | 2018-06-06 15:06:01 -0700 | [diff] [blame] | 89 | BOOST_CHECK_EQUAL(frags[0].get<lp::FragCountField>(), 5); |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 90 | BOOST_CHECK_LE(frags[0].wireEncode().size(), mtu); |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 91 | auto [frag0Begin, frag0End] = frags[0].get<lp::FragmentField>(); |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 92 | BOOST_REQUIRE_LE(std::distance(frag0Begin, frag0End), reassembledPayload.size()); |
| 93 | auto reassembledPos = std::copy(frag0Begin, frag0End, reassembledPayload.begin()); |
| 94 | |
| 95 | BOOST_CHECK(frags[1].has<lp::FragmentField>()); |
| 96 | BOOST_CHECK(!frags[1].has<lp::IncomingFaceIdField>()); |
| 97 | BOOST_CHECK_EQUAL(frags[1].get<lp::FragIndexField>(), 1); |
Eric Newberry | 812d615 | 2018-06-06 15:06:01 -0700 | [diff] [blame] | 98 | BOOST_CHECK_EQUAL(frags[1].get<lp::FragCountField>(), 5); |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 99 | BOOST_CHECK_LE(frags[1].wireEncode().size(), mtu); |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 100 | auto [frag1Begin, frag1End] = frags[1].get<lp::FragmentField>(); |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 101 | BOOST_REQUIRE_LE(std::distance(frag1Begin, frag1End), |
| 102 | std::distance(reassembledPos, reassembledPayload.end())); |
Eric Newberry | 812d615 | 2018-06-06 15:06:01 -0700 | [diff] [blame] | 103 | reassembledPos = std::copy(frag1Begin, frag1End, reassembledPos); |
| 104 | |
| 105 | BOOST_CHECK(frags[2].has<lp::FragmentField>()); |
| 106 | BOOST_CHECK(!frags[2].has<lp::IncomingFaceIdField>()); |
| 107 | BOOST_CHECK_EQUAL(frags[2].get<lp::FragIndexField>(), 2); |
| 108 | BOOST_CHECK_EQUAL(frags[2].get<lp::FragCountField>(), 5); |
| 109 | BOOST_CHECK_LE(frags[2].wireEncode().size(), mtu); |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 110 | auto [frag2Begin, frag2End] = frags[2].get<lp::FragmentField>(); |
Eric Newberry | 812d615 | 2018-06-06 15:06:01 -0700 | [diff] [blame] | 111 | BOOST_REQUIRE_LE(std::distance(frag2Begin, frag2End), |
| 112 | std::distance(reassembledPos, reassembledPayload.end())); |
| 113 | reassembledPos = std::copy(frag2Begin, frag2End, reassembledPos); |
| 114 | |
| 115 | BOOST_CHECK(frags[3].has<lp::FragmentField>()); |
| 116 | BOOST_CHECK(!frags[3].has<lp::IncomingFaceIdField>()); |
| 117 | BOOST_CHECK_EQUAL(frags[3].get<lp::FragIndexField>(), 3); |
| 118 | BOOST_CHECK_EQUAL(frags[3].get<lp::FragCountField>(), 5); |
| 119 | BOOST_CHECK_LE(frags[3].wireEncode().size(), mtu); |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 120 | auto [frag3Begin, frag3End] = frags[3].get<lp::FragmentField>(); |
Eric Newberry | 812d615 | 2018-06-06 15:06:01 -0700 | [diff] [blame] | 121 | BOOST_REQUIRE_LE(std::distance(frag3Begin, frag3End), |
| 122 | std::distance(reassembledPos, reassembledPayload.end())); |
| 123 | reassembledPos = std::copy(frag3Begin, frag3End, reassembledPos); |
| 124 | |
| 125 | BOOST_CHECK(frags[4].has<lp::FragmentField>()); |
| 126 | BOOST_CHECK(!frags[4].has<lp::IncomingFaceIdField>()); |
| 127 | BOOST_CHECK_EQUAL(frags[4].get<lp::FragIndexField>(), 4); |
| 128 | BOOST_CHECK_EQUAL(frags[4].get<lp::FragCountField>(), 5); |
| 129 | BOOST_CHECK_LE(frags[4].wireEncode().size(), mtu); |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 130 | auto [frag4Begin, frag4End] = frags[4].get<lp::FragmentField>(); |
Eric Newberry | 812d615 | 2018-06-06 15:06:01 -0700 | [diff] [blame] | 131 | BOOST_REQUIRE_LE(std::distance(frag4Begin, frag4End), |
| 132 | std::distance(reassembledPos, reassembledPayload.end())); |
| 133 | std::copy(frag4Begin, frag4End, reassembledPos); |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 134 | |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 135 | BOOST_TEST(data->wireEncode() == reassembledPayload, boost::test_tools::per_element()); |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Davide Pesavento | b7e72c3 | 2020-10-02 20:00:03 -0400 | [diff] [blame] | 138 | BOOST_AUTO_TEST_CASE(MtuTooSmall) |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 139 | { |
Davide Pesavento | b7e72c3 | 2020-10-02 20:00:03 -0400 | [diff] [blame] | 140 | const size_t mtu = 20; |
Eric Newberry | cb6551e | 2020-03-02 14:12:16 -0800 | [diff] [blame] | 141 | BOOST_ASSERT(mtu < MIN_MTU); |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 142 | |
| 143 | lp::Packet packet; |
| 144 | packet.add<lp::IncomingFaceIdField>(123); |
| 145 | |
Davide Pesavento | 5bafd2c | 2020-07-28 16:16:27 -0400 | [diff] [blame] | 146 | auto data = makeData("/test/data123/123456789/987654321/123456789"); |
| 147 | packet.add<lp::FragmentField>({data->wireEncode().begin(), data->wireEncode().end()}); |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 148 | |
| 149 | bool isOk = false; |
| 150 | std::tie(isOk, std::ignore) = fragmenter.fragmentPacket(packet, mtu); |
Davide Pesavento | 5bafd2c | 2020-07-28 16:16:27 -0400 | [diff] [blame] | 151 | BOOST_CHECK_EQUAL(isOk, false); |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 152 | } |
| 153 | |
Davide Pesavento | b7e72c3 | 2020-10-02 20:00:03 -0400 | [diff] [blame] | 154 | BOOST_AUTO_TEST_CASE(FragCountOverLimit) |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 155 | { |
| 156 | LpFragmenter::Options options; |
| 157 | options.nMaxFragments = 2; |
| 158 | fragmenter.setOptions(options); |
| 159 | |
Davide Pesavento | b7e72c3 | 2020-10-02 20:00:03 -0400 | [diff] [blame] | 160 | const size_t mtu = 70; |
| 161 | BOOST_ASSERT(mtu >= MIN_MTU); |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 162 | |
| 163 | lp::Packet packet; |
| 164 | packet.add<lp::IncomingFaceIdField>(123); |
| 165 | |
Davide Pesavento | 5bafd2c | 2020-07-28 16:16:27 -0400 | [diff] [blame] | 166 | auto data = makeData("/test/data123/123456789/987654321/123456789"); |
| 167 | packet.add<lp::FragmentField>({data->wireEncode().begin(), data->wireEncode().end()}); |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 168 | |
| 169 | bool isOk = false; |
| 170 | std::tie(isOk, std::ignore) = fragmenter.fragmentPacket(packet, mtu); |
Davide Pesavento | 5bafd2c | 2020-07-28 16:16:27 -0400 | [diff] [blame] | 171 | BOOST_CHECK_EQUAL(isOk, false); |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | BOOST_AUTO_TEST_SUITE_END() // TestLpFragmentation |
| 175 | BOOST_AUTO_TEST_SUITE_END() // Face |
| 176 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 177 | } // namespace nfd::tests |