A Discrete-Event Network Simulator
API
point-to-point-test.cc
Go to the documentation of this file.
1/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2009 INRIA
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: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19 */
20
21#include "ns3/test.h"
22#include "ns3/drop-tail-queue.h"
23#include "ns3/simulator.h"
24#include "ns3/point-to-point-net-device.h"
25#include "ns3/point-to-point-channel.h"
26#include "ns3/net-device-queue-interface.h"
27
28#include <string>
29
30using namespace ns3;
31
39{
40public:
45
49 virtual void DoRun (void);
50
51private:
52
61 void SendOnePacket (Ptr<PointToPointNetDevice> device, uint8_t const *buffer, uint32_t size);
72 bool RxPacket (Ptr<NetDevice> dev, Ptr<const Packet> pkt, uint16_t mode, const Address &sender);
73};
74
76 : TestCase ("PointToPoint")
77{
78}
79
80void
82{
83 Ptr<Packet> p = Create<Packet> (buffer, size);
84 device->Send (p, device->GetBroadcast (), 0x800);
85}
86
87bool
89{
90 m_recvdPacket = pkt;
91 return true;
92}
93
94
95void
97{
98 Ptr<Node> a = CreateObject<Node> ();
99 Ptr<Node> b = CreateObject<Node> ();
100 Ptr<PointToPointNetDevice> devA = CreateObject<PointToPointNetDevice> ();
101 Ptr<PointToPointNetDevice> devB = CreateObject<PointToPointNetDevice> ();
102 Ptr<PointToPointChannel> channel = CreateObject<PointToPointChannel> ();
103
104 devA->Attach (channel);
105 devA->SetAddress (Mac48Address::Allocate ());
106 devA->SetQueue (CreateObject<DropTailQueue<Packet> > ());
107 devB->Attach (channel);
108 devB->SetAddress (Mac48Address::Allocate ());
109 devB->SetQueue (CreateObject<DropTailQueue<Packet> > ());
110
111 a->AddDevice (devA);
112 b->AddDevice (devB);
113
114 devB->SetReceiveCallback (MakeCallback (&PointToPointTest::RxPacket,
115 this));
116 uint8_t txBuffer [] = "\"Can you tell me where my country lies?\" \\ said the unifaun to his true love's eyes. \\ \"It lies with me!\" cried the Queen of Maybe \\ - for her merchandise, he traded in his prize.";
117 size_t txBufferSize = sizeof(txBuffer);
118
119 Simulator::Schedule (Seconds (1.0), &PointToPointTest::SendOnePacket, this, devA, txBuffer, txBufferSize);
120
121 Simulator::Run ();
122
123 NS_TEST_EXPECT_MSG_EQ (m_recvdPacket->GetSize (), txBufferSize, "trivial");
124
125 uint8_t rxBuffer [1500]; // As large as the P2P MTU size, assuming that the user didn't change it.
126
127 m_recvdPacket->CopyData (rxBuffer, txBufferSize);
128 NS_TEST_EXPECT_MSG_EQ (memcmp (rxBuffer, txBuffer, txBufferSize), 0, "trivial");
129
130 Simulator::Destroy ();
131}
132
137{
138public:
143};
144
146 : TestSuite ("devices-point-to-point", UNIT)
147{
148 AddTestCase (new PointToPointTest, TestCase::QUICK);
149}
150
Test class for PointToPoint model.
PointToPointTest()
Create the test.
void SendOnePacket(Ptr< PointToPointNetDevice > device, uint8_t const *buffer, uint32_t size)
Send one packet to the device specified.
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
virtual void DoRun(void)
Run the test.
TestSuite for PointToPoint module.
PointToPointTestSuite()
Constructor.
a polymophic address class
Definition: address.h:91
uint32_t AddDevice(Ptr< NetDevice > device)
Associate a NetDevice to this node.
Definition: node.cc:130
uint32_t CopyData(uint8_t *buffer, uint32_t size) const
Copy the packet contents to a byte buffer.
Definition: packet.cc:378
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:856
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
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition: object.h:576
#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
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1244
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:92
static PointToPointTestSuite g_pointToPointTestSuite
The testsuite.