Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a way to dynamically change whether a node identity should be advertised. #35634

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/credentials/FabricTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2164,4 +2164,17 @@ CHIP_ERROR FabricTable::SetFabricIndexForNextAddition(FabricIndex fabricIndex)
return CHIP_NO_ERROR;
}

CHIP_ERROR FabricTable::SetShouldAdvertiseIdentity(FabricIndex fabricIndex, AdvertiseIdentity advertiseIdentity)
{
VerifyOrReturnError(IsValidFabricIndex(fabricIndex), CHIP_ERROR_INVALID_FABRIC_INDEX);

FabricInfo * fabricInfo = GetMutableFabricByIndex(fabricIndex);
bool fabricIsInitialized = (fabricInfo != nullptr) && fabricInfo->IsInitialized();
VerifyOrReturnError(fabricIsInitialized, CHIP_ERROR_INVALID_FABRIC_INDEX);

fabricInfo->SetShouldAdvertiseIdentity(advertiseIdentity == AdvertiseIdentity::Yes);

return CHIP_NO_ERROR;
}

} // namespace chip
14 changes: 14 additions & 0 deletions src/credentials/FabricTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,8 @@ class DLL_EXPORT FabricInfo
mNodeId = kUndefinedNodeId;
}

void SetShouldAdvertiseIdentity(bool advertiseIdentity) { mShouldAdvertiseIdentity = advertiseIdentity; }

static constexpr size_t MetadataTLVMaxSize()
{
return TLV::EstimateStructOverhead(sizeof(uint16_t), kFabricLabelMaxLengthInBytes);
Expand Down Expand Up @@ -1027,6 +1029,18 @@ class DLL_EXPORT FabricTable
*/
CHIP_ERROR SetFabricIndexForNextAddition(FabricIndex fabricIndex);

/**
* @brief Set the advertising behavior for the fabric identified by `fabricIndex`.
*
* It is the caller's responsibility to actually restart DNS-SD advertising
* as needed after updating this state.
*
* @param fabricIndex - Fabric Index for which to set the label
* @param advertiseIdentity - whether the identity for this fabric should be advertised.
* @retval CHIP_ERROR_INVALID_FABRIC_INDEX if fabricIndex does not refer to a fabric in the table
*/
CHIP_ERROR SetShouldAdvertiseIdentity(FabricIndex fabricIndex, AdvertiseIdentity advertiseIdentity);

private:
enum class StateFlags : uint16_t
{
Expand Down
32 changes: 31 additions & 1 deletion src/credentials/tests/TestFabricTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,7 @@ TEST_F(TestFabricTable, TestBasicAddNocUpdateNocFlow)
{
EXPECT_EQ(iterFabricInfo.GetNodeId(), nodeId);
EXPECT_EQ(iterFabricInfo.GetFabricId(), fabricId);
EXPECT_TRUE(iterFabricInfo.ShouldAdvertiseIdentity());
saw1 = true;
}
}
Expand Down Expand Up @@ -817,12 +818,14 @@ TEST_F(TestFabricTable, TestBasicAddNocUpdateNocFlow)
{
EXPECT_EQ(iterFabricInfo.GetNodeId(), 55u);
EXPECT_EQ(iterFabricInfo.GetFabricId(), 11u);
EXPECT_TRUE(iterFabricInfo.ShouldAdvertiseIdentity());
saw1 = true;
}
if (iterFabricInfo.GetFabricIndex() == 2)
{
EXPECT_EQ(iterFabricInfo.GetNodeId(), 999u);
EXPECT_EQ(iterFabricInfo.GetFabricId(), 44u);
EXPECT_TRUE(iterFabricInfo.ShouldAdvertiseIdentity());
saw2 = true;
}
}
Expand Down Expand Up @@ -1104,7 +1107,9 @@ TEST_F(TestFabricTable, TestAddMultipleSameRootDifferentFabricId)
EXPECT_EQ(fabricTable.FabricCount(), 0);
EXPECT_EQ(fabricTable.AddNewPendingTrustedRootCert(rcac), CHIP_NO_ERROR);
FabricIndex newFabricIndex = kUndefinedFabricIndex;
EXPECT_EQ(fabricTable.AddNewPendingFabricWithOperationalKeystore(noc, icac, kVendorId, &newFabricIndex), CHIP_NO_ERROR);
EXPECT_EQ(fabricTable.AddNewPendingFabricWithOperationalKeystore(noc, icac, kVendorId, &newFabricIndex,
FabricTable::AdvertiseIdentity::No),
CHIP_NO_ERROR);
EXPECT_EQ(fabricTable.FabricCount(), 1);
EXPECT_EQ(newFabricIndex, 1);

