Skip to content

Commit 8c77dc2

Browse files
committed
Fixes from review
1 parent d3c4470 commit 8c77dc2

3 files changed

Lines changed: 63 additions & 40 deletions

File tree

CMakeLists.txt

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -344,17 +344,22 @@ if (WOLFMQTT_BROKER)
344344
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>)
345345
endif()
346346

347-
option(WOLFMQTT_UNIT_TESTS "Build unit tests" ON)
348-
if(WOLFMQTT_UNIT_TESTS)
347+
add_option("WOLFMQTT_UNIT_TESTS"
348+
"Build unit tests (only when wolfMQTT is the top-level project)"
349+
"yes" "yes;no")
350+
# Only build tests when wolfMQTT is the top-level project, to avoid adding
351+
# a test target to parent projects that consume wolfMQTT via add_subdirectory.
352+
if(WOLFMQTT_UNIT_TESTS AND CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR)
349353
enable_testing()
350-
add_executable(unit_tests
354+
add_executable(wolfmqtt_unit_tests
351355
tests/unit_test.c
352356
tests/test_mqtt_packet.c
353357
tests/test_mqtt_client.c
354358
)
355-
target_include_directories(unit_tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
356-
target_link_libraries(unit_tests wolfmqtt)
357-
add_test(NAME unit_tests COMMAND unit_tests)
359+
target_include_directories(wolfmqtt_unit_tests
360+
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
361+
target_link_libraries(wolfmqtt_unit_tests wolfmqtt)
362+
add_test(NAME wolfmqtt_unit_tests COMMAND wolfmqtt_unit_tests)
358363
endif()
359364

360365
####################################################

tests/test_mqtt_client.c

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,12 +192,29 @@ TEST(init_negative_tx_buf_len)
192192
ASSERT_EQ(MQTT_CODE_ERROR_BAD_ARG, rc);
193193
}
194194

195+
TEST(init_negative_rx_buf_len)
196+
{
197+
int rc;
198+
199+
rc = MqttClient_Init(&test_client, &test_net, NULL,
200+
test_tx_buf, TEST_TX_BUF_SIZE,
201+
test_rx_buf, -1,
202+
TEST_CMD_TIMEOUT_MS);
203+
ASSERT_EQ(MQTT_CODE_ERROR_BAD_ARG, rc);
204+
}
205+
195206
/* ============================================================================
196207
* MqttClient_DeInit Tests
197208
* ============================================================================ */
198209

199210
TEST(deinit_null_client)
200211
{
212+
/* MqttClient_DeInit(NULL) still calls MqttProps_ShutDown() under
213+
* WOLFMQTT_V5, which decrements a refcount. Pair it with MqttProps_Init()
214+
* so the refcount stays balanced across test runs. */
215+
#ifdef WOLFMQTT_V5
216+
(void)MqttProps_Init();
217+
#endif
201218
/* Should not crash with NULL client */
202219
MqttClient_DeInit(NULL);
203220
/* If we reach here, test passes */
@@ -267,7 +284,7 @@ TEST(connect_with_mock_network)
267284
/* Connect will fail at the network write stage since mock returns error */
268285
rc = MqttClient_Connect(&test_client, &connect);
269286
/* Should fail with network error since mock network write returns error */
270-
ASSERT_TRUE(rc < 0);
287+
ASSERT_EQ(MQTT_CODE_ERROR_NETWORK, rc);
271288
}
272289

273290
/* ============================================================================
@@ -499,6 +516,7 @@ void run_mqtt_client_tests(void)
499516
RUN_TEST(init_zero_rx_buf_len);
500517
RUN_TEST(init_success);
501518
RUN_TEST(init_negative_tx_buf_len);
519+
RUN_TEST(init_negative_rx_buf_len);
502520

503521
/* MqttClient_DeInit tests */
504522
RUN_TEST(deinit_null_client);

tests/unit_test.h

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
#ifndef WOLFMQTT_UNIT_TEST_H
2323
#define WOLFMQTT_UNIT_TEST_H
2424

25-
#include <stdio.h>
25+
#include "wolfmqtt/mqtt_types.h"
2626
#include <stdlib.h>
2727
#include <string.h>
2828

@@ -98,8 +98,8 @@ static int ut_current_test_failed UT_MAYBE_UNUSED;
9898
#define ASSERT_TRUE(cond) \
9999
do { \
100100
if (!(cond)) { \
101-
printf(" " UT_COLOR_RED "FAIL" UT_COLOR_RESET ": %s:%d: " \
102-
"ASSERT_TRUE(" #cond ") failed\n", __FILE__, __LINE__); \
101+
PRINTF(" " UT_COLOR_RED "FAIL" UT_COLOR_RESET ": %s:%d: " \
102+
"ASSERT_TRUE(" #cond ") failed", __FILE__, __LINE__); \
103103
ut_current_test_failed = 1; \
104104
return; \
105105
} \
@@ -108,8 +108,8 @@ static int ut_current_test_failed UT_MAYBE_UNUSED;
108108
#define ASSERT_FALSE(cond) \
109109
do { \
110110
if (cond) { \
111-
printf(" " UT_COLOR_RED "FAIL" UT_COLOR_RESET ": %s:%d: " \
112-
"ASSERT_FALSE(" #cond ") failed\n", __FILE__, __LINE__); \
111+
PRINTF(" " UT_COLOR_RED "FAIL" UT_COLOR_RESET ": %s:%d: " \
112+
"ASSERT_FALSE(" #cond ") failed", __FILE__, __LINE__); \
113113
ut_current_test_failed = 1; \
114114
return; \
115115
} \
@@ -120,9 +120,9 @@ static int ut_current_test_failed UT_MAYBE_UNUSED;
120120
long long _exp = (long long)(expected); \
121121
long long _act = (long long)(actual); \
122122
if (_exp != _act) { \
123-
printf(" " UT_COLOR_RED "FAIL" UT_COLOR_RESET ": %s:%d: " \
123+
PRINTF(" " UT_COLOR_RED "FAIL" UT_COLOR_RESET ": %s:%d: " \
124124
"ASSERT_EQ(" #expected ", " #actual ") failed: " \
125-
"expected %lld, got %lld\n", __FILE__, __LINE__, _exp, _act); \
125+
"expected %lld, got %lld", __FILE__, __LINE__, _exp, _act); \
126126
ut_current_test_failed = 1; \
127127
return; \
128128
} \
@@ -133,8 +133,8 @@ static int ut_current_test_failed UT_MAYBE_UNUSED;
133133
long long _a = (long long)(a); \
134134
long long _b = (long long)(b); \
135135
if (_a == _b) { \
136-
printf(" " UT_COLOR_RED "FAIL" UT_COLOR_RESET ": %s:%d: " \
137-
"ASSERT_NE(" #a ", " #b ") failed: both are %lld\n", \
136+
PRINTF(" " UT_COLOR_RED "FAIL" UT_COLOR_RESET ": %s:%d: " \
137+
"ASSERT_NE(" #a ", " #b ") failed: both are %lld", \
138138
__FILE__, __LINE__, _a); \
139139
ut_current_test_failed = 1; \
140140
return; \
@@ -144,8 +144,8 @@ static int ut_current_test_failed UT_MAYBE_UNUSED;
144144
#define ASSERT_NULL(ptr) \
145145
do { \
146146
if ((ptr) != NULL) { \
147-
printf(" " UT_COLOR_RED "FAIL" UT_COLOR_RESET ": %s:%d: " \
148-
"ASSERT_NULL(" #ptr ") failed: pointer is not NULL\n", \
147+
PRINTF(" " UT_COLOR_RED "FAIL" UT_COLOR_RESET ": %s:%d: " \
148+
"ASSERT_NULL(" #ptr ") failed: pointer is not NULL", \
149149
__FILE__, __LINE__); \
150150
ut_current_test_failed = 1; \
151151
return; \
@@ -155,8 +155,8 @@ static int ut_current_test_failed UT_MAYBE_UNUSED;
155155
#define ASSERT_NOT_NULL(ptr) \
156156
do { \
157157
if ((ptr) == NULL) { \
158-
printf(" " UT_COLOR_RED "FAIL" UT_COLOR_RESET ": %s:%d: " \
159-
"ASSERT_NOT_NULL(" #ptr ") failed: pointer is NULL\n", \
158+
PRINTF(" " UT_COLOR_RED "FAIL" UT_COLOR_RESET ": %s:%d: " \
159+
"ASSERT_NOT_NULL(" #ptr ") failed: pointer is NULL", \
160160
__FILE__, __LINE__); \
161161
ut_current_test_failed = 1; \
162162
return; \
@@ -168,9 +168,9 @@ static int ut_current_test_failed UT_MAYBE_UNUSED;
168168
const char* _exp = (expected); \
169169
const char* _act = (actual); \
170170
if (_exp == NULL || _act == NULL || strcmp(_exp, _act) != 0) { \
171-
printf(" " UT_COLOR_RED "FAIL" UT_COLOR_RESET ": %s:%d: " \
171+
PRINTF(" " UT_COLOR_RED "FAIL" UT_COLOR_RESET ": %s:%d: " \
172172
"ASSERT_STR_EQ(" #expected ", " #actual ") failed: " \
173-
"expected \"%s\", got \"%s\"\n", __FILE__, __LINE__, \
173+
"expected \"%s\", got \"%s\"", __FILE__, __LINE__, \
174174
_exp ? _exp : "(null)", _act ? _act : "(null)"); \
175175
ut_current_test_failed = 1; \
176176
return; \
@@ -183,8 +183,8 @@ static int ut_current_test_failed UT_MAYBE_UNUSED;
183183
const void* _act = (actual); \
184184
size_t _size = (size); \
185185
if (_exp == NULL || _act == NULL || memcmp(_exp, _act, _size) != 0) { \
186-
printf(" " UT_COLOR_RED "FAIL" UT_COLOR_RESET ": %s:%d: " \
187-
"ASSERT_MEM_EQ(" #expected ", " #actual ", %zu) failed\n", \
186+
PRINTF(" " UT_COLOR_RED "FAIL" UT_COLOR_RESET ": %s:%d: " \
187+
"ASSERT_MEM_EQ(" #expected ", " #actual ", %zu) failed", \
188188
__FILE__, __LINE__, _size); \
189189
ut_current_test_failed = 1; \
190190
return; \
@@ -193,7 +193,7 @@ static int ut_current_test_failed UT_MAYBE_UNUSED;
193193

194194
#define FAIL(msg) \
195195
do { \
196-
printf(" " UT_COLOR_RED "FAIL" UT_COLOR_RESET ": %s:%d: %s\n", \
196+
PRINTF(" " UT_COLOR_RED "FAIL" UT_COLOR_RESET ": %s:%d: %s", \
197197
__FILE__, __LINE__, (msg)); \
198198
ut_current_test_failed = 1; \
199199
return; \
@@ -215,10 +215,10 @@ static int ut_current_test_failed UT_MAYBE_UNUSED;
215215
if (ut_suite_teardown) ut_suite_teardown(); \
216216
if (ut_current_test_failed) { \
217217
ut_suite_fail_count++; \
218-
printf(" " UT_COLOR_RED "[FAIL]" UT_COLOR_RESET " %s\n", #name); \
218+
PRINTF(" " UT_COLOR_RED "[FAIL]" UT_COLOR_RESET " %s", #name); \
219219
} else { \
220220
ut_suite_pass_count++; \
221-
printf(" " UT_COLOR_GREEN "[PASS]" UT_COLOR_RESET " %s\n", #name); \
221+
PRINTF(" " UT_COLOR_GREEN "[PASS]" UT_COLOR_RESET " %s", #name); \
222222
} \
223223
} while (0)
224224

@@ -230,19 +230,19 @@ static int ut_current_test_failed UT_MAYBE_UNUSED;
230230
ut_suite_name = (name); \
231231
ut_suite_setup = (setup_fn); \
232232
ut_suite_teardown = (teardown_fn); \
233-
printf("\n" UT_COLOR_CYAN "=== Test Suite: %s ===" UT_COLOR_RESET "\n", \
233+
PRINTF("\n" UT_COLOR_CYAN "=== Test Suite: %s ===" UT_COLOR_RESET, \
234234
ut_suite_name); \
235235
} while (0)
236236

237237
/* End a test suite and report results */
238238
#define TEST_SUITE_END() \
239239
do { \
240240
int total = ut_suite_pass_count + ut_suite_fail_count; \
241-
printf(UT_COLOR_CYAN "--- Suite Results: %s ---" UT_COLOR_RESET "\n", \
241+
PRINTF(UT_COLOR_CYAN "--- Suite Results: %s ---" UT_COLOR_RESET, \
242242
ut_suite_name); \
243-
printf(" Passed: " UT_COLOR_GREEN "%d" UT_COLOR_RESET \
243+
PRINTF(" Passed: " UT_COLOR_GREEN "%d" UT_COLOR_RESET \
244244
", Failed: " UT_COLOR_RED "%d" UT_COLOR_RESET \
245-
", Total: %d\n\n", \
245+
", Total: %d\n", \
246246
ut_suite_pass_count, ut_suite_fail_count, total); \
247247
ut_global_pass_count += ut_suite_pass_count; \
248248
ut_global_fail_count += ut_suite_fail_count; \
@@ -257,32 +257,32 @@ static int ut_current_test_failed UT_MAYBE_UNUSED;
257257
do { \
258258
ut_global_pass_count = 0; \
259259
ut_global_fail_count = 0; \
260-
printf(UT_COLOR_CYAN \
260+
PRINTF(UT_COLOR_CYAN \
261261
"============================================\n" \
262262
" wolfMQTT Unit Test Runner\n" \
263263
"============================================" \
264-
UT_COLOR_RESET "\n"); \
264+
UT_COLOR_RESET); \
265265
} while (0)
266266

267267
/* Report global results and return exit code */
268268
#define TEST_RUNNER_END() \
269269
do { \
270270
int total = ut_global_pass_count + ut_global_fail_count; \
271-
printf(UT_COLOR_CYAN \
271+
PRINTF(UT_COLOR_CYAN \
272272
"============================================\n" \
273273
" Final Results\n" \
274274
"============================================" \
275-
UT_COLOR_RESET "\n"); \
276-
printf(" Total Passed: " UT_COLOR_GREEN "%d" UT_COLOR_RESET "\n", \
275+
UT_COLOR_RESET); \
276+
PRINTF(" Total Passed: " UT_COLOR_GREEN "%d" UT_COLOR_RESET, \
277277
ut_global_pass_count); \
278-
printf(" Total Failed: " UT_COLOR_RED "%d" UT_COLOR_RESET "\n", \
278+
PRINTF(" Total Failed: " UT_COLOR_RED "%d" UT_COLOR_RESET, \
279279
ut_global_fail_count); \
280-
printf(" Total Tests: %d\n", total); \
280+
PRINTF(" Total Tests: %d", total); \
281281
if (ut_global_fail_count > 0) { \
282-
printf("\n" UT_COLOR_RED "TESTS FAILED" UT_COLOR_RESET "\n"); \
282+
PRINTF("\n" UT_COLOR_RED "TESTS FAILED" UT_COLOR_RESET); \
283283
return 1; \
284284
} else { \
285-
printf("\n" UT_COLOR_GREEN "ALL TESTS PASSED" UT_COLOR_RESET "\n"); \
285+
PRINTF("\n" UT_COLOR_GREEN "ALL TESTS PASSED" UT_COLOR_RESET); \
286286
return 0; \
287287
} \
288288
} while (0)

0 commit comments

Comments
 (0)