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
32using namespace ns3;
33
34
42{
45
46public:
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
75void
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> ();
115 server->TraceConnectWithoutContext ("Rx", MakeCallback (&PacketSocketAppsTest::ReceivePkt, this));
116 server->SetLocal (socketAddr);
117 nodes.Get (1)->AddApplication (server);
118
119
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{
136public:
137 PacketSocketAppsTestSuite () : TestSuite ("packet-socket-apps", UNIT)
138 {
139 AddTestCase (new PacketSocketAppsTest, TestCase::QUICK);
140 }
141};
142
void Run(ObjectFactory &factory, uint32_t pop, uint32_t total, uint32_t runs, Ptr< RandomVariableStream > eventStream, bool calRev)
Perform the runs for a single scheduler type.
PacketSocket apps Unit Test.
uint32_t m_receivedPacketSize
Received packet size.
uint32_t m_receivedPacketNumber
Number of received packets.
void ReceivePkt(Ptr< const Packet > packet, const Address &from)
Receive a packet.
virtual void DoRun(void)
Implementation to actually run this TestCase.
a polymophic address class
Definition: address.h:91
keep track of a set of node pointers.
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:856
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:994
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:299
A suite of tests to run.
Definition: test.h:1188
@ UNIT
This test suite implements a Unit Test.
Definition: test.h:1197
Hold an unsigned integer type.
Definition: uinteger.h:44
#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:240
nodes
Definition: first.py:28
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Callback< R, Ts... > MakeCallback(R(T::*memPtr)(Ts...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:1648
channel
Definition: third.py:83
static PacketSocketAppsTestSuite g_packetSocketAppsTestSuite
Static variable for test initialization.