Skip to content

Commit a8e2ae5

Browse files
silabs-olivierbrestyled-commitsbzbarsky-apple
authored
Check if a duplicated provider is present before loading into RAM (#37643)
* [Silabs] Check if a duplicated provider is present before loading it to RAM * [Silabs] Added change requested by bzbarsky-apple * Restyled by clang-format * Fixed codereview request * Fixed unit test issues * Restyled by clang-format * Make ProviderAlreadyInList public instead of private * Update src/app/clusters/ota-requestor/DefaultOTARequestorStorage.h Co-authored-by: Boris Zbarsky <bzbarsky@apple.com> * Should return false if the check is made on an empty list * Fixed unit test, removed GetListSize and put back ProviderAlreadyInList as a private member * Fixed unit test * Restyled by clang-format --------- Co-authored-by: Restyled.io <commits@restyled.io> Co-authored-by: Boris Zbarsky <bzbarsky@apple.com>
1 parent 16c99f6 commit a8e2ae5

File tree

3 files changed

+68
-1
lines changed

3 files changed

+68
-1
lines changed

src/app/clusters/ota-requestor/DefaultOTARequestorStorage.cpp

+22-1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,24 @@ CHIP_ERROR DefaultOTARequestorStorage::StoreDefaultProviders(const ProviderLocat
5959
static_cast<uint16_t>(writer.GetLengthWritten()));
6060
}
6161

62+
bool DefaultOTARequestorStorage::ProviderAlreadyInList(ProviderLocationList & listProviders, ProviderLocationType provider)
63+
{
64+
auto iterator = listProviders.Begin();
65+
66+
while (iterator.Next())
67+
{
68+
ProviderLocationType pl = iterator.GetValue();
69+
70+
if ((pl.providerNodeID == provider.providerNodeID) && (pl.fabricIndex == provider.fabricIndex) &&
71+
(pl.endpoint == provider.endpoint))
72+
{
73+
return true;
74+
}
75+
}
76+
77+
return false;
78+
}
79+
6280
CHIP_ERROR DefaultOTARequestorStorage::LoadDefaultProviders(ProviderLocationList & providers)
6381
{
6482
uint8_t buffer[kProviderListMaxSerializedSize];
@@ -77,7 +95,10 @@ CHIP_ERROR DefaultOTARequestorStorage::LoadDefaultProviders(ProviderLocationList
7795
{
7896
ProviderLocationType provider;
7997
ReturnErrorOnFailure(provider.Decode(reader));
80-
providers.Add(provider);
98+
if (!ProviderAlreadyInList(providers, provider))
99+
{
100+
providers.Add(provider);
101+
}
81102
}
82103

83104
ReturnErrorOnFailure(reader.ExitContainer(outerType));

src/app/clusters/ota-requestor/DefaultOTARequestorStorage.h

+4
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ class DefaultOTARequestorStorage : public OTARequestorStorage
4949
CHIP_ERROR ClearTargetVersion() override;
5050

5151
private:
52+
/**
53+
* Check whether a provider is already present in a list of providers.
54+
*/
55+
bool ProviderAlreadyInList(ProviderLocationList & listProviders, ProviderLocationType provider);
5256
CHIP_ERROR Load(const char * key, MutableByteSpan & buffer);
5357
PersistentStorageDelegate * mPersistentStorage = nullptr;
5458
};

src/app/tests/TestDefaultOTARequestorStorage.cpp

+42
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,48 @@ TEST(TestDefaultOTARequestorStorage, TestDefaultProvidersEmpty)
9797
EXPECT_FALSE(providers.Begin().Next());
9898
}
9999

100+
TEST(TestDefaultOTARequestorStorage, TestDefaultProvidersDuplicated)
101+
{
102+
TestPersistentStorageDelegate persistentStorage;
103+
DefaultOTARequestorStorage otaStorage;
104+
otaStorage.Init(persistentStorage);
105+
106+
const auto makeProvider = [](FabricIndex fabric, NodeId nodeId, EndpointId endpointId) {
107+
OTARequestorStorage::ProviderLocationType provider;
108+
provider.fabricIndex = fabric;
109+
provider.providerNodeID = nodeId;
110+
provider.endpoint = endpointId;
111+
return provider;
112+
};
113+
114+
ProviderLocationList providers = {};
115+
116+
EXPECT_EQ(CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND, otaStorage.LoadDefaultProviders(providers));
117+
auto iterator = providers.Begin();
118+
EXPECT_EQ(false, iterator.Next());
119+
120+
for (uint8_t i = 0; i < CHIP_CONFIG_MAX_FABRICS; i++)
121+
{
122+
EXPECT_EQ(CHIP_NO_ERROR, providers.Add(makeProvider(FabricIndex(1), NodeId(0x11111111), EndpointId(1))));
123+
}
124+
125+
EXPECT_EQ(CHIP_NO_ERROR, otaStorage.StoreDefaultProviders(providers));
126+
127+
providers = {};
128+
EXPECT_EQ(CHIP_NO_ERROR, otaStorage.LoadDefaultProviders(providers));
129+
130+
iterator = providers.Begin();
131+
132+
// Check provider #1
133+
EXPECT_EQ(true, iterator.Next());
134+
OTARequestorStorage::ProviderLocationType provider1 = iterator.GetValue();
135+
EXPECT_EQ(FabricIndex(1), provider1.fabricIndex);
136+
EXPECT_EQ(NodeId(0x11111111), provider1.providerNodeID);
137+
EXPECT_EQ(EndpointId(1), provider1.endpoint);
138+
139+
EXPECT_EQ(false, iterator.Next());
140+
}
141+
100142
TEST(TestDefaultOTARequestorStorage, TestCurrentProviderLocation)
101143
{
102144
TestPersistentStorageDelegate persistentStorage;

0 commit comments

Comments
 (0)