A Discrete-Event Network Simulator
API
packet-socket-apps-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) 2014 Universita' di Firenze
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  * Author: Tommaso Pecorella <tommaso.pecorella@unifi.it>
19  */
20 
21 #include "ns3/test.h"
22 #include "ns3/simulator.h"
23 #include "ns3/uinteger.h"
24 #include "ns3/traced-callback.h"
25 #include "ns3/packet.h"
26 #include "ns3/packet-socket-helper.h"
27 #include "ns3/packet-socket-client.h"
28 #include "ns3/packet-socket-server.h"
29 #include "ns3/simple-net-device.h"
30 #include "ns3/simple-channel.h"
31 
32 using namespace ns3;
33 
34 
42 {
45 
46 public:
47  virtual void DoRun (void);
49 
55  void ReceivePkt (Ptr<const Packet> packet, const Address &from);
56 };
57 
59  : TestCase ("Packet Socket Apps test")
60 {
63 }
64 
66 {
67  if (packet)
68  {
69  m_receivedPacketSize = packet->GetSize ();
71  }
72 }
73 
74 
75 void
77 {
78  // Create topology
79 
81  nodes.Create (2);
82 
83  PacketSocketHelper packetSocket;
84 
85  // give packet socket powers to nodes.
86  packetSocket.Install (nodes);
87 
89  txDev = CreateObject<SimpleNetDevice> ();
90  nodes.Get (0)->AddDevice (txDev);
91 
93  rxDev = CreateObject<SimpleNetDevice> ();
94  nodes.Get (1)->AddDevice (rxDev);
95 
96  Ptr<SimpleChannel> channel = CreateObject<SimpleChannel> ();
97  txDev->SetChannel (channel);
98  rxDev->SetChannel (channel);
99  txDev->SetNode (nodes.Get (0));
100  rxDev->SetNode (nodes.Get (1));
101 
102 
103  PacketSocketAddress socketAddr;
104  socketAddr.SetSingleDevice (txDev->GetIfIndex ());
105  socketAddr.SetPhysicalAddress (rxDev->GetAddress ());
106  socketAddr.SetProtocol (1);
107 
108  Ptr<PacketSocketClient> client = CreateObject<PacketSocketClient> ();
109  client->SetRemote (socketAddr);
110  client->SetAttribute ("PacketSize", UintegerValue (1000));
111  client->SetAttribute ("MaxPackets", UintegerValue (3));
112  nodes.Get (0)->AddApplication (client);
113 
114  Ptr<PacketSocketServer> server = CreateObject<PacketSocketServer> ();
116  server->SetLocal (socketAddr);
117  nodes.Get (1)->AddApplication (server);
118 
119 
120  Simulator::Run ();
121  Simulator::Destroy ();
122 
123  NS_TEST_EXPECT_MSG_EQ (m_receivedPacketNumber, 3, "Number of packet received");
124  NS_TEST_EXPECT_MSG_EQ (m_receivedPacketSize, 1000, "Size of packet received");
125 }
126 
127 
135 {
136 public:
137  PacketSocketAppsTestSuite () : TestSuite ("packet-socket-apps", UNIT)
138  {
139  AddTestCase (new PacketSocketAppsTest, TestCase::QUICK);
140  }
141 };
142 
void SetLocal(PacketSocketAddress addr)
set the local address and protocol to be used
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:831
static PacketSocketAppsTestSuite g_packetSocketAppsTestSuite
Static variable for test initialization.
A suite of tests to run.
Definition: test.h:1342
an address for a packet socket
virtual void DoRun(void)
Implementation to actually run this TestCase.
#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:285
encapsulates test code
Definition: test.h:1155
PacketSocket apps TestSuite.
uint32_t m_receivedPacketNumber
Number of received packets.
Give ns3::PacketSocket powers to ns3::Node.
void SetSingleDevice(uint32_t device)
Set the address to match only a specified NetDevice.
a polymophic address class
Definition: address.h:90
channel
Definition: third.py:85
virtual void SetNode(Ptr< Node > node)
PacketSocket apps Unit Test.
nodes
Definition: first.py:25
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:299
Hold an unsigned integer type.
Definition: uinteger.h:44
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1489
virtual Address GetAddress(void) const
bool TraceConnectWithoutContext(std::string name, const CallbackBase &cb)
Connect a TraceSource to a Callback without a context.
Definition: object-base.cc:293
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void SetPhysicalAddress(const Address address)
Set the destination address.
keep track of a set of node pointers.
void SetRemote(PacketSocketAddress addr)
set the remote address and protocol to be used
uint32_t m_receivedPacketSize
Received packet size.
void SetProtocol(uint16_t protocol)
Set the protocol.
void Install(Ptr< Node > node) const
Aggregate an instance of a ns3::PacketSocketFactory onto the provided node.
virtual uint32_t GetIfIndex(void) const
This test suite implements a Unit Test.
Definition: test.h:1352
void ReceivePkt(Ptr< const Packet > packet, const Address &from)
Receive a packet.
void SetChannel(Ptr< SimpleChannel > channel)
Attach a channel to this net device.
void SetAttribute(std::string name, const AttributeValue &value)
Set a single attribute, raising fatal errors if unsuccessful.
Definition: object-base.cc:185