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/simulator.h"
49 #include "ns3/node.h"
50 
51 using namespace ns3;
52 
53 namespace {
54 
55 static void
57 {
58  //ARP
59  Ptr<ArpL3Protocol> arp = CreateObject<ArpL3Protocol> ();
60  node->AggregateObject (arp);
61  //IPV4
62  Ptr<Ipv4L3Protocol> ipv4 = CreateObject<Ipv4L3Protocol> ();
63  //Routing for Ipv4
64  Ptr<Ipv4ListRouting> ipv4Routing = CreateObject<Ipv4ListRouting> ();
65  ipv4->SetRoutingProtocol (ipv4Routing);
66  Ptr<Ipv4StaticRouting> ipv4staticRouting = CreateObject<Ipv4StaticRouting> ();
67  ipv4Routing->AddRoutingProtocol (ipv4staticRouting, 0);
68  node->AggregateObject (ipv4);
69  //ICMP
70  Ptr<Icmpv4L4Protocol> icmp = CreateObject<Icmpv4L4Protocol> ();
71  node->AggregateObject (icmp);
72  //UDP
73  Ptr<UdpL4Protocol> udp = CreateObject<UdpL4Protocol> ();
74  node->AggregateObject (udp);
75 }
76 
77 }
78 
80 {
81 public:
83 private:
84  virtual void DoRun (void);
85  void RxCb (Ptr<Socket> socket);
86  void DoSendData (Ptr<Socket> socket, std::string to);
87 };
88 
90  : TestCase ("Ipv4PacketInfoTagTest")
91 {
92 }
93 
94 void
96 {
97  uint32_t availableData;
98  Ptr<Packet> m_receivedPacket;
99 
100  availableData = socket->GetRxAvailable ();
101  m_receivedPacket = socket->Recv (std::numeric_limits<uint32_t>::max (), 0);
102  NS_TEST_ASSERT_MSG_EQ (availableData, m_receivedPacket->GetSize (), "Did not read expected data");
103 
104  Ipv4PacketInfoTag tag;
105  bool found;
106  found = m_receivedPacket->RemovePacketTag (tag);
107  NS_TEST_ASSERT_MSG_EQ (found, true, "Could not find tag");
108 }
109 
110 void
112 {
113  Address realTo = InetSocketAddress (Ipv4Address (to.c_str ()), 200);
114  if (DynamicCast<UdpSocket> (socket) != 0)
115  {
116  NS_TEST_EXPECT_MSG_EQ (socket->SendTo (Create<Packet> (123), 0, realTo),
117  123, "100");
118  }
119  // Should only Ipv4RawSock
120  else
121  {
122  socket->SendTo (Create<Packet> (123), 0, realTo);
123  }
124 }
125 
126 void
128 {
129  Ptr<Node> node0 = CreateObject<Node> ();
130  Ptr<Node> node1 = CreateObject<Node> ();
131 
132  Ptr<SimpleNetDevice> device = CreateObject<SimpleNetDevice> ();
133  Ptr<SimpleNetDevice> device2 = CreateObject<SimpleNetDevice> ();
134 
135  // For Node 0
136  node0->AddDevice (device);
137  AddInternetStack (node0);
138  Ptr<Ipv4> ipv4 = node0->GetObject<Ipv4> ();
139 
140  uint32_t index = ipv4->AddInterface (device);
141  Ipv4InterfaceAddress ifaceAddr1 = Ipv4InterfaceAddress ("10.1.1.1",
142  "255.255.255.0");
143  ipv4->AddAddress (index, ifaceAddr1);
144  ipv4->SetMetric (index, 1);
145  ipv4->SetUp (index);
146 
147  // For Node 1
148  node1->AddDevice (device2);
149  AddInternetStack (node1);
150  ipv4 = node1->GetObject<Ipv4> ();
151 
152  index = ipv4->AddInterface (device2);
153  Ipv4InterfaceAddress ifaceAddr2 = Ipv4InterfaceAddress ("10.1.1.2",
154  "255.255.255.0");
155  ipv4->AddAddress (index, ifaceAddr2);
156  ipv4->SetMetric (index, 1);
157  ipv4->SetUp (index);
158 
159  // IPv4 test
160  Ptr<SocketFactory> factory = node0->GetObject<SocketFactory> (UdpSocketFactory::GetTypeId ());
161  Ptr<Socket> socket = factory->CreateSocket ();
162  InetSocketAddress local = InetSocketAddress (Ipv4Address::GetAny (), 200);
163  socket->Bind (local);
164  socket->SetRecvPktInfo (true);
165  socket->SetRecvCallback (MakeCallback (&Ipv4PacketInfoTagTest::RxCb, this));
166 
167  // receive on loopback
168  Simulator::ScheduleWithContext (socket->GetNode ()->GetId (), Seconds (0),
169  &Ipv4PacketInfoTagTest::DoSendData, this, socket, "127.0.0.1");
170  Simulator::Run ();
171 
172  Ptr<SocketFactory> factory2 = node1->GetObject<SocketFactory> (UdpSocketFactory::GetTypeId ());
173  Ptr<Socket> socket2 = factory2->CreateSocket ();
174  Simulator::ScheduleWithContext (socket2->GetNode ()->GetId (), Seconds (0),
175  &Ipv4PacketInfoTagTest::DoSendData, this, socket, "10.1.1.1");
176  Simulator::Run ();
177 
178  // ipv4 w rawsocket
179  factory = node0->GetObject<SocketFactory> (Ipv4RawSocketFactory::GetTypeId ());
180  socket = factory->CreateSocket ();
181  local = InetSocketAddress (Ipv4Address::GetAny (), 0);
182  socket->Bind (local);
183  socket->SetRecvPktInfo (true);
184  socket->SetRecvCallback (MakeCallback (&Ipv4PacketInfoTagTest::RxCb, this));
185 
186  // receive on loopback
187  Simulator::ScheduleWithContext (socket->GetNode ()->GetId (), Seconds (0),
188  &Ipv4PacketInfoTagTest::DoSendData, this, socket, "127.0.0.1");
189  Simulator::Run ();
190 
191  factory2 = node1->GetObject<SocketFactory> (Ipv4RawSocketFactory::GetTypeId ());
192  socket2 = factory2->CreateSocket ();
193  Simulator::ScheduleWithContext (socket2->GetNode ()->GetId (), Seconds (0),
194  &Ipv4PacketInfoTagTest::DoSendData, this, socket, "10.1.1.1");
195  Simulator::Run ();
196  Simulator::Destroy ();
197 }
198 
200 {
201 public:
203 private:
205 
207  : TestSuite ("ipv4-packet-info-tag", UNIT)
208 {
209  AddTestCase (new Ipv4PacketInfoTagTest (), TestCase::QUICK);
210 }
virtual uint32_t AddInterface(Ptr< NetDevice > device)=0
an Inet address class
A suite of tests to run.
Definition: test.h:1276
#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:273
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:766
virtual Ptr< Socket > CreateSocket(void)=0
encapsulates test code
Definition: test.h:1108
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
void AddTestCase(TestCase *testCase, enum TestDuration duration)
Add an individual child TestCase to this test suite.
Definition: test.cc:190
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:158
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1296
void DoSendData(Ptr< Socket > socket, std::string to)
void AggregateObject(Ptr< Object > other)
Aggregate two Objects together.
Definition: object.cc:246
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:121
a class to store IPv4 address information on an interface
bool RemovePacketTag(Tag &tag)
Remove a packet tag.
Definition: packet.cc:843
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:866
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.
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:455
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.