A Discrete-Event Network Simulator
API
ipv6-packet-info-tag-test-suite.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2010 Hajime Tazaki
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  * Authors: Hajime Tazaki <tazaki@sfc.wide.ad.jp>
19  */
20 
21 //-----------------------------------------------------------------------------
22 // Unit tests
23 //-----------------------------------------------------------------------------
24 
25 #include "ns3/test.h"
26 #include "ns3/ipv6-address.h"
27 #include "ns3/ipv6-packet-info-tag.h"
28 #include "ns3/log.h"
29 #include "ns3/abort.h"
30 #include "ns3/attribute.h"
31 #include "ns3/simple-net-device.h"
32 #include "ns3/object-factory.h"
33 #include "ns3/socket-factory.h"
34 #include "ns3/udp-socket-factory.h"
35 #include "ns3/udp-socket.h"
36 #include "ns3/ipv6-l3-protocol.h"
37 #include "ns3/ipv6-raw-socket-factory.h"
38 #include "ns3/ipv6-interface.h"
39 #include "ns3/icmpv6-l4-protocol.h"
40 #include "ns3/ipv6-static-routing.h"
41 #include "ns3/ipv6-list-routing.h"
42 #include "ns3/inet6-socket-address.h"
43 #include "ns3/simulator.h"
44 #include "ns3/uinteger.h"
45 #include "ns3/boolean.h"
46 #include "ns3/node.h"
47 #include "ns3/traffic-control-layer.h"
48 
49 using namespace ns3;
50 
51 static void
53 {
54  Ptr<Ipv6L3Protocol> ipv6 = CreateObject<Ipv6L3Protocol> ();
55  Ptr<Icmpv6L4Protocol> icmpv6 = CreateObject<Icmpv6L4Protocol> ();
56  node->AggregateObject (ipv6);
57  node->AggregateObject (icmpv6);
58  ipv6->Insert (icmpv6);
59  icmpv6->SetAttribute ("DAD", BooleanValue (false));
60 
61  //Routing for Ipv6
62  Ptr<Ipv6ListRouting> ipv6Routing = CreateObject<Ipv6ListRouting> ();
63  ipv6->SetRoutingProtocol (ipv6Routing);
64  Ptr<Ipv6StaticRouting> ipv6staticRouting = CreateObject<Ipv6StaticRouting> ();
65  ipv6Routing->AddRoutingProtocol (ipv6staticRouting, 0);
66 
67  /* register IPv6 extensions and options */
68  ipv6->RegisterExtensions ();
69  ipv6->RegisterOptions ();
70 
71  // Traffic Control
72  Ptr<TrafficControlLayer> tc = CreateObject<TrafficControlLayer> ();
73  node->AggregateObject (tc);
74 }
75 
77 {
78 public:
80 private:
81  virtual void DoRun (void);
82  void RxCb (Ptr<Socket> socket);
83  void DoSendData (Ptr<Socket> socket, std::string to);
84 };
85 
87  : TestCase ("Ipv6PacketInfoTagTest")
88 {
89 }
90 
91 void
93 {
94  uint32_t availableData;
95  Ptr<Packet> m_receivedPacket;
96 
97  availableData = socket->GetRxAvailable ();
98  m_receivedPacket = socket->Recv (std::numeric_limits<uint32_t>::max (), 0);
99  NS_TEST_ASSERT_MSG_EQ (availableData, m_receivedPacket->GetSize (), "Did not read expected data");
100 
101  Ipv6PacketInfoTag tag;
102  bool found;
103  found = m_receivedPacket->RemovePacketTag (tag);
104  NS_TEST_ASSERT_MSG_EQ (found, true, "Could not find tag");
105 }
106 
107 void
109 {
110  Address realTo = Inet6SocketAddress (Ipv6Address (to.c_str ()), 200);
111  if (DynamicCast<UdpSocket> (socket) != 0)
112  {
113  NS_TEST_EXPECT_MSG_EQ (socket->SendTo (Create<Packet> (123), 0, realTo),
114  123, "100");
115  }
116  // Should only Ipv6RawSock
117  else
118  {
119  socket->SendTo (Create<Packet> (123), 0, realTo);
120  }
121 }
122 
123 void
125 {
126  Ptr<Node> node0 = CreateObject<Node> ();
127  Ptr<Node> node1 = CreateObject<Node> ();
128 
129  Ptr<SimpleNetDevice> device = CreateObject<SimpleNetDevice> ();
130  Ptr<SimpleNetDevice> device2 = CreateObject<SimpleNetDevice> ();
131 
132  // For Node 0
133  node0->AddDevice (device);
134  AddInternetStack (node0);
135  Ptr<Ipv6> ipv6 = node0->GetObject<Ipv6> ();
136 
137  uint32_t index = ipv6->AddInterface (device);
138  Ipv6InterfaceAddress ifaceAddr1 = Ipv6InterfaceAddress (Ipv6Address ("2000:1000:0:2000::1"),
139  Ipv6Prefix (64));
140  ipv6->AddAddress (index, ifaceAddr1);
141  ipv6->SetMetric (index, 1);
142  ipv6->SetUp (index);
143 
144  // For Node 1
145  node1->AddDevice (device2);
146  AddInternetStack (node1);
147  ipv6 = node1->GetObject<Ipv6> ();
148 
149  index = ipv6->AddInterface (device2);
150  Ipv6InterfaceAddress ifaceAddr2 = Ipv6InterfaceAddress (Ipv6Address ("2000:1000:0:2000::2"),
151  Ipv6Prefix (64));
152  ipv6->AddAddress (index, ifaceAddr2);
153  ipv6->SetMetric (index, 1);
154  ipv6->SetUp (index);
155 
156  // ipv6 w rawsocket
157  Ptr<SocketFactory> factory = node0->GetObject<SocketFactory> (Ipv6RawSocketFactory::GetTypeId ());
158  Ptr<Socket> socket = factory->CreateSocket ();
159  Inet6SocketAddress local = Inet6SocketAddress (Ipv6Address::GetAny (), 0);
160  socket->SetAttribute ("Protocol", UintegerValue (Ipv6Header::IPV6_ICMPV6));
161  socket->Bind (local);
162  socket->SetRecvPktInfo (true);
163  socket->SetRecvCallback (MakeCallback (&Ipv6PacketInfoTagTest::RxCb, this));
164 
165  // receive on loopback
166  Simulator::ScheduleWithContext (socket->GetNode ()->GetId (), Seconds (0),
167  &Ipv6PacketInfoTagTest::DoSendData, this, socket, "::1");
168  Simulator::Run ();
169 
170  Ptr<SocketFactory> factory2 = node1->GetObject<SocketFactory> (Ipv6RawSocketFactory::GetTypeId ());
171  Ptr<Socket> socket2 = factory2->CreateSocket ();
172  std::stringstream dst;
173  dst << ifaceAddr1.GetAddress ();
174  Simulator::ScheduleWithContext (socket2->GetNode ()->GetId (), Seconds (0),
175  &Ipv6PacketInfoTagTest::DoSendData, this, socket,
176  dst.str ());
177  Simulator::Run ();
178 
179 #ifdef UDP6_SUPPORTED
180  // IPv6 test
181  factory = node0->GetObject<SocketFactory> (UdpSocketFactory::GetTypeId ());
182  socket = factory->CreateSocket ();
183  local = Inet6SocketAddress (Ipv6Address::GetAny (), 200);
184  socket->Bind (local);
185  socket->SetRecvPktInfo (true);
186  socket->SetRecvCallback (MakeCallback (&Ipv6PacketInfoTagTest::RxCb, this));
187 
188  // receive on loopback
189  Simulator::ScheduleWithContext (socket->GetNode ()->GetId (), Seconds (0),
190  &Ipv6PacketInfoTagTest::DoSendData, this, socket, "::1");
191  Simulator::Run ();
192 
193  factory2 = node1->GetObject<SocketFactory> (UdpSocketFactory::GetTypeId ());
194  socket2 = factory2->CreateSocket ();
195  Simulator::ScheduleWithContext (socket2->GetNode ()->GetId (), Seconds (0),
196  &Ipv6PacketInfoTagTest::DoSendData, this, socket, "10.1.1.1");
197  Simulator::Run ();
198 
199 #endif // UDP6_SUPPORTED
200 
201  Simulator::Destroy ();
202  // IPv6 test
203 }
204 
206 {
207 public:
209 private:
211 
213  : TestSuite ("ipv6-packet-info-tag", UNIT)
214 {
215  AddTestCase (new Ipv6PacketInfoTagTest (), TestCase::QUICK);
216 }
AttributeValue implementation for Boolean.
Definition: boolean.h:34
virtual void SetMetric(uint32_t interface, uint16_t metric)=0
Set metric on specified Ipv6 interface.
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:462
Access to the IPv6 forwarding table, interfaces, and configuration.
Definition: ipv6.h:81
A suite of tests to run.
Definition: test.h:1333
void AggregateObject(Ptr< Object > other)
Aggregate two Objects together.
Definition: object.cc:252
IPv6 address associated with an interface.
#define NS_TEST_EXPECT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report if not.
Definition: test.h:278
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:792
virtual Ptr< Socket > CreateSocket(void)=0
encapsulates test code
Definition: test.h:1147
virtual void DoRun(void)
Implementation to actually run this TestCase.
Object to create transport layer instances that provide a socket API to applications.
virtual void AddRoutingProtocol(Ptr< Ipv6RoutingProtocol > routingProtocol, int16_t priority)
Register a new routing protocol to be used in this IPv4 stack.
a polymophic address class
Definition: address.h:90
void RxCb(Ptr< Socket > socket)
Ipv6Address GetAddress() const
Get the IPv6 address.
#define max(a, b)
Definition: 80211b.c:45
void AddTestCase(TestCase *testCase, enum TestDuration duration)
Add an individual child TestCase to this test suite.
Definition: test.cc:298
Hold an unsigned integer type.
Definition: uinteger.h:44
static void AddInternetStack(Ptr< Node > node)
#define NS_TEST_ASSERT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report and abort if not.
Definition: test.h:161
An Inet6 address class.
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1489
Every class exported by the ns3 library is enclosed in the ns3 namespace.
virtual Ptr< Packet > Recv(uint32_t maxSize, uint32_t flags)=0
Read data from the socket.
virtual uint32_t AddInterface(Ptr< NetDevice > device)=0
Add a NetDevice interface.
virtual void RegisterExtensions()
Register the IPv6 Extensions.
Describes an IPv6 address.
Definition: ipv6-address.h:48
void SetRoutingProtocol(Ptr< Ipv6RoutingProtocol > routingProtocol)
Set routing protocol for this stack.
uint32_t AddDevice(Ptr< NetDevice > device)
Associate a NetDevice to this node.
Definition: node.cc:128
bool RemovePacketTag(Tag &tag)
Remove a packet tag.
Definition: packet.cc:831
virtual void Insert(Ptr< IpL4Protocol > protocol)
Add a L4 protocol.
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:895
virtual void RegisterOptions()
Register the IPv6 Options.
void DoSendData(Ptr< Socket > socket, std::string to)
Describes an IPv6 prefix.
Definition: ipv6-address.h:394
virtual bool AddAddress(uint32_t interface, Ipv6InterfaceAddress address)=0
Add an address on the specified IPv6 interface.
virtual int SendTo(Ptr< Packet > p, uint32_t flags, const Address &toAddress)=0
Send data to a specified peer.
This class implements a tag that carries socket ancillary data to the socket interface.
Ipv6PacketInfoTagTestSuite g_packetinfotagTests
virtual void SetUp(uint32_t interface)=0
Set the interface into the "up" state.
virtual uint32_t GetRxAvailable(void) const =0
Return number of bytes which can be returned from one or multiple calls to Recv.