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 | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2022, 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 | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame^] | 30 | namespace nfd::tests { |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 31 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame^] | 32 | using namespace nfd::face; |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 33 | |
Davide Pesavento | b7e72c3 | 2020-10-02 20:00:03 -0400 | [diff] [blame] | 34 | class LpFragmenterFixture |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 35 | { |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 36 | protected: |
| 37 | LpFragmenter fragmenter{{}}; |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 38 | }; |
| 39 | |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 40 | BOOST_AUTO_TEST_SUITE(Face) |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 41 | BOOST_FIXTURE_TEST_SUITE(TestLpFragmenter, LpFragmenterFixture) |
| 42 | |
Davide Pesavento | b7e72c3 | 2020-10-02 20:00:03 -0400 | [diff] [blame] | 43 | BOOST_AUTO_TEST_CASE(SingleFragment) |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 44 | { |
Davide Pesavento | b7e72c3 | 2020-10-02 20:00:03 -0400 | [diff] [blame] | 45 | const size_t mtu = 256; |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 46 | |
| 47 | lp::Packet packet; |
| 48 | packet.add<lp::IncomingFaceIdField>(123); |
| 49 | |
Davide Pesavento | 5bafd2c | 2020-07-28 16:16:27 -0400 | [diff] [blame] | 50 | auto data = makeData("/test/data123"); |
Davide Pesavento | 5bafd2c | 2020-07-28 16:16:27 -0400 | [diff] [blame] | 51 | packet.add<lp::FragmentField>({data->wireEncode().begin(), data->wireEncode().end()}); |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 52 | |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 53 | auto [isOk, frags] = fragmenter.fragmentPacket(packet, mtu); |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 54 | BOOST_REQUIRE(isOk); |
| 55 | BOOST_REQUIRE_EQUAL(frags.size(), 1); |
Davide Pesavento | b7e72c3 | 2020-10-02 20:00:03 -0400 | [diff] [blame] | 56 | |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 57 | BOOST_CHECK(frags[0].has<lp::FragmentField>()); |
| 58 | BOOST_CHECK_EQUAL(frags[0].get<lp::IncomingFaceIdField>(), 123); |
| 59 | BOOST_CHECK(!frags[0].has<lp::FragIndexField>()); |
| 60 | BOOST_CHECK(!frags[0].has<lp::FragCountField>()); |
| 61 | BOOST_CHECK_LE(frags[0].wireEncode().size(), mtu); |
| 62 | |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 63 | auto [fragBegin, fragEnd] = frags[0].get<lp::FragmentField>(); |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 64 | BOOST_CHECK_EQUAL_COLLECTIONS(data->wireEncode().begin(), data->wireEncode().end(), |
| 65 | fragBegin, fragEnd); |
| 66 | } |
| 67 | |
Davide Pesavento | b7e72c3 | 2020-10-02 20:00:03 -0400 | [diff] [blame] | 68 | BOOST_AUTO_TEST_CASE(MultipleFragments) |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 69 | { |
Davide Pesavento | b7e72c3 | 2020-10-02 20:00:03 -0400 | [diff] [blame] | 70 | const size_t mtu = MIN_MTU; |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 71 | |
| 72 | lp::Packet packet; |
| 73 | packet.add<lp::IncomingFaceIdField>(123); |
| 74 | |
Davide Pesavento | 5bafd2c | 2020-07-28 16:16:27 -0400 | [diff] [blame] | 75 | auto data = makeData("/test/data123/123456789/987654321/123456789"); |
Davide Pesavento | 5bafd2c | 2020-07-28 16:16:27 -0400 | [diff] [blame] | 76 | packet.add<lp::FragmentField>({data->wireEncode().begin(), data->wireEncode().end()}); |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 77 | |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 78 | auto [isOk, frags] = fragmenter.fragmentPacket(packet, mtu); |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 79 | BOOST_REQUIRE(isOk); |
Eric Newberry | 812d615 | 2018-06-06 15:06:01 -0700 | [diff] [blame] | 80 | BOOST_REQUIRE_EQUAL(frags.size(), 5); |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 81 | |
Davide Pesavento | b7e72c3 | 2020-10-02 20:00:03 -0400 | [diff] [blame] | 82 | ndn::Buffer reassembledPayload(data->wireEncode().size()); |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 83 | |
| 84 | BOOST_CHECK(frags[0].has<lp::FragmentField>()); |
| 85 | BOOST_CHECK_EQUAL(frags[0].get<lp::IncomingFaceIdField>(), 123); |
| 86 | BOOST_CHECK_EQUAL(frags[0].get<lp::FragIndexField>(), 0); |
Eric Newberry | 812d615 | 2018-06-06 15:06:01 -0700 | [diff] [blame] | 87 | BOOST_CHECK_EQUAL(frags[0].get<lp::FragCountField>(), 5); |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 88 | BOOST_CHECK_LE(frags[0].wireEncode().size(), mtu); |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 89 | auto [frag0Begin, frag0End] = frags[0].get<lp::FragmentField>(); |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 90 | BOOST_REQUIRE_LE(std::distance(frag0Begin, frag0End), reassembledPayload.size()); |
| 91 | auto reassembledPos = std::copy(frag0Begin, frag0End, reassembledPayload.begin()); |
| 92 | |
| 93 | BOOST_CHECK(frags[1].has<lp::FragmentField>()); |
| 94 | BOOST_CHECK(!frags[1].has<lp::IncomingFaceIdField>()); |
| 95 | BOOST_CHECK_EQUAL(frags[1].get<lp::FragIndexField>(), 1); |
Eric Newberry | 812d615 | 2018-06-06 15:06:01 -0700 | [diff] [blame] | 96 | BOOST_CHECK_EQUAL(frags[1].get<lp::FragCountField>(), 5); |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 97 | BOOST_CHECK_LE(frags[1].wireEncode().size(), mtu); |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 98 | auto [frag1Begin, frag1End] = frags[1].get<lp::FragmentField>(); |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 99 | BOOST_REQUIRE_LE(std::distance(frag1Begin, frag1End), |
| 100 | std::distance(reassembledPos, reassembledPayload.end())); |
Eric Newberry | 812d615 | 2018-06-06 15:06:01 -0700 | [diff] [blame] | 101 | reassembledPos = std::copy(frag1Begin, frag1End, reassembledPos); |
| 102 | |
| 103 | BOOST_CHECK(frags[2].has<lp::FragmentField>()); |
| 104 | BOOST_CHECK(!frags[2].has<lp::IncomingFaceIdField>()); |
| 105 | BOOST_CHECK_EQUAL(frags[2].get<lp::FragIndexField>(), 2); |
| 106 | BOOST_CHECK_EQUAL(frags[2].get<lp::FragCountField>(), 5); |
| 107 | BOOST_CHECK_LE(frags[2].wireEncode().size(), mtu); |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 108 | auto [frag2Begin, frag2End] = frags[2].get<lp::FragmentField>(); |
Eric Newberry | 812d615 | 2018-06-06 15:06:01 -0700 | [diff] [blame] | 109 | BOOST_REQUIRE_LE(std::distance(frag2Begin, frag2End), |
| 110 | std::distance(reassembledPos, reassembledPayload.end())); |
| 111 | reassembledPos = std::copy(frag2Begin, frag2End, reassembledPos); |
| 112 | |
| 113 | BOOST_CHECK(frags[3].has<lp::FragmentField>()); |
| 114 | BOOST_CHECK(!frags[3].has<lp::IncomingFaceIdField>()); |
| 115 | BOOST_CHECK_EQUAL(frags[3].get<lp::FragIndexField>(), 3); |
| 116 | BOOST_CHECK_EQUAL(frags[3].get<lp::FragCountField>(), 5); |
| 117 | BOOST_CHECK_LE(frags[3].wireEncode().size(), mtu); |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 118 | auto [frag3Begin, frag3End] = frags[3].get<lp::FragmentField>(); |
Eric Newberry | 812d615 | 2018-06-06 15:06:01 -0700 | [diff] [blame] | 119 | BOOST_REQUIRE_LE(std::distance(frag3Begin, frag3End), |
| 120 | std::distance(reassembledPos, reassembledPayload.end())); |
| 121 | reassembledPos = std::copy(frag3Begin, frag3End, reassembledPos); |
| 122 | |
| 123 | BOOST_CHECK(frags[4].has<lp::FragmentField>()); |
| 124 | BOOST_CHECK(!frags[4].has<lp::IncomingFaceIdField>()); |
| 125 | BOOST_CHECK_EQUAL(frags[4].get<lp::FragIndexField>(), 4); |
| 126 | BOOST_CHECK_EQUAL(frags[4].get<lp::FragCountField>(), 5); |
| 127 | BOOST_CHECK_LE(frags[4].wireEncode().size(), mtu); |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 128 | auto [frag4Begin, frag4End] = frags[4].get<lp::FragmentField>(); |
Eric Newberry | 812d615 | 2018-06-06 15:06:01 -0700 | [diff] [blame] | 129 | BOOST_REQUIRE_LE(std::distance(frag4Begin, frag4End), |
| 130 | std::distance(reassembledPos, reassembledPayload.end())); |
| 131 | std::copy(frag4Begin, frag4End, reassembledPos); |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 132 | |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 133 | BOOST_TEST(data->wireEncode() == reassembledPayload, boost::test_tools::per_element()); |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 134 | } |
| 135 | |
Davide Pesavento | b7e72c3 | 2020-10-02 20:00:03 -0400 | [diff] [blame] | 136 | BOOST_AUTO_TEST_CASE(MtuTooSmall) |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 137 | { |
Davide Pesavento | b7e72c3 | 2020-10-02 20:00:03 -0400 | [diff] [blame] | 138 | const size_t mtu = 20; |
Eric Newberry | cb6551e | 2020-03-02 14:12:16 -0800 | [diff] [blame] | 139 | BOOST_ASSERT(mtu < MIN_MTU); |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 140 | |
| 141 | lp::Packet packet; |
| 142 | packet.add<lp::IncomingFaceIdField>(123); |
| 143 | |
Davide Pesavento | 5bafd2c | 2020-07-28 16:16:27 -0400 | [diff] [blame] | 144 | auto data = makeData("/test/data123/123456789/987654321/123456789"); |
| 145 | packet.add<lp::FragmentField>({data->wireEncode().begin(), data->wireEncode().end()}); |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 146 | |
| 147 | bool isOk = false; |
| 148 | std::tie(isOk, std::ignore) = fragmenter.fragmentPacket(packet, mtu); |
Davide Pesavento | 5bafd2c | 2020-07-28 16:16:27 -0400 | [diff] [blame] | 149 | BOOST_CHECK_EQUAL(isOk, false); |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Davide Pesavento | b7e72c3 | 2020-10-02 20:00:03 -0400 | [diff] [blame] | 152 | BOOST_AUTO_TEST_CASE(FragCountOverLimit) |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 153 | { |
| 154 | LpFragmenter::Options options; |
| 155 | options.nMaxFragments = 2; |
| 156 | fragmenter.setOptions(options); |
| 157 | |
Davide Pesavento | b7e72c3 | 2020-10-02 20:00:03 -0400 | [diff] [blame] | 158 | const size_t mtu = 70; |
| 159 | BOOST_ASSERT(mtu >= MIN_MTU); |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 160 | |
| 161 | lp::Packet packet; |
| 162 | packet.add<lp::IncomingFaceIdField>(123); |
| 163 | |
Davide Pesavento | 5bafd2c | 2020-07-28 16:16:27 -0400 | [diff] [blame] | 164 | auto data = makeData("/test/data123/123456789/987654321/123456789"); |
| 165 | packet.add<lp::FragmentField>({data->wireEncode().begin(), data->wireEncode().end()}); |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 166 | |
| 167 | bool isOk = false; |
| 168 | std::tie(isOk, std::ignore) = fragmenter.fragmentPacket(packet, mtu); |
Davide Pesavento | 5bafd2c | 2020-07-28 16:16:27 -0400 | [diff] [blame] | 169 | BOOST_CHECK_EQUAL(isOk, false); |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | BOOST_AUTO_TEST_SUITE_END() // TestLpFragmentation |
| 173 | BOOST_AUTO_TEST_SUITE_END() // Face |
| 174 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame^] | 175 | } // namespace nfd::tests |