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 #include "ns3/internet-stack-helper.h"
49 #include "ns3/simple-net-device-helper.h"
50 
51 using namespace ns3;
52 
53 //static void
54 //AddInternetStack (Ptr<Node> node)
55 //{
56 // Ptr<Ipv6L3Protocol> ipv6 = CreateObject<Ipv6L3Protocol> ();
57 // Ptr<Icmpv6L4Protocol> icmpv6 = CreateObject<Icmpv6L4Protocol> ();
58 // node->AggregateObject (ipv6);
59 // node->AggregateObject (icmpv6);
60 // ipv6->Insert (icmpv6);
61 // icmpv6->SetAttribute ("DAD", BooleanValue (false));
62 //
63 // //Routing for Ipv6
64 // Ptr<Ipv6ListRouting> ipv6Routing = CreateObject<Ipv6ListRouting> ();
65 // ipv6->SetRoutingProtocol (ipv6Routing);
66 // Ptr<Ipv6StaticRouting> ipv6staticRouting = CreateObject<Ipv6StaticRouting> ();
67 // ipv6Routing->AddRoutingProtocol (ipv6staticRouting, 0);
68 //
69 // /* register IPv6 extensions and options */
70 // ipv6->RegisterExtensions ();
71 // ipv6->RegisterOptions ();
72 //
73 // // Traffic Control
74 // Ptr<TrafficControlLayer> tc = CreateObject<TrafficControlLayer> ();
75 // node->AggregateObject (tc);
76 //}
77 
85 {
86 public:
88 private:
89  virtual void DoRun (void);
94  void RxCb (Ptr<Socket> socket);
100  void DoSendData (Ptr<Socket> socket, std::string to);
101 };
102 
104  : TestCase ("Ipv6PacketInfoTagTest")
105 {
106 }
107 
108 void
110 {
111  uint32_t availableData;
112  Ptr<Packet> m_receivedPacket;
113 
114  availableData = socket->GetRxAvailable ();
115  m_receivedPacket = socket->Recv (std::numeric_limits<uint32_t>::max (), 0);
116  NS_TEST_ASSERT_MSG_EQ (availableData, m_receivedPacket->GetSize (), "Did not read expected data");
117 
118  Ipv6PacketInfoTag tag;
119  bool found;
120  found = m_receivedPacket->RemovePacketTag (tag);
121  NS_TEST_ASSERT_MSG_EQ (found, true, "Could not find tag");
122 }
123 
124 void
126 {
127  Address realTo = Inet6SocketAddress (Ipv6Address (to.c_str ()), 200);
128  if (DynamicCast<UdpSocket> (socket) != 0)
129  {
130  NS_TEST_EXPECT_MSG_EQ (socket->SendTo (Create<Packet> (123), 0, realTo),
131  123, "100");
132  }
133  // Should only Ipv6RawSock
134  else
135  {
136  socket->SendTo (Create<Packet> (123), 0, realTo);
137  }
138 }
139 
140 void
142 {
143  Ptr<Node> node0 = CreateObject<Node> ();
144  Ptr<Node> node1 = CreateObject<Node> ();
145 
146  SimpleNetDeviceHelper simpleNetDevHelper;
147  NetDeviceContainer devs = simpleNetDevHelper.Install (NodeContainer (node0, node1));
148  Ptr<SimpleNetDevice> device = DynamicCast<SimpleNetDevice> (devs.Get (0));
149  Ptr<SimpleNetDevice> device2 = DynamicCast<SimpleNetDevice> (devs.Get (1));
150 
151  InternetStackHelper internet;
152  internet.SetIpv4StackInstall (false);
153 
154  // For Node 0
155  node0->AddDevice (device);
156  internet.Install (node0);
157  Ptr<Ipv6> ipv6 = node0->GetObject<Ipv6> ();
159  icmpv6->SetAttribute ("DAD", BooleanValue (false));
160 
161  uint32_t index = ipv6->AddInterface (device);
162  Ipv6InterfaceAddress ifaceAddr1 = Ipv6InterfaceAddress (Ipv6Address ("2000:1000:0:2000::1"),
163  Ipv6Prefix (64));
164  ipv6->AddAddress (index, ifaceAddr1);
165  ipv6->SetMetric (index, 1);
166  ipv6->SetUp (index);
167 
168  // For Node 1
169  node1->AddDevice (device2);
170  internet.Install (node1);
171  ipv6 = node1->GetObject<Ipv6> ();
172  icmpv6 = node0->GetObject<Icmpv6L4Protocol> ();
173  icmpv6->SetAttribute ("DAD", BooleanValue (false));
174 
175  index = ipv6->AddInterface (device2);
176  Ipv6InterfaceAddress ifaceAddr2 = Ipv6InterfaceAddress (Ipv6Address ("2000:1000:0:2000::2"),
177  Ipv6Prefix (64));
178  ipv6->AddAddress (index, ifaceAddr2);
179  ipv6->SetMetric (index, 1);
180  ipv6->SetUp (index);
181 
182  // ipv6 w rawsocket
183  Ptr<SocketFactory> factory = node0->GetObject<SocketFactory> (Ipv6RawSocketFactory::GetTypeId ());
184  Ptr<Socket> socket = factory->CreateSocket ();
185  Inet6SocketAddress local = Inet6SocketAddress (Ipv6Address::GetAny (), 0);
186  socket->SetAttribute ("Protocol", UintegerValue (Ipv6Header::IPV6_ICMPV6));
187  socket->Bind (local);
188  socket->SetRecvPktInfo (true);
189  socket->SetRecvCallback (MakeCallback (&Ipv6PacketInfoTagTest::RxCb, this));
190 
191  // receive on loopback
192  Simulator::ScheduleWithContext (socket->GetNode ()->GetId (), Seconds (0),
193  &Ipv6PacketInfoTagTest::DoSendData, this, socket, "::1");
194  Simulator::Run ();
195 
196  Ptr<SocketFactory> factory2 = node1->GetObject<SocketFactory> (Ipv6RawSocketFactory::GetTypeId ());
197  Ptr<Socket> socket2 = factory2->CreateSocket ();
198  std::stringstream dst;
199  dst << ifaceAddr1.GetAddress ();
200  Simulator::ScheduleWithContext (socket2->GetNode ()->GetId (), Seconds (0),
201  &Ipv6PacketInfoTagTest::DoSendData, this, socket,
202  dst.str ());
203  Simulator::Run ();
204 
205 #ifdef UDP6_SUPPORTED
206  // IPv6 test
207  factory = node0->GetObject<SocketFactory> (UdpSocketFactory::GetTypeId ());
208  socket = factory->CreateSocket ();
209  local = Inet6SocketAddress (Ipv6Address::GetAny (), 200);
210  socket->Bind (local);
211  socket->SetRecvPktInfo (true);
212  socket->SetRecvCallback (MakeCallback (&Ipv6PacketInfoTagTest::RxCb, this));
213 
214  // receive on loopback
215  Simulator::ScheduleWithContext (socket->GetNode ()->GetId (), Seconds (0),
216  &Ipv6PacketInfoTagTest::DoSendData, this, socket, "::1");
217  Simulator::Run ();
218 
219  factory2 = node1->GetObject<SocketFactory> (UdpSocketFactory::GetTypeId ());
220  socket2 = factory2->CreateSocket ();
221  Simulator::ScheduleWithContext (socket2->GetNode ()->GetId (), Seconds (0),
222  &Ipv6PacketInfoTagTest::DoSendData, this, socket, "10.1.1.1");
223  Simulator::Run ();
224 
225 #endif // UDP6_SUPPORTED
226 
227  Simulator::Destroy ();
228  // IPv6 test
229 }
230 
238 {
239 public:
241 private:
242 };
243 
245  : TestSuite ("ipv6-packet-info-tag", UNIT)
246 {
247  AddTestCase (new Ipv6PacketInfoTagTest (), TestCase::QUICK);
248 }
249 
#define max(a, b)
Definition: 80211b.c:43
virtual void DoRun(void)
Implementation to actually run this TestCase.
void RxCb(Ptr< Socket > socket)
Receive callback.
void DoSendData(Ptr< Socket > socket, std::string to)
Send data.
a polymophic address class
Definition: address.h:91
AttributeValue implementation for Boolean.
Definition: boolean.h:37
An implementation of the ICMPv6 protocol.
An Inet6 address class.
aggregate IP/TCP/UDP functionality to existing Nodes.
void SetIpv4StackInstall(bool enable)
Enable/disable IPv4 stack install.
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
Describes an IPv6 address.
Definition: ipv6-address.h:50
Access to the IPv6 forwarding table, interfaces, and configuration.
Definition: ipv6.h:82
IPv6 address associated with an interface.
Ipv6Address GetAddress() const
Get the IPv6 address.
This class implements a tag that carries socket ancillary data to the socket interface.
Describes an IPv6 prefix.
Definition: ipv6-address.h:456
holds a vector of ns3::NetDevice pointers
Ptr< NetDevice > Get(uint32_t i) const
Get the Ptr<NetDevice> stored in this container at a given index.
keep track of a set of node pointers.
uint32_t AddDevice(Ptr< NetDevice > device)
Associate a NetDevice to this node.
Definition: node.cc:130
void SetAttribute(std::string name, const AttributeValue &value)
Set a single attribute, raising fatal errors if unsuccessful.
Definition: object-base.cc:256
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:470
bool RemovePacketTag(Tag &tag)
Remove a packet tag.
Definition: packet.cc:963
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:856
build a set of SimpleNetDevice objects
NetDeviceContainer Install(Ptr< Node > node) const
This method creates an ns3::SimpleChannel with the attributes configured by SimpleNetDeviceHelper::Se...
Object to create transport layer instances that provide a socket API to applications.
virtual Ptr< Packet > Recv(uint32_t maxSize, uint32_t flags)=0
Read data from the socket.
virtual uint32_t GetRxAvailable(void) const =0
Return number of bytes which can be returned from one or multiple calls to Recv.
virtual int SendTo(Ptr< Packet > p, uint32_t flags, const Address &toAddress)=0
Send data to a specified peer.
encapsulates test code
Definition: test.h:994
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:299
A suite of tests to run.
Definition: test.h:1188
Hold an unsigned integer type.
Definition: uinteger.h:44
#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:141
#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:240
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1244
static Ipv6PacketInfoTagTestSuite g_packetinfotagTests
Static variable for test initialization.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Callback< R, Ts... > MakeCallback(R(T::*memPtr)(Ts...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:1648