Skip to content

Commit 3466347

Browse files
turonpull[bot]
authored andcommitted
[session][test] Extend TestSessionManagerDispatch with group test (#22769)
* [session][test] Add group message test vector. * [session][test] Add short payload secure unicast message test vector. * [restyle] * [test] Resolve review comments. Decouple TestInjectGroupSession from nl_unit. * [test] Fix group name string too large.
1 parent aa1b62e commit 3466347

File tree

1 file changed

+123
-9
lines changed

1 file changed

+123
-9
lines changed

src/transport/tests/TestSessionManagerDispatch.cpp

+123-9
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#define CHIP_ENABLE_TEST_ENCRYPTED_BUFFER_API // Up here in case some other header
2525
// includes SessionManager.h indirectly
2626

27+
#include <credentials/GroupDataProviderImpl.h>
2728
#include <credentials/PersistentStorageOpCertStore.h>
2829
#include <crypto/PersistentStorageOperationalKeystore.h>
2930
#include <lib/core/CHIPCore.h>
@@ -49,6 +50,12 @@ using namespace chip;
4950
using namespace chip::Inet;
5051
using namespace chip::Transport;
5152
using namespace chip::Test;
53+
using namespace chip::Credentials;
54+
55+
using GroupInfo = GroupDataProvider::GroupInfo;
56+
using GroupKey = GroupDataProvider::GroupKey;
57+
using KeySet = GroupDataProvider::KeySet;
58+
using SecurityPolicy = GroupDataProvider::SecurityPolicy;
5259

5360
using TestContext = chip::Test::LoopbackTransportManager;
5461

@@ -80,12 +87,13 @@ struct MessageTestEntry
8087

8188
uint16_t sessionId;
8289
NodeId peerNodeId;
83-
FabricIndex fabricIndex;
90+
GroupId groupId;
91+
NodeId sourceNodeId;
8492
};
8593

