A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
sixlowpan-hc1-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
39/**
40 * \ingroup sixlowpan-tests
41 *
42 * \brief 6LoWPAN HC1 Test
43 */
45{
46 Ptr<Packet> m_receivedPacket; //!< received packet
47
48 /**
49 * Send data function.
50 *
51 * \param socket The sending socket.
52 * \param to The destination.
53 */
54 void DoSendData(Ptr<Socket> socket, std::string to);
55
56 /**
57 * Send data function.
58 *
59 * \param socket The sending socket.
60 * \param to The destination.
61 */
62 void SendData(Ptr<Socket> socket, std::string to);
63
64 public:
65 void DoRun() override;
67
68 /**
69 * Packet receive function.
70 *
71 * \param socket The receiving socket.
72 * \param packet The received packet.
73 * \param from The sender.
74 */
75 void ReceivePacket(Ptr<Socket> socket, Ptr<Packet> packet, const Address& from);
76 /**
77 * Packet receive function.
78 *
79 * \param socket The receiving socket.
80 */
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();
99 m_receivedPacket = socket->Recv(std::numeric_limits<uint32_t>::max(), 0);
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);
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>();
142 rxNode->AddDevice(rxDev);
143
144 Ptr<SixLowPanNetDevice> rxSix = CreateObject<SixLowPanNetDevice>();
145 rxSix->SetAttribute("Rfc6282", BooleanValue(false));
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>();
165 txNode->AddDevice(txDev);
166
167 Ptr<SixLowPanNetDevice> txSix = CreateObject<SixLowPanNetDevice>();
168 txSix->SetAttribute("Rfc6282", BooleanValue(false));
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(&SixlowpanHc1ImplTest::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
212}
213
214/**
215 * \ingroup sixlowpan-tests
216 *
217 * \brief 6LoWPAN HC1 TestSuite
218 */
220{
221 public:
223
224 private:
225};
226
228 : TestSuite("sixlowpan-hc1", Type::UNIT)
229{
230 AddTestCase(new SixlowpanHc1ImplTest(), TestCase::Duration::QUICK);
231}
232
233static SixlowpanHc1TestSuite g_sixlowpanHc1TestSuite; //!< Static variable for test initialization
void ReceivePkt(Ptr< Socket > socket)
Packet receive function.
void SendData(Ptr< Socket > socket, std::string to)
Send data function.
void DoRun() override
Implementation to actually run this TestCase.
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:101
AttributeValue implementation for Boolean.
Definition: boolean.h:37
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
static Mac48Address ConvertFrom(const Address &address)
static Mac48Address Allocate()
Allocate a new Mac48Address.
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
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:142
static void ScheduleWithContext(uint32_t context, const Time &delay, FUNC f, Ts &&... args)
Schedule an event with the given context.
Definition: simulator.h:588
static void Run()
Run the simulation.
Definition: simulator.cc:178
encapsulates test code
Definition: test.h:1061
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:301
A suite of tests to run.
Definition: test.h:1268
Type
Type of test.
Definition: test.h:1275
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:252
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 SixlowpanHc1TestSuite g_sixlowpanHc1TestSuite
Static variable for test initialization.