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 Inet 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 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,
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,
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
84 static void CheckInetErrorStr(nlTestSuite * inSuite, void * inContext)
86 // Register the layer error formatter
88 Inet::RegisterLayerErrorFormatter();
90 // For each defined error...
91 for (int err : sContext)
93 const char * errStr = ErrorStr(err);
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));
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
109 * Test Suite. It lists all the test functions.
113 static const nlTest sTests[] =
115 NL_TEST_DEF("InetErrorStr", CheckInetErrorStr),
121 int TestInetErrorStr(void)
124 nlTestSuite theSuite =
126 "Inet-Error-Strings",
133 // Run test suit againt one context.
134 nlTestRunner(&theSuite, &sContext);
136 return (nlTestRunnerStats(&theSuite));
139 CHIP_REGISTER_TEST_SUITE(TestInetErrorStr)