Expand All @@ -1118,6 +1123,23 @@ TEST_F(TestFabricTable, TestAddMultipleSameRootDifferentFabricId)
EXPECT_EQ(fabricInfo->GetFabricId(), 1111u);
EXPECT_EQ(fabricInfo->GetVendorId(), kVendorId);
EXPECT_EQ(fabricInfo->GetFabricLabel().size(), 0u);
EXPECT_FALSE(fabricInfo->ShouldAdvertiseIdentity());

EXPECT_EQ(fabricTable.SetShouldAdvertiseIdentity(newFabricIndex, FabricTable::AdvertiseIdentity::Yes), CHIP_NO_ERROR);
EXPECT_TRUE(fabricInfo->ShouldAdvertiseIdentity());

// Check that for indices we don't have a fabric for, SetShouldAdvertiseIdentity fails.
EXPECT_EQ(fabricTable.SetShouldAdvertiseIdentity(kUndefinedFabricIndex, FabricTable::AdvertiseIdentity::No),
CHIP_ERROR_INVALID_FABRIC_INDEX);
EXPECT_EQ(fabricTable.SetShouldAdvertiseIdentity(kUndefinedFabricIndex, FabricTable::AdvertiseIdentity::Yes),
CHIP_ERROR_INVALID_FABRIC_INDEX);
EXPECT_EQ(fabricTable.SetShouldAdvertiseIdentity(2, FabricTable::AdvertiseIdentity::Yes), CHIP_ERROR_INVALID_FABRIC_INDEX);
EXPECT_EQ(fabricTable.SetShouldAdvertiseIdentity(2, FabricTable::AdvertiseIdentity::No), CHIP_ERROR_INVALID_FABRIC_INDEX);

EXPECT_TRUE(fabricInfo->ShouldAdvertiseIdentity());

EXPECT_EQ(fabricTable.SetShouldAdvertiseIdentity(newFabricIndex, FabricTable::AdvertiseIdentity::No), CHIP_NO_ERROR);
EXPECT_FALSE(fabricInfo->ShouldAdvertiseIdentity());
}
size_t numStorageKeysAfterFirstAdd = storage.GetNumKeys();
EXPECT_EQ(numStorageKeysAfterFirstAdd, 7u); // Metadata, index, 3 certs, 1 opkey, last known good time
Expand Down Expand Up @@ -1155,6 +1177,14 @@ TEST_F(TestFabricTable, TestAddMultipleSameRootDifferentFabricId)
EXPECT_EQ(fabricInfo->GetFabricId(), 2222u);
EXPECT_EQ(fabricInfo->GetVendorId(), kVendorId);
EXPECT_EQ(fabricInfo->GetFabricLabel().size(), 0u);

EXPECT_TRUE(fabricInfo->ShouldAdvertiseIdentity());

EXPECT_EQ(fabricTable.SetShouldAdvertiseIdentity(newFabricIndex, FabricTable::AdvertiseIdentity::No), CHIP_NO_ERROR);
EXPECT_FALSE(fabricInfo->ShouldAdvertiseIdentity());

EXPECT_EQ(fabricTable.SetShouldAdvertiseIdentity(newFabricIndex, FabricTable::AdvertiseIdentity::Yes), CHIP_NO_ERROR);
EXPECT_TRUE(fabricInfo->ShouldAdvertiseIdentity());
}
size_t numStorageKeysAfterSecondAdd = storage.GetNumKeys();
EXPECT_EQ(numStorageKeysAfterSecondAdd, (numStorageKeysAfterFirstAdd + 5)); // Add 3 certs, 1 metadata, 1 opkey
Expand Down
Loading