A Discrete-Event Network Simulator
API
sixlowpan-iphc-stateful-test.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020 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 "mock-net-device.h"
21 
22 #include "ns3/boolean.h"
23 #include "ns3/icmpv6-l4-protocol.h"
24 #include "ns3/inet6-socket-address.h"
25 #include "ns3/internet-stack-helper.h"
26 #include "ns3/ipv6-address-helper.h"
27 #include "ns3/log.h"
28 #include "ns3/node.h"
29 #include "ns3/simulator.h"
30 #include "ns3/sixlowpan-header.h"
31 #include "ns3/sixlowpan-helper.h"
32 #include "ns3/sixlowpan-net-device.h"
33 #include "ns3/socket-factory.h"
34 #include "ns3/socket.h"
35 #include "ns3/test.h"
36 
37 #include <limits>
38 #include <string>
39 #include <vector>
40 
41 using namespace ns3;
42 
49 {
53  struct Data
54  {
58  };
59 
60  std::vector<Data> m_txPackets;
61  std::vector<Data> m_rxPackets;
62 
73  bool ReceiveFromMockDevice(Ptr<NetDevice> device,
74  Ptr<const Packet> packet,
75  uint16_t protocol,
76  const Address& source,
77  const Address& destination,
78  NetDevice::PacketType packetType);
79 
90  bool PromiscReceiveFromSixLowPanDevice(Ptr<NetDevice> device,
91  Ptr<const Packet> packet,
92  uint16_t protocol,
93  const Address& source,
94  const Address& destination,
95  NetDevice::PacketType packetType);
96 
103  void SendOnePacket(Ptr<NetDevice> device, Ipv6Address from, Ipv6Address to);
104 
107 
108  public:
109  void DoRun() override;
111 };
112 
114  : TestCase("Sixlowpan IPHC stateful implementation")
115 {
116 }
117 
118 bool
120  Ptr<const Packet> packet,
121  uint16_t protocol,
122  const Address& source,
123  const Address& destination,
124  NetDevice::PacketType packetType)
125 {
126  Data incomingPkt;
127  incomingPkt.packet = packet->Copy();
128  incomingPkt.src = source;
129  incomingPkt.dst = destination;
130  m_txPackets.push_back(incomingPkt);
131 
132  Ptr<MockNetDevice> mockDev = DynamicCast<MockNetDevice>(m_mockDevices.Get(1));
133  if (mockDev)
134  {
135  uint32_t id = mockDev->GetNode()->GetId();
136  Simulator::ScheduleWithContext(id,
137  Time(1),
138  &MockNetDevice::Receive,
139  mockDev,
140  incomingPkt.packet,
141  protocol,
142  destination,
143  source,
144  packetType);
145  }
146  return true;
147 }
148 
149 bool
151  Ptr<const Packet> packet,
152  uint16_t protocol,
153  const Address& source,
154  const Address& destination,
155  NetDevice::PacketType packetType)
156 {
157  Data incomingPkt;
158  incomingPkt.packet = packet->Copy();
159  incomingPkt.src = source;
160  incomingPkt.dst = destination;
161  m_rxPackets.push_back(incomingPkt);
162 
163  return true;
164 }
165 
166 void
168  Ipv6Address from,
169  Ipv6Address to)
170 {
171  Ptr<Packet> pkt = Create<Packet>(10);
172  Ipv6Header ipHdr;
173  ipHdr.SetSource(from);
174  ipHdr.SetDestination(to);
175  ipHdr.SetHopLimit(64);
176  ipHdr.SetPayloadLength(10);
177  ipHdr.SetNextHeader(0xff);
178  pkt->AddHeader(ipHdr);
179 
180  device->Send(pkt, Mac48Address("00:00:00:00:00:02"), 0);
181 }
182 
183 void
185 {
187  nodes.Create(2);
188 
189  // First node, setup NetDevices.
190  Ptr<MockNetDevice> mockNetDevice0 = CreateObject<MockNetDevice>();
191  nodes.Get(0)->AddDevice(mockNetDevice0);
192  mockNetDevice0->SetNode(nodes.Get(0));
193  mockNetDevice0->SetAddress(Mac48Address("00:00:00:00:00:01"));
194  mockNetDevice0->SetMtu(150);
195  mockNetDevice0->SetSendCallback(
197  m_mockDevices.Add(mockNetDevice0);
198 
199  // Second node, setup NetDevices.
200  Ptr<MockNetDevice> mockNetDevice1 = CreateObject<MockNetDevice>();
201  nodes.Get(1)->AddDevice(mockNetDevice1);
202  mockNetDevice1->SetNode(nodes.Get(1));
203  mockNetDevice1->SetAddress(Mac48Address("00:00:00:00:00:02"));
204  mockNetDevice1->SetMtu(150);
205  m_mockDevices.Add(mockNetDevice1);
206 
207  InternetStackHelper internetv6;
208  internetv6.Install(nodes);
209 
210  SixLowPanHelper sixlowpan;
211  m_sixDevices = sixlowpan.Install(m_mockDevices);
212  m_sixDevices.Get(1)->SetPromiscReceiveCallback(
214 
215  Ipv6AddressHelper ipv6;
216  ipv6.SetBase(Ipv6Address("2001:2::"), Ipv6Prefix(64));
217  Ipv6InterfaceContainer deviceInterfaces;
218  deviceInterfaces = ipv6.Assign(m_sixDevices);
219 
220  // This is a hack to prevent Router Solicitations and Duplicate Address Detection being sent.
221  for (auto i = nodes.Begin(); i != nodes.End(); i++)
222  {
223  Ptr<Node> node = *i;
224  Ptr<Ipv6L3Protocol> ipv6L3 = (*i)->GetObject<Ipv6L3Protocol>();
225  if (ipv6L3)
226  {
227  ipv6L3->SetAttribute("IpForward", BooleanValue(true));
228  ipv6L3->SetAttribute("SendIcmpv6Redirect", BooleanValue(false));
229  }
230  Ptr<Icmpv6L4Protocol> icmpv6 = (*i)->GetObject<Icmpv6L4Protocol>();
231  if (icmpv6)
232  {
233  icmpv6->SetAttribute("DAD", BooleanValue(false));
234  }
235  }
236 
237  sixlowpan.AddContext(m_sixDevices, 0, Ipv6Prefix("2001:2::", 64), Time(Minutes(30)));
238  sixlowpan.AddContext(m_sixDevices, 1, Ipv6Prefix("2001:1::", 64), Time(Minutes(30)));
239 
240  Ipv6Address srcElided = deviceInterfaces.GetAddress(0, 1);
241  Ipv6Address dstElided =
242  Ipv6Address::MakeAutoconfiguredAddress(Mac48Address("00:00:00:00:00:02"),
243  Ipv6Prefix("2001:2::", 64));
244 
245  Simulator::Schedule(Seconds(1),
247  this,
248  m_sixDevices.Get(0),
249  Ipv6Address::GetAny(),
250  dstElided);
251 
252  Simulator::Schedule(Seconds(2),
254  this,
255  m_sixDevices.Get(0),
256  Ipv6Address("2001:2::f00d:f00d:cafe:cafe"),
257  Ipv6Address("2001:1::0000:00ff:fe00:cafe"));
258 
259  Simulator::Schedule(Seconds(3),
261  this,
262  m_sixDevices.Get(0),
263  Ipv6Address("2001:1::0000:00ff:fe00:cafe"),
264  Ipv6Address("2001:1::f00d:f00d:cafe:cafe"));
265 
266  Simulator::Schedule(Seconds(4),
268  this,
269  m_sixDevices.Get(0),
270  srcElided,
271  Ipv6Address("2001:1::f00d:f00d:cafe:cafe"));
272 
273  Simulator::Stop(Seconds(10));
274 
275  Simulator::Run();
276  Simulator::Destroy();
277 
278  // ------ Now the tests ------------
279 
280  SixLowPanIphc iphcHdr;
281  Ipv6Header ipv6Hdr;
282 
283  // first packet sent, expected CID(0) SAC(1) SAM (0) M(0) DAC(1) DAM (3)
284  m_txPackets[0].packet->RemoveHeader(iphcHdr);
285  NS_TEST_EXPECT_MSG_EQ(iphcHdr.GetCid(), false, "CID should be false, is true");
286  NS_TEST_EXPECT_MSG_EQ(iphcHdr.GetSac(), true, "SAC should be true, is false");
287  NS_TEST_EXPECT_MSG_EQ(iphcHdr.GetSam(),
288  SixLowPanIphc::HC_INLINE,
289  "SAM should be HC_INLINE, it is not");
290  NS_TEST_EXPECT_MSG_EQ(iphcHdr.GetM(), false, "M should be false, is true");
291  NS_TEST_EXPECT_MSG_EQ(iphcHdr.GetDac(), true, "DAC should be true, is false");
292  NS_TEST_EXPECT_MSG_EQ(iphcHdr.GetSrcContextId(), 0, "Src context should be 0, it is not");
293  NS_TEST_EXPECT_MSG_EQ(iphcHdr.GetDstContextId(), 0, "Dst context should be 0, it is not");
294  NS_TEST_EXPECT_MSG_EQ(iphcHdr.GetDam(),
295  SixLowPanIphc::HC_COMPR_0,
296  "DAM should be HC_COMPR_0, it is not");
297 
298  // first packet received, expected :: -> dstElided
299  m_rxPackets[0].packet->RemoveHeader(ipv6Hdr);
301  Ipv6Address::GetAny(),
302  "Src address wrongly rebuilt");
303  NS_TEST_EXPECT_MSG_EQ(ipv6Hdr.GetDestination(), dstElided, "Dst address wrongly rebuilt");
304 
305  // second packet sent, expected CID(1) SAC(1) SAM (1) M(0) DAC(1) DAM (2)
306  m_txPackets[1].packet->RemoveHeader(iphcHdr);
307  NS_TEST_EXPECT_MSG_EQ(iphcHdr.GetCid(), true, "CID should be true, is false");
308  NS_TEST_EXPECT_MSG_EQ(iphcHdr.GetSac(), true, "SAC should be true, is false");
309  NS_TEST_EXPECT_MSG_EQ(iphcHdr.GetSam(),
310  SixLowPanIphc::HC_COMPR_64,
311  "SAM should be HC_COMPR_64, it is not");
312  NS_TEST_EXPECT_MSG_EQ(iphcHdr.GetM(), false, "M should be false, is true");
313  NS_TEST_EXPECT_MSG_EQ(iphcHdr.GetDac(), true, "DAC should be true, is false");
314  NS_TEST_EXPECT_MSG_EQ(iphcHdr.GetSrcContextId(), 0, "Src context should be 0, it is not");
315  NS_TEST_EXPECT_MSG_EQ(iphcHdr.GetDstContextId(), 1, "Dst context should be 1, it is not");
316  NS_TEST_EXPECT_MSG_EQ(iphcHdr.GetDam(),
317  SixLowPanIphc::HC_COMPR_16,
318  "DAM should be HC_COMPR_16, it is not");
319 
320  // second packet received, expected 2001:2::f00d:f00d:cafe:cafe -> 2001:1::0000:00ff:fe00:cafe
321  m_rxPackets[1].packet->RemoveHeader(ipv6Hdr);
323  Ipv6Address("2001:2::f00d:f00d:cafe:cafe"),
324  "Src address wrongly rebuilt");
326  Ipv6Address("2001:1::0000:00ff:fe00:cafe"),
327  "Dst address wrongly rebuilt");
328 
329  // third packet sent, expected CID(17) SAC(1) SAM (2) M(0) DAC(1) DAM (1)
330  m_txPackets[2].packet->RemoveHeader(iphcHdr);
331  NS_TEST_EXPECT_MSG_EQ(iphcHdr.GetCid(), true, "CID should be true, is false");
332  NS_TEST_EXPECT_MSG_EQ(iphcHdr.GetSac(), true, "SAC should be true, is false");
333  NS_TEST_EXPECT_MSG_EQ(iphcHdr.GetSam(),
334  SixLowPanIphc::HC_COMPR_16,
335  "SAM should be HC_COMPR_16, it is not");
336  NS_TEST_EXPECT_MSG_EQ(iphcHdr.GetM(), false, "M should be false, is true");
337  NS_TEST_EXPECT_MSG_EQ(iphcHdr.GetDac(), true, "DAC should be true, is false");
338  NS_TEST_EXPECT_MSG_EQ(iphcHdr.GetSrcContextId(), 1, "Src context should be 1, it is not");
339  NS_TEST_EXPECT_MSG_EQ(iphcHdr.GetDstContextId(), 1, "Dst context should be 1, it is not");
340  NS_TEST_EXPECT_MSG_EQ(iphcHdr.GetDam(),
341  SixLowPanIphc::HC_COMPR_64,
342  "DAM should be HC_COMPR_64, it is not");
343 
344  // third packet received, expected 2001:1::0000:00ff:fe00:cafe -> 2001:1::f00d:f00d:cafe:cafe
345  m_rxPackets[2].packet->RemoveHeader(ipv6Hdr);
347  Ipv6Address("2001:1::0000:00ff:fe00:cafe"),
348  "Src address wrongly rebuilt");
350  Ipv6Address("2001:1::f00d:f00d:cafe:cafe"),
351  "Dst address wrongly rebuilt");
352 
353  // fourth packet sent, expected CID(1) SAC(1) SAM (3) M(0) DAC(1) DAM (1)
354  m_txPackets[3].packet->RemoveHeader(iphcHdr);
355  NS_TEST_EXPECT_MSG_EQ(iphcHdr.GetCid(), true, "CID should be true, is false");
356  NS_TEST_EXPECT_MSG_EQ(iphcHdr.GetSac(), true, "SAC should be true, is false");
357  NS_TEST_EXPECT_MSG_EQ(iphcHdr.GetSam(),
358  SixLowPanIphc::HC_COMPR_0,
359  "SAM should be HC_COMPR_0, it is not");
360  NS_TEST_EXPECT_MSG_EQ(iphcHdr.GetM(), false, "M should be false, is true");
361  NS_TEST_EXPECT_MSG_EQ(iphcHdr.GetDac(), true, "DAC should be true, is false");
362  NS_TEST_EXPECT_MSG_EQ(iphcHdr.GetSrcContextId(), 0, "Src context should be 0, it is not");
363  NS_TEST_EXPECT_MSG_EQ(iphcHdr.GetDstContextId(), 1, "Dst context should be 1, it is not");
364  NS_TEST_EXPECT_MSG_EQ(iphcHdr.GetDam(),
365  SixLowPanIphc::HC_COMPR_64,
366  "DAM should be HC_COMPR_64, it is not");
367 
368  // fourth packet received, expected srcElided -> 2001:1::f00d:f00d:cafe:cafe
369  m_rxPackets[3].packet->RemoveHeader(ipv6Hdr);
370  NS_TEST_EXPECT_MSG_EQ(ipv6Hdr.GetSource(), srcElided, "Src address wrongly rebuilt");
372  Ipv6Address("2001:1::f00d:f00d:cafe:cafe"),
373  "Dst address wrongly rebuilt");
374 
375  m_rxPackets.clear();
376  m_txPackets.clear();
377 }
378 
385 {
386  public:
388 
389  private:
390 };
391 
393  : TestSuite("sixlowpan-iphc-stateful", UNIT)
394 {
395  AddTestCase(new SixlowpanIphcStatefulImplTest(), TestCase::QUICK);
396 }
397 
6LoWPAN IPHC stateful compression Test
std::vector< Data > m_txPackets
Transmitted packets.
bool ReceiveFromMockDevice(Ptr< NetDevice > device, Ptr< const Packet > packet, uint16_t protocol, const Address &source, const Address &destination, NetDevice::PacketType packetType)
Receive from a MockDevice.
void DoRun() override
Implementation to actually run this TestCase.
bool PromiscReceiveFromSixLowPanDevice(Ptr< NetDevice > device, Ptr< const Packet > packet, uint16_t protocol, const Address &source, const Address &destination, NetDevice::PacketType packetType)
Promiscuous receive from a SixLowPanNetDevice.
NetDeviceContainer m_mockDevices
MockNetDevice container.
std::vector< Data > m_rxPackets
Received packets.
void SendOnePacket(Ptr< NetDevice > device, Ipv6Address from, Ipv6Address to)
Send one packet.
NetDeviceContainer m_sixDevices
SixLowPanNetDevice container.
a polymophic address class
Definition: address.h:101
An implementation of the ICMPv6 protocol.
aggregate IP/TCP/UDP functionality to existing Nodes.
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
Helper class to auto-assign global IPv6 unicast addresses.
void SetBase(Ipv6Address network, Ipv6Prefix prefix, Ipv6Address base=Ipv6Address("::1"))
Set the base network number, network prefix, and base interface ID.
Ipv6InterfaceContainer Assign(const NetDeviceContainer &c)
Allocate an Ipv6InterfaceContainer with auto-assigned addresses.
Describes an IPv6 address.
Definition: ipv6-address.h:49
Packet header for IPv6.
Definition: ipv6-header.h:35
void SetDestination(Ipv6Address dst)
Set the "Destination address" field.
Definition: ipv6-header.cc:118
void SetSource(Ipv6Address src)
Set the "Source address" field.
Definition: ipv6-header.cc:106
void SetHopLimit(uint8_t limit)
Set the "Hop limit" field (TTL).
Definition: ipv6-header.cc:94
Ipv6Address GetDestination() const
Get the "Destination address" field.
Definition: ipv6-header.cc:124
void SetPayloadLength(uint16_t len)
Set the "Payload length" field.
Definition: ipv6-header.cc:70
Ipv6Address GetSource() const
Get the "Source address" field.
Definition: ipv6-header.cc:112
void SetNextHeader(uint8_t next)
Set the "Next header" field.
Definition: ipv6-header.cc:82
Keep track of a set of IPv6 interfaces.
Ipv6Address GetAddress(uint32_t i, uint32_t j) const
Get the address for the specified index.
IPv6 layer implementation.
Describes an IPv6 prefix.
Definition: ipv6-address.h:455
an EUI-48 address
Definition: mac48-address.h:46
holds a vector of ns3::NetDevice pointers
void Add(NetDeviceContainer other)
Append the contents of another NetDeviceContainer to the end of this container.
Ptr< NetDevice > Get(uint32_t i) const
Get the Ptr<NetDevice> stored in this container at a given index.
PacketType
Packet types are used as they are in Linux.
Definition: net-device.h:300
keep track of a set of node pointers.
Iterator End() const
Get an iterator which indicates past-the-last Node in the container.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
Iterator Begin() const
Get an iterator which refers to the first Node in the container.
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
uint32_t AddDevice(Ptr< NetDevice > device)
Associate a NetDevice to this node.
Definition: node.cc:138
void AddHeader(const Header &header)
Add header to this packet.
Definition: packet.cc:268
Ptr< Packet > Copy() const
performs a COW copy of the packet.
Definition: packet.cc:131
Setup a sixlowpan stack to be used as a shim between IPv6 and a generic NetDevice.
NetDeviceContainer Install(NetDeviceContainer c)
Install the SixLoWPAN stack on top of an existing NetDevice.
void AddContext(NetDeviceContainer c, uint8_t contextId, Ipv6Prefix context, Time validity)
Adds a compression Context to a set of NetDevices.
LOWPAN_IPHC base Encoding - see RFC 6282.
bool GetSac() const
Get the SAC (Source Address Compression) compression.
HeaderCompression_e GetDam() const
Get the DAM (Destination Address Mode) compression.
bool GetDac() const
Get the DAC (Destination Address Compression) compression.
bool GetM() const
Get the M (Multicast) compression.
HeaderCompression_e GetSam() const
Get the SAM (Source Address Mode) compression.
bool GetCid() const
Get the CID (Context Identifier Extension) compression.
uint8_t GetSrcContextId() const
Get the SrcContextId.
uint8_t GetDstContextId() const
Get the DstContextId.
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
#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
Time Minutes(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1314
NodeContainer nodes
void SendOnePacket(Ptr< LrWpanPhy > sender, Ptr< LrWpanPhy > receiver)
Send one packet.
void(* Time)(Time oldValue, Time newValue)
TracedValue callback signature for Time.
Definition: nstime.h:839
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 SixlowpanIphcStatefulTestSuite g_sixlowpanIphcStatefulTestSuite
Static variable for test initialization.
Structure to hold the Rx/Tx packets.