blob: 4bc7022aecf6efc08a3ba773cf328c7d72e0f74b [file] [log] [blame]
Eric Newberry4c3e6b82015-11-10 16:48:42 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014-2015, Regents of the University of California,
4 * 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
36BOOST_AUTO_TEST_SUITE(Face)
37
38class LpFragmenterFixture
39{
40public:
41 LpFragmenter fragmenter;
42};
43
44BOOST_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{
78 size_t mtu = 90;
79
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);
93 BOOST_REQUIRE_EQUAL(frags.size(), 2);
94
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);
100 BOOST_CHECK_EQUAL(frags[0].get<lp::FragCountField>(), 2);
101 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);
110 BOOST_CHECK_EQUAL(frags[1].get<lp::FragCountField>(), 2);
111 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()));
116 std::copy(frag1Begin, frag1End, reassembledPos);
117
118 BOOST_CHECK_EQUAL_COLLECTIONS(data->wireEncode().begin(), data->wireEncode().end(),
119 reassembledPayload.begin(), reassembledPayload.end());
120}
121
122BOOST_AUTO_TEST_CASE(FragmentMtuTooSmall)
123{
124 size_t mtu = 20;
125
126 lp::Packet packet;
127 packet.add<lp::IncomingFaceIdField>(123);
128
129 shared_ptr<Data> data = makeData("/test/data1/123456789/987654321/123456789");
130 packet.add<lp::FragmentField>(std::make_pair(data->wireEncode().begin(),
131 data->wireEncode().end()));
132
133 bool isOk = false;
134 std::tie(isOk, std::ignore) = fragmenter.fragmentPacket(packet, mtu);
135 BOOST_REQUIRE(!isOk);
136}
137
138BOOST_AUTO_TEST_CASE(FragmentOverFragCount)
139{
140 LpFragmenter::Options options;
141 options.nMaxFragments = 2;
142 fragmenter.setOptions(options);
143
144 size_t mtu = 70;
145
146 lp::Packet packet;
147 packet.add<lp::IncomingFaceIdField>(123);
148
149 shared_ptr<Data> data = makeData("/test/data1/123456789/987654321/123456789");
150 packet.add<lp::FragmentField>(std::make_pair(data->wireEncode().begin(),
151 data->wireEncode().end()));
152
153 bool isOk = false;
154 std::tie(isOk, std::ignore) = fragmenter.fragmentPacket(packet, mtu);
155 BOOST_REQUIRE(!isOk);
156}
157
158BOOST_AUTO_TEST_SUITE_END() // TestLpFragmentation
159BOOST_AUTO_TEST_SUITE_END() // Face
160
161} // namespace tests
162} // namespace face
163} // namespace nfd