A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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/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 
35 #include "ns3/ipv6-l3-protocol.h"
36 #include "ns3/icmpv6-l4-protocol.h"
37 #include "ns3/udp-l4-protocol.h"
38 #include "ns3/ipv6-list-routing.h"
39 #include "ns3/ipv6-static-routing.h"
40 
41 #include "ns3/sixlowpan-net-device.h"
42 
43 #include <string>
44 #include <limits>
45 
46 using namespace ns3;
47 
48 static void
50 {
51  //IPV6
52  Ptr<Ipv6L3Protocol> ipv6 = CreateObject<Ipv6L3Protocol> ();
53  //Routing for Ipv6
54  Ptr<Ipv6ListRouting> ipv6Routing = CreateObject<Ipv6ListRouting> ();
55  ipv6->SetRoutingProtocol (ipv6Routing);
56  Ptr<Ipv6StaticRouting> ipv6staticRouting = CreateObject<Ipv6StaticRouting> ();
57  ipv6Routing->AddRoutingProtocol (ipv6staticRouting, 0);
58  node->AggregateObject (ipv6);
59  //ICMP
60  Ptr<Icmpv6L4Protocol> icmp = CreateObject<Icmpv6L4Protocol> ();
61  node->AggregateObject (icmp);
62  //Ipv6 Extensions
63  ipv6->RegisterExtensions ();
64  ipv6->RegisterOptions ();
65  //UDP
66  Ptr<UdpL4Protocol> udp = CreateObject<UdpL4Protocol> ();
67  node->AggregateObject (udp);
68 }
69 
70 
72 {
74  void DoSendData (Ptr<Socket> socket, std::string to);
75  void SendData (Ptr<Socket> socket, std::string to);
76 
77 public:
78  virtual void DoRun (void);
80 
81  void ReceivePacket (Ptr<Socket> socket, Ptr<Packet> packet, const Address &from);
82  void ReceivePkt (Ptr<Socket> socket);
83 };
84 
86  : TestCase ("Sixlowpan implementation")
87 {
88 }
89 
91 {
92  m_receivedPacket = packet;
93 }
94 
96 {
97  uint32_t availableData;
98  availableData = socket->GetRxAvailable ();
99  m_receivedPacket = socket->Recv (std::numeric_limits<uint32_t>::max (), 0);
100  NS_ASSERT (availableData == m_receivedPacket->GetSize ());
101  //cast availableData to void, to suppress 'availableData' set but not used
102  //compiler warning
103  (void) availableData;
104 }
105 
106 void
108 {
109  Address realTo = Inet6SocketAddress (Ipv6Address (to.c_str ()), 1234);
110  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.";
111 
112  Ptr<Packet> packet = Create<Packet> (buffer, 180);
113  NS_TEST_EXPECT_MSG_EQ (socket->SendTo (packet, 0, realTo),
114  180, "200");
115 }
116 
117 void
119 {
120  m_receivedPacket = Create<Packet> ();
121  Simulator::ScheduleWithContext (socket->GetNode ()->GetId (), Seconds (0),
122  &SixlowpanIphcImplTest::DoSendData, this, socket, to);
123  Simulator::Run ();
124 }
125 
126 void
128 {
129  // Create topology
130 
131  // Receiver Node
132  Ptr<Node> rxNode = CreateObject<Node> ();
133  AddInternetStack6 (rxNode);
134  Ptr<SimpleNetDevice> rxDev;
135  { // first interface
136  rxDev = CreateObject<SimpleNetDevice> ();
137  rxDev->SetAddress (Mac48Address::ConvertFrom (Mac48Address::Allocate ()));
138  rxNode->AddDevice (rxDev);
139 
140  Ptr<SixLowPanNetDevice> rxSix = CreateObject<SixLowPanNetDevice> ();
141  rxSix->SetAttribute ("ForceEtherType", BooleanValue (true) );
142  rxNode->AddDevice (rxSix);
143  rxSix->SetNetDevice (rxDev);
144 
145  Ptr<Ipv6> ipv6 = rxNode->GetObject<Ipv6> ();
146  ipv6->AddInterface (rxDev);
147  uint32_t netdev_idx = ipv6->AddInterface (rxSix);
148  Ipv6InterfaceAddress ipv6Addr = Ipv6InterfaceAddress (Ipv6Address ("2001:0100::1"), Ipv6Prefix (64));
149  ipv6->AddAddress (netdev_idx, ipv6Addr);
150  ipv6->SetUp (netdev_idx);
151  }
152 
153  // Sender Node
154  Ptr<Node> txNode = CreateObject<Node> ();
155  AddInternetStack6 (txNode);
156  Ptr<SimpleNetDevice> txDev;
157  {
158  txDev = CreateObject<SimpleNetDevice> ();
159  txDev->SetAddress (Mac48Address::ConvertFrom (Mac48Address::Allocate ()));
160  txNode->AddDevice (txDev);
161 
162  Ptr<SixLowPanNetDevice> txSix = CreateObject<SixLowPanNetDevice> ();
163  txSix->SetAttribute ("ForceEtherType", BooleanValue (true) );
164  txNode->AddDevice (txSix);
165  txSix->SetNetDevice (txDev);
166 
167  Ptr<Ipv6> ipv6 = txNode->GetObject<Ipv6> ();
168  ipv6->AddInterface (txDev);
169  uint32_t netdev_idx = ipv6->AddInterface (txSix);
170  Ipv6InterfaceAddress ipv6Addr = Ipv6InterfaceAddress (Ipv6Address ("2001:0100::2"), Ipv6Prefix (64));
171  ipv6->AddAddress (netdev_idx, ipv6Addr);
172  ipv6->SetUp (netdev_idx);
173  }
174 
175  // link the two nodes
176  Ptr<SimpleChannel> channel1 = CreateObject<SimpleChannel> ();
177  rxDev->SetChannel (channel1);
178  txDev->SetChannel (channel1);
179 
180  // Create the UDP sockets
181  Ptr<SocketFactory> rxSocketFactory = rxNode->GetObject<UdpSocketFactory> ();
182  Ptr<Socket> rxSocket = rxSocketFactory->CreateSocket ();
183  NS_TEST_EXPECT_MSG_EQ (rxSocket->Bind (Inet6SocketAddress (Ipv6Address ("2001:0100::1"), 1234)), 0, "trivial");
184  rxSocket->SetRecvCallback (MakeCallback (&SixlowpanIphcImplTest::ReceivePkt, this));
185 
186  Ptr<SocketFactory> txSocketFactory = txNode->GetObject<UdpSocketFactory> ();
187  Ptr<Socket> txSocket = txSocketFactory->CreateSocket ();
188  txSocket->SetAllowBroadcast (true);
189  // ------ Now the tests ------------
190 
191  // Unicast test
192  SendData (txSocket, "2001:0100::1");
193  NS_TEST_EXPECT_MSG_EQ (m_receivedPacket->GetSize (), 180, "trivial");
194  uint8_t rxBuffer [180];
195  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.";
196  m_receivedPacket->CopyData (rxBuffer, 180);
197  NS_TEST_EXPECT_MSG_EQ (memcmp (rxBuffer, txBuffer, 180), 0, "trivial");
198 
200 
201  Simulator::Destroy ();
202 
203 }
204 
205 
206 //-----------------------------------------------------------------------------
208 {
209 public:
210  SixlowpanIphcTestSuite () : TestSuite ("sixlowpan-iphc", UNIT)
211  {
212  AddTestCase (new SixlowpanIphcImplTest, TestCase::QUICK);
213  }
void SendData(Ptr< Socket > socket, std::string to)
void DoSendData(Ptr< Socket > socket, std::string to)
Hold a bool native type.
Definition: boolean.h:38
Access to the IPv6 forwarding table, interfaces, and configuration.
Definition: ipv6.h:80
virtual bool SetAllowBroadcast(bool allowBroadcast)=0
Configure whether broadcast datagram transmissions are allowed.
A suite of tests to run.
Definition: test.h:1105
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:61
void ReceivePkt(Ptr< Socket > socket)
IPv6 address associated with an interface.
static void AddInternetStack6(Ptr< Node > node)
#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:265
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:744
virtual Ptr< Socket > CreateSocket(void)=0
encapsulates test code
Definition: test.h:929
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:86
An Inet6 address class.
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1242
void AggregateObject(Ptr< Object > other)
Definition: object.cc:242
virtual Ptr< Packet > Recv(uint32_t maxSize, uint32_t flags)=0
Read data from the socket.
void AddTestCase(TestCase *testCase) NS_DEPRECATED
Add an individual child TestCase case to this TestCase.
Definition: test.cc:184
SixlowpanIphcTestSuite g_sixlowpanIphcTestSuite
virtual uint32_t AddInterface(Ptr< NetDevice > device)=0
Add a NetDevice interface.
virtual void RegisterExtensions()
Register the IPv6 Extensions.
Describes an IPv6 address.
Definition: ipv6-address.h:46
void SetRoutingProtocol(Ptr< Ipv6RoutingProtocol > routingProtocol)
Set routing protocol for this stack.
uint32_t AddDevice(Ptr< NetDevice > device)
Definition: node.cc:120
uint32_t GetId(void) const
Definition: node.cc:106
virtual Ptr< Node > GetNode(void) const =0
Return the node this socket is associated with.
virtual void SetAddress(Address address)
Set the address of this interface.
void RemoveAllByteTags(void)
Remove all byte tags stored in this packet.
Definition: packet.cc:361
virtual void RegisterOptions()
Register the IPv6 Options.
Describes an IPv6 prefix.
Definition: ipv6-address.h:387
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:381
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.
This test suite implements a Unit Test.
Definition: test.h:1115
void ReceivePacket(Ptr< Socket > socket, Ptr< Packet > packet, const Address &from)
void SetChannel(Ptr< SimpleChannel > channel)
Attach a channel to this net device.
void SetAttribute(std::string name, const AttributeValue &value)
Definition: object-base.cc:176
Ptr< T > GetObject(void) const
Definition: object.h:362
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.