Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / include / platform / internal / testing / ConfigUnitTest.h
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 <core/CHIPCore.h>
23 #include <support/CodeUtils.h>
24
25 namespace chip {
26 namespace DeviceLayer {
27 namespace Internal {
28
29 template <class ConfigClass>
30 void RunConfigUnitTest()
31 {
32     CHIP_ERROR err;
33
34     // ===== Test 1: Store and read uint32_t
35     {
36         uint32_t v = 42;
37
38         err = ConfigClass::WriteConfigValue(ConfigClass::kConfigKey_LastUsedEpochKeyId, v);
39         VerifyOrDie(err == CHIP_NO_ERROR);
40
41         v = 0;
42
43         err = ConfigClass::ReadConfigValue(ConfigClass::kConfigKey_LastUsedEpochKeyId, v);
44         VerifyOrDie(err == CHIP_NO_ERROR);
45
46         VerifyOrDie(v == 42);
47     }
48
49     // ===== Test 2: Store and read uint64_t
50     {
51         uint64_t v = 9872349687345;
52
53         err = ConfigClass::WriteConfigValue(ConfigClass::kConfigKey_MfrDeviceId, v);
54         VerifyOrDie(err == CHIP_NO_ERROR);
55
56         v = 0;
57
58         err = ConfigClass::ReadConfigValue(ConfigClass::kConfigKey_MfrDeviceId, v);
59         VerifyOrDie(err == CHIP_NO_ERROR);
60
61         VerifyOrDie(v == 9872349687345);
62     }
63
64     // ===== Test 3: Store and read bool value
65     {
66         bool v = true;
67
68         err = ConfigClass::WriteConfigValue(ConfigClass::kConfigKey_FailSafeArmed, v);
69         VerifyOrDie(err == CHIP_NO_ERROR);
70
71         v = false;
72
73         err = ConfigClass::ReadConfigValue(ConfigClass::kConfigKey_FailSafeArmed, v);
74         VerifyOrDie(err == CHIP_NO_ERROR);
75
76         VerifyOrDie(v == true);
77     }
78
79     // ===== Test 4: Clear value
80     {
81         uint32_t v;
82
83         err = ConfigClass::ClearConfigValue(ConfigClass::kConfigKey_LastUsedEpochKeyId);
84         VerifyOrDie(err == CHIP_NO_ERROR);
85
86         err = ConfigClass::ReadConfigValue(ConfigClass::kConfigKey_LastUsedEpochKeyId, v);
87         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         err = ConfigClass::WriteConfigValueStr(ConfigClass::kConfigKey_PairedAccountId, kTestString1);
98         VerifyOrDie(err == CHIP_NO_ERROR);
99
100         err = ConfigClass::ReadConfigValueStr(ConfigClass::kConfigKey_PairedAccountId, buf, sizeof(buf), strLen);
101         VerifyOrDie(err == CHIP_NO_ERROR);
102
103         VerifyOrDie(strLen == strlen(kTestString1));
104         VerifyOrDie(memcmp(buf, kTestString1, strLen + 1) == 0);
105
106         err = ConfigClass::WriteConfigValueStr(ConfigClass::kConfigKey_PairedAccountId, kTestString2);
107         VerifyOrDie(err == CHIP_NO_ERROR);
108
109         err = ConfigClass::ReadConfigValueStr(ConfigClass::kConfigKey_PairedAccountId, buf, sizeof(buf), strLen);
110         VerifyOrDie(err == CHIP_NO_ERROR);
111
112         VerifyOrDie(strLen == strlen(kTestString2));
113         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         err = ConfigClass::WriteConfigValueStr(ConfigClass::kConfigKey_PairedAccountId, nullptr);
122         VerifyOrDie(err == CHIP_NO_ERROR);
123
124         err = ConfigClass::ReadConfigValueStr(ConfigClass::kConfigKey_PairedAccountId, buf, sizeof(buf), strLen);
125         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         err = ConfigClass::WriteConfigValueBin(ConfigClass::kConfigKey_MfrDeviceCert, kTestData, sizeof(kTestData));
149         VerifyOrDie(err == CHIP_NO_ERROR);
150
151         err = ConfigClass::ReadConfigValueBin(ConfigClass::kConfigKey_MfrDeviceCert, buf, sizeof(buf), dataLen);
152         VerifyOrDie(err == CHIP_NO_ERROR);
153
154         VerifyOrDie(dataLen == sizeof(kTestData));
155         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         err = ConfigClass::WriteConfigValueBin(ConfigClass::kConfigKey_MfrDeviceCert, nullptr, 0);
164         VerifyOrDie(err == CHIP_NO_ERROR);
165
166         err = ConfigClass::ReadConfigValueBin(ConfigClass::kConfigKey_MfrDeviceCert, buf, sizeof(buf), dataLen);
167         VerifyOrDie(err == CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND);
168     }
169
170     // ===== Test 9: Config value exists
171     {
172         bool v;
173
174         v = ConfigClass::ConfigValueExists(ConfigClass::kConfigKey_MfrDeviceId);
175         VerifyOrDie(v == true);
176
177         v = ConfigClass::ConfigValueExists(ConfigClass::kConfigKey_FailSafeArmed);
178         VerifyOrDie(v == true);
179
180         v = ConfigClass::ConfigValueExists(ConfigClass::kConfigKey_MfrDeviceCert);
181         VerifyOrDie(v == false);
182     }
183
184     // ===== Test 10: Factory reset config
185     {
186         bool v;
187
188         err = ConfigClass::FactoryResetConfig();
189         VerifyOrDie(err == CHIP_NO_ERROR);
190
191         v = ConfigClass::ConfigValueExists(ConfigClass::kConfigKey_MfrDeviceId);
192         VerifyOrDie(v == true);
193
194         v = ConfigClass::ConfigValueExists(ConfigClass::kConfigKey_FailSafeArmed);
195         VerifyOrDie(v == false);
196     }
197 }
198
199 } // namespace Internal
200 } // namespace DeviceLayer
201 } // namespace chip