Skip to content

Commit 5bbfffd

Browse files
jepenven-silabspull[bot]
authored andcommitted
[Group] Added Unit Test for Group Peer counter (#15349)
* Added Unit Test for Group Peer counter * Apply comments * Last comments * Remove compilation error
1 parent 0abcac7 commit 5bbfffd

File tree

4 files changed

+238
-11
lines changed

4 files changed

+238
-11
lines changed

src/transport/PeerMessageCounter.h

+13-6
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,9 @@ class PeerMessageCounter
113113

114114
// 2. Counter Window check
115115
uint32_t offset = mSynced.mMaxCounter - counter;
116-
if (offset < CHIP_CONFIG_MESSAGE_COUNTER_WINDOW_SIZE)
116+
if (offset <= CHIP_CONFIG_MESSAGE_COUNTER_WINDOW_SIZE)
117117
{
118-
if ((offset == 0) || mSynced.mWindow.test(offset))
118+
if ((offset == 0) || mSynced.mWindow.test(offset - 1))
119119
{
120120
return CHIP_ERROR_DUPLICATE_MESSAGE_RECEIVED; // duplicated, in window
121121
}
@@ -199,9 +199,16 @@ class PeerMessageCounter
199199
void CommitWithRollOver(uint32_t counter)
200200
{
201201
uint32_t offset = mSynced.mMaxCounter - counter;
202-
if (offset < CHIP_CONFIG_MESSAGE_COUNTER_WINDOW_SIZE)
202+
if (offset <= CHIP_CONFIG_MESSAGE_COUNTER_WINDOW_SIZE)
203203
{
204-
mSynced.mWindow.set(offset);
204+
205+
// Make sure that offset is never 0.
206+
// This should not happen but we cannot guarantee it
207+
// since this class doesn't own the call to this function
208+
if (offset != 0)
209+
{
210+
mSynced.mWindow.set(offset - 1);
211+
}
205212
}
206213
else
207214
{
@@ -215,8 +222,8 @@ class PeerMessageCounter
215222
else
216223
{
217224
mSynced.mWindow <<= shift;
225+
mSynced.mWindow.set(shift - 1);
218226
}
219-
mSynced.mWindow.set(0);
220227
}
221228
}
222229

@@ -279,7 +286,7 @@ class PeerMessageCounter
279286
{
280287
/*
281288
* Past <-- --> Future
282-
* MaxCounter
289+
* MaxCounter - 1
283290
* |
284291
* v
285292
* | <-- mWindow -->|

src/transport/tests/BUILD.gn

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ chip_test_suite("tests") {
2626
"TestGroupMessageCounter.cpp",
2727
"TestPairingSession.cpp",
2828
"TestPeerConnections.cpp",
29+
"TestPeerMessageCounter.cpp",
2930
"TestSecureSession.cpp",
3031
"TestSessionManager.cpp",
3132
]

src/transport/tests/TestGroupMessageCounter.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -326,8 +326,8 @@ void ReorderPeerRemovalTest(nlTestSuite * inSuite, void * inContext)
326326
NL_TEST_ASSERT(inSuite, mGroupPeerMsgCounter.GetNodeIdAt(1, 0, false) == 9);
327327
}
328328

329-
#if !__ZEPHYR__
330-
329+
// Disabled for devices with fabric count lower than 12
330+
#if CHIP_CONFIG_MAX_FABRICS > 12
331331
void ReorderFabricRemovalTest(nlTestSuite * inSuite, void * inContext)
332332
{
333333
CHIP_ERROR err = CHIP_NO_ERROR;
@@ -380,7 +380,7 @@ void ReorderFabricRemovalTest(nlTestSuite * inSuite, void * inContext)
380380
err = counter->VerifyOrTrustFirst(4756, true);
381381
NL_TEST_ASSERT(inSuite, err != CHIP_NO_ERROR);
382382
}
383-
#endif // !__ZEPHYR__
383+
#endif // CHIP_CONFIG_MAX_FABRICS > 12
384384
void GroupMessageCounterTest(nlTestSuite * inSuite, void * inContext)
385385
{
386386

@@ -449,9 +449,9 @@ const nlTest sTests[] =
449449
NL_TEST_DEF("Counter Rollover", CounterCommitRolloverTest),
450450
NL_TEST_DEF("Counter Trust first", CounterTrustFirstTest),
451451
NL_TEST_DEF("Reorder Peer removal", ReorderPeerRemovalTest),
452-
#if !__ZEPHYR__
452+
#if CHIP_CONFIG_MAX_FABRICS > 12
453453
NL_TEST_DEF("Reorder Fabric Removal", ReorderFabricRemovalTest),
454-
#endif
454+
#endif // CHIP_CONFIG_MAX_FABRICS > 12
455455
NL_TEST_DEF("Group Message Counter", GroupMessageCounterTest),
456456
NL_TEST_SENTINEL()
457457
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,219 @@
1+
/*
2+
*
3+
* Copyright (c) 2020-2021 Project CHIP Authors
4+
* All rights reserved.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
/**
20+
* @file
21+
* This file implements unit tests for the SessionManager implementation.
22+
*/
23+
24+
#include <lib/support/UnitTestRegistration.h>
25+
#include <transport/PeerMessageCounter.h>
26+
27+
#include <errno.h>
28+
#include <nlbyteorder.h>
29+
#include <nlunit-test.h>
30+
#include <vector>
31+
32+
namespace {
33+
34+
using namespace chip;
35+
36+
static uint32_t counterValuesArray[] = { 0, 10, 0x7FFFFFFF, 0x80000000, 0x80000001, 0x80000002, 0xFFFFFFF0, 0xFFFFFFFF };
37+
38+
void GroupRollOverTest(nlTestSuite * inSuite, void * inContext)
39+
{
40+
for (auto n : counterValuesArray)
41+
{
42+
for (uint32_t k = 1; k <= 2 * CHIP_CONFIG_MESSAGE_COUNTER_WINDOW_SIZE; k++)
43+
{
44+
chip::Transport::PeerMessageCounter counter;
45+
NL_TEST_ASSERT(inSuite, counter.VerifyOrTrustFirst(n, true) == CHIP_NO_ERROR);
46+
47+
counter.CommitWithRollOver(n);
48+
49+
// 1. A counter value of N + k comes in, we detect it as valid and commit it.
50+
NL_TEST_ASSERT(inSuite, counter.VerifyOrTrustFirst(n + k, true) == CHIP_NO_ERROR);
51+
counter.CommitWithRollOver(n + k);
52+
53+
// 2. A counter value of N comes in, we detect it as duplicate.
54+
if (k <= CHIP_CONFIG_MESSAGE_COUNTER_WINDOW_SIZE)
55+
{
56+
NL_TEST_ASSERT(inSuite, counter.VerifyOrTrustFirst(n, true) == CHIP_ERROR_DUPLICATE_MESSAGE_RECEIVED);
57+
}
58+
else
59+
{
60+
NL_TEST_ASSERT(inSuite, counter.VerifyOrTrustFirst(n, true) == CHIP_ERROR_MESSAGE_COUNTER_OUT_OF_WINDOW);
61+
}
62+
63+
// 3. A counter value between N - CHIP_CONFIG_MESSAGE_COUNTER_WINDOW_SIZE and N + k -
64+
// CHIP_CONFIG_MESSAGE_COUNTER_WINDOW_SIZE
65+
// (but not including N + k - CHIP_CONFIG_MESSAGE_COUNTER_WINDOW_SIZE) comes in, we treat it as duplicate.
66+
for (uint32_t i = n - CHIP_CONFIG_MESSAGE_COUNTER_WINDOW_SIZE; i != (n + k - CHIP_CONFIG_MESSAGE_COUNTER_WINDOW_SIZE);
67+
i++)
68+
{
69+
NL_TEST_ASSERT(inSuite, counter.VerifyOrTrustFirst(i, true) != CHIP_NO_ERROR);
70+
}
71+
72+
// 4. A counter value of N + k - CHIP_CONFIG_MESSAGE_COUNTER_WINDOW_SIZE comes in, is treated as valid.
73+
if (k != CHIP_CONFIG_MESSAGE_COUNTER_WINDOW_SIZE)
74+
{
75+
NL_TEST_ASSERT(
76+
inSuite, counter.VerifyOrTrustFirst((n + k - CHIP_CONFIG_MESSAGE_COUNTER_WINDOW_SIZE), true) == CHIP_NO_ERROR);
77+
}
78+
else
79+
{
80+
NL_TEST_ASSERT(
81+
inSuite, counter.VerifyOrTrustFirst((n + k - CHIP_CONFIG_MESSAGE_COUNTER_WINDOW_SIZE), true) != CHIP_NO_ERROR);
82+
}
83+
}
84+
}
85+
}
86+
87+
void GroupBackTrackTest(nlTestSuite * inSuite, void * inContext)
88+
{
89+
for (auto n : counterValuesArray)
90+
{
91+
chip::Transport::PeerMessageCounter counter;
92+
NL_TEST_ASSERT(inSuite, counter.VerifyOrTrustFirst(n, true) == CHIP_NO_ERROR);
93+
94+
counter.CommitWithRollOver(n);
95+
// 1. Some set of values N - k come in, for 0 < k < CHIP_CONFIG_MESSAGE_COUNTER_WINDOW_SIZE.
96+
// All of those should be considered valid and committed.
97+
for (uint32_t k = 1; k * k < CHIP_CONFIG_MESSAGE_COUNTER_WINDOW_SIZE; k++)
98+
{
99+
NL_TEST_ASSERT(inSuite, counter.VerifyOrTrustFirst(n - (k * k), true) == CHIP_NO_ERROR);
100+
counter.CommitWithRollOver(n - (k * k));
101+
}
102+
// 2. Counter value N + 3 comes in
103+
NL_TEST_ASSERT(inSuite, counter.VerifyOrTrustFirst(n + 3, true) == CHIP_NO_ERROR);
104+
counter.CommitWithRollOver(n + 3);
105+
106+
// 3. The same set of values N - k come in as in step (1) and are all considered duplicates/out of window.
107+
for (uint32_t k = 1; k * k < CHIP_CONFIG_MESSAGE_COUNTER_WINDOW_SIZE; k++)
108+
{
109+
NL_TEST_ASSERT(inSuite, counter.VerifyOrTrustFirst(n - (k * k), true) != CHIP_NO_ERROR);
110+
}
111+
112+
// 4. The values that were not in the set in step (a) (but are at least N + 3 - CHIP_CONFIG_MESSAGE_COUNTER_WINDOW_SIZE)
113+
// come in, and all are treated as allowed.
114+
for (uint32_t k = n + 3 - CHIP_CONFIG_MESSAGE_COUNTER_WINDOW_SIZE; k != n + 3; ++k)
115+
{
116+
if (n - k == 0 || n - k == 1 || n - k == 4 || n - k == 9 || n - k == 16 || n - k == 25)
117+
{
118+
continue;
119+
}
120+
NL_TEST_ASSERT(inSuite, counter.VerifyOrTrustFirst(k, true) == CHIP_NO_ERROR);
121+
counter.CommitWithRollOver(k);
122+
}
123+
}
124+
}
125+
126+
void GroupBigLeapTest(nlTestSuite * inSuite, void * inContext)
127+
{
128+
for (auto n : counterValuesArray)
129+
{
130+
for (uint32_t k = (static_cast<uint32_t>(1 << 31) - 5); k <= (static_cast<uint32_t>(1 << 31) - 1); k++)
131+
{
132+
chip::Transport::PeerMessageCounter counter;
133+
NL_TEST_ASSERT(inSuite, counter.VerifyOrTrustFirst(n, true) == CHIP_NO_ERROR);
134+
135+
counter.CommitWithRollOver(n);
136+
137+
// 1. A counter value of N + k comes in, we detect it as valid and commit it.
138+
NL_TEST_ASSERT(inSuite, counter.VerifyOrTrustFirst(n + k, true) == CHIP_NO_ERROR);
139+
counter.CommitWithRollOver(n + k);
140+
141+
// 2. A counter value of N comes in, we detect it as duplicate.
142+
NL_TEST_ASSERT(inSuite, counter.VerifyOrTrustFirst(n, true) != CHIP_NO_ERROR);
143+
144+
// 3. A counter value between N - CHIP_CONFIG_MESSAGE_COUNTER_WINDOW_SIZE and N + k -
145+
// CHIP_CONFIG_MESSAGE_COUNTER_WINDOW_SIZE
146+
// (but not including N + k - CHIP_CONFIG_MESSAGE_COUNTER_WINDOW_SIZE) comes in, we treat it as duplicate.
147+
148+
// Only test some values to save processing time
149+
std::vector<uint32_t> testValues;
150+
testValues.push_back(static_cast<uint32_t>(n + (k / 32) - CHIP_CONFIG_MESSAGE_COUNTER_WINDOW_SIZE));
151+
testValues.push_back(static_cast<uint32_t>(n + (k / 16) - CHIP_CONFIG_MESSAGE_COUNTER_WINDOW_SIZE));
152+
testValues.push_back(static_cast<uint32_t>(n + (k / 8) - CHIP_CONFIG_MESSAGE_COUNTER_WINDOW_SIZE));
153+
testValues.push_back(static_cast<uint32_t>(n + (k / 4) - CHIP_CONFIG_MESSAGE_COUNTER_WINDOW_SIZE));
154+
testValues.push_back(static_cast<uint32_t>(n + (k / 3) - CHIP_CONFIG_MESSAGE_COUNTER_WINDOW_SIZE));
155+
testValues.push_back(static_cast<uint32_t>(n + (k / 2) - CHIP_CONFIG_MESSAGE_COUNTER_WINDOW_SIZE));
156+
testValues.push_back(static_cast<uint32_t>(n + k - CHIP_CONFIG_MESSAGE_COUNTER_WINDOW_SIZE - 1));
157+
158+
// Will be inside the valid window of counter + (2^31 -1)
159+
NL_TEST_ASSERT(inSuite, counter.VerifyOrTrustFirst(n - CHIP_CONFIG_MESSAGE_COUNTER_WINDOW_SIZE, true) == CHIP_NO_ERROR);
160+
161+
for (auto it : testValues)
162+
{
163+
NL_TEST_ASSERT(inSuite, counter.VerifyOrTrustFirst(it, true) != CHIP_NO_ERROR);
164+
}
165+
166+
// 4. A counter value of N + k - CHIP_CONFIG_MESSAGE_COUNTER_WINDOW_SIZE comes in, is treated as valid.
167+
NL_TEST_ASSERT(inSuite,
168+
counter.VerifyOrTrustFirst((n + k - CHIP_CONFIG_MESSAGE_COUNTER_WINDOW_SIZE), true) == CHIP_NO_ERROR);
169+
}
170+
}
171+
}
172+
173+
void GroupOutOfWindow(nlTestSuite * inSuite, void * inContext)
174+
{
175+
for (auto n : counterValuesArray)
176+
{
177+
for (uint32_t k = (static_cast<uint32_t>(1 << 31)); k <= (static_cast<uint32_t>(1 << 31) + 2); k++)
178+
{
179+
chip::Transport::PeerMessageCounter counter;
180+
NL_TEST_ASSERT(inSuite, counter.VerifyOrTrustFirst(n, true) == CHIP_NO_ERROR);
181+
182+
counter.CommitWithRollOver(n);
183+
184+
// 1. A counter value of N + k comes in, we detect it as out of window
185+
NL_TEST_ASSERT(inSuite, counter.VerifyOrTrustFirst(n + k, true) == CHIP_ERROR_MESSAGE_COUNTER_OUT_OF_WINDOW);
186+
}
187+
}
188+
}
189+
190+
} // namespace
191+
192+
/**
193+
* Test Suite that lists all the test functions.
194+
*/
195+
// clang-format off
196+
const nlTest sTests[] =
197+
{
198+
NL_TEST_DEF("Group Roll over Test", GroupRollOverTest),
199+
NL_TEST_DEF("Group Backtrack Test", GroupBackTrackTest),
200+
NL_TEST_DEF("Group All value test", GroupBigLeapTest),
201+
NL_TEST_DEF("Group Out of Window Test", GroupOutOfWindow),
202+
NL_TEST_SENTINEL()
203+
};
204+
// clang-format on
205+
206+
/**
207+
* Main
208+
*/
209+
int TestPeerMessageCounter()
210+
{
211+
// Run test suit against one context
212+
213+
nlTestSuite theSuite = { "Transport-TestPeerMessageCounter", &sTests[0], nullptr, nullptr };
214+
nlTestRunner(&theSuite, nullptr);
215+
216+
return (nlTestRunnerStats(&theSuite));
217+
}
218+
219+
CHIP_REGISTER_TEST_SUITE(TestPeerMessageCounter);

0 commit comments

Comments
 (0)