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 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
7 */
8
9#include "ns3/drop-tail-queue.h"
10#include "ns3/net-device-queue-interface.h"
11#include "ns3/point-to-point-channel.h"
12#include "ns3/point-to-point-net-device.h"
13#include "ns3/simulator.h"
14#include "ns3/test.h"
15
16#include <string>
17
18using namespace ns3;
19
20/**
21 * \brief Test class for PointToPoint model
22 *
23 * It tries to send one packet from one NetDevice to another, over a
24 * PointToPointChannel.
25 */
27{
28 public:
29 /**
30 * \brief Create the test
31 */
33
34 /**
35 * \brief Run the test
36 */
37 void DoRun() override;
38
39 private:
40 Ptr<const Packet> m_recvdPacket; //!< received packet
41 /**
42 * \brief Send one packet to the device specified
43 *
44 * \param device NetDevice to send to.
45 * \param buffer Payload content of the packet.
46 * \param size Size of the payload.
47 */
48 void SendOnePacket(Ptr<PointToPointNetDevice> device, const uint8_t* buffer, uint32_t size);
49 /**
50 * \brief Callback function which sets the recvdPacket parameter
51 *
52 * \param dev The receiving device.
53 * \param pkt The received packet.
54 * \param mode The protocol mode used.
55 * \param sender The sender address.
56 *
57 * \return A boolean indicating packet handled properly.
58 */
59 bool RxPacket(Ptr<NetDevice> dev, Ptr<const Packet> pkt, uint16_t mode, const Address& sender);
60};
61
63 : TestCase("PointToPoint")
64{
65}
66
67void
69 const uint8_t* buffer,
70 uint32_t size)
71{
72 Ptr<Packet> p = Create<Packet>(buffer, size);
73 device->Send(p, device->GetBroadcast(), 0x800);
74}
75
76bool
79 uint16_t mode,
80 const Address& sender)
81{
82 m_recvdPacket = pkt;
83 return true;
84}
85
86void
88{
94
95 devA->Attach(channel);
96 devA->SetAddress(Mac48Address::Allocate());
97 devA->SetQueue(CreateObject<DropTailQueue<Packet>>());
98 devB->Attach(channel);
99 devB->SetAddress(Mac48Address::Allocate());
100 devB->SetQueue(CreateObject<DropTailQueue<Packet>>());
101
102 a->AddDevice(devA);
103 b->AddDevice(devB);
104
105 devB->SetReceiveCallback(MakeCallback(&PointToPointTest::RxPacket, this));
106 uint8_t txBuffer[] = "\"Can you tell me where my country lies?\" \\ said the unifaun to his "
107 "true love's eyes. \\ \"It lies with me!\" cried the Queen of Maybe \\ - "
108 "for her merchandise, he traded in his prize.";
109 size_t txBufferSize = sizeof(txBuffer);
110
113 this,
114 devA,
115 txBuffer,
116 txBufferSize);
117
119
120 NS_TEST_EXPECT_MSG_EQ(m_recvdPacket->GetSize(), txBufferSize, "trivial");
121
122 uint8_t
123 rxBuffer[1500]; // As large as the P2P MTU size, assuming that the user didn't change it.
124
125 m_recvdPacket->CopyData(rxBuffer, txBufferSize);
126 NS_TEST_EXPECT_MSG_EQ(memcmp(rxBuffer, txBuffer, txBufferSize), 0, "trivial");
127
129}
130
131/**
132 * \brief TestSuite for PointToPoint module
133 */
135{
136 public:
137 /**
138 * \brief Constructor
139 */
141};
142
144 : TestSuite("devices-point-to-point", Type::UNIT)
145{
146 AddTestCase(new PointToPointTest, TestCase::Duration::QUICK);
147}
148
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.
a polymophic address class
Definition address.h:90
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:850
uint32_t CopyData(uint8_t *buffer, uint32_t size) const
Copy the packet contents to a byte buffer.
Definition packet.cc:389
Smart pointer class similar to boost::intrusive_ptr.
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition simulator.h:560
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
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:436
#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
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
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 PointToPointTestSuite g_pointToPointTestSuite
The testsuite.