Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / lib / support / tests / TestErrorStr.cpp
1 /*
2  *
3  *    Copyright (c) 2020 Project CHIP Authors
4  *    All rights reserved.
5  *
6  *    Licensed under the Apache License, Version 2.0 (the "License");
7  *    you may not use this file except in compliance with the License.
8  *    You may obtain a copy of the License at
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *    Unless required by applicable law or agreed to in writing, software
13  *    distributed under the License is distributed on an "AS IS" BASIS,
14  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *    See the License for the specific language governing permissions and
16  *    limitations under the License.
17  */
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21
22 #include <core/CHIPCore.h>
23
24 #include <support/ErrorStr.h>
25 #include <support/UnitTestRegistration.h>
26
27 using namespace chip;
28
29 #define CHECK(v) (v) ? true : (fprintf(stderr, "%s:%d: error: CHECK(%s) failed\n", __FUNCTION__, __LINE__, #v), false)
30
31 #define CHECK_EQ_STR(a, b)                                                                                                         \
32     !strcmp((a), (b))                                                                                                              \
33         ? true                                                                                                                     \
34         : (fprintf(stderr, "%s:%d: error: CHECK_EQ_STR(\"%s\", \"%s\") failed\n", __FUNCTION__, __LINE__, (a), (b)), false)
35
36 static int falseFormatCalled = 0;
37 static bool falseFormat(char * buf, uint16_t bufSize, int32_t err)
38 {
39     falseFormatCalled += 1;
40     return false; // means keep going
41 }
42 static int falseFormat2Called = 0;
43 static bool falseFormat2(char * buf, uint16_t bufSize, int32_t err)
44 {
45     falseFormat2Called += 1;
46     return false; // means keep going
47 }
48 static int trueFormatCalled = 0;
49 static bool trueFormat(char * buf, uint16_t bufSize, int32_t err)
50 {
51     trueFormatCalled += 1;
52     return true; // means I handled it
53 }
54
55 static bool testRegisterDeregisterErrorFormatter()
56 {
57     static ErrorFormatter falseFormatter  = { falseFormat, nullptr };
58     static ErrorFormatter falseFormatter2 = { falseFormat2, nullptr };
59     static ErrorFormatter trueFormatter   = { trueFormat, nullptr };
60
61     // assume success
62     bool ret = true;
63
64     // simple case
65     RegisterErrorFormatter(&falseFormatter);
66     ErrorStr(1);
67     ret &= CHECK(falseFormatCalled == 1);
68     // reset
69     falseFormatCalled = 0;
70
71     // re-registration should be ignored
72     RegisterErrorFormatter(&falseFormatter);
73     ErrorStr(1);
74     ret &= CHECK(falseFormatCalled == 1);
75     // reset
76     falseFormatCalled = 0;
77
78     // registration of a new handler, nobody handling anything
79     RegisterErrorFormatter(&falseFormatter2);
80     ErrorStr(1);
81     ret &= CHECK(falseFormatCalled == 1);
82     ret &= CHECK(falseFormat2Called == 1);
83     // reset
84     falseFormatCalled  = 0;
85     falseFormat2Called = 0;
86
87     // registration of a true handler, gets first crack
88     RegisterErrorFormatter(&trueFormatter);
89     ErrorStr(1);
90     ret &= CHECK(trueFormatCalled == 1);
91     ret &= CHECK(falseFormatCalled == 0);
92     ret &= CHECK(falseFormat2Called == 0);
93     // reset
94     trueFormatCalled = 0;
95
96     // deregister true
97     DeregisterErrorFormatter(&trueFormatter);
98     ErrorStr(1);
99     ret &= CHECK(trueFormatCalled == 0);
100     ret &= CHECK(falseFormatCalled == 1);
101     ret &= CHECK(falseFormat2Called == 1);
102
103     // verify this doesn't crash
104     DeregisterErrorFormatter(&trueFormatter);
105
106     return ret;
107 }
108
109 static bool testNoError()
110 {
111     return CHECK_EQ_STR(ErrorStr(CHIP_NO_ERROR), "No Error");
112 }
113
114 static bool testFormatErr()
115 {
116     // assume success
117     bool ret = true;
118
119 #if CHIP_CONFIG_SHORT_ERROR_STR
120
121     // TODO tests for this config
122
123 #else // CHIP_CONFIG_SHORT_ERROR_STR
124     static const size_t kBufSize = 1024;
125     static char buf[kBufSize];
126     static const char * subsys = "subsys";
127     static const char * desc   = "desc";
128
129     strcpy(buf, "hi");
130     // shouldn't touch the buffer
131     FormatError(buf, 0, subsys, 0, desc);
132     ret &= CHECK_EQ_STR(buf, "hi");
133
134     // guarantees null termination, doesn't touch past 1st byte
135     strcpy(buf, "hi");
136     FormatError(buf, 1, subsys, 0, desc);
137     ret &= CHECK_EQ_STR(buf, "");
138     ret &= CHECK(buf[1] == 'i');
139
140     // whole shebang
141     FormatError(buf, kBufSize, subsys, 1, desc);
142     ret &= CHECK_EQ_STR(buf, "subsys Error 1 (0x00000001): desc");
143
144     // skip desc
145     FormatError(buf, kBufSize, subsys, 1, nullptr);
146     ret &= CHECK_EQ_STR(buf, "subsys Error 1 (0x00000001)");
147
148     // skip subsys
149     FormatError(buf, kBufSize, nullptr, 1, desc);
150     ret &= CHECK_EQ_STR(buf, "Error 1 (0x00000001): desc");
151
152     // skip both
153     FormatError(buf, kBufSize, nullptr, 1, nullptr);
154     ret &= CHECK_EQ_STR(buf, "Error 1 (0x00000001)");
155
156     // negative
157     FormatError(buf, kBufSize, nullptr, -1, nullptr);
158     ret &= CHECK_EQ_STR(buf, "Error -1 (0xFFFFFFFF)");
159 #endif
160
161     return ret;
162 }
163
164 int TestErrorStr(void)
165 {
166
167     if (!testNoError() || !testRegisterDeregisterErrorFormatter() || !testFormatErr())
168     {
169         return EXIT_FAILURE;
170     }
171
172     printf("All tests succeeded\n");
173
174     return EXIT_SUCCESS;
175 }
176
177 CHIP_REGISTER_TEST_SUITE(TestErrorStr);