Skip to content

fix tests #3196

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .github/workflows/nightly.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ jobs:
steps:
- uses: actions/checkout@v4
with: { fetch-depth: 2 }
- run: sudo apt -y update ; sudo apt -y install binfmt-support qemu-user-static && docker run --rm --privileged multiarch/qemu-user-static --reset -p yes && make -C test s390 SSL=${{ matrix.ssl }}
- run: sudo apt -y update ; sudo apt -y install binfmt-support qemu-user-static && docker run --rm --privileged multiarch/qemu-user-static --reset -p yes && make -C test s390 SSL=${{ matrix.ssl }} MULTIREC=NO
armhf:
runs-on: ubuntu-latest
strategy:
Expand All @@ -75,7 +75,7 @@ jobs:
steps:
- uses: actions/checkout@v4
with: { fetch-depth: 2 }
- run: sudo apt -y update ; sudo apt -y install binfmt-support qemu-user-static && docker run --rm --privileged multiarch/qemu-user-static --reset -p yes && make -C test armhf SSL=${{ matrix.ssl }}
- run: sudo apt -y update ; sudo apt -y install binfmt-support qemu-user-static && docker run --rm --privileged multiarch/qemu-user-static --reset -p yes && make -C test armhf SSL=${{ matrix.ssl }} MULTIREC=NO
unamalgamated-mg_prefix:
runs-on: ubuntu-latest
strategy:
Expand Down Expand Up @@ -138,7 +138,7 @@ jobs:
select: -DMG_ENABLE_POLL=1
name: windows ${{ matrix.target }} SSL=${{ matrix.ssl }} TFLAGS=${{ matrix.select }}
env:
SSL: SSL=${{ matrix.ssl }}
SSL: ${{ matrix.ssl }}
TFLAGS: ${{ matrix.select }}
steps:
- uses: actions/checkout@v4
Expand Down Expand Up @@ -357,7 +357,7 @@ jobs:
matrix:
ssl: ["", -DMG_TLS=MG_TLS_BUILTIN]
example:
- path: tutorials/http/device-dashboard/microchip/same54-xpro
- path: http/device-dashboard/microchip/same54-xpro
- path: nxp/rt1020-evk-make-freertos-builtin
- path: nxp/rt1060-evk-make-freertos-builtin
- path: nxp/rt1170-evk-make-freertos-builtin
Expand Down
6 changes: 3 additions & 3 deletions mongoose.c
Original file line number Diff line number Diff line change
Expand Up @@ -9763,6 +9763,9 @@ void mg_timer_poll(struct mg_timer **head, uint64_t now_ms) {
*
*******************************************************************************/



#if MG_TLS == MG_TLS_BUILTIN
/******************************************************************************/
#define AES_DECRYPTION 1 // whether AES decryption is supported
/******************************************************************************/
Expand All @@ -9772,9 +9775,6 @@ void mg_timer_poll(struct mg_timer **head, uint64_t now_ms) {





#if MG_TLS == MG_TLS_BUILTIN
/******************************************************************************
* AES_INIT_KEYGEN_TABLES : MUST be called once before any AES use
******************************************************************************/
Expand Down
6 changes: 3 additions & 3 deletions src/tls_aes128.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,18 @@
*
*******************************************************************************/

#include "arch.h"
#include "tls.h"
#if MG_TLS == MG_TLS_BUILTIN
/******************************************************************************/
#define AES_DECRYPTION 1 // whether AES decryption is supported
/******************************************************************************/

#define MG_ENCRYPT 1 // specify whether we're encrypting
#define MG_DECRYPT 0 // or decrypting

#include "arch.h"
#include "tls.h"
#include "tls_aes128.h"

#if MG_TLS == MG_TLS_BUILTIN
/******************************************************************************
* AES_INIT_KEYGEN_TABLES : MUST be called once before any AES use
******************************************************************************/
Expand Down
16 changes: 11 additions & 5 deletions test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ ROOT_DIR = $(realpath $(CWD)/..)
ENV ?= -e Tmp=. -e WINEDEBUG=-all
DOCKER_BIN ?= docker
DOCKER ?= $(DOCKER_BIN) run --platform linux/amd64 --rm $(ENV) -v $(ROOT_DIR):$(ROOT_DIR) -w $(CWD)
VCFLAGS = /nologo /W3 /O2 /MD /I. $(DEFS) $(TFLAGS)
VCFLAGS ?= /nologo /W3 /O2 /MD /I.
IPV6 ?= 1
ASAN ?= -fsanitize=address,undefined,alignment -fno-sanitize-recover=all -fno-omit-frame-pointer -fno-common
ASAN_OPTIONS ?= detect_leaks=1
Expand Down Expand Up @@ -75,7 +75,7 @@ LDFLAGS += -L$(WOLFSSL)/lib -lwolfssl
endif

ifeq "$(SSL)" "BUILTIN"
CFLAGS += -DMG_TLS=MG_TLS_BUILTIN
DEFS += -DMG_TLS=MG_TLS_BUILTIN
endif

all:
Expand Down Expand Up @@ -132,7 +132,12 @@ test: Makefile mongoose.h $(SRCS) tls_multirec/server
ASAN_OPTIONS=$(ASAN_OPTIONS) $(RUN) ./unit_test

tls_multirec/server: FORCE
ifneq "$(SSL)" ""
ifeq "$(MULTIREC)" ""
$(MAKE) -C tls_multirec CC=gcc
endif
endif
true

FORCE:
true
Expand Down Expand Up @@ -164,6 +169,7 @@ armhf: test

s390: ASAN=
s390: IPV6=0
s390: CFLAGS += -Wno-stringop-overflow
s390: CC = $(DOCKER) mdashnet/s390 cc
s390: RUN = $(DOCKER) mdashnet/s390
s390: test
Expand All @@ -177,15 +183,15 @@ riscv: mongoose.h $(SRCS)
$(DOCKER) mdashnet/riscv riscv-none-elf-gcc -march=rv32imc -mabi=ilp32 $(SRCS) $(OPTS) $(WARN) $(INCS) $(DEFS) $(TFLAGS) -o unit_test

vc98: Makefile mongoose.h $(SRCS)
$(DOCKER) mdashnet/vc98 wine cl $(SRCS) $(VCFLAGS) /[email protected]
$(DOCKER) mdashnet/vc98 wine cl $(SRCS) $(VCFLAGS) $(DEFS) $(TFLAGS) /[email protected]
$(DOCKER) mdashnet/vc98 wine [email protected]

vc17: Makefile mongoose.h $(SRCS)
$(DOCKER) mdashnet/vc17 wine64 cl $(SRCS) $(VCFLAGS) /[email protected]
$(DOCKER) mdashnet/vc17 wine64 cl $(SRCS) $(VCFLAGS) $(DEFS) $(TFLAGS) /[email protected]
$(DOCKER) mdashnet/vc17 wine64 [email protected]

vc22: Makefile mongoose.h $(SRCS)
$(DOCKER) mdashnet/vc22 wine64 cl $(SRCS) $(VCFLAGS) /[email protected]
$(DOCKER) mdashnet/vc22 wine64 cl $(SRCS) $(VCFLAGS) $(DEFS) $(TFLAGS) /[email protected]
$(DOCKER) mdashnet/vc22 wine64 [email protected]

mingw: Makefile mongoose.h $(SRCS)
Expand Down
85 changes: 46 additions & 39 deletions test/mip_test.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#define MG_ENABLE_TCPIP 1
#define MG_ENABLE_TCPIP_DRIVER_INIT 0

#include "mongoose.c"
#include "mongoose.c" // order is important, this one first
#include "driver_mock.c"

static int s_num_tests = 0;
Expand All @@ -11,9 +11,9 @@ static int s_seg_sent = 0;
#ifdef NO_SLEEP_ABORT
#define ABORT() abort()
#else
#define ABORT() \
sleep(2); /* 2s, GH print reason */ \
abort();
#define ABORT() \
sleep(2); /* 2s, GH print reason */ \
abort();
#endif

#define ASSERT(expr) \
Expand Down Expand Up @@ -48,18 +48,21 @@ static void fn(struct mg_connection *c, int ev, void *ev_data) {
static void frag_recv_fn(struct mg_connection *c, int ev, void *ev_data) {
if (ev == MG_EV_ERROR) {
if (s_sent_fragment) {
ASSERT(strcmp((char*) ev_data, "Received fragmented packet") == 0);
ASSERT(strcmp((char *) ev_data, "Received fragmented packet") == 0);
}
}
(void) c, (void) ev_data;
}

// mock send to a non-existent peer using the listener connection
static void frag_send_fn(struct mg_connection *c, int ev, void *ev_data) {
static bool s_sent;
static int s_seg_sizes[] = {416, 416, 368};
static int s_seg_sizes[] = {416, 416, 368}; // based on len=1200 and MTU=500
if (ev == MG_EV_POLL) {
if (!s_sent) {
c->send.len = 1200; // setting TCP payload size
struct connstate *s = (struct connstate *) (c + 1);
s->dmss = 1500; // mock set some destination MSS way larger
c->send.len = 1200; // setting TCP payload size
s_sent = true;
}
} else if (ev == MG_EV_WRITE) {
Expand Down Expand Up @@ -88,7 +91,7 @@ static void test_poll(void) {
struct driver_data {
char buf[DRIVER_BUF_SIZE];
size_t len;
bool tx_ready; // data can be read from tx
bool tx_ready; // data can be read from tx
};

static struct driver_data s_driver_data;
Expand All @@ -111,7 +114,7 @@ static size_t if_rx(void *buf, size_t len, struct mg_tcpip_if *ifp) {
if (!driver_data->len) return 0;
if (len > driver_data->len) len = driver_data->len;
memcpy(buf, driver_data->buf, len);
driver_data->len = 0; // cleaning up the buffer
driver_data->len = 0; // cleaning up the buffer
return len;
}

Expand All @@ -132,8 +135,8 @@ static void create_tcp_pkt(struct eth *e, struct ip *ip, uint32_t seq,
memcpy(s_driver_data.buf, e, sizeof(*e));
memcpy(s_driver_data.buf + sizeof(*e), ip, sizeof(*ip));
memcpy(s_driver_data.buf + sizeof(*e) + sizeof(*ip), &t, sizeof(struct tcp));
s_driver_data.len = sizeof(*e) + sizeof(*ip)
+ sizeof(struct tcp) + payload_len;
s_driver_data.len =
sizeof(*e) + sizeof(*ip) + sizeof(struct tcp) + payload_len;
}

static void init_tcp_handshake(struct eth *e, struct ip *ip, struct tcp *tcp,
Expand All @@ -143,9 +146,9 @@ static void init_tcp_handshake(struct eth *e, struct ip *ip, struct tcp *tcp,
mg_mgr_poll(mgr, 0);

// SYN-ACK
while(!received_response(&s_driver_data)) mg_mgr_poll(mgr, 0);
while (!received_response(&s_driver_data)) mg_mgr_poll(mgr, 0);
tcp = (struct tcp *) (s_driver_data.buf + sizeof(struct eth) +
sizeof(struct ip));
sizeof(struct ip));
ASSERT((tcp->flags == (TH_SYN | TH_ACK)));
ASSERT((tcp->ack == mg_htonl(1001)));

Expand All @@ -154,6 +157,9 @@ static void init_tcp_handshake(struct eth *e, struct ip *ip, struct tcp *tcp,
mg_mgr_poll(mgr, 0);
}

// NOTE: a 1-byte payload could be an erroneous Keep-Alive, keep length > 1 in
// this operation, we're testing retransmissions and having len=1 won't work

static void test_retransmit(void) {
struct mg_mgr mgr;
struct eth e;
Expand All @@ -167,12 +173,13 @@ static void test_retransmit(void) {
mg_mgr_init(&mgr);
memset(&mif, 0, sizeof(mif));
memset(&s_driver_data, 0, sizeof(struct driver_data));
driver.init = NULL, driver.tx = if_tx, driver.poll = if_poll, driver.rx = if_rx;
driver.init = NULL, driver.tx = if_tx, driver.poll = if_poll,
driver.rx = if_rx;
mif.driver = &driver;
mif.driver_data = &s_driver_data;
mg_tcpip_init(&mgr, &mif);
mg_http_listen(&mgr, "http://0.0.0.0:0", fn, NULL);
mgr.conns->pfn = NULL; // HTTP handler not needed
mgr.conns->pfn = NULL; // HTTP handler not needed
mg_mgr_poll(&mgr, 0);

// setting the Ethernet header
Expand All @@ -189,20 +196,20 @@ static void test_retransmit(void) {

// packet with seq_no = 1001
ip.len =
mg_htons(sizeof(struct ip) + sizeof(struct tcp) + /* TCP Payload */ 1);
create_tcp_pkt(&e, &ip, 1001, 1, TH_PUSH | TH_ACK, 1);
mg_htons(sizeof(struct ip) + sizeof(struct tcp) + /* TCP Payload */ 2);
create_tcp_pkt(&e, &ip, 1001, 1, TH_PUSH | TH_ACK, 2);
mg_mgr_poll(&mgr, 0);
while(!received_response(&s_driver_data)) mg_mgr_poll(&mgr, 0);
while (!received_response(&s_driver_data)) mg_mgr_poll(&mgr, 0);
t = (struct tcp *) (s_driver_data.buf + sizeof(struct eth) +
sizeof(struct ip));
ASSERT((t->flags == TH_ACK));
ASSERT((t->ack == mg_htonl(1002))); // OK
ASSERT((t->ack == mg_htonl(1003))); // OK

// resend packet with seq_no = 1001 (e.g.: MIP ACK lost)
create_tcp_pkt(&e, &ip, 1001, 1, TH_PUSH | TH_ACK, 1);
create_tcp_pkt(&e, &ip, 1001, 1, TH_PUSH | TH_ACK, 2);
mg_mgr_poll(&mgr, 0);
start = mg_millis();
while(!received_response(&s_driver_data)) {
while (!received_response(&s_driver_data)) {
mg_mgr_poll(&mgr, 0);
now = mg_millis() - start;
// we wait enough time for a reply
Expand All @@ -211,31 +218,31 @@ static void test_retransmit(void) {
break;
}
}
ASSERT((!response_recv)); // replies should not be sent for duplicate packets
ASSERT((!response_recv)); // replies should not be sent for duplicate packets

// packet with seq_no = 1002 got lost/delayed, send seq_no = 1003
create_tcp_pkt(&e, &ip, 1003, 1, TH_PUSH | TH_ACK, 1);
// packet with seq_no = 1003 got lost/delayed, send seq_no = 1005
create_tcp_pkt(&e, &ip, 1005, 1, TH_PUSH | TH_ACK, 2);
mg_mgr_poll(&mgr, 0);
start = mg_millis();
while(!received_response(&s_driver_data)) {
while (!received_response(&s_driver_data)) {
mg_mgr_poll(&mgr, 0);
now = mg_millis() - start;
if (now > 2 * MIP_TCP_ACK_MS)
ASSERT(0); // response should have been received by now
ASSERT(0); // response should have been received by now
}
t = (struct tcp *) (s_driver_data.buf + sizeof(struct eth) +
sizeof(struct ip));
ASSERT((t->flags == TH_ACK));
ASSERT((t->ack == mg_htonl(1002))); // dup ACK
ASSERT((t->ack == mg_htonl(1003))); // dup ACK

// retransmitting packet with seq_no = 1002
create_tcp_pkt(&e, &ip, 1002, 1, TH_PUSH | TH_ACK, 1);
// retransmitting packet with seq_no = 1003
create_tcp_pkt(&e, &ip, 1003, 1, TH_PUSH | TH_ACK, 2);
mg_mgr_poll(&mgr, 0);
while(!received_response(&s_driver_data)) mg_mgr_poll(&mgr, 0);
while (!received_response(&s_driver_data)) mg_mgr_poll(&mgr, 0);
t = (struct tcp *) (s_driver_data.buf + sizeof(struct eth) +
sizeof(struct ip));
ASSERT((t->flags == TH_ACK));
ASSERT((t->ack == mg_htonl(1003))); // OK
ASSERT((t->ack == mg_htonl(1005))); // OK

s_driver_data.len = 0;
mg_mgr_free(&mgr);
Expand All @@ -252,7 +259,8 @@ static void test_frag_recv_path(void) {
mg_mgr_init(&mgr);
memset(&mif, 0, sizeof(mif));
memset(&s_driver_data, 0, sizeof(struct driver_data));
driver.init = NULL, driver.tx = if_tx, driver.poll = if_poll, driver.rx = if_rx;
driver.init = NULL, driver.tx = if_tx, driver.poll = if_poll,
driver.rx = if_rx;
mif.driver = &driver;
mif.driver_data = &s_driver_data;
mg_tcpip_init(&mgr, &mif);
Expand All @@ -273,9 +281,8 @@ static void test_frag_recv_path(void) {
init_tcp_handshake(&e, &ip, t, &mgr);

// send fragmented TCP packet
ip.len =
mg_htons(sizeof(struct ip) + sizeof(struct tcp) + 1000);
ip.frag |= IP_MORE_FRAGS_MSK; // setting More Fragments bit to 1
ip.len = mg_htons(sizeof(struct ip) + sizeof(struct tcp) + 1000);
ip.frag |= IP_MORE_FRAGS_MSK; // setting More Fragments bit to 1
create_tcp_pkt(&e, &ip, 1001, 1, TH_PUSH | TH_ACK, 1000);
s_sent_fragment = true;
mg_mgr_poll(&mgr, 0);
Expand All @@ -292,15 +299,15 @@ static void test_frag_send_path(void) {
mg_mgr_init(&mgr);
memset(&mif, 0, sizeof(mif));
memset(&s_driver_data, 0, sizeof(struct driver_data));
driver.init = NULL, driver.tx = if_tx, driver.poll = if_poll, driver.rx = if_rx;
driver.init = NULL, driver.tx = if_tx, driver.poll = if_poll,
driver.rx = if_rx;
mif.driver = &driver;
mif.driver_data = &s_driver_data;
mg_tcpip_init(&mgr, &mif);
mif.mtu = 500;
mif.mtu = 500; // force ad hoc small MTU to fragment IP
mg_http_listen(&mgr, "http://0.0.0.0:0", frag_send_fn, NULL);
mgr.conns->pfn = NULL;
for (int i = 0; i < 10; i++)
mg_mgr_poll(&mgr, 0);
for (int i = 0; i < 10; i++) mg_mgr_poll(&mgr, 0);
ASSERT(s_seg_sent == 3);
s_driver_data.len = 0;
mg_mgr_free(&mgr);
Expand Down
2 changes: 1 addition & 1 deletion test/tls_multirec/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CFLAGS = -W -Wall -Wextra -g -I. # Build options
CFLAGS = -g -I. # This is a known version, no need to check
CFLAGS_MONGOOSE += -DMG_ENABLE_LINES=1 -DMG_ENABLE_IPV6=1
CFLAGS_EXTRA ?= -DMG_TLS=MG_TLS_BUILTIN

Expand Down
2 changes: 1 addition & 1 deletion test/tls_multirec/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ static void cb(struct mg_connection *c, int ev, void *ev_data) {
mg_tls_init(c, &opts);
}
if (ev == MG_EV_HTTP_MSG) {
struct mg_http_message *hm = ev_data;
struct mg_http_message *hm = (struct mg_http_message *) ev_data;

if (mg_match(hm->uri, mg_str("/upload"), NULL)) {
// Serve file upload
Expand Down
4 changes: 3 additions & 1 deletion test/unit_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -1418,7 +1418,7 @@ static void test_tls(void) {
#if MG_TLS == MG_TLS_BUILTIN && defined(__linux__)
// fire patched server, test multiple TLS records per TCP segment handling
// skip other TLS stacks to avoid "bad client hello", we are 1.3 only
{
if (access("tls_multirec/server", X_OK) == 0) {
ASSERT(system("tls_multirec/server -d tls_multirec &") == 0);
sleep(1);
// fetch() needs to loop enough times in order to process all TLS records;
Expand All @@ -1427,6 +1427,8 @@ static void test_tls(void) {
"GET /thefile HTTP/1.0\n\n") == 200);
ASSERT(cmpbody(buf, data.buf) == 0); // "thefile" links to Makefile
ASSERT(system("killall tls_multirec/server") == 0);
} else {
MG_ERROR(("SKIPPED TLS MULTIPLE RECORDS TEST, tls_multirec/server NOT PRESENT"));
}
#else
printf(
Expand Down
Loading
Loading