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 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Tommaso Pecorella <tommaso.pecorella@unifi.it>
7 */
8
9#include "ns3/boolean.h"
10#include "ns3/enum.h"
11#include "ns3/inet6-socket-address.h"
12#include "ns3/internet-stack-helper.h"
13#include "ns3/log.h"
14#include "ns3/node.h"
15#include "ns3/simple-channel.h"
16#include "ns3/simple-net-device.h"
17#include "ns3/simulator.h"
18#include "ns3/sixlowpan-net-device.h"
19#include "ns3/socket-factory.h"
20#include "ns3/socket.h"
21#include "ns3/test.h"
22#include "ns3/udp-socket-factory.h"
23
24#include <limits>
25#include <string>
26
27using namespace ns3;
28
29/**
30 * @ingroup sixlowpan-tests
31 *
32 * @brief 6LoWPAN HC1 Test
33 */
35{
36 Ptr<Packet> m_receivedPacket; //!< received packet
37
38 /**
39 * Send data function.
40 *
41 * @param socket The sending socket.
42 * @param to The destination.
43 */
44 void DoSendData(Ptr<Socket> socket, std::string to);
45
46 /**
47 * Send data function.
48 *
49 * @param socket The sending socket.
50 * @param to The destination.
51 */
52 void SendData(Ptr<Socket> socket, std::string to);
53
54 public:
55 void DoRun() override;
57
58 /**
59 * Packet receive function.
60 *
61 * @param socket The receiving socket.
62 * @param packet The received packet.
63 * @param from The sender.
64 */
65 void ReceivePacket(Ptr<Socket> socket, Ptr<Packet> packet, const Address& from);
66 /**
67 * Packet receive function.
68 *
69 * @param socket The receiving socket.
70 */
71 void ReceivePkt(Ptr<Socket> socket);
72};
73
75 : TestCase("Sixlowpan implementation")
76{
77}
78
79void
84
85void
87{
88 uint32_t availableData [[maybe_unused]] = socket->GetRxAvailable();
89 m_receivedPacket = socket->Recv(std::numeric_limits<uint32_t>::max(), 0);
90 NS_ASSERT(availableData == m_receivedPacket->GetSize());
91}
92
93void
95{
96 Address realTo = Inet6SocketAddress(Ipv6Address(to.c_str()), 1234);
97 uint8_t buffer[] = "\"Can you tell me where my country lies?\" \\ said the unifaun to his true "
98 "love's eyes. \\ \"It lies with me!\" cried the Queen of Maybe \\ - for her "
99 "merchandise, he traded in his prize.";
100
101 Ptr<Packet> packet = Create<Packet>(buffer, 180);
102 NS_TEST_EXPECT_MSG_EQ(socket->SendTo(packet, 0, realTo), 180, "200");
103}
104
105void
107{
109 Simulator::ScheduleWithContext(socket->GetNode()->GetId(),
110 Seconds(0),
112 this,
113 socket,
114 to);
116}
117
118void
120{
121 // Create topology
122 InternetStackHelper internet;
123 internet.SetIpv4StackInstall(false);
124
125 // Receiver Node
126 Ptr<Node> rxNode = CreateObject<Node>();
127 internet.Install(rxNode);
129 { // first interface
132 rxNode->AddDevice(rxDev);
133
135 rxSix->SetAttribute("CompressionType", EnumValue(SixLowPanNetDevice::HC1));
136 rxNode->AddDevice(rxSix);
137 rxSix->SetNetDevice(rxDev);
138
139 Ptr<Ipv6> ipv6 = rxNode->GetObject<Ipv6>();
140 ipv6->AddInterface(rxDev);
141 uint32_t netdev_idx = ipv6->AddInterface(rxSix);
142 Ipv6InterfaceAddress ipv6Addr =
143 Ipv6InterfaceAddress(Ipv6Address("2001:0100::1"), Ipv6Prefix(64));
144 ipv6->AddAddress(netdev_idx, ipv6Addr);
145 ipv6->SetUp(netdev_idx);
146 }
147
148 // Sender Node
149 Ptr<Node> txNode = CreateObject<Node>();
150 internet.Install(txNode);
152 {
155 txNode->AddDevice(txDev);
156
158 txSix->SetAttribute("CompressionType", EnumValue(SixLowPanNetDevice::HC1));
159 txNode->AddDevice(txSix);
160 txSix->SetNetDevice(txDev);
161
162 Ptr<Ipv6> ipv6 = txNode->GetObject<Ipv6>();
163 ipv6->AddInterface(txDev);
164 uint32_t netdev_idx = ipv6->AddInterface(txSix);
165 Ipv6InterfaceAddress ipv6Addr =
166 Ipv6InterfaceAddress(Ipv6Address("2001:0100::2"), Ipv6Prefix(64));
167 ipv6->AddAddress(netdev_idx, ipv6Addr);
168 ipv6->SetUp(netdev_idx);
169 }
170
171 // link the two nodes
173 rxDev->SetChannel(channel1);
174 txDev->SetChannel(channel1);
175
176 // Create the UDP sockets
177 Ptr<SocketFactory> rxSocketFactory = rxNode->GetObject<UdpSocketFactory>();
178 Ptr<Socket> rxSocket = rxSocketFactory->CreateSocket();
179 NS_TEST_EXPECT_MSG_EQ(rxSocket->Bind(Inet6SocketAddress(Ipv6Address("2001:0100::1"), 1234)),
180 0,
181 "trivial");
182 rxSocket->SetRecvCallback(MakeCallback(&SixlowpanHc1ImplTest::ReceivePkt, this));
183
184 Ptr<SocketFactory> txSocketFactory = txNode->GetObject<UdpSocketFactory>();
185 Ptr<Socket> txSocket = txSocketFactory->CreateSocket();
186 txSocket->SetAllowBroadcast(true);
187 // ------ Now the tests ------------
188
189 // Unicast test
190 SendData(txSocket, "2001:0100::1");
191 NS_TEST_EXPECT_MSG_EQ(m_receivedPacket->GetSize(), 180, "trivial");
192 uint8_t rxBuffer[180];
193 uint8_t txBuffer[180] = "\"Can you tell me where my country lies?\" \\ said the unifaun to his "
194 "true love's eyes. \\ \"It lies with me!\" cried the Queen of Maybe \\ "
195 "- 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
199 m_receivedPacket->RemoveAllByteTags();
200
202}
203
204/**
205 * @ingroup sixlowpan-tests
206 *
207 * @brief 6LoWPAN HC1 TestSuite
208 */
210{
211 public:
213
214 private:
215};
216
222
223static 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:114
Hold variables of type enum.
Definition enum.h:52
An Inet6 address class.
aggregate IP/TCP/UDP functionality to existing Nodes.
Describes an IPv6 address.
Access to the IPv6 forwarding table, interfaces, and configuration.
Definition ipv6.h:71
IPv6 address associated with an interface.
Describes an IPv6 prefix.
static Mac48Address ConvertFrom(const Address &address)
static Mac48Address Allocate()
Allocate a new Mac48Address.
Smart pointer class similar to boost::intrusive_ptr.
Definition ptr.h:70
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:125
static void ScheduleWithContext(uint32_t context, const Time &delay, FUNC f, Ts &&... args)
Schedule an event with the given context.
Definition simulator.h:597
static void Run()
Run the simulation.
Definition simulator.cc:161
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition test.cc:296
@ QUICK
Fast test.
Definition test.h:1057
TestCase(const TestCase &)=delete
Caller graph was not generated because of its size.
Type
Type of test.
Definition test.h:1271
TestSuite(std::string name, Type type=Type::UNIT)
Construct a new test suite.
Definition test.cc:494
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:55
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:690
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:627
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:492
#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:240
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1273
Every class exported by the ns3 library is enclosed in the ns3 namespace.
static SixlowpanHc1TestSuite g_sixlowpanHc1TestSuite
Static variable for test initialization.