blob: 9360998ef4b6d1432bd0e08d7b4841735fdff9cb [file] [log] [blame]
Spyridon Mastorakis5ea33222016-12-07 14:33:53 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev0ae7eae2020-05-30 21:16:36 -04003 * Copyright (c) 2011-2019 Regents of the University of California.
Spyridon Mastorakis5ea33222016-12-07 14:33:53 -08004 *
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>
Alexander Afanasyev0ae7eae2020-05-30 21:16:36 -040024#include <ndn-cxx/encoding/block.hpp>
Spyridon Mastorakis5ea33222016-12-07 14:33:53 -080025
26#include "ns3/ndnSIM/NFD/daemon/face/transport.hpp"
27#include "ns3/packet.h"
28
29#include "../tests-common.hpp"
30
31namespace ns3 {
32namespace ndn {
33
Alexander Afanasyev0ae7eae2020-05-30 21:16:36 -040034using ::ndn::operator "" _block;
35
Spyridon Mastorakis5ea33222016-12-07 14:33:53 -080036BOOST_FIXTURE_TEST_SUITE(ModelNdnBlockHeader, CleanupFixture)
37
38class EnablePacketPrintingFixture
39{
40public:
41 EnablePacketPrintingFixture()
42 {
43 Packet::EnablePrinting();
44 }
45};
46
47BOOST_GLOBAL_FIXTURE(EnablePacketPrintingFixture)
48#if BOOST_VERSION >= 105900
49;
50#endif // BOOST_VERSION >= 105900
51
52
53BOOST_AUTO_TEST_CASE(EncodePrintInterest)
54{
55 Interest interest("/prefix");
56 interest.setNonce(10);
Alexander Afanasyevdc3c3a32019-02-17 20:17:32 -050057 interest.setCanBePrefix(true);
Spyridon Mastorakis5ea33222016-12-07 14:33:53 -080058 lp::Packet lpPacket(interest.wireEncode());
Alexander Afanasyev59090db2020-02-21 16:37:43 -050059 auto packet(lpPacket.wireEncode());
Spyridon Mastorakis5ea33222016-12-07 14:33:53 -080060 BlockHeader header(packet);
61
Alexander Afanasyev59090db2020-02-21 16:37:43 -050062 BOOST_CHECK_EQUAL(header.GetSerializedSize(), 20); // 20
Spyridon Mastorakis5ea33222016-12-07 14:33:53 -080063
64 {
65 Ptr<Packet> packet = Create<Packet>();
66 packet->AddHeader(header);
67 boost::test_tools::output_test_stream output;
68 packet->Print(output);
Alexander Afanasyev59090db2020-02-21 16:37:43 -050069 BOOST_CHECK(output.is_equal("ns3::ndn::Packet (Interest: /prefix?CanBePrefix&Nonce=10)"));
Spyridon Mastorakis5ea33222016-12-07 14:33:53 -080070 }
71}
72
73BOOST_AUTO_TEST_CASE(EncodePrintData)
74{
75 Data data("/other/prefix");
76 data.setFreshnessPeriod(ndn::time::milliseconds(1000));
77 data.setContent(std::make_shared< ::ndn::Buffer>(1024));
78 ndn::StackHelper::getKeyChain().sign(data);
79 lp::Packet lpPacket(data.wireEncode());
Alexander Afanasyev59090db2020-02-21 16:37:43 -050080 auto packet(lpPacket.wireEncode());
Spyridon Mastorakis5ea33222016-12-07 14:33:53 -080081 BlockHeader header(packet);
82
Spyridon Mastorakisf6d32852017-09-27 20:28:52 -070083 BOOST_CHECK_EQUAL(header.GetSerializedSize(), 1350);
Spyridon Mastorakis5ea33222016-12-07 14:33:53 -080084
85 {
86 Ptr<Packet> packet = Create<Packet>();
87 packet->AddHeader(header);
88 boost::test_tools::output_test_stream output;
89 packet->Print(output);
90 BOOST_CHECK(output.is_equal("ns3::ndn::Packet (Data: /other/prefix)"));
91 }
92}
93
94BOOST_AUTO_TEST_CASE(PrintLpPacket)
95{
96 Interest interest("/prefix");
97 interest.setNonce(10);
Alexander Afanasyevdc3c3a32019-02-17 20:17:32 -050098 interest.setCanBePrefix(true);
Spyridon Mastorakis5ea33222016-12-07 14:33:53 -080099
100 lp::Packet lpPacket;
101 lpPacket.add<::ndn::lp::SequenceField>(0); // to make sure that the NDNLP header is added
102 lpPacket.add<::ndn::lp::FragmentField>(std::make_pair(interest.wireEncode().begin(), interest.wireEncode().end()));
103
104 {
Alexander Afanasyev59090db2020-02-21 16:37:43 -0500105 BlockHeader header(lpPacket.wireEncode());
Spyridon Mastorakis5ea33222016-12-07 14:33:53 -0800106 Ptr<Packet> packet = Create<Packet>();
107 packet->AddHeader(header);
108 boost::test_tools::output_test_stream output;
109 packet->Print(output);
Alexander Afanasyev59090db2020-02-21 16:37:43 -0500110 BOOST_CHECK(output.is_equal("ns3::ndn::Packet (NDNLP(Interest: /prefix?CanBePrefix&Nonce=10))"));
Spyridon Mastorakis5ea33222016-12-07 14:33:53 -0800111 }
112
113 lpPacket.add<::ndn::lp::NackField>(::ndn::lp::NackHeader().setReason(::ndn::lp::NackReason::NO_ROUTE));
114
115 {
Alexander Afanasyev59090db2020-02-21 16:37:43 -0500116 BlockHeader header(lpPacket.wireEncode());
Spyridon Mastorakis5ea33222016-12-07 14:33:53 -0800117 Ptr<Packet> packet = Create<Packet>();
118 packet->AddHeader(header);
119 boost::test_tools::output_test_stream output;
120 packet->Print(output);
Alexander Afanasyev59090db2020-02-21 16:37:43 -0500121 BOOST_CHECK(output.is_equal("ns3::ndn::Packet (NDNLP(NACK(NoRoute) for Interest: /prefix?CanBePrefix&Nonce=10))"));
Spyridon Mastorakis5ea33222016-12-07 14:33:53 -0800122 }
123
124 lpPacket.remove<::ndn::lp::NackField>();
125 lpPacket.add<::ndn::lp::FragIndexField>(0);
126 lpPacket.add<::ndn::lp::FragCountField>(1);
127
128 {
Alexander Afanasyev59090db2020-02-21 16:37:43 -0500129 BlockHeader header(lpPacket.wireEncode());
Spyridon Mastorakis5ea33222016-12-07 14:33:53 -0800130 Ptr<Packet> packet = Create<Packet>();
131 packet->AddHeader(header);
132 boost::test_tools::output_test_stream output;
133 packet->Print(output);
Alexander Afanasyev59090db2020-02-21 16:37:43 -0500134 BOOST_CHECK(output.is_equal("ns3::ndn::Packet (NDNLP(Interest: /prefix?CanBePrefix&Nonce=10))"));
Spyridon Mastorakis5ea33222016-12-07 14:33:53 -0800135 }
136
137 lpPacket.set<::ndn::lp::FragCountField>(2);
138
139 {
Alexander Afanasyev59090db2020-02-21 16:37:43 -0500140 BlockHeader header(lpPacket.wireEncode());
Spyridon Mastorakis5ea33222016-12-07 14:33:53 -0800141 Ptr<Packet> packet = Create<Packet>();
142 packet->AddHeader(header);
143 boost::test_tools::output_test_stream output;
144 packet->Print(output);
145 BOOST_CHECK(output.is_equal("ns3::ndn::Packet (NDNLP(fragment 1 out of 2))"));
146 }
147
Alexander Afanasyev0ae7eae2020-05-30 21:16:36 -0400148 Block unknown = "8808 0000 0000 0000 0000"_block;
149 lpPacket.set<::ndn::lp::FragmentField>(std::make_pair(unknown.begin(), unknown.end()));
Spyridon Mastorakis5ea33222016-12-07 14:33:53 -0800150 lpPacket.remove<::ndn::lp::FragCountField>();
151 lpPacket.remove<::ndn::lp::FragIndexField>();
152
153 {
Alexander Afanasyev59090db2020-02-21 16:37:43 -0500154 BlockHeader header(lpPacket.wireEncode());
Spyridon Mastorakis5ea33222016-12-07 14:33:53 -0800155 Ptr<Packet> packet = Create<Packet>();
156 packet->AddHeader(header);
157 boost::test_tools::output_test_stream output;
158 packet->Print(output);
159 BOOST_CHECK(output.is_equal("ns3::ndn::Packet (NDNLP(Unrecognized))"));
160 }
161}
162
163BOOST_AUTO_TEST_SUITE_END()
164
165} // namespace ndn
166} // namespace ns3