Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / system / tests / TestSystemErrorStr.cpp
1 /*
2  *
3  *    Copyright (c) 2020 Project CHIP Authors
4  *    Copyright (c) 2016-2017 Nest Labs, Inc.
5  *    All rights reserved.
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 /**
21  *    @file
22  *      This file implements a process to effect a functional test for
23  *      the CHIP System layer library error string support interfaces.
24  *
25  */
26
27 #ifndef __STDC_FORMAT_MACROS
28 #define __STDC_FORMAT_MACROS
29 #endif
30
31 #ifndef __STDC_LIMIT_MACROS
32 #define __STDC_LIMIT_MACROS
33 #endif
34
35 #include <inttypes.h>
36 #include <stdint.h>
37 #include <string.h>
38
39 #include <inet/InetError.h>
40 #include <support/CodeUtils.h>
41 #include <support/ErrorStr.h>
42 #include <support/UnitTestRegistration.h>
43
44 #include <nlunit-test.h>
45
46 using namespace chip;
47
48 // Test input data.
49
50 // clang-format off
51 static int32_t sContext[] =
52 {
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
61 };
62 // clang-format on
63
64 static void CheckSystemErrorStr(nlTestSuite * inSuite, void * inContext)
65 {
66     // Register the layer error formatter
67
68     System::RegisterLayerErrorFormatter();
69
70     // For each defined error...
71     for (int err : sContext)
72     {
73         const char * errStr = ErrorStr(err);
74         char expectedText[9];
75
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));
79
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
85     }
86 }
87
88 /**
89  *   Test Suite. It lists all the test functions.
90  */
91
92 // clang-format off
93 static const nlTest sTests[] =
94 {
95     NL_TEST_DEF("SystemErrorStr", CheckSystemErrorStr),
96
97     NL_TEST_SENTINEL()
98 };
99 // clang-format on
100
101 int TestSystemErrorStr(void)
102 {
103     // clang-format off
104     nlTestSuite theSuite =
105         {
106         "System-Error-Strings",
107         &sTests[0],
108         nullptr,
109         nullptr
110     };
111     // clang-format on
112
113     // Run test suit againt one context.
114     nlTestRunner(&theSuite, &sContext);
115
116     return (nlTestRunnerStats(&theSuite));
117 }
118
119 CHIP_REGISTER_TEST_SUITE(TestSystemErrorStr)