blob: 4bc8607de85b7dbf02eb38b498d5c15b65265f69 [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/*
3 * Copyright (c) 2014-2018, 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"
30
31namespace nfd {
32namespace face {
33namespace tests {
34
35using namespace nfd::tests;
36
Davide Pesaventoe4b22382018-06-10 14:37:24 -040037class LpFragmenterFixture : public BaseFixture
Eric Newberry4c3e6b82015-11-10 16:48:42 -070038{
Davide Pesaventoe4b22382018-06-10 14:37:24 -040039protected:
40 LpFragmenter fragmenter{{}};
Eric Newberry4c3e6b82015-11-10 16:48:42 -070041};
42
Davide Pesaventoe4b22382018-06-10 14:37:24 -040043BOOST_AUTO_TEST_SUITE(Face)
Eric Newberry4c3e6b82015-11-10 16:48:42 -070044BOOST_FIXTURE_TEST_SUITE(TestLpFragmenter, LpFragmenterFixture)
45
46BOOST_AUTO_TEST_CASE(FragmentSingleFragment)
47{
48 size_t mtu = 256;
49
50 lp::Packet packet;
51 packet.add<lp::IncomingFaceIdField>(123);
52
53 shared_ptr<Data> data = makeData("/test/data1");
54 BOOST_REQUIRE_EQUAL(data->wireEncode().size(), 30);
55 packet.add<lp::FragmentField>(std::make_pair(data->wireEncode().begin(),
56 data->wireEncode().end()));
57
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 Newberry812d6152018-06-06 15:06:01 -070078 size_t mtu = Transport::MIN_MTU;
Eric Newberry4c3e6b82015-11-10 16:48:42 -070079
80 lp::Packet packet;
81 packet.add<lp::IncomingFaceIdField>(123);
82
83 shared_ptr<Data> data = makeData("/test/data1/123456789/987654321/123456789");
84 BOOST_REQUIRE_EQUAL(data->wireEncode().size(), 63);
85 packet.add<lp::FragmentField>(std::make_pair(data->wireEncode().begin(),
86 data->wireEncode().end()));
87
88 bool isOk = false;
89 std::vector<lp::Packet> frags;
90 std::tie(isOk, frags) = fragmenter.fragmentPacket(packet, mtu);
91
92 BOOST_REQUIRE(isOk);
Eric Newberry812d6152018-06-06 15:06:01 -070093 BOOST_REQUIRE_EQUAL(frags.size(), 5);
Eric Newberry4c3e6b82015-11-10 16:48:42 -070094
95 ndn::Buffer reassembledPayload(63);
96
97 BOOST_CHECK(frags[0].has<lp::FragmentField>());
98 BOOST_CHECK_EQUAL(frags[0].get<lp::IncomingFaceIdField>(), 123);
99 BOOST_CHECK_EQUAL(frags[0].get<lp::FragIndexField>(), 0);
Eric Newberry812d6152018-06-06 15:06:01 -0700100 BOOST_CHECK_EQUAL(frags[0].get<lp::FragCountField>(), 5);
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700101 BOOST_CHECK_LE(frags[0].wireEncode().size(), mtu);
102 ndn::Buffer::const_iterator frag0Begin, frag0End;
103 std::tie(frag0Begin, frag0End) = frags[0].get<lp::FragmentField>();
104 BOOST_REQUIRE_LE(std::distance(frag0Begin, frag0End), reassembledPayload.size());
105 auto reassembledPos = std::copy(frag0Begin, frag0End, reassembledPayload.begin());
106
107 BOOST_CHECK(frags[1].has<lp::FragmentField>());
108 BOOST_CHECK(!frags[1].has<lp::IncomingFaceIdField>());
109 BOOST_CHECK_EQUAL(frags[1].get<lp::FragIndexField>(), 1);
Eric Newberry812d6152018-06-06 15:06:01 -0700110 BOOST_CHECK_EQUAL(frags[1].get<lp::FragCountField>(), 5);
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700111 BOOST_CHECK_LE(frags[1].wireEncode().size(), mtu);
112 ndn::Buffer::const_iterator frag1Begin, frag1End;
113 std::tie(frag1Begin, frag1End) = frags[1].get<lp::FragmentField>();
114 BOOST_REQUIRE_LE(std::distance(frag1Begin, frag1End),
115 std::distance(reassembledPos, reassembledPayload.end()));
Eric Newberry812d6152018-06-06 15:06:01 -0700116 reassembledPos = std::copy(frag1Begin, frag1End, reassembledPos);
117
118 BOOST_CHECK(frags[2].has<lp::FragmentField>());
119 BOOST_CHECK(!frags[2].has<lp::IncomingFaceIdField>());
120 BOOST_CHECK_EQUAL(frags[2].get<lp::FragIndexField>(), 2);
121 BOOST_CHECK_EQUAL(frags[2].get<lp::FragCountField>(), 5);
122 BOOST_CHECK_LE(frags[2].wireEncode().size(), mtu);
123 ndn::Buffer::const_iterator frag2Begin, frag2End;
124 std::tie(frag2Begin, frag2End) = frags[2].get<lp::FragmentField>();
125 BOOST_REQUIRE_LE(std::distance(frag2Begin, frag2End),
126 std::distance(reassembledPos, reassembledPayload.end()));
127 reassembledPos = std::copy(frag2Begin, frag2End, reassembledPos);
128
129 BOOST_CHECK(frags[3].has<lp::FragmentField>());
130 BOOST_CHECK(!frags[3].has<lp::IncomingFaceIdField>());
131 BOOST_CHECK_EQUAL(frags[3].get<lp::FragIndexField>(), 3);
132 BOOST_CHECK_EQUAL(frags[3].get<lp::FragCountField>(), 5);
133 BOOST_CHECK_LE(frags[3].wireEncode().size(), mtu);
134 ndn::Buffer::const_iterator frag3Begin, frag3End;
135 std::tie(frag3Begin, frag3End) = frags[3].get<lp::FragmentField>();
136 BOOST_REQUIRE_LE(std::distance(frag3Begin, frag3End),
137 std::distance(reassembledPos, reassembledPayload.end()));
138 reassembledPos = std::copy(frag3Begin, frag3End, reassembledPos);
139
140 BOOST_CHECK(frags[4].has<lp::FragmentField>());
141 BOOST_CHECK(!frags[4].has<lp::IncomingFaceIdField>());
142 BOOST_CHECK_EQUAL(frags[4].get<lp::FragIndexField>(), 4);
143 BOOST_CHECK_EQUAL(frags[4].get<lp::FragCountField>(), 5);
144 BOOST_CHECK_LE(frags[4].wireEncode().size(), mtu);
145 ndn::Buffer::const_iterator frag4Begin, frag4End;
146 std::tie(frag4Begin, frag4End) = frags[4].get<lp::FragmentField>();
147 BOOST_REQUIRE_LE(std::distance(frag4Begin, frag4End),
148 std::distance(reassembledPos, reassembledPayload.end()));
149 std::copy(frag4Begin, frag4End, reassembledPos);
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700150
151 BOOST_CHECK_EQUAL_COLLECTIONS(data->wireEncode().begin(), data->wireEncode().end(),
152 reassembledPayload.begin(), reassembledPayload.end());
153}
154
155BOOST_AUTO_TEST_CASE(FragmentMtuTooSmall)
156{
157 size_t mtu = 20;
Eric Newberry812d6152018-06-06 15:06:01 -0700158 BOOST_ASSERT(mtu < Transport::MIN_MTU);
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700159
160 lp::Packet packet;
161 packet.add<lp::IncomingFaceIdField>(123);
162
163 shared_ptr<Data> data = makeData("/test/data1/123456789/987654321/123456789");
164 packet.add<lp::FragmentField>(std::make_pair(data->wireEncode().begin(),
165 data->wireEncode().end()));
166
167 bool isOk = false;
168 std::tie(isOk, std::ignore) = fragmenter.fragmentPacket(packet, mtu);
169 BOOST_REQUIRE(!isOk);
170}
171
172BOOST_AUTO_TEST_CASE(FragmentOverFragCount)
173{
174 LpFragmenter::Options options;
175 options.nMaxFragments = 2;
176 fragmenter.setOptions(options);
177
178 size_t mtu = 70;
179
180 lp::Packet packet;
181 packet.add<lp::IncomingFaceIdField>(123);
182
183 shared_ptr<Data> data = makeData("/test/data1/123456789/987654321/123456789");
184 packet.add<lp::FragmentField>(std::make_pair(data->wireEncode().begin(),
185 data->wireEncode().end()));
186
187 bool isOk = false;
188 std::tie(isOk, std::ignore) = fragmenter.fragmentPacket(packet, mtu);
189 BOOST_REQUIRE(!isOk);
190}
191
192BOOST_AUTO_TEST_SUITE_END() // TestLpFragmentation
193BOOST_AUTO_TEST_SUITE_END() // Face
194
195} // namespace tests
196} // namespace face
197} // namespace nfd