Damian Królik | 16c7fea | 2022-03-08 13:18:23 +0100 | [diff] [blame] | 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/clusters/ota-requestor/DefaultOTARequestorStorage.h> |
| 19 | #include <app/clusters/ota-requestor/OTARequestorInterface.h> |
| 20 | #include <lib/core/CHIPPersistentStorageDelegate.h> |
| 21 | #include <lib/support/TestPersistentStorageDelegate.h> |
| 22 | #include <lib/support/UnitTestRegistration.h> |
| 23 | |
| 24 | #include <nlunit-test.h> |
| 25 | |
| 26 | using namespace chip; |
| 27 | using namespace chip::DeviceLayer; |
| 28 | |
| 29 | namespace { |
| 30 | |
| 31 | void TestDefaultProviders(nlTestSuite * inSuite, void * inContext) |
| 32 | { |
| 33 | TestPersistentStorageDelegate persistentStorage; |
| 34 | DefaultOTARequestorStorage otaStorage; |
| 35 | otaStorage.Init(persistentStorage); |
| 36 | |
| 37 | const auto makeProvider = [](FabricIndex fabric, NodeId nodeId, EndpointId endpointId) { |
| 38 | OTARequestorStorage::ProviderLocationType provider; |
| 39 | provider.fabricIndex = fabric; |
| 40 | provider.providerNodeID = nodeId; |
| 41 | provider.endpoint = endpointId; |
| 42 | return provider; |
| 43 | }; |
| 44 | |
| 45 | ProviderLocationList providers = {}; |
| 46 | NL_TEST_ASSERT(inSuite, CHIP_NO_ERROR == providers.Add(makeProvider(FabricIndex(1), NodeId(0x11111111), EndpointId(1)))); |
| 47 | NL_TEST_ASSERT(inSuite, CHIP_NO_ERROR == providers.Add(makeProvider(FabricIndex(2), NodeId(0x22222222), EndpointId(2)))); |
| 48 | NL_TEST_ASSERT(inSuite, CHIP_NO_ERROR == providers.Add(makeProvider(FabricIndex(3), NodeId(0x33333333), EndpointId(3)))); |
| 49 | NL_TEST_ASSERT(inSuite, CHIP_NO_ERROR == otaStorage.StoreDefaultProviders(providers)); |
| 50 | |
| 51 | providers = {}; |
| 52 | NL_TEST_ASSERT(inSuite, !providers.Begin().Next()); |
| 53 | NL_TEST_ASSERT(inSuite, CHIP_NO_ERROR == otaStorage.LoadDefaultProviders(providers)); |
| 54 | |
| 55 | auto provider = providers.Begin(); |
| 56 | bool hasNext; |
| 57 | |
| 58 | NL_TEST_ASSERT(inSuite, hasNext = provider.Next()); |
| 59 | |
| 60 | if (hasNext) |
| 61 | { |
| 62 | NL_TEST_ASSERT(inSuite, provider.GetValue().fabricIndex == 1); |
| 63 | NL_TEST_ASSERT(inSuite, provider.GetValue().providerNodeID == 0x11111111); |
| 64 | NL_TEST_ASSERT(inSuite, provider.GetValue().endpoint == 1); |
| 65 | } |
| 66 | |
| 67 | NL_TEST_ASSERT(inSuite, hasNext = provider.Next()); |
| 68 | |
| 69 | if (hasNext) |
| 70 | { |
| 71 | NL_TEST_ASSERT(inSuite, provider.GetValue().fabricIndex == 2); |
| 72 | NL_TEST_ASSERT(inSuite, provider.GetValue().providerNodeID == 0x22222222); |
| 73 | NL_TEST_ASSERT(inSuite, provider.GetValue().endpoint == 2); |
| 74 | } |
| 75 | |
| 76 | NL_TEST_ASSERT(inSuite, hasNext = provider.Next()); |
| 77 | |
| 78 | if (hasNext) |
| 79 | { |
| 80 | NL_TEST_ASSERT(inSuite, provider.GetValue().fabricIndex == 3); |
| 81 | NL_TEST_ASSERT(inSuite, provider.GetValue().providerNodeID == 0x33333333); |
| 82 | NL_TEST_ASSERT(inSuite, provider.GetValue().endpoint == 3); |
| 83 | } |
| 84 | |
| 85 | NL_TEST_ASSERT(inSuite, !provider.Next()); |
| 86 | } |
| 87 | |
| 88 | void TestDefaultProvidersEmpty(nlTestSuite * inSuite, void * inContext) |
| 89 | { |
| 90 | TestPersistentStorageDelegate persistentStorage; |
| 91 | DefaultOTARequestorStorage otaStorage; |
| 92 | otaStorage.Init(persistentStorage); |
| 93 | |
| 94 | ProviderLocationList providers = {}; |
| 95 | |
| 96 | NL_TEST_ASSERT(inSuite, CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND == otaStorage.LoadDefaultProviders(providers)); |
| 97 | NL_TEST_ASSERT(inSuite, !providers.Begin().Next()); |
| 98 | } |
| 99 | |
| 100 | void TestCurrentProviderLocation(nlTestSuite * inSuite, void * inContext) |
| 101 | { |
| 102 | TestPersistentStorageDelegate persistentStorage; |
| 103 | DefaultOTARequestorStorage otaStorage; |
| 104 | otaStorage.Init(persistentStorage); |
| 105 | |
| 106 | OTARequestorStorage::ProviderLocationType provider; |
| 107 | provider.fabricIndex = 1; |
| 108 | provider.providerNodeID = 0x12344321; |
| 109 | provider.endpoint = 10; |
| 110 | |
| 111 | NL_TEST_ASSERT(inSuite, CHIP_NO_ERROR == otaStorage.StoreCurrentProviderLocation(provider)); |
| 112 | |
| 113 | provider = {}; |
| 114 | |
| 115 | NL_TEST_ASSERT(inSuite, CHIP_NO_ERROR == otaStorage.LoadCurrentProviderLocation(provider)); |
| 116 | NL_TEST_ASSERT(inSuite, provider.fabricIndex == 1); |
| 117 | NL_TEST_ASSERT(inSuite, provider.providerNodeID == 0x12344321); |
| 118 | NL_TEST_ASSERT(inSuite, provider.endpoint == 10); |
| 119 | NL_TEST_ASSERT(inSuite, CHIP_NO_ERROR == otaStorage.ClearCurrentProviderLocation()); |
| 120 | NL_TEST_ASSERT(inSuite, CHIP_NO_ERROR != otaStorage.LoadCurrentProviderLocation(provider)); |
| 121 | } |
| 122 | |
| 123 | void TestUpdateToken(nlTestSuite * inSuite, void * inContext) |
| 124 | { |
| 125 | TestPersistentStorageDelegate persistentStorage; |
| 126 | DefaultOTARequestorStorage otaStorage; |
| 127 | otaStorage.Init(persistentStorage); |
| 128 | |
| 129 | constexpr size_t updateTokenLength = 32; |
| 130 | uint8_t updateTokenBuffer[updateTokenLength]; |
| 131 | ByteSpan updateToken(updateTokenBuffer); |
| 132 | |
| 133 | for (uint8_t i = 0; i < updateTokenLength; ++i) |
| 134 | updateTokenBuffer[i] = i; |
| 135 | |
| 136 | NL_TEST_ASSERT(inSuite, CHIP_NO_ERROR == otaStorage.StoreUpdateToken(updateToken)); |
| 137 | |
| 138 | uint8_t readBuffer[updateTokenLength + 10]; |
| 139 | MutableByteSpan readUpdateToken(readBuffer); |
| 140 | NL_TEST_ASSERT(inSuite, CHIP_NO_ERROR == otaStorage.LoadUpdateToken(readUpdateToken)); |
| 141 | NL_TEST_ASSERT(inSuite, readUpdateToken.size() == updateTokenLength); |
| 142 | |
| 143 | for (uint8_t i = 0; i < updateTokenLength; ++i) |
| 144 | NL_TEST_ASSERT(inSuite, readBuffer[i] == i); |
| 145 | |
| 146 | NL_TEST_ASSERT(inSuite, CHIP_NO_ERROR == otaStorage.ClearUpdateToken()); |
| 147 | NL_TEST_ASSERT(inSuite, CHIP_NO_ERROR != otaStorage.LoadUpdateToken(readUpdateToken)); |
| 148 | } |
| 149 | |
Carol Yang | 4505e87 | 2022-03-23 09:38:26 -0700 | [diff] [blame] | 150 | void TestCurrentUpdateState(nlTestSuite * inSuite, void * inContext) |
| 151 | { |
| 152 | TestPersistentStorageDelegate persistentStorage; |
| 153 | DefaultOTARequestorStorage otaStorage; |
| 154 | otaStorage.Init(persistentStorage); |
| 155 | |
| 156 | OTARequestorStorage::OTAUpdateStateEnum updateState = OTARequestorStorage::OTAUpdateStateEnum::kApplying; |
| 157 | |
| 158 | NL_TEST_ASSERT(inSuite, CHIP_NO_ERROR == otaStorage.StoreCurrentUpdateState(updateState)); |
| 159 | |
| 160 | updateState = OTARequestorStorage::OTAUpdateStateEnum::kUnknown; |
| 161 | |
| 162 | NL_TEST_ASSERT(inSuite, CHIP_NO_ERROR == otaStorage.LoadCurrentUpdateState(updateState)); |
| 163 | NL_TEST_ASSERT(inSuite, updateState == OTARequestorStorage::OTAUpdateStateEnum::kApplying); |
| 164 | NL_TEST_ASSERT(inSuite, CHIP_NO_ERROR == otaStorage.ClearCurrentUpdateState()); |
| 165 | NL_TEST_ASSERT(inSuite, CHIP_NO_ERROR != otaStorage.LoadCurrentUpdateState(updateState)); |
| 166 | } |
| 167 | |
| 168 | void TestTargetVersion(nlTestSuite * inSuite, void * inContext) |
| 169 | { |
| 170 | TestPersistentStorageDelegate persistentStorage; |
| 171 | DefaultOTARequestorStorage otaStorage; |
| 172 | otaStorage.Init(persistentStorage); |
| 173 | |
| 174 | uint32_t targetVersion = 2; |
| 175 | |
| 176 | NL_TEST_ASSERT(inSuite, CHIP_NO_ERROR == otaStorage.StoreTargetVersion(targetVersion)); |
| 177 | |
| 178 | targetVersion = 0; |
| 179 | |
| 180 | NL_TEST_ASSERT(inSuite, CHIP_NO_ERROR == otaStorage.LoadTargetVersion(targetVersion)); |
| 181 | NL_TEST_ASSERT(inSuite, targetVersion == 2); |
| 182 | NL_TEST_ASSERT(inSuite, CHIP_NO_ERROR == otaStorage.ClearTargetVersion()); |
| 183 | NL_TEST_ASSERT(inSuite, CHIP_NO_ERROR != otaStorage.LoadTargetVersion(targetVersion)); |
| 184 | } |
| 185 | |
Damian Królik | 16c7fea | 2022-03-08 13:18:23 +0100 | [diff] [blame] | 186 | const nlTest sTests[] = { NL_TEST_DEF("Test default providers", TestDefaultProviders), |
| 187 | NL_TEST_DEF("Test default providers (empty list)", TestDefaultProvidersEmpty), |
| 188 | NL_TEST_DEF("Test current provider location", TestCurrentProviderLocation), |
Carol Yang | 4505e87 | 2022-03-23 09:38:26 -0700 | [diff] [blame] | 189 | NL_TEST_DEF("Test update token", TestUpdateToken), |
| 190 | NL_TEST_DEF("Test current update state", TestCurrentUpdateState), |
| 191 | NL_TEST_DEF("Test target version", TestTargetVersion), |
| 192 | NL_TEST_SENTINEL() }; |
Damian Królik | 16c7fea | 2022-03-08 13:18:23 +0100 | [diff] [blame] | 193 | |
| 194 | int TestSetup(void * inContext) |
| 195 | { |
| 196 | return SUCCESS; |
| 197 | } |
| 198 | |
| 199 | int TestTearDown(void * inContext) |
| 200 | { |
| 201 | return SUCCESS; |
| 202 | } |
| 203 | |
| 204 | } // namespace |
| 205 | |
| 206 | int TestDefaultOTARequestorStorage() |
| 207 | { |
| 208 | nlTestSuite theSuite = { "OTA Storage tests", &sTests[0], TestSetup, TestTearDown }; |
| 209 | |
| 210 | // Run test suit againt one context. |
| 211 | nlTestRunner(&theSuite, nullptr); |
| 212 | return nlTestRunnerStats(&theSuite); |
| 213 | } |
| 214 | |
| 215 | CHIP_REGISTER_TEST_SUITE(TestDefaultOTARequestorStorage) |