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
37using 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
89void
91{
92 m_receivedPacket = packet;
93}
94
95void
97{
98 uint32_t availableData [[maybe_unused]] = socket->GetRxAvailable();
100 NS_ASSERT(availableData == m_receivedPacket->GetSize());
101}
102
103void
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
115void
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
128void
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);
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 rxNode->AddDevice(rxSix);
147 rxSix->SetNetDevice(rxDev);
148
149 Ptr<Ipv6> ipv6 = rxNode->GetObject<Ipv6>();
150 ipv6->AddInterface(rxDev);
151 uint32_t netdev_idx = ipv6->AddInterface(rxSix);
152 Ipv6InterfaceAddress ipv6Addr =
153 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);
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 txNode->AddDevice(txSix);
170 txSix->SetNetDevice(txDev);
171
172 Ptr<Ipv6> ipv6 = txNode->GetObject<Ipv6>();
173 ipv6->AddInterface(txDev);
174 uint32_t netdev_idx = ipv6->AddInterface(txSix);
175 Ipv6InterfaceAddress ipv6Addr =
176 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)),
190 0,
191 "trivial");
192 rxSocket->SetRecvCallback(MakeCallback(&SixlowpanIphcImplTest::ReceivePkt, this));
193
194 Ptr<SocketFactory> txSocketFactory = txNode->GetObject<UdpSocketFactory>();
195 Ptr<Socket> txSocket = txSocketFactory->CreateSocket();
196 txSocket->SetAllowBroadcast(true);
197 // ------ Now the tests ------------
198
199 // Unicast test
200 SendData(txSocket, "2001:0100::1");
202 uint8_t rxBuffer[180];
203 uint8_t txBuffer[180] = "\"Can you tell me where my country lies?\" \\ said the unifaun to his "
204 "true love's eyes. \\ \"It lies with me!\" cried the Queen of Maybe \\ "
205 "- for her merchandise, he traded in his prize.";
206 m_receivedPacket->CopyData(rxBuffer, 180);
207 NS_TEST_EXPECT_MSG_EQ(memcmp(rxBuffer, txBuffer, 180), 0, "trivial");
208
210
211 Simulator::Destroy();
212}
213
220{
221 public:
223
224 private:
225};
226
228 : TestSuite("sixlowpan-iphc", UNIT)
229{
230 AddTestCase(new SixlowpanIphcImplTest(), TestCase::QUICK);
231}
232
#define max(a, b)
Definition: 80211b.c:43
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:92
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 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:863
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:305
A suite of tests to run.
Definition: test.h:1256
API to create UDP socket instances.
#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:1338
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:691
static SixlowpanIphcTestSuite g_sixlowpanIphcTestSuite
Static variable for test initialization.