A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
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
31using namespace ns3;
32
33/**
34 * \ingroup network-test
35 * \ingroup tests
36 *
37 * \brief PacketSocket apps Unit Test
38 */
40{
41 uint32_t m_receivedPacketSize; //!< Received packet size
42 uint32_t m_receivedPacketNumber; //!< Number of received packets
43
44 public:
45 void DoRun() override;
47
48 /**
49 * Receive a packet
50 * \param packet The packet
51 * \param from Address of the sender
52 */
53 void ReceivePkt(Ptr<const Packet> packet, const Address& from);
54};
55
57 : TestCase("Packet Socket Apps test")
58{
61}
62
63void
65{
66 if (packet)
67 {
68 m_receivedPacketSize = packet->GetSize();
70 }
71}
72
73void
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));
109 nodes.Get(0)->AddApplication(client);
110
111 Ptr<PacketSocketServer> server = CreateObject<PacketSocketServer>();
112 server->TraceConnectWithoutContext("Rx", MakeCallback(&PacketSocketAppsTest::ReceivePkt, this));
113 server->SetLocal(socketAddr);
114 nodes.Get(1)->AddApplication(server);
115
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
123/**
124 * \ingroup network-test
125 * \ingroup tests
126 *
127 * \brief PacketSocket apps TestSuite
128 */
130{
131 public:
133 : TestSuite("packet-socket-apps", Type::UNIT)
134 {
135 AddTestCase(new PacketSocketAppsTest, TestCase::Duration::QUICK);
136 }
137};
138
140 g_packetSocketAppsTestSuite; //!< Static variable for test initialization
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:135
uint32_t AddApplication(Ptr< Application > application)
Associate an Application to this Node.
Definition: node.cc:164
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.
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:142
static void Run()
Run the simulation.
Definition: simulator.cc:178
encapsulates test code
Definition: test.h:1061
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:301
A suite of tests to run.
Definition: test.h:1268
Type
Type of test.
Definition: test.h:1275
static constexpr auto UNIT
Definition: test.h:1286
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:252
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:706
static PacketSocketAppsTestSuite g_packetSocketAppsTestSuite
Static variable for test initialization.