Skip to content

Commit 7ec58e6

Browse files
committed
[nrf toup] Test FabricTable Delete
Test if Delete with cleanup flag invokes delegates, as this is needed to cleanup all fabric related settings after device is reset during AddNOC. Signed-off-by: Adrian Gielniewski <adrian.gielniewski@nordicsemi.no>
1 parent 1f297d4 commit 7ec58e6

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

src/credentials/tests/TestFabricTable.cpp

+76
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,15 @@
3131

3232
#include <credentials/FabricTable.h>
3333

34+
#include <credentials/GroupDataProviderImpl.h>
3435
#include <credentials/PersistentStorageOpCertStore.h>
3536
#include <credentials/TestOnlyLocalCertificateAuthority.h>
3637
#include <credentials/tests/CHIPCert_test_vectors.h>
3738
#include <crypto/CHIPCryptoPAL.h>
3839
#include <crypto/PersistentStorageOperationalKeystore.h>
3940
#include <lib/asn1/ASN1.h>
4041
#include <lib/support/CodeUtils.h>
42+
#include <lib/support/DefaultStorageKeyAllocator.h>
4143
#include <lib/support/TestPersistentStorageDelegate.h>
4244

4345
#include <platform/ConfigurationManager.h>
@@ -72,6 +74,16 @@ class ScopedFabricTable
7274
return mFabricTable.Init(initParams);
7375
}
7476

77+
CHIP_ERROR ReinitFabricTable(chip::TestPersistentStorageDelegate * storage)
78+
{
79+
chip::FabricTable::InitParams initParams;
80+
initParams.storage = storage;
81+
initParams.operationalKeystore = &mOpKeyStore;
82+
initParams.opCertStore = &mOpCertStore;
83+
84+
return mFabricTable.Init(initParams);
85+
}
86+
7587
FabricTable & GetFabricTable() { return mFabricTable; }
7688

7789
private:
@@ -2998,4 +3010,68 @@ TEST_F(TestFabricTable, TestCommitMarker)
29983010
#endif // CONFIG_BUILD_FOR_HOST_UNIT_TEST
29993011
}
30003012

3013+
class TestFabricTableDelegate : public FabricTable::Delegate
3014+
{
3015+
public:
3016+
void FabricWillBeRemoved(const FabricTable & fabricTable, FabricIndex fabricIndex) override { willBeRemovedCalled = true; }
3017+
3018+
void OnFabricRemoved(const FabricTable & fabricTable, FabricIndex fabricIndex) override { onRemovedCalled = true; }
3019+
3020+
bool willBeRemovedCalled = false;
3021+
bool onRemovedCalled = false;
3022+
};
3023+
3024+
TEST_F(TestFabricTable, Delete)
3025+
{
3026+
Credentials::TestOnlyLocalCertificateAuthority fabricCertAuthority;
3027+
EXPECT_TRUE(fabricCertAuthority.Init().IsSuccess());
3028+
3029+
chip::TestPersistentStorageDelegate storage;
3030+
ScopedFabricTable fabricTableHolder;
3031+
EXPECT_EQ(fabricTableHolder.Init(&storage), CHIP_NO_ERROR);
3032+
3033+
FabricTable & fabricTable = fabricTableHolder.GetFabricTable();
3034+
FabricId fabricId = 1;
3035+
NodeId nodeId = 10;
3036+
3037+
// Simulate AddNOC
3038+
uint8_t csrBuf[chip::Crypto::kMIN_CSR_Buffer_Size];
3039+
MutableByteSpan csrSpan{ csrBuf };
3040+
EXPECT_EQ(fabricTable.AllocatePendingOperationalKey(chip::NullOptional, csrSpan), CHIP_NO_ERROR);
3041+
3042+
EXPECT_EQ(fabricCertAuthority.SetIncludeIcac(true).GenerateNocChain(fabricId, nodeId, csrSpan).GetStatus(), CHIP_NO_ERROR);
3043+
ByteSpan rcac = fabricCertAuthority.GetRcac();
3044+
ByteSpan icac = fabricCertAuthority.GetIcac();
3045+
ByteSpan noc = fabricCertAuthority.GetNoc();
3046+
3047+
fabricTable.AddNewPendingTrustedRootCert(rcac);
3048+
3049+
constexpr uint16_t kVendorId = 0xFFF1u;
3050+
FabricIndex newFabricIndex = kUndefinedFabricIndex;
3051+
EXPECT_EQ(fabricTable.AddNewPendingFabricWithOperationalKeystore(noc, icac, kVendorId, &newFabricIndex), CHIP_NO_ERROR);
3052+
3053+
TestFabricTableDelegate fabricDelegate;
3054+
3055+
// Reinitialize FabricTable to simulate device reboot
3056+
EXPECT_EQ(fabricTableHolder.ReinitFabricTable(&storage), CHIP_NO_ERROR);
3057+
fabricTable.AddFabricDelegate(&fabricDelegate);
3058+
3059+
// Calling normal Delete doesn't invoke OnFabricRemoved on delegates
3060+
fabricTable.Delete(newFabricIndex);
3061+
EXPECT_TRUE(fabricDelegate.willBeRemovedCalled);
3062+
EXPECT_FALSE(fabricDelegate.onRemovedCalled);
3063+
3064+
// Reset delegate
3065+
fabricDelegate = TestFabricTableDelegate{};
3066+
3067+
// Reinitialize FabricTable to simulate device reboot
3068+
EXPECT_EQ(fabricTableHolder.ReinitFabricTable(&storage), CHIP_NO_ERROR);
3069+
fabricTable.AddFabricDelegate(&fabricDelegate);
3070+
3071+
// Calling Delete with cleanup flag innvokes OnFabricRemoved on delegates
3072+
fabricTable.Delete(newFabricIndex, true);
3073+
EXPECT_TRUE(fabricDelegate.willBeRemovedCalled);
3074+
EXPECT_TRUE(fabricDelegate.onRemovedCalled);
3075+
}
3076+
30013077
} // namespace

0 commit comments

Comments
 (0)