8694
struct MessageTestEntry theMessageTestVector[] = {
8795
{
88-
.name = "secure pase message",
96+
.name = "secure pase message (no payload)",
8997
.peerAddr = "::1",
9098

9199
.payload = "",
@@ -100,19 +108,80 @@ struct MessageTestEntry theMessageTestVector[] = {
100108
.encryptedLength = 30,
101109
.privacyLength = 30,
102110

111+
// TODO(#22830): unicast message tests must use test key currently
103112
.encryptKey = "\x5e\xde\xd2\x44\xe5\x53\x2b\x3c\xdc\x23\x40\x9d\xba\xd0\x52\xd2",
104113

105114
.nonce = "\x00\x39\x30\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
106115

107-
.sessionId = 0x0bb8, // 3000
108-
.peerNodeId = 0x0000000000000000ULL,
109-
.fabricIndex = 1,
116+
.sessionId = 0x0bb8, // 3000
117+
.peerNodeId = 0x0000000000000000ULL,
118+
},
119+
{
120+
.name = "secure pase message (short payload)",
121+
.peerAddr = "::1",
122+
123+
.payload = "\x11\x22\x33\x44\x55",
124+
.plain = "\x00\xb8\x0b\x00\x39\x30\x00\x00\x05\x64\xee\x0e\x20\x7d\x11\x22\x33\x44\x55",
125+
.encrypted = "\x00\xb8\x0b\x00\x39\x30\x00\x00\x5a\x98\x9a\xe4\x2e\x8d\x0f\x7f\x88\x5d\xfb"
126+
"\x2f\xaa\x89\x49\xcf\x73\x0a\x57\x28\xe0\x35\x46\x10\xa0\xc4\xa7", // Includes MIC
127+
.privacy = "\x00\xb8\x0b\x00\x39\x30\x00\x00\x5a\x98\x9a\xe4\x2e\x8d\x0f\x7f\x88\x5d\xfb"
128+
"\x2f\xaa\x89\x49\xcf\x73\x0a\x57\x28\xe0\x35\x46\x10\xa0\xc4\xa7", // Includes MIC
129+
130+
.payloadLength = 5,
131+
.plainLength = 19,
132+
.encryptedLength = 35,
133+
.privacyLength = 35,
134+
135+
// TODO(#22830): unicast message tests must use test key currently
136+
.encryptKey = "\x5e\xde\xd2\x44\xe5\x53\x2b\x3c\xdc\x23\x40\x9d\xba\xd0\x52\xd2",
137+
138+
.nonce = "\x00\x39\x30\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
139+
140+
.sessionId = 0x0bb8, // 3000
141+
.peerNodeId = 0x0000000000000000ULL,
142+
},
143+
{
144+
.name = "secure group message (no privacy)",
145+
.peerAddr = "::1",
146+
147+
.payload = "",
148+
149+
.plain = "\06\x7d\xdb\x01\x78\x56\x34\x12\x01\x00\x00\x00\x00\x00\x00\x00\x02\x00\x01\x64\xee\x0e\x20\x7d",
150+
.encrypted = "\x06\x7d\xdb\x01\x78\x56\x34\x12\x01\x00\x00\x00\x00\x00\x00\x00\x02\x00\x65\xc7\x67\xbc\x6c\xda"
151+
"\x01\x06\xc9\x80\x13\x23\x90\x0e\x9b\x3c\xe6\xd4\xbb\x03\x27\xd6", // Includes MIC
152+
.privacy = "\x06\x7d\xdb\x01\x78\x56\x34\x12\x01\x00\x00\x00\x00\x00\x00\x00\x02\x00\x65\xc7\x67\xbc\x6c\xda"
153+
"\x01\x06\xc9\x80\x13\x23\x90\x0e\x9b\x3c\xe6\xd4\xbb\x03\x27\xd6", // Includes MIC
154+
155+
.payloadLength = 0,
156+
.plainLength = 24,
157+
.encryptedLength = 40,
158+
.privacyLength = 40,
159+
160+
.encryptKey = "\xca\x92\xd7\xa0\x94\x2d\x1a\x51\x1a\x0e\x26\xad\x07\x4f\x4c\x2f",
161+
.privacyKey = "\xbf\xe9\xda\x01\x6a\x76\x53\x65\xf2\xdd\x97\xa9\xf9\x39\xe4\x25",
162+
.epochKey = "\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf",
163+
164+
.nonce = "\x01\x78\x56\x34\x12\x01\x00\x00\x00\x00\x00\x00\x00",
165+
.privacyNonce = "\xdb\x7d\x23\x90\x0e\x9b\x3c\xe6\xd4\xbb\x03\x27\xd6",
166+
167+
.sessionId = 0xdb7d, // 56189
168+
.peerNodeId = 0x0000000000000000ULL,
169+
.groupId = 2,
170+
.sourceNodeId = 0x0000000000000002ULL,
110171
},
111172
};
112173

113174
const uint16_t theMessageTestVectorLength = sizeof(theMessageTestVector) / sizeof(theMessageTestVector[0]);
114175

115176
// Just enough init to replace a ton of boilerplate
177+
constexpr FabricIndex kFabricIndex = kMinValidFabricIndex;
178+
constexpr size_t kGroupIndex = 0;
179+
180+
constexpr uint16_t kMaxGroupsPerFabric = 5;
181+
constexpr uint16_t kMaxGroupKeysPerFabric = 8;
182+
183+
static chip::TestPersistentStorageDelegate sStorageDelegate;
184+
static GroupDataProviderImpl sProvider(kMaxGroupsPerFabric, kMaxGroupKeysPerFabric);
116185
class FabricTableHolder
117186
{
118187
public:
@@ -129,6 +198,13 @@ class FabricTableHolder
129198
ReturnErrorOnFailure(mOpKeyStore.Init(&mStorage));
130199
ReturnErrorOnFailure(mOpCertStore.Init(&mStorage));
131200

201+
// Initialize Group Data Provider
202+
sProvider.SetStorageDelegate(&sStorageDelegate);
203+
// sProvider.SetListener(&chip::app::TestGroups::sListener);
204+
ReturnErrorOnFailure(sProvider.Init());
205+
Credentials::SetGroupDataProvider(&sProvider);
206+
207+
// Initialize Fabric Table
132208
chip::FabricTable::InitParams initParams;
133209
initParams.storage = &mStorage;
134210
initParams.operationalKeystore = &mOpKeyStore;
@@ -165,7 +241,7 @@ class TestSessionManagerCallback : public SessionMessageDelegate
165241
NL_TEST_ASSERT(mSuite, dataLength == expectLength);
166242
NL_TEST_ASSERT(mSuite, memcmp(msgBuf->Start(), testEntry.payload, dataLength) == 0);
167243

168-
ChipLogProgress(Test, "TestSessionManagerDispatch[%d] PASS", mTestVectorIndex);
244+
ChipLogProgress(Test, "::: TestSessionManagerDispatch[%d] PASS", mTestVectorIndex);
169245
}
170246

171247
void ResetTest(unsigned testVectorIndex)
@@ -203,6 +279,39 @@ void TestSessionManagerInit(nlTestSuite * inSuite, TestContext & ctx, SessionMan
203279
&fabricTableHolder.GetFabricTable()));
204280
}
205281

