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/drop-tail-queue.h"
28 #include "ns3/socket.h"
29 #include "ns3/boolean.h"
30 
31 #include "ns3/log.h"
32 #include "ns3/node.h"
33 #include "ns3/inet6-socket-address.h"
34 #include "ns3/internet-stack-helper.h"
35 
36 #include "ns3/sixlowpan-net-device.h"
37 
38 #include <string>
39 #include <limits>
40 
41 using namespace ns3;
42 
44 {
46  void DoSendData (Ptr<Socket> socket, std::string to);
47  void SendData (Ptr<Socket> socket, std::string to);
48 
49 public:
50  virtual void DoRun (void);
52 
53  void ReceivePacket (Ptr<Socket> socket, Ptr<Packet> packet, const Address &from);
54  void ReceivePkt (Ptr<Socket> socket);
55 };
56 
58  : TestCase ("Sixlowpan implementation")
59 {
60 }
61 
63 {
64  m_receivedPacket = packet;
65 }
66 
68 {
69  uint32_t availableData;
70  availableData = socket->GetRxAvailable ();
72  NS_ASSERT (availableData == m_receivedPacket->GetSize ());
73  //cast availableData to void, to suppress 'availableData' set but not used
74  //compiler warning
75  (void) availableData;
76 }
77 
78 void
80 {
81  Address realTo = Inet6SocketAddress (Ipv6Address (to.c_str ()), 1234);
82  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.";
83 
84  Ptr<Packet> packet = Create<Packet> (buffer, 180);
85  NS_TEST_EXPECT_MSG_EQ (socket->SendTo (packet, 0, realTo),
86  180, "200");
87 }
88 
89 void
91 {
92  m_receivedPacket = Create<Packet> ();
93  Simulator::ScheduleWithContext (socket->GetNode ()->GetId (), Seconds (0),
94  &SixlowpanHc1ImplTest::DoSendData, this, socket, to);
95  Simulator::Run ();
96 }
97 
98 void
100 {
101  // Create topology
102  InternetStackHelper internet;
103  internet.SetIpv4StackInstall (false);
104 
105  // Receiver Node
106  Ptr<Node> rxNode = CreateObject<Node> ();
107  internet.Install (rxNode);
108  Ptr<SimpleNetDevice> rxDev;
109  { // first interface
110  rxDev = CreateObject<SimpleNetDevice> ();
111  rxDev->SetAddress (Mac48Address::ConvertFrom (Mac48Address::Allocate ()));
112  rxNode->AddDevice (rxDev);
113 
114  Ptr<SixLowPanNetDevice> rxSix = CreateObject<SixLowPanNetDevice> ();
115  rxSix->SetAttribute ("ForceEtherType", BooleanValue (true) );
116  rxSix->SetAttribute ("Rfc6282", BooleanValue (false) );
117  rxNode->AddDevice (rxSix);
118  rxSix->SetNetDevice (rxDev);
119 
120  Ptr<Ipv6> ipv6 = rxNode->GetObject<Ipv6> ();
121  ipv6->AddInterface (rxDev);
122  uint32_t netdev_idx = ipv6->AddInterface (rxSix);
123  Ipv6InterfaceAddress ipv6Addr = Ipv6InterfaceAddress (Ipv6Address ("2001:0100::1"), Ipv6Prefix (64));
124  ipv6->AddAddress (netdev_idx, ipv6Addr);
125  ipv6->SetUp (netdev_idx);
126  }
127 
128  // Sender Node
129  Ptr<Node> txNode = CreateObject<Node> ();
130  internet.Install (txNode);
131  Ptr<SimpleNetDevice> txDev;
132  {
133  txDev = CreateObject<SimpleNetDevice> ();
134  txDev->SetAddress (Mac48Address::ConvertFrom (Mac48Address::Allocate ()));
135  txNode->AddDevice (txDev);
136 
137  Ptr<SixLowPanNetDevice> txSix = CreateObject<SixLowPanNetDevice> ();
138  txSix->SetAttribute ("ForceEtherType", BooleanValue (true) );
139  txSix->SetAttribute ("Rfc6282", BooleanValue (false) );
140  txNode->AddDevice (txSix);
141  txSix->SetNetDevice (txDev);
142 
143  Ptr<Ipv6> ipv6 = txNode->GetObject<Ipv6> ();
144  ipv6->AddInterface (txDev);
145  uint32_t netdev_idx = ipv6->AddInterface (txSix);
146  Ipv6InterfaceAddress ipv6Addr = Ipv6InterfaceAddress (Ipv6Address ("2001:0100::2"), Ipv6Prefix (64));
147  ipv6->AddAddress (netdev_idx, ipv6Addr);
148  ipv6->SetUp (netdev_idx);
149  }
150 
151  // link the two nodes
152  Ptr<SimpleChannel> channel1 = CreateObject<SimpleChannel> ();
153  rxDev->SetChannel (channel1);
154  txDev->SetChannel (channel1);
155 
156  // Create the UDP sockets
157  Ptr<SocketFactory> rxSocketFactory = rxNode->GetObject<UdpSocketFactory> ();
158  Ptr<Socket> rxSocket = rxSocketFactory->CreateSocket ();
159  NS_TEST_EXPECT_MSG_EQ (rxSocket->Bind (Inet6SocketAddress (Ipv6Address ("2001:0100::1"), 1234)), 0, "trivial");
160  rxSocket->SetRecvCallback (MakeCallback (&SixlowpanHc1ImplTest::ReceivePkt, this));
161 
162  Ptr<SocketFactory> txSocketFactory = txNode->GetObject<UdpSocketFactory> ();
163  Ptr<Socket> txSocket = txSocketFactory->CreateSocket ();
164  txSocket->SetAllowBroadcast (true);
165  // ------ Now the tests ------------
166 
167  // Unicast test
168  SendData (txSocket, "2001:0100::1");
169  NS_TEST_EXPECT_MSG_EQ (m_receivedPacket->GetSize (), 180, "trivial");
170  uint8_t rxBuffer [180];
171  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.";
172  m_receivedPacket->CopyData (rxBuffer, 180);
173  NS_TEST_EXPECT_MSG_EQ (memcmp (rxBuffer, txBuffer, 180), 0, "trivial");
174 
176 
177  Simulator::Destroy ();
178 
179 }
180 
181 
182 //-----------------------------------------------------------------------------
184 {
185 public:
186  SixlowpanHc1TestSuite () : TestSuite ("sixlowpan-hc1", UNIT)
187  {
188  AddTestCase (new SixlowpanHc1ImplTest, TestCase::QUICK);
189  }
AttributeValue implementation for Boolean.
Definition: boolean.h:34
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:462
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)
virtual void DoRun(void)
Implementation to actually run this TestCase.
A suite of tests to run.
Definition: test.h:1333
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:278
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:792
virtual Ptr< Socket > CreateSocket(void)=0
encapsulates test code
Definition: test.h:1147
This test suite implements a Unit Test.
Definition: test.h:1343
void SetNetDevice(Ptr< NetDevice > device)
Setup SixLowPan to be a proxy for the specified NetDevice.
a polymophic address class
Definition: address.h:90
#define max(a, b)
Definition: 80211b.c:45
void AddTestCase(TestCase *testCase, enum TestDuration duration)
Add an individual child TestCase to this test suite.
Definition: test.cc:298
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
SixlowpanHc1TestSuite g_sixlowpanHc1TestSuite
void SendData(Ptr< Socket > socket, std::string to)
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.
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
virtual uint32_t AddInterface(Ptr< NetDevice > device)=0
Add a NetDevice interface.
Describes an IPv6 address.
Definition: ipv6-address.h:48
uint32_t AddDevice(Ptr< NetDevice > device)
Associate a NetDevice to this node.
Definition: node.cc:128
uint32_t GetId(void) const
Definition: node.cc:107
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)
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:895
void RemoveAllByteTags(void)
Remove all byte tags stored in this packet.
Definition: packet.cc:349
Describes an IPv6 prefix.
Definition: ipv6-address.h:394
void ReceivePkt(Ptr< Socket > socket)
virtual bool AddAddress(uint32_t interface, Ipv6InterfaceAddress address)=0
Add an address on the specified IPv6 interface.
uint32_t CopyData(uint8_t *buffer, uint32_t size) const
Copy the packet contents to a byte buffer.
Definition: packet.cc:356
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:191
Ptr< Packet > m_receivedPacket
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.