A Discrete-Event Network Simulator
API
sixlowpan-iphc-test.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Universita' di Firenze, Italy
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Author: Tommaso Pecorella <tommaso.pecorella@unifi.it>
18  */
19 
20 #include "ns3/boolean.h"
21 #include "ns3/inet6-socket-address.h"
22 #include "ns3/internet-stack-helper.h"
23 #include "ns3/log.h"
24 #include "ns3/node.h"
25 #include "ns3/simple-channel.h"
26 #include "ns3/simple-net-device.h"
27 #include "ns3/simulator.h"
28 #include "ns3/sixlowpan-net-device.h"
29 #include "ns3/socket-factory.h"
30 #include "ns3/socket.h"
31 #include "ns3/test.h"
32 #include "ns3/udp-socket-factory.h"
33 
34 #include <limits>
35 #include <string>
36 
37 using namespace ns3;
38 
45 {
47 
54  void DoSendData(Ptr<Socket> socket, std::string to);
55 
62  void SendData(Ptr<Socket> socket, std::string to);
63 
64  public:
65  void DoRun() override;
67 
75  void ReceivePacket(Ptr<Socket> socket, Ptr<Packet> packet, const Address& from);
81  void ReceivePkt(Ptr<Socket> socket);
82 };
83 
85  : TestCase("Sixlowpan implementation")
86 {
87 }
88 
89 void
91 {
92  m_receivedPacket = packet;
93 }
94 
95 void
97 {
98  uint32_t availableData [[maybe_unused]] = socket->GetRxAvailable();
100  NS_ASSERT(availableData == m_receivedPacket->GetSize());
101 }
102 
103 void
105 {
106  Address realTo = Inet6SocketAddress(Ipv6Address(to.c_str()), 1234);
107  uint8_t buffer[] = "\"Can you tell me where my country lies?\" \\ said the unifaun to his true "
108  "love's eyes. \\ \"It lies with me!\" cried the Queen of Maybe \\ - for her "
109  "merchandise, he traded in his prize.";
110 
111  Ptr<Packet> packet = Create<Packet>(buffer, 180);
112  NS_TEST_EXPECT_MSG_EQ(socket->SendTo(packet, 0, realTo), 180, "200");
113 }
114 
115 void
117 {
118  m_receivedPacket = Create<Packet>();
119  Simulator::ScheduleWithContext(socket->GetNode()->GetId(),
120  Seconds(0),
122  this,
123  socket,
124  to);
125  Simulator::Run();
126 }
127 
128 void
130 {
131  // Create topology
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  rxNode->AddDevice(rxSix);
146  rxSix->SetNetDevice(rxDev);
147 
148  Ptr<Ipv6> ipv6 = rxNode->GetObject<Ipv6>();
149  ipv6->AddInterface(rxDev);
150  uint32_t netdev_idx = ipv6->AddInterface(rxSix);
151  Ipv6InterfaceAddress ipv6Addr =
152  Ipv6InterfaceAddress(Ipv6Address("2001:0100::1"), Ipv6Prefix(64));
153  ipv6->AddAddress(netdev_idx, ipv6Addr);
154  ipv6->SetUp(netdev_idx);
155  }
156 
157  // Sender Node
158  Ptr<Node> txNode = CreateObject<Node>();
159  internet.Install(txNode);
160  Ptr<SimpleNetDevice> txDev;
161  {
162  txDev = CreateObject<SimpleNetDevice>();
163  txDev->SetAddress(Mac48Address::ConvertFrom(Mac48Address::Allocate()));
164  txNode->AddDevice(txDev);
165 
166  Ptr<SixLowPanNetDevice> txSix = CreateObject<SixLowPanNetDevice>();
167  txNode->AddDevice(txSix);
168  txSix->SetNetDevice(txDev);
169 
170  Ptr<Ipv6> ipv6 = txNode->GetObject<Ipv6>();
171  ipv6->AddInterface(txDev);
172  uint32_t netdev_idx = ipv6->AddInterface(txSix);
173  Ipv6InterfaceAddress ipv6Addr =
174  Ipv6InterfaceAddress(Ipv6Address("2001:0100::2"), Ipv6Prefix(64));
175  ipv6->AddAddress(netdev_idx, ipv6Addr);
176  ipv6->SetUp(netdev_idx);
177  }
178 
179  // link the two nodes
180  Ptr<SimpleChannel> channel1 = CreateObject<SimpleChannel>();
181  rxDev->SetChannel(channel1);
182  txDev->SetChannel(channel1);
183 
184  // Create the UDP sockets
185  Ptr<SocketFactory> rxSocketFactory = rxNode->GetObject<UdpSocketFactory>();
186  Ptr<Socket> rxSocket = rxSocketFactory->CreateSocket();
187  NS_TEST_EXPECT_MSG_EQ(rxSocket->Bind(Inet6SocketAddress(Ipv6Address("2001:0100::1"), 1234)),
188  0,
189  "trivial");
190  rxSocket->SetRecvCallback(MakeCallback(&SixlowpanIphcImplTest::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 "
202  "true love's eyes. \\ \"It lies with me!\" cried the Queen of Maybe \\ "
203  "- for her merchandise, he traded in his prize.";
204  m_receivedPacket->CopyData(rxBuffer, 180);
205  NS_TEST_EXPECT_MSG_EQ(memcmp(rxBuffer, txBuffer, 180), 0, "trivial");
206 
208 
209  Simulator::Destroy();
210 }
211 
218 {
219  public:
221 
222  private:
223 };
224 
226  : TestSuite("sixlowpan-iphc", UNIT)
227 {
228  AddTestCase(new SixlowpanIphcImplTest(), TestCase::QUICK);
229 }
230 
#define max(a, b)
Definition: 80211b.c:42
Ptr< Packet > m_receivedPacket
received packet
void DoRun() override
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.
void ReceivePkt(Ptr< Socket > socket)
Packet receive function.
6LoWPAN IPHC TestSuite
a polymophic address class
Definition: address.h:101
An Inet6 address class.
aggregate IP/TCP/UDP functionality to existing Nodes.
Describes an IPv6 address.
Definition: ipv6-address.h:49
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:455
uint32_t AddDevice(Ptr< NetDevice > device)
Associate a NetDevice to this node.
Definition: node.cc:138
uint32_t GetId() const
Definition: node.cc:117
Ptr< T > GetObject() const
Get a pointer to the requested aggregated Object.
Definition: object.h:471
uint32_t GetSize() const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:861
uint32_t CopyData(uint8_t *buffer, uint32_t size) const
Copy the packet contents to a byte buffer.
Definition: packet.cc:400
void RemoveAllByteTags()
Remove all byte tags stored in this packet.
Definition: packet.cc:393
virtual uint32_t GetRxAvailable() const =0
Return number of bytes which can be returned from one or multiple calls to Recv.
virtual Ptr< Packet > Recv(uint32_t maxSize, uint32_t flags)=0
Read data from the socket.
virtual Ptr< Node > GetNode() const =0
Return the node this socket is associated with.
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:1060
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:301
A suite of tests to run.
Definition: test.h:1256
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:66
#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:251
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Callback< R, Args... > MakeCallback(R(T::*memPtr)(Args...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:704
static SixlowpanIphcTestSuite g_sixlowpanIphcTestSuite
Static variable for test initialization.