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 
36 {
39 
40 public:
41  virtual void DoRun (void);
43 
44  void ReceivePkt (Ptr<const Packet> packet, const Address &from);
45 };
46 
48  : TestCase ("Packet Socket Apps test")
49 {
52 }
53 
55 {
56  if (packet)
57  {
58  m_receivedPacketSize = packet->GetSize ();
60  }
61 }
62 
63 
64 void
66 {
67  // Create topology
68 
70  nodes.Create (2);
71 
72  PacketSocketHelper packetSocket;
73 
74  // give packet socket powers to nodes.
75  packetSocket.Install (nodes);
76 
78  txDev = CreateObject<SimpleNetDevice> ();
79  nodes.Get (0)->AddDevice (txDev);
80 
82  rxDev = CreateObject<SimpleNetDevice> ();
83  nodes.Get (1)->AddDevice (rxDev);
84 
85  Ptr<SimpleChannel> channel = CreateObject<SimpleChannel> ();
86  txDev->SetChannel (channel);
87  rxDev->SetChannel (channel);
88  txDev->SetNode (nodes.Get (0));
89  rxDev->SetNode (nodes.Get (1));
90 
91 
92  PacketSocketAddress socketAddr;
93  socketAddr.SetSingleDevice (txDev->GetIfIndex ());
94  socketAddr.SetPhysicalAddress (rxDev->GetAddress ());
95  socketAddr.SetProtocol (1);
96 
97  Ptr<PacketSocketClient> client = CreateObject<PacketSocketClient> ();
98  client->SetRemote (socketAddr);
99  client->SetAttribute ("PacketSize", UintegerValue (1000));
100  client->SetAttribute ("MaxPackets", UintegerValue (3));
101  nodes.Get (0)->AddApplication (client);
102 
103  Ptr<PacketSocketServer> server = CreateObject<PacketSocketServer> ();
105  server->SetLocal (socketAddr);
106  nodes.Get (1)->AddApplication (server);
107 
108 
109  Simulator::Run ();
110  Simulator::Destroy ();
111 
112  NS_TEST_EXPECT_MSG_EQ (m_receivedPacketNumber, 3, "Number of packet received");
113  NS_TEST_EXPECT_MSG_EQ (m_receivedPacketSize, 1000, "Size of packet received");
114 }
115 
116 
117 //-----------------------------------------------------------------------------
118 //-----------------------------------------------------------------------------
120 {
121 public:
122  PacketSocketAppsTestSuite () : TestSuite ("packet-socket-apps", UNIT)
123  {
124  AddTestCase (new PacketSocketAppsTest, TestCase::QUICK);
125  }
virtual Address GetAddress(void) const
tuple channel
Definition: third.py:85
uint32_t AddApplication(Ptr< Application > application)
Associate an Application to this Node.
Definition: node.cc:157
void SetLocal(PacketSocketAddress addr)
set the local address and protocol to be used
PacketSocketAppsTestSuite g_packetSocketAppsTestSuite
A suite of tests to run.
Definition: test.h:1333
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: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
encapsulates test code
Definition: test.h:1147
This test suite implements a Unit Test.
Definition: test.h:1343
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
tuple nodes
Definition: first.py:25
virtual void SetNode(Ptr< Node > node)
void AddTestCase(TestCase *testCase, enum TestDuration duration)
Add an individual child TestCase to this test suite.
Definition: test.cc:298
Hold an unsigned integer type.
Definition: uinteger.h:44
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1489
bool TraceConnectWithoutContext(std::string name, const CallbackBase &cb)
Connect a TraceSource to a Callback without a context.
Definition: object-base.cc:299
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 Install(Ptr< Node > node) const
Aggregate an instance of a ns3::PacketSocketFactory onto the provided node.
uint32_t AddDevice(Ptr< NetDevice > device)
Associate a NetDevice to this node.
Definition: node.cc:128
void SetRemote(PacketSocketAddress addr)
set the remote address and protocol to be used
Ptr< Node > Get(uint32_t i) const
Get the Ptr stored in this container at a given index.
void SetProtocol(uint16_t protocol)
Set the protocol.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
void ReceivePkt(Ptr< const Packet > packet, const Address &from)
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:191
virtual uint32_t GetIfIndex(void) const