blob: 5dc65f14e1e1222a158414fc89f1197df0708517 [file] [log] [blame]
Spyridon Mastorakis5ea33222016-12-07 14:33:53 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2011-2015 Regents of the University of California.
4 *
5 * This file is part of ndnSIM. See AUTHORS for complete list of ndnSIM authors and
6 * contributors.
7 *
8 * ndnSIM is free software: you can redistribute it and/or modify it under the terms
9 * of the GNU General Public License as published by the Free Software Foundation,
10 * either version 3 of the License, or (at your option) any later version.
11 *
12 * ndnSIM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * ndnSIM, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18 **/
19
20#include "model/ndn-block-header.hpp"
21#include "helper/ndn-stack-helper.hpp"
22
23#include <ndn-cxx/lp/packet.hpp>
24
25#include "ns3/ndnSIM/NFD/daemon/face/transport.hpp"
26#include "ns3/packet.h"
27
28#include "../tests-common.hpp"
29
30namespace ns3 {
31namespace ndn {
32
33BOOST_FIXTURE_TEST_SUITE(ModelNdnBlockHeader, CleanupFixture)
34
35class EnablePacketPrintingFixture
36{
37public:
38 EnablePacketPrintingFixture()
39 {
40 Packet::EnablePrinting();
41 }
42};
43
44BOOST_GLOBAL_FIXTURE(EnablePacketPrintingFixture)
45#if BOOST_VERSION >= 105900
46;
47#endif // BOOST_VERSION >= 105900
48
49
50BOOST_AUTO_TEST_CASE(EncodePrintInterest)
51{
52 Interest interest("/prefix");
53 interest.setNonce(10);
Alexander Afanasyevdc3c3a32019-02-17 20:17:32 -050054 interest.setCanBePrefix(true);
Spyridon Mastorakis5ea33222016-12-07 14:33:53 -080055 lp::Packet lpPacket(interest.wireEncode());
56 nfd::face::Transport::Packet packet(lpPacket.wireEncode());
57 BlockHeader header(packet);
58
59 BOOST_CHECK_EQUAL(header.GetSerializedSize(), 18); // 18
60
61 {
62 Ptr<Packet> packet = Create<Packet>();
63 packet->AddHeader(header);
64 boost::test_tools::output_test_stream output;
65 packet->Print(output);
66 BOOST_CHECK(output.is_equal("ns3::ndn::Packet (Interest: /prefix?ndn.Nonce=10)"));
67 }
68}
69
70BOOST_AUTO_TEST_CASE(EncodePrintData)
71{
72 Data data("/other/prefix");
73 data.setFreshnessPeriod(ndn::time::milliseconds(1000));
74 data.setContent(std::make_shared< ::ndn::Buffer>(1024));
75 ndn::StackHelper::getKeyChain().sign(data);
76 lp::Packet lpPacket(data.wireEncode());
77 nfd::face::Transport::Packet packet(lpPacket.wireEncode());
78 BlockHeader header(packet);
79
Spyridon Mastorakisf6d32852017-09-27 20:28:52 -070080 BOOST_CHECK_EQUAL(header.GetSerializedSize(), 1350);
Spyridon Mastorakis5ea33222016-12-07 14:33:53 -080081
82 {
83 Ptr<Packet> packet = Create<Packet>();
84 packet->AddHeader(header);
85 boost::test_tools::output_test_stream output;
86 packet->Print(output);
87 BOOST_CHECK(output.is_equal("ns3::ndn::Packet (Data: /other/prefix)"));
88 }
89}
90
91BOOST_AUTO_TEST_CASE(PrintLpPacket)
92{
93 Interest interest("/prefix");
94 interest.setNonce(10);
Alexander Afanasyevdc3c3a32019-02-17 20:17:32 -050095 interest.setCanBePrefix(true);
Spyridon Mastorakis5ea33222016-12-07 14:33:53 -080096
97 lp::Packet lpPacket;
98 lpPacket.add<::ndn::lp::SequenceField>(0); // to make sure that the NDNLP header is added
99 lpPacket.add<::ndn::lp::FragmentField>(std::make_pair(interest.wireEncode().begin(), interest.wireEncode().end()));
100
101 {
102 BlockHeader header(nfd::face::Transport::Packet(lpPacket.wireEncode()));
103 Ptr<Packet> packet = Create<Packet>();
104 packet->AddHeader(header);
105 boost::test_tools::output_test_stream output;
106 packet->Print(output);
107 BOOST_CHECK(output.is_equal("ns3::ndn::Packet (NDNLP(Interest: /prefix?ndn.Nonce=10))"));
108 }
109
110 lpPacket.add<::ndn::lp::NackField>(::ndn::lp::NackHeader().setReason(::ndn::lp::NackReason::NO_ROUTE));
111
112 {
113 BlockHeader header(nfd::face::Transport::Packet(lpPacket.wireEncode()));
114 Ptr<Packet> packet = Create<Packet>();
115 packet->AddHeader(header);
116 boost::test_tools::output_test_stream output;
117 packet->Print(output);
118 BOOST_CHECK(output.is_equal("ns3::ndn::Packet (NDNLP(NACK(NoRoute) for Interest: /prefix?ndn.Nonce=10))"));
119 }
120
121 lpPacket.remove<::ndn::lp::NackField>();
122 lpPacket.add<::ndn::lp::FragIndexField>(0);
123 lpPacket.add<::ndn::lp::FragCountField>(1);
124
125 {
126 BlockHeader header(nfd::face::Transport::Packet(lpPacket.wireEncode()));
127 Ptr<Packet> packet = Create<Packet>();
128 packet->AddHeader(header);
129 boost::test_tools::output_test_stream output;
130 packet->Print(output);
131 BOOST_CHECK(output.is_equal("ns3::ndn::Packet (NDNLP(Interest: /prefix?ndn.Nonce=10))"));
132 }
133
134 lpPacket.set<::ndn::lp::FragCountField>(2);
135
136 {
137 BlockHeader header(nfd::face::Transport::Packet(lpPacket.wireEncode()));
138 Ptr<Packet> packet = Create<Packet>();
139 packet->AddHeader(header);
140 boost::test_tools::output_test_stream output;
141 packet->Print(output);
142 BOOST_CHECK(output.is_equal("ns3::ndn::Packet (NDNLP(fragment 1 out of 2))"));
143 }
144
145 ::ndn::Buffer buf(10);
146 lpPacket.set<::ndn::lp::FragmentField>(std::make_pair(buf.begin(), buf.end()));
147 lpPacket.remove<::ndn::lp::FragCountField>();
148 lpPacket.remove<::ndn::lp::FragIndexField>();
149
150 {
151 BlockHeader header(nfd::face::Transport::Packet(lpPacket.wireEncode()));
152 Ptr<Packet> packet = Create<Packet>();
153 packet->AddHeader(header);
154 boost::test_tools::output_test_stream output;
155 packet->Print(output);
156 BOOST_CHECK(output.is_equal("ns3::ndn::Packet (NDNLP(Unrecognized))"));
157 }
158}
159
160BOOST_AUTO_TEST_SUITE_END()
161
162} // namespace ndn
163} // namespace ns3