blob: d4403100c63a9f1a0fcce605f7eda5c923f92412 [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"
Eric Newberry812d6152018-06-06 15:06:01 -070027#include "face/transport.hpp"
Eric Newberry4c3e6b82015-11-10 16:48:42 -070028
29#include "tests/test-common.hpp"
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040030#include "tests/daemon/global-io-fixture.hpp"
Eric Newberry4c3e6b82015-11-10 16:48:42 -070031
32namespace nfd {
33namespace face {
34namespace tests {
35
36using namespace nfd::tests;
37
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040038class LpFragmenterFixture : public GlobalIoFixture
Eric Newberry4c3e6b82015-11-10 16:48:42 -070039{
Davide Pesaventoe4b22382018-06-10 14:37:24 -040040protected:
41 LpFragmenter fragmenter{{}};
Eric Newberry4c3e6b82015-11-10 16:48:42 -070042};
43
Davide Pesaventoe4b22382018-06-10 14:37:24 -040044BOOST_AUTO_TEST_SUITE(Face)
Eric Newberry4c3e6b82015-11-10 16:48:42 -070045BOOST_FIXTURE_TEST_SUITE(TestLpFragmenter, LpFragmenterFixture)
46
47BOOST_AUTO_TEST_CASE(FragmentSingleFragment)
48{
49 size_t mtu = 256;
50
51 lp::Packet packet;
52 packet.add<lp::IncomingFaceIdField>(123);
53
Davide Pesavento5bafd2c2020-07-28 16:16:27 -040054 auto data = makeData("/test/data123");
Eric Newberry4c3e6b82015-11-10 16:48:42 -070055 BOOST_REQUIRE_EQUAL(data->wireEncode().size(), 30);
Davide Pesavento5bafd2c2020-07-28 16:16:27 -040056 packet.add<lp::FragmentField>({data->wireEncode().begin(), data->wireEncode().end()});
Eric Newberry4c3e6b82015-11-10 16:48:42 -070057
58 bool isOk = false;
59 std::vector<lp::Packet> frags;
60 std::tie(isOk, frags) = fragmenter.fragmentPacket(packet, mtu);
61
62 BOOST_REQUIRE(isOk);
63 BOOST_REQUIRE_EQUAL(frags.size(), 1);
64 BOOST_CHECK(frags[0].has<lp::FragmentField>());
65 BOOST_CHECK_EQUAL(frags[0].get<lp::IncomingFaceIdField>(), 123);
66 BOOST_CHECK(!frags[0].has<lp::FragIndexField>());
67 BOOST_CHECK(!frags[0].has<lp::FragCountField>());
68 BOOST_CHECK_LE(frags[0].wireEncode().size(), mtu);
69
70 ndn::Buffer::const_iterator fragBegin, fragEnd;
71 std::tie(fragBegin, fragEnd) = frags[0].get<lp::FragmentField>();
72 BOOST_CHECK_EQUAL_COLLECTIONS(data->wireEncode().begin(), data->wireEncode().end(),
73 fragBegin, fragEnd);
74}
75
76BOOST_AUTO_TEST_CASE(FragmentMultipleFragments)
77{
Eric Newberrycb6551e2020-03-02 14:12:16 -080078 size_t mtu = MIN_MTU;
Eric Newberry4c3e6b82015-11-10 16:48:42 -070079
80 lp::Packet packet;
81 packet.add<lp::IncomingFaceIdField>(123);
82
Davide Pesavento5bafd2c2020-07-28 16:16:27 -040083 auto data = makeData("/test/data123/123456789/987654321/123456789");
Eric Newberry4c3e6b82015-11-10 16:48:42 -070084 BOOST_REQUIRE_EQUAL(data->wireEncode().size(), 63);
Davide Pesavento5bafd2c2020-07-28 16:16:27 -040085 packet.add<lp::FragmentField>({data->wireEncode().begin(), data->wireEncode().end()});
Eric Newberry4c3e6b82015-11-10 16:48:42 -070086
87 bool isOk = false;
88 std::vector<lp::Packet> frags;
89 std::tie(isOk, frags) = fragmenter.fragmentPacket(packet, mtu);
90
91 BOOST_REQUIRE(isOk);
Eric Newberry812d6152018-06-06 15:06:01 -070092 BOOST_REQUIRE_EQUAL(frags.size(), 5);
Eric Newberry4c3e6b82015-11-10 16:48:42 -070093
94 ndn::Buffer reassembledPayload(63);
95
96 BOOST_CHECK(frags[0].has<lp::FragmentField>());
97 BOOST_CHECK_EQUAL(frags[0].get<lp::IncomingFaceIdField>(), 123);
98 BOOST_CHECK_EQUAL(frags[0].get<lp::FragIndexField>(), 0);
Eric Newberry812d6152018-06-06 15:06:01 -070099 BOOST_CHECK_EQUAL(frags[0].get<lp::FragCountField>(), 5);
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700100 BOOST_CHECK_LE(frags[0].wireEncode().size(), mtu);
101 ndn::Buffer::const_iterator frag0Begin, frag0End;
102 std::tie(frag0Begin, frag0End) = frags[0].get<lp::FragmentField>();
103 BOOST_REQUIRE_LE(std::distance(frag0Begin, frag0End), reassembledPayload.size());
104 auto reassembledPos = std::copy(frag0Begin, frag0End, reassembledPayload.begin());
105
106 BOOST_CHECK(frags[1].has<lp::FragmentField>());
107 BOOST_CHECK(!frags[1].has<lp::IncomingFaceIdField>());
108 BOOST_CHECK_EQUAL(frags[1].get<lp::FragIndexField>(), 1);
Eric Newberry812d6152018-06-06 15:06:01 -0700109 BOOST_CHECK_EQUAL(frags[1].get<lp::FragCountField>(), 5);
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700110 BOOST_CHECK_LE(frags[1].wireEncode().size(), mtu);
111 ndn::Buffer::const_iterator frag1Begin, frag1End;
112 std::tie(frag1Begin, frag1End) = frags[1].get<lp::FragmentField>();
113 BOOST_REQUIRE_LE(std::distance(frag1Begin, frag1End),
114 std::distance(reassembledPos, reassembledPayload.end()));
Eric Newberry812d6152018-06-06 15:06:01 -0700115 reassembledPos = std::copy(frag1Begin, frag1End, reassembledPos);
116
117 BOOST_CHECK(frags[2].has<lp::FragmentField>());
118 BOOST_CHECK(!frags[2].has<lp::IncomingFaceIdField>());
119 BOOST_CHECK_EQUAL(frags[2].get<lp::FragIndexField>(), 2);
120 BOOST_CHECK_EQUAL(frags[2].get<lp::FragCountField>(), 5);
121 BOOST_CHECK_LE(frags[2].wireEncode().size(), mtu);
122 ndn::Buffer::const_iterator frag2Begin, frag2End;
123 std::tie(frag2Begin, frag2End) = frags[2].get<lp::FragmentField>();
124 BOOST_REQUIRE_LE(std::distance(frag2Begin, frag2End),
125 std::distance(reassembledPos, reassembledPayload.end()));
126 reassembledPos = std::copy(frag2Begin, frag2End, reassembledPos);
127
128 BOOST_CHECK(frags[3].has<lp::FragmentField>());
129 BOOST_CHECK(!frags[3].has<lp::IncomingFaceIdField>());
130 BOOST_CHECK_EQUAL(frags[3].get<lp::FragIndexField>(), 3);
131 BOOST_CHECK_EQUAL(frags[3].get<lp::FragCountField>(), 5);
132 BOOST_CHECK_LE(frags[3].wireEncode().size(), mtu);
133 ndn::Buffer::const_iterator frag3Begin, frag3End;
134 std::tie(frag3Begin, frag3End) = frags[3].get<lp::FragmentField>();
135 BOOST_REQUIRE_LE(std::distance(frag3Begin, frag3End),
136 std::distance(reassembledPos, reassembledPayload.end()));
137 reassembledPos = std::copy(frag3Begin, frag3End, reassembledPos);
138
139 BOOST_CHECK(frags[4].has<lp::FragmentField>());
140 BOOST_CHECK(!frags[4].has<lp::IncomingFaceIdField>());
141 BOOST_CHECK_EQUAL(frags[4].get<lp::FragIndexField>(), 4);
142 BOOST_CHECK_EQUAL(frags[4].get<lp::FragCountField>(), 5);
143 BOOST_CHECK_LE(frags[4].wireEncode().size(), mtu);
144 ndn::Buffer::const_iterator frag4Begin, frag4End;
145 std::tie(frag4Begin, frag4End) = frags[4].get<lp::FragmentField>();
146 BOOST_REQUIRE_LE(std::distance(frag4Begin, frag4End),
147 std::distance(reassembledPos, reassembledPayload.end()));
148 std::copy(frag4Begin, frag4End, reassembledPos);
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700149
150 BOOST_CHECK_EQUAL_COLLECTIONS(data->wireEncode().begin(), data->wireEncode().end(),
151 reassembledPayload.begin(), reassembledPayload.end());
152}
153
154BOOST_AUTO_TEST_CASE(FragmentMtuTooSmall)
155{
156 size_t mtu = 20;
Eric Newberrycb6551e2020-03-02 14:12:16 -0800157 BOOST_ASSERT(mtu < MIN_MTU);
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700158
159 lp::Packet packet;
160 packet.add<lp::IncomingFaceIdField>(123);
161
Davide Pesavento5bafd2c2020-07-28 16:16:27 -0400162 auto data = makeData("/test/data123/123456789/987654321/123456789");
163 packet.add<lp::FragmentField>({data->wireEncode().begin(), data->wireEncode().end()});
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700164
165 bool isOk = false;
166 std::tie(isOk, std::ignore) = fragmenter.fragmentPacket(packet, mtu);
Davide Pesavento5bafd2c2020-07-28 16:16:27 -0400167 BOOST_CHECK_EQUAL(isOk, false);
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700168}
169
170BOOST_AUTO_TEST_CASE(FragmentOverFragCount)
171{
172 LpFragmenter::Options options;
173 options.nMaxFragments = 2;
174 fragmenter.setOptions(options);
175
176 size_t mtu = 70;
177
178 lp::Packet packet;
179 packet.add<lp::IncomingFaceIdField>(123);
180
Davide Pesavento5bafd2c2020-07-28 16:16:27 -0400181 auto data = makeData("/test/data123/123456789/987654321/123456789");
182 packet.add<lp::FragmentField>({data->wireEncode().begin(), data->wireEncode().end()});
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700183
184 bool isOk = false;
185 std::tie(isOk, std::ignore) = fragmenter.fragmentPacket(packet, mtu);
Davide Pesavento5bafd2c2020-07-28 16:16:27 -0400186 BOOST_CHECK_EQUAL(isOk, false);
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700187}
188
189BOOST_AUTO_TEST_SUITE_END() // TestLpFragmentation
190BOOST_AUTO_TEST_SUITE_END() // Face
191
192} // namespace tests
193} // namespace face
194} // namespace nfd