* Extract upload http 200 reply serialization into mongoose_upload_ok_reply
* Add test_upload_ok_reply_format
* Fix mongoose_upload_ok_reply http 200 reply serialization
Signed-off-by: David Alonso de la Torre <
david...@gmail.com>
---
include/mongoose_interface.h | 7 ++++
mongoose/mongoose_interface.c | 17 +++++---
test/Makefile | 1 +
test/test_mongoose_upload.c | 79 +++++++++++++++++++++++++++++++++++
4 files changed, 98 insertions(+), 6 deletions(-)
create mode 100644 test/test_mongoose_upload.c
diff --git a/include/mongoose_interface.h b/include/mongoose_interface.h
index 3ed7b4fb..376063a8 100644
--- a/include/mongoose_interface.h
+++ b/include/mongoose_interface.h
@@ -7,6 +7,11 @@
#pragma once
+#include <stddef.h>
+
+struct mg_connection;
+struct mg_str;
+
/*
* Max number of command line options
* to be passed to the mongoose webserver
@@ -18,3 +23,5 @@
int start_mongoose(const char *cfgfname, int argc, char *argv[]);
void mongoose_print_help(void);
+void mongoose_upload_ok_reply(struct mg_connection *nc,
+ const struct mg_str *filename, size_t len);
diff --git a/mongoose/mongoose_interface.c b/mongoose/mongoose_interface.c
index 729862e6..093dc292 100644
--- a/mongoose/mongoose_interface.c
+++ b/mongoose/mongoose_interface.c
@@ -550,6 +550,16 @@ static void timer_ev_handler(void *fn_data)
/*
* Code common to V1 and V2
*/
+void mongoose_upload_ok_reply(struct mg_connection *nc,
+ const struct mg_str *filename, size_t len)
+{
+ mg_http_reply(nc, 200,
+ "Content-Type: text/plain\r\n"
+ "Connection: close\r\n",
+ "Ok, %s - %d bytes.\r\n", filename->buf, (int)len);
+ nc->is_draining = 1;
+}
+
static void upload_handler(struct mg_connection *nc, int ev, void *ev_data)
{
struct mg_http_multipart *mp;
@@ -655,12 +665,7 @@ static void upload_handler(struct mg_connection *nc, int ev, void *ev_data)
ipc_end(fus->fd);
- mg_http_reply(nc, 200, "%s",
- "Content-Type: text/plain\r\n"
- "Connection: close");
- mg_send(nc, "\r\n", 2);
- mg_printf(nc, "Ok, %s - %d bytes.\r\n", mp->part.filename, (int) fus->len);
- nc->is_draining = 1;
+ mongoose_upload_ok_reply(nc, &mp->part.filename, fus->len);
mp->user_data = NULL;
mg_timer_free(&fus->c->mgr->timers, fus->timer);
diff --git a/test/Makefile b/test/Makefile
index 385efd85..578a3878 100644
--- a/test/Makefile
+++ b/test/Makefile
@@ -26,6 +26,7 @@ tests-$(CONFIG_SIGNED_IMAGES) += test_verify
endif
tests-$(CONFIG_SURICATTA_HAWKBIT) += test_json
tests-$(CONFIG_SURICATTA_HAWKBIT) += test_server_hawkbit
+tests-$(CONFIG_MONGOOSE) += test_mongoose_upload
tests-y += test_util
tests-$(CONFIG_CFI) += test_flash_handler
diff --git a/test/test_mongoose_upload.c b/test/test_mongoose_upload.c
new file mode 100644
index 00000000..4385a4b7
--- /dev/null
+++ b/test/test_mongoose_upload.c
@@ -0,0 +1,79 @@
+/*
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#include <stdarg.h>
+#include <stddef.h>
+#include <setjmp.h>
+#include <cmocka.h>
+
+#include <stdlib.h>
+#include <string.h>
+
+#include "mongoose/mongoose.h"
+
+void mongoose_upload_ok_reply(struct mg_connection *nc,
+ const struct mg_str *filename, size_t len);
+
+static void test_upload_ok_reply_format(void **state)
+{
+ (void)state;
+
+ struct mg_connection nc;
+ const struct mg_str filename = mg_str("test.swu");
+ const char *expected_body = "Ok, test.swu - 20 bytes.\r\n";
+ const size_t filesize = 20;
+ char *buf;
+ char *body;
+ const char *line;
+ char *line_end;
+ int line_idx = 0;
+
+ memset(&nc, 0, sizeof(nc));
+
+ mongoose_upload_ok_reply(&nc, &filename, filesize);
+
+ // copy the data to a temporary buffer, to use null-terminated string functions
+ assert_non_null(nc.send.buf);
+ buf = malloc(nc.send.len + 1);
+ assert_non_null(buf);
+ memcpy(buf, nc.send.buf, nc.send.len);
+ buf[nc.send.len] = '\0';
+
+ line = buf;
+ while (true) {
+ line_end = strstr(line, "\r\n");
+ assert_non_null(line_end);
+ if (line_end == line) {
+ body = line_end + 2;
+ break;
+ }
+ *line_end = '\0';
+ if (line_idx == 0)
+ assert_string_equal(line, "HTTP/1.1 200 OK");
+ else if (line_idx == 1)
+ assert_string_equal(line, "Content-Type: text/plain");
+ else if (line_idx == 2)
+ assert_string_equal(line, "Connection: close");
+ else if (line_idx == 3)
+ assert_string_equal(line, "Content-Length: 26 ");
+ else
+ fail();
+ line_idx++;
+ line = line_end + 2;
+ }
+ assert_int_equal(line_idx, 4);
+ assert_string_equal(body, expected_body);
+
+ free(buf);
+ mg_iobuf_free(&nc.send);
+}
+
+int main(void)
+{
+ const struct CMUnitTest tests[] = {
+ cmocka_unit_test(test_upload_ok_reply_format),
+ };
+
+ return cmocka_run_group_tests(tests, NULL, NULL);
+}
--
2.43.0