blob: 065fc6d0f44c1299079ce96cc46b2618d8dd3dbc [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
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040030namespace nfd::tests {
Eric Newberry4c3e6b82015-11-10 16:48:42 -070031
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040032using namespace nfd::face;
Eric Newberry4c3e6b82015-11-10 16:48:42 -070033
Davide Pesaventob7e72c32020-10-02 20:00:03 -040034class LpFragmenterFixture
Eric Newberry4c3e6b82015-11-10 16:48:42 -070035{
Davide Pesaventoe4b22382018-06-10 14:37:24 -040036protected:
37 LpFragmenter fragmenter{{}};
Eric Newberry4c3e6b82015-11-10 16:48:42 -070038};
39
Davide Pesaventoe4b22382018-06-10 14:37:24 -040040BOOST_AUTO_TEST_SUITE(Face)
Eric Newberry4c3e6b82015-11-10 16:48:42 -070041BOOST_FIXTURE_TEST_SUITE(TestLpFragmenter, LpFragmenterFixture)
42
Davide Pesaventob7e72c32020-10-02 20:00:03 -040043BOOST_AUTO_TEST_CASE(SingleFragment)
Eric Newberry4c3e6b82015-11-10 16:48:42 -070044{
Davide Pesaventob7e72c32020-10-02 20:00:03 -040045 const size_t mtu = 256;
Eric Newberry4c3e6b82015-11-10 16:48:42 -070046
47 lp::Packet packet;
48 packet.add<lp::IncomingFaceIdField>(123);
49
Davide Pesavento5bafd2c2020-07-28 16:16:27 -040050 auto data = makeData("/test/data123");
Davide Pesavento5bafd2c2020-07-28 16:16:27 -040051 packet.add<lp::FragmentField>({data->wireEncode().begin(), data->wireEncode().end()});
Eric Newberry4c3e6b82015-11-10 16:48:42 -070052
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040053 auto [isOk, frags] = fragmenter.fragmentPacket(packet, mtu);
Eric Newberry4c3e6b82015-11-10 16:48:42 -070054 BOOST_REQUIRE(isOk);
55 BOOST_REQUIRE_EQUAL(frags.size(), 1);
Davide Pesaventob7e72c32020-10-02 20:00:03 -040056
Eric Newberry4c3e6b82015-11-10 16:48:42 -070057 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 Pesaventoa3a7a4e2022-05-29 16:06:22 -040063 auto [fragBegin, fragEnd] = frags[0].get<lp::FragmentField>();
Eric Newberry4c3e6b82015-11-10 16:48:42 -070064 BOOST_CHECK_EQUAL_COLLECTIONS(data->wireEncode().begin(), data->wireEncode().end(),
65 fragBegin, fragEnd);
66}
67
Davide Pesaventob7e72c32020-10-02 20:00:03 -040068BOOST_AUTO_TEST_CASE(MultipleFragments)
Eric Newberry4c3e6b82015-11-10 16:48:42 -070069{
Davide Pesaventob7e72c32020-10-02 20:00:03 -040070 const size_t mtu = MIN_MTU;
Eric Newberry4c3e6b82015-11-10 16:48:42 -070071
72 lp::Packet packet;
73 packet.add<lp::IncomingFaceIdField>(123);
74
Davide Pesavento5bafd2c2020-07-28 16:16:27 -040075 auto data = makeData("/test/data123/123456789/987654321/123456789");
Davide Pesavento5bafd2c2020-07-28 16:16:27 -040076 packet.add<lp::FragmentField>({data->wireEncode().begin(), data->wireEncode().end()});
Eric Newberry4c3e6b82015-11-10 16:48:42 -070077
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040078 auto [isOk, frags] = fragmenter.fragmentPacket(packet, mtu);
Eric Newberry4c3e6b82015-11-10 16:48:42 -070079 BOOST_REQUIRE(isOk);
Eric Newberry812d6152018-06-06 15:06:01 -070080 BOOST_REQUIRE_EQUAL(frags.size(), 5);
Eric Newberry4c3e6b82015-11-10 16:48:42 -070081
Davide Pesaventob7e72c32020-10-02 20:00:03 -040082 ndn::Buffer reassembledPayload(data->wireEncode().size());
Eric Newberry4c3e6b82015-11-10 16:48:42 -070083
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 Newberry812d6152018-06-06 15:06:01 -070087 BOOST_CHECK_EQUAL(frags[0].get<lp::FragCountField>(), 5);
Eric Newberry4c3e6b82015-11-10 16:48:42 -070088 BOOST_CHECK_LE(frags[0].wireEncode().size(), mtu);
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040089 auto [frag0Begin, frag0End] = frags[0].get<lp::FragmentField>();
Eric Newberry4c3e6b82015-11-10 16:48:42 -070090 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 Newberry812d6152018-06-06 15:06:01 -070096 BOOST_CHECK_EQUAL(frags[1].get<lp::FragCountField>(), 5);
Eric Newberry4c3e6b82015-11-10 16:48:42 -070097 BOOST_CHECK_LE(frags[1].wireEncode().size(), mtu);
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040098 auto [frag1Begin, frag1End] = frags[1].get<lp::FragmentField>();
Eric Newberry4c3e6b82015-11-10 16:48:42 -070099 BOOST_REQUIRE_LE(std::distance(frag1Begin, frag1End),
100 std::distance(reassembledPos, reassembledPayload.end()));
Eric Newberry812d6152018-06-06 15:06:01 -0700101 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 Pesaventoa3a7a4e2022-05-29 16:06:22 -0400108 auto [frag2Begin, frag2End] = frags[2].get<lp::FragmentField>();
Eric Newberry812d6152018-06-06 15:06:01 -0700109 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 Pesaventoa3a7a4e2022-05-29 16:06:22 -0400118 auto [frag3Begin, frag3End] = frags[3].get<lp::FragmentField>();
Eric Newberry812d6152018-06-06 15:06:01 -0700119 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 Pesaventoa3a7a4e2022-05-29 16:06:22 -0400128 auto [frag4Begin, frag4End] = frags[4].get<lp::FragmentField>();
Eric Newberry812d6152018-06-06 15:06:01 -0700129 BOOST_REQUIRE_LE(std::distance(frag4Begin, frag4End),
130 std::distance(reassembledPos, reassembledPayload.end()));
131 std::copy(frag4Begin, frag4End, reassembledPos);
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700132
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400133 BOOST_TEST(data->wireEncode() == reassembledPayload, boost::test_tools::per_element());
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700134}
135
Davide Pesaventob7e72c32020-10-02 20:00:03 -0400136BOOST_AUTO_TEST_CASE(MtuTooSmall)
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700137{
Davide Pesaventob7e72c32020-10-02 20:00:03 -0400138 const size_t mtu = 20;
Eric Newberrycb6551e2020-03-02 14:12:16 -0800139 BOOST_ASSERT(mtu < MIN_MTU);
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700140
141 lp::Packet packet;
142 packet.add<lp::IncomingFaceIdField>(123);
143
Davide Pesavento5bafd2c2020-07-28 16:16:27 -0400144 auto data = makeData("/test/data123/123456789/987654321/123456789");
145 packet.add<lp::FragmentField>({data->wireEncode().begin(), data->wireEncode().end()});
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700146
147 bool isOk = false;
148 std::tie(isOk, std::ignore) = fragmenter.fragmentPacket(packet, mtu);
Davide Pesavento5bafd2c2020-07-28 16:16:27 -0400149 BOOST_CHECK_EQUAL(isOk, false);
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700150}
151
Davide Pesaventob7e72c32020-10-02 20:00:03 -0400152BOOST_AUTO_TEST_CASE(FragCountOverLimit)
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700153{
154 LpFragmenter::Options options;
155 options.nMaxFragments = 2;
156 fragmenter.setOptions(options);
157
Davide Pesaventob7e72c32020-10-02 20:00:03 -0400158 const size_t mtu = 70;
159 BOOST_ASSERT(mtu >= MIN_MTU);
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700160
161 lp::Packet packet;
162 packet.add<lp::IncomingFaceIdField>(123);
163
Davide Pesavento5bafd2c2020-07-28 16:16:27 -0400164 auto data = makeData("/test/data123/123456789/987654321/123456789");
165 packet.add<lp::FragmentField>({data->wireEncode().begin(), data->wireEncode().end()});
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700166
167 bool isOk = false;
168 std::tie(isOk, std::ignore) = fragmenter.fragmentPacket(packet, mtu);
Davide Pesavento5bafd2c2020-07-28 16:16:27 -0400169 BOOST_CHECK_EQUAL(isOk, false);
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700170}
171
172BOOST_AUTO_TEST_SUITE_END() // TestLpFragmentation
173BOOST_AUTO_TEST_SUITE_END() // Face
174
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400175} // namespace nfd::tests