282+
// constexpr chip::FabricId kFabricId1 = 0x2906C908D115D362;
283+
static const uint8_t kCompressedFabricIdBuffer1[] = { 0x87, 0xe1, 0xb0, 0x04, 0xe2, 0x35, 0xa1, 0x30 };
284+
constexpr ByteSpan kCompressedFabricId1(kCompressedFabricIdBuffer1);
285+
286+
CHIP_ERROR InjectGroupSessionWithTestKey(SessionHolder & sessionHolder, MessageTestEntry & testEntry)
287+
{
288+
constexpr uint16_t kKeySetIndex = 0x0;
289+
290+
GroupId groupId = testEntry.groupId;
291+
GroupDataProvider * provider = GetGroupDataProvider();
292+
293+
static KeySet sKeySet(kKeySetIndex, SecurityPolicy::kTrustFirst, 1);
294+
static GroupKey sGroupKeySet(groupId, kKeySetIndex);
295+
static GroupInfo sGroupInfo(groupId, "Name Matter Not");
296+
static Transport::IncomingGroupSession sSessionBobToFriends(groupId, kFabricIndex, testEntry.sourceNodeId);
297+
298+
if (testEntry.epochKey)
299+
{
300+
memcpy(sKeySet.epoch_keys[0].key, testEntry.epochKey, 16);
301+
sKeySet.epoch_keys[0].start_time = 0;
302+
sGroupInfo.group_id = groupId;
303+
sGroupKeySet.group_id = groupId;
304+
305+
ReturnErrorOnFailure(provider->SetKeySet(kFabricIndex, kCompressedFabricId1, sKeySet));
306+
ReturnErrorOnFailure(provider->SetGroupKeyAt(kFabricIndex, kGroupIndex, sGroupKeySet));
307+
ReturnErrorOnFailure(provider->SetGroupInfoAt(kFabricIndex, kGroupIndex, sGroupInfo));
308+
}
309+
310+
sessionHolder = SessionHandle(sSessionBobToFriends);
311+
312+
return CHIP_NO_ERROR;
313+
}
314+
206315
void TestSessionManagerDispatch(nlTestSuite * inSuite, void * inContext)
207316
{
208317
CHIP_ERROR err = CHIP_NO_ERROR;
@@ -219,6 +328,7 @@ void TestSessionManagerDispatch(nlTestSuite * inSuite, void * inContext)
219328
Transport::PeerAddress peer(Transport::PeerAddress::UDP(addr, CHIP_PORT));
220329

221330
SessionHolder aliceToBobSession;
331+
SessionHolder testGroupSession;
222332

223333
callback.mSuite = inSuite;
224334
for (unsigned i = 0; i < theMessageTestVectorLength; i++)
@@ -228,20 +338,24 @@ void TestSessionManagerDispatch(nlTestSuite * inSuite, void * inContext)
228338

229339
ChipLogProgress(Test, "===> TestSessionManagerDispatch[%d] '%s': sessionId=0x%04x", i, testEntry.name, testEntry.sessionId);
230340

341+
// TODO(#22830): inject raw keys rather than always defaulting to test key
342+
// TODO: switch on session type
343+
231344
// Inject Sessions
232345
err = sessionManager.InjectPaseSessionWithTestKey(aliceToBobSession, testEntry.sessionId, testEntry.peerNodeId,
233-
testEntry.sessionId, testEntry.fabricIndex, peer,
346+
testEntry.sessionId, kFabricIndex, peer,
234347
CryptoContext::SessionRole::kResponder);
235348
NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR);
236349

350+
err = InjectGroupSessionWithTestKey(testGroupSession, testEntry);
351+
NL_TEST_ASSERT(inSuite, CHIP_NO_ERROR == err);
352+
237353
const char * plain = testEntry.plain;
238354
const ByteSpan expectedPlain(reinterpret_cast<const uint8_t *>(plain), testEntry.plainLength);
239355
const char * privacy = testEntry.privacy;
240356
chip::System::PacketBufferHandle msg =
241357
chip::MessagePacketBuffer::NewWithData(reinterpret_cast<const uint8_t *>(privacy), testEntry.privacyLength);
242358

243-
// TODO: inject raw keys rather than always defaulting to test key
244-
245359
const PeerAddress peerAddress = AddressFromString(testEntry.peerAddr);
246360
sessionManager.OnMessageReceived(peerAddress, std::move(msg));
247361
NL_TEST_ASSERT(inSuite, callback.NumMessagesReceived() > 0);

0 commit comments

Comments
 (0)