blob: c41edd71565650d5a0da30d40ca845ca52522e76 [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 Afanasyevf007a992022-05-05 15:57:08 -040069 BOOST_CHECK(output.is_equal("ns3::ndn::Packet (Interest: /prefix?CanBePrefix&Nonce=0000000a)"));
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 Mastorakis5ea33222016-12-07 14:33:53 -080083 {
84 Ptr<Packet> packet = Create<Packet>();
85 packet->AddHeader(header);
86 boost::test_tools::output_test_stream output;
87 packet->Print(output);
88 BOOST_CHECK(output.is_equal("ns3::ndn::Packet (Data: /other/prefix)"));
89 }
Alexander Afanasyeva4d86e72022-05-05 16:12:51 -040090
91 BOOST_TEST(data.getKeyLocator()->getName() == "/dummy/KEY/-%9C%28r%B8%AA%3B%60/NA/%FD%00%00%01%5E%DF%3C%C6%7C");
92 BOOST_CHECK_EQUAL(header.GetSerializedSize(), 1365);
Spyridon Mastorakis5ea33222016-12-07 14:33:53 -080093}
94
95BOOST_AUTO_TEST_CASE(PrintLpPacket)
96{
97 Interest interest("/prefix");
98 interest.setNonce(10);
Alexander Afanasyevdc3c3a32019-02-17 20:17:32 -050099 interest.setCanBePrefix(true);
Spyridon Mastorakis5ea33222016-12-07 14:33:53 -0800100
101 lp::Packet lpPacket;
102 lpPacket.add<::ndn::lp::SequenceField>(0); // to make sure that the NDNLP header is added
103 lpPacket.add<::ndn::lp::FragmentField>(std::make_pair(interest.wireEncode().begin(), interest.wireEncode().end()));
104
105 {
Alexander Afanasyev59090db2020-02-21 16:37:43 -0500106 BlockHeader header(lpPacket.wireEncode());
Spyridon Mastorakis5ea33222016-12-07 14:33:53 -0800107 Ptr<Packet> packet = Create<Packet>();
108 packet->AddHeader(header);
109 boost::test_tools::output_test_stream output;
110 packet->Print(output);
Alexander Afanasyevf007a992022-05-05 15:57:08 -0400111 BOOST_CHECK(output.is_equal("ns3::ndn::Packet (NDNLP(Interest: /prefix?CanBePrefix&Nonce=0000000a))"));
Spyridon Mastorakis5ea33222016-12-07 14:33:53 -0800112 }
113
114 lpPacket.add<::ndn::lp::NackField>(::ndn::lp::NackHeader().setReason(::ndn::lp::NackReason::NO_ROUTE));
115
116 {
Alexander Afanasyev59090db2020-02-21 16:37:43 -0500117 BlockHeader header(lpPacket.wireEncode());
Spyridon Mastorakis5ea33222016-12-07 14:33:53 -0800118 Ptr<Packet> packet = Create<Packet>();
119 packet->AddHeader(header);
120 boost::test_tools::output_test_stream output;
121 packet->Print(output);
Alexander Afanasyevf007a992022-05-05 15:57:08 -0400122 BOOST_CHECK(output.is_equal("ns3::ndn::Packet (NDNLP(NACK(NoRoute) for Interest: /prefix?CanBePrefix&Nonce=0000000a))"));
Spyridon Mastorakis5ea33222016-12-07 14:33:53 -0800123 }
124
125 lpPacket.remove<::ndn::lp::NackField>();
126 lpPacket.add<::ndn::lp::FragIndexField>(0);
127 lpPacket.add<::ndn::lp::FragCountField>(1);
128
129 {
Alexander Afanasyev59090db2020-02-21 16:37:43 -0500130 BlockHeader header(lpPacket.wireEncode());
Spyridon Mastorakis5ea33222016-12-07 14:33:53 -0800131 Ptr<Packet> packet = Create<Packet>();
132 packet->AddHeader(header);
133 boost::test_tools::output_test_stream output;
134 packet->Print(output);
Alexander Afanasyevf007a992022-05-05 15:57:08 -0400135 BOOST_CHECK(output.is_equal("ns3::ndn::Packet (NDNLP(Interest: /prefix?CanBePrefix&Nonce=0000000a))"));
Spyridon Mastorakis5ea33222016-12-07 14:33:53 -0800136 }
137
138 lpPacket.set<::ndn::lp::FragCountField>(2);
139
140 {
Alexander Afanasyev59090db2020-02-21 16:37:43 -0500141 BlockHeader header(lpPacket.wireEncode());
Spyridon Mastorakis5ea33222016-12-07 14:33:53 -0800142 Ptr<Packet> packet = Create<Packet>();
143 packet->AddHeader(header);
144 boost::test_tools::output_test_stream output;
145 packet->Print(output);
146 BOOST_CHECK(output.is_equal("ns3::ndn::Packet (NDNLP(fragment 1 out of 2))"));
147 }
148
Alexander Afanasyev0ae7eae2020-05-30 21:16:36 -0400149 Block unknown = "8808 0000 0000 0000 0000"_block;
150 lpPacket.set<::ndn::lp::FragmentField>(std::make_pair(unknown.begin(), unknown.end()));
Spyridon Mastorakis5ea33222016-12-07 14:33:53 -0800151 lpPacket.remove<::ndn::lp::FragCountField>();
152 lpPacket.remove<::ndn::lp::FragIndexField>();
153
154 {
Alexander Afanasyev59090db2020-02-21 16:37:43 -0500155 BlockHeader header(lpPacket.wireEncode());
Spyridon Mastorakis5ea33222016-12-07 14:33:53 -0800156 Ptr<Packet> packet = Create<Packet>();
157 packet->AddHeader(header);
158 boost::test_tools::output_test_stream output;
159 packet->Print(output);
160 BOOST_CHECK(output.is_equal("ns3::ndn::Packet (NDNLP(Unrecognized))"));
161 }
162}
163
164BOOST_AUTO_TEST_SUITE_END()
165
166} // namespace ndn
167} // namespace ns3