A Discrete-Event Network Simulator
API
sixlowpan-hc1-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 
49 {
51 
58  void DoSendData (Ptr<Socket> socket, std::string to);
59 
66  void SendData (Ptr<Socket> socket, std::string to);
67 
68 public:
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 
109 void
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 
120 void
122 {
123  m_receivedPacket = Create<Packet> ();
124  Simulator::ScheduleWithContext (socket->GetNode ()->GetId (), Seconds (0),
125  &SixlowpanHc1ImplTest::DoSendData, this, socket, to);
126  Simulator::Run ();
127 }
128 
129 void
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);
139  Ptr<SimpleNetDevice> rxDev;
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  rxSix->SetAttribute ("Rfc6282", BooleanValue (false) );
148  rxNode->AddDevice (rxSix);
149  rxSix->SetNetDevice (rxDev);
150 
151  Ptr<Ipv6> ipv6 = rxNode->GetObject<Ipv6> ();
152  ipv6->AddInterface (rxDev);
153  uint32_t netdev_idx = ipv6->AddInterface (rxSix);
154  Ipv6InterfaceAddress ipv6Addr = Ipv6InterfaceAddress (Ipv6Address ("2001:0100::1"), Ipv6Prefix (64));
155  ipv6->AddAddress (netdev_idx, ipv6Addr);
156  ipv6->SetUp (netdev_idx);
157  }
158 
159  // Sender Node
160  Ptr<Node> txNode = CreateObject<Node> ();
161  internet.Install (txNode);
162  Ptr<SimpleNetDevice> txDev;
163  {
164  txDev = CreateObject<SimpleNetDevice> ();
165  txDev->SetAddress (Mac48Address::ConvertFrom (Mac48Address::Allocate ()));
166  txNode->AddDevice (txDev);
167 
168  Ptr<SixLowPanNetDevice> txSix = CreateObject<SixLowPanNetDevice> ();
169  txSix->SetAttribute ("ForceEtherType", BooleanValue (true) );
170  txSix->SetAttribute ("Rfc6282", BooleanValue (false) );
171  txNode->AddDevice (txSix);
172  txSix->SetNetDevice (txDev);
173 
174  Ptr<Ipv6> ipv6 = txNode->GetObject<Ipv6> ();
175  ipv6->AddInterface (txDev);
176  uint32_t netdev_idx = ipv6->AddInterface (txSix);
177  Ipv6InterfaceAddress ipv6Addr = Ipv6InterfaceAddress (Ipv6Address ("2001:0100::2"), Ipv6Prefix (64));
178  ipv6->AddAddress (netdev_idx, ipv6Addr);
179  ipv6->SetUp (netdev_idx);
180  }
181 
182  // link the two nodes
183  Ptr<SimpleChannel> channel1 = CreateObject<SimpleChannel> ();
184  rxDev->SetChannel (channel1);
185  txDev->SetChannel (channel1);
186 
187  // Create the UDP sockets
188  Ptr<SocketFactory> rxSocketFactory = rxNode->GetObject<UdpSocketFactory> ();
189  Ptr<Socket> rxSocket = rxSocketFactory->CreateSocket ();
190  NS_TEST_EXPECT_MSG_EQ (rxSocket->Bind (Inet6SocketAddress (Ipv6Address ("2001:0100::1"), 1234)), 0, "trivial");
191  rxSocket->SetRecvCallback (MakeCallback (&SixlowpanHc1ImplTest::ReceivePkt, this));
192 
193  Ptr<SocketFactory> txSocketFactory = txNode->GetObject<UdpSocketFactory> ();
194  Ptr<Socket> txSocket = txSocketFactory->CreateSocket ();
195  txSocket->SetAllowBroadcast (true);
196  // ------ Now the tests ------------
197 
198  // Unicast test
199  SendData (txSocket, "2001:0100::1");
200  NS_TEST_EXPECT_MSG_EQ (m_receivedPacket->GetSize (), 180, "trivial");
201  uint8_t rxBuffer [180];
202  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.";
203  m_receivedPacket->CopyData (rxBuffer, 180);
204  NS_TEST_EXPECT_MSG_EQ (memcmp (rxBuffer, txBuffer, 180), 0, "trivial");
205 
207 
208  Simulator::Destroy ();
209 
210 }
211 
212 
220 {
221 public:
223 private:
224 };
225 
227  : TestSuite ("sixlowpan-hc1", UNIT)
228 {
229  AddTestCase (new SixlowpanHc1ImplTest (), TestCase::QUICK);
230 }
231 
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.
void DoSendData(Ptr< Socket > socket, std::string to)
Send data function.
virtual void DoRun(void)
Implementation to actually run this TestCase.
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
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
static SixlowpanHc1TestSuite g_sixlowpanHc1TestSuite
Static variable for test initialization.
encapsulates test code
Definition: test.h:1155
void SetNetDevice(Ptr< NetDevice > device)
Setup SixLowPan to be a proxy for the specified NetDevice.
a polymophic address class
Definition: address.h:90
6LoWPAN HC1 TestSuite
#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
void SendData(Ptr< Socket > socket, std::string to)
Send data function.
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.
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.
void ReceivePacket(Ptr< Socket > socket, Ptr< Packet > packet, const Address &from)
Packet receive function.
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.
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
void ReceivePkt(Ptr< Socket > socket)
Packet receive function.
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 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
Ptr< Packet > m_receivedPacket
received packet
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.