A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
point-to-point-test.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2009 INRIA
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: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
18 */
19
20#include "ns3/drop-tail-queue.h"
21#include "ns3/net-device-queue-interface.h"
22#include "ns3/point-to-point-channel.h"
23#include "ns3/point-to-point-net-device.h"
24#include "ns3/simulator.h"
25#include "ns3/test.h"
26
27#include <string>
28
29using namespace ns3;
30
31/**
32 * \brief Test class for PointToPoint model
33 *
34 * It tries to send one packet from one NetDevice to another, over a
35 * PointToPointChannel.
36 */
38{
39 public:
40 /**
41 * \brief Create the test
42 */
44
45 /**
46 * \brief Run the test
47 */
48 void DoRun() override;
49
50 private:
51 Ptr<const Packet> m_recvdPacket; //!< received packet
52 /**
53 * \brief Send one packet to the device specified
54 *
55 * \param device NetDevice to send to.
56 * \param buffer Payload content of the packet.
57 * \param size Size of the payload.
58 */
59 void SendOnePacket(Ptr<PointToPointNetDevice> device, const uint8_t* buffer, uint32_t size);
60 /**
61 * \brief Callback function which sets the recvdPacket parameter
62 *
63 * \param dev The receiving device.
64 * \param pkt The received packet.
65 * \param mode The protocol mode used.
66 * \param sender The sender address.
67 *
68 * \return A boolean indicating packet handled properly.
69 */
70 bool RxPacket(Ptr<NetDevice> dev, Ptr<const Packet> pkt, uint16_t mode, const Address& sender);
71};
72
74 : TestCase("PointToPoint")
75{
76}
77
78void
80 const uint8_t* buffer,
81 uint32_t size)
82{
83 Ptr<Packet> p = Create<Packet>(buffer, size);
84 device->Send(p, device->GetBroadcast(), 0x800);
85}
86
87bool
90 uint16_t mode,
91 const Address& sender)
92{
93 m_recvdPacket = pkt;
94 return true;
95}
96
97void
99{
100 Ptr<Node> a = CreateObject<Node>();
101 Ptr<Node> b = CreateObject<Node>();
102 Ptr<PointToPointNetDevice> devA = CreateObject<PointToPointNetDevice>();
103 Ptr<PointToPointNetDevice> devB = CreateObject<PointToPointNetDevice>();
104 Ptr<PointToPointChannel> channel = CreateObject<PointToPointChannel>();
105
106 devA->Attach(channel);
107 devA->SetAddress(Mac48Address::Allocate());
108 devA->SetQueue(CreateObject<DropTailQueue<Packet>>());
109 devB->Attach(channel);
110 devB->SetAddress(Mac48Address::Allocate());
111 devB->SetQueue(CreateObject<DropTailQueue<Packet>>());
112
113 a->AddDevice(devA);
114 b->AddDevice(devB);
115
116 devB->SetReceiveCallback(MakeCallback(&PointToPointTest::RxPacket, this));
117 uint8_t txBuffer[] = "\"Can you tell me where my country lies?\" \\ said the unifaun to his "
118 "true love's eyes. \\ \"It lies with me!\" cried the Queen of Maybe \\ - "
119 "for her merchandise, he traded in his prize.";
120 size_t txBufferSize = sizeof(txBuffer);
121
124 this,
125 devA,
126 txBuffer,
127 txBufferSize);
128
130
131 NS_TEST_EXPECT_MSG_EQ(m_recvdPacket->GetSize(), txBufferSize, "trivial");
132
133 uint8_t
134 rxBuffer[1500]; // As large as the P2P MTU size, assuming that the user didn't change it.
135
136 m_recvdPacket->CopyData(rxBuffer, txBufferSize);
137 NS_TEST_EXPECT_MSG_EQ(memcmp(rxBuffer, txBuffer, txBufferSize), 0, "trivial");
138
140}
141
142/**
143 * \brief TestSuite for PointToPoint module
144 */
146{
147 public:
148 /**
149 * \brief Constructor
150 */
152};
153
155 : TestSuite("devices-point-to-point", Type::UNIT)
156{
157 AddTestCase(new PointToPointTest, TestCase::Duration::QUICK);
158}
159
Test class for PointToPoint model.
void SendOnePacket(Ptr< PointToPointNetDevice > device, const uint8_t *buffer, uint32_t size)
Send one packet to the device specified.
PointToPointTest()
Create the test.
void DoRun() override
Run the test.
bool RxPacket(Ptr< NetDevice > dev, Ptr< const Packet > pkt, uint16_t mode, const Address &sender)
Callback function which sets the recvdPacket parameter.
Ptr< const Packet > m_recvdPacket
received packet
TestSuite for PointToPoint module.
PointToPointTestSuite()
Constructor.
a polymophic address class
Definition: address.h:101
A FIFO packet queue that drops tail-end packets on overflow.
static Mac48Address Allocate()
Allocate a new Mac48Address.
uint32_t GetSize() const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:861
uint32_t CopyData(uint8_t *buffer, uint32_t size) const
Copy the packet contents to a byte buffer.
Definition: packet.cc:400
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:571
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
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition: object.h:630
#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
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
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
static PointToPointTestSuite g_pointToPointTestSuite
The testsuite.