Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / platform / tests / TestConfigurationMgr.cpp
1 /*
2  *
3  *    Copyright (c) 2020 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 /**
19  *    @file
20  *      This file implements a unit test suite for the Configuration Manager
21  *      code functionality.
22  *
23  */
24
25 #include <inttypes.h>
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31 #include <nlunit-test.h>
32 #include <support/CHIPMem.h>
33 #include <support/CodeUtils.h>
34 #include <support/UnitTestRegistration.h>
35
36 #include <platform/CHIPDeviceLayer.h>
37
38 using namespace chip;
39 using namespace chip::Logging;
40 using namespace chip::Inet;
41 using namespace chip::DeviceLayer;
42
43 // =================================
44 //      Unit tests
45 // =================================
46
47 static void TestPlatformMgr_Init(nlTestSuite * inSuite, void * inContext)
48 {
49     // ConfigurationManager is initialized from PlatformManager indirectly
50     CHIP_ERROR err = PlatformMgr().InitChipStack();
51     NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR);
52 }
53
54 #if !defined(NDEBUG)
55 static void TestPlatformMgr_RunUnitTest(nlTestSuite * inSuite, void * inContext)
56 {
57     CHIP_ERROR err = CHIP_NO_ERROR;
58
59     err = ConfigurationMgr().RunUnitTests();
60     NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR);
61 }
62 #endif
63
64 static void TestConfigurationMgr_SerialNumber(nlTestSuite * inSuite, void * inContext)
65 {
66     CHIP_ERROR err = CHIP_NO_ERROR;
67
68     char buf[64];
69     size_t serialNumberLen    = 0;
70     const char * serialNumber = "89051AAZZ236";
71
72     err = ConfigurationMgr().StoreSerialNumber(serialNumber, strlen(serialNumber));
73     NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR);
74
75     err = ConfigurationMgr().GetSerialNumber(buf, 64, serialNumberLen);
76     NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR);
77
78     NL_TEST_ASSERT(inSuite, serialNumberLen == strlen(serialNumber));
79     NL_TEST_ASSERT(inSuite, strcmp(buf, serialNumber) == 0);
80
81     err = ConfigurationMgr().StoreSerialNumber(serialNumber, 5);
82     NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR);
83
84     err = ConfigurationMgr().GetSerialNumber(buf, 64, serialNumberLen);
85     NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR);
86
87     NL_TEST_ASSERT(inSuite, serialNumberLen == 5);
88     NL_TEST_ASSERT(inSuite, strcmp(buf, "89051") == 0);
89 }
90
91 static void TestConfigurationMgr_ManufacturingDate(nlTestSuite * inSuite, void * inContext)
92 {
93     CHIP_ERROR err = CHIP_NO_ERROR;
94
95     const char * mfgDate = "2008/09/20";
96     uint16_t year;
97     uint8_t month;
98     uint8_t dayOfMonth;
99
100     err = ConfigurationMgr().StoreManufacturingDate(mfgDate, strlen(mfgDate));
101     NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR);
102
103     err = ConfigurationMgr().GetManufacturingDate(year, month, dayOfMonth);
104     NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR);
105
106     NL_TEST_ASSERT(inSuite, year == 2008);
107     NL_TEST_ASSERT(inSuite, month == 9);
108     NL_TEST_ASSERT(inSuite, dayOfMonth == 20);
109 }
110
111 static void TestConfigurationMgr_ProductRevision(nlTestSuite * inSuite, void * inContext)
112 {
113     CHIP_ERROR err = CHIP_NO_ERROR;
114     uint16_t productRev;
115
116     err = ConfigurationMgr().StoreProductRevision(1234);
117     NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR);
118
119     err = ConfigurationMgr().GetProductRevision(productRev);
120     NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR);
121
122     NL_TEST_ASSERT(inSuite, productRev == 1234);
123 }
124
125 static void TestConfigurationMgr_ManufacturerDeviceId(nlTestSuite * inSuite, void * inContext)
126 {
127     CHIP_ERROR err = CHIP_NO_ERROR;
128     uint64_t deviceId;
129
130     err = ConfigurationMgr().StoreManufacturerDeviceId(7212064004600625234);
131     NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR);
132
133     err = ConfigurationMgr().GetManufacturerDeviceId(deviceId);
134     NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR);
135
136     NL_TEST_ASSERT(inSuite, deviceId == 7212064004600625234);
137 }
138
139 static void TestConfigurationMgr_ManufacturerDeviceCertificate(nlTestSuite * inSuite, void * inContext)
140 {
141     CHIP_ERROR err              = CHIP_NO_ERROR;
142     const static uint8_t cert[] = {
143         0xD5, 0x00, 0x00, 0x04, 0x00, 0x01, 0x00, 0x30, 0x01, 0x08, 0x79, 0x55, 0x9F, 0x15, 0x1F, 0x66, 0x3D, 0x8F, 0x24,
144         0x02, 0x05, 0x37, 0x03, 0x27, 0x13, 0x02, 0x00, 0x00, 0xEE, 0xEE, 0x30, 0xB4, 0x18, 0x18, 0x26, 0x04, 0x80, 0x41,
145         0x1B, 0x23, 0x26, 0x05, 0x7F, 0xFF, 0xFF, 0x52, 0x37, 0x06, 0x27, 0x11, 0x01, 0x00, 0x00, 0x00, 0x00, 0x30, 0xB4,
146         0x18, 0x18, 0x24, 0x07, 0x02, 0x26, 0x08, 0x25, 0x00, 0x5A, 0x23, 0x30, 0x0A, 0x39, 0x04, 0x9E, 0xC7, 0x77, 0xC5,
147         0xA4, 0x13, 0x31, 0xF7, 0x72, 0x2E, 0x27, 0xC2, 0x86, 0x3D, 0xC5, 0x2E, 0xD5, 0xD2, 0x3C, 0xCF, 0x7E, 0x06, 0xE3,
148         0x48, 0x53, 0x87, 0xE8, 0x4D, 0xB0, 0x27, 0x07, 0x58, 0x4A, 0x38, 0xB4, 0xF3, 0xB2, 0x47, 0x94, 0x45, 0x58, 0x65,
149         0x80, 0x08, 0x17, 0x6B, 0x8E, 0x4F, 0x07, 0x41, 0xA3, 0x3D, 0x5D, 0xCE, 0x76, 0x86, 0x35, 0x83, 0x29, 0x01, 0x18,
150         0x35, 0x82, 0x29, 0x01, 0x24, 0x02, 0x05, 0x18, 0x35, 0x84, 0x29, 0x01, 0x36, 0x02, 0x04, 0x02, 0x04, 0x01, 0x18,
151         0x18, 0x35, 0x81, 0x30, 0x02, 0x08, 0x42, 0xBD, 0x2C, 0x6B, 0x5B, 0x3A, 0x18, 0x16, 0x18, 0x35, 0x80, 0x30, 0x02,
152         0x08, 0x44, 0xE3, 0x40, 0x38, 0xA9, 0xD4, 0xB5, 0xA7, 0x18, 0x35, 0x0C, 0x30, 0x01, 0x19, 0x00, 0xA6, 0x5D, 0x54,
153         0xF5, 0xAE, 0x5D, 0x63, 0xEB, 0x69, 0xD8, 0xDB, 0xCB, 0xE2, 0x20, 0x0C, 0xD5, 0x6F, 0x43, 0x5E, 0x96, 0xA8, 0x54,
154         0xB2, 0x74, 0x30, 0x02, 0x19, 0x00, 0xE0, 0x37, 0x02, 0x8B, 0xB3, 0x04, 0x06, 0xDD, 0xBD, 0x28, 0xAA, 0xC4, 0xF1,
155         0xFF, 0xFB, 0xB1, 0xD4, 0x1C, 0x78, 0x40, 0xDA, 0x2C, 0xD8, 0x40, 0x18, 0x18,
156     };
157     uint8_t buf[512];
158     size_t certLen;
159
160     err = ConfigurationMgr().StoreManufacturerDeviceCertificate(cert, sizeof(cert));
161     NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR);
162
163     err = ConfigurationMgr().GetManufacturerDeviceCertificate(buf, sizeof(buf), certLen);
164     NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR);
165
166     NL_TEST_ASSERT(inSuite, certLen == sizeof(cert));
167     NL_TEST_ASSERT(inSuite, memcmp(buf, cert, certLen) == 0);
168 }
169
170 static void TestConfigurationMgr_ManufacturerDeviceIntermediateCACerts(nlTestSuite * inSuite, void * inContext)
171 {
172     CHIP_ERROR err               = CHIP_NO_ERROR;
173     const static uint8_t certs[] = {
174         0xD5, 0x00, 0x00, 0x04, 0x00, 0x01, 0x00, 0x30, 0x01, 0x08, 0x79, 0x55, 0x9F, 0x15, 0x1F, 0x66, 0x3D, 0x8F, 0x24,
175         0x02, 0x05, 0x37, 0x03, 0x27, 0x13, 0x02, 0x00, 0x00, 0xEE, 0xEE, 0x30, 0xB4, 0x18, 0x18, 0x26, 0x04, 0x80, 0x41,
176         0x1B, 0x23, 0x26, 0x05, 0x7F, 0xFF, 0xFF, 0x52, 0x37, 0x06, 0x27, 0x11, 0x01, 0x00, 0x00, 0x00, 0x00, 0x30, 0xB4,
177         0x18, 0x18, 0x24, 0x07, 0x02, 0x26, 0x08, 0x25, 0x00, 0x5A, 0x23, 0x30, 0x0A, 0x39, 0x04, 0x9E, 0xC7, 0x77, 0xC5,
178         0xA4, 0x13, 0x31, 0xF7, 0x72, 0x2E, 0x27, 0xC2, 0x86, 0x3D, 0xC5, 0x2E, 0xD5, 0xD2, 0x3C, 0xCF, 0x7E, 0x06, 0xE3,
179         0x48, 0x53, 0x87, 0xE8, 0x4D, 0xB0, 0x27, 0x07, 0x58, 0x4A, 0x38, 0xB4, 0xF3, 0xB2, 0x47, 0x94, 0x45, 0x58, 0x65,
180         0x80, 0x08, 0x17, 0x6B, 0x8E, 0x4F, 0x07, 0x41, 0xA3, 0x3D, 0x5D, 0xCE, 0x76, 0x86, 0x35, 0x83, 0x29, 0x01, 0x18,
181         0x35, 0x82, 0x29, 0x01, 0x24, 0x02, 0x05, 0x18, 0x35, 0x84, 0x29, 0x01, 0x36, 0x02, 0x04, 0x02, 0x04, 0x01, 0x18,
182         0x18, 0x35, 0x81, 0x30, 0x02, 0x08, 0x42, 0xBD, 0x2C, 0x6B, 0x5B, 0x3A, 0x18, 0x16, 0x18, 0x35, 0x80, 0x30, 0x02,
183         0x08, 0x44, 0xE3, 0x40, 0x38, 0xA9, 0xD4, 0xB5, 0xA7, 0x18, 0x35, 0x0C, 0x30, 0x01, 0x19, 0x00, 0xA6, 0x5D, 0x54,
184         0xF5, 0xAE, 0x5D, 0x63, 0xEB, 0x69, 0xD8, 0xDB, 0xCB, 0xE2, 0x20, 0x0C, 0xD5, 0x6F, 0x43, 0x5E, 0x96, 0xA8, 0x54,
185         0xB2, 0x74, 0x30, 0x02, 0x19, 0x00, 0xE0, 0x37, 0x02, 0x8B, 0xB3, 0x04, 0x06, 0xDD, 0xBD, 0x28, 0xAA, 0xC4, 0xF1,
186         0xFF, 0xFB, 0xB1, 0xD4, 0x1C, 0x78, 0x40, 0xDA, 0x2C, 0xD8, 0x40, 0x18, 0x18,
187     };
188     uint8_t buf[512];
189     size_t certsLen;
190
191     err = ConfigurationMgr().StoreManufacturerDeviceCertificate(certs, sizeof(certs));
192     NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR);
193
194     err = ConfigurationMgr().GetManufacturerDeviceCertificate(buf, sizeof(buf), certsLen);
195     NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR);
196
197     NL_TEST_ASSERT(inSuite, certsLen == sizeof(certs));
198     NL_TEST_ASSERT(inSuite, memcmp(buf, certs, certsLen) == 0);
199 }
200
201 static void TestConfigurationMgr_ManufacturerDevicePrivateKey(nlTestSuite * inSuite, void * inContext)
202 {
203     CHIP_ERROR err             = CHIP_NO_ERROR;
204     const static uint8_t key[] = {
205         0x80, 0x08, 0x17, 0x6B, 0x8E, 0x4F, 0x07, 0x41, 0xA3, 0x3D, 0x5D, 0xCE, 0x76, 0x86, 0x35, 0x83, 0x29, 0x01, 0x18,
206         0x35, 0x82, 0x29, 0x01, 0x24, 0x02, 0x05, 0x18, 0x35, 0x84, 0x29, 0x01, 0x36, 0x02, 0x04, 0x02, 0x04, 0x01, 0x18,
207         0x18, 0x35, 0x81, 0x30, 0x02, 0x08, 0x42, 0xBD, 0x2C, 0x6B, 0x5B, 0x3A, 0x18, 0x16, 0x18, 0x35, 0x80, 0x30, 0x02,
208     };
209     uint8_t buf[128];
210     size_t keyLen;
211
212     err = ConfigurationMgr().StoreManufacturerDevicePrivateKey(key, sizeof(key));
213     NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR);
214
215     err = ConfigurationMgr().GetManufacturerDevicePrivateKey(buf, sizeof(buf), keyLen);
216     NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR);
217
218     NL_TEST_ASSERT(inSuite, keyLen == sizeof(key));
219     NL_TEST_ASSERT(inSuite, memcmp(buf, key, keyLen) == 0);
220 }
221
222 static void TestConfigurationMgr_FabricId(nlTestSuite * inSuite, void * inContext)
223 {
224 #if CHIP_CONFIG_ENABLE_FABRIC_STATE
225     CHIP_ERROR err    = CHIP_NO_ERROR;
226     uint64_t fabricId = 0;
227
228     err = ConfigurationMgr().StoreFabricId(2006255910626445ULL);
229     NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR);
230
231     err = ConfigurationMgr().GetFabricId(fabricId);
232     NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR);
233
234     NL_TEST_ASSERT(inSuite, fabricId == 2006255910626445ULL);
235 #endif
236 }
237
238 static void TestConfigurationMgr_ServiceConfig(nlTestSuite * inSuite, void * inContext)
239 {
240     CHIP_ERROR err = CHIP_NO_ERROR;
241
242     uint8_t buf[1024];
243     size_t serviceConfigLen              = 0;
244     const static uint8_t serviceConfig[] = {
245         0x1B, 0x23, 0x26, 0x05, 0x7F, 0xFF, 0xFF, 0x52, 0x37, 0x06, 0x27, 0x11, 0x01, 0x00, 0x00, 0x00, 0x00, 0x30, 0xB4, 0x18,
246         0x18, 0x24, 0x07, 0x02, 0x26, 0x08, 0x25, 0x00, 0x5A, 0x23, 0x30, 0x0A, 0x39, 0x04, 0x9E, 0xC7, 0x77, 0xC5, 0xA4, 0x13,
247         0x31, 0xF7, 0x72, 0x2E, 0x27, 0xC2, 0x86, 0x3D, 0xC5, 0x2E, 0xD5, 0xD2, 0x3C, 0xCF, 0x7E, 0x06, 0xE3, 0x48, 0x53, 0x87,
248         0xE8, 0x4D, 0xB0, 0x27, 0x07, 0x58, 0x4A, 0x38, 0xB4, 0xF3, 0xB2, 0x47, 0x94, 0x45, 0x58, 0x65, 0x80, 0x08, 0x17, 0x6B,
249         0x8E, 0x4F, 0x07, 0x41, 0xA3, 0x3D, 0x5D, 0xCE, 0x76, 0x86, 0x35, 0x83, 0x29, 0x01, 0x18, 0x35, 0x82, 0x29, 0x01, 0x24,
250         0x02, 0x05, 0x18, 0x35, 0x84, 0x29, 0x01, 0x36, 0x02, 0x04, 0x02, 0x04, 0x01, 0x18, 0x18, 0x35, 0x81, 0x30, 0x02, 0x08,
251         0x42, 0xBD, 0x2C, 0x6B, 0x5B, 0x3A, 0x18, 0x16, 0x18, 0x35, 0x80, 0x30, 0x02, 0x08, 0x44, 0xE3, 0x40, 0x38, 0xA9, 0xD4,
252         0xB5, 0xA7, 0x18, 0x35, 0x0C, 0x30, 0x01, 0x19, 0x00, 0xA6, 0x5D, 0x54, 0xD5, 0x00, 0x00, 0x04, 0x00, 0x01, 0x00, 0x30,
253         0x01, 0x08, 0x79, 0x55, 0x9F, 0x15, 0x1F, 0x66, 0x3D, 0x8F, 0x24, 0x02, 0x05, 0x37, 0x03, 0x27, 0x13, 0x02, 0x00, 0x00,
254         0xEE, 0xEE, 0x30, 0xB4, 0x18, 0x18, 0x26, 0x04, 0x80, 0x41, 0x1B, 0x23, 0x26, 0x05, 0x7F, 0xFF, 0xFF, 0x52, 0x37, 0x06,
255         0x27, 0x11, 0x01, 0x00, 0x00, 0x00, 0x00, 0x30, 0xB4, 0x18, 0x18, 0x24, 0x07, 0x02, 0x26, 0x08, 0x25, 0x00, 0x5A, 0x23,
256         0x30, 0x0A, 0x39, 0x04, 0x9E, 0xC7, 0x77, 0xC5, 0xA4, 0x13, 0x31, 0xF7, 0x72, 0x2E, 0x27, 0xC2, 0x86, 0x3D, 0xC5, 0x2E,
257         0xD5, 0xD2, 0x3C, 0xCF, 0x7E, 0x06, 0xE3, 0x48, 0x53, 0x87, 0xE8, 0x4D, 0xB0, 0x27, 0x07, 0x58, 0x4A, 0x38, 0xB4, 0xF3,
258         0xB2, 0x47, 0x94, 0x45, 0x58, 0x65, 0x80, 0x08, 0x17, 0x6B, 0x8E, 0x4F, 0x07, 0x41, 0xA3, 0x3D, 0x5D, 0xCE, 0x76, 0x86,
259         0x35, 0x83, 0x29, 0x01, 0x18, 0x35, 0x82, 0x29, 0x01, 0x24, 0x02, 0x05, 0x18, 0x35, 0x84, 0x29, 0x01, 0x36, 0x02, 0x04,
260         0x02, 0x04, 0x01, 0x18, 0x18, 0x35, 0x81, 0x30, 0x02, 0x08, 0x42, 0xBD, 0x2C, 0x6B, 0x5B, 0x3A, 0x18, 0x16, 0x18, 0x35,
261         0x80, 0x30, 0x02, 0x08, 0x44, 0xE3, 0x40, 0x38, 0xA9, 0xD4, 0xB5, 0xA7, 0x18, 0x35, 0x0C, 0x30, 0x01, 0x19, 0x00, 0xA6,
262         0x5D, 0x54, 0xF5, 0xAE, 0x5D, 0x63, 0xEB, 0x69, 0xD8, 0xDB, 0xCB, 0xE2, 0x20, 0x0C, 0xD5, 0x6F, 0x43, 0x5E, 0x96, 0xA8,
263         0x54, 0xB2, 0x74, 0x30, 0x02, 0x19, 0x00, 0xE0, 0x37, 0x02, 0x8B, 0xB3, 0x04, 0x06, 0xDD, 0xBD, 0x28, 0xAA, 0xC4, 0xF1,
264         0xFF, 0xFB, 0xB1, 0xD4, 0x1C, 0x78, 0x40, 0xDA, 0x2C, 0xD8, 0x40, 0x18, 0x18, 0x18, 0x02, 0x40
265     };
266     err = ConfigurationMgr().StoreServiceConfig(serviceConfig, sizeof(serviceConfig));
267     NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR);
268
269     err = ConfigurationMgr().GetServiceConfig(buf, 1024, serviceConfigLen);
270     NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR);
271
272     NL_TEST_ASSERT(inSuite, serviceConfigLen == sizeof(serviceConfig));
273     NL_TEST_ASSERT(inSuite, memcmp(buf, serviceConfig, serviceConfigLen) == 0);
274 }
275
276 static void TestConfigurationMgr_SetupPinCode(nlTestSuite * inSuite, void * inContext)
277 {
278     CHIP_ERROR err = CHIP_NO_ERROR;
279
280     const uint32_t setSetupPinCode = 34567890;
281     uint32_t getSetupPinCode       = 0;
282
283     err = ConfigurationMgr().StoreSetupPinCode(setSetupPinCode);
284     NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR);
285
286     err = ConfigurationMgr().GetSetupPinCode(getSetupPinCode);
287     NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR);
288
289     NL_TEST_ASSERT(inSuite, getSetupPinCode == setSetupPinCode);
290 }
291
292 static void TestConfigurationMgr_SetupDiscriminator(nlTestSuite * inSuite, void * inContext)
293 {
294     CHIP_ERROR err = CHIP_NO_ERROR;
295
296     const uint16_t setSetupDiscriminator = 0xBA0;
297     uint16_t getSetupDiscriminator       = 0;
298
299     err = ConfigurationMgr().StoreSetupDiscriminator(setSetupDiscriminator);
300     NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR);
301
302     err = ConfigurationMgr().GetSetupDiscriminator(getSetupDiscriminator);
303     NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR);
304
305     NL_TEST_ASSERT(inSuite, getSetupDiscriminator == setSetupDiscriminator);
306 }
307
308 static void TestConfigurationMgr_PairedAccountId(nlTestSuite * inSuite, void * inContext)
309 {
310     CHIP_ERROR err = CHIP_NO_ERROR;
311
312     char buf[64];
313     size_t accountIdLen    = 0;
314     const char * accountId = "USER_016CB664A86A888D";
315
316     err = ConfigurationMgr().StorePairedAccountId(accountId, strlen(accountId));
317     NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR);
318
319     err = ConfigurationMgr().GetPairedAccountId(buf, 64, accountIdLen);
320     NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR);
321
322     NL_TEST_ASSERT(inSuite, accountIdLen == strlen(accountId));
323     NL_TEST_ASSERT(inSuite, strcmp(buf, "USER_016CB664A86A888D") == 0);
324 }
325
326 static void TestConfigurationMgr_ServiceProvisioningData(nlTestSuite * inSuite, void * inContext)
327 {
328     CHIP_ERROR err = CHIP_NO_ERROR;
329
330     uint64_t serviceId;
331
332     char account[64];
333     size_t accountIdLen    = 0;
334     const char * accountId = "USER_016CB664A86A888D";
335
336     uint8_t buf[1024];
337     size_t serviceConfigLen              = 0;
338     const static uint8_t serviceConfig[] = {
339         0x1B, 0x23, 0x26, 0x05, 0x7F, 0xFF, 0xFF, 0x52, 0x37, 0x06, 0x27, 0x11, 0x01, 0x00, 0x00, 0x00, 0x00, 0x30, 0xB4, 0x18,
340         0x18, 0x24, 0x07, 0x02, 0x26, 0x08, 0x25, 0x00, 0x5A, 0x23, 0x30, 0x0A, 0x39, 0x04, 0x9E, 0xC7, 0x77, 0xC5, 0xA4, 0x13,
341         0x31, 0xF7, 0x72, 0x2E, 0x27, 0xC2, 0x86, 0x3D, 0xC5, 0x2E, 0xD5, 0xD2, 0x3C, 0xCF, 0x7E, 0x06, 0xE3, 0x48, 0x53, 0x87,
342         0xE8, 0x4D, 0xB0, 0x27, 0x07, 0x58, 0x4A, 0x38, 0xB4, 0xF3, 0xB2, 0x47, 0x94, 0x45, 0x58, 0x65, 0x80, 0x08, 0x17, 0x6B,
343         0x8E, 0x4F, 0x07, 0x41, 0xA3, 0x3D, 0x5D, 0xCE, 0x76, 0x86, 0x35, 0x83, 0x29, 0x01, 0x18, 0x35, 0x82, 0x29, 0x01, 0x24,
344         0x02, 0x05, 0x18, 0x35, 0x84, 0x29, 0x01, 0x36, 0x02, 0x04, 0x02, 0x04, 0x01, 0x18, 0x18, 0x35, 0x81, 0x30, 0x02, 0x08,
345         0x42, 0xBD, 0x2C, 0x6B, 0x5B, 0x3A, 0x18, 0x16, 0x18, 0x35, 0x80, 0x30, 0x02, 0x08, 0x44, 0xE3, 0x40, 0x38, 0xA9, 0xD4,
346         0xB5, 0xA7, 0x18, 0x35, 0x0C, 0x30, 0x01, 0x19, 0x00, 0xA6, 0x5D, 0x54, 0xD5, 0x00, 0x00, 0x04, 0x00, 0x01, 0x00, 0x30,
347         0x01, 0x08, 0x79, 0x55, 0x9F, 0x15, 0x1F, 0x66, 0x3D, 0x8F, 0x24, 0x02, 0x05, 0x37, 0x03, 0x27, 0x13, 0x02, 0x00, 0x00,
348         0xEE, 0xEE, 0x30, 0xB4, 0x18, 0x18, 0x26, 0x04, 0x80, 0x41, 0x1B, 0x23, 0x26, 0x05, 0x7F, 0xFF, 0xFF, 0x52, 0x37, 0x06,
349         0x27, 0x11, 0x01, 0x00, 0x00, 0x00, 0x00, 0x30, 0xB4, 0x18, 0x18, 0x24, 0x07, 0x02, 0x26, 0x08, 0x25, 0x00, 0x5A, 0x23,
350         0x30, 0x0A, 0x39, 0x04, 0x9E, 0xC7, 0x77, 0xC5, 0xA4, 0x13, 0x31, 0xF7, 0x72, 0x2E, 0x27, 0xC2, 0x86, 0x3D, 0xC5, 0x2E,
351         0xD5, 0xD2, 0x3C, 0xCF, 0x7E, 0x06, 0xE3, 0x48, 0x53, 0x87, 0xE8, 0x4D, 0xB0, 0x27, 0x07, 0x58, 0x4A, 0x38, 0xB4, 0xF3,
352         0xB2, 0x47, 0x94, 0x45, 0x58, 0x65, 0x80, 0x08, 0x17, 0x6B, 0x8E, 0x4F, 0x07, 0x41, 0xA3, 0x3D, 0x5D, 0xCE, 0x76, 0x86,
353         0x35, 0x83, 0x29, 0x01, 0x18, 0x35, 0x82, 0x29, 0x01, 0x24, 0x02, 0x05, 0x18, 0x35, 0x84, 0x29, 0x01, 0x36, 0x02, 0x04,
354         0x02, 0x04, 0x01, 0x18, 0x18, 0x35, 0x81, 0x30, 0x02, 0x08, 0x42, 0xBD, 0x2C, 0x6B, 0x5B, 0x3A, 0x18, 0x16, 0x18, 0x35,
355         0x80, 0x30, 0x02, 0x08, 0x44, 0xE3, 0x40, 0x38, 0xA9, 0xD4, 0xB5, 0xA7, 0x18, 0x35, 0x0C, 0x30, 0x01, 0x19, 0x00, 0xA6,
356         0x5D, 0x54, 0xF5, 0xAE, 0x5D, 0x63, 0xEB, 0x69, 0xD8, 0xDB, 0xCB, 0xE2, 0x20, 0x0C, 0xD5, 0x6F, 0x43, 0x5E, 0x96, 0xA8,
357         0x54, 0xB2, 0x74, 0x30, 0x02, 0x19, 0x00, 0xE0, 0x37, 0x02, 0x8B, 0xB3, 0x04, 0x06, 0xDD, 0xBD, 0x28, 0xAA, 0xC4, 0xF1,
358         0xFF, 0xFB, 0xB1, 0xD4, 0x1C, 0x78, 0x40, 0xDA, 0x2C, 0xD8, 0x40, 0x18, 0x18, 0x18, 0x02, 0x40
359     };
360
361     // Test write/clear
362     err = ConfigurationMgr().StoreServiceProvisioningData(7212064004600625234, serviceConfig, sizeof(serviceConfig), accountId,
363                                                           strlen(accountId));
364     NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR);
365
366     err = ConfigurationMgr().ClearServiceProvisioningData();
367     NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR);
368
369     err = ConfigurationMgr().GetServiceId(serviceId);
370     NL_TEST_ASSERT(inSuite, err != CHIP_NO_ERROR);
371
372     err = ConfigurationMgr().GetPairedAccountId(account, 64, accountIdLen);
373     NL_TEST_ASSERT(inSuite, err != CHIP_NO_ERROR);
374
375     err = ConfigurationMgr().GetServiceConfig(buf, 512, serviceConfigLen);
376     NL_TEST_ASSERT(inSuite, err != CHIP_NO_ERROR);
377
378     // Test write/read
379     err = ConfigurationMgr().StoreServiceProvisioningData(7212064004600625234, serviceConfig, sizeof(serviceConfig), accountId,
380                                                           strlen(accountId));
381     NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR);
382
383     err = ConfigurationMgr().GetServiceId(serviceId);
384     NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR);
385     NL_TEST_ASSERT(inSuite, serviceId == 7212064004600625234);
386
387     err = ConfigurationMgr().GetPairedAccountId(account, 64, accountIdLen);
388     NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR);
389     NL_TEST_ASSERT(inSuite, accountIdLen == strlen(accountId));
390     NL_TEST_ASSERT(inSuite, strcmp(account, "USER_016CB664A86A888D") == 0);
391
392     err = ConfigurationMgr().GetServiceConfig(buf, 512, serviceConfigLen);
393     NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR);
394     NL_TEST_ASSERT(inSuite, serviceConfigLen == sizeof(serviceConfig));
395     NL_TEST_ASSERT(inSuite, memcmp(buf, serviceConfig, serviceConfigLen) == 0);
396 }
397
398 /**
399  *   Test Suite. It lists all the test functions.
400  */
401 static const nlTest sTests[] = {
402
403     NL_TEST_DEF("Test PlatformMgr::Init", TestPlatformMgr_Init),
404 #if !defined(NDEBUG)
405     NL_TEST_DEF("Test PlatformMgr::RunUnitTest", TestPlatformMgr_RunUnitTest),
406 #endif
407     NL_TEST_DEF("Test ConfigurationMgr::SerialNumber", TestConfigurationMgr_SerialNumber),
408     NL_TEST_DEF("Test ConfigurationMgr::ManufacturingDate", TestConfigurationMgr_ManufacturingDate),
409     NL_TEST_DEF("Test ConfigurationMgr::ProductRevision", TestConfigurationMgr_ProductRevision),
410     NL_TEST_DEF("Test ConfigurationMgr::ManufacturerDeviceId", TestConfigurationMgr_ManufacturerDeviceId),
411     NL_TEST_DEF("Test ConfigurationMgr::ManufacturerDeviceCertificate", TestConfigurationMgr_ManufacturerDeviceCertificate),
412     NL_TEST_DEF("Test ConfigurationMgr::ManufacturerDeviceIntermediateCACerts",
413                 TestConfigurationMgr_ManufacturerDeviceIntermediateCACerts),
414     NL_TEST_DEF("Test ConfigurationMgr::ManufacturerDevicePrivateKey", TestConfigurationMgr_ManufacturerDevicePrivateKey),
415     NL_TEST_DEF("Test ConfigurationMgr::SetupPinCode", TestConfigurationMgr_SetupPinCode),
416     NL_TEST_DEF("Test ConfigurationMgr::SetupDiscriminator", TestConfigurationMgr_SetupDiscriminator),
417     NL_TEST_DEF("Test ConfigurationMgr::FabricId", TestConfigurationMgr_FabricId),
418     NL_TEST_DEF("Test ConfigurationMgr::ServiceConfig", TestConfigurationMgr_ServiceConfig),
419     NL_TEST_DEF("Test ConfigurationMgr::PairedAccountId", TestConfigurationMgr_PairedAccountId),
420     NL_TEST_DEF("Test ConfigurationMgr::ServiceProvisioningData", TestConfigurationMgr_ServiceProvisioningData),
421     NL_TEST_SENTINEL()
422 };
423
424 /**
425  *  Set up the test suite.
426  */
427 int TestConfigurationMgr_Setup(void * inContext)
428 {
429     CHIP_ERROR error = chip::Platform::MemoryInit();
430     if (error != CHIP_NO_ERROR)
431         return FAILURE;
432     return SUCCESS;
433 }
434
435 /**
436  *  Tear down the test suite.
437  */
438 int TestConfigurationMgr_Teardown(void * inContext)
439 {
440     chip::Platform::MemoryShutdown();
441     return SUCCESS;
442 }
443
444 int TestConfigurationMgr()
445 {
446     nlTestSuite theSuite = { "ConfigurationMgr tests", &sTests[0], TestConfigurationMgr_Setup, TestConfigurationMgr_Teardown };
447
448     // Run test suit againt one context.
449     nlTestRunner(&theSuite, nullptr);
450     return nlTestRunnerStats(&theSuite);
451 }
452
453 CHIP_REGISTER_TEST_SUITE(TestConfigurationMgr)