Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / ble / tests / TestBleErrorStr.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 BLE 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 <ble/BleError.h>
40 #include <support/ErrorStr.h>
41 #include <support/UnitTestRegistration.h>
42
43 #include <nlunit-test.h>
44
45 using namespace chip;
46
47 // Test input data.
48
49 // clang-format off
50 static int32_t sContext[] =
51 {
52     BLE_ERROR_BAD_ARGS,
53     BLE_ERROR_INCORRECT_STATE,
54     BLE_ERROR_NO_ENDPOINTS,
55     BLE_ERROR_NO_CONNECTION_RECEIVED_CALLBACK,
56     BLE_ERROR_CENTRAL_UNSUBSCRIBED,
57     BLE_ERROR_GATT_SUBSCRIBE_FAILED,
58     BLE_ERROR_GATT_UNSUBSCRIBE_FAILED,
59     BLE_ERROR_GATT_WRITE_FAILED,
60     BLE_ERROR_GATT_INDICATE_FAILED,
61     BLE_ERROR_NOT_IMPLEMENTED,
62     BLE_ERROR_CHIPOBLE_PROTOCOL_ABORT,
63     BLE_ERROR_REMOTE_DEVICE_DISCONNECTED,
64     BLE_ERROR_APP_CLOSED_CONNECTION,
65     BLE_ERROR_OUTBOUND_MESSAGE_TOO_BIG,
66     BLE_ERROR_NOT_CHIP_DEVICE,
67     BLE_ERROR_INCOMPATIBLE_PROTOCOL_VERSIONS,
68     BLE_ERROR_NO_MEMORY,
69     BLE_ERROR_MESSAGE_INCOMPLETE,
70     BLE_ERROR_INVALID_FRAGMENT_SIZE,
71     BLE_ERROR_START_TIMER_FAILED,
72     BLE_ERROR_CONNECT_TIMED_OUT,
73     BLE_ERROR_RECEIVE_TIMED_OUT,
74     BLE_ERROR_INVALID_MESSAGE,
75     BLE_ERROR_FRAGMENT_ACK_TIMED_OUT,
76     BLE_ERROR_KEEP_ALIVE_TIMED_OUT,
77     BLE_ERROR_NO_CONNECT_COMPLETE_CALLBACK,
78     BLE_ERROR_INVALID_ACK,
79     BLE_ERROR_REASSEMBLER_MISSING_DATA,
80     BLE_ERROR_INVALID_BTP_HEADER_FLAGS,
81     BLE_ERROR_INVALID_BTP_SEQUENCE_NUMBER,
82     BLE_ERROR_REASSEMBLER_INCORRECT_STATE,
83     BLE_ERROR_RECEIVED_MESSAGE_TOO_BIG
84 };
85 // clang-format on
86
87 static const size_t kTestElements = sizeof(sContext) / sizeof(sContext[0]);
88
89 static void CheckBleErrorStr(nlTestSuite * inSuite, void * inContext)
90 {
91     // Register the layer error formatter
92
93     Ble::RegisterLayerErrorFormatter();
94
95     // For each defined error...
96     for (size_t i = 0; i < kTestElements; i++)
97     {
98         int32_t err         = sContext[i];
99         const char * errStr = ErrorStr(err);
100         char expectedText[9];
101
102         // Assert that the error string contains the error number in hex.
103         snprintf(expectedText, sizeof(expectedText), "%08" PRIX32, err);
104         NL_TEST_ASSERT(inSuite, (strstr(errStr, expectedText) != NULL));
105
106 #if !CHIP_CONFIG_SHORT_ERROR_STR
107         // Assert that the error string contains a description, which is signaled
108         // by a presence of a colon proceeding the description.
109         NL_TEST_ASSERT(inSuite, (strchr(errStr, ':') != NULL));
110 #endif // !CHIP_CONFIG_SHORT_ERROR_STR
111     }
112 }
113
114 /**
115  *   Test Suite. It lists all the test functions.
116  */
117
118 // clang-format off
119 static const nlTest sTests[] =
120 {
121     NL_TEST_DEF("BleErrorStr", CheckBleErrorStr),
122
123     NL_TEST_SENTINEL()
124 };
125 // clang-format on
126
127 int TestBleErrorStr(void)
128 {
129     // clang-format off
130     nlTestSuite theSuite =
131         {
132         "Ble-Error-Strings",
133         &sTests[0],
134         NULL,
135         NULL
136     };
137     // clang-format on
138
139     // Run test suit againt one context.
140     nlTestRunner(&theSuite, &sContext);
141
142     return nlTestRunnerStats(&theSuite);
143 }
144
145 CHIP_REGISTER_TEST_SUITE(TestBleErrorStr)