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/inet6-socket-address.h"
11#include "ns3/internet-stack-helper.h"
12#include "ns3/log.h"
13#include "ns3/node.h"
14#include "ns3/simple-channel.h"
15#include "ns3/simple-net-device.h"
16#include "ns3/simulator.h"
17#include "ns3/sixlowpan-net-device.h"
18#include "ns3/socket-factory.h"
19#include "ns3/socket.h"
20#include "ns3/test.h"
21#include "ns3/udp-socket-factory.h"
22
23#include <limits>
24#include <string>
25
26using namespace ns3;
27
28/**
29 * @ingroup sixlowpan-tests
30 *
31 * @brief 6LoWPAN HC1 Test
32 */
34{
35 Ptr<Packet> m_receivedPacket; //!< received packet
36
37 /**
38 * Send data function.
39 *
40 * @param socket The sending socket.
41 * @param to The destination.
42 */
43 void DoSendData(Ptr<Socket> socket, std::string to);
44
45 /**
46 * Send data function.
47 *
48 * @param socket The sending socket.
49 * @param to The destination.
50 */
51 void SendData(Ptr<Socket> socket, std::string to);
52
53 public:
54 void DoRun() override;
56
57 /**
58 * Packet receive function.
59 *
60 * @param socket The receiving socket.
61 * @param packet The received packet.
62 * @param from The sender.
63 */
64 void ReceivePacket(Ptr<Socket> socket, Ptr<Packet> packet, const Address& from);
65 /**
66 * Packet receive function.
67 *
68 * @param socket The receiving socket.
69 */
70 void ReceivePkt(Ptr<Socket> socket);
71};
72
74 : TestCase("Sixlowpan implementation")
75{
76}
77
78void
83
84void
86{
87 uint32_t availableData [[maybe_unused]] = socket->GetRxAvailable();
88 m_receivedPacket = socket->Recv(std::numeric_limits<uint32_t>::max(), 0);
89 NS_ASSERT(availableData == m_receivedPacket->GetSize());
90}
91
92void
94{
95 Address realTo = Inet6SocketAddress(Ipv6Address(to.c_str()), 1234);
96 uint8_t buffer[] = "\"Can you tell me where my country lies?\" \\ said the unifaun to his true "
97 "love's eyes. \\ \"It lies with me!\" cried the Queen of Maybe \\ - for her "
98 "merchandise, he traded in his prize.";
99
100 Ptr<Packet> packet = Create<Packet>(buffer, 180);
101 NS_TEST_EXPECT_MSG_EQ(socket->SendTo(packet, 0, realTo), 180, "200");
102}
103
104void
106{
108 Simulator::ScheduleWithContext(socket->GetNode()->GetId(),
109 Seconds(0),
111 this,
112 socket,
113 to);
115}
116
117void
119{
120 // Create topology
121 InternetStackHelper internet;
122 internet.SetIpv4StackInstall(false);
123
124 // Receiver Node
125 Ptr<Node> rxNode = CreateObject<Node>();
126 internet.Install(rxNode);
128 { // first interface
131 rxNode->AddDevice(rxDev);
132
134 rxSix->SetAttribute("Rfc6282", BooleanValue(false));
135 rxNode->AddDevice(rxSix);
136 rxSix->SetNetDevice(rxDev);
137
138 Ptr<Ipv6> ipv6 = rxNode->GetObject<Ipv6>();
139 ipv6->AddInterface(rxDev);
140 uint32_t netdev_idx = ipv6->AddInterface(rxSix);
141 Ipv6InterfaceAddress ipv6Addr =
142 Ipv6InterfaceAddress(Ipv6Address("2001:0100::1"), Ipv6Prefix(64));
143 ipv6->AddAddress(netdev_idx, ipv6Addr);
144 ipv6->SetUp(netdev_idx);
145 }
146
147 // Sender Node
148 Ptr<Node> txNode = CreateObject<Node>();
149 internet.Install(txNode);
151 {
154 txNode->AddDevice(txDev);
155
157 txSix->SetAttribute("Rfc6282", BooleanValue(false));
158 txNode->AddDevice(txSix);
159 txSix->SetNetDevice(txDev);
160
161 Ptr<Ipv6> ipv6 = txNode->GetObject<Ipv6>();
162 ipv6->AddInterface(txDev);
163 uint32_t netdev_idx = ipv6->AddInterface(txSix);
164 Ipv6InterfaceAddress ipv6Addr =
165 Ipv6InterfaceAddress(Ipv6Address("2001:0100::2"), Ipv6Prefix(64));
166 ipv6->AddAddress(netdev_idx, ipv6Addr);
167 ipv6->SetUp(netdev_idx);
168 }
169
170 // link the two nodes
172 rxDev->SetChannel(channel1);
173 txDev->SetChannel(channel1);
174
175 // Create the UDP sockets
176 Ptr<SocketFactory> rxSocketFactory = rxNode->GetObject<UdpSocketFactory>();
177 Ptr<Socket> rxSocket = rxSocketFactory->CreateSocket();
178 NS_TEST_EXPECT_MSG_EQ(rxSocket->Bind(Inet6SocketAddress(Ipv6Address("2001:0100::1"), 1234)),
179 0,
180 "trivial");
181 rxSocket->SetRecvCallback(MakeCallback(&SixlowpanHc1ImplTest::ReceivePkt, this));
182
183 Ptr<SocketFactory> txSocketFactory = txNode->GetObject<UdpSocketFactory>();
184 Ptr<Socket> txSocket = txSocketFactory->CreateSocket();
185 txSocket->SetAllowBroadcast(true);
186 // ------ Now the tests ------------
187
188 // Unicast test
189 SendData(txSocket, "2001:0100::1");
191 uint8_t rxBuffer[180];
192 uint8_t txBuffer[180] = "\"Can you tell me where my country lies?\" \\ said the unifaun to his "
193 "true love's eyes. \\ \"It lies with me!\" cried the Queen of Maybe \\ "
194 "- for her merchandise, he traded in his prize.";
195 m_receivedPacket->CopyData(rxBuffer, 180);
196 NS_TEST_EXPECT_MSG_EQ(memcmp(rxBuffer, txBuffer, 180), 0, "trivial");
197
199
201}
202
203/**
204 * @ingroup sixlowpan-tests
205 *
206 * @brief 6LoWPAN HC1 TestSuite
207 */
209{
210 public:
212
213 private:
214};
215
217 : TestSuite("sixlowpan-hc1", Type::UNIT)
218{
219 AddTestCase(new SixlowpanHc1ImplTest(), TestCase::Duration::QUICK);
220}
221
222static 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:90
AttributeValue implementation for Boolean.
Definition boolean.h:26
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.
uint32_t GetSize() const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition packet.h:850
uint32_t CopyData(uint8_t *buffer, uint32_t size) const
Copy the packet contents to a byte buffer.
Definition packet.cc:389
void RemoveAllByteTags()
Remove all byte tags stored in this packet.
Definition packet.cc:382
Smart pointer class similar to boost::intrusive_ptr.
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
static void ScheduleWithContext(uint32_t context, const Time &delay, FUNC f, Ts &&... args)
Schedule an event with the given context.
Definition simulator.h:577
static void Run()
Run the simulation.
Definition simulator.cc:167
encapsulates test code
Definition test.h:1050
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition test.cc:292
A suite of tests to run.
Definition test.h:1267
Type
Type of test.
Definition test.h:1274
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
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:436
#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:241
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1344
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:684
static SixlowpanHc1TestSuite g_sixlowpanHc1TestSuite
Static variable for test initialization.