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