improve MIP tests

This commit is contained in:
Sergio R. Caprile 2024-07-22 17:14:10 -03:00
parent 22b52f831b
commit 91606a9e3f
6 changed files with 165 additions and 105 deletions

View File

@ -13,7 +13,7 @@ jobs:
fail-fast: false fail-fast: false
matrix: matrix:
cc: [gcc, clang, g++, clang++] cc: [gcc, clang, g++, clang++]
target: [test, mip_test] target: [test]
ssl: ["", BUILTIN, MBEDTLS, OPENSSL, WOLFSSL] ssl: ["", BUILTIN, MBEDTLS, OPENSSL, WOLFSSL]
select: ["-DMG_ENABLE_POLL=0 -DMG_ENABLE_EPOLL=0", "-DMG_ENABLE_POLL=1 -DMG_ENABLE_EPOLL=0", "-DMG_ENABLE_POLL=0 -DMG_ENABLE_EPOLL=1"] select: ["-DMG_ENABLE_POLL=0 -DMG_ENABLE_EPOLL=0", "-DMG_ENABLE_POLL=1 -DMG_ENABLE_EPOLL=0", "-DMG_ENABLE_POLL=0 -DMG_ENABLE_EPOLL=1"]
exclude: exclude:
@ -37,7 +37,23 @@ jobs:
steps: steps:
- uses: actions/checkout@v4 - uses: actions/checkout@v4
with: { fetch-depth: 2 } with: { fetch-depth: 2 }
- run: ./test/setup_ga_network.sh && sudo apt -y update ; sudo apt -y install libmbedtls-dev libwolfssl-dev && make -C test ${{ matrix.target }} - run: sudo apt -y update ; sudo apt -y install libmbedtls-dev libwolfssl-dev && make -C test ${{ matrix.target }}
mip:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
cc: [gcc, clang, g++, clang++]
target: [mip_test, mip_tap_test]
ssl: ["", BUILTIN, MBEDTLS, OPENSSL, WOLFSSL]
name: ${{ matrix.target }} CC=${{ matrix.cc }} SSL=${{ matrix.ssl }}
env:
CC: ${{ matrix.cc }}
SSL: ${{ matrix.ssl }}
steps:
- uses: actions/checkout@v4
with: { fetch-depth: 2 }
- run: if [ "${{ matrix.target }}" == "mip_tap_test" ]; then ./test/setup_ga_network.sh ; fi && sudo apt -y update ; sudo apt -y install libmbedtls-dev libwolfssl-dev && make -C test ${{ matrix.target }} IPV6=0
s390: s390:
runs-on: ubuntu-latest runs-on: ubuntu-latest
strategy: strategy:

View File

@ -25,7 +25,7 @@ jobs:
with: { fetch-depth: 2 } with: { fetch-depth: 2 }
- run: if ./test/match_changed_files.sh '^test|^src/.*.[ch]' ; then echo GO=1 >> $GITHUB_ENV ; fi - run: if ./test/match_changed_files.sh '^test|^src/.*.[ch]' ; then echo GO=1 >> $GITHUB_ENV ; fi
- if: ${{ env.GO == 1 }} - if: ${{ env.GO == 1 }}
run: ./test/setup_ga_network.sh && make -C test ${{ matrix.target }} run: make -C test ${{ matrix.target }}
s390: s390:
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:

View File

@ -85,12 +85,12 @@ all:
tall: mg_prefix unamalgamated test mip_test arm examples vc98 vc17 vc22 mingw mingw++ fuzz tall: mg_prefix unamalgamated test mip_test arm examples vc98 vc17 vc22 mingw mingw++ fuzz
mip_test: mip_test.c mongoose.c mongoose.h Makefile mip_tap_test mip_test: mip_test.c mongoose.c mongoose.h packed_fs.c Makefile
$(CC) mip_test.c $(INCS) $(WARN) $(OPTS) $(C_WARN) $(ASAN) -o $@ $(CC) mip_test.c packed_fs.c $(CFLAGS) $(LDFLAGS) -o $@
ASAN_OPTIONS=$(ASAN_OPTIONS) $(RUN) ./$@ ASAN_OPTIONS=$(ASAN_OPTIONS) $(RUN) ./$@
mip_tap_test: mip_tap_test.c mongoose.c mongoose.h Makefile mip_tap_test: mip_tap_test.c mongoose.c mongoose.h packed_fs.c Makefile
$(CC) mip_tap_test.c $(INCS) $(WARN) $(OPTS) $(C_WARN) $(ASAN) -o $@ $(CC) mip_tap_test.c packed_fs.c $(CFLAGS) $(LDFLAGS) -o $@
ASAN_OPTIONS=$(ASAN_OPTIONS) $(RUN) ./$@ ASAN_OPTIONS=$(ASAN_OPTIONS) $(RUN) ./$@
packed_fs.c: Makefile data/ssi.h fuzz.c data/a.txt data/ca.pem certs/ca.crt certs/server.crt certs/server.key packed_fs.c: Makefile data/ssi.h fuzz.c data/a.txt data/ca.pem certs/ca.crt certs/server.crt certs/server.key

