mongoose/test/mip_test.c

57 lines
1.6 KiB
C
Raw Normal View History

2022-09-19 20:28:07 +08:00
#define MG_ENABLE_SOCKET 0
2022-09-23 04:05:30 +08:00
#define MG_ENABLE_LINES 1
#define MG_ENABLE_TCPIP 1
2022-09-19 20:28:07 +08:00
#define MG_ENABLE_PACKED_FS 0
2022-09-03 20:36:08 +08:00
#include "mongoose.c"
#include "driver_mock.c"
static int s_num_tests = 0;
#define ASSERT(expr) \
do { \
s_num_tests++; \
if (!(expr)) { \
printf("FAILURE %s:%d: %s\n", __FILE__, __LINE__, #expr); \
abort(); \
} \
} while (0)
2022-09-19 20:28:07 +08:00
static void test_statechange(void) {
2022-11-09 21:11:22 +08:00
char tx[1540];
struct mg_tcpip_if iface;
2022-09-19 20:28:07 +08:00
memset(&iface, 0, sizeof(iface));
iface.ip = mg_htonl(0x01020304);
2023-03-09 22:25:02 +08:00
iface.state = MG_TCPIP_STATE_READY;
2022-11-09 21:11:22 +08:00
iface.tx.ptr = tx, iface.tx.len = sizeof(tx);
iface.driver = &mg_tcpip_driver_mock;
2022-09-19 20:28:07 +08:00
onstatechange(&iface);
}
2022-11-30 22:47:37 +08:00
static void ph(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
if (ev == MG_EV_POLL) ++(*(int *) fn_data);
(void) c, (void) ev_data;
}
static void test_poll(void) {
2022-12-01 04:37:32 +08:00
int count = 0, i;
2022-11-30 22:47:37 +08:00
struct mg_mgr mgr;
mg_mgr_init(&mgr);
struct mg_tcpip_if mif;
2022-12-01 04:37:32 +08:00
memset(&mif, 0, sizeof(mif));
mif.driver = &mg_tcpip_driver_mock;
mg_tcpip_init(&mgr, &mif);
2022-11-30 22:47:37 +08:00
mg_http_listen(&mgr, "http://127.0.0.1:12346", ph, &count);
2022-12-01 04:37:32 +08:00
for (i = 0; i < 10; i++) mg_mgr_poll(&mgr, 0);
2022-11-30 22:47:37 +08:00
ASSERT(count == 10);
mg_tcpip_free(&mif);
2022-11-30 22:47:37 +08:00
mg_mgr_free(&mgr);
}
2022-09-03 20:36:08 +08:00
int main(void) {
2022-09-19 20:28:07 +08:00
test_statechange();
2022-11-30 22:47:37 +08:00
test_poll();
2022-11-29 18:20:22 +08:00
printf("SUCCESS. Total tests: %d\n", s_num_tests);
2022-09-03 20:36:08 +08:00
return 0;
}