A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 namespace ns3 {
52 
53 static void
55 {
56  //ARP
57  Ptr<ArpL3Protocol> arp = CreateObject<ArpL3Protocol> ();
58  node->AggregateObject (arp);
59  //IPV4
60  Ptr<Ipv4L3Protocol> ipv4 = CreateObject<Ipv4L3Protocol> ();
61  //Routing for Ipv4
62  Ptr<Ipv4ListRouting> ipv4Routing = CreateObject<Ipv4ListRouting> ();
63  ipv4->SetRoutingProtocol (ipv4Routing);
64  Ptr<Ipv4StaticRouting> ipv4staticRouting = CreateObject<Ipv4StaticRouting> ();
65  ipv4Routing->AddRoutingProtocol (ipv4staticRouting, 0);
66  node->AggregateObject (ipv4);
67  //ICMP
68  Ptr<Icmpv4L4Protocol> icmp = CreateObject<Icmpv4L4Protocol> ();
69  node->AggregateObject (icmp);
70  //UDP
71  Ptr<UdpL4Protocol> udp = CreateObject<UdpL4Protocol> ();
72  node->AggregateObject (udp);
73 }
74 
76 {
77 public:
79 private:
80  virtual void DoRun (void);
81  void RxCb (Ptr<Socket> socket);
82  void DoSendData (Ptr<Socket> socket, std::string to);
83 };
84 
86  : TestCase ("Ipv4PacketInfoTagTest")
87 {
88 }
89 
90 void
92 {
93  uint32_t availableData;
94  Ptr<Packet> m_receivedPacket;
95 
96  availableData = socket->GetRxAvailable ();
97  m_receivedPacket = socket->Recv (std::numeric_limits<uint32_t>::max (), 0);
98  NS_TEST_ASSERT_MSG_EQ (availableData, m_receivedPacket->GetSize (), "Did not read expected data");
99 
100  Ipv4PacketInfoTag tag;
101  bool found;
102  found = m_receivedPacket->RemovePacketTag (tag);
103  NS_TEST_ASSERT_MSG_EQ (found, true, "Could not find tag");
104 }
105 
106 void
108 {
109  Address realTo = InetSocketAddress (Ipv4Address (to.c_str ()), 200);
110  if (DynamicCast<UdpSocket> (socket) != 0)
111  {
112  NS_TEST_EXPECT_MSG_EQ (socket->SendTo (Create<Packet> (123), 0, realTo),
113  123, "XXX");
114  }
115  // Should only Ipv4RawSock
116  else
117  {
118  socket->SendTo (Create<Packet> (123), 0, realTo);
119  }
120 }
121 
122 void
124 {
125  Ptr<Node> node0 = CreateObject<Node> ();
126  Ptr<Node> node1 = CreateObject<Node> ();
127 
128  Ptr<SimpleNetDevice> device = CreateObject<SimpleNetDevice> ();
129  Ptr<SimpleNetDevice> device2 = CreateObject<SimpleNetDevice> ();
130 
131  // For Node 0
132  node0->AddDevice (device);
133  AddInternetStack (node0);
134  Ptr<Ipv4> ipv4 = node0->GetObject<Ipv4> ();
135 
136  uint32_t index = ipv4->AddInterface (device);
137  Ipv4InterfaceAddress ifaceAddr1 = Ipv4InterfaceAddress ("10.1.1.1",
138  "255.255.255.0");
139  ipv4->AddAddress (index, ifaceAddr1);
140  ipv4->SetMetric (index, 1);
141  ipv4->SetUp (index);
142 
143  // For Node 1
144  node1->AddDevice (device2);
145  AddInternetStack (node1);
146  ipv4 = node1->GetObject<Ipv4> ();
147 
148  index = ipv4->AddInterface (device2);
149  Ipv4InterfaceAddress ifaceAddr2 = Ipv4InterfaceAddress ("10.1.1.2",
150  "255.255.255.0");
151  ipv4->AddAddress (index, ifaceAddr2);
152  ipv4->SetMetric (index, 1);
153  ipv4->SetUp (index);
154 
155  // IPv4 test
157  Ptr<Socket> socket = factory->CreateSocket ();
159  socket->Bind (local);
160  socket->SetRecvPktInfo (true);
161  socket->SetRecvCallback (MakeCallback (&ns3::Ipv4PacketInfoTagTest::RxCb, this));
162 
163  // receive on loopback
164  Simulator::ScheduleWithContext (socket->GetNode ()->GetId (), Seconds (0),
165  &Ipv4PacketInfoTagTest::DoSendData, this, socket, "127.0.0.1");
166  Simulator::Run ();
167 
168  Ptr<SocketFactory> factory2 = node1->GetObject<SocketFactory> (UdpSocketFactory::GetTypeId ());
169  Ptr<Socket> socket2 = factory2->CreateSocket ();
170  Simulator::ScheduleWithContext (socket2->GetNode ()->GetId (), Seconds (0),
171  &Ipv4PacketInfoTagTest::DoSendData, this, socket, "10.1.1.1");
172  Simulator::Run ();
173 
174  // ipv4 w rawsocket
176  socket = factory->CreateSocket ();
177  local = InetSocketAddress (Ipv4Address::GetAny (), 0);
178  socket->Bind (local);
179  socket->SetRecvPktInfo (true);
180  socket->SetRecvCallback (MakeCallback (&ns3::Ipv4PacketInfoTagTest::RxCb, this));
181 
182  // receive on loopback
183  Simulator::ScheduleWithContext (socket->GetNode ()->GetId (), Seconds (0),
184  &Ipv4PacketInfoTagTest::DoSendData, this, socket, "127.0.0.1");
185  Simulator::Run ();
186 
187  factory2 = node1->GetObject<SocketFactory> (Ipv4RawSocketFactory::GetTypeId ());
188  socket2 = factory2->CreateSocket ();
189  Simulator::ScheduleWithContext (socket2->GetNode ()->GetId (), Seconds (0),
190  &Ipv4PacketInfoTagTest::DoSendData, this, socket, "10.1.1.1");
191  Simulator::Run ();
193 }
194 
196 {
197 public:
199 private:
201 
203  : TestSuite ("ipv4-packet-info-tag", UNIT)
204 {
206 }
207 
208 
209 }