|
31 | 31 |
|
32 | 32 | #include <credentials/FabricTable.h>
|
33 | 33 |
|
| 34 | +#include <credentials/GroupDataProviderImpl.h> |
34 | 35 | #include <credentials/PersistentStorageOpCertStore.h>
|
35 | 36 | #include <credentials/TestOnlyLocalCertificateAuthority.h>
|
36 | 37 | #include <credentials/tests/CHIPCert_test_vectors.h>
|
37 | 38 | #include <crypto/CHIPCryptoPAL.h>
|
38 | 39 | #include <crypto/PersistentStorageOperationalKeystore.h>
|
39 | 40 | #include <lib/asn1/ASN1.h>
|
40 | 41 | #include <lib/support/CodeUtils.h>
|
| 42 | +#include <lib/support/DefaultStorageKeyAllocator.h> |
41 | 43 | #include <lib/support/TestPersistentStorageDelegate.h>
|
42 | 44 |
|
43 | 45 | #include <platform/ConfigurationManager.h>
|
@@ -72,6 +74,16 @@ class ScopedFabricTable
|
72 | 74 | return mFabricTable.Init(initParams);
|
73 | 75 | }
|
74 | 76 |
|
| 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 | + |
75 | 87 | FabricTable & GetFabricTable() { return mFabricTable; }
|
76 | 88 |
|
77 | 89 | private:
|
@@ -2998,4 +3010,68 @@ TEST_F(TestFabricTable, TestCommitMarker)
|
2998 | 3010 | #endif // CONFIG_BUILD_FOR_HOST_UNIT_TEST
|
2999 | 3011 | }
|
3000 | 3012 |
|
| 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 | + |
3001 | 3077 | } // namespace
|
0 commit comments