A Discrete-Event Network Simulator
API
packet-socket-apps-test-suite.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014 Universita' di Firenze
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Author: Tommaso Pecorella <tommaso.pecorella@unifi.it>
18  */
19 
20 #include "ns3/packet-socket-client.h"
21 #include "ns3/packet-socket-helper.h"
22 #include "ns3/packet-socket-server.h"
23 #include "ns3/packet.h"
24 #include "ns3/simple-channel.h"
25 #include "ns3/simple-net-device.h"
26 #include "ns3/simulator.h"
27 #include "ns3/test.h"
28 #include "ns3/traced-callback.h"
29 #include "ns3/uinteger.h"
30 
31 using namespace ns3;
32 
40 {
43 
44  public:
45  void DoRun() override;
47 
53  void ReceivePkt(Ptr<const Packet> packet, const Address& from);
54 };
55 
57  : TestCase("Packet Socket Apps test")
58 {
61 }
62 
63 void
65 {
66  if (packet)
67  {
68  m_receivedPacketSize = packet->GetSize();
70  }
71 }
72 
73 void
75 {
76  // Create topology
77 
79  nodes.Create(2);
80 
81  PacketSocketHelper packetSocket;
82 
83  // give packet socket powers to nodes.
84  packetSocket.Install(nodes);
85 
87  txDev = CreateObject<SimpleNetDevice>();
88  nodes.Get(0)->AddDevice(txDev);
89 
91  rxDev = CreateObject<SimpleNetDevice>();
92  nodes.Get(1)->AddDevice(rxDev);
93 
94  Ptr<SimpleChannel> channel = CreateObject<SimpleChannel>();
95  txDev->SetChannel(channel);
96  rxDev->SetChannel(channel);
97  txDev->SetNode(nodes.Get(0));
98  rxDev->SetNode(nodes.Get(1));
99 
100  PacketSocketAddress socketAddr;
101  socketAddr.SetSingleDevice(txDev->GetIfIndex());
102  socketAddr.SetPhysicalAddress(rxDev->GetAddress());
103  socketAddr.SetProtocol(1);
104 
105  Ptr<PacketSocketClient> client = CreateObject<PacketSocketClient>();
106  client->SetRemote(socketAddr);
107  client->SetAttribute("PacketSize", UintegerValue(1000));
108  client->SetAttribute("MaxPackets", UintegerValue(3));
110 
111  Ptr<PacketSocketServer> server = CreateObject<PacketSocketServer>();
112  server->TraceConnectWithoutContext("Rx", MakeCallback(&PacketSocketAppsTest::ReceivePkt, this));
113  server->SetLocal(socketAddr);
115 
116  Simulator::Run();
117  Simulator::Destroy();
118 
119  NS_TEST_EXPECT_MSG_EQ(m_receivedPacketNumber, 3, "Number of packet received");
120  NS_TEST_EXPECT_MSG_EQ(m_receivedPacketSize, 1000, "Size of packet received");
121 }
122 
130 {
131  public:
133  : TestSuite("packet-socket-apps", UNIT)
134  {
135  AddTestCase(new PacketSocketAppsTest, TestCase::QUICK);
136  }
137 };
138 
PacketSocket apps Unit Test.
uint32_t m_receivedPacketSize
Received packet size.
void DoRun() override
Implementation to actually run this TestCase.
uint32_t m_receivedPacketNumber
Number of received packets.
void ReceivePkt(Ptr< const Packet > packet, const Address &from)
Receive a packet.
a polymophic address class
Definition: address.h:101
keep track of a set of node pointers.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
uint32_t AddDevice(Ptr< NetDevice > device)
Associate a NetDevice to this node.
Definition: node.cc:138
uint32_t AddApplication(Ptr< Application > application)
Associate an Application to this Node.
Definition: node.cc:169
uint32_t GetSize() const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:861
an address for a packet socket
void SetProtocol(uint16_t protocol)
Set the protocol.
void SetPhysicalAddress(const Address address)
Set the destination address.
void SetSingleDevice(uint32_t device)
Set the address to match only a specified NetDevice.
Give ns3::PacketSocket powers to ns3::Node.
void Install(Ptr< Node > node) const
Aggregate an instance of a ns3::PacketSocketFactory onto the provided node.
encapsulates test code
Definition: test.h:1060
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:301
A suite of tests to run.
Definition: test.h:1256
@ UNIT
This test suite implements a Unit Test.
Definition: test.h:1265
Hold an unsigned integer type.
Definition: uinteger.h:45
#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:251
NodeContainer nodes
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Callback< R, Args... > MakeCallback(R(T::*memPtr)(Args...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:704
channel
Definition: third.py:88
static PacketSocketAppsTestSuite g_packetSocketAppsTestSuite
Static variable for test initialization.