3 * Copyright (c) 2020 Project CHIP Authors
4 * Copyright (c) 2016-2017 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 * This file implements a process to effect a functional test for
23 * the CHIP System layer library error string support interfaces.
27 #ifndef __STDC_FORMAT_MACROS
28 #define __STDC_FORMAT_MACROS
31 #ifndef __STDC_LIMIT_MACROS
32 #define __STDC_LIMIT_MACROS
39 #include <inet/InetError.h>
40 #include <support/CodeUtils.h>
41 #include <support/ErrorStr.h>
42 #include <support/UnitTestRegistration.h>
44 #include <nlunit-test.h>
51 static int32_t sContext[] =
53 CHIP_SYSTEM_ERROR_NOT_IMPLEMENTED,
54 CHIP_SYSTEM_ERROR_NOT_SUPPORTED,
55 CHIP_SYSTEM_ERROR_BAD_ARGS,
56 CHIP_SYSTEM_ERROR_UNEXPECTED_STATE,
57 CHIP_SYSTEM_ERROR_UNEXPECTED_EVENT,
58 CHIP_SYSTEM_ERROR_NO_MEMORY,
59 CHIP_SYSTEM_ERROR_REAL_TIME_NOT_SYNCED,
60 CHIP_SYSTEM_ERROR_ACCESS_DENIED
64 static void CheckSystemErrorStr(nlTestSuite * inSuite, void * inContext)
66 // Register the layer error formatter
68 System::RegisterLayerErrorFormatter();
70 // For each defined error...
71 for (int err : sContext)
73 const char * errStr = ErrorStr(err);
76 // Assert that the error string contains the error number in hex.
77 snprintf(expectedText, sizeof(expectedText), "%08" PRIX32, err);
78 NL_TEST_ASSERT(inSuite, (strstr(errStr, expectedText) != nullptr));
80 #if !CHIP_CONFIG_SHORT_ERROR_STR
81 // Assert that the error string contains a description, which is signaled
82 // by a presence of a colon proceeding the description.
83 NL_TEST_ASSERT(inSuite, (strchr(errStr, ':') != nullptr));
84 #endif // !CHIP_CONFIG_SHORT_ERROR_STR
89 * Test Suite. It lists all the test functions.
93 static const nlTest sTests[] =
95 NL_TEST_DEF("SystemErrorStr", CheckSystemErrorStr),
101 int TestSystemErrorStr(void)
104 nlTestSuite theSuite =
106 "System-Error-Strings",
113 // Run test suit againt one context.
114 nlTestRunner(&theSuite, &sContext);
116 return (nlTestRunnerStats(&theSuite));
119 CHIP_REGISTER_TEST_SUITE(TestSystemErrorStr)