blob: 8bd48c2499c45dde5eb5c91d21dac0b296a18780 [file] [log] [blame]
Eric Newberry4c3e6b82015-11-10 16:48:42 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventoe4b22382018-06-10 14:37:24 -04002/*
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -04003 * Copyright (c) 2014-2022, Regents of the University of California,
Eric Newberry4c3e6b82015-11-10 16:48:42 -07004 * 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
30namespace nfd {
31namespace face {
32namespace tests {
33
34using namespace nfd::tests;
35
Davide Pesaventob7e72c32020-10-02 20:00:03 -040036class LpFragmenterFixture
Eric Newberry4c3e6b82015-11-10 16:48:42 -070037{
Davide Pesaventoe4b22382018-06-10 14:37:24 -040038protected:
39 LpFragmenter fragmenter{{}};
Eric Newberry4c3e6b82015-11-10 16:48:42 -070040};
41
Davide Pesaventoe4b22382018-06-10 14:37:24 -040042BOOST_AUTO_TEST_SUITE(Face)
Eric Newberry4c3e6b82015-11-10 16:48:42 -070043BOOST_FIXTURE_TEST_SUITE(TestLpFragmenter, LpFragmenterFixture)
44
Davide Pesaventob7e72c32020-10-02 20:00:03 -040045BOOST_AUTO_TEST_CASE(SingleFragment)
Eric Newberry4c3e6b82015-11-10 16:48:42 -070046{
Davide Pesaventob7e72c32020-10-02 20:00:03 -040047 const size_t mtu = 256;
Eric Newberry4c3e6b82015-11-10 16:48:42 -070048
49 lp::Packet packet;
50 packet.add<lp::IncomingFaceIdField>(123);
51
Davide Pesavento5bafd2c2020-07-28 16:16:27 -040052 auto data = makeData("/test/data123");
Davide Pesavento5bafd2c2020-07-28 16:16:27 -040053 packet.add<lp::FragmentField>({data->wireEncode().begin(), data->wireEncode().end()});
Eric Newberry4c3e6b82015-11-10 16:48:42 -070054
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040055 auto [isOk, frags] = fragmenter.fragmentPacket(packet, mtu);
Eric Newberry4c3e6b82015-11-10 16:48:42 -070056 BOOST_REQUIRE(isOk);
57 BOOST_REQUIRE_EQUAL(frags.size(), 1);
Davide Pesaventob7e72c32020-10-02 20:00:03 -040058
Eric Newberry4c3e6b82015-11-10 16:48:42 -070059 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 Pesaventoa3a7a4e2022-05-29 16:06:22 -040065 auto [fragBegin, fragEnd] = frags[0].get<lp::FragmentField>();
Eric Newberry4c3e6b82015-11-10 16:48:42 -070066 BOOST_CHECK_EQUAL_COLLECTIONS(data->wireEncode().begin(), data->wireEncode().end(),
67 fragBegin, fragEnd);
68}
69
Davide Pesaventob7e72c32020-10-02 20:00:03 -040070BOOST_AUTO_TEST_CASE(MultipleFragments)
Eric Newberry4c3e6b82015-11-10 16:48:42 -070071{
Davide Pesaventob7e72c32020-10-02 20:00:03 -040072 const size_t mtu = MIN_MTU;
Eric Newberry4c3e6b82015-11-10 16:48:42 -070073
74 lp::Packet packet;
75 packet.add<lp::IncomingFaceIdField>(123);
76
Davide Pesavento5bafd2c2020-07-28 16:16:27 -040077 auto data = makeData("/test/data123/123456789/987654321/123456789");
Davide Pesavento5bafd2c2020-07-28 16:16:27 -040078 packet.add<lp::FragmentField>({data->wireEncode().begin(), data->wireEncode().end()});
Eric Newberry4c3e6b82015-11-10 16:48:42 -070079
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040080 auto [isOk, frags] = fragmenter.fragmentPacket(packet, mtu);
Eric Newberry4c3e6b82015-11-10 16:48:42 -070081 BOOST_REQUIRE(isOk);
Eric Newberry812d6152018-06-06 15:06:01 -070082 BOOST_REQUIRE_EQUAL(frags.size(), 5);
Eric Newberry4c3e6b82015-11-10 16:48:42 -070083
Davide Pesaventob7e72c32020-10-02 20:00:03 -040084 ndn::Buffer reassembledPayload(data->wireEncode().size());
Eric Newberry4c3e6b82015-11-10 16:48:42 -070085
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 Newberry812d6152018-06-06 15:06:01 -070089 BOOST_CHECK_EQUAL(frags[0].get<lp::FragCountField>(), 5);
Eric Newberry4c3e6b82015-11-10 16:48:42 -070090 BOOST_CHECK_LE(frags[0].wireEncode().size(), mtu);
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040091 auto [frag0Begin, frag0End] = frags[0].get<lp::FragmentField>();
Eric Newberry4c3e6b82015-11-10 16:48:42 -070092 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 Newberry812d6152018-06-06 15:06:01 -070098 BOOST_CHECK_EQUAL(frags[1].get<lp::FragCountField>(), 5);
Eric Newberry4c3e6b82015-11-10 16:48:42 -070099 BOOST_CHECK_LE(frags[1].wireEncode().size(), mtu);
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400100 auto [frag1Begin, frag1End] = frags[1].get<lp::FragmentField>();
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700101 BOOST_REQUIRE_LE(std::distance(frag1Begin, frag1End),
102 std::distance(reassembledPos, reassembledPayload.end()));
Eric Newberry812d6152018-06-06 15:06:01 -0700103 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 Pesaventoa3a7a4e2022-05-29 16:06:22 -0400110 auto [frag2Begin, frag2End] = frags[2].get<lp::FragmentField>();
Eric Newberry812d6152018-06-06 15:06:01 -0700111 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 Pesaventoa3a7a4e2022-05-29 16:06:22 -0400120 auto [frag3Begin, frag3End] = frags[3].get<lp::FragmentField>();
Eric Newberry812d6152018-06-06 15:06:01 -0700121 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 Pesaventoa3a7a4e2022-05-29 16:06:22 -0400130 auto [frag4Begin, frag4End] = frags[4].get<lp::FragmentField>();
Eric Newberry812d6152018-06-06 15:06:01 -0700131 BOOST_REQUIRE_LE(std::distance(frag4Begin, frag4End),
132 std::distance(reassembledPos, reassembledPayload.end()));
133 std::copy(frag4Begin, frag4End, reassembledPos);
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700134
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400135 BOOST_TEST(data->wireEncode() == reassembledPayload, boost::test_tools::per_element());
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700136}
137
Davide Pesaventob7e72c32020-10-02 20:00:03 -0400138BOOST_AUTO_TEST_CASE(MtuTooSmall)
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700139{
Davide Pesaventob7e72c32020-10-02 20:00:03 -0400140 const size_t mtu = 20;
Eric Newberrycb6551e2020-03-02 14:12:16 -0800141 BOOST_ASSERT(mtu < MIN_MTU);
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700142
143 lp::Packet packet;
144 packet.add<lp::IncomingFaceIdField>(123);
145
Davide Pesavento5bafd2c2020-07-28 16:16:27 -0400146 auto data = makeData("/test/data123/123456789/987654321/123456789");
147 packet.add<lp::FragmentField>({data->wireEncode().begin(), data->wireEncode().end()});
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700148
149 bool isOk = false;
150 std::tie(isOk, std::ignore) = fragmenter.fragmentPacket(packet, mtu);
Davide Pesavento5bafd2c2020-07-28 16:16:27 -0400151 BOOST_CHECK_EQUAL(isOk, false);
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700152}
153
Davide Pesaventob7e72c32020-10-02 20:00:03 -0400154BOOST_AUTO_TEST_CASE(FragCountOverLimit)
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700155{
156 LpFragmenter::Options options;
157 options.nMaxFragments = 2;
158 fragmenter.setOptions(options);
159
Davide Pesaventob7e72c32020-10-02 20:00:03 -0400160 const size_t mtu = 70;
161 BOOST_ASSERT(mtu >= MIN_MTU);
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700162
163 lp::Packet packet;
164 packet.add<lp::IncomingFaceIdField>(123);
165
Davide Pesavento5bafd2c2020-07-28 16:16:27 -0400166 auto data = makeData("/test/data123/123456789/987654321/123456789");
167 packet.add<lp::FragmentField>({data->wireEncode().begin(), data->wireEncode().end()});
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700168
169 bool isOk = false;
170 std::tie(isOk, std::ignore) = fragmenter.fragmentPacket(packet, mtu);
Davide Pesavento5bafd2c2020-07-28 16:16:27 -0400171 BOOST_CHECK_EQUAL(isOk, false);
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700172}
173
174BOOST_AUTO_TEST_SUITE_END() // TestLpFragmentation
175BOOST_AUTO_TEST_SUITE_END() // Face
176
177} // namespace tests
178} // namespace face
179} // namespace nfd