24
24
#define CHIP_ENABLE_TEST_ENCRYPTED_BUFFER_API // Up here in case some other header
25
25
// includes SessionManager.h indirectly
26
26
27
+ #include < credentials/GroupDataProviderImpl.h>
27
28
#include < credentials/PersistentStorageOpCertStore.h>
28
29
#include < crypto/PersistentStorageOperationalKeystore.h>
29
30
#include < lib/core/CHIPCore.h>
@@ -49,6 +50,12 @@ using namespace chip;
49
50
using namespace chip ::Inet;
50
51
using namespace chip ::Transport;
51
52
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;
52
59
53
60
using TestContext = chip::Test::LoopbackTransportManager;
54
61
@@ -80,12 +87,13 @@ struct MessageTestEntry
80
87
81
88
uint16_t sessionId;
82
89
NodeId peerNodeId;
83
- FabricIndex fabricIndex;
90
+ GroupId groupId;
91
+ NodeId sourceNodeId;
84
92
};
85
93
86
94
struct MessageTestEntry theMessageTestVector[] = {
87
95
{
88
- .name = " secure pase message" ,
96
+ .name = " secure pase message (no payload) " ,
89
97
.peerAddr = " ::1" ,
90
98
91
99
.payload = " " ,
@@ -100,19 +108,80 @@ struct MessageTestEntry theMessageTestVector[] = {
100
108
.encryptedLength = 30 ,
101
109
.privacyLength = 30 ,
102
110
111
+ // TODO(#22830): unicast message tests must use test key currently
103
112
.encryptKey = " \x5e\xde\xd2\x44\xe5\x53\x2b\x3c\xdc\x23\x40\x9d\xba\xd0\x52\xd2 " ,
104
113
105
114
.nonce = " \x00\x39\x30\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00 " ,
106
115
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 ,
110
171
},
111
172
};
112
173
113
174
const uint16_t theMessageTestVectorLength = sizeof (theMessageTestVector) / sizeof (theMessageTestVector[0 ]);
114
175
115
176
// 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 );
116
185
class FabricTableHolder
117
186
{
118
187
public:
@@ -129,6 +198,13 @@ class FabricTableHolder
129
198
ReturnErrorOnFailure (mOpKeyStore .Init (&mStorage ));
130
199
ReturnErrorOnFailure (mOpCertStore .Init (&mStorage ));
131
200
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
132
208
chip::FabricTable::InitParams initParams;
133
209
initParams.storage = &mStorage ;
134
210
initParams.operationalKeystore = &mOpKeyStore ;
@@ -165,7 +241,7 @@ class TestSessionManagerCallback : public SessionMessageDelegate
165
241
NL_TEST_ASSERT (mSuite , dataLength == expectLength);
166
242
NL_TEST_ASSERT (mSuite , memcmp (msgBuf->Start (), testEntry.payload , dataLength) == 0 );
167
243
168
- ChipLogProgress (Test, " TestSessionManagerDispatch[%d] PASS" , mTestVectorIndex );
244
+ ChipLogProgress (Test, " ::: TestSessionManagerDispatch[%d] PASS" , mTestVectorIndex );
169
245
}
170
246
171
247
void ResetTest (unsigned testVectorIndex)
@@ -203,6 +279,39 @@ void TestSessionManagerInit(nlTestSuite * inSuite, TestContext & ctx, SessionMan
203
279
&fabricTableHolder.GetFabricTable ()));
204
280
}
205
281
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
+
206
315
void TestSessionManagerDispatch (nlTestSuite * inSuite, void * inContext)
207
316
{
208
317
CHIP_ERROR err = CHIP_NO_ERROR;
@@ -219,6 +328,7 @@ void TestSessionManagerDispatch(nlTestSuite * inSuite, void * inContext)
219
328
Transport::PeerAddress peer (Transport::PeerAddress::UDP (addr, CHIP_PORT));
220
329
221
330
SessionHolder aliceToBobSession;
331
+ SessionHolder testGroupSession;
222
332
223
333
callback.mSuite = inSuite;
224
334
for (unsigned i = 0 ; i < theMessageTestVectorLength; i++)
@@ -228,20 +338,24 @@ void TestSessionManagerDispatch(nlTestSuite * inSuite, void * inContext)
228
338
229
339
ChipLogProgress (Test, " ===> TestSessionManagerDispatch[%d] '%s': sessionId=0x%04x" , i, testEntry.name , testEntry.sessionId );
230
340
341
+ // TODO(#22830): inject raw keys rather than always defaulting to test key
342
+ // TODO: switch on session type
343
+
231
344
// Inject Sessions
232
345
err = sessionManager.InjectPaseSessionWithTestKey (aliceToBobSession, testEntry.sessionId , testEntry.peerNodeId ,
233
- testEntry.sessionId , testEntry. fabricIndex , peer,
346
+ testEntry.sessionId , kFabricIndex , peer,
234
347
CryptoContext::SessionRole::kResponder );
235
348
NL_TEST_ASSERT (inSuite, err == CHIP_NO_ERROR);
236
349
350
+ err = InjectGroupSessionWithTestKey (testGroupSession, testEntry);
351
+ NL_TEST_ASSERT (inSuite, CHIP_NO_ERROR == err);
352
+
237
353
const char * plain = testEntry.plain ;
238
354
const ByteSpan expectedPlain (reinterpret_cast <const uint8_t *>(plain), testEntry.plainLength );
239
355
const char * privacy = testEntry.privacy ;
240
356
chip::System::PacketBufferHandle msg =
241
357
chip::MessagePacketBuffer::NewWithData (reinterpret_cast <const uint8_t *>(privacy), testEntry.privacyLength );
242
358
243
- // TODO: inject raw keys rather than always defaulting to test key
244
-
245
359
const PeerAddress peerAddress = AddressFromString (testEntry.peerAddr );
246
360
sessionManager.OnMessageReceived (peerAddress, std::move (msg));
247
361
NL_TEST_ASSERT (inSuite, callback.NumMessagesReceived () > 0 );
0 commit comments