3 * Copyright (c) 2020 Project CHIP Authors
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include <core/CHIPCore.h>
24 #include <support/ErrorStr.h>
25 #include <support/UnitTestRegistration.h>
29 #define CHECK(v) (v) ? true : (fprintf(stderr, "%s:%d: error: CHECK(%s) failed\n", __FUNCTION__, __LINE__, #v), false)
31 #define CHECK_EQ_STR(a, b) \
34 : (fprintf(stderr, "%s:%d: error: CHECK_EQ_STR(\"%s\", \"%s\") failed\n", __FUNCTION__, __LINE__, (a), (b)), false)
36 static int falseFormatCalled = 0;
37 static bool falseFormat(char * buf, uint16_t bufSize, int32_t err)
39 falseFormatCalled += 1;
40 return false; // means keep going
42 static int falseFormat2Called = 0;
43 static bool falseFormat2(char * buf, uint16_t bufSize, int32_t err)
45 falseFormat2Called += 1;
46 return false; // means keep going
48 static int trueFormatCalled = 0;
49 static bool trueFormat(char * buf, uint16_t bufSize, int32_t err)
51 trueFormatCalled += 1;
52 return true; // means I handled it
55 static bool testRegisterDeregisterErrorFormatter()
57 static ErrorFormatter falseFormatter = { falseFormat, nullptr };
58 static ErrorFormatter falseFormatter2 = { falseFormat2, nullptr };
59 static ErrorFormatter trueFormatter = { trueFormat, nullptr };
65 RegisterErrorFormatter(&falseFormatter);
67 ret &= CHECK(falseFormatCalled == 1);
69 falseFormatCalled = 0;
71 // re-registration should be ignored
72 RegisterErrorFormatter(&falseFormatter);
74 ret &= CHECK(falseFormatCalled == 1);
76 falseFormatCalled = 0;
78 // registration of a new handler, nobody handling anything
79 RegisterErrorFormatter(&falseFormatter2);
81 ret &= CHECK(falseFormatCalled == 1);
82 ret &= CHECK(falseFormat2Called == 1);
84 falseFormatCalled = 0;
85 falseFormat2Called = 0;
87 // registration of a true handler, gets first crack
88 RegisterErrorFormatter(&trueFormatter);
90 ret &= CHECK(trueFormatCalled == 1);
91 ret &= CHECK(falseFormatCalled == 0);
92 ret &= CHECK(falseFormat2Called == 0);
97 DeregisterErrorFormatter(&trueFormatter);
99 ret &= CHECK(trueFormatCalled == 0);
100 ret &= CHECK(falseFormatCalled == 1);
101 ret &= CHECK(falseFormat2Called == 1);
103 // verify this doesn't crash
104 DeregisterErrorFormatter(&trueFormatter);
109 static bool testNoError()
111 return CHECK_EQ_STR(ErrorStr(CHIP_NO_ERROR), "No Error");
114 static bool testFormatErr()
119 #if CHIP_CONFIG_SHORT_ERROR_STR
121 // TODO tests for this config
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";
130 // shouldn't touch the buffer
131 FormatError(buf, 0, subsys, 0, desc);
132 ret &= CHECK_EQ_STR(buf, "hi");
134 // guarantees null termination, doesn't touch past 1st byte
136 FormatError(buf, 1, subsys, 0, desc);
137 ret &= CHECK_EQ_STR(buf, "");
138 ret &= CHECK(buf[1] == 'i');
141 FormatError(buf, kBufSize, subsys, 1, desc);
142 ret &= CHECK_EQ_STR(buf, "subsys Error 1 (0x00000001): desc");
145 FormatError(buf, kBufSize, subsys, 1, nullptr);
146 ret &= CHECK_EQ_STR(buf, "subsys Error 1 (0x00000001)");
149 FormatError(buf, kBufSize, nullptr, 1, desc);
150 ret &= CHECK_EQ_STR(buf, "Error 1 (0x00000001): desc");
153 FormatError(buf, kBufSize, nullptr, 1, nullptr);
154 ret &= CHECK_EQ_STR(buf, "Error 1 (0x00000001)");
157 FormatError(buf, kBufSize, nullptr, -1, nullptr);
158 ret &= CHECK_EQ_STR(buf, "Error -1 (0xFFFFFFFF)");
164 int TestErrorStr(void)
167 if (!testNoError() || !testRegisterDeregisterErrorFormatter() || !testFormatErr())
172 printf("All tests succeeded\n");
177 CHIP_REGISTER_TEST_SUITE(TestErrorStr);