mongoose/test/mip_test.c

57 lines
1.6 KiB
C
Raw Normal View History

2022-09-19 13:28:07 +01:00
#define MG_ENABLE_SOCKET 0
2022-09-22 21:05:30 +01:00
#define MG_ENABLE_LINES 1
#define MG_ENABLE_TCPIP 1
2022-09-19 13:28:07 +01:00
#define MG_ENABLE_PACKED_FS 0
2022-09-03 13:36:08 +01: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 13:28:07 +01:00
static void test_statechange(void) {
2022-11-09 13:11:22 +00:00
char tx[1540];
struct mg_tcpip_if iface;
2022-09-19 13:28:07 +01:00
memset(&iface, 0, sizeof(iface));
iface.ip = mg_htonl(0x01020304);
iface.state = MIP_STATE_READY;
2022-11-09 13:11:22 +00:00
iface.tx.ptr = tx, iface.tx.len = sizeof(tx);
iface.driver = &mg_tcpip_driver_mock;
2022-09-19 13:28:07 +01:00
onstatechange(&iface);
}
2022-11-30 11:47:37 -03: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-11-30 17:37:32 -03:00
int count = 0, i;
2022-11-30 11:47:37 -03:00
struct mg_mgr mgr;
mg_mgr_init(&mgr);
struct mg_tcpip_if mif;
2022-11-30 17:37:32 -03:00
memset(&mif, 0, sizeof(mif));
mif.driver = &mg_tcpip_driver_mock;
mg_tcpip_init(&mgr, &mif);
2022-11-30 11:47:37 -03:00
mg_http_listen(&mgr, "http://127.0.0.1:12346", ph, &count);
2022-11-30 17:37:32 -03:00
for (i = 0; i < 10; i++) mg_mgr_poll(&mgr, 0);
2022-11-30 11:47:37 -03:00
ASSERT(count == 10);
mg_tcpip_free(&mif);
2022-11-30 11:47:37 -03:00
mg_mgr_free(&mgr);
}
2022-09-03 13:36:08 +01:00
int main(void) {
2022-09-19 13:28:07 +01:00
test_statechange();
2022-11-30 11:47:37 -03:00
test_poll();
2022-11-29 11:20:22 +01:00
printf("SUCCESS. Total tests: %d\n", s_num_tests);
2022-09-03 13:36:08 +01:00
return 0;
}