A Discrete-Event Network Simulator
API
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 
29 using namespace ns3;
30 
37 class PointToPointTest : public TestCase
38 {
39  public:
44 
48  void DoRun() override;
49 
50  private:
59  void SendOnePacket(Ptr<PointToPointNetDevice> device, const uint8_t* buffer, uint32_t size);
70  bool RxPacket(Ptr<NetDevice> dev, Ptr<const Packet> pkt, uint16_t mode, const Address& sender);
71 };
72 
74  : TestCase("PointToPoint")
75 {
76 }
77 
78 void
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 
87 bool
90  uint16_t mode,
91  const Address& sender)
92 {
93  m_recvdPacket = pkt;
94  return true;
95 }
96 
97 void
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 
122  Simulator::Schedule(Seconds(1.0),
124  this,
125  devA,
126  txBuffer,
127  txBufferSize);
128 
129  Simulator::Run();
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 
139  Simulator::Destroy();
140 }
141 
146 {
147  public:
152 };
153 
155  : TestSuite("devices-point-to-point", UNIT)
156 {
157  AddTestCase(new PointToPointTest, TestCase::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.
uint32_t AddDevice(Ptr< NetDevice > device)
Associate a NetDevice to this node.
Definition: node.cc:138
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
encapsulates test code
Definition: test.h:1060
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:301
A suite of tests to run.
Definition: test.h:1256
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition: object.h:579
#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:251
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
void SendOnePacket(Ptr< LrWpanPhy > sender, Ptr< LrWpanPhy > receiver)
Send one packet.
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
channel
Definition: third.py:88
static PointToPointTestSuite g_pointToPointTestSuite
The testsuite.