blob: 30eb8e909141d7a34cbbf3955eebc2d8f25f5064 [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/*
Eric Newberrycb6551e2020-03-02 14:12:16 -08003 * Copyright (c) 2014-2020, 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
55 bool isOk = false;
56 std::vector<lp::Packet> frags;
57 std::tie(isOk, frags) = fragmenter.fragmentPacket(packet, mtu);
Eric Newberry4c3e6b82015-11-10 16:48:42 -070058 BOOST_REQUIRE(isOk);
59 BOOST_REQUIRE_EQUAL(frags.size(), 1);
Davide Pesaventob7e72c32020-10-02 20:00:03 -040060
Eric Newberry4c3e6b82015-11-10 16:48:42 -070061 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 Pesaventob7e72c32020-10-02 20:00:03 -040073BOOST_AUTO_TEST_CASE(MultipleFragments)
Eric Newberry4c3e6b82015-11-10 16:48:42 -070074{
Davide Pesaventob7e72c32020-10-02 20:00:03 -040075 const size_t mtu = MIN_MTU;
Eric Newberry4c3e6b82015-11-10 16:48:42 -070076
77 lp::Packet packet;
78 packet.add<lp::IncomingFaceIdField>(123);
79
Davide Pesavento5bafd2c2020-07-28 16:16:27 -040080 auto data = makeData("/test/data123/123456789/987654321/123456789");
Davide Pesavento5bafd2c2020-07-28 16:16:27 -040081 packet.add<lp::FragmentField>({data->wireEncode().begin(), data->wireEncode().end()});
Eric Newberry4c3e6b82015-11-10 16:48:42 -070082
83 bool isOk = false;
84 std::vector<lp::Packet> frags;
85 std::tie(isOk, frags) = fragmenter.fragmentPacket(packet, mtu);
Eric Newberry4c3e6b82015-11-10 16:48:42 -070086 BOOST_REQUIRE(isOk);
Eric Newberry812d6152018-06-06 15:06:01 -070087 BOOST_REQUIRE_EQUAL(frags.size(), 5);
Eric Newberry4c3e6b82015-11-10 16:48:42 -070088
Davide Pesaventob7e72c32020-10-02 20:00:03 -040089 ndn::Buffer reassembledPayload(data->wireEncode().size());
Eric Newberry4c3e6b82015-11-10 16:48:42 -070090
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 Newberry812d6152018-06-06 15:06:01 -070094 BOOST_CHECK_EQUAL(frags[0].get<lp::FragCountField>(), 5);
Eric Newberry4c3e6b82015-11-10 16:48:42 -070095 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 Newberry812d6152018-06-06 15:06:01 -0700104 BOOST_CHECK_EQUAL(frags[1].get<lp::FragCountField>(), 5);
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700105 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 Newberry812d6152018-06-06 15:06:01 -0700110 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 Newberry4c3e6b82015-11-10 16:48:42 -0700144
145 BOOST_CHECK_EQUAL_COLLECTIONS(data->wireEncode().begin(), data->wireEncode().end(),
146 reassembledPayload.begin(), reassembledPayload.end());
147}
148
Davide Pesaventob7e72c32020-10-02 20:00:03 -0400149BOOST_AUTO_TEST_CASE(MtuTooSmall)
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700150{
Davide Pesaventob7e72c32020-10-02 20:00:03 -0400151 const size_t mtu = 20;
Eric Newberrycb6551e2020-03-02 14:12:16 -0800152 BOOST_ASSERT(mtu < MIN_MTU);
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700153
154 lp::Packet packet;
155 packet.add<lp::IncomingFaceIdField>(123);
156
Davide Pesavento5bafd2c2020-07-28 16:16:27 -0400157 auto data = makeData("/test/data123/123456789/987654321/123456789");
158 packet.add<lp::FragmentField>({data->wireEncode().begin(), data->wireEncode().end()});
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700159
160 bool isOk = false;
161 std::tie(isOk, std::ignore) = fragmenter.fragmentPacket(packet, mtu);
Davide Pesavento5bafd2c2020-07-28 16:16:27 -0400162 BOOST_CHECK_EQUAL(isOk, false);
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700163}
164
Davide Pesaventob7e72c32020-10-02 20:00:03 -0400165BOOST_AUTO_TEST_CASE(FragCountOverLimit)
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700166{
167 LpFragmenter::Options options;
168 options.nMaxFragments = 2;
169 fragmenter.setOptions(options);
170
Davide Pesaventob7e72c32020-10-02 20:00:03 -0400171 const size_t mtu = 70;
172 BOOST_ASSERT(mtu >= MIN_MTU);
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700173
174 lp::Packet packet;
175 packet.add<lp::IncomingFaceIdField>(123);
176
Davide Pesavento5bafd2c2020-07-28 16:16:27 -0400177 auto data = makeData("/test/data123/123456789/987654321/123456789");
178 packet.add<lp::FragmentField>({data->wireEncode().begin(), data->wireEncode().end()});
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700179
180 bool isOk = false;
181 std::tie(isOk, std::ignore) = fragmenter.fragmentPacket(packet, mtu);
Davide Pesavento5bafd2c2020-07-28 16:16:27 -0400182 BOOST_CHECK_EQUAL(isOk, false);
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700183}
184
185BOOST_AUTO_TEST_SUITE_END() // TestLpFragmentation
186BOOST_AUTO_TEST_SUITE_END() // Face
187
188} // namespace tests
189} // namespace face
190} // namespace nfd