blob: 86e94309213d8b4115da255083ae8588d5454947 [file] [log] [blame]
Alexander Afanasyevf1e013f2012-07-11 17:59:40 -07001/* -*- 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 Afanasyevcf6dc922012-08-10 16:55:27 -070018 * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
19 * Ilya Moiseenko <iliamo@cs.ucla.edu>
Alexander Afanasyevf1e013f2012-07-11 17:59:40 -070020 */
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>
27
28using namespace std;
29
Alexander Afanasyevcf6dc922012-08-10 16:55:27 -070030namespace ns3 {
31
32using namespace ndn;
Alexander Afanasyevf1e013f2012-07-11 17:59:40 -070033
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070034NS_LOG_COMPONENT_DEFINE ("ndn.Serialization");
Alexander Afanasyevf1e013f2012-07-11 17:59:40 -070035
36void
37InterestSerializationTest::DoRun ()
38{
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -070039 Interest source;
Alexander Afanasyevbd9c18e2012-11-19 15:23:41 -080040
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -070041 source.SetName (Create<Name> (boost::lexical_cast<Name> ("/test/test2")));
42 NS_TEST_ASSERT_MSG_EQ (source.GetName (), boost::lexical_cast<Name> ("/test/test2"), "set/get name failed");
Alexander Afanasyevf1e013f2012-07-11 17:59:40 -070043
Alexander Afanasyevf1e013f2012-07-11 17:59:40 -070044 source.SetScope (2);
45 NS_TEST_ASSERT_MSG_EQ (source.GetScope (), 2, "set/get scope failed");
46
47 source.SetInterestLifetime (Seconds (100));
48 NS_TEST_ASSERT_MSG_EQ (source.GetInterestLifetime (), Seconds (100), "set/get interest lifetime failed");
49
50 source.SetNonce (200);
51 NS_TEST_ASSERT_MSG_EQ (source.GetNonce (), 200, "set/get nonce failed");
52
53 source.SetNack (10);
54 NS_TEST_ASSERT_MSG_EQ (source.GetNack (), 10, "set/get NACK failed");
55
56 Packet packet (0);
57 //serialization
58 packet.AddHeader (source);
59
60 //deserialization
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -070061 Interest target;
Alexander Afanasyevf1e013f2012-07-11 17:59:40 -070062 packet.RemoveHeader (target);
63
Alexander Afanasyevbd9c18e2012-11-19 15:23:41 -080064 NS_TEST_ASSERT_MSG_EQ (source.GetName () , target.GetName () , "source/target name failed");
65 NS_TEST_ASSERT_MSG_EQ (source.GetScope () , target.GetScope () , "source/target scope failed");
66 NS_TEST_ASSERT_MSG_EQ (source.GetInterestLifetime (), target.GetInterestLifetime (), "source/target interest lifetime failed");
67 NS_TEST_ASSERT_MSG_EQ (source.GetNonce () , target.GetNonce () , "source/target nonce failed");
68 NS_TEST_ASSERT_MSG_EQ (source.GetNack () , target.GetNack () , "source/target NACK failed");
Alexander Afanasyevf1e013f2012-07-11 17:59:40 -070069}
70
71void
72ContentObjectSerializationTest::DoRun ()
73{
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -070074 ContentObject source;
Alexander Afanasyev11fdadd2012-11-19 15:43:17 -080075
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -070076 source.SetName (Create<Name> (boost::lexical_cast<Name> ("/test/test2/1")));
77 NS_TEST_ASSERT_MSG_EQ (source.GetName (), boost::lexical_cast<Name> ("/test/test2/1"), "set/get name failed");
Alexander Afanasyev11fdadd2012-11-19 15:43:17 -080078
79 source.SetFreshness (Seconds (10));
80 NS_TEST_ASSERT_MSG_EQ (source.GetFreshness (), Seconds (10), "set/get freshness failed");
81
82 source.SetTimestamp (Seconds (100));
83 NS_TEST_ASSERT_MSG_EQ (source.GetTimestamp (), Seconds (100), "set/get timestamp failed");
84
Alexander Afanasyevd6e5c5f2013-03-30 19:09:56 -070085 NS_TEST_ASSERT_MSG_EQ (source.GetSignature (), 0, "initialization of signature failed");
86 int size = source.GetSerializedSize ();
87 source.SetSignature (10);
88 NS_TEST_ASSERT_MSG_EQ (source.GetSignature (), 10, "set/get signature failed");
89
Alexander Afanasyev7e6749a2013-04-10 12:19:39 -070090 NS_TEST_ASSERT_MSG_EQ (source.GetSerializedSize (), static_cast<unsigned int> (size + 4), "Signature size should have increased by 4");
Alexander Afanasyevd6e5c5f2013-03-30 19:09:56 -070091
Alexander Afanasyev11fdadd2012-11-19 15:43:17 -080092 Packet packet (0);
93 //serialization
94 packet.AddHeader (source);
95
96 //deserialization
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -070097 ContentObject target;
Alexander Afanasyev11fdadd2012-11-19 15:43:17 -080098 packet.RemoveHeader (target);
99
100 NS_TEST_ASSERT_MSG_EQ (source.GetName () , target.GetName () , "source/target name failed");
101 NS_TEST_ASSERT_MSG_EQ (source.GetFreshness (), target.GetFreshness (), "source/target freshness failed");
102 NS_TEST_ASSERT_MSG_EQ (source.GetTimestamp (), target.GetTimestamp (), "source/target timestamp failed");
Alexander Afanasyevd6e5c5f2013-03-30 19:09:56 -0700103 NS_TEST_ASSERT_MSG_EQ (source.GetSignature (), target.GetSignature (), "source/target signature failed");
Alexander Afanasyevf1e013f2012-07-11 17:59:40 -0700104}
105
106}