blob: b3ee8d62686cdd8ff4bbba3b89f0fc22cff58585 [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"
27
28#include "tests/test-common.hpp"
29
30namespace nfd {
31namespace face {
32namespace tests {
33
34using namespace nfd::tests;
35
Davide Pesaventoe4b22382018-06-10 14:37:24 -040036class LpFragmenterFixture : public BaseFixture
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
45BOOST_AUTO_TEST_CASE(FragmentSingleFragment)
46{
47 size_t mtu = 256;
48
49 lp::Packet packet;
50 packet.add<lp::IncomingFaceIdField>(123);
51
52 shared_ptr<Data> data = makeData("/test/data1");
53 BOOST_REQUIRE_EQUAL(data->wireEncode().size(), 30);
54 packet.add<lp::FragmentField>(std::make_pair(data->wireEncode().begin(),
55 data->wireEncode().end()));
56
57 bool isOk = false;
58 std::vector<lp::Packet> frags;
59 std::tie(isOk, frags) = fragmenter.fragmentPacket(packet, mtu);
60
61 BOOST_REQUIRE(isOk);
62 BOOST_REQUIRE_EQUAL(frags.size(), 1);
63 BOOST_CHECK(frags[0].has<lp::FragmentField>());
64 BOOST_CHECK_EQUAL(frags[0].get<lp::IncomingFaceIdField>(), 123);
65 BOOST_CHECK(!frags[0].has<lp::FragIndexField>());
66 BOOST_CHECK(!frags[0].has<lp::FragCountField>());
67 BOOST_CHECK_LE(frags[0].wireEncode().size(), mtu);
68
69 ndn::Buffer::const_iterator fragBegin, fragEnd;
70 std::tie(fragBegin, fragEnd) = frags[0].get<lp::FragmentField>();
71 BOOST_CHECK_EQUAL_COLLECTIONS(data->wireEncode().begin(), data->wireEncode().end(),
72 fragBegin, fragEnd);
73}
74
75BOOST_AUTO_TEST_CASE(FragmentMultipleFragments)
76{
77 size_t mtu = 90;
78
79 lp::Packet packet;
80 packet.add<lp::IncomingFaceIdField>(123);
81
82 shared_ptr<Data> data = makeData("/test/data1/123456789/987654321/123456789");
83 BOOST_REQUIRE_EQUAL(data->wireEncode().size(), 63);
84 packet.add<lp::FragmentField>(std::make_pair(data->wireEncode().begin(),
85 data->wireEncode().end()));
86
87 bool isOk = false;
88 std::vector<lp::Packet> frags;
89 std::tie(isOk, frags) = fragmenter.fragmentPacket(packet, mtu);
90
91 BOOST_REQUIRE(isOk);
92 BOOST_REQUIRE_EQUAL(frags.size(), 2);
93
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);
99 BOOST_CHECK_EQUAL(frags[0].get<lp::FragCountField>(), 2);
100 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);
109 BOOST_CHECK_EQUAL(frags[1].get<lp::FragCountField>(), 2);
110 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()));
115 std::copy(frag1Begin, frag1End, reassembledPos);
116
117 BOOST_CHECK_EQUAL_COLLECTIONS(data->wireEncode().begin(), data->wireEncode().end(),
118 reassembledPayload.begin(), reassembledPayload.end());
119}
120
121BOOST_AUTO_TEST_CASE(FragmentMtuTooSmall)
122{
123 size_t mtu = 20;
124
125 lp::Packet packet;
126 packet.add<lp::IncomingFaceIdField>(123);
127
128 shared_ptr<Data> data = makeData("/test/data1/123456789/987654321/123456789");
129 packet.add<lp::FragmentField>(std::make_pair(data->wireEncode().begin(),
130 data->wireEncode().end()));
131
132 bool isOk = false;
133 std::tie(isOk, std::ignore) = fragmenter.fragmentPacket(packet, mtu);
134 BOOST_REQUIRE(!isOk);
135}
136
137BOOST_AUTO_TEST_CASE(FragmentOverFragCount)
138{
139 LpFragmenter::Options options;
140 options.nMaxFragments = 2;
141 fragmenter.setOptions(options);
142
143 size_t mtu = 70;
144
145 lp::Packet packet;
146 packet.add<lp::IncomingFaceIdField>(123);
147
148 shared_ptr<Data> data = makeData("/test/data1/123456789/987654321/123456789");
149 packet.add<lp::FragmentField>(std::make_pair(data->wireEncode().begin(),
150 data->wireEncode().end()));
151
152 bool isOk = false;
153 std::tie(isOk, std::ignore) = fragmenter.fragmentPacket(packet, mtu);
154 BOOST_REQUIRE(!isOk);
155}
156
157BOOST_AUTO_TEST_SUITE_END() // TestLpFragmentation
158BOOST_AUTO_TEST_SUITE_END() // Face
159
160} // namespace tests
161} // namespace face
162} // namespace nfd