A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 
48 using namespace ns3;
49 
50 static void
52 {
53  Ptr<Ipv6L3Protocol> ipv6 = CreateObject<Ipv6L3Protocol> ();
54  Ptr<Icmpv6L4Protocol> icmpv6 = CreateObject<Icmpv6L4Protocol> ();
55  node->AggregateObject (ipv6);
56  node->AggregateObject (icmpv6);
57  ipv6->Insert (icmpv6);
58  icmpv6->SetAttribute ("DAD", BooleanValue (false));
59 
60  //Routing for Ipv6
61  Ptr<Ipv6ListRouting> ipv6Routing = CreateObject<Ipv6ListRouting> ();
62  ipv6->SetRoutingProtocol (ipv6Routing);
63  Ptr<Ipv6StaticRouting> ipv6staticRouting = CreateObject<Ipv6StaticRouting> ();
64  ipv6Routing->AddRoutingProtocol (ipv6staticRouting, 0);
65 
66  /* register IPv6 extensions and options */
67  ipv6->RegisterExtensions ();
68  ipv6->RegisterOptions ();
69 }
70 
72 {
73 public:
75 private:
76  virtual void DoRun (void);
77  void RxCb (Ptr<Socket> socket);
78  void DoSendData (Ptr<Socket> socket, std::string to);
79 };
80 
82  : TestCase ("Ipv6PacketInfoTagTest")
83 {
84 }
85 
86 void
88 {
89  uint32_t availableData;
90  Ptr<Packet> m_receivedPacket;
91 
92  availableData = socket->GetRxAvailable ();
93  m_receivedPacket = socket->Recv (std::numeric_limits<uint32_t>::max (), 0);
94  NS_TEST_ASSERT_MSG_EQ (availableData, m_receivedPacket->GetSize (), "Did not read expected data");
95 
97  bool found;
98  found = m_receivedPacket->RemovePacketTag (tag);
99  NS_TEST_ASSERT_MSG_EQ (found, true, "Could not find tag");
100 }
101 
102 void
104 {
105  Address realTo = Inet6SocketAddress (Ipv6Address (to.c_str ()), 200);
106  if (DynamicCast<UdpSocket> (socket) != 0)
107  {
108  NS_TEST_EXPECT_MSG_EQ (socket->SendTo (Create<Packet> (123), 0, realTo),
109  123, "100");
110  }
111  // Should only Ipv6RawSock
112  else
113  {
114  socket->SendTo (Create<Packet> (123), 0, realTo);
115  }
116 }
117 
118 void
120 {
121  Ptr<Node> node0 = CreateObject<Node> ();
122  Ptr<Node> node1 = CreateObject<Node> ();
123 
124  Ptr<SimpleNetDevice> device = CreateObject<SimpleNetDevice> ();
125  Ptr<SimpleNetDevice> device2 = CreateObject<SimpleNetDevice> ();
126 
127  // For Node 0
128  node0->AddDevice (device);
129  AddInternetStack (node0);
130  Ptr<Ipv6> ipv6 = node0->GetObject<Ipv6> ();
131 
132  uint32_t index = ipv6->AddInterface (device);
133  Ipv6InterfaceAddress ifaceAddr1 = Ipv6InterfaceAddress (Ipv6Address ("2000:1000:0:2000::1"),
134  Ipv6Prefix (64));
135  ipv6->AddAddress (index, ifaceAddr1);
136  ipv6->SetMetric (index, 1);
137  ipv6->SetUp (index);
138 
139  // For Node 1
140  node1->AddDevice (device2);
141  AddInternetStack (node1);
142  ipv6 = node1->GetObject<Ipv6> ();
143 
144  index = ipv6->AddInterface (device2);
145  Ipv6InterfaceAddress ifaceAddr2 = Ipv6InterfaceAddress (Ipv6Address ("2000:1000:0:2000::2"),
146  Ipv6Prefix (64));
147  ipv6->AddAddress (index, ifaceAddr2);
148  ipv6->SetMetric (index, 1);
149  ipv6->SetUp (index);
150 
151  // ipv6 w rawsocket
152  Ptr<SocketFactory> factory = node0->GetObject<SocketFactory> (Ipv6RawSocketFactory::GetTypeId ());
153  Ptr<Socket> socket = factory->CreateSocket ();
154  Inet6SocketAddress local = Inet6SocketAddress (Ipv6Address::GetAny (), 0);
155  socket->SetAttribute ("Protocol", UintegerValue (Ipv6Header::IPV6_ICMPV6));
156  socket->Bind (local);
157  socket->SetRecvPktInfo (true);
158  socket->SetRecvCallback (MakeCallback (&Ipv6PacketInfoTagTest::RxCb, this));
159 
160  // receive on loopback
161  Simulator::ScheduleWithContext (socket->GetNode ()->GetId (), Seconds (0),
162  &Ipv6PacketInfoTagTest::DoSendData, this, socket, "::1");
163  Simulator::Run ();
164 
165  Ptr<SocketFactory> factory2 = node1->GetObject<SocketFactory> (Ipv6RawSocketFactory::GetTypeId ());
166  Ptr<Socket> socket2 = factory2->CreateSocket ();
167  std::stringstream dst;
168  dst << ifaceAddr1.GetAddress ();
169  Simulator::ScheduleWithContext (socket2->GetNode ()->GetId (), Seconds (0),
170  &Ipv6PacketInfoTagTest::DoSendData, this, socket,
171  dst.str ());
172  Simulator::Run ();
173 
174 #ifdef UDP6_SUPPORTED
175  // IPv6 test
176  factory = node0->GetObject<SocketFactory> (UdpSocketFactory::GetTypeId ());
177  socket = factory->CreateSocket ();
178  local = Inet6SocketAddress (Ipv6Address::GetAny (), 200);
179  socket->Bind (local);
180  socket->SetRecvPktInfo (true);
181  socket->SetRecvCallback (MakeCallback (&Ipv6PacketInfoTagTest::RxCb, this));
182 
183  // receive on loopback
184  Simulator::ScheduleWithContext (socket->GetNode ()->GetId (), Seconds (0),
185  &Ipv6PacketInfoTagTest::DoSendData, this, socket, "::1");
186  Simulator::Run ();
187 
188  factory2 = node1->GetObject<SocketFactory> (UdpSocketFactory::GetTypeId ());
189  socket2 = factory2->CreateSocket ();
190  Simulator::ScheduleWithContext (socket2->GetNode ()->GetId (), Seconds (0),
191  &Ipv6PacketInfoTagTest::DoSendData, this, socket, "10.1.1.1");
192  Simulator::Run ();
193 
194 #endif // UDP6_SUPPORTED
195 
196  Simulator::Destroy ();
197  // IPv6 test
198 }
199 
201 {
202 public:
204 private:
206 
208  : TestSuite ("ipv6-packet-info-tag", UNIT)
209 {
210  AddTestCase (new Ipv6PacketInfoTagTest (), TestCase::QUICK);
211 }
Hold a bool native type.
Definition: boolean.h:38
virtual void SetMetric(uint32_t interface, uint16_t metric)=0
Set metric on specified Ipv6 interface.
Access to the IPv6 forwarding table, interfaces, and configuration.
Definition: ipv6.h:79
A suite of tests to run.
Definition: test.h:1025
IPv6 address associated with an interface.
uint32_t GetSize(void) const
Definition: packet.h:650
virtual Ptr< Socket > CreateSocket(void)=0
encapsulates test code
Definition: test.h:849
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:86
void RxCb(Ptr< Socket > socket)
Ipv6Address GetAddress() const
Get the IPv6 address.
Hold an unsigned integer type.
Definition: uinteger.h:46
static void AddInternetStack(Ptr< Node > node)
#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:244
An Inet6 address class.
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1238
void AggregateObject(Ptr< Object > other)
Definition: object.cc:243
virtual Ptr< Packet > Recv(uint32_t maxSize, uint32_t flags)=0
Read data from the socket.
void AddTestCase(TestCase *testCase) NS_DEPRECATED
Add an individual child TestCase case to this TestCase.
Definition: test.cc:173
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:46
void SetRoutingProtocol(Ptr< Ipv6RoutingProtocol > routingProtocol)
Set routing protocol for this stack.
uint32_t AddDevice(Ptr< NetDevice > device)
Definition: node.cc:118
bool RemovePacketTag(Tag &tag)
Remove a packet tag.
Definition: packet.cc:848
void Insert(Ptr< IpL4Protocol > protocol)
Add an L4 protocol.
virtual void RegisterOptions()
Register the IPv6 Options.
void DoSendData(Ptr< Socket > socket, std::string to)
Describes an IPv6 prefix.
Definition: ipv6-address.h:387
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
Ptr< T > GetObject(void) const
Definition: object.h:361
virtual void SetUp(uint32_t interface)=0
Set the interface into the "up" state.
#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:137
virtual uint32_t GetRxAvailable(void) const =0
Return number of bytes which can be returned from one or multiple calls to Recv.