Matter SDK Coverage Report
Current view: top level - include/platform/internal/testing - ConfigUnitTest.h (source / functions) Coverage Total Hit
Test: SHA:4d2388ac7eed75b2fe5e05e20de377999c632502 Lines: 100.0 % 65 65
Test Date: 2025-07-27 07:17:09 Functions: 100.0 % 1 1

            Line data    Source code
       1              : /*
       2              :  *
       3              :  *    Copyright (c) 2020 Project CHIP Authors
       4              :  *    Copyright (c) 2019-2020 Google LLC.
       5              :  *    Copyright (c) 2018 Nest Labs, Inc.
       6              :  *
       7              :  *    Licensed under the Apache License, Version 2.0 (the "License");
       8              :  *    you may not use this file except in compliance with the License.
       9              :  *    You may obtain a copy of the License at
      10              :  *
      11              :  *        http://www.apache.org/licenses/LICENSE-2.0
      12              :  *
      13              :  *    Unless required by applicable law or agreed to in writing, software
      14              :  *    distributed under the License is distributed on an "AS IS" BASIS,
      15              :  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      16              :  *    See the License for the specific language governing permissions and
      17              :  *    limitations under the License.
      18              :  */
      19              : 
      20              : #pragma once
      21              : 
      22              : #include <lib/core/CHIPCore.h>
      23              : #include <lib/support/CodeUtils.h>
      24              : 
      25              : namespace chip {
      26              : namespace DeviceLayer {
      27              : namespace Internal {
      28              : 
      29              : template <class ConfigClass>
      30            1 : void RunConfigUnitTest()
      31              : {
      32              :     CHIP_ERROR err;
      33              : 
      34              :     // ===== Test 1: Store and read uint32_t
      35              :     {
      36            1 :         uint32_t v = 42;
      37              : 
      38            1 :         err = ConfigClass::WriteConfigValue(ConfigClass::kConfigKey_LastUsedEpochKeyId, v);
      39            1 :         VerifyOrDie(err == CHIP_NO_ERROR);
      40              : 
      41            1 :         v = 0;
      42              : 
      43            1 :         err = ConfigClass::ReadConfigValue(ConfigClass::kConfigKey_LastUsedEpochKeyId, v);
      44            1 :         VerifyOrDie(err == CHIP_NO_ERROR);
      45              : 
      46            1 :         VerifyOrDie(v == 42);
      47              :     }
      48              : 
      49              :     // ===== Test 2: Store and read uint64_t
      50              :     {
      51            1 :         uint64_t v = 9872349687345;
      52              : 
      53            1 :         err = ConfigClass::WriteConfigValue(ConfigClass::kConfigKey_MfrDeviceId, v);
      54            1 :         VerifyOrDie(err == CHIP_NO_ERROR);
      55              : 
      56            1 :         v = 0;
      57              : 
      58            1 :         err = ConfigClass::ReadConfigValue(ConfigClass::kConfigKey_MfrDeviceId, v);
      59            1 :         VerifyOrDie(err == CHIP_NO_ERROR);
      60              : 
      61            1 :         VerifyOrDie(v == 9872349687345);
      62              :     }
      63              : 
      64              :     // ===== Test 3: Store and read bool value
      65              :     {
      66            1 :         bool v = true;
      67              : 
      68            1 :         err = ConfigClass::WriteConfigValue(ConfigClass::kConfigKey_FailSafeArmed, v);
      69            1 :         VerifyOrDie(err == CHIP_NO_ERROR);
      70              : 
      71            1 :         v = false;
      72              : 
      73            1 :         err = ConfigClass::ReadConfigValue(ConfigClass::kConfigKey_FailSafeArmed, v);
      74            1 :         VerifyOrDie(err == CHIP_NO_ERROR);
      75              : 
      76            1 :         VerifyOrDie(v == true);
      77              :     }
      78              : 
      79              :     // ===== Test 4: Clear value
      80              :     {
      81              :         uint32_t v;
      82              : 
      83            1 :         err = ConfigClass::ClearConfigValue(ConfigClass::kConfigKey_LastUsedEpochKeyId);
      84            1 :         VerifyOrDie(err == CHIP_NO_ERROR);
      85              : 
      86            1 :         err = ConfigClass::ReadConfigValue(ConfigClass::kConfigKey_LastUsedEpochKeyId, v);
      87            1 :         VerifyOrDie(err == CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND);
      88              :     }
      89              : 
      90              :     // ===== Test 5: Store and read string
      91              :     {
      92              :         const static char kTestString1[] = "This is a test";
      93              :         const static char kTestString2[] = "";
      94              :         char buf[64];
      95              :         size_t strLen;
      96              : 
      97            1 :         err = ConfigClass::WriteConfigValueStr(ConfigClass::kConfigKey_PairedAccountId, kTestString1);
      98            1 :         VerifyOrDie(err == CHIP_NO_ERROR);
      99              : 
     100            1 :         err = ConfigClass::ReadConfigValueStr(ConfigClass::kConfigKey_PairedAccountId, buf, sizeof(buf), strLen);
     101            1 :         VerifyOrDie(err == CHIP_NO_ERROR);
     102              : 
     103            1 :         VerifyOrDie(strLen == strlen(kTestString1));
     104            1 :         VerifyOrDie(memcmp(buf, kTestString1, strLen + 1) == 0);
     105              : 
     106            1 :         err = ConfigClass::WriteConfigValueStr(ConfigClass::kConfigKey_PairedAccountId, kTestString2);
     107            1 :         VerifyOrDie(err == CHIP_NO_ERROR);
     108              : 
     109            1 :         err = ConfigClass::ReadConfigValueStr(ConfigClass::kConfigKey_PairedAccountId, buf, sizeof(buf), strLen);
     110            1 :         VerifyOrDie(err == CHIP_NO_ERROR);
     111              : 
     112            1 :         VerifyOrDie(strLen == strlen(kTestString2));
     113            1 :         VerifyOrDie(memcmp(buf, kTestString2, strLen + 1) == 0);
     114              :     }
     115              : 
     116              :     // ===== Test 6: Clear string
     117              :     {
     118              :         char buf[64];
     119              :         size_t strLen;
     120              : 
     121            1 :         err = ConfigClass::WriteConfigValueStr(ConfigClass::kConfigKey_PairedAccountId, nullptr);
     122            1 :         VerifyOrDie(err == CHIP_NO_ERROR);
     123              : 
     124            1 :         err = ConfigClass::ReadConfigValueStr(ConfigClass::kConfigKey_PairedAccountId, buf, sizeof(buf), strLen);
     125            1 :         VerifyOrDie(err == CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND);
     126              :     }
     127              : 
     128              :     // ===== Test 7: Store and read binary data
     129              :     {
     130              :         const static uint8_t kTestData[] = {
     131              :             0xD5, 0x00, 0x00, 0x04, 0x00, 0x01, 0x00, 0x30, 0x01, 0x08, 0x79, 0x55, 0x9F, 0x15, 0x1F, 0x66, 0x3D, 0x8F, 0x24,
     132              :             0x02, 0x05, 0x37, 0x03, 0x27, 0x13, 0x02, 0x00, 0x00, 0xEE, 0xEE, 0x30, 0xB4, 0x18, 0x18, 0x26, 0x04, 0x80, 0x41,
     133              :             0x1B, 0x23, 0x26, 0x05, 0x7F, 0xFF, 0xFF, 0x52, 0x37, 0x06, 0x27, 0x11, 0x01, 0x00, 0x00, 0x00, 0x00, 0x30, 0xB4,
     134              :             0x18, 0x18, 0x24, 0x07, 0x02, 0x26, 0x08, 0x25, 0x00, 0x5A, 0x23, 0x30, 0x0A, 0x39, 0x04, 0x9E, 0xC7, 0x77, 0xC5,
     135              :             0xA4, 0x13, 0x31, 0xF7, 0x72, 0x2E, 0x27, 0xC2, 0x86, 0x3D, 0xC5, 0x2E, 0xD5, 0xD2, 0x3C, 0xCF, 0x7E, 0x06, 0xE3,
     136              :             0x48, 0x53, 0x87, 0xE8, 0x4D, 0xB0, 0x27, 0x07, 0x58, 0x4A, 0x38, 0xB4, 0xF3, 0xB2, 0x47, 0x94, 0x45, 0x58, 0x65,
     137              :             0x80, 0x08, 0x17, 0x6B, 0x8E, 0x4F, 0x07, 0x41, 0xA3, 0x3D, 0x5D, 0xCE, 0x76, 0x86, 0x35, 0x83, 0x29, 0x01, 0x18,
     138              :             0x35, 0x82, 0x29, 0x01, 0x24, 0x02, 0x05, 0x18, 0x35, 0x84, 0x29, 0x01, 0x36, 0x02, 0x04, 0x02, 0x04, 0x01, 0x18,
     139              :             0x18, 0x35, 0x81, 0x30, 0x02, 0x08, 0x42, 0xBD, 0x2C, 0x6B, 0x5B, 0x3A, 0x18, 0x16, 0x18, 0x35, 0x80, 0x30, 0x02,
     140              :             0x08, 0x44, 0xE3, 0x40, 0x38, 0xA9, 0xD4, 0xB5, 0xA7, 0x18, 0x35, 0x0C, 0x30, 0x01, 0x19, 0x00, 0xA6, 0x5D, 0x54,
     141              :             0xF5, 0xAE, 0x5D, 0x63, 0xEB, 0x69, 0xD8, 0xDB, 0xCB, 0xE2, 0x20, 0x0C, 0xD5, 0x6F, 0x43, 0x5E, 0x96, 0xA8, 0x54,
     142              :             0xB2, 0x74, 0x30, 0x02, 0x19, 0x00, 0xE0, 0x37, 0x02, 0x8B, 0xB3, 0x04, 0x06, 0xDD, 0xBD, 0x28, 0xAA, 0xC4, 0xF1,
     143              :             0xFF, 0xFB, 0xB1, 0xD4, 0x1C, 0x78, 0x40, 0xDA, 0x2C, 0xD8, 0x40, 0x18, 0x18,
     144              :         };
     145              :         uint8_t buf[512];
     146              :         size_t dataLen;
     147              : 
     148            1 :         err = ConfigClass::WriteConfigValueBin(ConfigClass::kConfigKey_MfrDeviceCert, kTestData, sizeof(kTestData));
     149            1 :         VerifyOrDie(err == CHIP_NO_ERROR);
     150              : 
     151            1 :         err = ConfigClass::ReadConfigValueBin(ConfigClass::kConfigKey_MfrDeviceCert, buf, sizeof(buf), dataLen);
     152            1 :         VerifyOrDie(err == CHIP_NO_ERROR);
     153              : 
     154            1 :         VerifyOrDie(dataLen == sizeof(kTestData));
     155            1 :         VerifyOrDie(memcmp(buf, kTestData, dataLen) == 0);
     156              :     }
     157              : 
     158              :     // ===== Test 8: Clear binary data
     159              :     {
     160              :         uint8_t buf[512];
     161              :         size_t dataLen;
     162              : 
     163            1 :         err = ConfigClass::WriteConfigValueBin(ConfigClass::kConfigKey_MfrDeviceCert, nullptr, 0);
     164            1 :         VerifyOrDie(err == CHIP_NO_ERROR);
     165              : 
     166            1 :         err = ConfigClass::ReadConfigValueBin(ConfigClass::kConfigKey_MfrDeviceCert, buf, sizeof(buf), dataLen);
     167            1 :         VerifyOrDie(err == CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND);
     168              :     }
     169              : 
     170              :     // ===== Test 9: Config value exists
     171              :     {
     172              :         bool v;
     173              : 
     174            1 :         v = ConfigClass::ConfigValueExists(ConfigClass::kConfigKey_MfrDeviceId);
     175            1 :         VerifyOrDie(v == true);
     176              : 
     177            1 :         v = ConfigClass::ConfigValueExists(ConfigClass::kConfigKey_FailSafeArmed);
     178            1 :         VerifyOrDie(v == true);
     179              : 
     180            1 :         v = ConfigClass::ConfigValueExists(ConfigClass::kConfigKey_MfrDeviceCert);
     181            1 :         VerifyOrDie(v == false);
     182              :     }
     183              : 
     184              :     // ===== Test 10: Factory reset config
     185              :     {
     186              :         bool v;
     187              : 
     188            1 :         err = ConfigClass::FactoryResetConfig();
     189            1 :         VerifyOrDie(err == CHIP_NO_ERROR);
     190              : 
     191            1 :         v = ConfigClass::ConfigValueExists(ConfigClass::kConfigKey_MfrDeviceId);
     192            1 :         VerifyOrDie(v == true);
     193              : 
     194            1 :         v = ConfigClass::ConfigValueExists(ConfigClass::kConfigKey_FailSafeArmed);
     195            1 :         VerifyOrDie(v == false);
     196              :     }
     197            1 : }
     198              : 
     199              : } // namespace Internal
     200              : } // namespace DeviceLayer
     201              : } // namespace chip
        

Generated by: LCOV version 2.0-1