A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Documentation ▼
Installation
Manual
Models
Contributing
Wiki
Development ▼
API Docs
Issue Tracker
Merge Requests
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
21
using namespace
ns3
;
22
23
NS_LOG_COMPONENT_DEFINE
(
"PowerSaveTest"
);
24
25
/**
26
* @ingroup wifi-test
27
* @ingroup tests
28
*
29
* @brief Test TIM Information element serialization and deserialization
30
*/
31
class
TimInformationElementTest
:
public
HeaderSerializationTestCase
32
{
33
public
:
34
/**
35
* @brief Constructor
36
*/
37
TimInformationElementTest
();
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
74
TimInformationElementTest::TimInformationElementTest
()
75
:
HeaderSerializationTestCase
(
"Test for the TIM Information Element implementation"
)
76
{
77
}
78
79
void
80
TimInformationElementTest::SetTim
(
Tim
&
tim
,
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
93
void
94
TimInformationElementTest::CheckSerializationAgainstBuffer
(
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
123
void
124
TimInformationElementTest::CheckAidSet
(
const
Tim
&
tim
,
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
166
void
167
TimInformationElementTest::DoRun
()
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});
175
TestHeaderSerialization
(
tim
);
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});
185
TestHeaderSerialization
(
tim
);
186
CheckSerializationAgainstBuffer
(
tim
,
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});
211
TestHeaderSerialization
(
tim
);
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});
217
TestHeaderSerialization
(
tim
);
218
SetTim
(
tim
, 1, 3,
true
, {1, 134});
219
TestHeaderSerialization
(
tim
);
220
SetTim
(
tim
, 1, 3,
false
, {1, 2});
221
TestHeaderSerialization
(
tim
);
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
, {});
232
TestHeaderSerialization
(
tim
);
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
, {});
243
TestHeaderSerialization
(
tim
);
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
*/
254
class
PowerSaveTestSuite
:
public
TestSuite
255
{
256
public
:
257
PowerSaveTestSuite
();
258
};
259
260
PowerSaveTestSuite::PowerSaveTestSuite
()
261
:
TestSuite
(
"wifi-power-save"
,
Type
::UNIT)
262
{
263
AddTestCase
(
new
TimInformationElementTest
, TestCase::Duration::QUICK);
264
}
265
266
static
PowerSaveTestSuite
g_powerSaveTestSuite
;
///< the test suite
PowerSaveTestSuite
Power Save Test Suite.
Definition
power-save-test.cc:255
PowerSaveTestSuite::PowerSaveTestSuite
PowerSaveTestSuite()
Definition
power-save-test.cc:260
TimInformationElementTest
Test TIM Information element serialization and deserialization.
Definition
power-save-test.cc:32
TimInformationElementTest::TimInformationElementTest
TimInformationElementTest()
Constructor.
Definition
power-save-test.cc:74
TimInformationElementTest::SetTim
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.
Definition
power-save-test.cc:80
TimInformationElementTest::DoRun
void DoRun() override
Implementation to actually run this TestCase.
Definition
power-save-test.cc:167
TimInformationElementTest::CheckSerializationAgainstBuffer
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...
Definition
power-save-test.cc:94
TimInformationElementTest::CheckAidSet
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.
Definition
power-save-test.cc:124
ns3::Buffer::Iterator
iterator in a Buffer instance
Definition
buffer.h:89
ns3::Buffer
automatically resized byte buffer
Definition
buffer.h:83
ns3::Buffer::GetSize
uint32_t GetSize() const
Definition
buffer.h:1057
ns3::Buffer::AddAtStart
void AddAtStart(uint32_t start)
Definition
buffer.cc:303
ns3::Buffer::Begin
Buffer::Iterator Begin() const
Definition
buffer.h:1063
ns3::HeaderSerializationTestCase
Subclass of TestCase class adding the ability to test the serialization and deserialization of a Head...
Definition
header-serialization-test.h:24
ns3::HeaderSerializationTestCase::TestHeaderSerialization
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...
Definition
header-serialization-test.h:62
ns3::TestCase::AddTestCase
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition
test.cc:292
ns3::TestSuite
A suite of tests to run.
Definition
test.h:1267
ns3::TestSuite::Type
Type
Type of test.
Definition
test.h:1274
ns3::Tim
The Traffic Indication Map Information Element.
Definition
tim.h:29
uint32_t
NS_LOG_COMPONENT_DEFINE
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition
log.h:191
ns3::Create
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition
ptr.h:436
NS_TEST_EXPECT_MSG_EQ
#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
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
g_powerSaveTestSuite
static PowerSaveTestSuite g_powerSaveTestSuite
the test suite
Definition
power-save-test.cc:266
src
wifi
test
power-save-test.cc
Generated on Mon Dec 15 2025 15:22:09 for ns-3 by
1.9.8