From 1739afdf6b8960ed508207f40fab96432a5f4503 Mon Sep 17 00:00:00 2001 From: Ricardo Casallas <77841255+rcasallas-silabs@users.noreply.github.com> Date: Mon, 6 Dec 2021 13:27:59 -0500 Subject: [PATCH] Group Data Provider: Added missing group mapping iterator. (#12613) * Group Data Provider: Added missing group mapping iterator. * Group Data Provider: Code review changes. --- src/credentials/GroupDataProvider.h | 22 +- src/credentials/GroupDataProviderImpl.cpp | 119 ++++++-- src/credentials/GroupDataProviderImpl.h | 32 ++- .../tests/TestGroupDataProvider.cpp | 266 +++++++++++------- 4 files changed, 310 insertions(+), 129 deletions(-) diff --git a/src/credentials/GroupDataProvider.h b/src/credentials/GroupDataProvider.h index 51e2de1b83c273..798bb1d6724e77 100644 --- a/src/credentials/GroupDataProvider.h +++ b/src/credentials/GroupDataProvider.h @@ -16,6 +16,7 @@ */ #pragma once +#include #include #include #include @@ -94,29 +95,24 @@ class GroupDataProvider { GroupState() = default; GroupState(chip::FabricIndex fabric, chip::GroupId group_id, uint16_t key_set) : - fabric_index(fabric), group(group_id), keyset_index(key_set) + fabric_index(fabric), group(group_id), keyset_id(key_set) {} // Fabric Index associated with the group state entry's fabric scoping chip::FabricIndex fabric_index = kUndefinedFabricIndex; // Identifies the group within the scope of the given fabric chip::GroupId group = kUndefinedGroupId; // References the set of group keys that generate operationa group keys for use with the given group - uint16_t keyset_index = 0; + uint16_t keyset_id = 0; bool operator==(const GroupState & other) { - return this->fabric_index == other.fabric_index && this->group == other.group && - this->keyset_index == other.keyset_index; + return this->fabric_index == other.fabric_index && this->group == other.group && this->keyset_id == other.keyset_id; } }; // A operational group key set, usable by many GroupState mappings struct KeySet { - enum class SecurityPolicy : uint8_t - { - kStandard = 0, - kLowLatency = 1 - }; + using SecurityPolicy = chip::app::Clusters::GroupKeyManagement::GroupKeySecurityPolicy; KeySet() = default; KeySet(uint16_t id) : keyset_id(id) {} @@ -225,6 +221,14 @@ class GroupDataProvider virtual CHIP_ERROR AddGroupMapping(chip::FabricIndex fabric_index, const GroupMapping & mapping) = 0; virtual CHIP_ERROR RemoveGroupMapping(chip::FabricIndex fabric_index, const GroupMapping & mapping) = 0; virtual CHIP_ERROR RemoveAllGroupMappings(chip::FabricIndex fabric_index, EndpointId endpoint) = 0; + /** + * Creates an iterator that may be used to obtain the groups associated with the given fabric. + * The number of concurrent instances of this iterator is limited. In order to release the allocated memory, + * the iterator's Release() method must be called after the iteration is finished. + * @retval An instance of GroupMappingIterator on success + * @retval nullptr if no iterator instances are available. + */ + virtual GroupMappingIterator * IterateGroupMappings(chip::FabricIndex fabric_index) = 0; /** * Creates an iterator that may be used to obtain the groups associated with the given fabric and endpoint. * The number of concurrent instances of this iterator is limited. In order to release the allocated memory, diff --git a/src/credentials/GroupDataProviderImpl.cpp b/src/credentials/GroupDataProviderImpl.cpp index cc95cd935316f3..ec9433bc05c2e5 100644 --- a/src/credentials/GroupDataProviderImpl.cpp +++ b/src/credentials/GroupDataProviderImpl.cpp @@ -343,7 +343,7 @@ struct StateData : public GroupDataProvider::GroupState, PersistentData(fabric_index))); ReturnErrorOnFailure(writer.Put(kTagGroup, static_cast(group))); - ReturnErrorOnFailure(writer.Put(kTagKeySet, static_cast(keyset_index))); + ReturnErrorOnFailure(writer.Put(kTagKeySet, static_cast(keyset_id))); ReturnErrorOnFailure(writer.Put(kTagNext, static_cast(next))); return writer.EndContainer(container); @@ -373,9 +373,9 @@ struct StateData : public GroupDataProvider::GroupState, PersistentData(index + 1); return states.Save(mStorage); - - return CHIP_ERROR_INTERNAL; } CHIP_ERROR GroupDataProviderImpl::GetGroupState(size_t state_index, GroupState & out_state) @@ -990,7 +1075,7 @@ CHIP_ERROR GroupDataProviderImpl::GetGroupState(size_t state_index, GroupState & // Target index found out_state.fabric_index = state.fabric_index; out_state.group = state.group; - out_state.keyset_index = state.keyset_index; + out_state.keyset_id = state.keyset_id; return CHIP_NO_ERROR; } state.id = state.next; @@ -1097,7 +1182,7 @@ bool GroupDataProviderImpl::AllStatesIterator::Next(GroupState & item) mIndex = state.next; item.fabric_index = state.fabric_index; item.group = state.group; - item.keyset_index = state.keyset_index; + item.keyset_id = state.keyset_id; return true; } @@ -1156,7 +1241,7 @@ bool GroupDataProviderImpl::FabricStatesIterator::Next(GroupState & item) { item.fabric_index = state.fabric_index; item.group = state.group; - item.keyset_index = state.keyset_index; + item.keyset_id = state.keyset_id; mIndex = state.id; return true; } diff --git a/src/credentials/GroupDataProviderImpl.h b/src/credentials/GroupDataProviderImpl.h index e650ffd64fc517..cbd25f78d4d07f 100644 --- a/src/credentials/GroupDataProviderImpl.h +++ b/src/credentials/GroupDataProviderImpl.h @@ -43,6 +43,7 @@ class GroupDataProviderImpl : public GroupDataProvider CHIP_ERROR AddGroupMapping(chip::FabricIndex fabric_index, const GroupMapping & mapping) override; CHIP_ERROR RemoveGroupMapping(chip::FabricIndex fabric_index, const GroupMapping & mapping) override; CHIP_ERROR RemoveAllGroupMappings(chip::FabricIndex fabric_index, EndpointId endpoint) override; + GroupMappingIterator * IterateGroupMappings(chip::FabricIndex fabric_index) override; GroupMappingIterator * IterateGroupMappings(chip::FabricIndex fabric_index, EndpointId endpoint) override; // @@ -71,6 +72,25 @@ class GroupDataProviderImpl : public GroupDataProvider CHIP_ERROR Decrypt(PacketHeader packetHeader, PayloadHeader & payloadHeader, System::PacketBufferHandle & msg) override; private: + class AllGroupMappingsIteratorImpl : public GroupMappingIterator + { + public: + AllGroupMappingsIteratorImpl(GroupDataProviderImpl & provider, chip::FabricIndex fabric); + size_t Count() override; + bool Next(GroupMapping & item) override; + void Release() override; + + private: + GroupDataProviderImpl & mProvider; + chip::FabricIndex mFabric = kUndefinedFabricIndex; + chip::EndpointId mEndpoint = kInvalidEndpointId; + size_t mEndpointIndex = 0; + size_t mEndpointCount = 0; + chip::GroupId mGroup = kUndefinedGroupId; + chip::EndpointId mFirstEndpoint = kInvalidEndpointId; + bool mFirstGroup = true; + }; + class GroupMappingIteratorImpl : public GroupMappingIterator { public: @@ -81,8 +101,9 @@ class GroupDataProviderImpl : public GroupDataProvider private: GroupDataProviderImpl & mProvider; - chip::FabricIndex mFabric = 0; - chip::EndpointId mEndpoint = 0; + chip::FabricIndex mFabric = kUndefinedFabricIndex; + chip::EndpointId mEndpoint = kInvalidEndpointId; + chip::GroupId mFirstGroup = kUndefinedGroupId; chip::GroupId mGroup = kUndefinedGroupId; }; @@ -111,7 +132,7 @@ class GroupDataProviderImpl : public GroupDataProvider private: GroupDataProviderImpl & mProvider; - chip::FabricIndex mFabric = 0; + chip::FabricIndex mFabric = kUndefinedFabricIndex; uint16_t mIndex = 0; size_t mCount = 0; size_t mTotalCount = 0; @@ -127,7 +148,7 @@ class GroupDataProviderImpl : public GroupDataProvider private: GroupDataProviderImpl & mProvider; - chip::FabricIndex mFabric = 0; + chip::FabricIndex mFabric = kUndefinedFabricIndex; uint16_t mNextId = 0; size_t mCount = 0; size_t mIndex = 0; @@ -135,7 +156,8 @@ class GroupDataProviderImpl : public GroupDataProvider chip::PersistentStorageDelegate & mStorage; bool mInitialized = false; - BitMapObjectPool mEndpointIterators; + BitMapObjectPool mAllGroupsIterators; + BitMapObjectPool mEndpointGroupsIterators; BitMapObjectPool mAllStatesIterators; BitMapObjectPool mFabricStatesIterators; BitMapObjectPool mKeySetIterators; diff --git a/src/credentials/tests/TestGroupDataProvider.cpp b/src/credentials/tests/TestGroupDataProvider.cpp index 946c533c43bca8..409f40368de463 100644 --- a/src/credentials/tests/TestGroupDataProvider.cpp +++ b/src/credentials/tests/TestGroupDataProvider.cpp @@ -293,10 +293,35 @@ void TestGroupMappingIterator(nlTestSuite * apSuite, void * apContext) NL_TEST_ASSERT(apSuite, CHIP_NO_ERROR == groups->AddGroupMapping(kFabric2, endpoint3group2)); NL_TEST_ASSERT(apSuite, CHIP_NO_ERROR == groups->AddGroupMapping(kFabric2, endpoint3group1)); - // Fabric 1 - GroupMapping mapping; + // Fabric1, all endpoints + + std::set> expected_f1_all = { + { kEndpoint1, kGroup1 }, { kEndpoint1, kGroup2 }, { kEndpoint1, kGroup3 }, { kEndpoint2, kGroup1 }, { kEndpoint2, kGroup2 }, + { kEndpoint2, kGroup3 }, { kEndpoint3, kGroup1 }, { kEndpoint3, kGroup2 }, { kEndpoint3, kGroup3 }, + }; + + auto it = groups->IterateGroupMappings(kFabric1); + NL_TEST_ASSERT(apSuite, it); + if (it) + { + size_t count = 0; + NL_TEST_ASSERT(apSuite, it->Count() == expected_f1_all.size()); + while (it->Next(mapping)) + { + std::pair pair(mapping.endpoint, mapping.group); + NL_TEST_ASSERT(apSuite, expected_f1_all.count(pair) > 0); + count++; + } + + NL_TEST_ASSERT(apSuite, count == expected_f1_all.size()); + NL_TEST_ASSERT(apSuite, it->Count() == expected_f1_all.size()); + it->Release(); + } + + // Fabric 1, by endpoint + constexpr size_t endpoints_count = 3; constexpr EndpointId endpoints[endpoints_count] = { kEndpoint1, kEndpoint2, kEndpoint3 }; @@ -308,7 +333,7 @@ void TestGroupMappingIterator(nlTestSuite * apSuite, void * apContext) for (size_t i = 0; i < endpoints_count; i++) { - auto it = groups->IterateGroupMappings(kFabric1, endpoints[i]); + it = groups->IterateGroupMappings(kFabric1, endpoints[i]); NL_TEST_ASSERT(apSuite, it); if (it) { @@ -327,7 +352,33 @@ void TestGroupMappingIterator(nlTestSuite * apSuite, void * apContext) } } - // // Fabric 2 + // Fabric2, all endpoints + + std::set> expected_f2_all = { { kEndpoint1, kGroup1 }, { kEndpoint2, kGroup2 }, + { kEndpoint2, kGroup1 }, { kEndpoint3, kGroup3 }, + { kEndpoint3, kGroup2 }, { kEndpoint3, kGroup1 } }; + + it = groups->IterateGroupMappings(kFabric2); + NL_TEST_ASSERT(apSuite, it); + if (it) + { + size_t count = 0; + NL_TEST_ASSERT(apSuite, it->Count() == expected_f2_all.size()); + + while (it->Next(mapping)) + { + std::pair pair(mapping.endpoint, mapping.group); + NL_TEST_ASSERT(apSuite, expected_f2_all.count(pair) > 0); + count++; + } + + NL_TEST_ASSERT(apSuite, count == expected_f2_all.size()); + NL_TEST_ASSERT(apSuite, it->Count() == expected_f2_all.size()); + it->Release(); + it = nullptr; + } + + // Fabric 2, by endpoint std::set expected_f2[3] = { { kGroup1, kUndefinedGroupId, kUndefinedGroupId }, { kGroup2, kGroup1, kUndefinedGroupId }, @@ -336,7 +387,7 @@ void TestGroupMappingIterator(nlTestSuite * apSuite, void * apContext) for (size_t i = 0; i < endpoints_count; i++) { - auto it = groups->IterateGroupMappings(kFabric2, endpoints[i]); + it = groups->IterateGroupMappings(kFabric2, endpoints[i]); NL_TEST_ASSERT(apSuite, it); if (it) { @@ -355,6 +406,7 @@ void TestGroupMappingIterator(nlTestSuite * apSuite, void * apContext) } NL_TEST_ASSERT(apSuite, j == expected_count); it->Release(); + it = nullptr; } } } @@ -494,20 +546,21 @@ void TestGroupStateIterator(nlTestSuite * apSuite, void * apContext) fabric2group1set2, fabric2group1set3, fabric1group1set2 }; GroupState state(0, 0, 0); - auto it_all = groups->IterateGroupStates(); - NL_TEST_ASSERT(apSuite, it_all); - if (it_all) + auto it = groups->IterateGroupStates(); + NL_TEST_ASSERT(apSuite, it); + if (it) { size_t i = 0; - NL_TEST_ASSERT(apSuite, expected_count == it_all->Count()); + NL_TEST_ASSERT(apSuite, expected_count == it->Count()); - while (it_all->Next(state) && i < expected_count) + while (it->Next(state) && i < expected_count) { NL_TEST_ASSERT(apSuite, state == expected[i]); i++; } NL_TEST_ASSERT(apSuite, i == expected_count); - it_all->Release(); + it->Release(); + it = nullptr; } // Iterate Fabric 1 only @@ -515,20 +568,21 @@ void TestGroupStateIterator(nlTestSuite * apSuite, void * apContext) constexpr size_t expected_count_f1 = 3; const GroupState expected_f1[expected_count_f1] = { fabric1group1set3, fabric1group1set1, fabric1group1set2 }; - auto it_f1 = groups->IterateGroupStates(kFabric1); - NL_TEST_ASSERT(apSuite, it_f1); - if (it_f1) + it = groups->IterateGroupStates(kFabric1); + NL_TEST_ASSERT(apSuite, it); + if (it) { size_t i = 0; - NL_TEST_ASSERT(apSuite, expected_count_f1 == it_f1->Count()); + NL_TEST_ASSERT(apSuite, expected_count_f1 == it->Count()); - while (it_f1->Next(state) && i < expected_count_f1) + while (it->Next(state) && i < expected_count_f1) { NL_TEST_ASSERT(apSuite, state == expected_f1[i]); i++; } NL_TEST_ASSERT(apSuite, i == expected_count_f1); - it_f1->Release(); + it->Release(); + it = nullptr; } // Iterate Fabric 2 only @@ -536,20 +590,21 @@ void TestGroupStateIterator(nlTestSuite * apSuite, void * apContext) constexpr size_t expected_count_f2 = 3; const GroupState expected_f2[expected_count_f2] = { fabric2group1set1, fabric2group1set2, fabric2group1set3 }; - auto it_f2 = groups->IterateGroupStates(kFabric2); - NL_TEST_ASSERT(apSuite, it_f2); - if (it_f2) + it = groups->IterateGroupStates(kFabric2); + NL_TEST_ASSERT(apSuite, it); + if (it) { size_t i = 0; - NL_TEST_ASSERT(apSuite, expected_count_f2 == it_f2->Count()); + NL_TEST_ASSERT(apSuite, expected_count_f2 == it->Count()); - while (it_f2->Next(state) && i < expected_count_f2) + while (it->Next(state) && i < expected_count_f2) { NL_TEST_ASSERT(apSuite, state == expected_f2[i]); i++; } NL_TEST_ASSERT(apSuite, i == expected_count_f2); - it_f2->Release(); + it->Release(); + it = nullptr; } } @@ -704,42 +759,44 @@ void TestKeySetIterator(nlTestSuite * apSuite, void * apContext) { kKeySetId1, keyset1 }, { kKeySetId0, keyset0 }, { kKeySetId2, keyset2 }, { kKeySetId3, keyset3 } }; - auto it_f1 = groups->IterateKeySets(kFabric1); - NL_TEST_ASSERT(apSuite, it_f1); - if (it_f1) + auto it = groups->IterateKeySets(kFabric1); + NL_TEST_ASSERT(apSuite, it); + if (it) { size_t i = 0; - NL_TEST_ASSERT(apSuite, expected_f1.size() == it_f1->Count()); + NL_TEST_ASSERT(apSuite, expected_f1.size() == it->Count()); - while (it_f1->Next(keysets) && i < expected_f1.size()) + while (it->Next(keysets) && i < expected_f1.size()) { NL_TEST_ASSERT(apSuite, expected_f1.count(keysets.keyset_id) > 0); NL_TEST_ASSERT(apSuite, keysets == expected_f1[keysets.keyset_id]); i++; } NL_TEST_ASSERT(apSuite, i == expected_f1.size()); - it_f1->Release(); + it->Release(); + it = nullptr; } // Iterate Fabric 2 std::map expected_f2{ { kKeySetId3, keyset2 }, { kKeySetId1, keyset3 }, { kKeySetId2, keyset1 } }; - auto it_f2 = groups->IterateKeySets(kFabric2); - NL_TEST_ASSERT(apSuite, it_f2); - if (it_f2) + it = groups->IterateKeySets(kFabric2); + NL_TEST_ASSERT(apSuite, it); + if (it) { size_t i = 0; - NL_TEST_ASSERT(apSuite, expected_f2.size() == it_f2->Count()); + NL_TEST_ASSERT(apSuite, expected_f2.size() == it->Count()); - while (it_f2->Next(keysets) && i < expected_f2.size()) + while (it->Next(keysets) && i < expected_f2.size()) { NL_TEST_ASSERT(apSuite, expected_f2.count(keysets.keyset_id) > 0); NL_TEST_ASSERT(apSuite, keysets == expected_f2[keysets.keyset_id]); i++; } NL_TEST_ASSERT(apSuite, i == expected_f2.size()); - it_f2->Release(); + it->Release(); + it = nullptr; } } @@ -919,38 +976,41 @@ void TestEndpointIterator(nlTestSuite * apSuite, void * apContext) // Endpoint 1 + GroupDataProvider::GroupMapping mapping; auto * it = groups->IterateGroupMappings(kFabric1, kEndpoint1); NL_TEST_ASSERT(apSuite, it); - - GroupDataProvider::GroupMapping mapping; - size_t count1 = it->Count(); - size_t count2 = 0; - NL_TEST_ASSERT(apSuite, 2 == count1); - while (it->Next(mapping)) + if (it) { - count2++; - NL_TEST_ASSERT(apSuite, kGroup1 == mapping.group || kGroup2 == mapping.group); + size_t count1 = it->Count(); + size_t count2 = 0; + NL_TEST_ASSERT(apSuite, 2 == count1); + while (it->Next(mapping)) + { + count2++; + NL_TEST_ASSERT(apSuite, kGroup1 == mapping.group || kGroup2 == mapping.group); + } + NL_TEST_ASSERT(apSuite, count2 == count1); + it->Release(); } - NL_TEST_ASSERT(apSuite, count2 == count1); - it->Release(); - it = nullptr; // Endpoint 3 it = groups->IterateGroupMappings(kFabric1, kEndpoint3); NL_TEST_ASSERT(apSuite, it); - - count1 = it->Count(); - count2 = 0; - NL_TEST_ASSERT(apSuite, 3 == count1); - while (it->Next(mapping)) + if (it) { - count2++; - NL_TEST_ASSERT(apSuite, kGroup1 == mapping.group || kGroup2 == mapping.group || kGroup3 == mapping.group); + size_t count1 = it->Count(); + size_t count2 = 0; + NL_TEST_ASSERT(apSuite, 3 == count1); + while (it->Next(mapping)) + { + count2++; + NL_TEST_ASSERT(apSuite, kGroup1 == mapping.group || kGroup2 == mapping.group || kGroup3 == mapping.group); + } + NL_TEST_ASSERT(apSuite, count2 == count1); + it->Release(); + it = nullptr; } - NL_TEST_ASSERT(apSuite, count2 == count1); - it->Release(); - it = nullptr; } void TestStates(nlTestSuite * apSuite, void * apContext) @@ -985,21 +1045,24 @@ void TestStates(nlTestSuite * apSuite, void * apContext) NL_TEST_ASSERT(apSuite, CHIP_ERROR_INVALID_ARGUMENT == err); auto * it = groups->IterateGroupStates(kFabric1); - NL_TEST_ASSERT(apSuite, it != nullptr); - NL_TEST_ASSERT(apSuite, 2 == it->Count()); - it->Release(); - it = nullptr; + if (it) + { + NL_TEST_ASSERT(apSuite, it != nullptr); + NL_TEST_ASSERT(apSuite, 2 == it->Count()); + it->Release(); + it = nullptr; + } err = groups->GetGroupState(0, state0b); NL_TEST_ASSERT(apSuite, CHIP_NO_ERROR == err); NL_TEST_ASSERT(apSuite, state0a.group == state0b.group); - NL_TEST_ASSERT(apSuite, state0a.keyset_index == state0b.keyset_index); + NL_TEST_ASSERT(apSuite, state0a.keyset_id == state0b.keyset_id); NL_TEST_ASSERT(apSuite, kFabric1 == state0b.fabric_index); err = groups->GetGroupState(1, state1b); NL_TEST_ASSERT(apSuite, CHIP_NO_ERROR == err); NL_TEST_ASSERT(apSuite, state1a.group == state1b.group); - NL_TEST_ASSERT(apSuite, state1a.keyset_index == state1b.keyset_index); + NL_TEST_ASSERT(apSuite, state1a.keyset_id == state1b.keyset_id); NL_TEST_ASSERT(apSuite, kFabric1 == state1b.fabric_index); err = groups->GetGroupState(2, state3b); @@ -1010,16 +1073,16 @@ void TestStates(nlTestSuite * apSuite, void * apContext) // Entry 1 should remain, now at slot 0 state1b.group = 10; - state1b.keyset_index = 12; + state1b.keyset_id = 12; state1b.fabric_index = 14; err = groups->GetGroupState(0, state1b); NL_TEST_ASSERT(apSuite, CHIP_NO_ERROR == err); NL_TEST_ASSERT(apSuite, state1a.group == state1b.group); - NL_TEST_ASSERT(apSuite, state1a.keyset_index == state1b.keyset_index); + NL_TEST_ASSERT(apSuite, state1a.keyset_id == state1b.keyset_id); NL_TEST_ASSERT(apSuite, kFabric1 == state1b.fabric_index); state1b.group = 10; - state1b.keyset_index = 12; + state1b.keyset_id = 12; state1b.fabric_index = 14; err = groups->GetGroupState(1, state1b); NL_TEST_ASSERT(apSuite, CHIP_ERROR_KEY_NOT_FOUND == err); @@ -1041,7 +1104,7 @@ void TestStates(nlTestSuite * apSuite, void * apContext) err = groups->GetGroupState(0, state4b); NL_TEST_ASSERT(apSuite, CHIP_NO_ERROR == err); NL_TEST_ASSERT(apSuite, state4a.group == state4b.group); - NL_TEST_ASSERT(apSuite, state4a.keyset_index == state4b.keyset_index); + NL_TEST_ASSERT(apSuite, state4a.keyset_id == state4b.keyset_id); NL_TEST_ASSERT(apSuite, state4a.fabric_index == state4b.fabric_index); // Incorrect fabric @@ -1090,7 +1153,7 @@ void TestStateIterator(nlTestSuite * apSuite, void * apContext) GroupDataProvider::GroupState state; while (it->Next(state)) { - NL_TEST_ASSERT(apSuite, (state.group > 0 && state.group < 4) && (state.keyset_index == 1)); + NL_TEST_ASSERT(apSuite, (state.group > 0 && state.group < 4) && (state.keyset_id == 1)); NL_TEST_ASSERT(apSuite, (state.fabric_index == kFabric1)); count2++; } @@ -1099,17 +1162,17 @@ void TestStateIterator(nlTestSuite * apSuite, void * apContext) it = nullptr; } + // Fabric Index 2 has 1 entry + auto * it = groups->IterateGroupStates(kFabric2); + NL_TEST_ASSERT(apSuite, it != nullptr); + if (it) { - // Fabric Index 2 has 1 entry - auto * it = groups->IterateGroupStates(kFabric2); - NL_TEST_ASSERT(apSuite, it != nullptr); - size_t count1 = it->Count(); NL_TEST_ASSERT(apSuite, 1 == count1); GroupDataProvider::GroupState state; NL_TEST_ASSERT(apSuite, it->Next(state)); - NL_TEST_ASSERT(apSuite, (state.group > 0 && state.group < 4) && (state.keyset_index == 2)); + NL_TEST_ASSERT(apSuite, (state.group > 0 && state.group < 4) && (state.keyset_id == 2)); NL_TEST_ASSERT(apSuite, (state.fabric_index == kFabric2)); NL_TEST_ASSERT(apSuite, !it->Next(state)); @@ -1118,11 +1181,11 @@ void TestStateIterator(nlTestSuite * apSuite, void * apContext) it = nullptr; } + // Fabric Index 1 has 3 entries + Fabric Index 2 has 1 entry + it = groups->IterateGroupStates(); + NL_TEST_ASSERT(apSuite, it != nullptr); + if (it) { - // Fabric Index 1 has 3 entries + Fabric Index 2 has 1 entry - auto * it = groups->IterateGroupStates(); - NL_TEST_ASSERT(apSuite, it != nullptr); - size_t count1 = it->Count(); size_t count2 = 0; NL_TEST_ASSERT(apSuite, 4 == count1); @@ -1130,7 +1193,7 @@ void TestStateIterator(nlTestSuite * apSuite, void * apContext) while (it->Next(state)) { NL_TEST_ASSERT(apSuite, (state.fabric_index == kFabric1 || state.fabric_index == kFabric2)); - NL_TEST_ASSERT(apSuite, (state.group > 0 && state.group < 4) && (state.keyset_index == 1 || state.keyset_index == 2)); + NL_TEST_ASSERT(apSuite, (state.group > 0 && state.group < 4) && (state.keyset_id == 1 || state.keyset_id == 2)); count2++; } NL_TEST_ASSERT(apSuite, count2 == count1); @@ -1173,7 +1236,12 @@ void TestKeys(nlTestSuite * apSuite, void * apContext) auto * it = groups->IterateKeySets(kFabric1); NL_TEST_ASSERT(apSuite, it != nullptr); - NL_TEST_ASSERT(apSuite, it->Count() == 2); + if (it) + { + NL_TEST_ASSERT(apSuite, it->Count() == 2); + it->Release(); + it = nullptr; + } err = groups->GetKeySet(kFabric1, kKeySetId0, keys0b); NL_TEST_ASSERT(apSuite, CHIP_NO_ERROR == err); @@ -1233,24 +1301,26 @@ void TestKeysIterator(nlTestSuite * apSuite, void * apContext) auto * it = groups->IterateKeySets(kFabric1); NL_TEST_ASSERT(apSuite, it); + if (it) + { + size_t count1 = it->Count(); + size_t count2 = 0; + NL_TEST_ASSERT(apSuite, 3 == count1); + GroupDataProvider::KeySet keys; - size_t count1 = it->Count(); - size_t count2 = 0; - NL_TEST_ASSERT(apSuite, 3 == count1); - GroupDataProvider::KeySet keys; - - uint16_t last_keyset_id = UINT16_MAX; + uint16_t last_keyset_id = UINT16_MAX; - while (it->Next(keys)) - { - NL_TEST_ASSERT(apSuite, keys.keyset_id == kKeySetId0 || keys.keyset_id == kKeySetId1 || keys.keyset_id == kKeySetId2); - NL_TEST_ASSERT(apSuite, keys.keyset_id != last_keyset_id); - last_keyset_id = keys.keyset_id; - count2++; + while (it->Next(keys)) + { + NL_TEST_ASSERT(apSuite, keys.keyset_id == kKeySetId0 || keys.keyset_id == kKeySetId1 || keys.keyset_id == kKeySetId2); + NL_TEST_ASSERT(apSuite, keys.keyset_id != last_keyset_id); + last_keyset_id = keys.keyset_id; + count2++; + } + NL_TEST_ASSERT(apSuite, count2 == count1); + it->Release(); + it = nullptr; } - NL_TEST_ASSERT(apSuite, count2 == count1); - it->Release(); - it = nullptr; } void TestPerFabricData(nlTestSuite * apSuite, void * apContext) @@ -1330,19 +1400,19 @@ void TestPerFabricData(nlTestSuite * apSuite, void * apContext) NL_TEST_ASSERT(apSuite, CHIP_NO_ERROR == err); NL_TEST_ASSERT(apSuite, state0a.fabric_index == state0b.fabric_index); NL_TEST_ASSERT(apSuite, state0a.group == state0b.group); - NL_TEST_ASSERT(apSuite, state0a.keyset_index == state0b.keyset_index); + NL_TEST_ASSERT(apSuite, state0a.keyset_id == state0b.keyset_id); err = groups->GetGroupState(1, state1b); NL_TEST_ASSERT(apSuite, CHIP_NO_ERROR == err); NL_TEST_ASSERT(apSuite, state1a.fabric_index == state1b.fabric_index); NL_TEST_ASSERT(apSuite, state1a.group == state1b.group); - NL_TEST_ASSERT(apSuite, state1a.keyset_index == state1b.keyset_index); + NL_TEST_ASSERT(apSuite, state1a.keyset_id == state1b.keyset_id); err = groups->GetGroupState(2, state2b); NL_TEST_ASSERT(apSuite, CHIP_NO_ERROR == err); NL_TEST_ASSERT(apSuite, state2a.fabric_index == state2b.fabric_index); NL_TEST_ASSERT(apSuite, state2a.group == state2b.group); - NL_TEST_ASSERT(apSuite, state2a.keyset_index == state2b.keyset_index); + NL_TEST_ASSERT(apSuite, state2a.keyset_id == state2b.keyset_id); err = groups->GetGroupState(4, state4b); NL_TEST_ASSERT(apSuite, CHIP_ERROR_KEY_NOT_FOUND == err); @@ -1452,13 +1522,13 @@ void TestPerFabricData(nlTestSuite * apSuite, void * apContext) NL_TEST_ASSERT(apSuite, CHIP_NO_ERROR == err); NL_TEST_ASSERT(apSuite, state1a.fabric_index == state0b.fabric_index); NL_TEST_ASSERT(apSuite, state1a.group == state0b.group); - NL_TEST_ASSERT(apSuite, state1a.keyset_index == state0b.keyset_index); + NL_TEST_ASSERT(apSuite, state1a.keyset_id == state0b.keyset_id); err = groups->GetGroupState(1, state1b); NL_TEST_ASSERT(apSuite, CHIP_NO_ERROR == err); NL_TEST_ASSERT(apSuite, state2a.fabric_index == state1b.fabric_index); NL_TEST_ASSERT(apSuite, state2a.group == state1b.group); - NL_TEST_ASSERT(apSuite, state2a.keyset_index == state1b.keyset_index); + NL_TEST_ASSERT(apSuite, state2a.keyset_id == state1b.keyset_id); err = groups->GetGroupState(2, state2b); NL_TEST_ASSERT(apSuite, CHIP_ERROR_KEY_NOT_FOUND == err);