blob: 2027d41799b5970422f087efe96e5d948aaeeaba [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);
54 lp::Packet lpPacket(interest.wireEncode());
55 nfd::face::Transport::Packet packet(lpPacket.wireEncode());
56 BlockHeader header(packet);
57
58 BOOST_CHECK_EQUAL(header.GetSerializedSize(), 18); // 18
59
60 {
61 Ptr<Packet> packet = Create<Packet>();
62 packet->AddHeader(header);
63 boost::test_tools::output_test_stream output;
64 packet->Print(output);
65 BOOST_CHECK(output.is_equal("ns3::ndn::Packet (Interest: /prefix?ndn.Nonce=10)"));
66 }
67}
68
69BOOST_AUTO_TEST_CASE(EncodePrintData)
70{
71 Data data("/other/prefix");
72 data.setFreshnessPeriod(ndn::time::milliseconds(1000));
73 data.setContent(std::make_shared< ::ndn::Buffer>(1024));
74 ndn::StackHelper::getKeyChain().sign(data);
75 lp::Packet lpPacket(data.wireEncode());
76 nfd::face::Transport::Packet packet(lpPacket.wireEncode());
77 BlockHeader header(packet);
78
79 BOOST_CHECK_EQUAL(header.GetSerializedSize(), 1369);
80
81 {
82 Ptr<Packet> packet = Create<Packet>();
83 packet->AddHeader(header);
84 boost::test_tools::output_test_stream output;
85 packet->Print(output);
86 BOOST_CHECK(output.is_equal("ns3::ndn::Packet (Data: /other/prefix)"));
87 }
88}
89
90BOOST_AUTO_TEST_CASE(PrintLpPacket)
91{
92 Interest interest("/prefix");
93 interest.setNonce(10);
94
95 lp::Packet lpPacket;
96 lpPacket.add<::ndn::lp::SequenceField>(0); // to make sure that the NDNLP header is added
97 lpPacket.add<::ndn::lp::FragmentField>(std::make_pair(interest.wireEncode().begin(), interest.wireEncode().end()));
98
99 {
100 BlockHeader header(nfd::face::Transport::Packet(lpPacket.wireEncode()));
101 Ptr<Packet> packet = Create<Packet>();
102 packet->AddHeader(header);
103 boost::test_tools::output_test_stream output;
104 packet->Print(output);
105 BOOST_CHECK(output.is_equal("ns3::ndn::Packet (NDNLP(Interest: /prefix?ndn.Nonce=10))"));
106 }
107
108 lpPacket.add<::ndn::lp::NackField>(::ndn::lp::NackHeader().setReason(::ndn::lp::NackReason::NO_ROUTE));
109
110 {
111 BlockHeader header(nfd::face::Transport::Packet(lpPacket.wireEncode()));
112 Ptr<Packet> packet = Create<Packet>();
113 packet->AddHeader(header);
114 boost::test_tools::output_test_stream output;
115 packet->Print(output);
116 BOOST_CHECK(output.is_equal("ns3::ndn::Packet (NDNLP(NACK(NoRoute) for Interest: /prefix?ndn.Nonce=10))"));
117 }
118
119 lpPacket.remove<::ndn::lp::NackField>();
120 lpPacket.add<::ndn::lp::FragIndexField>(0);
121 lpPacket.add<::ndn::lp::FragCountField>(1);
122
123 {
124 BlockHeader header(nfd::face::Transport::Packet(lpPacket.wireEncode()));
125 Ptr<Packet> packet = Create<Packet>();
126 packet->AddHeader(header);
127 boost::test_tools::output_test_stream output;
128 packet->Print(output);
129 BOOST_CHECK(output.is_equal("ns3::ndn::Packet (NDNLP(Interest: /prefix?ndn.Nonce=10))"));
130 }
131
132 lpPacket.set<::ndn::lp::FragCountField>(2);
133
134 {
135 BlockHeader header(nfd::face::Transport::Packet(lpPacket.wireEncode()));
136 Ptr<Packet> packet = Create<Packet>();
137 packet->AddHeader(header);
138 boost::test_tools::output_test_stream output;
139 packet->Print(output);
140 BOOST_CHECK(output.is_equal("ns3::ndn::Packet (NDNLP(fragment 1 out of 2))"));
141 }
142
143 ::ndn::Buffer buf(10);
144 lpPacket.set<::ndn::lp::FragmentField>(std::make_pair(buf.begin(), buf.end()));
145 lpPacket.remove<::ndn::lp::FragCountField>();
146 lpPacket.remove<::ndn::lp::FragIndexField>();
147
148 {
149 BlockHeader header(nfd::face::Transport::Packet(lpPacket.wireEncode()));
150 Ptr<Packet> packet = Create<Packet>();
151 packet->AddHeader(header);
152 boost::test_tools::output_test_stream output;
153 packet->Print(output);
154 BOOST_CHECK(output.is_equal("ns3::ndn::Packet (NDNLP(Unrecognized))"));
155 }
156}
157
158BOOST_AUTO_TEST_SUITE_END()
159
160} // namespace ndn
161} // namespace ns3