Skip to content

Commit 5873c5e

Browse files
authored
Merge pull request #482 from embhorn/test_update
Update testing with a more flexible framework
2 parents 94ea184 + 291a66d commit 5873c5e

9 files changed

Lines changed: 1895 additions & 587 deletions

File tree

CMakeLists.txt

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

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)
353+
enable_testing()
354+
add_executable(wolfmqtt_unit_tests
355+
tests/unit_test.c
356+
tests/test_framework.c
357+
tests/test_mqtt_packet.c
358+
tests/test_mqtt_client.c
359+
)
360+
target_include_directories(wolfmqtt_unit_tests
361+
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
362+
target_link_libraries(wolfmqtt_unit_tests wolfmqtt)
363+
add_test(NAME wolfmqtt_unit_tests COMMAND wolfmqtt_unit_tests)
364+
endif()
365+
347366
####################################################
348367
# Installation
349368
####################################################

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ cmake .. -DWITH_WOLFSSL_TREE=/path/to/wolfssl/
5454
cmake --build .
5555
```
5656

57+
Additional CMake options:
58+
* `-DWOLFMQTT_UNIT_TESTS=no` disables the `wolfmqtt_unit_tests` target (default `yes`). The target is only added when wolfMQTT is the top-level project, so downstream consumers using `add_subdirectory()` are unaffected.
59+
5760
### vcpkg
5861

5962
You can download and install wolfMQTT using the [vcpkg](https://github.com/Microsoft/vcpkg):

src/mqtt_client.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2581,6 +2581,9 @@ int MqttClient_Ping_ex(MqttClient *client, MqttPing* ping)
25812581

25822582
int MqttClient_Ping(MqttClient *client)
25832583
{
2584+
if (client == NULL) {
2585+
return MQTT_TRACE_ERROR(MQTT_CODE_ERROR_BAD_ARG);
2586+
}
25842587
return MqttClient_Ping_ex(client, &client->msg.ping);
25852588
}
25862589

tests/include.am

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,18 @@
22
# included from Top Level Makefile.am
33
# All paths should be given relative to the root
44

5-
# Unit tests for packet encode/decode
6-
check_PROGRAMS += tests/unit_test
7-
tests_unit_test_SOURCES = tests/unit_test.c
8-
tests_unit_test_LDADD = src/libwolfmqtt.la
9-
tests_unit_test_DEPENDENCIES = src/libwolfmqtt.la
10-
tests_unit_test_CPPFLAGS = $(AM_CPPFLAGS)
5+
# Unit tests
6+
check_PROGRAMS += tests/unit_tests
7+
tests_unit_tests_SOURCES = tests/unit_test.c \
8+
tests/test_framework.c \
9+
tests/test_mqtt_packet.c \
10+
tests/test_mqtt_client.c
11+
tests_unit_tests_CPPFLAGS = -I$(top_srcdir) $(AM_CPPFLAGS)
12+
tests_unit_tests_LDADD = src/libwolfmqtt.la
13+
tests_unit_tests_DEPENDENCIES = src/libwolfmqtt.la
14+
15+
# Test framework header
16+
noinst_HEADERS += tests/unit_test.h
1117

1218
if BUILD_FUZZ
1319
if BUILD_BROKER

tests/test_framework.c

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/* test_framework.c
2+
*
3+
* Copyright (C) 2006-2026 wolfSSL Inc.
4+
*
5+
* This file is part of wolfMQTT.
6+
*
7+
* wolfMQTT is free software; you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation; either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* wolfMQTT is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program; if not, write to the Free Software
19+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
20+
*/
21+
22+
/* Pass-path smoke tests for every assertion macro defined by unit_test.h.
23+
* The fail paths call return after setting ut_current_test_failed, so they
24+
* cannot be exercised without the framework reporting the test as failed.
25+
* These tests ensure each macro at least compiles and evaluates its
26+
* non-failing branch correctly. */
27+
28+
#ifdef HAVE_CONFIG_H
29+
#include <config.h>
30+
#endif
31+
32+
#include "tests/unit_test.h"
33+
34+
void run_framework_tests(void);
35+
36+
TEST(assert_true_pass)
37+
{
38+
ASSERT_TRUE(1);
39+
ASSERT_TRUE(1 == 1);
40+
}
41+
42+
TEST(assert_false_pass)
43+
{
44+
ASSERT_FALSE(0);
45+
ASSERT_FALSE(1 == 2);
46+
}
47+
48+
TEST(assert_eq_pass)
49+
{
50+
ASSERT_EQ(0, 0);
51+
ASSERT_EQ(42, 42);
52+
}
53+
54+
TEST(assert_ne_pass)
55+
{
56+
ASSERT_NE(0, 1);
57+
ASSERT_NE(42, -1);
58+
}
59+
60+
TEST(assert_null_pass)
61+
{
62+
void* p = NULL;
63+
ASSERT_NULL(p);
64+
}
65+
66+
TEST(assert_not_null_pass)
67+
{
68+
int x;
69+
ASSERT_NOT_NULL(&x);
70+
}
71+
72+
TEST(assert_str_eq_pass)
73+
{
74+
ASSERT_STR_EQ("hello", "hello");
75+
}
76+
77+
TEST(assert_mem_eq_pass)
78+
{
79+
const unsigned char a[] = {0x01, 0x02, 0x03, 0x04};
80+
const unsigned char b[] = {0x01, 0x02, 0x03, 0x04};
81+
ASSERT_MEM_EQ(a, b, sizeof(a));
82+
}
83+
84+
/* FAIL has no pass path; guard with a never-true condition so the macro
85+
* body is compiled but never executed. */
86+
TEST(fail_compiles)
87+
{
88+
if (0) {
89+
FAIL("unreachable");
90+
}
91+
ASSERT_TRUE(1);
92+
}
93+
94+
void run_framework_tests(void)
95+
{
96+
TEST_SUITE_BEGIN("framework", NULL, NULL);
97+
98+
RUN_TEST(assert_true_pass);
99+
RUN_TEST(assert_false_pass);
100+
RUN_TEST(assert_eq_pass);
101+
RUN_TEST(assert_ne_pass);
102+
RUN_TEST(assert_null_pass);
103+
RUN_TEST(assert_not_null_pass);
104+
RUN_TEST(assert_str_eq_pass);
105+
RUN_TEST(assert_mem_eq_pass);
106+
RUN_TEST(fail_compiles);
107+
108+
TEST_SUITE_END();
109+
}

0 commit comments

Comments
 (0)