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 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Tommaso Pecorella <tommaso.pecorella@unifi.it>
7 */
8
9#include "ns3/packet-socket-client.h"
10#include "ns3/packet-socket-helper.h"
11#include "ns3/packet-socket-server.h"
12#include "ns3/packet.h"
13#include "ns3/simple-channel.h"
14#include "ns3/simple-net-device.h"
15#include "ns3/simulator.h"
16#include "ns3/test.h"
17#include "ns3/traced-callback.h"
18#include "ns3/uinteger.h"
19
20using namespace ns3;
21
22/**
23 * \ingroup network-test
24 * \ingroup tests
25 *
26 * \brief PacketSocket apps Unit Test
27 */
29{
30 uint32_t m_receivedPacketSize; //!< Received packet size
31 uint32_t m_receivedPacketNumber; //!< Number of received packets
32
33 public:
34 void DoRun() override;
36
37 /**
38 * Receive a packet
39 * \param packet The packet
40 * \param from Address of the sender
41 */
42 void ReceivePkt(Ptr<const Packet> packet, const Address& from);
43};
44
51
52void
54{
55 if (packet)
56 {
57 m_receivedPacketSize = packet->GetSize();
59 }
60}
61
62void
64{
65 // Create topology
66
68 nodes.Create(2);
69
70 PacketSocketHelper packetSocket;
71
72 // give packet socket powers to nodes.
73 packetSocket.Install(nodes);
74
77 nodes.Get(0)->AddDevice(txDev);
78
81 nodes.Get(1)->AddDevice(rxDev);
82
84 txDev->SetChannel(channel);
85 rxDev->SetChannel(channel);
86 txDev->SetNode(nodes.Get(0));
87 rxDev->SetNode(nodes.Get(1));
88
89 PacketSocketAddress socketAddr;
90 socketAddr.SetSingleDevice(txDev->GetIfIndex());
91 socketAddr.SetPhysicalAddress(rxDev->GetAddress());
92 socketAddr.SetProtocol(1);
93
95 client->SetRemote(socketAddr);
96 client->SetAttribute("PacketSize", UintegerValue(1000));
97 client->SetAttribute("MaxPackets", UintegerValue(3));
98 nodes.Get(0)->AddApplication(client);
99
101 server->TraceConnectWithoutContext("Rx", MakeCallback(&PacketSocketAppsTest::ReceivePkt, this));
102 server->SetLocal(socketAddr);
103 nodes.Get(1)->AddApplication(server);
104
107
108 NS_TEST_EXPECT_MSG_EQ(m_receivedPacketNumber, 3, "Number of packet received");
109 NS_TEST_EXPECT_MSG_EQ(m_receivedPacketSize, 1000, "Size of packet received");
110}
111
112/**
113 * \ingroup network-test
114 * \ingroup tests
115 *
116 * \brief PacketSocket apps TestSuite
117 */
119{
120 public:
122 : TestSuite("packet-socket-apps", Type::UNIT)
123 {
124 AddTestCase(new PacketSocketAppsTest, TestCase::Duration::QUICK);
125 }
126};
127
129 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:90
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:124
uint32_t AddApplication(Ptr< Application > application)
Associate an Application to this Node.
Definition node.cc:153
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.
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
static void Run()
Run the simulation.
Definition simulator.cc:167
encapsulates test code
Definition test.h:1050
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition test.cc:292
A suite of tests to run.
Definition test.h:1267
Type
Type of test.
Definition test.h:1274
static constexpr auto UNIT
Definition test.h:1291
Hold an unsigned integer type.
Definition uinteger.h:34
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
#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:241
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:684
static PacketSocketAppsTestSuite g_packetSocketAppsTestSuite
Static variable for test initialization.