2022-09-19 20:28:07 +08:00
|
|
|
#define MG_ENABLE_SOCKET 0
|
|
|
|
#define MG_ENABLE_LOG 0
|
|
|
|
#define MG_ENABLE_LINES 1
|
2023-02-08 05:16:42 +08:00
|
|
|
#define MG_ENABLE_TCPIP 1
|
2023-09-27 02:59:42 +08:00
|
|
|
#define MG_IO_SIZE (32 * 1024 * 1024) // Big IO size for fast resizes
|
2022-09-19 20:28:07 +08:00
|
|
|
|
|
|
|
#include "mongoose.c"
|
|
|
|
|
|
|
|
#include "driver_mock.c"
|
2019-07-12 20:28:20 +08:00
|
|
|
|
2020-12-14 00:56:30 +08:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *, size_t);
|
2021-10-23 02:41:26 +08:00
|
|
|
#else
|
|
|
|
int LLVMFuzzerTestOneInput(const uint8_t *, size_t);
|
2020-12-14 00:56:30 +08:00
|
|
|
#endif
|
2020-12-05 19:26:32 +08:00
|
|
|
|
2024-01-09 04:34:34 +08:00
|
|
|
static void fn(struct mg_connection *c, int ev, void *ev_data) {
|
2023-08-15 22:09:48 +08:00
|
|
|
struct mg_http_serve_opts opts = {.root_dir = "."};
|
|
|
|
if (ev == MG_EV_HTTP_MSG) {
|
|
|
|
mg_http_serve_dir(c, (struct mg_http_message *) ev_data, &opts);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-12 20:28:20 +08:00
|
|
|
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
|
2023-08-15 18:52:22 +08:00
|
|
|
mg_log_set(MG_LL_INFO);
|
2022-05-07 04:09:13 +08:00
|
|
|
|
2020-12-05 19:26:32 +08:00
|
|
|
struct mg_dns_message dm;
|
|
|
|
mg_dns_parse(data, size, &dm);
|
2020-12-11 21:16:51 +08:00
|
|
|
mg_dns_parse(NULL, 0, &dm);
|
2020-12-05 19:26:32 +08:00
|
|
|
|
|
|
|
struct mg_http_message hm;
|
2023-06-30 05:37:10 +08:00
|
|
|
if (mg_http_parse((const char *) data, size, &hm) > 0) {
|
|
|
|
mg_crc32(0, hm.method.ptr, hm.method.len);
|
|
|
|
mg_crc32(0, hm.uri.ptr, hm.uri.len);
|
|
|
|
mg_crc32(0, hm.uri.ptr, hm.uri.len);
|
|
|
|
for (size_t i = 0; i < sizeof(hm.headers) / sizeof(hm.headers[0]); i++) {
|
|
|
|
struct mg_str *k = &hm.headers[i].name, *v = &hm.headers[i].value;
|
|
|
|
mg_crc32(0, k->ptr, k->len);
|
|
|
|
mg_crc32(0, v->ptr, v->len);
|
|
|
|
}
|
2023-06-29 23:34:15 +08:00
|
|
|
}
|
2020-12-11 21:16:51 +08:00
|
|
|
mg_http_parse(NULL, 0, &hm);
|
2020-12-05 19:26:32 +08:00
|
|
|
|
2020-12-08 02:52:40 +08:00
|
|
|
struct mg_str body = mg_str_n((const char *) data, size);
|
|
|
|
char tmp[256];
|
|
|
|
mg_http_get_var(&body, "key", tmp, sizeof(tmp));
|
2020-12-14 00:33:46 +08:00
|
|
|
mg_http_get_var(&body, "key", NULL, 0);
|
2020-12-11 17:35:50 +08:00
|
|
|
mg_url_decode((char *) data, size, tmp, sizeof(tmp), 1);
|
|
|
|
mg_url_decode((char *) data, size, tmp, 1, 1);
|
2020-12-11 21:16:51 +08:00
|
|
|
mg_url_decode(NULL, 0, tmp, 1, 1);
|
2020-12-08 02:52:40 +08:00
|
|
|
|
2020-12-05 19:26:32 +08:00
|
|
|
struct mg_mqtt_message mm;
|
2023-06-30 05:37:10 +08:00
|
|
|
if (mg_mqtt_parse(data, size, 0, &mm) == MQTT_OK) {
|
|
|
|
mg_crc32(0, mm.topic.ptr, mm.topic.len);
|
|
|
|
mg_crc32(0, mm.data.ptr, mm.data.len);
|
|
|
|
mg_crc32(0, mm.dgram.ptr, mm.dgram.len);
|
|
|
|
}
|
2022-06-28 18:31:13 +08:00
|
|
|
mg_mqtt_parse(NULL, 0, 0, &mm);
|
2023-06-30 05:37:10 +08:00
|
|
|
if (mg_mqtt_parse(data, size, 5, &mm) == MQTT_OK) {
|
|
|
|
mg_crc32(0, mm.topic.ptr, mm.topic.len);
|
|
|
|
mg_crc32(0, mm.data.ptr, mm.data.len);
|
|
|
|
mg_crc32(0, mm.dgram.ptr, mm.dgram.len);
|
|
|
|
}
|
2022-06-28 18:31:13 +08:00
|
|
|
mg_mqtt_parse(NULL, 0, 5, &mm);
|
2020-12-05 19:26:32 +08:00
|
|
|
|
2021-12-22 05:50:18 +08:00
|
|
|
mg_sntp_parse(data, size);
|
|
|
|
mg_sntp_parse(NULL, 0);
|
2020-12-05 19:26:32 +08:00
|
|
|
|
2020-12-08 02:52:40 +08:00
|
|
|
char buf[size * 4 / 3 + 5]; // At least 4 chars and nul termination
|
2023-08-22 18:50:19 +08:00
|
|
|
mg_base64_decode((char *) data, size, buf, sizeof(buf));
|
|
|
|
mg_base64_decode(NULL, 0, buf, sizeof(buf));
|
|
|
|
mg_base64_encode(data, size, buf, sizeof(buf));
|
|
|
|
mg_base64_encode(NULL, 0, buf, sizeof(buf));
|
2020-12-08 02:52:40 +08:00
|
|
|
|
2020-12-24 17:07:55 +08:00
|
|
|
mg_globmatch((char *) data, size, (char *) data, size);
|
|
|
|
|
|
|
|
struct mg_str k, v, s = mg_str_n((char *) data, size);
|
2021-08-28 13:54:56 +08:00
|
|
|
while (mg_commalist(&s, &k, &v)) k.len = v.len = 0;
|
2020-12-24 17:07:55 +08:00
|
|
|
|
2022-06-09 19:39:48 +08:00
|
|
|
int n;
|
2022-07-30 14:55:26 +08:00
|
|
|
mg_json_get(mg_str_n((char *) data, size), "$", &n);
|
2022-09-19 20:28:07 +08:00
|
|
|
mg_json_get(mg_str_n((char *) data, size), "$.a.b", &n);
|
|
|
|
mg_json_get(mg_str_n((char *) data, size), "$[0]", &n);
|
|
|
|
|
2022-09-25 17:19:17 +08:00
|
|
|
if (size > 0) {
|
2023-02-08 05:16:42 +08:00
|
|
|
struct mg_tcpip_if mif = {.ip = 0x01020304,
|
2023-06-30 05:37:10 +08:00
|
|
|
.mask = 255,
|
|
|
|
.gw = 0x01010101,
|
|
|
|
.driver = &mg_tcpip_driver_mock};
|
2022-09-25 17:19:17 +08:00
|
|
|
struct mg_mgr mgr;
|
|
|
|
mg_mgr_init(&mgr);
|
2023-02-08 05:16:42 +08:00
|
|
|
mg_tcpip_init(&mgr, &mif);
|
2022-09-25 17:19:17 +08:00
|
|
|
|
|
|
|
// Make a copy of the random data, in order to modify it
|
2022-10-18 22:21:59 +08:00
|
|
|
void *pkt = malloc(size);
|
2022-09-25 17:19:17 +08:00
|
|
|
struct eth *eth = (struct eth *) pkt;
|
|
|
|
memcpy(pkt, data, size);
|
|
|
|
if (size > sizeof(*eth)) {
|
2022-09-25 18:58:28 +08:00
|
|
|
static size_t i;
|
|
|
|
uint16_t eth_types[] = {0x800, 0x800, 0x806, 0x86dd};
|
2022-11-09 21:11:22 +08:00
|
|
|
memcpy(eth->dst, mif.mac, 6); // Set valid destination MAC
|
2022-09-25 18:58:28 +08:00
|
|
|
eth->type = mg_htons(eth_types[i++]);
|
|
|
|
if (i >= sizeof(eth_types) / sizeof(eth_types[0])) i = 0;
|
2022-09-25 17:19:17 +08:00
|
|
|
}
|
|
|
|
|
2023-02-08 05:16:42 +08:00
|
|
|
mg_tcpip_rx(&mif, pkt, size);
|
2023-08-15 22:09:48 +08:00
|
|
|
|
|
|
|
// Test HTTP serving
|
|
|
|
const char *url = "http://localhost:12345";
|
|
|
|
struct mg_connection *c = mg_http_connect(&mgr, url, fn, NULL);
|
|
|
|
mg_iobuf_add(&c->recv, 0, data, size);
|
2024-01-09 04:34:34 +08:00
|
|
|
c->pfn(c, MG_EV_READ, NULL); // manually invoke protocol event handler
|
2023-08-15 22:09:48 +08:00
|
|
|
|
2022-09-25 17:19:17 +08:00
|
|
|
mg_mgr_free(&mgr);
|
2022-10-15 19:54:56 +08:00
|
|
|
free(pkt);
|
2023-02-19 17:48:11 +08:00
|
|
|
mg_tcpip_free(&mif);
|
2022-09-25 17:19:17 +08:00
|
|
|
}
|
2022-06-09 19:39:48 +08:00
|
|
|
|
2019-07-12 20:28:20 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2022-09-29 23:53:11 +08:00
|
|
|
|
|
|
|
#if defined(MAIN)
|
|
|
|
int main(int argc, char *argv[]) {
|
2022-09-30 18:44:50 +08:00
|
|
|
int res = EXIT_FAILURE;
|
2022-09-29 23:53:11 +08:00
|
|
|
if (argc > 1) {
|
|
|
|
size_t len = 0;
|
|
|
|
char *buf = mg_file_read(&mg_fs_posix, argv[1], &len);
|
2022-09-30 18:44:50 +08:00
|
|
|
if (buf != NULL) {
|
|
|
|
LLVMFuzzerTestOneInput((uint8_t *) buf, len);
|
|
|
|
res = EXIT_SUCCESS;
|
|
|
|
}
|
2022-09-29 23:53:11 +08:00
|
|
|
free(buf);
|
|
|
|
}
|
2022-09-30 18:44:50 +08:00
|
|
|
return res;
|
2022-09-29 23:53:11 +08:00
|
|
|
}
|
|
|
|
#endif
|