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" |
Eric Newberry | 812d615 | 2018-06-06 15:06:01 -0700 | [diff] [blame] | 27 | #include "face/transport.hpp" |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 28 | |
| 29 | #include "tests/test-common.hpp" |
Davide Pesavento | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame] | 30 | #include "tests/daemon/global-io-fixture.hpp" |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 31 | |
| 32 | namespace nfd { |
| 33 | namespace face { |
| 34 | namespace tests { |
| 35 | |
| 36 | using namespace nfd::tests; |
| 37 | |
Davide Pesavento | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame] | 38 | class LpFragmenterFixture : public GlobalIoFixture |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 39 | { |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 40 | protected: |
| 41 | LpFragmenter fragmenter{{}}; |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 42 | }; |
| 43 | |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 44 | BOOST_AUTO_TEST_SUITE(Face) |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 45 | BOOST_FIXTURE_TEST_SUITE(TestLpFragmenter, LpFragmenterFixture) |
| 46 | |
| 47 | BOOST_AUTO_TEST_CASE(FragmentSingleFragment) |
| 48 | { |
| 49 | size_t mtu = 256; |
| 50 | |
| 51 | lp::Packet packet; |
| 52 | packet.add<lp::IncomingFaceIdField>(123); |
| 53 | |
| 54 | shared_ptr<Data> data = makeData("/test/data1"); |
| 55 | BOOST_REQUIRE_EQUAL(data->wireEncode().size(), 30); |
| 56 | packet.add<lp::FragmentField>(std::make_pair(data->wireEncode().begin(), |
| 57 | data->wireEncode().end())); |
| 58 | |
| 59 | bool isOk = false; |
| 60 | std::vector<lp::Packet> frags; |
| 61 | std::tie(isOk, frags) = fragmenter.fragmentPacket(packet, mtu); |
| 62 | |
| 63 | BOOST_REQUIRE(isOk); |
| 64 | BOOST_REQUIRE_EQUAL(frags.size(), 1); |
| 65 | BOOST_CHECK(frags[0].has<lp::FragmentField>()); |
| 66 | BOOST_CHECK_EQUAL(frags[0].get<lp::IncomingFaceIdField>(), 123); |
| 67 | BOOST_CHECK(!frags[0].has<lp::FragIndexField>()); |
| 68 | BOOST_CHECK(!frags[0].has<lp::FragCountField>()); |
| 69 | BOOST_CHECK_LE(frags[0].wireEncode().size(), mtu); |
| 70 | |
| 71 | ndn::Buffer::const_iterator fragBegin, fragEnd; |
| 72 | std::tie(fragBegin, fragEnd) = frags[0].get<lp::FragmentField>(); |
| 73 | BOOST_CHECK_EQUAL_COLLECTIONS(data->wireEncode().begin(), data->wireEncode().end(), |
| 74 | fragBegin, fragEnd); |
| 75 | } |
| 76 | |
| 77 | BOOST_AUTO_TEST_CASE(FragmentMultipleFragments) |
| 78 | { |
Eric Newberry | cb6551e | 2020-03-02 14:12:16 -0800 | [diff] [blame^] | 79 | size_t mtu = MIN_MTU; |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 80 | |
| 81 | lp::Packet packet; |
| 82 | packet.add<lp::IncomingFaceIdField>(123); |
| 83 | |
| 84 | shared_ptr<Data> data = makeData("/test/data1/123456789/987654321/123456789"); |
| 85 | BOOST_REQUIRE_EQUAL(data->wireEncode().size(), 63); |
| 86 | packet.add<lp::FragmentField>(std::make_pair(data->wireEncode().begin(), |
| 87 | data->wireEncode().end())); |
| 88 | |
| 89 | bool isOk = false; |
| 90 | std::vector<lp::Packet> frags; |
| 91 | std::tie(isOk, frags) = fragmenter.fragmentPacket(packet, mtu); |
| 92 | |
| 93 | BOOST_REQUIRE(isOk); |
Eric Newberry | 812d615 | 2018-06-06 15:06:01 -0700 | [diff] [blame] | 94 | BOOST_REQUIRE_EQUAL(frags.size(), 5); |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 95 | |
| 96 | ndn::Buffer reassembledPayload(63); |
| 97 | |
| 98 | BOOST_CHECK(frags[0].has<lp::FragmentField>()); |
| 99 | BOOST_CHECK_EQUAL(frags[0].get<lp::IncomingFaceIdField>(), 123); |
| 100 | BOOST_CHECK_EQUAL(frags[0].get<lp::FragIndexField>(), 0); |
Eric Newberry | 812d615 | 2018-06-06 15:06:01 -0700 | [diff] [blame] | 101 | BOOST_CHECK_EQUAL(frags[0].get<lp::FragCountField>(), 5); |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 102 | BOOST_CHECK_LE(frags[0].wireEncode().size(), mtu); |
| 103 | ndn::Buffer::const_iterator frag0Begin, frag0End; |
| 104 | std::tie(frag0Begin, frag0End) = frags[0].get<lp::FragmentField>(); |
| 105 | BOOST_REQUIRE_LE(std::distance(frag0Begin, frag0End), reassembledPayload.size()); |
| 106 | auto reassembledPos = std::copy(frag0Begin, frag0End, reassembledPayload.begin()); |
| 107 | |
| 108 | BOOST_CHECK(frags[1].has<lp::FragmentField>()); |
| 109 | BOOST_CHECK(!frags[1].has<lp::IncomingFaceIdField>()); |
| 110 | BOOST_CHECK_EQUAL(frags[1].get<lp::FragIndexField>(), 1); |
Eric Newberry | 812d615 | 2018-06-06 15:06:01 -0700 | [diff] [blame] | 111 | BOOST_CHECK_EQUAL(frags[1].get<lp::FragCountField>(), 5); |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 112 | BOOST_CHECK_LE(frags[1].wireEncode().size(), mtu); |
| 113 | ndn::Buffer::const_iterator frag1Begin, frag1End; |
| 114 | std::tie(frag1Begin, frag1End) = frags[1].get<lp::FragmentField>(); |
| 115 | BOOST_REQUIRE_LE(std::distance(frag1Begin, frag1End), |
| 116 | std::distance(reassembledPos, reassembledPayload.end())); |
Eric Newberry | 812d615 | 2018-06-06 15:06:01 -0700 | [diff] [blame] | 117 | reassembledPos = std::copy(frag1Begin, frag1End, reassembledPos); |
| 118 | |
| 119 | BOOST_CHECK(frags[2].has<lp::FragmentField>()); |
| 120 | BOOST_CHECK(!frags[2].has<lp::IncomingFaceIdField>()); |
| 121 | BOOST_CHECK_EQUAL(frags[2].get<lp::FragIndexField>(), 2); |
| 122 | BOOST_CHECK_EQUAL(frags[2].get<lp::FragCountField>(), 5); |
| 123 | BOOST_CHECK_LE(frags[2].wireEncode().size(), mtu); |
| 124 | ndn::Buffer::const_iterator frag2Begin, frag2End; |
| 125 | std::tie(frag2Begin, frag2End) = frags[2].get<lp::FragmentField>(); |
| 126 | BOOST_REQUIRE_LE(std::distance(frag2Begin, frag2End), |
| 127 | std::distance(reassembledPos, reassembledPayload.end())); |
| 128 | reassembledPos = std::copy(frag2Begin, frag2End, reassembledPos); |
| 129 | |
| 130 | BOOST_CHECK(frags[3].has<lp::FragmentField>()); |
| 131 | BOOST_CHECK(!frags[3].has<lp::IncomingFaceIdField>()); |
| 132 | BOOST_CHECK_EQUAL(frags[3].get<lp::FragIndexField>(), 3); |
| 133 | BOOST_CHECK_EQUAL(frags[3].get<lp::FragCountField>(), 5); |
| 134 | BOOST_CHECK_LE(frags[3].wireEncode().size(), mtu); |
| 135 | ndn::Buffer::const_iterator frag3Begin, frag3End; |
| 136 | std::tie(frag3Begin, frag3End) = frags[3].get<lp::FragmentField>(); |
| 137 | BOOST_REQUIRE_LE(std::distance(frag3Begin, frag3End), |
| 138 | std::distance(reassembledPos, reassembledPayload.end())); |
| 139 | reassembledPos = std::copy(frag3Begin, frag3End, reassembledPos); |
| 140 | |
| 141 | BOOST_CHECK(frags[4].has<lp::FragmentField>()); |
| 142 | BOOST_CHECK(!frags[4].has<lp::IncomingFaceIdField>()); |
| 143 | BOOST_CHECK_EQUAL(frags[4].get<lp::FragIndexField>(), 4); |
| 144 | BOOST_CHECK_EQUAL(frags[4].get<lp::FragCountField>(), 5); |
| 145 | BOOST_CHECK_LE(frags[4].wireEncode().size(), mtu); |
| 146 | ndn::Buffer::const_iterator frag4Begin, frag4End; |
| 147 | std::tie(frag4Begin, frag4End) = frags[4].get<lp::FragmentField>(); |
| 148 | BOOST_REQUIRE_LE(std::distance(frag4Begin, frag4End), |
| 149 | std::distance(reassembledPos, reassembledPayload.end())); |
| 150 | std::copy(frag4Begin, frag4End, reassembledPos); |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 151 | |
| 152 | BOOST_CHECK_EQUAL_COLLECTIONS(data->wireEncode().begin(), data->wireEncode().end(), |
| 153 | reassembledPayload.begin(), reassembledPayload.end()); |
| 154 | } |
| 155 | |
| 156 | BOOST_AUTO_TEST_CASE(FragmentMtuTooSmall) |
| 157 | { |
| 158 | size_t mtu = 20; |
Eric Newberry | cb6551e | 2020-03-02 14:12:16 -0800 | [diff] [blame^] | 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 | |
| 164 | shared_ptr<Data> data = makeData("/test/data1/123456789/987654321/123456789"); |
| 165 | packet.add<lp::FragmentField>(std::make_pair(data->wireEncode().begin(), |
| 166 | data->wireEncode().end())); |
| 167 | |
| 168 | bool isOk = false; |
| 169 | std::tie(isOk, std::ignore) = fragmenter.fragmentPacket(packet, mtu); |
| 170 | BOOST_REQUIRE(!isOk); |
| 171 | } |
| 172 | |
| 173 | BOOST_AUTO_TEST_CASE(FragmentOverFragCount) |
| 174 | { |
| 175 | LpFragmenter::Options options; |
| 176 | options.nMaxFragments = 2; |
| 177 | fragmenter.setOptions(options); |
| 178 | |
| 179 | size_t mtu = 70; |
| 180 | |
| 181 | lp::Packet packet; |
| 182 | packet.add<lp::IncomingFaceIdField>(123); |
| 183 | |
| 184 | shared_ptr<Data> data = makeData("/test/data1/123456789/987654321/123456789"); |
| 185 | packet.add<lp::FragmentField>(std::make_pair(data->wireEncode().begin(), |
| 186 | data->wireEncode().end())); |
| 187 | |
| 188 | bool isOk = false; |
| 189 | std::tie(isOk, std::ignore) = fragmenter.fragmentPacket(packet, mtu); |
| 190 | BOOST_REQUIRE(!isOk); |
| 191 | } |
| 192 | |
| 193 | BOOST_AUTO_TEST_SUITE_END() // TestLpFragmentation |
| 194 | BOOST_AUTO_TEST_SUITE_END() // Face |
| 195 | |
| 196 | } // namespace tests |
| 197 | } // namespace face |
| 198 | } // namespace nfd |