blob: 7e38a62ec4b8d2e1677f497fc2929136889e7089 [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
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
77BOOST_AUTO_TEST_CASE(FragmentMultipleFragments)
78{
Eric Newberrycb6551e2020-03-02 14:12:16 -080079 size_t mtu = MIN_MTU;
Eric Newberry4c3e6b82015-11-10 16:48:42 -070080
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 Newberry812d6152018-06-06 15:06:01 -070094 BOOST_REQUIRE_EQUAL(frags.size(), 5);
Eric Newberry4c3e6b82015-11-10 16:48:42 -070095
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 Newberry812d6152018-06-06 15:06:01 -0700101 BOOST_CHECK_EQUAL(frags[0].get<lp::FragCountField>(), 5);
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700102 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 Newberry812d6152018-06-06 15:06:01 -0700111 BOOST_CHECK_EQUAL(frags[1].get<lp::FragCountField>(), 5);
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700112 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 Newberry812d6152018-06-06 15:06:01 -0700117 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 Newberry4c3e6b82015-11-10 16:48:42 -0700151
152 BOOST_CHECK_EQUAL_COLLECTIONS(data->wireEncode().begin(), data->wireEncode().end(),
153 reassembledPayload.begin(), reassembledPayload.end());
154}
155
156BOOST_AUTO_TEST_CASE(FragmentMtuTooSmall)
157{
158 size_t mtu = 20;
Eric Newberrycb6551e2020-03-02 14:12:16 -0800159 BOOST_ASSERT(mtu < MIN_MTU);
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700160
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
173BOOST_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
193BOOST_AUTO_TEST_SUITE_END() // TestLpFragmentation
194BOOST_AUTO_TEST_SUITE_END() // Face
195
196} // namespace tests
197} // namespace face
198} // namespace nfd