3 * Copyright (c) 2020 Project CHIP Authors
4 * Copyright (c) 2019-2020 Google LLC.
5 * Copyright (c) 2018 Nest Labs, Inc.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include <core/CHIPCore.h>
23 #include <support/CodeUtils.h>
26 namespace DeviceLayer {
29 template <class ConfigClass>
30 void RunConfigUnitTest()
34 // ===== Test 1: Store and read uint32_t
38 err = ConfigClass::WriteConfigValue(ConfigClass::kConfigKey_LastUsedEpochKeyId, v);
39 VerifyOrDie(err == CHIP_NO_ERROR);
43 err = ConfigClass::ReadConfigValue(ConfigClass::kConfigKey_LastUsedEpochKeyId, v);
44 VerifyOrDie(err == CHIP_NO_ERROR);
49 // ===== Test 2: Store and read uint64_t
51 uint64_t v = 9872349687345;
53 err = ConfigClass::WriteConfigValue(ConfigClass::kConfigKey_MfrDeviceId, v);
54 VerifyOrDie(err == CHIP_NO_ERROR);
58 err = ConfigClass::ReadConfigValue(ConfigClass::kConfigKey_MfrDeviceId, v);
59 VerifyOrDie(err == CHIP_NO_ERROR);
61 VerifyOrDie(v == 9872349687345);
64 // ===== Test 3: Store and read bool value
68 err = ConfigClass::WriteConfigValue(ConfigClass::kConfigKey_FailSafeArmed, v);
69 VerifyOrDie(err == CHIP_NO_ERROR);
73 err = ConfigClass::ReadConfigValue(ConfigClass::kConfigKey_FailSafeArmed, v);
74 VerifyOrDie(err == CHIP_NO_ERROR);
76 VerifyOrDie(v == true);
79 // ===== Test 4: Clear value
83 err = ConfigClass::ClearConfigValue(ConfigClass::kConfigKey_LastUsedEpochKeyId);
84 VerifyOrDie(err == CHIP_NO_ERROR);
86 err = ConfigClass::ReadConfigValue(ConfigClass::kConfigKey_LastUsedEpochKeyId, v);
87 VerifyOrDie(err == CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND);
90 // ===== Test 5: Store and read string
92 const static char kTestString1[] = "This is a test";
93 const static char kTestString2[] = "";
97 err = ConfigClass::WriteConfigValueStr(ConfigClass::kConfigKey_PairedAccountId, kTestString1);
98 VerifyOrDie(err == CHIP_NO_ERROR);
100 err = ConfigClass::ReadConfigValueStr(ConfigClass::kConfigKey_PairedAccountId, buf, sizeof(buf), strLen);
101 VerifyOrDie(err == CHIP_NO_ERROR);
103 VerifyOrDie(strLen == strlen(kTestString1));
104 VerifyOrDie(memcmp(buf, kTestString1, strLen + 1) == 0);
106 err = ConfigClass::WriteConfigValueStr(ConfigClass::kConfigKey_PairedAccountId, kTestString2);
107 VerifyOrDie(err == CHIP_NO_ERROR);
109 err = ConfigClass::ReadConfigValueStr(ConfigClass::kConfigKey_PairedAccountId, buf, sizeof(buf), strLen);
110 VerifyOrDie(err == CHIP_NO_ERROR);
112 VerifyOrDie(strLen == strlen(kTestString2));
113 VerifyOrDie(memcmp(buf, kTestString2, strLen + 1) == 0);
116 // ===== Test 6: Clear string
121 err = ConfigClass::WriteConfigValueStr(ConfigClass::kConfigKey_PairedAccountId, nullptr);
122 VerifyOrDie(err == CHIP_NO_ERROR);
124 err = ConfigClass::ReadConfigValueStr(ConfigClass::kConfigKey_PairedAccountId, buf, sizeof(buf), strLen);
125 VerifyOrDie(err == CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND);
128 // ===== Test 7: Store and read binary data
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,
148 err = ConfigClass::WriteConfigValueBin(ConfigClass::kConfigKey_MfrDeviceCert, kTestData, sizeof(kTestData));
149 VerifyOrDie(err == CHIP_NO_ERROR);
151 err = ConfigClass::ReadConfigValueBin(ConfigClass::kConfigKey_MfrDeviceCert, buf, sizeof(buf), dataLen);
152 VerifyOrDie(err == CHIP_NO_ERROR);
154 VerifyOrDie(dataLen == sizeof(kTestData));
155 VerifyOrDie(memcmp(buf, kTestData, dataLen) == 0);
158 // ===== Test 8: Clear binary data
163 err = ConfigClass::WriteConfigValueBin(ConfigClass::kConfigKey_MfrDeviceCert, nullptr, 0);
164 VerifyOrDie(err == CHIP_NO_ERROR);
166 err = ConfigClass::ReadConfigValueBin(ConfigClass::kConfigKey_MfrDeviceCert, buf, sizeof(buf), dataLen);
167 VerifyOrDie(err == CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND);
170 // ===== Test 9: Config value exists
174 v = ConfigClass::ConfigValueExists(ConfigClass::kConfigKey_MfrDeviceId);
175 VerifyOrDie(v == true);
177 v = ConfigClass::ConfigValueExists(ConfigClass::kConfigKey_FailSafeArmed);
178 VerifyOrDie(v == true);
180 v = ConfigClass::ConfigValueExists(ConfigClass::kConfigKey_MfrDeviceCert);
181 VerifyOrDie(v == false);
184 // ===== Test 10: Factory reset config
188 err = ConfigClass::FactoryResetConfig();
189 VerifyOrDie(err == CHIP_NO_ERROR);
191 v = ConfigClass::ConfigValueExists(ConfigClass::kConfigKey_MfrDeviceId);
192 VerifyOrDie(v == true);
194 v = ConfigClass::ConfigValueExists(ConfigClass::kConfigKey_FailSafeArmed);
195 VerifyOrDie(v == false);
199 } // namespace Internal
200 } // namespace DeviceLayer