|
| 1 | +/* |
| 2 | + * |
| 3 | + * Copyright (c) 2022 Project CHIP Authors |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +#include <app/util/ClientMonitoringRegistrationTable.h> |
| 19 | +#include <lib/core/CHIPError.h> |
| 20 | +#include <lib/support/DefaultStorageKeyAllocator.h> |
| 21 | +#include <lib/support/TestPersistentStorageDelegate.h> |
| 22 | +#include <lib/support/UnitTestRegistration.h> |
| 23 | +#include <nlunit-test.h> |
| 24 | + |
| 25 | +using chip::ClientMonitoringRegistrationTable; |
| 26 | +using chip::NullOptional; |
| 27 | + |
| 28 | +namespace { |
| 29 | + |
| 30 | +constexpr chip::FabricIndex kTestFabricIndex = 1; |
| 31 | +constexpr uint64_t kTestICid = 10; |
| 32 | +constexpr chip::NodeId kTestClientNodeId = 11; |
| 33 | + |
| 34 | +constexpr chip::FabricIndex kTestFabricIndex_2 = 2; |
| 35 | +constexpr uint64_t kTestICid_2 = 20; |
| 36 | +constexpr chip::NodeId kTestClientNodeId_2 = 21; |
| 37 | + |
| 38 | +void TestDefaultClientValues(nlTestSuite * aSuite, void * aContext) |
| 39 | +{ |
| 40 | + chip::TestPersistentStorageDelegate testStorage; |
| 41 | + ClientMonitoringRegistrationTable registration(testStorage); |
| 42 | + |
| 43 | + NL_TEST_ASSERT(aSuite, !registration.GetClientRegistrationEntry().IsValid()); |
| 44 | + NL_TEST_ASSERT(aSuite, registration.GetClientRegistrationEntry().clientNodeId == chip::kUndefinedFabricIndex); |
| 45 | + NL_TEST_ASSERT(aSuite, registration.GetClientRegistrationEntry().ICid == chip::kUndefinedNodeId); |
| 46 | + NL_TEST_ASSERT(aSuite, registration.GetClientRegistrationEntry().fabricIndex == chip::kInvalidIcId); |
| 47 | +} |
| 48 | + |
| 49 | +void TestLoadFromStorageEmptyValue(nlTestSuite * aSuite, void * aContext) |
| 50 | +{ |
| 51 | + chip::TestPersistentStorageDelegate testStorage; |
| 52 | + ClientMonitoringRegistrationTable registration(testStorage); |
| 53 | + |
| 54 | + CHIP_ERROR err = registration.LoadFromStorage(kTestFabricIndex); |
| 55 | + NL_TEST_ASSERT(aSuite, err == CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND); |
| 56 | +} |
| 57 | + |
| 58 | +void TestSaveAndLoadRegistrationValue(nlTestSuite * aSuite, void * aContext) |
| 59 | +{ |
| 60 | + chip::TestPersistentStorageDelegate testStorage; |
| 61 | + ClientMonitoringRegistrationTable savedRegistration(testStorage); |
| 62 | + ClientMonitoringRegistrationTable loadedRegistration(testStorage); |
| 63 | + |
| 64 | + savedRegistration.GetClientRegistrationEntry().clientNodeId = kTestClientNodeId; |
| 65 | + savedRegistration.GetClientRegistrationEntry().ICid = kTestICid; |
| 66 | + savedRegistration.GetClientRegistrationEntry().fabricIndex = kTestFabricIndex; |
| 67 | + |
| 68 | + CHIP_ERROR err = savedRegistration.SaveToStorage(); |
| 69 | + NL_TEST_ASSERT(aSuite, err == CHIP_NO_ERROR); |
| 70 | + |
| 71 | + err = loadedRegistration.LoadFromStorage(kTestFabricIndex); |
| 72 | + NL_TEST_ASSERT(aSuite, err == CHIP_NO_ERROR); |
| 73 | + |
| 74 | + NL_TEST_ASSERT(aSuite, loadedRegistration.GetClientRegistrationEntry().clientNodeId == kTestClientNodeId); |
| 75 | + NL_TEST_ASSERT(aSuite, loadedRegistration.GetClientRegistrationEntry().ICid == kTestICid); |
| 76 | + NL_TEST_ASSERT(aSuite, loadedRegistration.GetClientRegistrationEntry().fabricIndex == kTestFabricIndex); |
| 77 | +} |
| 78 | + |
| 79 | +void TestSaveLoadRegistrationValueForMultipleFabrics(nlTestSuite * aSuite, void * aContexT) |
| 80 | +{ |
| 81 | + chip::TestPersistentStorageDelegate testStorage; |
| 82 | + ClientMonitoringRegistrationTable savedRegistration(testStorage); |
| 83 | + ClientMonitoringRegistrationTable loadedRegistration(testStorage); |
| 84 | + |
| 85 | + savedRegistration.GetClientRegistrationEntry().clientNodeId = kTestClientNodeId; |
| 86 | + savedRegistration.GetClientRegistrationEntry().ICid = kTestICid; |
| 87 | + savedRegistration.GetClientRegistrationEntry().fabricIndex = kTestFabricIndex; |
| 88 | + |
| 89 | + CHIP_ERROR err = savedRegistration.SaveToStorage(); |
| 90 | + NL_TEST_ASSERT(aSuite, err == CHIP_NO_ERROR); |
| 91 | + |
| 92 | + savedRegistration.GetClientRegistrationEntry().clientNodeId = kTestClientNodeId_2; |
| 93 | + savedRegistration.GetClientRegistrationEntry().ICid = kTestICid_2; |
| 94 | + savedRegistration.GetClientRegistrationEntry().fabricIndex = kTestFabricIndex_2; |
| 95 | + |
| 96 | + err = savedRegistration.SaveToStorage(); |
| 97 | + NL_TEST_ASSERT(aSuite, err == CHIP_NO_ERROR); |
| 98 | + |
| 99 | + err = loadedRegistration.LoadFromStorage(kTestFabricIndex); |
| 100 | + NL_TEST_ASSERT(aSuite, err == CHIP_NO_ERROR); |
| 101 | + |
| 102 | + NL_TEST_ASSERT(aSuite, loadedRegistration.GetClientRegistrationEntry().clientNodeId == kTestClientNodeId); |
| 103 | + NL_TEST_ASSERT(aSuite, loadedRegistration.GetClientRegistrationEntry().ICid == kTestICid); |
| 104 | + NL_TEST_ASSERT(aSuite, loadedRegistration.GetClientRegistrationEntry().fabricIndex == kTestFabricIndex); |
| 105 | + |
| 106 | + err = loadedRegistration.LoadFromStorage(kTestFabricIndex_2); |
| 107 | + NL_TEST_ASSERT(aSuite, err == CHIP_NO_ERROR); |
| 108 | + |
| 109 | + NL_TEST_ASSERT(aSuite, loadedRegistration.GetClientRegistrationEntry().clientNodeId == kTestClientNodeId_2); |
| 110 | + NL_TEST_ASSERT(aSuite, loadedRegistration.GetClientRegistrationEntry().ICid == kTestICid_2); |
| 111 | + NL_TEST_ASSERT(aSuite, loadedRegistration.GetClientRegistrationEntry().fabricIndex == kTestFabricIndex_2); |
| 112 | +} |
| 113 | + |
| 114 | +void TestSaveAllInvalidRegistrationValues(nlTestSuite * aSuite, void * context) |
| 115 | +{ |
| 116 | + chip::TestPersistentStorageDelegate testStorage; |
| 117 | + ClientMonitoringRegistrationTable registration(testStorage); |
| 118 | + |
| 119 | + CHIP_ERROR err = registration.SaveToStorage(); |
| 120 | + NL_TEST_ASSERT(aSuite, err == CHIP_ERROR_INCORRECT_STATE); |
| 121 | +} |
| 122 | + |
| 123 | +} // namespace |
| 124 | + |
| 125 | +int TestClientMonitoringRegistrationTable() |
| 126 | +{ |
| 127 | + static nlTest sTests[] = { NL_TEST_DEF("TestDefaultClientValues", TestDefaultClientValues), |
| 128 | + NL_TEST_DEF("TestLoadFromStorageEmptyValue", TestLoadFromStorageEmptyValue), |
| 129 | + NL_TEST_DEF("TestSaveAndLoadRegistrationValue", TestSaveAndLoadRegistrationValue), |
| 130 | + NL_TEST_DEF("TestSaveAllInvalidRegistrationValues", TestSaveAllInvalidRegistrationValues), |
| 131 | + NL_TEST_DEF("TestSaveLoadRegistrationValueForMultipleFabrics", |
| 132 | + TestSaveLoadRegistrationValueForMultipleFabrics), |
| 133 | + NL_TEST_SENTINEL() }; |
| 134 | + |
| 135 | + nlTestSuite cmSuite = { "TestClientMonitoringRegistrationTable", &sTests[0], nullptr, nullptr }; |
| 136 | + |
| 137 | + nlTestRunner(&cmSuite, nullptr); |
| 138 | + return (nlTestRunnerStats(&cmSuite)); |
| 139 | +} |
| 140 | + |
| 141 | +CHIP_REGISTER_TEST_SUITE(TestClientMonitoringRegistrationTable) |
0 commit comments