Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / inet / tests / TestInetErrorStr.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 Inet 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     INET_ERROR_WRONG_ADDRESS_TYPE,
54     INET_ERROR_CONNECTION_ABORTED,
55     INET_ERROR_PEER_DISCONNECTED,
56     INET_ERROR_INCORRECT_STATE,
57     INET_ERROR_MESSAGE_TOO_LONG,
58     INET_ERROR_NO_CONNECTION_HANDLER,
59     INET_ERROR_NO_MEMORY,
60     INET_ERROR_OUTBOUND_MESSAGE_TRUNCATED,
61     INET_ERROR_INBOUND_MESSAGE_TOO_BIG,
62     INET_ERROR_HOST_NOT_FOUND,
63     INET_ERROR_DNS_TRY_AGAIN,
64     INET_ERROR_DNS_NO_RECOVERY,
65     INET_ERROR_BAD_ARGS,
66     INET_ERROR_WRONG_PROTOCOL_TYPE,
67     INET_ERROR_UNKNOWN_INTERFACE,
68     INET_ERROR_NOT_IMPLEMENTED,
69     INET_ERROR_ADDRESS_NOT_FOUND,
70     INET_ERROR_HOST_NAME_TOO_LONG,
71     INET_ERROR_INVALID_HOST_NAME,
72     INET_ERROR_NOT_SUPPORTED,
73     INET_ERROR_NO_ENDPOINTS,
74     INET_ERROR_IDLE_TIMEOUT,
75     INET_ERROR_UNEXPECTED_EVENT,
76     INET_ERROR_INVALID_IPV6_PKT,
77     INET_ERROR_INTERFACE_INIT_FAILURE,
78     INET_ERROR_TCP_USER_TIMEOUT,
79     INET_ERROR_TCP_CONNECT_TIMEOUT,
80     INET_ERROR_INCOMPATIBLE_IP_ADDRESS_TYPE
81 };
82 // clang-format on
83
84 static void CheckInetErrorStr(nlTestSuite * inSuite, void * inContext)
85 {
86     // Register the layer error formatter
87
88     Inet::RegisterLayerErrorFormatter();
89
90     // For each defined error...
91     for (int err : sContext)
92     {
93         const char * errStr = ErrorStr(err);
94         char expectedText[9];
95
96         // Assert that the error string contains the error number in hex.
97         snprintf(expectedText, sizeof(expectedText), "%08" PRIX32, err);
98         NL_TEST_ASSERT(inSuite, (strstr(errStr, expectedText) != nullptr));
99
100 #if !CHIP_CONFIG_SHORT_ERROR_STR
101         // Assert that the error string contains a description, which is signaled
102         // by a presence of a colon proceeding the description.
103         NL_TEST_ASSERT(inSuite, (strchr(errStr, ':') != nullptr));
104 #endif // !CHIP_CONFIG_SHORT_ERROR_STR
105     }
106 }
107
108 /**
109  *   Test Suite. It lists all the test functions.
110  */
111
112 // clang-format off
113 static const nlTest sTests[] =
114 {
115     NL_TEST_DEF("InetErrorStr", CheckInetErrorStr),
116
117     NL_TEST_SENTINEL()
118 };
119 // clang-format on
120
121 int TestInetErrorStr(void)
122 {
123     // clang-format off
124     nlTestSuite theSuite =
125         {
126         "Inet-Error-Strings",
127         &sTests[0],
128         nullptr,
129         nullptr
130     };
131     // clang-format on
132
133     // Run test suit againt one context.
134     nlTestRunner(&theSuite, &sContext);
135
136     return (nlTestRunnerStats(&theSuite));
137 }
138
139 CHIP_REGISTER_TEST_SUITE(TestInetErrorStr)