Skip to content

Commit 5479918

Browse files
authored
Merge pull request #2996 from cesanta/test
improve tests and debuggability
2 parents 317a152 + 6d0320b commit 5479918

File tree

9 files changed

+91
-59
lines changed

9 files changed

+91
-59
lines changed

.github/workflows/codeql.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name: "CodeQL Scanning"
33

44
on:
55
schedule:
6-
- cron: '30 2 * * *' # run at 2:30 AM UTC
6+
- cron: '30 23 * * *' # run at 11:30 PM UTC
77
# Allow manual runs
88
workflow_dispatch:
99

.github/workflows/nightly.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: Full build and test
22
on:
33
schedule:
4-
- cron: '0 1 * * *' # run at 1 AM UTC
4+
- cron: '0 22 * * *' # run at 10 PM UTC
55
# Allow manual runs
66
workflow_dispatch:
77
env:
@@ -117,7 +117,7 @@ jobs:
117117
name: macos SSL=${{ matrix.ssl }} TFLAGS=${{ matrix.select }}
118118
env:
119119
SSL: ${{ matrix.ssl }}
120-
TFLAGS: ${{ matrix.select }} -DNO_SNTP_CHECK -Wno-sign-conversion # Workaround for MbedTLS 3.5.0
120+
TFLAGS: ${{ matrix.select }} -Wno-sign-conversion # Workaround for MbedTLS 3.5.0
121121
HOMEBREW_NO_AUTO_UPDATE: 1
122122
steps:
123123
- uses: actions/checkout@v4

.github/workflows/quicktest.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ jobs:
5959
name: macos SSL=${{ matrix.ssl }}
6060
env:
6161
SSL: ${{ matrix.ssl }}
62-
TFLAGS: -DNO_SNTP_CHECK
62+
#TFLAGS: -DNO_SNTP_CHECK
6363
HOMEBREW_NO_AUTO_UPDATE: 1
6464
steps:
6565
- uses: actions/checkout@v4

