Skip to content

Commit

Permalink
Merge pull request #2996 from cesanta/test
Browse files Browse the repository at this point in the history
improve tests and debuggability
  • Loading branch information
scaprile authored Dec 30, 2024
2 parents 317a152 + 6d0320b commit 5479918
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 59 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: "CodeQL Scanning"

on:
schedule:
- cron: '30 2 * * *' # run at 2:30 AM UTC
- cron: '30 23 * * *' # run at 11:30 PM UTC
# Allow manual runs
workflow_dispatch:

Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/nightly.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: Full build and test
on:
schedule:
- cron: '0 1 * * *' # run at 1 AM UTC
- cron: '0 22 * * *' # run at 10 PM UTC
# Allow manual runs
workflow_dispatch:
env:
Expand Down Expand Up @@ -117,7 +117,7 @@ jobs:
name: macos SSL=${{ matrix.ssl }} TFLAGS=${{ matrix.select }}
env:
SSL: ${{ matrix.ssl }}
TFLAGS: ${{ matrix.select }} -DNO_SNTP_CHECK -Wno-sign-conversion # Workaround for MbedTLS 3.5.0
TFLAGS: ${{ matrix.select }} -Wno-sign-conversion # Workaround for MbedTLS 3.5.0
HOMEBREW_NO_AUTO_UPDATE: 1
steps:
- uses: actions/checkout@v4
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/quicktest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ jobs:
name: macos SSL=${{ matrix.ssl }}
env:
SSL: ${{ matrix.ssl }}
TFLAGS: -DNO_SNTP_CHECK
#TFLAGS: -DNO_SNTP_CHECK
HOMEBREW_NO_AUTO_UPDATE: 1
steps:
- uses: actions/checkout@v4
Expand Down
19 changes: 11 additions & 8 deletions mongoose.c
Original file line number Diff line number Diff line change
Expand Up @@ -3523,7 +3523,7 @@ void mg_mqtt_login(struct mg_connection *c, const struct mg_mqtt_opts *opts) {
total_len += 2 + (uint32_t) opts->pass.len;
hdr[7] |= MQTT_HAS_PASSWORD;
}
if (opts->topic.len > 0) { // allow zero-length msgs, message.len is size_t
if (opts->topic.len > 0) { // allow zero-length msgs, message.len is size_t
total_len += 4 + (uint32_t) opts->topic.len + (uint32_t) opts->message.len;
hdr[7] |= MQTT_HAS_WILL;
}
Expand Down Expand Up @@ -3569,18 +3569,19 @@ uint16_t mg_mqtt_pub(struct mg_connection *c, const struct mg_mqtt_opts *opts) {
uint16_t id = opts->retransmit_id;
uint8_t flags = (uint8_t) (((opts->qos & 3) << 1) | (opts->retain ? 1 : 0));
size_t len = 2 + opts->topic.len + opts->message.len;
MG_DEBUG(("%lu [%.*s] -> [%.*s]", c->id, (int) opts->topic.len,
(char *) opts->topic.buf, (int) opts->message.len,
(char *) opts->message.buf));
MG_DEBUG(("%lu [%.*s] <- [%.*s%c", c->id, (int) opts->topic.len,
(char *) opts->topic.buf,
(int) (opts->message.len <= 10 ? opts->message.len : 10),
(char *) opts->message.buf, opts->message.len <= 10 ? ']' : ' '));
if (opts->qos > 0) len += 2;
if (c->is_mqtt5) len += get_props_size(opts->props, opts->num_props);

if (opts->qos > 0 && id != 0) flags |= 1 << 3;
mg_mqtt_send_header(c, MQTT_CMD_PUBLISH, flags, (uint32_t) len);
mg_send_u16(c, mg_htons((uint16_t) opts->topic.len));
mg_send(c, opts->topic.buf, opts->topic.len);
if (opts->qos > 0) { // need to send 'id' field
if (id == 0) { // generate new one if not resending
if (opts->qos > 0) { // need to send 'id' field
if (id == 0) { // generate new one if not resending
if (++c->mgr->mqtt_id == 0) ++c->mgr->mqtt_id;
id = c->mgr->mqtt_id;
}
Expand Down Expand Up @@ -3703,8 +3704,10 @@ static void mqtt_cb(struct mg_connection *c, int ev, void *ev_data) {
}
break;
case MQTT_CMD_PUBLISH: {
/*MG_DEBUG(("%lu [%.*s] -> [%.*s]", c->id, (int) mm.topic.len,
mm.topic.buf, (int) mm.data.len, mm.data.buf));*/
MG_DEBUG(("%lu [%.*s] -> [%.*s%c", c->id, (int) mm.topic.len,
mm.topic.buf,
(int) (mm.data.len <= 10 ? mm.data.len : 10), mm.data.buf,
mm.data.len <= 10 ? ']' : ' '));
if (mm.qos > 0) {
uint16_t id = mg_ntohs(mm.id);
uint32_t remaining_len = sizeof(id);
Expand Down
59 changes: 32 additions & 27 deletions mongoose.h
Original file line number Diff line number Diff line change
Expand Up @@ -2712,6 +2712,7 @@ bool mg_ota_flash_end(struct mg_flash *flash);




struct mg_tcpip_if; // Mongoose TCP/IP network interface

struct mg_tcpip_driver {
Expand Down Expand Up @@ -2778,7 +2779,6 @@ void mg_tcpip_arp_request(struct mg_tcpip_if *ifp, uint32_t ip, uint8_t *mac);

extern struct mg_tcpip_driver mg_tcpip_driver_stm32f;
extern struct mg_tcpip_driver mg_tcpip_driver_w5500;
extern struct mg_tcpip_driver mg_tcpip_driver_w5100;
extern struct mg_tcpip_driver mg_tcpip_driver_tm4c;
extern struct mg_tcpip_driver mg_tcpip_driver_tms570;
extern struct mg_tcpip_driver mg_tcpip_driver_stm32h;
Expand Down Expand Up @@ -3195,22 +3195,14 @@ struct mg_tcpip_driver_tms570_data {



#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_XMC) && MG_ENABLE_DRIVER_XMC
#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_W5500) && MG_ENABLE_DRIVER_W5500

struct mg_tcpip_driver_xmc_data {
// 13.2.8.1 Station Management Functions
// MDC clock divider (). MDC clock is derived from ETH MAC clock
// It must not exceed 2.5MHz
// ETH Clock range DIVIDER mdc_cr VALUE
// --------------------------------------------
// -1 <-- tell driver to guess the value
// 60-100 MHz ETH Clock/42 0
// 100-150 MHz ETH Clock/62 1
// 20-35 MHz ETH Clock/16 2
// 35-60 MHz ETH Clock/26 3
// 150-250 MHz ETH Clock/102 4
// 250-300 MHz ETH Clock/124 5
// 110, 111 Reserved
#endif


#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_XMC7) && MG_ENABLE_DRIVER_XMC7

struct mg_tcpip_driver_xmc7_data {
int mdc_cr; // Valid values: -1, 0, 1, 2, 3, 4, 5
uint8_t phy_addr;
};
Expand All @@ -3220,31 +3212,45 @@ struct mg_tcpip_driver_xmc_data {
#endif

#ifndef MG_DRIVER_MDC_CR
#define MG_DRIVER_MDC_CR 4
#define MG_DRIVER_MDC_CR 3
#endif

#define MG_TCPIP_DRIVER_INIT(mgr) \
do { \
static struct mg_tcpip_driver_xmc_data driver_data_; \
static struct mg_tcpip_driver_xmc7_data driver_data_; \
static struct mg_tcpip_if mif_; \
driver_data_.mdc_cr = MG_DRIVER_MDC_CR; \
driver_data_.phy_addr = MG_TCPIP_PHY_ADDR; \
mif_.ip = MG_TCPIP_IP; \
mif_.mask = MG_TCPIP_MASK; \
mif_.gw = MG_TCPIP_GW; \
mif_.driver = &mg_tcpip_driver_xmc; \
mif_.driver = &mg_tcpip_driver_xmc7; \
mif_.driver_data = &driver_data_; \
MG_SET_MAC_ADDRESS(mif_.mac); \
mg_tcpip_init(mgr, &mif_); \
MG_INFO(("Driver: xmc, MAC: %M", mg_print_mac, mif_.mac)); \
MG_INFO(("Driver: xmc7, MAC: %M", mg_print_mac, mif_.mac)); \
} while (0)

#endif


#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_XMC7) && MG_ENABLE_DRIVER_XMC7

struct mg_tcpip_driver_xmc7_data {
#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_XMC) && MG_ENABLE_DRIVER_XMC

struct mg_tcpip_driver_xmc_data {
// 13.2.8.1 Station Management Functions
// MDC clock divider (). MDC clock is derived from ETH MAC clock
// It must not exceed 2.5MHz
// ETH Clock range DIVIDER mdc_cr VALUE
// --------------------------------------------
// -1 <-- tell driver to guess the value
// 60-100 MHz ETH Clock/42 0
// 100-150 MHz ETH Clock/62 1
// 20-35 MHz ETH Clock/16 2
// 35-60 MHz ETH Clock/26 3
// 150-250 MHz ETH Clock/102 4
// 250-300 MHz ETH Clock/124 5
// 110, 111 Reserved
int mdc_cr; // Valid values: -1, 0, 1, 2, 3, 4, 5
uint8_t phy_addr;
};
Expand All @@ -3254,28 +3260,27 @@ struct mg_tcpip_driver_xmc7_data {
#endif

#ifndef MG_DRIVER_MDC_CR
#define MG_DRIVER_MDC_CR 3
#define MG_DRIVER_MDC_CR 4
#endif

#define MG_TCPIP_DRIVER_INIT(mgr) \
do { \
static struct mg_tcpip_driver_xmc7_data driver_data_; \
static struct mg_tcpip_driver_xmc_data driver_data_; \
static struct mg_tcpip_if mif_; \
driver_data_.mdc_cr = MG_DRIVER_MDC_CR; \
driver_data_.phy_addr = MG_TCPIP_PHY_ADDR; \
mif_.ip = MG_TCPIP_IP; \
mif_.mask = MG_TCPIP_MASK; \
mif_.gw = MG_TCPIP_GW; \
mif_.driver = &mg_tcpip_driver_xmc7; \
mif_.driver = &mg_tcpip_driver_xmc; \
mif_.driver_data = &driver_data_; \
MG_SET_MAC_ADDRESS(mif_.mac); \
mg_tcpip_init(mgr, &mif_); \
MG_INFO(("Driver: xmc7, MAC: %M", mg_print_mac, mif_.mac)); \
MG_INFO(("Driver: xmc, MAC: %M", mg_print_mac, mif_.mac)); \
} while (0)

#endif


#ifdef __cplusplus
}
#endif
Expand Down
19 changes: 11 additions & 8 deletions src/mqtt.c
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ void mg_mqtt_login(struct mg_connection *c, const struct mg_mqtt_opts *opts) {
total_len += 2 + (uint32_t) opts->pass.len;
hdr[7] |= MQTT_HAS_PASSWORD;
}
if (opts->topic.len > 0) { // allow zero-length msgs, message.len is size_t
if (opts->topic.len > 0) { // allow zero-length msgs, message.len is size_t
total_len += 4 + (uint32_t) opts->topic.len + (uint32_t) opts->message.len;
hdr[7] |= MQTT_HAS_WILL;
}
Expand Down Expand Up @@ -322,18 +322,19 @@ uint16_t mg_mqtt_pub(struct mg_connection *c, const struct mg_mqtt_opts *opts) {
uint16_t id = opts->retransmit_id;
uint8_t flags = (uint8_t) (((opts->qos & 3) << 1) | (opts->retain ? 1 : 0));
size_t len = 2 + opts->topic.len + opts->message.len;
MG_DEBUG(("%lu [%.*s] -> [%.*s]", c->id, (int) opts->topic.len,
(char *) opts->topic.buf, (int) opts->message.len,
(char *) opts->message.buf));
MG_DEBUG(("%lu [%.*s] <- [%.*s%c", c->id, (int) opts->topic.len,
(char *) opts->topic.buf,
(int) (opts->message.len <= 10 ? opts->message.len : 10),
(char *) opts->message.buf, opts->message.len <= 10 ? ']' : ' '));
if (opts->qos > 0) len += 2;
if (c->is_mqtt5) len += get_props_size(opts->props, opts->num_props);

if (opts->qos > 0 && id != 0) flags |= 1 << 3;
mg_mqtt_send_header(c, MQTT_CMD_PUBLISH, flags, (uint32_t) len);
mg_send_u16(c, mg_htons((uint16_t) opts->topic.len));
mg_send(c, opts->topic.buf, opts->topic.len);
if (opts->qos > 0) { // need to send 'id' field
if (id == 0) { // generate new one if not resending
if (opts->qos > 0) { // need to send 'id' field
if (id == 0) { // generate new one if not resending
if (++c->mgr->mqtt_id == 0) ++c->mgr->mqtt_id;
id = c->mgr->mqtt_id;
}
Expand Down Expand Up @@ -456,8 +457,10 @@ static void mqtt_cb(struct mg_connection *c, int ev, void *ev_data) {
}
break;
case MQTT_CMD_PUBLISH: {
/*MG_DEBUG(("%lu [%.*s] -> [%.*s]", c->id, (int) mm.topic.len,
mm.topic.buf, (int) mm.data.len, mm.data.buf));*/
MG_DEBUG(("%lu [%.*s] -> [%.*s%c", c->id, (int) mm.topic.len,
mm.topic.buf,
(int) (mm.data.len <= 10 ? mm.data.len : 10), mm.data.buf,
mm.data.len <= 10 ? ']' : ' '));
if (mm.qos > 0) {
uint16_t id = mg_ntohs(mm.id);
uint32_t remaining_len = sizeof(id);
Expand Down
4 changes: 2 additions & 2 deletions test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,11 @@ s390: CC = $(DOCKER) mdashnet/s390 cc
s390: RUN = $(DOCKER) mdashnet/s390
s390: test

arm: DEFS += -DMG_ENABLE_POSIX_FS=0 -DMG_ENABLE_TCPIP=1 -DMG_ENABLE_TCPIP_DRIVER_INIT=0 -DMG_ARCH=MG_ARCH_NEWLIB
arm: DEFS += -DMG_ENABLE_POSIX_FS=0 -DMG_ENABLE_TCPIP=1 -DMG_ENABLE_TCPIP_DRIVER_INIT=0 -DMG_ARCH=MG_ARCH_NEWLIB -DNO_SLEEP_ABORT
arm: mongoose.h $(SRCS)
$(DOCKER) mdashnet/armgcc arm-none-eabi-gcc -mcpu=cortex-m3 -mthumb $(SRCS) $(OPTS) $(WARN) $(INCS) $(DEFS) $(TFLAGS) -o unit_test -nostartfiles --specs nosys.specs -e 0

riscv: DEFS += -DMG_ENABLE_POSIX_FS=0 -DMG_ENABLE_TCPIP=1 -DMG_ENABLE_TCPIP_DRIVER_INIT=0 -DMG_ARCH=MG_ARCH_NEWLIB
riscv: DEFS += -DMG_ENABLE_POSIX_FS=0 -DMG_ENABLE_TCPIP=1 -DMG_ENABLE_TCPIP_DRIVER_INIT=0 -DMG_ARCH=MG_ARCH_NEWLIB -DNO_SLEEP_ABORT
riscv: mongoose.h $(SRCS)
$(DOCKER) mdashnet/riscv riscv-none-elf-gcc -march=rv32imc -mabi=ilp32 $(SRCS) $(OPTS) $(WARN) $(INCS) $(DEFS) $(TFLAGS) -o unit_test

Expand Down
10 changes: 9 additions & 1 deletion test/mip_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,20 @@ static int s_num_tests = 0;
static bool s_sent_fragment = 0;
static int s_seg_sent = 0;

#ifdef NO_SLEEP_ABORT
#define ABORT() abort()
#else
#define ABORT() \
sleep(2); /* 2s, GH print reason */ \
abort();
#endif

#define ASSERT(expr) \
do { \
s_num_tests++; \
if (!(expr)) { \
printf("FAILURE %s:%d: %s\n", __FILE__, __LINE__, #expr); \
abort(); \
ABORT(); \
} \
} while (0)

Expand Down
31 changes: 22 additions & 9 deletions test/unit_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,20 @@

static int s_num_tests = 0;

#ifdef NO_SLEEP_ABORT
#define ABORT() abort()
#else
#define ABORT() \
sleep(2); /* 2s, GH print reason */ \
abort();
#endif

#define ASSERT(expr) \
do { \
s_num_tests++; \
if (!(expr)) { \
printf("FAILURE %s:%d: %s\n", __FILE__, __LINE__, #expr); \
abort(); \
ABORT(); \
} \
} while (0)

Expand Down Expand Up @@ -329,7 +337,7 @@ static void sntp_cb(struct mg_connection *c, int ev, void *ev_data) {
(void) c;
}

static void test_sntp_server(const char *url) {
static bool test_sntp_server(const char *url) {
int64_t ms = 0;
struct mg_mgr mgr;
struct mg_connection *c = NULL;
Expand All @@ -341,25 +349,29 @@ static void test_sntp_server(const char *url) {
ASSERT(c->is_udp == 1);
for (i = 0; i < 60 && ms == 0; i++) mg_mgr_poll(&mgr, 50);
MG_DEBUG(("server: %s, ms: %lld", url ? url : "(default)", ms));
#if !defined(NO_SNTP_CHECK)
ASSERT(ms > 0);
#endif
mg_mgr_free(&mgr);
return ms > 0;
}

static void test_sntp(void) {
bool result;
const unsigned char bad[] =
"\x55\x02\x00\xeb\x00\x00\x00\x1e\x00\x00\x07\xb6\x3e\xc9\xd6\xa2"
"\xdb\xde\xea\x30\x91\x86\xb7\x10\xdb\xde\xed\x98\x00\x00\x00\xde"
"\xdb\xde\xed\x99\x0a\xe2\xc7\x96\xdb\xde\xed\x99\x0a\xe4\x6b\xda";

ASSERT(mg_sntp_parse(bad, sizeof(bad)) < 0);
ASSERT(mg_sntp_parse(NULL, 0) == -1);
// NOTE(cpq): temporarily disabled until Github Actions fix their NTP
// port blockage issue, https://github.com/actions/runner-images/issues/5615
// test_sntp_server("udp://time.apple.com:123");
test_sntp_server("udp://time.windows.com:123");
// NOTE(): historical NTP port blockage issue; expect at least one to be
// reachable and work. https://github.com/actions/runner-images/issues/5615
result = test_sntp_server("udp://time.apple.com:123") ||
test_sntp_server("udp://time.windows.com:123") ||
test_sntp_server(NULL);
#if defined(NO_SNTP_CHECK)
(void) result;
#else
ASSERT(result);
#endif
}

struct mqtt_data {
Expand Down Expand Up @@ -567,6 +579,7 @@ static void test_mqtt_ver(uint8_t mqtt_version) {
const char *url = "mqtt://broker.hivemq.com:1883";
int i, retries;

MG_DEBUG(("ver: %u", mqtt_version));
// Connect with options: version, clean session, last will, keepalive
// time. Don't set retain, some runners are not random
test_data.flags = 0;
Expand Down

0 comments on commit 5479918

Please sign in to comment.