A Discrete-Event Network Simulator
API
sixlowpan-iphc-test.cc
Go to the documentation of this file.
1/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2013 Universita' di Firenze, Italy
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/socket-factory.h"
23#include "ns3/udp-socket-factory.h"
24#include "ns3/simulator.h"
25#include "ns3/simple-channel.h"
26#include "ns3/simple-net-device.h"
27#include "ns3/socket.h"
28#include "ns3/boolean.h"
29
30#include "ns3/log.h"
31#include "ns3/node.h"
32#include "ns3/inet6-socket-address.h"
33#include "ns3/internet-stack-helper.h"
34
35#include "ns3/sixlowpan-net-device.h"
36
37#include <string>
38#include <limits>
39
40using namespace ns3;
41
42
49{
51
58 void DoSendData (Ptr<Socket> socket, std::string to);
59
66 void SendData (Ptr<Socket> socket, std::string to);
67
68public:
69 virtual void DoRun (void);
71
79 void ReceivePacket (Ptr<Socket> socket, Ptr<Packet> packet, const Address &from);
85 void ReceivePkt (Ptr<Socket> socket);
86};
87
89 : TestCase ("Sixlowpan implementation")
90{
91}
92
94{
95 m_receivedPacket = packet;
96}
97
99{
100 uint32_t availableData;
101 availableData = socket->GetRxAvailable ();
103 NS_ASSERT (availableData == m_receivedPacket->GetSize ());
104 //cast availableData to void, to suppress 'availableData' set but not used
105 //compiler warning
106 (void) availableData;
107}
108
109void
111{
112 Address realTo = Inet6SocketAddress (Ipv6Address (to.c_str ()), 1234);
113 uint8_t buffer [] = "\"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.";
114
115 Ptr<Packet> packet = Create<Packet> (buffer, 180);
116 NS_TEST_EXPECT_MSG_EQ (socket->SendTo (packet, 0, realTo),
117 180, "200");
118}
119
120void
122{
123 m_receivedPacket = Create<Packet> ();
124 Simulator::ScheduleWithContext (socket->GetNode ()->GetId (), Seconds (0),
125 &SixlowpanIphcImplTest::DoSendData, this, socket, to);
127}
128
129void
131{
132 // Create topology
133 InternetStackHelper internet;
134 internet.SetIpv4StackInstall (false);
135
136 // Receiver Node
137 Ptr<Node> rxNode = CreateObject<Node> ();
138 internet.Install (rxNode);
140 { // first interface
141 rxDev = CreateObject<SimpleNetDevice> ();
142 rxDev->SetAddress (Mac48Address::ConvertFrom (Mac48Address::Allocate ()));
143 rxNode->AddDevice (rxDev);
144
145 Ptr<SixLowPanNetDevice> rxSix = CreateObject<SixLowPanNetDevice> ();
146 rxSix->SetAttribute ("ForceEtherType", BooleanValue (true) );
147 rxNode->AddDevice (rxSix);
148 rxSix->SetNetDevice (rxDev);
149
150 Ptr<Ipv6> ipv6 = rxNode->GetObject<Ipv6> ();
151 ipv6->AddInterface (rxDev);
152 uint32_t netdev_idx = ipv6->AddInterface (rxSix);
153 Ipv6InterfaceAddress ipv6Addr = Ipv6InterfaceAddress (Ipv6Address ("2001:0100::1"), Ipv6Prefix (64));
154 ipv6->AddAddress (netdev_idx, ipv6Addr);
155 ipv6->SetUp (netdev_idx);
156 }
157
158 // Sender Node
159 Ptr<Node> txNode = CreateObject<Node> ();
160 internet.Install (txNode);
162 {
163 txDev = CreateObject<SimpleNetDevice> ();
164 txDev->SetAddress (Mac48Address::ConvertFrom (Mac48Address::Allocate ()));
165 txNode->AddDevice (txDev);
166
167 Ptr<SixLowPanNetDevice> txSix = CreateObject<SixLowPanNetDevice> ();
168 txSix->SetAttribute ("ForceEtherType", BooleanValue (true) );
169 txNode->AddDevice (txSix);
170 txSix->SetNetDevice (txDev);
171
172 Ptr<Ipv6> ipv6 = txNode->GetObject<Ipv6> ();
173 ipv6->AddInterface (txDev);
174 uint32_t netdev_idx = ipv6->AddInterface (txSix);
175 Ipv6InterfaceAddress ipv6Addr = Ipv6InterfaceAddress (Ipv6Address ("2001:0100::2"), Ipv6Prefix (64));
176 ipv6->AddAddress (netdev_idx, ipv6Addr);
177 ipv6->SetUp (netdev_idx);
178 }
179
180 // link the two nodes
181 Ptr<SimpleChannel> channel1 = CreateObject<SimpleChannel> ();
182 rxDev->SetChannel (channel1);
183 txDev->SetChannel (channel1);
184
185 // Create the UDP sockets
186 Ptr<SocketFactory> rxSocketFactory = rxNode->GetObject<UdpSocketFactory> ();
187 Ptr<Socket> rxSocket = rxSocketFactory->CreateSocket ();
188 NS_TEST_EXPECT_MSG_EQ (rxSocket->Bind (Inet6SocketAddress (Ipv6Address ("2001:0100::1"), 1234)), 0, "trivial");
189 rxSocket->SetRecvCallback (MakeCallback (&SixlowpanIphcImplTest::ReceivePkt, this));
190
191 Ptr<SocketFactory> txSocketFactory = txNode->GetObject<UdpSocketFactory> ();
192 Ptr<Socket> txSocket = txSocketFactory->CreateSocket ();
193 txSocket->SetAllowBroadcast (true);
194 // ------ Now the tests ------------
195
196 // Unicast test
197 SendData (txSocket, "2001:0100::1");
198 NS_TEST_EXPECT_MSG_EQ (m_receivedPacket->GetSize (), 180, "trivial");
199 uint8_t rxBuffer [180];
200 uint8_t txBuffer [180] = "\"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.";
201 m_receivedPacket->CopyData (rxBuffer, 180);
202 NS_TEST_EXPECT_MSG_EQ (memcmp (rxBuffer, txBuffer, 180), 0, "trivial");
203
205
206 Simulator::Destroy ();
207
208}
209
210
217{
218public:
220private:
221};
222
224 : TestSuite ("sixlowpan-iphc", UNIT)
225{
226 AddTestCase (new SixlowpanIphcImplTest (), TestCase::QUICK);
227}
228
#define max(a, b)
Definition: 80211b.c:43
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.
Ptr< Packet > m_receivedPacket
received packet
void SendData(Ptr< Socket > socket, std::string to)
Send data function.
void DoSendData(Ptr< Socket > socket, std::string to)
Send data function.
void ReceivePacket(Ptr< Socket > socket, Ptr< Packet > packet, const Address &from)
Packet receive function.
virtual void DoRun(void)
Implementation to actually run this TestCase.
void ReceivePkt(Ptr< Socket > socket)
Packet receive function.
6LoWPAN IPHC TestSuite
a polymophic address class
Definition: address.h:91
AttributeValue implementation for Boolean.
Definition: boolean.h:37
An Inet6 address class.
aggregate IP/TCP/UDP functionality to existing Nodes.
void SetIpv4StackInstall(bool enable)
Enable/disable IPv4 stack install.
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
Describes an IPv6 address.
Definition: ipv6-address.h:50
Access to the IPv6 forwarding table, interfaces, and configuration.
Definition: ipv6.h:82
IPv6 address associated with an interface.
Describes an IPv6 prefix.
Definition: ipv6-address.h:456
uint32_t GetId(void) const
Definition: node.cc:109
uint32_t AddDevice(Ptr< NetDevice > device)
Associate a NetDevice to this node.
Definition: node.cc:130
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:470
void RemoveAllByteTags(void)
Remove all byte tags stored in this packet.
Definition: packet.cc:371
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
virtual Ptr< Node > GetNode(void) const =0
Return the node this socket is associated with.
virtual uint32_t GetRxAvailable(void) const =0
Return number of bytes which can be returned from one or multiple calls to Recv.
virtual Ptr< Packet > Recv(uint32_t maxSize, uint32_t flags)=0
Read data from the socket.
virtual int SendTo(Ptr< Packet > p, uint32_t flags, const Address &toAddress)=0
Send data to a specified peer.
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
API to create UDP socket instances.
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition: assert.h:67
#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:1245
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
static SixlowpanIphcTestSuite g_sixlowpanIphcTestSuite
Static variable for test initialization.