mongoose.c

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3523,7 +3523,7 @@ void mg_mqtt_login(struct mg_connection *c, const struct mg_mqtt_opts *opts) {
35233523
total_len += 2 + (uint32_t) opts->pass.len;
35243524
hdr[7] |= MQTT_HAS_PASSWORD;
35253525
}
3526-
if (opts->topic.len > 0) { // allow zero-length msgs, message.len is size_t
3526+
if (opts->topic.len > 0) { // allow zero-length msgs, message.len is size_t
35273527
total_len += 4 + (uint32_t) opts->topic.len + (uint32_t) opts->message.len;
35283528
hdr[7] |= MQTT_HAS_WILL;
35293529
}
@@ -3569,18 +3569,19 @@ uint16_t mg_mqtt_pub(struct mg_connection *c, const struct mg_mqtt_opts *opts) {
35693569
uint16_t id = opts->retransmit_id;
35703570
uint8_t flags = (uint8_t) (((opts->qos & 3) << 1) | (opts->retain ? 1 : 0));
35713571
size_t len = 2 + opts->topic.len + opts->message.len;
3572-
MG_DEBUG(("%lu [%.*s] -> [%.*s]", c->id, (int) opts->topic.len,
3573-
(char *) opts->topic.buf, (int) opts->message.len,
3574-
(char *) opts->message.buf));
3572+
MG_DEBUG(("%lu [%.*s] <- [%.*s%c", c->id, (int) opts->topic.len,
3573+
(char *) opts->topic.buf,
3574+
(int) (opts->message.len <= 10 ? opts->message.len : 10),
3575+
(char *) opts->message.buf, opts->message.len <= 10 ? ']' : ' '));
35753576
if (opts->qos > 0) len += 2;
35763577
if (c->is_mqtt5) len += get_props_size(opts->props, opts->num_props);
35773578

35783579
if (opts->qos > 0 && id != 0) flags |= 1 << 3;
35793580
mg_mqtt_send_header(c, MQTT_CMD_PUBLISH, flags, (uint32_t) len);
35803581
mg_send_u16(c, mg_htons((uint16_t) opts->topic.len));
35813582
mg_send(c, opts->topic.buf, opts->topic.len);
3582-
if (opts->qos > 0) { // need to send 'id' field
3583-
if (id == 0) { // generate new one if not resending
3583+
if (opts->qos > 0) { // need to send 'id' field
3584+
if (id == 0) { // generate new one if not resending
35843585
if (++c->mgr->mqtt_id == 0) ++c->mgr->mqtt_id;
35853586
id = c->mgr->mqtt_id;
35863587
}
@@ -3703,8 +3704,10 @@ static void mqtt_cb(struct mg_connection *c, int ev, void *ev_data) {
37033704
}
37043705
break;
37053706
case MQTT_CMD_PUBLISH: {
3706-
/*MG_DEBUG(("%lu [%.*s] -> [%.*s]", c->id, (int) mm.topic.len,
3707-
mm.topic.buf, (int) mm.data.len, mm.data.buf));*/
3707+
MG_DEBUG(("%lu [%.*s] -> [%.*s%c", c->id, (int) mm.topic.len,
3708+
mm.topic.buf,
3709+
(int) (mm.data.len <= 10 ? mm.data.len : 10), mm.data.buf,
3710+
mm.data.len <= 10 ? ']' : ' '));
37083711
if (mm.qos > 0) {
37093712
uint16_t id = mg_ntohs(mm.id);
37103713
uint32_t remaining_len = sizeof(id);

mongoose.h

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2712,6 +2712,7 @@ bool mg_ota_flash_end(struct mg_flash *flash);
27122712

27132713

27142714

2715+
27152716
struct mg_tcpip_if; // Mongoose TCP/IP network interface
27162717

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

27792780
extern struct mg_tcpip_driver mg_tcpip_driver_stm32f;
27802781
extern struct mg_tcpip_driver mg_tcpip_driver_w5500;
2781-
extern struct mg_tcpip_driver mg_tcpip_driver_w5100;
27822782
extern struct mg_tcpip_driver mg_tcpip_driver_tm4c;
27832783
extern struct mg_tcpip_driver mg_tcpip_driver_tms570;
27842784
extern struct mg_tcpip_driver mg_tcpip_driver_stm32h;
@@ -3195,22 +3195,14 @@ struct mg_tcpip_driver_tms570_data {
31953195

31963196

31973197

3198-
#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_XMC) && MG_ENABLE_DRIVER_XMC
3198+
#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_W5500) && MG_ENABLE_DRIVER_W5500
31993199

3200-
struct mg_tcpip_driver_xmc_data {
3201-
// 13.2.8.1 Station Management Functions
3202-
// MDC clock divider (). MDC clock is derived from ETH MAC clock
3203-
// It must not exceed 2.5MHz
3204-
// ETH Clock range DIVIDER mdc_cr VALUE
3205-
// --------------------------------------------
3206-
// -1 <-- tell driver to guess the value
3207-
// 60-100 MHz ETH Clock/42 0
3208-
// 100-150 MHz ETH Clock/62 1
3209-
// 20-35 MHz ETH Clock/16 2
3210-
// 35-60 MHz ETH Clock/26 3
3211-
// 150-250 MHz ETH Clock/102 4
3212-
// 250-300 MHz ETH Clock/124 5
3213-
// 110, 111 Reserved
3200+
#endif
3201+
3202+
3203+
#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_XMC7) && MG_ENABLE_DRIVER_XMC7
3204+
3205+
struct mg_tcpip_driver_xmc7_data {
32143206
int mdc_cr; // Valid values: -1, 0, 1, 2, 3, 4, 5
32153207
uint8_t phy_addr;
32163208
};
@@ -3220,31 +3212,45 @@ struct mg_tcpip_driver_xmc_data {
32203212
#endif
32213213

32223214
#ifndef MG_DRIVER_MDC_CR
3223-
#define MG_DRIVER_MDC_CR 4
3215+
#define MG_DRIVER_MDC_CR 3
32243216
#endif
32253217

32263218
#define MG_TCPIP_DRIVER_INIT(mgr) \
32273219
do { \
3228-
static struct mg_tcpip_driver_xmc_data driver_data_; \
3220+
static struct mg_tcpip_driver_xmc7_data driver_data_; \
32293221
static struct mg_tcpip_if mif_; \
32303222
driver_data_.mdc_cr = MG_DRIVER_MDC_CR; \
32313223
driver_data_.phy_addr = MG_TCPIP_PHY_ADDR; \
32323224
mif_.ip = MG_TCPIP_IP; \
32333225
mif_.mask = MG_TCPIP_MASK; \
32343226
mif_.gw = MG_TCPIP_GW; \
3235-
mif_.driver = &mg_tcpip_driver_xmc; \
3227+
mif_.driver = &mg_tcpip_driver_xmc7; \
32363228
mif_.driver_data = &driver_data_; \
32373229
MG_SET_MAC_ADDRESS(mif_.mac); \
32383230
mg_tcpip_init(mgr, &mif_); \
3239-
MG_INFO(("Driver: xmc, MAC: %M", mg_print_mac, mif_.mac)); \
3231+
MG_INFO(("Driver: xmc7, MAC: %M", mg_print_mac, mif_.mac)); \
32403232
} while (0)
32413233

32423234
#endif
32433235

32443236

3245-
#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_XMC7) && MG_ENABLE_DRIVER_XMC7
32463237

3247-
struct mg_tcpip_driver_xmc7_data {
3238+
#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_XMC) && MG_ENABLE_DRIVER_XMC
3239+
3240+
struct mg_tcpip_driver_xmc_data {
3241+
// 13.2.8.1 Station Management Functions
3242+
// MDC clock divider (). MDC clock is derived from ETH MAC clock
3243+
// It must not exceed 2.5MHz
3244+
// ETH Clock range DIVIDER mdc_cr VALUE
3245+
// --------------------------------------------
3246+
// -1 <-- tell driver to guess the value
3247+
// 60-100 MHz ETH Clock/42 0
3248+
// 100-150 MHz ETH Clock/62 1
3249+
// 20-35 MHz ETH Clock/16 2
3250+
// 35-60 MHz ETH Clock/26 3
3251+
// 150-250 MHz ETH Clock/102 4
3252+
// 250-300 MHz ETH Clock/124 5
3253+
// 110, 111 Reserved
32483254
int mdc_cr; // Valid values: -1, 0, 1, 2, 3, 4, 5
32493255
uint8_t phy_addr;
32503256
};
@@ -3254,28 +3260,27 @@ struct mg_tcpip_driver_xmc7_data {
32543260
#endif
32553261

32563262
#ifndef MG_DRIVER_MDC_CR
3257-
#define MG_DRIVER_MDC_CR 3
3263+
#define MG_DRIVER_MDC_CR 4
32583264
#endif
32593265

32603266
#define MG_TCPIP_DRIVER_INIT(mgr) \
32613267
do { \
3262-
static struct mg_tcpip_driver_xmc7_data driver_data_; \
3268+
static struct mg_tcpip_driver_xmc_data driver_data_; \
32633269
static struct mg_tcpip_if mif_; \
32643270
driver_data_.mdc_cr = MG_DRIVER_MDC_CR; \
32653271
driver_data_.phy_addr = MG_TCPIP_PHY_ADDR; \
32663272
mif_.ip = MG_TCPIP_IP; \
32673273
mif_.mask = MG_TCPIP_MASK; \
32683274
mif_.gw = MG_TCPIP_GW; \
3269-
mif_.driver = &mg_tcpip_driver_xmc7; \
3275+
mif_.driver = &mg_tcpip_driver_xmc; \
32703276
mif_.driver_data = &driver_data_; \
32713277
MG_SET_MAC_ADDRESS(mif_.mac); \
32723278
mg_tcpip_init(mgr, &mif_); \
3273-
MG_INFO(("Driver: xmc7, MAC: %M", mg_print_mac, mif_.mac)); \
3279+
MG_INFO(("Driver: xmc, MAC: %M", mg_print_mac, mif_.mac)); \
32743280
} while (0)
32753281

32763282
#endif
32773283

3278-
32793284
#ifdef __cplusplus
32803285
}
32813286
#endif

src/mqtt.c

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ void mg_mqtt_login(struct mg_connection *c, const struct mg_mqtt_opts *opts) {
276276
total_len += 2 + (uint32_t) opts->pass.len;
277277
hdr[7] |= MQTT_HAS_PASSWORD;
278278
}
279-
if (opts->topic.len > 0) { // allow zero-length msgs, message.len is size_t
279+
if (opts->topic.len > 0) { // allow zero-length msgs, message.len is size_t
280280
total_len += 4 + (uint32_t) opts->topic.len + (uint32_t) opts->message.len;
281281
hdr[7] |= MQTT_HAS_WILL;
282282
}
@@ -322,18 +322,19 @@ uint16_t mg_mqtt_pub(struct mg_connection *c, const struct mg_mqtt_opts *opts) {
322322
uint16_t id = opts->retransmit_id;
323323
uint8_t flags = (uint8_t) (((opts->qos & 3) << 1) | (opts->retain ? 1 : 0));
324324
size_t len = 2 + opts->topic.len + opts->message.len;
325-
MG_DEBUG(("%lu [%.*s] -> [%.*s]", c->id, (int) opts->topic.len,
326-
(char *) opts->topic.buf, (int) opts->message.len,
327-
(char *) opts->message.buf));
325+
MG_DEBUG(("%lu [%.*s] <- [%.*s%c", c->id, (int) opts->topic.len,
326+
(char *) opts->topic.buf,
327+
(int) (opts->message.len <= 10 ? opts->message.len : 10),
328+
(char *) opts->message.buf, opts->message.len <= 10 ? ']' : ' '));
328329
if (opts->qos > 0) len += 2;
329330
if (c->is_mqtt5) len += get_props_size(opts->props, opts->num_props);
330331

331332
if (opts->qos > 0 && id != 0) flags |= 1 << 3;
332333
mg_mqtt_send_header(c, MQTT_CMD_PUBLISH, flags, (uint32_t) len);
333334
mg_send_u16(c, mg_htons((uint16_t) opts->topic.len));
334335
mg_send(c, opts->topic.buf, opts->topic.len);
335-
if (opts->qos > 0) { // need to send 'id' field
336-
if (id == 0) { // generate new one if not resending
336+
if (opts->qos > 0) { // need to send 'id' field
337+
if (id == 0) { // generate new one if not resending
337338
if (++c->mgr->mqtt_id == 0) ++c->mgr->mqtt_id;
338339
id = c->mgr->mqtt_id;
339340
}
@@ -456,8 +457,10 @@ static void mqtt_cb(struct mg_connection *c, int ev, void *ev_data) {
456457
}
457458
break;
458459
case MQTT_CMD_PUBLISH: {
459-
/*MG_DEBUG(("%lu [%.*s] -> [%.*s]", c->id, (int) mm.topic.len,
460-
mm.topic.buf, (int) mm.data.len, mm.data.buf));*/
460+
MG_DEBUG(("%lu [%.*s] -> [%.*s%c", c->id, (int) mm.topic.len,
461+
mm.topic.buf,
462+
(int) (mm.data.len <= 10 ? mm.data.len : 10), mm.data.buf,
463+
mm.data.len <= 10 ? ']' : ' '));
461464
if (mm.qos > 0) {
462465
uint16_t id = mg_ntohs(mm.id);
463466
uint32_t remaining_len = sizeof(id);

test/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,11 +158,11 @@ s390: CC = $(DOCKER) mdashnet/s390 cc
158158
s390: RUN = $(DOCKER) mdashnet/s390
159159
s390: test
160160

161-
arm: DEFS += -DMG_ENABLE_POSIX_FS=0 -DMG_ENABLE_TCPIP=1 -DMG_ENABLE_TCPIP_DRIVER_INIT=0 -DMG_ARCH=MG_ARCH_NEWLIB
161+
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
162162
arm: mongoose.h $(SRCS)
163163
$(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
164164

165-
riscv: DEFS += -DMG_ENABLE_POSIX_FS=0 -DMG_ENABLE_TCPIP=1 -DMG_ENABLE_TCPIP_DRIVER_INIT=0 -DMG_ARCH=MG_ARCH_NEWLIB
165+
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
166166
riscv: mongoose.h $(SRCS)
167167
$(DOCKER) mdashnet/riscv riscv-none-elf-gcc -march=rv32imc -mabi=ilp32 $(SRCS) $(OPTS) $(WARN) $(INCS) $(DEFS) $(TFLAGS) -o unit_test
168168

test/mip_test.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,20 @@ static int s_num_tests = 0;
88
static bool s_sent_fragment = 0;
99
static int s_seg_sent = 0;
1010

11+
#ifdef NO_SLEEP_ABORT
12+
#define ABORT() abort()
13+
#else
14+
#define ABORT() \
15+
sleep(2); /* 2s, GH print reason */ \
16+
abort();
17+
#endif
18+
1119
#define ASSERT(expr) \
1220
do { \
1321
s_num_tests++; \
1422
if (!(expr)) { \
1523
printf("FAILURE %s:%d: %s\n", __FILE__, __LINE__, #expr); \
16-
abort(); \
24+
ABORT(); \
1725
} \
1826
} while (0)
1927

test/unit_test.c

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,20 @@
55

66
static int s_num_tests = 0;
77

8+
#ifdef NO_SLEEP_ABORT
9+
#define ABORT() abort()
10+
#else
11+
#define ABORT() \
12+
sleep(2); /* 2s, GH print reason */ \
13+
abort();
14+
#endif
15+
816
#define ASSERT(expr) \
917
do { \
1018
s_num_tests++; \
1119
if (!(expr)) { \
1220
printf("FAILURE %s:%d: %s\n", __FILE__, __LINE__, #expr); \
13-
abort(); \
21+
ABORT(); \
1422
} \
1523
} while (0)
1624

@@ -329,7 +337,7 @@ static void sntp_cb(struct mg_connection *c, int ev, void *ev_data) {
329337
(void) c;
330338
}
331339

332-
static void test_sntp_server(const char *url) {
340+
static bool test_sntp_server(const char *url) {
333341
int64_t ms = 0;
334342
struct mg_mgr mgr;
335343
struct mg_connection *c = NULL;
@@ -341,25 +349,29 @@ static void test_sntp_server(const char *url) {
341349
ASSERT(c->is_udp == 1);
342350
for (i = 0; i < 60 && ms == 0; i++) mg_mgr_poll(&mgr, 50);
343351
MG_DEBUG(("server: %s, ms: %lld", url ? url : "(default)", ms));
344-
#if !defined(NO_SNTP_CHECK)
345-
ASSERT(ms > 0);
346-
#endif
347352
mg_mgr_free(&mgr);
353+
return ms > 0;
348354
}
349355

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

356363
ASSERT(mg_sntp_parse(bad, sizeof(bad)) < 0);
357364
ASSERT(mg_sntp_parse(NULL, 0) == -1);
358-
// NOTE(cpq): temporarily disabled until Github Actions fix their NTP
359-
// port blockage issue, https://github.com/actions/runner-images/issues/5615
360-
// test_sntp_server("udp://time.apple.com:123");
361-
test_sntp_server("udp://time.windows.com:123");
365+
// NOTE(): historical NTP port blockage issue; expect at least one to be
366+
// reachable and work. https://github.com/actions/runner-images/issues/5615
367+
result = test_sntp_server("udp://time.apple.com:123") ||
368+
test_sntp_server("udp://time.windows.com:123") ||
362369
test_sntp_server(NULL);
370+
#if defined(NO_SNTP_CHECK)
371+
(void) result;
372+
#else
373+
ASSERT(result);
374+
#endif
363375
}
364376

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

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

0 commit comments

Comments
 (0)