A Discrete-Event Network Simulator
API
ipv4-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 <string>
26 
27 #include "ns3/test.h"
28 #include "ns3/ipv4-packet-info-tag.h"
29 #include "ns3/ipv4-address.h"
30 #include "ns3/log.h"
31 #include "ns3/abort.h"
32 #include "ns3/attribute.h"
33 #include "ns3/simple-net-device.h"
34 #include "ns3/object-factory.h"
35 #include "ns3/socket-factory.h"
36 #include "ns3/udp-socket-factory.h"
37 #include "ns3/udp-socket.h"
38 #include "ns3/inet-socket-address.h"
39 #include "ns3/ipv4-l3-protocol.h"
40 #include "ns3/ipv4-raw-socket-factory.h"
41 #include "ns3/ipv4-interface.h"
42 #include "ns3/arp-l3-protocol.h"
43 #include "ns3/icmpv4-l4-protocol.h"
44 #include "ns3/ipv4-static-routing.h"
45 #include "ns3/ipv4-list-routing.h"
46 #include "ns3/udp-l4-protocol.h"
47 #include "ns3/tcp-l4-protocol.h"
48 #include "ns3/traffic-control-layer.h"
49 #include "ns3/simulator.h"
50 #include "ns3/node.h"
51 
52 using namespace ns3;
53 
54 namespace {
55 
56 static void
58 {
59  //ARP
60  Ptr<ArpL3Protocol> arp = CreateObject<ArpL3Protocol> ();
61  node->AggregateObject (arp);
62  //IPV4
63  Ptr<Ipv4L3Protocol> ipv4 = CreateObject<Ipv4L3Protocol> ();
64  //Routing for Ipv4
65  Ptr<Ipv4ListRouting> ipv4Routing = CreateObject<Ipv4ListRouting> ();
66  ipv4->SetRoutingProtocol (ipv4Routing);
67  Ptr<Ipv4StaticRouting> ipv4staticRouting = CreateObject<Ipv4StaticRouting> ();
68  ipv4Routing->AddRoutingProtocol (ipv4staticRouting, 0);
69  node->AggregateObject (ipv4);
70  //ICMP
71  Ptr<Icmpv4L4Protocol> icmp = CreateObject<Icmpv4L4Protocol> ();
72  node->AggregateObject (icmp);
73  //UDP
74  Ptr<UdpL4Protocol> udp = CreateObject<UdpL4Protocol> ();
75  node->AggregateObject (udp);
76  // Traffic Control
77  Ptr<TrafficControlLayer> tc = CreateObject<TrafficControlLayer> ();
78  node->AggregateObject (tc);
79 }
80 
81 }
82 
84 {
85 public:
87 private:
88  virtual void DoRun (void);
89  void RxCb (Ptr<Socket> socket);
90  void DoSendData (Ptr<Socket> socket, std::string to);
91 };
92 
94  : TestCase ("Ipv4PacketInfoTagTest")
95 {
96 }
97 
98 void
100 {
101  uint32_t availableData;
102  Ptr<Packet> m_receivedPacket;
103 
104  availableData = socket->GetRxAvailable ();
105  m_receivedPacket = socket->Recv (std::numeric_limits<uint32_t>::max (), 0);
106  NS_TEST_ASSERT_MSG_EQ (availableData, m_receivedPacket->GetSize (), "Did not read expected data");
107 
108  Ipv4PacketInfoTag tag;
109  bool found;
110  found = m_receivedPacket->RemovePacketTag (tag);
111  NS_TEST_ASSERT_MSG_EQ (found, true, "Could not find tag");
112 }
113 
114 void
116 {
117  Address realTo = InetSocketAddress (Ipv4Address (to.c_str ()), 200);
118  if (DynamicCast<UdpSocket> (socket) != 0)
119  {
120  NS_TEST_EXPECT_MSG_EQ (socket->SendTo (Create<Packet> (123), 0, realTo),
121  123, "100");
122  }
123  // Should only Ipv4RawSock
124  else
125  {
126  socket->SendTo (Create<Packet> (123), 0, realTo);
127  }
128 }
129 
130 void
132 {
133  Ptr<Node> node0 = CreateObject<Node> ();
134  Ptr<Node> node1 = CreateObject<Node> ();
135 
136  Ptr<SimpleNetDevice> device = CreateObject<SimpleNetDevice> ();
137  Ptr<SimpleNetDevice> device2 = CreateObject<SimpleNetDevice> ();
138 
139  // For Node 0
140  node0->AddDevice (device);
141  AddInternetStack (node0);
142  Ptr<Ipv4> ipv4 = node0->GetObject<Ipv4> ();
143 
144  uint32_t index = ipv4->AddInterface (device);
145  Ipv4InterfaceAddress ifaceAddr1 = Ipv4InterfaceAddress ("10.1.1.1",
146  "255.255.255.0");
147  ipv4->AddAddress (index, ifaceAddr1);
148  ipv4->SetMetric (index, 1);
149  ipv4->SetUp (index);
150 
151  // For Node 1
152  node1->AddDevice (device2);
153  AddInternetStack (node1);
154  ipv4 = node1->GetObject<Ipv4> ();
155 
156  index = ipv4->AddInterface (device2);
157  Ipv4InterfaceAddress ifaceAddr2 = Ipv4InterfaceAddress ("10.1.1.2",
158  "255.255.255.0");
159  ipv4->AddAddress (index, ifaceAddr2);
160  ipv4->SetMetric (index, 1);
161  ipv4->SetUp (index);
162 
163  // IPv4 test
164  Ptr<SocketFactory> factory = node0->GetObject<SocketFactory> (UdpSocketFactory::GetTypeId ());
165  Ptr<Socket> socket = factory->CreateSocket ();
166  InetSocketAddress local = InetSocketAddress (Ipv4Address::GetAny (), 200);
167  socket->Bind (local);
168  socket->SetRecvPktInfo (true);
169  socket->SetRecvCallback (MakeCallback (&Ipv4PacketInfoTagTest::RxCb, this));
170 
171  // receive on loopback
172  Simulator::ScheduleWithContext (socket->GetNode ()->GetId (), Seconds (0),
173  &Ipv4PacketInfoTagTest::DoSendData, this, socket, "127.0.0.1");
174  Simulator::Run ();
175 
176  Ptr<SocketFactory> factory2 = node1->GetObject<SocketFactory> (UdpSocketFactory::GetTypeId ());
177  Ptr<Socket> socket2 = factory2->CreateSocket ();
178  Simulator::ScheduleWithContext (socket2->GetNode ()->GetId (), Seconds (0),
179  &Ipv4PacketInfoTagTest::DoSendData, this, socket, "10.1.1.1");
180  Simulator::Run ();
181 
182  // ipv4 w rawsocket
183  factory = node0->GetObject<SocketFactory> (Ipv4RawSocketFactory::GetTypeId ());
184  socket = factory->CreateSocket ();
185  local = InetSocketAddress (Ipv4Address::GetAny (), 0);
186  socket->Bind (local);
187  socket->SetRecvPktInfo (true);
188  socket->SetRecvCallback (MakeCallback (&Ipv4PacketInfoTagTest::RxCb, this));
189 
190  // receive on loopback
191  Simulator::ScheduleWithContext (socket->GetNode ()->GetId (), Seconds (0),
192  &Ipv4PacketInfoTagTest::DoSendData, this, socket, "127.0.0.1");
193  Simulator::Run ();
194 
195  factory2 = node1->GetObject<SocketFactory> (Ipv4RawSocketFactory::GetTypeId ());
196  socket2 = factory2->CreateSocket ();
197  Simulator::ScheduleWithContext (socket2->GetNode ()->GetId (), Seconds (0),
198  &Ipv4PacketInfoTagTest::DoSendData, this, socket, "10.1.1.1");
199  Simulator::Run ();
200  Simulator::Destroy ();
201 }
202 
204 {
205 public:
207 private:
209 
211  : TestSuite ("ipv4-packet-info-tag", UNIT)
212 {
213  AddTestCase (new Ipv4PacketInfoTagTest (), TestCase::QUICK);
214 }
virtual uint32_t AddInterface(Ptr< NetDevice > device)=0
an Inet address class
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:462
A suite of tests to run.
Definition: test.h:1333
void AggregateObject(Ptr< Object > other)
Aggregate two Objects together.
Definition: object.cc:252
#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
Object to create transport layer instances that provide a socket API to applications.
a polymophic address class
Definition: address.h:90
virtual void DoRun(void)
Implementation to actually run this TestCase.
Ipv4PacketInfoTagTestSuite g_packetinfotagTests
#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
virtual void SetUp(uint32_t interface)=0
#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
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1489
void DoSendData(Ptr< Socket > socket, std::string to)
Access to the IPv4 forwarding table, interfaces, and configuration.
Definition: ipv4.h:76
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.
This class implements Linux struct pktinfo in order to deliver ancillary information to the socket in...
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:40
uint32_t AddDevice(Ptr< NetDevice > device)
Associate a NetDevice to this node.
Definition: node.cc:128
a class to store IPv4 address information on an interface
bool RemovePacketTag(Tag &tag)
Remove a packet tag.
Definition: packet.cc:831
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:895
virtual void SetMetric(uint32_t interface, uint16_t metric)=0
virtual bool AddAddress(uint32_t interface, Ipv4InterfaceAddress address)=0
virtual int SendTo(Ptr< Packet > p, uint32_t flags, const Address &toAddress)=0
Send data to a specified peer.
void RxCb(Ptr< Socket > socket)
virtual uint32_t GetRxAvailable(void) const =0
Return number of bytes which can be returned from one or multiple calls to Recv.
void SetRoutingProtocol(Ptr< Ipv4RoutingProtocol > routingProtocol)
Register a new routing protocol to be used by this Ipv4 stack.