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 2025-07-28 16:30:35 -03:00
parent 10d9e1e9ae
commit bd15c456b2
5 changed files with 56 additions and 17 deletions

View file

@ -124,19 +124,25 @@ jobs:
strategy:
fail-fast: false
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]
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:
- ssl: MBEDTLS
select: -DMG_ENABLE_POLL=0
- ssl: OPENSSL
select: -DMG_ENABLE_POLL=0
- ssl: WOLFSSL
select: -DMG_ENABLE_POLL=0
#- ssl: WOLFSSL
# select: -DMG_ENABLE_POLL=0
name: macos SSL=${{ matrix.ssl }} TFLAGS=${{ matrix.select }}
env:
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
steps:
- uses: actions/checkout@v4

View file

@ -62,7 +62,7 @@ static const char *s_ca_cert =
"-----END CERTIFICATE-----\n";
#elif MG_TLS
#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)
static const char *s_ca_cert =
"-----BEGIN CERTIFICATE-----\n"
@ -264,14 +264,20 @@ static void test_http_client(struct mg_mgr *mgr) {
static struct mg_connection *s_conn;
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) {
struct mqtt_data *data = (struct mqtt_data *) c->fn_data;
if (ev == MG_EV_CONNECT) {
MG_DEBUG(("CONNECT"));
#if MG_TLS
struct mg_tls_opts opts;
memset(&opts, 0, sizeof(opts));
opts.ca = MQTTS_CA;
opts.name = mg_url_host(MQTTS_URL);
opts.name = mg_url_host(data->url);
mg_tls_init(c, &opts);
#endif
} 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
MG_INFO(("%lu CLOSING", c->id));
mg_mqtt_disconnect(c, NULL);
*(bool *) c->fn_data = true;
data->passed = true;
} else if (mm->data.len == 21098) {
struct mg_mqtt_opts pub_opts;
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) {
bool passed = false;
struct mqtt_data data;
struct mg_mqtt_opts opts;
memset(&opts, 0, sizeof(opts));
opts.clean = true, opts.version = 4;
#if MG_TLS
s_conn = mg_mqtt_connect(mgr, MQTTS_URL, &opts, mqtt_fn, &passed);
#else
s_conn = mg_mqtt_connect(mgr, MQTT_URL, &opts, mqtt_fn, &passed);
data.passed = false;
#if defined(MQTT_LOCALHOST) && MG_TLS != MG_TLS_BUILTIN
if (host_ip == NULL) {
printf("\nMQTT_LOCALHOST defined but no HOST_IP provided, skipping MQTTS tests\n");
return;
}
printf("HOST_IP: %s\n", host_ip);
#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);
for (int i = 0; i < 1000 && s_conn != NULL && !s_conn->is_closing; i++) {
mg_mgr_poll(mgr, 0);
usleep(10000); // 10 ms. Slow down poll loop to ensure packets transit
}
ASSERT(passed);
ASSERT(data.passed);
mg_mgr_poll(mgr, 0);
free(data.url);
}
#include <pthread.h>

View file

@ -1,3 +1,4 @@
allow_anonymous true
listener 8883 127.0.0.1
cafile /etc/mosquitto/certs/ca.crt
certfile /etc/mosquitto/certs/server.crt

View file

@ -1,9 +1,13 @@
#!/bin/sh
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/certs/ca.crt /etc/mosquitto/certs/
sudo cp test/certs/server.crt /etc/mosquitto/certs/
sudo cp test/certs/server.key /etc/mosquitto/certs/
sudo chmod a+w /var/log/mosquitto/*
sudo mosquitto -c /etc/mosquitto/mosquitto.conf &
sudo ss -tpln | grep 883
# so we need to restart it with the actual config
sudo systemctl restart mosquitto
sudo ss -tpln | grep 883
sudo cat /var/log/mosquitto/*

View file

@ -450,7 +450,6 @@ struct mqtt_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;
char *buf = test_data->msg;
#if MG_TLS
if (c->is_tls && ev == MG_EV_CONNECT) {
struct mg_tls_opts opts;
@ -759,9 +758,14 @@ static void test_mqtt_ver(uint8_t mqtt_version) {
static void test_mqtt(void) {
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_ver(4);
test_mqtt_ver(5);
#endif
}
static void eh1(struct mg_connection *c, int ev, void *ev_data) {
@ -3981,7 +3985,7 @@ int main(void) {
test_http_range();
#ifndef LOCALHOST_ONLY
test_sntp();
test_mqtt();
test_mqtt(); // sorry, MQTT_LOCALHOST is also skipped
test_http_client();
#else
(void) test_sntp, (void) test_mqtt, (void) test_http_client;