2
test/mip_curl.sh Executable file
View File

@ -0,0 +1,2 @@
curl -s $*/a.txt -o /tmp/a.txt && diff /tmp/a.txt data/a.txt && rm /tmp/a.txt &&
curl -s -H Expect: $*/body --data-binary @./Makefile -o /tmp/Makefile && diff /tmp/Makefile ./Makefile && rm /tmp/Makefile

View File

@ -1,9 +1,10 @@
#define MG_ENABLE_TCPIP 1 #define MG_ENABLE_TCPIP 1
#define MG_ENABLE_TCPIP_DRIVER_INIT 0 #define MG_ENABLE_TCPIP_DRIVER_INIT 0
#define MG_ENABLE_SOCKET 0
#define MG_USING_DHCP 1 #define MIPTAPTEST_USING_DHCP 1
#define MG_ENABLE_PACKED_FS 0
#define MG_ENABLE_LINES 1 #define FETCH_BUF_SIZE (8 * 1024)
#include <sys/socket.h> #include <sys/socket.h>
#ifndef __OpenBSD__ #ifndef __OpenBSD__
@ -52,107 +53,110 @@ static bool tap_up(struct mg_tcpip_if *ifp) {
return ifp->driver_data ? true : false; return ifp->driver_data ? true : false;
} }
// HTTP fetches IOs
struct Post_reply { static void eh1(struct mg_connection *c, int ev, void *ev_data) {
char *post; // HTTP POST data struct mg_tls_opts *topts = (struct mg_tls_opts *) c->fn_data;
void *http_response; // Server response(s) if (ev == MG_EV_ACCEPT && topts != NULL) mg_tls_init(c, topts);
unsigned int http_responses_received; // Number responses received if (ev == MG_EV_HTTP_MSG) {
struct mg_http_message *hm = (struct mg_http_message *) ev_data;
MG_DEBUG(("[%.*s %.*s] message len %d", (int) hm->method.len, hm->method.buf,
(int) hm->uri.len, hm->uri.buf, (int) hm->message.len));
if (mg_match(hm->uri, mg_str("/foo/*"), NULL)) {
mg_http_reply(c, 200, "", "uri: %.*s", hm->uri.len - 5, hm->uri.buf + 5);
} else if (mg_match(hm->uri, mg_str("/ws"), NULL)) {
mg_ws_upgrade(c, hm, NULL);
} else if (mg_match(hm->uri, mg_str("/body"), NULL)) {
mg_http_reply(c, 200, "", "%.*s", (int) hm->body.len, hm->body.buf);
} else {
struct mg_http_serve_opts sopts;
memset(&sopts, 0, sizeof(sopts));
sopts.root_dir = "./data";
mg_http_serve_dir(c, hm, &sopts);
}
} else if (ev == MG_EV_WS_OPEN) {
struct mg_http_message *hm = (struct mg_http_message *) ev_data;
ASSERT(mg_strcmp(hm->uri, mg_str("/ws")) == 0);
mg_ws_send(c, "opened", 6, WEBSOCKET_OP_BINARY);
} else if (ev == MG_EV_WS_MSG) {
struct mg_ws_message *wm = (struct mg_ws_message *) ev_data;
mg_ws_send(c, wm->data.buf, wm->data.len, WEBSOCKET_OP_BINARY);
}
}
struct fetch_data {
char *buf;
int code, closed;
}; };
char *fetch(struct mg_mgr *mgr, const char *url, const char *post_data); static void fcb(struct mg_connection *c, int ev, void *ev_data) {
static void f_http_fetch_query(struct mg_connection *c, int ev, void *ev_data); struct fetch_data *fd = (struct fetch_data *) c->fn_data;
int get_response_code(char *); // Returns HTTP status code from full char* msg if (ev == MG_EV_HTTP_MSG) {
static void f_http_fetch_query(struct mg_connection *c, int ev, void *ev_data) {
static char *http_response = 0;
static bool http_response_allocated =
0; // So that we will update out parameter
unsigned int http_responses_received = 0;
struct Post_reply *post_reply_l;
post_reply_l = (struct Post_reply *) c->fn_data;
if (ev == MG_EV_CONNECT) {
mg_printf(c, post_reply_l->post);
} else if (ev == MG_EV_HTTP_MSG) {
struct mg_http_message *hm = (struct mg_http_message *) ev_data; struct mg_http_message *hm = (struct mg_http_message *) ev_data;
http_responses_received++; snprintf(fd->buf, FETCH_BUF_SIZE, "%.*s", (int) hm->message.len, hm->message.buf);
if (!http_response_allocated) { fd->code = atoi(hm->uri.buf);
http_response = mg_mprintf("%.*s", hm->message.len, hm->message.buf); fd->closed = 1;
http_response_allocated = 1; c->is_closing = 1;
} MG_DEBUG(("CODE: %d, MSG: %.*s", fd->code, (int) hm->message.len, hm->message.buf));
if (http_responses_received > 0) { (void) c;
post_reply_l->http_response = http_response; } else if (ev == MG_EV_CLOSE) {
post_reply_l->http_responses_received = http_responses_received; MG_DEBUG(("CLOSE"));
} fd->closed = 1;
} }
} }
// Fetch utility returns message from fetch(..., URL, POST) static int fetch(struct mg_mgr *mgr, char *buf, const char *url,
char *fetch(struct mg_mgr *mgr, const char *url, const char *fn_data) { const char *fmt, ...) {
struct Post_reply post_reply; struct fetch_data fd = {buf, 0, 0};
{ int i;
post_reply.post = (char *) fn_data; struct mg_connection *c = NULL;
post_reply.http_response = 0; va_list ap;
post_reply.http_responses_received = 0; c = mg_http_connect(mgr, url, fcb, &fd);
ASSERT(c != NULL);
if (c != NULL && mg_url_is_ssl(url)) {
struct mg_tls_opts opts;
memset(&opts, 0, sizeof(opts)); // read CA from packed_fs
opts.name = mg_url_host(url);
opts.ca = mg_unpacked("/data/ca.pem");
if (strstr(url, "localhost") != NULL) {
// Local connection, use self-signed certificates
opts.ca = mg_unpacked("/certs/ca.crt");
// opts.cert = mg_str(s_tls_cert);
// opts.key = mg_str(s_tls_key);
}
mg_tls_init(c, &opts);
} }
struct mg_connection *conn; va_start(ap, fmt);
conn = mg_http_connect(mgr, url, f_http_fetch_query, &post_reply); mg_vprintf(c, fmt, &ap);
ASSERT(conn != NULL); // Assertion on initialisation va_end(ap);
for (int i = 0; i < 500 && !post_reply.http_responses_received; i++) { buf[0] = '\0';
mg_mgr_poll(mgr, 100); for (i = 0; i < 500 && buf[0] == '\0' && !fd.closed; i++) {
usleep(10000); // 10 ms. Slow down poll loop to ensure packets transit
}
if (mgr->conns != 0) {
conn->is_closing = 1;
mg_mgr_poll(mgr, 0); mg_mgr_poll(mgr, 0);
usleep(10000); // 10 ms. Slow down poll loop to ensure packet transit
} }
if (!fd.closed) c->is_closing = 1;
mg_mgr_poll(mgr, 0); mg_mgr_poll(mgr, 0);
if (!post_reply.http_responses_received) { return fd.code;
return 0;
} else {
return (char *) post_reply.http_response;
}
} }
// Returns server's HTTP response code static void test_http_client(struct mg_mgr *mgr) {
int get_response_code(char *http_msg_raw) { char buf[FETCH_BUF_SIZE];
int http_status = 0; int rc = 0;
struct mg_http_message http_msg_parsed;
if (mg_http_parse(http_msg_raw, strlen(http_msg_raw), &http_msg_parsed)) {
http_status = mg_http_status(&http_msg_parsed);
} else {
printf("Error: mg_http_parse()\n");
ASSERT(http_status != 0); // Couldn't parse.
}
return http_status;
}
static void test_http_fetch(struct mg_mgr *mgr) {
char *http_feedback = (char *) "";
const bool ipv6 = 0; const bool ipv6 = 0;
#if MG_TLS
if (ipv6) { if (ipv6) {
http_feedback = fetch(mgr, "ipv6.google.com", rc = fetch(mgr, buf, "https://ipv6.google.com", "GET / HTTP/1.0\r\nHost: ipv6.google.com\r\n\r\n");
"GET/ HTTP/1.0\r\nHost: ipv6.google.com\r\n\r\n");
} else { } else {
http_feedback = rc = fetch(mgr, buf, "https://cesanta.com", "GET /robots.txt HTTP/1.0\r\nHost: cesanta.com\r\n\r\n");
fetch(mgr, "http://cesanta.com",
"GET //robots.txt HTTP/1.0\r\nHost: cesanta.com\r\n\r\n");
} }
ASSERT(rc == 200); // OK
ASSERT(http_feedback != NULL && #else
*http_feedback != '\0'); // HTTP response received ? if (ipv6) {
rc = fetch(mgr, buf, "http://ipv6.google.com", "GET / HTTP/1.0\r\nHost: ipv6.google.com\r\n\r\n");
int http_status = get_response_code(http_feedback); } else {
// printf("Server response HTTP status code: %d\n",http_status); rc = fetch(mgr, buf, "http://cesanta.com", "GET /robots.txt HTTP/1.0\r\nHost: cesanta.com\r\n\r\n");
ASSERT(http_status != 0);
ASSERT(http_status == 301); // OK: Permanently moved (HTTP->HTTPS redirect)
if (http_feedback) {
free(http_feedback);
http_feedback = 0;
} }
ASSERT(rc == 301); // OK: Permanently moved (HTTP->HTTPS redirect)
#endif
} }
static struct mg_connection *s_conn; static struct mg_connection *s_conn;
@ -174,12 +178,13 @@ static void mqtt_fn(struct mg_connection *c, int ev, void *ev_data) {
mg_mqtt_pub(c, &pub_opts); mg_mqtt_pub(c, &pub_opts);
} else if (ev == MG_EV_MQTT_MSG) { } else if (ev == MG_EV_MQTT_MSG) {
struct mg_mqtt_message *mm = (struct mg_mqtt_message *) ev_data; struct mg_mqtt_message *mm = (struct mg_mqtt_message *) ev_data;
if (mm->topic.len != strlen(s_topic) || strcmp(mm->topic.buf, s_topic)) MG_DEBUG(("TOPIC: %.*s, MSG: %.*s", (int) mm->topic.len, mm->topic.buf, (int) mm->data.len, mm->data.buf));
ASSERT(0); ASSERT(mm->topic.len == strlen(s_topic) && strcmp(mm->topic.buf, s_topic) == 0);
if (mm->data.len != 2 || strcmp(mm->data.buf, "hi")) ASSERT(0); ASSERT(mm->data.len == 2 && strcmp(mm->data.buf, "hi") == 0);
mg_mqtt_disconnect(c, NULL); mg_mqtt_disconnect(c, NULL);
*(bool *) c->fn_data = true; *(bool *) c->fn_data = true;
} else if (ev == MG_EV_CLOSE) { } else if (ev == MG_EV_CLOSE) {
MG_DEBUG(("CLOSE"));
s_conn = NULL; s_conn = NULL;
} }
} }
@ -200,6 +205,43 @@ static void test_mqtt_connsubpub(struct mg_mgr *mgr) {
mg_mgr_poll(mgr, 0); mg_mgr_poll(mgr, 0);
} }
#include <pthread.h>
static void *poll_thread(void *p) {
struct mg_mgr *mgr = (struct mg_mgr *) p;
int i;
for (i = 0; i < 500; i++) {
mg_mgr_poll(mgr, 0);
usleep(10000); // 10 ms. Slow down poll loop to ensure packet transit
}
return NULL;
}
static void test_http_server(struct mg_mgr *mgr, uint32_t ip) {
struct mg_connection *c;
char *cmd;
pthread_t thread_id = (pthread_t) 0;
#if MG_TLS
struct mg_tls_opts opts;
memset(&opts, 0, sizeof(opts));
// opts.ca = mg_str(s_tls_ca);
opts.cert = mg_unpacked("/certs/server.crt");
opts.key = mg_unpacked("/certs/server.key");
c = mg_http_listen(mgr, "https://0.0.0.0:12347", eh1, &opts);
cmd = mg_mprintf("./mip_curl.sh --insecure https://%M:12347", mg_print_ip4, &ip);
#else
c = mg_http_listen(mgr, "http://0.0.0.0:12347", eh1, NULL);
cmd = mg_mprintf("./mip_curl.sh http://%M:12347", mg_print_ip4, &ip);
#endif
ASSERT(c != NULL);
pthread_create(&thread_id, NULL, poll_thread, mgr); // simpler this way, no concurrency anyway
MG_DEBUG(("CURL"));
ASSERT(system(cmd) == 0); // wait for curl
MG_DEBUG(("MONGOOSE"));
pthread_join(thread_id, NULL); // wait for Mongoose
MG_DEBUG(("DONE"));
free(cmd);
}
int main(void) { int main(void) {
// Setup interface // Setup interface
const char *iface = "tap0"; // Network iface const char *iface = "tap0"; // Network iface
@ -232,6 +274,7 @@ int main(void) {
// Events // Events
struct mg_mgr mgr; // Event manager struct mg_mgr mgr; // Event manager
mg_log_set(MG_LL_DEBUG);
mg_mgr_init(&mgr); // Initialise event manager mg_mgr_init(&mgr); // Initialise event manager
// MIP driver // MIP driver
@ -248,7 +291,7 @@ int main(void) {
mif.driver = &driver; mif.driver = &driver;
mif.driver_data = &fd; mif.driver_data = &fd;
#if MG_USING_DHCP == 1 #if MIPTAPTEST_USING_DHCP == 1
#else #else
mif.ip = mg_htonl(MG_U32(192, 168, 32, 2)); // Triggering a network failure mif.ip = mg_htonl(MG_U32(192, 168, 32, 2)); // Triggering a network failure
mif.mask = mg_htonl(MG_U32(255, 255, 255, 0)); mif.mask = mg_htonl(MG_U32(255, 255, 255, 0));
@ -262,14 +305,14 @@ int main(void) {
MG_INFO(("Init done, starting main loop")); MG_INFO(("Init done, starting main loop"));
// Stack initialization, Network configuration (DHCP lease, ...) // Stack initialization, Network configuration (DHCP lease, ...)
#if MG_USING_DHCP == 0 #if MIPTAPTEST_USING_DHCP == 0
MG_INFO(("MIF configuration: Static IP")); MG_INFO(("MIF configuration: Static IP"));
ASSERT(mif.ip != 0); // Check we have a satic IP assigned ASSERT(mif.ip != 0); // Check we have a satic IP assigned
mg_mgr_poll(&mgr, 100); // For initialisation mg_mgr_poll(&mgr, 100); // For initialisation
#else #else
MG_INFO(("MIF configuration: DHCP")); MG_INFO(("MIF configuration: DHCP"));
ASSERT(!mif.ip); // Check we are set for DHCP ASSERT(!mif.ip); // Check we are set for DHCP
int pc = 500; // Timout on DHCP lease 500 ~ approx 5s (typical delay <1s) int pc = 500; // Timeout on DHCP lease 500 ~ approx 5s (typical delay <1s)
while (((pc--) > 0) && !mif.ip) { while (((pc--) > 0) && !mif.ip) {
mg_mgr_poll(&mgr, 100); mg_mgr_poll(&mgr, 100);
usleep(10000); // 10 ms usleep(10000); // 10 ms
@ -279,8 +322,10 @@ int main(void) {
#endif #endif
// RUN TESTS // RUN TESTS
test_http_fetch(&mgr); test_http_server(&mgr, mif.ip);
test_http_client(&mgr);
test_mqtt_connsubpub(&mgr); test_mqtt_connsubpub(&mgr);
printf("SUCCESS. Total tests: %d\n", s_num_tests); printf("SUCCESS. Total tests: %d\n", s_num_tests);
// Clear // Clear

View File

@ -1,8 +1,5 @@
#define MG_ENABLE_SOCKET 0
#define MG_ENABLE_LINES 1
#define MG_ENABLE_TCPIP 1 #define MG_ENABLE_TCPIP 1
#define MG_ENABLE_TCPIP_DRIVER_INIT 0 #define MG_ENABLE_TCPIP_DRIVER_INIT 0
#define MG_ENABLE_PACKED_FS 0
#include "mongoose.c" #include "mongoose.c"
#include "driver_mock.c" #include "driver_mock.c"