Spyridon Mastorakis | 5ea3322 | 2016-12-07 14:33:53 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 0ae7eae | 2020-05-30 21:16:36 -0400 | [diff] [blame] | 3 | * Copyright (c) 2011-2019 Regents of the University of California. |
Spyridon Mastorakis | 5ea3322 | 2016-12-07 14:33:53 -0800 | [diff] [blame] | 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> |
Alexander Afanasyev | 0ae7eae | 2020-05-30 21:16:36 -0400 | [diff] [blame] | 24 | #include <ndn-cxx/encoding/block.hpp> |
Spyridon Mastorakis | 5ea3322 | 2016-12-07 14:33:53 -0800 | [diff] [blame] | 25 | |
| 26 | #include "ns3/ndnSIM/NFD/daemon/face/transport.hpp" |
| 27 | #include "ns3/packet.h" |
| 28 | |
| 29 | #include "../tests-common.hpp" |
| 30 | |
| 31 | namespace ns3 { |
| 32 | namespace ndn { |
| 33 | |
Alexander Afanasyev | 0ae7eae | 2020-05-30 21:16:36 -0400 | [diff] [blame] | 34 | using ::ndn::operator "" _block; |
| 35 | |
Spyridon Mastorakis | 5ea3322 | 2016-12-07 14:33:53 -0800 | [diff] [blame] | 36 | BOOST_FIXTURE_TEST_SUITE(ModelNdnBlockHeader, CleanupFixture) |
| 37 | |
| 38 | class EnablePacketPrintingFixture |
| 39 | { |
| 40 | public: |
| 41 | EnablePacketPrintingFixture() |
| 42 | { |
| 43 | Packet::EnablePrinting(); |
| 44 | } |
| 45 | }; |
| 46 | |
| 47 | BOOST_GLOBAL_FIXTURE(EnablePacketPrintingFixture) |
| 48 | #if BOOST_VERSION >= 105900 |
| 49 | ; |
| 50 | #endif // BOOST_VERSION >= 105900 |
| 51 | |
| 52 | |
| 53 | BOOST_AUTO_TEST_CASE(EncodePrintInterest) |
| 54 | { |
| 55 | Interest interest("/prefix"); |
| 56 | interest.setNonce(10); |
Alexander Afanasyev | dc3c3a3 | 2019-02-17 20:17:32 -0500 | [diff] [blame] | 57 | interest.setCanBePrefix(true); |
Spyridon Mastorakis | 5ea3322 | 2016-12-07 14:33:53 -0800 | [diff] [blame] | 58 | lp::Packet lpPacket(interest.wireEncode()); |
Alexander Afanasyev | 59090db | 2020-02-21 16:37:43 -0500 | [diff] [blame] | 59 | auto packet(lpPacket.wireEncode()); |
Spyridon Mastorakis | 5ea3322 | 2016-12-07 14:33:53 -0800 | [diff] [blame] | 60 | BlockHeader header(packet); |
| 61 | |
Alexander Afanasyev | 59090db | 2020-02-21 16:37:43 -0500 | [diff] [blame] | 62 | BOOST_CHECK_EQUAL(header.GetSerializedSize(), 20); // 20 |
Spyridon Mastorakis | 5ea3322 | 2016-12-07 14:33:53 -0800 | [diff] [blame] | 63 | |
| 64 | { |
| 65 | Ptr<Packet> packet = Create<Packet>(); |
| 66 | packet->AddHeader(header); |
| 67 | boost::test_tools::output_test_stream output; |
| 68 | packet->Print(output); |
Alexander Afanasyev | 59090db | 2020-02-21 16:37:43 -0500 | [diff] [blame] | 69 | BOOST_CHECK(output.is_equal("ns3::ndn::Packet (Interest: /prefix?CanBePrefix&Nonce=10)")); |
Spyridon Mastorakis | 5ea3322 | 2016-12-07 14:33:53 -0800 | [diff] [blame] | 70 | } |
| 71 | } |
| 72 | |
| 73 | BOOST_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 Afanasyev | 59090db | 2020-02-21 16:37:43 -0500 | [diff] [blame] | 80 | auto packet(lpPacket.wireEncode()); |
Spyridon Mastorakis | 5ea3322 | 2016-12-07 14:33:53 -0800 | [diff] [blame] | 81 | BlockHeader header(packet); |
| 82 | |
Spyridon Mastorakis | f6d3285 | 2017-09-27 20:28:52 -0700 | [diff] [blame] | 83 | BOOST_CHECK_EQUAL(header.GetSerializedSize(), 1350); |
Spyridon Mastorakis | 5ea3322 | 2016-12-07 14:33:53 -0800 | [diff] [blame] | 84 | |
| 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 | |
| 94 | BOOST_AUTO_TEST_CASE(PrintLpPacket) |
| 95 | { |
| 96 | Interest interest("/prefix"); |
| 97 | interest.setNonce(10); |
Alexander Afanasyev | dc3c3a3 | 2019-02-17 20:17:32 -0500 | [diff] [blame] | 98 | interest.setCanBePrefix(true); |
Spyridon Mastorakis | 5ea3322 | 2016-12-07 14:33:53 -0800 | [diff] [blame] | 99 | |
| 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 Afanasyev | 59090db | 2020-02-21 16:37:43 -0500 | [diff] [blame] | 105 | BlockHeader header(lpPacket.wireEncode()); |
Spyridon Mastorakis | 5ea3322 | 2016-12-07 14:33:53 -0800 | [diff] [blame] | 106 | Ptr<Packet> packet = Create<Packet>(); |
| 107 | packet->AddHeader(header); |
| 108 | boost::test_tools::output_test_stream output; |
| 109 | packet->Print(output); |
Alexander Afanasyev | 59090db | 2020-02-21 16:37:43 -0500 | [diff] [blame] | 110 | BOOST_CHECK(output.is_equal("ns3::ndn::Packet (NDNLP(Interest: /prefix?CanBePrefix&Nonce=10))")); |
Spyridon Mastorakis | 5ea3322 | 2016-12-07 14:33:53 -0800 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | lpPacket.add<::ndn::lp::NackField>(::ndn::lp::NackHeader().setReason(::ndn::lp::NackReason::NO_ROUTE)); |
| 114 | |
| 115 | { |
Alexander Afanasyev | 59090db | 2020-02-21 16:37:43 -0500 | [diff] [blame] | 116 | BlockHeader header(lpPacket.wireEncode()); |
Spyridon Mastorakis | 5ea3322 | 2016-12-07 14:33:53 -0800 | [diff] [blame] | 117 | Ptr<Packet> packet = Create<Packet>(); |
| 118 | packet->AddHeader(header); |
| 119 | boost::test_tools::output_test_stream output; |
| 120 | packet->Print(output); |
Alexander Afanasyev | 59090db | 2020-02-21 16:37:43 -0500 | [diff] [blame] | 121 | BOOST_CHECK(output.is_equal("ns3::ndn::Packet (NDNLP(NACK(NoRoute) for Interest: /prefix?CanBePrefix&Nonce=10))")); |
Spyridon Mastorakis | 5ea3322 | 2016-12-07 14:33:53 -0800 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | lpPacket.remove<::ndn::lp::NackField>(); |
| 125 | lpPacket.add<::ndn::lp::FragIndexField>(0); |
| 126 | lpPacket.add<::ndn::lp::FragCountField>(1); |
| 127 | |
| 128 | { |
Alexander Afanasyev | 59090db | 2020-02-21 16:37:43 -0500 | [diff] [blame] | 129 | BlockHeader header(lpPacket.wireEncode()); |
Spyridon Mastorakis | 5ea3322 | 2016-12-07 14:33:53 -0800 | [diff] [blame] | 130 | Ptr<Packet> packet = Create<Packet>(); |
| 131 | packet->AddHeader(header); |
| 132 | boost::test_tools::output_test_stream output; |
| 133 | packet->Print(output); |
Alexander Afanasyev | 59090db | 2020-02-21 16:37:43 -0500 | [diff] [blame] | 134 | BOOST_CHECK(output.is_equal("ns3::ndn::Packet (NDNLP(Interest: /prefix?CanBePrefix&Nonce=10))")); |
Spyridon Mastorakis | 5ea3322 | 2016-12-07 14:33:53 -0800 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | lpPacket.set<::ndn::lp::FragCountField>(2); |
| 138 | |
| 139 | { |
Alexander Afanasyev | 59090db | 2020-02-21 16:37:43 -0500 | [diff] [blame] | 140 | BlockHeader header(lpPacket.wireEncode()); |
Spyridon Mastorakis | 5ea3322 | 2016-12-07 14:33:53 -0800 | [diff] [blame] | 141 | 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 Afanasyev | 0ae7eae | 2020-05-30 21:16:36 -0400 | [diff] [blame] | 148 | Block unknown = "8808 0000 0000 0000 0000"_block; |
| 149 | lpPacket.set<::ndn::lp::FragmentField>(std::make_pair(unknown.begin(), unknown.end())); |
Spyridon Mastorakis | 5ea3322 | 2016-12-07 14:33:53 -0800 | [diff] [blame] | 150 | lpPacket.remove<::ndn::lp::FragCountField>(); |
| 151 | lpPacket.remove<::ndn::lp::FragIndexField>(); |
| 152 | |
| 153 | { |
Alexander Afanasyev | 59090db | 2020-02-21 16:37:43 -0500 | [diff] [blame] | 154 | BlockHeader header(lpPacket.wireEncode()); |
Spyridon Mastorakis | 5ea3322 | 2016-12-07 14:33:53 -0800 | [diff] [blame] | 155 | 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 | |
| 163 | BOOST_AUTO_TEST_SUITE_END() |
| 164 | |
| 165 | } // namespace ndn |
| 166 | } // namespace ns3 |