A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
power-save-test.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2022 Universita' degli Studi di Napoli Federico II
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Davide Magrin <davide@magr.in>
7 */
8
9#include "ns3/assert.h"
10#include "ns3/header-serialization-test.h"
11#include "ns3/log.h"
12#include "ns3/test.h"
13#include "ns3/tim.h"
14
15#include <algorithm>
16#include <iterator>
17#include <list>
18#include <sstream>
19#include <vector>
20
21using namespace ns3;
22
23NS_LOG_COMPONENT_DEFINE("PowerSaveTest");
24
25/**
26 * \ingroup wifi-test
27 * \ingroup tests
28 *
29 * \brief Test TIM Information element serialization and deserialization
30 */
32{
33 public:
34 /**
35 * \brief Constructor
36 */
38
39 void DoRun() override;
40 /**
41 * Reset the passed TIM to have the provided parameters.
42 *
43 * \param tim the TIM element to set
44 * \param dtimCount the DTIM count value
45 * \param dtimPeriod the DTIM period value
46 * \param multicastPending whether group addressed frames are queued
47 * \param aidValues the AID values to set
48 */
49 void SetTim(Tim& tim,
50 uint8_t dtimCount,
51 uint8_t dtimPeriod,
52 bool multicastPending,
53 const std::list<uint16_t>& aidValues);
54
55 /**
56 * Test that the Bitmap Control and the Partial Virtual Bitmap
57 * fields of the provided TIM match the passed bufferContents.
58 *
59 * \param tim the provided TIM
60 * \param bufferContents the expected content of the buffer
61 */
62 void CheckSerializationAgainstBuffer(Tim& tim, const std::vector<uint8_t>& bufferContents);
63
64 /**
65 * Test that the GetAidSet() method return the expected set of AID values.
66 *
67 * \param tim the TIM element
68 * \param aid the AID value passed to GetAidSet()
69 * \param expectedSet the expected set of AID values returned by GetAidSet()
70 */
71 void CheckAidSet(const Tim& tim, uint16_t aid, const std::set<uint16_t>& expectedSet);
72};
73
75 : HeaderSerializationTestCase("Test for the TIM Information Element implementation")
76{
77}
78
79void
81 uint8_t dtimCount,
82 uint8_t dtimPeriod,
83 bool multicastPending,
84 const std::list<uint16_t>& aidValues)
85{
86 tim = Tim();
87 tim.m_dtimCount = dtimCount;
88 tim.m_dtimPeriod = dtimPeriod;
89 tim.m_hasMulticastPending = multicastPending;
90 tim.AddAid(aidValues.begin(), aidValues.end());
91}
92
93void
95 Tim& tim,
96 const std::vector<uint8_t>& bufferContents)
97{
98 // Serialize the TIM
99 Buffer buffer;
100 buffer.AddAtStart(tim.GetSerializedSize());
101 tim.Serialize(buffer.Begin());
102
103 // Check the two Buffer instances
104 Buffer::Iterator bufferIterator = buffer.Begin();
105 for (uint32_t j = 0; j < buffer.GetSize(); j++)
106 {
107 // We skip the first four bytes, since they contain known information
108 if (j > 3)
109 {
110 NS_TEST_EXPECT_MSG_EQ(bufferIterator.ReadU8(),
111 bufferContents.at(j - 4),
112 "Serialization is different than provided known serialization");
113 }
114 else
115 {
116 // Advance the serialized buffer, which also contains
117 // the Element ID, Length, DTIM Count, DTIM Period fields
118 bufferIterator.ReadU8();
119 }
120 }
121}
122
123void
125 uint16_t aid,
126 const std::set<uint16_t>& expectedSet)
127{
128 auto ret = tim.GetAidSet(aid);
129
130 {
131 std::vector<uint16_t> diff;
132
133 // Expected set minus returned set provides expected elements that are not returned
134 std::set_difference(expectedSet.cbegin(),
135 expectedSet.cend(),
136 ret.cbegin(),
137 ret.cend(),
138 std::back_inserter(diff));
139
140 std::stringstream ss;
141 std::copy(diff.cbegin(), diff.cend(), std::ostream_iterator<uint16_t>(ss, " "));
142
143 NS_TEST_EXPECT_MSG_EQ(diff.size(),
144 0,
145 "Expected elements not returned by GetAidSet(): " << ss.str());
146 }
147 {
148 std::vector<uint16_t> diff;
149
150 // Returned set minus expected set provides returned elements that are not expected
151 std::set_difference(ret.cbegin(),
152 ret.cend(),
153 expectedSet.cbegin(),
154 expectedSet.cend(),
155 std::back_inserter(diff));
156
157 std::stringstream ss;
158 std::copy(diff.cbegin(), diff.cend(), std::ostream_iterator<uint16_t>(ss, " "));
159
160 NS_TEST_EXPECT_MSG_EQ(diff.size(),
161 0,
162 "Returned elements not expected by GetAidSet(): " << ss.str());
163 }
164}
165
166void
168{
169 Tim tim;
170
171 // The first three examples from 802.11-2020, Annex L
172 //
173 // 1. No group addressed MSDUs, but there is traffic for STAs with AID 2 and AID 7
174 SetTim(tim, 0, 3, false, {2, 7});
176 CheckSerializationAgainstBuffer(tim, {0b00000000, 0b10000100});
177 CheckAidSet(tim, 0, {2, 7});
178 CheckAidSet(tim, 1, {2, 7});
179 CheckAidSet(tim, 2, {7});
180 CheckAidSet(tim, 7, {});
181 //
182 // 2. There are group addressed MSDUs, DTIM count = 0, the nodes
183 // with AID 2, 7, 22, and 24 have data buffered in the AP
184 SetTim(tim, 0, 3, true, {2, 7, 22, 24});
187 {
188 0b00000001,
189 // NOTE The following byte is different from the example
190 // in the standard. This is because the example sets the
191 // AID 0 bit in the partial virtual bitmap to 1. Our code
192 // and the example code provided in the Annex, instead, do
193 // not set this bit. Relevant Note from 802.11-2020,
194 // Section 9.4.2.5.1: "The bit numbered 0 in the traffic
195 // indication virtual bitmap need not be included in the
196 // Partial Virtual Bitmap field even if that bit is set."
197 0b10000100,
198 0b00000000,
199 0b01000000,
200 0b00000001,
201 });
202 CheckAidSet(tim, 0, {2, 7, 22, 24});
203 CheckAidSet(tim, 2, {7, 22, 24});
204 CheckAidSet(tim, 7, {22, 24});
205 CheckAidSet(tim, 22, {24});
206 CheckAidSet(tim, 24, {});
207 //
208 // 3. There are group addressed MSDUs, DTIM count = 0, only the node
209 // with AID 24 has data buffered in the AP
210 SetTim(tim, 0, 3, true, {24});
212 CheckSerializationAgainstBuffer(tim, {0b00000011, 0b00000000, 0b00000001});
213
214 // Other arbitrary examples just to make sure
215 // Serialization -> Deserialization -> Serialization works
216 SetTim(tim, 0, 3, false, {2000});
218 SetTim(tim, 1, 3, true, {1, 134});
220 SetTim(tim, 1, 3, false, {1, 2});
222
223 // Edge cases
224 //
225 // What if there is group addressed data only?
226 //
227 // In this case, we should still have an empty byte in the Partial Virtual Bitmap.
228 // From 802.11-2020: in the event that all bits other than bit 0 in the traffic indication
229 // virtual bitmap are 0, the Partial Virtual Bitmap field is encoded as a single octet
230 // equal to 0, the Bitmap Offset subfield is 0, and the Length field is 4.
231 SetTim(tim, 0, 3, true, {});
233 CheckSerializationAgainstBuffer(tim, {0b00000001, 0b00000000});
234 NS_TEST_EXPECT_MSG_EQ(tim.GetSerializedSize() - 2, 4, "Unexpected TIM Length");
235 //
236 // What if there is no group addressed data and no unicast data?
237 //
238 // From 802.11-2020: When the TIM is carried in a non-S1G PPDU, in the event that all bits
239 // other than bit 0 in the traffic indication virtual bitmap are 0, the Partial Virtual Bitmap
240 // field is encoded as a single octet equal to 0, the Bitmap Offset subfield is 0, and the
241 // Length field is 4.
242 SetTim(tim, 0, 3, false, {});
244 CheckSerializationAgainstBuffer(tim, {0b00000000, 0b00000000});
245 NS_TEST_EXPECT_MSG_EQ(tim.GetSerializedSize() - 2, 4, "Unexpected TIM Length");
246}
247
248/**
249 * \ingroup wifi-test
250 * \ingroup tests
251 *
252 * \brief Power Save Test Suite
253 */
255{
256 public:
258};
259
261 : TestSuite("wifi-power-save", Type::UNIT)
262{
263 AddTestCase(new TimInformationElementTest, TestCase::Duration::QUICK);
264}
265
266static PowerSaveTestSuite g_powerSaveTestSuite; ///< the test suite
Power Save Test Suite.
Test TIM Information element serialization and deserialization.
TimInformationElementTest()
Constructor.
void SetTim(Tim &tim, uint8_t dtimCount, uint8_t dtimPeriod, bool multicastPending, const std::list< uint16_t > &aidValues)
Reset the passed TIM to have the provided parameters.
void DoRun() override
Implementation to actually run this TestCase.
void CheckSerializationAgainstBuffer(Tim &tim, const std::vector< uint8_t > &bufferContents)
Test that the Bitmap Control and the Partial Virtual Bitmap fields of the provided TIM match the pass...
void CheckAidSet(const Tim &tim, uint16_t aid, const std::set< uint16_t > &expectedSet)
Test that the GetAidSet() method return the expected set of AID values.
iterator in a Buffer instance
Definition buffer.h:89
automatically resized byte buffer
Definition buffer.h:83
uint32_t GetSize() const
Definition buffer.h:1057
void AddAtStart(uint32_t start)
Definition buffer.cc:303
Buffer::Iterator Begin() const
Definition buffer.h:1063
Subclass of TestCase class adding the ability to test the serialization and deserialization of a Head...
void TestHeaderSerialization(const T &hdr, Args &&... args)
Serialize the given header in a buffer, then create a new header by deserializing from the buffer and...
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
The Traffic Indication Map Information Element.
Definition tim.h:29
uint8_t m_dtimPeriod
The DTIM Period field.
Definition tim.h:86
uint8_t m_dtimCount
The DTIM Count field.
Definition tim.h:85
void AddAid(uint16_t aid)
Add the provided AID value to the list contained in the Virtual Bitmap.
Definition tim.cc:38
std::set< uint16_t > GetAidSet(uint16_t aid=0) const
Return the AID values, greater than the given AID value, whose corresponding bits are set in the virt...
Definition tim.cc:52
bool m_hasMulticastPending
Whether there is Multicast / Broadcast data.
Definition tim.h:87
uint16_t GetSerializedSize() const
Get the size of the serialized IE including Element ID and length fields (for every element this IE i...
Buffer::Iterator Serialize(Buffer::Iterator i) const
Serialize entire IE including Element ID and length fields.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#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
Every class exported by the ns3 library is enclosed in the ns3 namespace.
static PowerSaveTestSuite g_powerSaveTestSuite
the test suite