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 some additional FabricTable unit tests #20534

Merged
merged 4 commits into from
Jul 11, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
34 changes: 23 additions & 11 deletions src/credentials/FabricTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1788,15 +1788,18 @@ CHIP_ERROR FabricTable::CommitPendingFabricData()
CHIP_ERROR err = CHIP_NO_ERROR;
if (hasInvalidInternalState)
{
ChipLogError(FabricProvisioning, "Failed to commit: internally inconsistent state!");
err = CHIP_ERROR_INTERNAL;
}
else if (haveNewTrustedRoot)
{
ChipLogError(FabricProvisioning,
"Failed to commit: tried to commit with only a new trusted root cert. No data committed.");
hasInvalidInternalState = true;
err = CHIP_ERROR_INCORRECT_STATE;
if (haveNewTrustedRoot)
{
ChipLogError(FabricProvisioning,
"Failed to commit: tried to commit with only a new trusted root cert. No data committed.");
tcarmelveilleux marked this conversation as resolved.
Show resolved Hide resolved
hasInvalidInternalState = true;
tcarmelveilleux marked this conversation as resolved.
Show resolved Hide resolved
err = CHIP_ERROR_INCORRECT_STATE;
}
else
{
ChipLogError(FabricProvisioning, "Failed to commit: internally inconsistent state!");
err = CHIP_ERROR_INTERNAL;
}
}
else
{
Expand Down Expand Up @@ -2005,13 +2008,13 @@ void FabricTable::RevertPendingOpCertsExceptRoot()
CHIP_ERROR FabricTable::SetFabricLabel(FabricIndex fabricIndex, const CharSpan & fabricLabel)
{
VerifyOrReturnError(mStorage != nullptr, CHIP_ERROR_INCORRECT_STATE);
VerifyOrReturnError(IsValidFabricIndex(fabricIndex), CHIP_ERROR_INVALID_ARGUMENT);
VerifyOrReturnError(IsValidFabricIndex(fabricIndex), CHIP_ERROR_INVALID_FABRIC_INDEX);

ReturnErrorCodeIf(fabricLabel.size() > kFabricLabelMaxLengthInBytes, CHIP_ERROR_INVALID_ARGUMENT);

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

// Update fabric table current in-memory entry, whether pending or not
ReturnErrorOnFailure(fabricInfo->SetFabricLabel(fabricLabel));
Expand All @@ -2025,4 +2028,13 @@ CHIP_ERROR FabricTable::SetFabricLabel(FabricIndex fabricIndex, const CharSpan &
return CHIP_NO_ERROR;
}

CHIP_ERROR FabricTable::GetFabricLabel(FabricIndex fabricIndex, CharSpan & outFabricLabel)
{
const FabricInfo * fabricInfo = FindFabricWithIndex(fabricIndex);
VerifyOrReturnError(fabricInfo != nullptr, CHIP_ERROR_INVALID_FABRIC_INDEX);

outFabricLabel = fabricInfo->GetFabricLabel();
return CHIP_NO_ERROR;
}

} // namespace chip
47 changes: 37 additions & 10 deletions src/credentials/FabricTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -435,12 +435,37 @@ class DLL_EXPORT FabricTable
CHIP_ERROR AddFabricDelegate(FabricTable::Delegate * delegate);
void RemoveFabricDelegate(FabricTable::Delegate * delegate);

// Set the Fabric Label for the given fabricIndex. If a fabric add/update is pending,
// only the pending version will be updated, so that on fail-safe expiry, you would
// actually see the only fabric label if Update fails. If the fabric label is
// set before UpdateNOC, then the change is immediate.
/**
* @brief Set the Fabric Label for the fabric referred by `fabricIndex`.
*
* If a fabric add/update is pending, only the pending version will be updated,
* so that on fail-safe expiry, you would actually see the only fabric label if
* Update fails. If the fabric label is set before UpdateNOC, then the change is immediate.
*
* @param fabricIndex - Fabric Index for which to set the label
* @param fabricLabel - Label to set on the fabric
* @retval CHIP_NO_ERROR on success
* @retval CHIP_ERROR_INVALID_FABRIC_INDEX if fabricIndex does not refer to an fabric in the table
* @retval CHIP_ERROR_INVALID_ARGUMENT on fabric label error (e.g. too large)
* @retval other CHIP_ERROR on internal errors
*/
CHIP_ERROR SetFabricLabel(FabricIndex fabricIndex, const CharSpan & fabricLabel);

/**
* @brief Get the Fabric Label for a given fabric
*
* NOTE: The outFabricLabel argument points to internal memory of the fabric info.
* It may become invalid on the next FabricTable API call due to shadow
* storage of data.
*
* @param fabricIndex - Fabric index for which to get the label
* @param outFabricLabel - char span that will be set to the label value
* @retval CHIP_NO_ERROR on success
* @retval CHIP_ERROR_INVALID_FABRIC_INDEX on error
* @retval other CHIP_ERROR on internal errors
*/
CHIP_ERROR GetFabricLabel(FabricIndex fabricIndex, CharSpan & outFabricLabel);

/**
* Get the current Last Known Good Time.
*
Expand Down Expand Up @@ -692,10 +717,11 @@ class DLL_EXPORT FabricTable
* @param vendorId - VendorID to use for the new fabric
* @param outNewFabricIndex - Pointer where the new fabric index for the fabric just added will be set. Cannot be nullptr.
*
* @retval CHIP_NO_ERROR on success
* @retval CHIP_ERROR_INCORRECT_STATE if this is called in an inconsistent order
* @retval CHIP_ERROR_NO_MEMORY if there is insufficient memory to make the fabric pending
* @retval CHIP_NO_ERROR on success.
* @retval CHIP_ERROR_INCORRECT_STATE if this is called in an inconsistent order.
* @retval CHIP_ERROR_NO_MEMORY if there is insufficient memory to make the fabric pending.
* @retval CHIP_ERROR_INVALID_ARGUMENT if any of the arguments are invalid such as too large or out of bounds.
* @retval CHIP_ERROR_FABRIC_EXISTS if operational identity collides with one already present.
* @retval other CHIP_ERROR_* on internal errors or certificate validation errors.
*/
CHIP_ERROR AddNewPendingFabricWithOperationalKeystore(const ByteSpan & noc, const ByteSpan & icac, uint16_t vendorId,
Expand Down Expand Up @@ -725,10 +751,11 @@ class DLL_EXPORT FabricTable
* copied using P256Keypair::Serialize/Deserialize and owned in heap of a FabricInfo.
* @param outNewFabricIndex - Pointer where the new fabric index for the fabric just added will be set. Cannot be nullptr.
*
* @retval CHIP_NO_ERROR on success
* @retval CHIP_ERROR_INCORRECT_STATE if this is called in an inconsistent order
* @retval CHIP_ERROR_NO_MEMORY if there is insufficient memory to make the fabric pending
* @retval CHIP_NO_ERROR on success.
* @retval CHIP_ERROR_INCORRECT_STATE if this is called in an inconsistent order.
* @retval CHIP_ERROR_NO_MEMORY if there is insufficient memory to make the fabric pending.
* @retval CHIP_ERROR_INVALID_ARGUMENT if any of the arguments are invalid such as too large or out of bounds.
* @retval CHIP_ERROR_FABRIC_EXISTS if operational identity collides with one already present.
* @retval other CHIP_ERROR_* on internal errors or certificate validation errors.
*/
CHIP_ERROR AddNewPendingFabricWithProvidedOpKey(const ByteSpan & noc, const ByteSpan & icac, uint16_t vendorId,
Expand Down
Loading