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 
40 using namespace ns3;
41 
55 {
57 
64  void DoSendData (Ptr<Socket> socket, std::string to);
65 
72  void SendData (Ptr<Socket> socket, std::string to);
73 
74 public:
75  virtual void DoRun (void);
77 
85  void ReceivePacket (Ptr<Socket> socket, Ptr<Packet> packet, const Address &from);
91  void ReceivePkt (Ptr<Socket> socket);
92 };
93 
95  : TestCase ("Sixlowpan implementation")
96 {
97 }
98 
100 {
101  m_receivedPacket = packet;
102 }
103 
105 {
106  uint32_t availableData;
107  availableData = socket->GetRxAvailable ();
109  NS_ASSERT (availableData == m_receivedPacket->GetSize ());
110  //cast availableData to void, to suppress 'availableData' set but not used
111  //compiler warning
112  (void) availableData;
113 }
114 
115 void
117 {
118  Address realTo = Inet6SocketAddress (Ipv6Address (to.c_str ()), 1234);
119  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.";
120 
121  Ptr<Packet> packet = Create<Packet> (buffer, 180);
122  NS_TEST_EXPECT_MSG_EQ (socket->SendTo (packet, 0, realTo),
123  180, "200");
124 }
125 
126 void
128 {
129  m_receivedPacket = Create<Packet> ();
130  Simulator::ScheduleWithContext (socket->GetNode ()->GetId (), Seconds (0),
131  &SixlowpanIphcImplTest::DoSendData, this, socket, to);
132  Simulator::Run ();
133 }
134 
135 void
137 {
138  // Create topology
139  InternetStackHelper internet;
140  internet.SetIpv4StackInstall (false);
141 
142  // Receiver Node
143  Ptr<Node> rxNode = CreateObject<Node> ();
144  internet.Install (rxNode);
145  Ptr<SimpleNetDevice> rxDev;
146  { // first interface
147  rxDev = CreateObject<SimpleNetDevice> ();
148  rxDev->SetAddress (Mac48Address::ConvertFrom (Mac48Address::Allocate ()));
149  rxNode->AddDevice (rxDev);
150 
151  Ptr<SixLowPanNetDevice> rxSix = CreateObject<SixLowPanNetDevice> ();
152  rxSix->SetAttribute ("ForceEtherType", BooleanValue (true) );
153  rxNode->AddDevice (rxSix);
154  rxSix->SetNetDevice (rxDev);
155 
156  Ptr<Ipv6> ipv6 = rxNode->GetObject<Ipv6> ();
157  ipv6->AddInterface (rxDev);
158  uint32_t netdev_idx = ipv6->AddInterface (rxSix);
159  Ipv6InterfaceAddress ipv6Addr = Ipv6InterfaceAddress (Ipv6Address ("2001:0100::1"), Ipv6Prefix (64));
160  ipv6->AddAddress (netdev_idx, ipv6Addr);
161  ipv6->SetUp (netdev_idx);
162  }
163 
164  // Sender Node
165  Ptr<Node> txNode = CreateObject<Node> ();
166  internet.Install (txNode);
167  Ptr<SimpleNetDevice> txDev;
168  {
169  txDev = CreateObject<SimpleNetDevice> ();
170  txDev->SetAddress (Mac48Address::ConvertFrom (Mac48Address::Allocate ()));
171  txNode->AddDevice (txDev);
172 
173  Ptr<SixLowPanNetDevice> txSix = CreateObject<SixLowPanNetDevice> ();
174  txSix->SetAttribute ("ForceEtherType", BooleanValue (true) );
175  txNode->AddDevice (txSix);
176  txSix->SetNetDevice (txDev);
177 
178  Ptr<Ipv6> ipv6 = txNode->GetObject<Ipv6> ();
179  ipv6->AddInterface (txDev);
180  uint32_t netdev_idx = ipv6->AddInterface (txSix);
181  Ipv6InterfaceAddress ipv6Addr = Ipv6InterfaceAddress (Ipv6Address ("2001:0100::2"), Ipv6Prefix (64));
182  ipv6->AddAddress (netdev_idx, ipv6Addr);
183  ipv6->SetUp (netdev_idx);
184  }
185 
186  // link the two nodes
187  Ptr<SimpleChannel> channel1 = CreateObject<SimpleChannel> ();
188  rxDev->SetChannel (channel1);
189  txDev->SetChannel (channel1);
190 
191  // Create the UDP sockets
192  Ptr<SocketFactory> rxSocketFactory = rxNode->GetObject<UdpSocketFactory> ();
193  Ptr<Socket> rxSocket = rxSocketFactory->CreateSocket ();
194  NS_TEST_EXPECT_MSG_EQ (rxSocket->Bind (Inet6SocketAddress (Ipv6Address ("2001:0100::1"), 1234)), 0, "trivial");
195  rxSocket->SetRecvCallback (MakeCallback (&SixlowpanIphcImplTest::ReceivePkt, this));
196 
197  Ptr<SocketFactory> txSocketFactory = txNode->GetObject<UdpSocketFactory> ();
198  Ptr<Socket> txSocket = txSocketFactory->CreateSocket ();
199  txSocket->SetAllowBroadcast (true);
200  // ------ Now the tests ------------
201 
202  // Unicast test
203  SendData (txSocket, "2001:0100::1");
204  NS_TEST_EXPECT_MSG_EQ (m_receivedPacket->GetSize (), 180, "trivial");
205  uint8_t rxBuffer [180];
206  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.";
207  m_receivedPacket->CopyData (rxBuffer, 180);
208  NS_TEST_EXPECT_MSG_EQ (memcmp (rxBuffer, txBuffer, 180), 0, "trivial");
209 
211 
212  Simulator::Destroy ();
213 
214 }
215 
216 
224 {
225 public:
227 private:
228 };
229 
231  : TestSuite ("sixlowpan-iphc", UNIT)
232 {
233  AddTestCase (new SixlowpanIphcImplTest (), TestCase::QUICK);
234 }
235 
void SendData(Ptr< Socket > socket, std::string to)
Send data function.
void DoSendData(Ptr< Socket > socket, std::string to)
Send data function.
AttributeValue implementation for Boolean.
Definition: boolean.h:36
uint32_t GetId(void) const
Definition: node.cc:107
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:831
Access to the IPv6 forwarding table, interfaces, and configuration.
Definition: ipv6.h:81
virtual bool SetAllowBroadcast(bool allowBroadcast)=0
Configure whether broadcast datagram transmissions are allowed.
A suite of tests to run.
Definition: test.h:1342
void ReceivePacket(Ptr< Socket > socket)
#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
void ReceivePkt(Ptr< Socket > socket)
Packet receive function.
IPv6 address associated with an interface.
aggregate IP/TCP/UDP functionality to existing Nodes.
#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:285
virtual Ptr< Socket > CreateSocket(void)=0
encapsulates test code
Definition: test.h:1155
void SetNetDevice(Ptr< NetDevice > device)
Setup SixLowPan to be a proxy for the specified NetDevice.
virtual void DoRun(void)
Implementation to actually run this TestCase.
a polymophic address class
Definition: address.h:90
#define max(a, b)
Definition: 80211b.c:43
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:299
void SetIpv4StackInstall(bool enable)
Enable/disable IPv4 stack install.
An Inet6 address class.
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1489
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:459
Every class exported by the ns3 library is enclosed in the ns3 namespace.
virtual Ptr< Packet > Recv(uint32_t maxSize, uint32_t flags)=0
Read data from the socket.
static SixlowpanIphcTestSuite g_sixlowpanIphcTestSuite
Static variable for test initialization.
virtual uint32_t AddInterface(Ptr< NetDevice > device)=0
Add a NetDevice interface.
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:49
uint32_t AddDevice(Ptr< NetDevice > device)
Associate a NetDevice to this node.
Definition: node.cc:128
virtual Ptr< Node > GetNode(void) const =0
Return the node this socket is associated with.
uint32_t CopyData(uint8_t *buffer, uint32_t size) const
Copy the packet contents to a byte buffer.
Definition: packet.cc:378
virtual void SetAddress(Address address)
Set the address of this interface.
Ptr< Packet > m_receivedPacket
received packet
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1014
void RemoveAllByteTags(void)
Remove all byte tags stored in this packet.
Definition: packet.cc:371
Describes an IPv6 prefix.
Definition: ipv6-address.h:428
6LoWPAN IPHC TestSuite
virtual bool AddAddress(uint32_t interface, Ipv6InterfaceAddress address)=0
Add an address on the specified IPv6 interface.
virtual int SendTo(Ptr< Packet > p, uint32_t flags, const Address &toAddress)=0
Send data to a specified peer.
API to create UDP socket instances.
void ReceivePacket(Ptr< Socket > socket, Ptr< Packet > packet, const Address &from)
Packet receive function.
void SetChannel(Ptr< SimpleChannel > channel)
Attach a channel to this net device.
void SetAttribute(std::string name, const AttributeValue &value)
Set a single attribute, raising fatal errors if unsuccessful.
Definition: object-base.cc:185
virtual void SetUp(uint32_t interface)=0
Set the interface into the "up" state.
virtual uint32_t GetRxAvailable(void) const =0
Return number of bytes which can be returned from one or multiple calls to Recv.