Ilya Moiseenko | a828e9f | 2011-08-31 11:04:00 -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 | * |
| 18 | * Author: Ilya Moiseenko <iliamo@cs.ucla.edu> |
| 19 | */ |
| 20 | |
Ilya Moiseenko | 3e15eff | 2011-08-30 13:33:09 -0700 | [diff] [blame] | 21 | #include "ns3/test.h" |
| 22 | #include "ns3/annotated-topology-reader.h" |
| 23 | #include "ns3/ccnx-interest-header.h" |
| 24 | #include "ns3/uinteger.h" |
| 25 | #include "ns3/random-variable.h" |
| 26 | #include <limits> |
| 27 | #include "ns3/ccnx-header-helper.h" |
| 28 | #include "ns3/header.h" |
| 29 | #include "ns3/ccnx-name-components.h" |
| 30 | #include "ns3/nstime.h" |
| 31 | #include "ns3/buffer.h" |
| 32 | #include "ns3/log.h" |
| 33 | |
Ilya Moiseenko | a828e9f | 2011-08-31 11:04:00 -0700 | [diff] [blame] | 34 | #include <iostream> |
| 35 | #include <fstream> |
| 36 | #include <sstream> |
| 37 | |
Ilya Moiseenko | 3e15eff | 2011-08-30 13:33:09 -0700 | [diff] [blame] | 38 | using namespace ns3; |
Ilya Moiseenko | a828e9f | 2011-08-31 11:04:00 -0700 | [diff] [blame] | 39 | using namespace std; |
Ilya Moiseenko | 3e15eff | 2011-08-30 13:33:09 -0700 | [diff] [blame] | 40 | |
| 41 | NS_LOG_COMPONENT_DEFINE ("InterestHeaderSerializationTest"); |
| 42 | |
| 43 | class InterestHeaderSerializationTest : public TestCase |
| 44 | { |
| 45 | public: |
Ilya Moiseenko | 3e15eff | 2011-08-30 13:33:09 -0700 | [diff] [blame] | 46 | InterestHeaderSerializationTest (); |
| 47 | virtual ~InterestHeaderSerializationTest (); |
| 48 | |
Ilya Moiseenko | 3e15eff | 2011-08-30 13:33:09 -0700 | [diff] [blame] | 49 | private: |
| 50 | virtual void DoRun (void); |
| 51 | }; |
| 52 | |
| 53 | InterestHeaderSerializationTest::InterestHeaderSerializationTest () |
| 54 | : TestCase ("Interest Header Serialization Test") |
| 55 | { |
| 56 | } |
| 57 | |
| 58 | InterestHeaderSerializationTest::~InterestHeaderSerializationTest () |
| 59 | { |
| 60 | } |
| 61 | |
| 62 | void |
| 63 | InterestHeaderSerializationTest::DoRun(void) |
| 64 | { |
Ilya Moiseenko | 0872371 | 2011-09-01 17:42:12 -0700 | [diff] [blame] | 65 | Packet packet (0); |
Ilya Moiseenko | a828e9f | 2011-08-31 11:04:00 -0700 | [diff] [blame] | 66 | |
Ilya Moiseenko | 3e15eff | 2011-08-30 13:33:09 -0700 | [diff] [blame] | 67 | uint32_t randomNonce = UniformVariable().GetInteger(1, std::numeric_limits<uint32_t>::max ()); |
| 68 | Ptr<CcnxNameComponents> testname = Create<CcnxNameComponents> (); |
| 69 | (*testname) ("test") ("test2"); |
| 70 | |
| 71 | Ptr<CcnxNameComponents> exclude = Create<CcnxNameComponents> (); |
Ilya Moiseenko | a828e9f | 2011-08-31 11:04:00 -0700 | [diff] [blame] | 72 | (*exclude) ("exclude") ("exclude2"); |
Ilya Moiseenko | 3e15eff | 2011-08-30 13:33:09 -0700 | [diff] [blame] | 73 | |
| 74 | Time lifetime = Seconds(4.0); |
| 75 | bool child = true; |
| 76 | |
| 77 | uint32_t maxSuffixComponents = 40; |
| 78 | uint32_t minSuffixComponents = 20; |
| 79 | |
| 80 | CcnxInterestHeader interestHeader; |
| 81 | interestHeader.SetNonce(randomNonce); |
| 82 | interestHeader.SetName(testname); |
| 83 | interestHeader.SetInterestLifetime(lifetime); |
| 84 | interestHeader.SetChildSelector(child); |
| 85 | interestHeader.SetExclude(exclude); |
| 86 | interestHeader.SetMaxSuffixComponents(maxSuffixComponents); |
| 87 | interestHeader.SetMinSuffixComponents(minSuffixComponents); |
| 88 | |
Ilya Moiseenko | 0872371 | 2011-09-01 17:42:12 -0700 | [diff] [blame] | 89 | //serialization |
| 90 | packet.AddHeader (interestHeader); |
| 91 | |
| 92 | //deserialization |
Ilya Moiseenko | 3e15eff | 2011-08-30 13:33:09 -0700 | [diff] [blame] | 93 | CcnxInterestHeader target; |
Ilya Moiseenko | 0872371 | 2011-09-01 17:42:12 -0700 | [diff] [blame] | 94 | packet.RemoveHeader (target); |
| 95 | |
Ilya Moiseenko | 3e15eff | 2011-08-30 13:33:09 -0700 | [diff] [blame] | 96 | |
Ilya Moiseenko | 3e15eff | 2011-08-30 13:33:09 -0700 | [diff] [blame] | 97 | NS_TEST_ASSERT_MSG_EQ (target.GetNonce(), randomNonce, "Interest Header nonce deserialization failed"); |
| 98 | |
Ilya Moiseenko | a828e9f | 2011-08-31 11:04:00 -0700 | [diff] [blame] | 99 | NS_TEST_ASSERT_MSG_EQ (target.GetName(), *testname, "Interest Header name deserialization failed"); |
Ilya Moiseenko | 3e15eff | 2011-08-30 13:33:09 -0700 | [diff] [blame] | 100 | |
| 101 | NS_TEST_ASSERT_MSG_EQ (target.GetInterestLifetime(), lifetime, "Interest Header lifetime deserialization failed"); |
| 102 | |
Ilya Moiseenko | a828e9f | 2011-08-31 11:04:00 -0700 | [diff] [blame] | 103 | NS_TEST_ASSERT_MSG_EQ (target.IsEnabledChildSelector(), child, "Interest Header childselector deserialization failed"); |
Ilya Moiseenko | 3e15eff | 2011-08-30 13:33:09 -0700 | [diff] [blame] | 104 | |
Ilya Moiseenko | a828e9f | 2011-08-31 11:04:00 -0700 | [diff] [blame] | 105 | NS_TEST_ASSERT_MSG_EQ (target.GetExclude(), *exclude, "Interest Header exclude deserialization failed"); |
Ilya Moiseenko | 3e15eff | 2011-08-30 13:33:09 -0700 | [diff] [blame] | 106 | |
| 107 | NS_TEST_ASSERT_MSG_EQ (target.GetMaxSuffixComponents(), (int)maxSuffixComponents, "Interest Header maxSuffixComponents deserialization failed"); |
| 108 | |
| 109 | NS_TEST_ASSERT_MSG_EQ (target.GetMinSuffixComponents(), (int)minSuffixComponents, "Interest Header minSuffixComponents deserialization failed"); |
| 110 | } |
| 111 | |
| 112 | class InterestHeaderSerializationTestSuite : public TestSuite |
| 113 | { |
| 114 | public: |
| 115 | InterestHeaderSerializationTestSuite (); |
| 116 | }; |
| 117 | |
| 118 | InterestHeaderSerializationTestSuite::InterestHeaderSerializationTestSuite () |
| 119 | : TestSuite ("interest-header-serialization-test-suite", UNIT) |
| 120 | { |
Ilya Moiseenko | a828e9f | 2011-08-31 11:04:00 -0700 | [diff] [blame] | 121 | SetDataDir (NS_TEST_SOURCEDIR); |
Ilya Moiseenko | 3e15eff | 2011-08-30 13:33:09 -0700 | [diff] [blame] | 122 | AddTestCase (new InterestHeaderSerializationTest); |
| 123 | } |
| 124 | |
| 125 | static InterestHeaderSerializationTestSuite suite; |