Alexander Afanasyev | f1e013f | 2012-07-11 17:59:40 -0700 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2011 University of California, Los Angeles |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 as |
| 7 | * published by the Free Software Foundation; |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | * |
Alexander Afanasyev | cf6dc92 | 2012-08-10 16:55:27 -0700 | [diff] [blame] | 18 | * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 19 | * Ilya Moiseenko <iliamo@cs.ucla.edu> |
Alexander Afanasyev | f1e013f | 2012-07-11 17:59:40 -0700 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | #include "ns3/core-module.h" |
| 23 | #include "ns3/ndnSIM-module.h" |
| 24 | #include "ndnSIM-serialization.h" |
| 25 | |
| 26 | #include <boost/lexical_cast.hpp> |
Alexander Afanasyev | ee76255 | 2013-07-10 22:29:22 -0700 | [diff] [blame] | 27 | #include "ns3/ndnSIM/model/wire/ndnsim.h" |
Alexander Afanasyev | f1e013f | 2012-07-11 17:59:40 -0700 | [diff] [blame] | 28 | |
| 29 | using namespace std; |
| 30 | |
Alexander Afanasyev | cf6dc92 | 2012-08-10 16:55:27 -0700 | [diff] [blame] | 31 | namespace ns3 { |
| 32 | |
| 33 | using namespace ndn; |
Alexander Afanasyev | f1e013f | 2012-07-11 17:59:40 -0700 | [diff] [blame] | 34 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 35 | NS_LOG_COMPONENT_DEFINE ("ndn.Serialization"); |
Alexander Afanasyev | f1e013f | 2012-07-11 17:59:40 -0700 | [diff] [blame] | 36 | |
| 37 | void |
| 38 | InterestSerializationTest::DoRun () |
| 39 | { |
Alexander Afanasyev | ee76255 | 2013-07-10 22:29:22 -0700 | [diff] [blame] | 40 | Ptr<Interest> source = Create<Interest> (); |
Alexander Afanasyev | bd9c18e | 2012-11-19 15:23:41 -0800 | [diff] [blame] | 41 | |
Alexander Afanasyev | ee76255 | 2013-07-10 22:29:22 -0700 | [diff] [blame] | 42 | source->SetName (Create<Name> (boost::lexical_cast<Name> ("/test/test2"))); |
| 43 | NS_TEST_ASSERT_MSG_EQ (source->GetName (), boost::lexical_cast<Name> ("/test/test2"), "set/get name failed"); |
Alexander Afanasyev | f1e013f | 2012-07-11 17:59:40 -0700 | [diff] [blame] | 44 | |
Alexander Afanasyev | ee76255 | 2013-07-10 22:29:22 -0700 | [diff] [blame] | 45 | source->SetScope (2); |
| 46 | NS_TEST_ASSERT_MSG_EQ (source->GetScope (), 2, "set/get scope failed"); |
Alexander Afanasyev | f1e013f | 2012-07-11 17:59:40 -0700 | [diff] [blame] | 47 | |
Alexander Afanasyev | ee76255 | 2013-07-10 22:29:22 -0700 | [diff] [blame] | 48 | source->SetInterestLifetime (Seconds (100)); |
| 49 | NS_TEST_ASSERT_MSG_EQ (source->GetInterestLifetime (), Seconds (100), "set/get interest lifetime failed"); |
Alexander Afanasyev | f1e013f | 2012-07-11 17:59:40 -0700 | [diff] [blame] | 50 | |
Alexander Afanasyev | ee76255 | 2013-07-10 22:29:22 -0700 | [diff] [blame] | 51 | source->SetNonce (200); |
| 52 | NS_TEST_ASSERT_MSG_EQ (source->GetNonce (), 200, "set/get nonce failed"); |
Alexander Afanasyev | f1e013f | 2012-07-11 17:59:40 -0700 | [diff] [blame] | 53 | |
Alexander Afanasyev | ee76255 | 2013-07-10 22:29:22 -0700 | [diff] [blame] | 54 | source->SetNack (10); |
| 55 | NS_TEST_ASSERT_MSG_EQ (source->GetNack (), 10, "set/get NACK failed"); |
Alexander Afanasyev | f1e013f | 2012-07-11 17:59:40 -0700 | [diff] [blame] | 56 | |
Alexander Afanasyev | ee76255 | 2013-07-10 22:29:22 -0700 | [diff] [blame] | 57 | NS_TEST_ASSERT_MSG_EQ (source->GetWire (), 0, "Wire should be empty"); |
| 58 | NS_TEST_ASSERT_MSG_NE (source->GetPayload (), 0, "Payload should not be empty"); |
| 59 | |
| 60 | Ptr<Packet> packet = wire::ndnSIM::Interest::ToWire (source); |
| 61 | |
| 62 | NS_TEST_ASSERT_MSG_NE (source->GetWire (), 0, "Wire should not be empty now"); |
| 63 | |
| 64 | Ptr<Interest> target = wire::ndnSIM::Interest::FromWire (packet); |
Alexander Afanasyev | f1e013f | 2012-07-11 17:59:40 -0700 | [diff] [blame] | 65 | |
Alexander Afanasyev | ee76255 | 2013-07-10 22:29:22 -0700 | [diff] [blame] | 66 | NS_TEST_ASSERT_MSG_EQ (source->GetName () , target->GetName () , "source/target name failed"); |
| 67 | NS_TEST_ASSERT_MSG_EQ (source->GetScope () , target->GetScope () , "source/target scope failed"); |
| 68 | NS_TEST_ASSERT_MSG_EQ (source->GetInterestLifetime (), target->GetInterestLifetime (), "source/target interest lifetime failed"); |
| 69 | NS_TEST_ASSERT_MSG_EQ (source->GetNonce () , target->GetNonce () , "source/target nonce failed"); |
| 70 | NS_TEST_ASSERT_MSG_EQ (source->GetNack () , target->GetNack () , "source/target NACK failed"); |
Alexander Afanasyev | abb493a | 2013-07-19 15:31:33 -0700 | [diff] [blame] | 71 | |
| 72 | NS_TEST_ASSERT_MSG_EQ (source->GetExclude () , 0, "exclude should be empty"); |
| 73 | NS_TEST_ASSERT_MSG_EQ (target->GetExclude () , 0, "exclude should be empty"); |
| 74 | |
| 75 | Ptr<Exclude> exclude = Create<Exclude> (); |
| 76 | exclude->excludeAfter (name::Component ()); |
| 77 | source->SetExclude (exclude); |
| 78 | |
| 79 | NS_TEST_ASSERT_MSG_EQ (boost::lexical_cast<std::string> (*source->GetExclude ()), |
| 80 | " ----> ", "exclude should contain only <ANY/>"); |
| 81 | |
| 82 | exclude->appendExclude (name::Component ("alex"), false); |
| 83 | exclude->excludeAfter (name::Component ("zhenkai")); |
| 84 | |
| 85 | source->SetExclude (exclude); |
| 86 | NS_TEST_ASSERT_MSG_EQ (boost::lexical_cast<std::string> (*source->GetExclude ()), |
| 87 | " ----> alex zhenkai ----> ", "exclude should contain only <ANY/>"); |
| 88 | |
| 89 | NS_TEST_ASSERT_MSG_EQ (source->GetWire (), 0, "Wire should be empty"); |
| 90 | NS_TEST_ASSERT_MSG_NE (source->GetPayload (), 0, "Payload should not be empty"); |
| 91 | |
| 92 | packet = wire::ndnSIM::Interest::ToWire (source); |
| 93 | target = wire::ndnSIM::Interest::FromWire (packet); |
| 94 | NS_TEST_ASSERT_MSG_NE (target->GetExclude (), 0, "exclude should not be empty"); |
| 95 | |
| 96 | NS_TEST_ASSERT_MSG_EQ (boost::lexical_cast<std::string> (*target->GetExclude ()), |
| 97 | " ----> alex zhenkai ----> ", "exclude should contain only <ANY/>"); |
Alexander Afanasyev | f1e013f | 2012-07-11 17:59:40 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | void |
| 101 | ContentObjectSerializationTest::DoRun () |
| 102 | { |
Alexander Afanasyev | ee76255 | 2013-07-10 22:29:22 -0700 | [diff] [blame] | 103 | Ptr<ContentObject> source = Create<ContentObject> (Create<Packet> (1024)); |
Alexander Afanasyev | 11fdadd | 2012-11-19 15:43:17 -0800 | [diff] [blame] | 104 | |
Alexander Afanasyev | ee76255 | 2013-07-10 22:29:22 -0700 | [diff] [blame] | 105 | source->SetName (Create<Name> (boost::lexical_cast<Name> ("/test/test2/1"))); |
| 106 | NS_TEST_ASSERT_MSG_EQ (source->GetName (), boost::lexical_cast<Name> ("/test/test2/1"), "set/get name failed"); |
Alexander Afanasyev | 11fdadd | 2012-11-19 15:43:17 -0800 | [diff] [blame] | 107 | |
Alexander Afanasyev | ee76255 | 2013-07-10 22:29:22 -0700 | [diff] [blame] | 108 | source->SetFreshness (Seconds (10)); |
| 109 | NS_TEST_ASSERT_MSG_EQ (source->GetFreshness (), Seconds (10), "set/get freshness failed"); |
Alexander Afanasyev | 11fdadd | 2012-11-19 15:43:17 -0800 | [diff] [blame] | 110 | |
Alexander Afanasyev | ee76255 | 2013-07-10 22:29:22 -0700 | [diff] [blame] | 111 | source->SetTimestamp (Seconds (100)); |
| 112 | NS_TEST_ASSERT_MSG_EQ (source->GetTimestamp (), Seconds (100), "set/get timestamp failed"); |
Alexander Afanasyev | 11fdadd | 2012-11-19 15:43:17 -0800 | [diff] [blame] | 113 | |
Alexander Afanasyev | ee76255 | 2013-07-10 22:29:22 -0700 | [diff] [blame] | 114 | NS_TEST_ASSERT_MSG_EQ (source->GetSignature (), 0, "initialization of signature failed"); |
Alexander Afanasyev | d6e5c5f | 2013-03-30 19:09:56 -0700 | [diff] [blame] | 115 | |
Alexander Afanasyev | ee76255 | 2013-07-10 22:29:22 -0700 | [diff] [blame] | 116 | Ptr<Packet> packet = wire::ndnSIM::Data::ToWire (source); |
| 117 | int size = packet->GetSize (); |
| 118 | |
| 119 | source->SetSignature (10); |
| 120 | NS_TEST_ASSERT_MSG_EQ (source->GetSignature (), 10, "set/get signature failed"); |
| 121 | |
| 122 | packet = wire::ndnSIM::Data::ToWire (source); |
| 123 | NS_TEST_ASSERT_MSG_EQ (packet->GetSize (), static_cast<unsigned int> (size + 4), "Signature size should have increased by 4"); |
| 124 | |
| 125 | Ptr<ContentObject> target = wire::ndnSIM::Data::FromWire (packet); |
Alexander Afanasyev | d6e5c5f | 2013-03-30 19:09:56 -0700 | [diff] [blame] | 126 | |
Alexander Afanasyev | ee76255 | 2013-07-10 22:29:22 -0700 | [diff] [blame] | 127 | NS_TEST_ASSERT_MSG_EQ (source->GetName () , target->GetName () , "source/target name failed"); |
| 128 | NS_TEST_ASSERT_MSG_EQ (source->GetFreshness (), target->GetFreshness (), "source/target freshness failed"); |
| 129 | NS_TEST_ASSERT_MSG_EQ (source->GetTimestamp (), target->GetTimestamp (), "source/target timestamp failed"); |
| 130 | NS_TEST_ASSERT_MSG_EQ (source->GetSignature (), target->GetSignature (), "source/target signature failed"); |
Alexander Afanasyev | f1e013f | 2012-07-11 17:59:40 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | } |