Fix tests

Fix Ubuntu starting Mosquitto right after install
Skip TLS on localhost on MacOS with MbedTLS
fix MQTT_LOCALHOST for built-in TCP/IP tests
This commit is contained in:
Sergio R. Caprile committed 2025-07-29 17:17:03 -03:00
1 parent 10d9e1e9ae
commit bd15c456b2
5 files changed
+56 -17

No files matched your search

+10 -4
View File
@@ -124,19 +124,25 @@ jobs:
strategy: strategy:
fail-fast: false fail-fast: false
matrix: matrix:
ssl: ["", BUILTIN, MBEDTLS, OPENSSL, WOLFSSL] # Mongoose does not work with WolfSSL version in MacOS
ssl: ["", BUILTIN, MBEDTLS, OPENSSL] # , WOLFSSL]
select: [-DMG_ENABLE_POLL=0, -DMG_ENABLE_POLL=1] select: [-DMG_ENABLE_POLL=0, -DMG_ENABLE_POLL=1]
include:
- ssl: MBEDTLS
env:
# MbedTLS version in MacOS refuses to validate a certificate without a hostname, causing MQTT tests to fail when using MQTT_LOCALHOST
tflags: -DNO_MQTT_TESTS
exclude: exclude:
- ssl: MBEDTLS - ssl: MBEDTLS
select: -DMG_ENABLE_POLL=0 select: -DMG_ENABLE_POLL=0
- ssl: OPENSSL - ssl: OPENSSL
select: -DMG_ENABLE_POLL=0 select: -DMG_ENABLE_POLL=0
- ssl: WOLFSSL #- ssl: WOLFSSL
select: -DMG_ENABLE_POLL=0 # select: -DMG_ENABLE_POLL=0
name: macos SSL=${{ matrix.ssl }} TFLAGS=${{ matrix.select }} name: macos SSL=${{ matrix.ssl }} TFLAGS=${{ matrix.select }}
env: env:
SSL: ${{ matrix.ssl }} SSL: ${{ matrix.ssl }}
TFLAGS: ${{ matrix.select }} -DMQTT_LOCALHOST -Wno-sign-conversion # Workaround for MbedTLS 3.5.0 TFLAGS: ${{ matrix.select }} -DMQTT_LOCALHOST ${{ matrix.env.tflags }} -Wno-sign-conversion # Workaround for MbedTLS 3.5.0
HOMEBREW_NO_AUTO_UPDATE: 1 HOMEBREW_NO_AUTO_UPDATE: 1
steps: steps:
- uses: actions/checkout@v4 - uses: actions/checkout@v4
+33 -9
View File
@@ -62,7 +62,7 @@ static const char *s_ca_cert =
"-----END CERTIFICATE-----\n"; "-----END CERTIFICATE-----\n";
#elif MG_TLS #elif MG_TLS
#ifdef MQTT_LOCALHOST #ifdef MQTT_LOCALHOST
#define MQTTS_URL "mqtts://127.0.0.1:8883" // we'll generate MQTTS_URL
#define MQTTS_CA mg_str(s_ca_cert) #define MQTTS_CA mg_str(s_ca_cert)
static const char *s_ca_cert = static const char *s_ca_cert =
"-----BEGIN CERTIFICATE-----\n" "-----BEGIN CERTIFICATE-----\n"
@@ -264,14 +264,20 @@ static void test_http_client(struct mg_mgr *mgr) {
static struct mg_connection *s_conn; static struct mg_connection *s_conn;
static char s_topic[16]; static char s_topic[16];
struct mqtt_data {
char *url;
bool passed;
};
static void mqtt_fn(struct mg_connection *c, int ev, void *ev_data) { static void mqtt_fn(struct mg_connection *c, int ev, void *ev_data) {
struct mqtt_data *data = (struct mqtt_data *) c->fn_data;
if (ev == MG_EV_CONNECT) { if (ev == MG_EV_CONNECT) {
MG_DEBUG(("CONNECT")); MG_DEBUG(("CONNECT"));
#if MG_TLS #if MG_TLS
struct mg_tls_opts opts; struct mg_tls_opts opts;
memset(&opts, 0, sizeof(opts)); memset(&opts, 0, sizeof(opts));
opts.ca = MQTTS_CA; opts.ca = MQTTS_CA;
opts.name = mg_url_host(MQTTS_URL); opts.name = mg_url_host(data->url);
mg_tls_init(c, &opts); mg_tls_init(c, &opts);
#endif #endif
} else if (ev == MG_EV_MQTT_OPEN) { } else if (ev == MG_EV_MQTT_OPEN) {
@@ -305,7 +311,7 @@ static void mqtt_fn(struct mg_connection *c, int ev, void *ev_data) {
// close on farewell // close on farewell
MG_INFO(("%lu CLOSING", c->id)); MG_INFO(("%lu CLOSING", c->id));
mg_mqtt_disconnect(c, NULL); mg_mqtt_disconnect(c, NULL);
*(bool *) c->fn_data = true; data->passed = true;
} else if (mm->data.len == 21098) { } else if (mm->data.len == 21098) {
struct mg_mqtt_opts pub_opts; struct mg_mqtt_opts pub_opts;
ASSERT(memcmp((const char *) (size_t) mqtt_fn, mm->data.buf, 21098) == 0); ASSERT(memcmp((const char *) (size_t) mqtt_fn, mm->data.buf, 21098) == 0);
@@ -325,22 +331,40 @@ static void mqtt_fn(struct mg_connection *c, int ev, void *ev_data) {
} }
static void test_mqtt_connsubpub(struct mg_mgr *mgr) { static void test_mqtt_connsubpub(struct mg_mgr *mgr) {
bool passed = false; struct mqtt_data data;
struct mg_mqtt_opts opts; struct mg_mqtt_opts opts;
memset(&opts, 0, sizeof(opts)); memset(&opts, 0, sizeof(opts));
opts.clean = true, opts.version = 4; opts.clean = true, opts.version = 4;
#if MG_TLS data.passed = false;
s_conn = mg_mqtt_connect(mgr, MQTTS_URL, &opts, mqtt_fn, &passed); #if defined(MQTT_LOCALHOST) && MG_TLS != MG_TLS_BUILTIN
#else if (host_ip == NULL) {
s_conn = mg_mqtt_connect(mgr, MQTT_URL, &opts, mqtt_fn, &passed); printf("\nMQTT_LOCALHOST defined but no HOST_IP provided, skipping MQTTS tests\n");
return;
}
printf("HOST_IP: %s\n", host_ip);
#endif #endif
#if MG_TLS
#if defined(MQTT_LOCALHOST) && MG_TLS != MG_TLS_BUILTIN
data.url = mg_mprintf("mqtts://%s:8883", host_ip);
#else
data.url = strdup(MQTTS_URL);
#endif
#else
#ifdef MQTT_LOCALHOST
data.url = mg_mprintf("mqtt://%s:1883", host_ip);
#else
data.url = strdup(MQTT_URL);
#endif
#endif
s_conn = mg_mqtt_connect(mgr, data.url, &opts, mqtt_fn, &data);
ASSERT(s_conn != NULL); ASSERT(s_conn != NULL);
for (int i = 0; i < 1000 && s_conn != NULL && !s_conn->is_closing; i++) { for (int i = 0; i < 1000 && s_conn != NULL && !s_conn->is_closing; i++) {
mg_mgr_poll(mgr, 0); mg_mgr_poll(mgr, 0);
usleep(10000); // 10 ms. Slow down poll loop to ensure packets transit usleep(10000); // 10 ms. Slow down poll loop to ensure packets transit
} }
ASSERT(passed); ASSERT(data.passed);
mg_mgr_poll(mgr, 0); mg_mgr_poll(mgr, 0);
free(data.url);
} }
#include <pthread.h> #include <pthread.h>
+1
View File
@@ -1,3 +1,4 @@
allow_anonymous true
listener 8883 127.0.0.1 listener 8883 127.0.0.1
cafile /etc/mosquitto/certs/ca.crt cafile /etc/mosquitto/certs/ca.crt
certfile /etc/mosquitto/certs/server.crt certfile /etc/mosquitto/certs/server.crt
+6 -2
View File
@@ -1,9 +1,13 @@
#!/bin/sh #!/bin/sh
sudo apt-get -y install mosquitto sudo apt-get -y install mosquitto
# "for some reason" they think starting the server is cool
sudo cp test/mosquitto.conf /etc/mosquitto/conf.d/ sudo cp test/mosquitto.conf /etc/mosquitto/conf.d/
sudo cp test/certs/ca.crt /etc/mosquitto/certs/ sudo cp test/certs/ca.crt /etc/mosquitto/certs/
sudo cp test/certs/server.crt /etc/mosquitto/certs/ sudo cp test/certs/server.crt /etc/mosquitto/certs/
sudo cp test/certs/server.key /etc/mosquitto/certs/ sudo cp test/certs/server.key /etc/mosquitto/certs/
sudo chmod a+w /var/log/mosquitto/* sudo ss -tpln | grep 883
sudo mosquitto -c /etc/mosquitto/mosquitto.conf & # so we need to restart it with the actual config
sudo systemctl restart mosquitto
sudo ss -tpln | grep 883
sudo cat /var/log/mosquitto/*
+6 -2
View File
@@ -450,7 +450,6 @@ struct mqtt_data {
static void mqtt_cb(struct mg_connection *c, int ev, void *ev_data) { static void mqtt_cb(struct mg_connection *c, int ev, void *ev_data) {
struct mqtt_data *test_data = (struct mqtt_data *) c->fn_data; struct mqtt_data *test_data = (struct mqtt_data *) c->fn_data;
char *buf = test_data->msg; char *buf = test_data->msg;
#if MG_TLS #if MG_TLS
if (c->is_tls && ev == MG_EV_CONNECT) { if (c->is_tls && ev == MG_EV_CONNECT) {
struct mg_tls_opts opts; struct mg_tls_opts opts;
@@ -759,9 +758,14 @@ static void test_mqtt_ver(uint8_t mqtt_version) {
static void test_mqtt(void) { static void test_mqtt(void) {
test_mqtt_base(); test_mqtt_base();
#ifdef NO_MQTT_TESTS
MG_ERROR(("MQTT tests skipped on request"));
(void) test_mqtt_basic, (void) test_mqtt_ver;
#else
test_mqtt_basic(); test_mqtt_basic();
test_mqtt_ver(4); test_mqtt_ver(4);
test_mqtt_ver(5); test_mqtt_ver(5);
#endif
} }
static void eh1(struct mg_connection *c, int ev, void *ev_data) { static void eh1(struct mg_connection *c, int ev, void *ev_data) {
@@ -3981,7 +3985,7 @@ int main(void) {
test_http_range(); test_http_range();
#ifndef LOCALHOST_ONLY #ifndef LOCALHOST_ONLY
test_sntp(); test_sntp();
test_mqtt(); test_mqtt(); // sorry, MQTT_LOCALHOST is also skipped
test_http_client(); test_http_client();
#else #else
(void) test_sntp, (void) test_mqtt, (void) test_http_client; (void) test_sntp, (void) test_mqtt, (void) test_http_client;