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 
48 {
50 
57  void DoSendData (Ptr<Socket> socket, std::string to);
58 
65  void SendData (Ptr<Socket> socket, std::string to);
66 
67 public:
68  virtual void DoRun (void);
70 
78  void ReceivePacket (Ptr<Socket> socket, Ptr<Packet> packet, const Address &from);
84  void ReceivePkt (Ptr<Socket> socket);
85 };
86 
88  : TestCase ("Sixlowpan implementation")
89 {
90 }
91 
93 {
94  m_receivedPacket = packet;
95 }
96 
98 {
99  uint32_t availableData;
100  availableData = socket->GetRxAvailable ();
102  NS_ASSERT (availableData == m_receivedPacket->GetSize ());
103  //cast availableData to void, to suppress 'availableData' set but not used
104  //compiler warning
105  (void) availableData;
106 }
107 
108 void
110 {
111  Address realTo = Inet6SocketAddress (Ipv6Address (to.c_str ()), 1234);
112  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.";
113 
114  Ptr<Packet> packet = Create<Packet> (buffer, 180);
115  NS_TEST_EXPECT_MSG_EQ (socket->SendTo (packet, 0, realTo),
116  180, "200");
117 }
118 
119 void
121 {
122  m_receivedPacket = Create<Packet> ();
123  Simulator::ScheduleWithContext (socket->GetNode ()->GetId (), Seconds (0),
124  &SixlowpanHc1ImplTest::DoSendData, this, socket, to);
125  Simulator::Run ();
126 }
127 
128 void
130 {
131  // Create topology
132  InternetStackHelper internet;
133  internet.SetIpv4StackInstall (false);
134 
135  // Receiver Node
136  Ptr<Node> rxNode = CreateObject<Node> ();
137  internet.Install (rxNode);
138  Ptr<SimpleNetDevice> rxDev;
139  { // first interface
140  rxDev = CreateObject<SimpleNetDevice> ();
141  rxDev->SetAddress (Mac48Address::ConvertFrom (Mac48Address::Allocate ()));
142  rxNode->AddDevice (rxDev);
143 
144  Ptr<SixLowPanNetDevice> rxSix = CreateObject<SixLowPanNetDevice> ();
145  rxSix->SetAttribute ("ForceEtherType", BooleanValue (true) );
146  rxSix->SetAttribute ("Rfc6282", BooleanValue (false) );
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);
161  Ptr<SimpleNetDevice> txDev;
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  txSix->SetAttribute ("Rfc6282", BooleanValue (false) );
170  txNode->AddDevice (txSix);
171  txSix->SetNetDevice (txDev);
172 
173  Ptr<Ipv6> ipv6 = txNode->GetObject<Ipv6> ();
174  ipv6->AddInterface (txDev);
175  uint32_t netdev_idx = ipv6->AddInterface (txSix);
176  Ipv6InterfaceAddress ipv6Addr = Ipv6InterfaceAddress (Ipv6Address ("2001:0100::2"), Ipv6Prefix (64));
177  ipv6->AddAddress (netdev_idx, ipv6Addr);
178  ipv6->SetUp (netdev_idx);
179  }
180 
181  // link the two nodes
182  Ptr<SimpleChannel> channel1 = CreateObject<SimpleChannel> ();
183  rxDev->SetChannel (channel1);
184  txDev->SetChannel (channel1);
185 
186  // Create the UDP sockets
187  Ptr<SocketFactory> rxSocketFactory = rxNode->GetObject<UdpSocketFactory> ();
188  Ptr<Socket> rxSocket = rxSocketFactory->CreateSocket ();
189  NS_TEST_EXPECT_MSG_EQ (rxSocket->Bind (Inet6SocketAddress (Ipv6Address ("2001:0100::1"), 1234)), 0, "trivial");
190  rxSocket->SetRecvCallback (MakeCallback (&SixlowpanHc1ImplTest::ReceivePkt, this));
191 
192  Ptr<SocketFactory> txSocketFactory = txNode->GetObject<UdpSocketFactory> ();
193  Ptr<Socket> txSocket = txSocketFactory->CreateSocket ();
194  txSocket->SetAllowBroadcast (true);
195  // ------ Now the tests ------------
196 
197  // Unicast test
198  SendData (txSocket, "2001:0100::1");
199  NS_TEST_EXPECT_MSG_EQ (m_receivedPacket->GetSize (), 180, "trivial");
200  uint8_t rxBuffer [180];
201  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.";
202  m_receivedPacket->CopyData (rxBuffer, 180);
203  NS_TEST_EXPECT_MSG_EQ (memcmp (rxBuffer, txBuffer, 180), 0, "trivial");
204 
206 
207  Simulator::Destroy ();
208 
209 }
210 
211 
218 {
219 public:
221 private:
222 };
223 
225  : TestSuite ("sixlowpan-hc1", UNIT)
226 {
227  AddTestCase (new SixlowpanHc1ImplTest (), TestCase::QUICK);
228 }
229 
#define max(a, b)
Definition: 80211b.c:43
void ReceivePkt(Ptr< Socket > socket)
Packet receive function.
virtual void DoRun(void)
Implementation to actually run this TestCase.
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.
Ptr< Packet > m_receivedPacket
received packet
6LoWPAN HC1 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< Packet > Recv(uint32_t maxSize, uint32_t flags)=0
Read data from the socket.
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 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.
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
#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
static SixlowpanHc1TestSuite g_sixlowpanHc1TestSuite
Static variable for test initialization.