From 7ea3293aba58c17e6d5802bbc2e17e6c50c718b0 Mon Sep 17 00:00:00 2001 From: cpq Date: Mon, 7 Dec 2020 08:54:58 +0000 Subject: [PATCH] More tests --- Makefile | 4 ++-- mongoose.c | 5 +++-- src/util.c | 5 +++-- test/unit_test.c | 23 +++++++++++++++++++++++ 4 files changed, 31 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index ae63a210..59ce7c7e 100644 --- a/Makefile +++ b/Makefile @@ -12,7 +12,7 @@ CLANG ?= clang # /usr/local/opt/llvm\@9/bin/clang ASAN_OPTIONS ?= EXAMPLES := $(wildcard examples/*) EXAMPLE_TARGET ?= example -.PHONY: ex #$(EXAMPLES) +.PHONY: ex test ifeq "$(SSL)" "MBEDTLS" MBEDTLSDIR ?= $(shell "$(brew --cellar mbedtls)/$(brew info mbedtls --json | jq -j .[0].installed[0].version)") @@ -84,4 +84,4 @@ mongoose.h: $(HDRS) Makefile clean: EXAMPLE_TARGET = clean clean: ex - rm -rf $(PROG) *.o *.dSYM unit_test* ut fuzzer *.gcov *.gcno *.gcda *.obj *.exe *.ilk *.pdb slow-unit* _CL_* infer-out + rm -rf $(PROG) *.o *.dSYM unit_test* ut fuzzer *.gcov *.gcno *.gcda *.obj *.exe *.ilk *.pdb slow-unit* _CL_* infer-out data.txt diff --git a/mongoose.c b/mongoose.c index be8fa62f..cb2ce2a8 100644 --- a/mongoose.c +++ b/mongoose.c @@ -3503,7 +3503,8 @@ int mg_vasprintf(char **buf, size_t size, const char *fmt, va_list ap) { // eCos and Windows are not standard-compliant and return -1 when // the buffer is too small. Keep allocating larger buffers until we // succeed or out of memory. - *buf = NULL; // LCOV_EXCL_START + // LCOV_EXCL_START + *buf = NULL; while (len < 0) { free(*buf); if (size == 0) size = 5; @@ -3519,7 +3520,7 @@ int mg_vasprintf(char **buf, size_t size, const char *fmt, va_list ap) { // Microsoft version of vsnprintf() is not always null-terminated, so put // the terminator manually (*buf)[len] = 0; - /// LCOV_EXCL_STOP + // LCOV_EXCL_STOP } else if (len >= (int) size) { /// Standard-compliant code path. Allocate a buffer that is large enough if ((*buf = (char *) malloc(len + 1)) == NULL) { diff --git a/src/util.c b/src/util.c index 43f514b6..4cc22792 100644 --- a/src/util.c +++ b/src/util.c @@ -174,7 +174,8 @@ int mg_vasprintf(char **buf, size_t size, const char *fmt, va_list ap) { // eCos and Windows are not standard-compliant and return -1 when // the buffer is too small. Keep allocating larger buffers until we // succeed or out of memory. - *buf = NULL; // LCOV_EXCL_START + // LCOV_EXCL_START + *buf = NULL; while (len < 0) { free(*buf); if (size == 0) size = 5; @@ -190,7 +191,7 @@ int mg_vasprintf(char **buf, size_t size, const char *fmt, va_list ap) { // Microsoft version of vsnprintf() is not always null-terminated, so put // the terminator manually (*buf)[len] = 0; - /// LCOV_EXCL_STOP + // LCOV_EXCL_STOP } else if (len >= (int) size) { /// Standard-compliant code path. Allocate a buffer that is large enough if ((*buf = (char *) malloc(len + 1)) == NULL) { diff --git a/test/unit_test.c b/test/unit_test.c index 3cce1310..879d92d4 100644 --- a/test/unit_test.c +++ b/test/unit_test.c @@ -839,8 +839,31 @@ static void test_timer(void) { ASSERT(g_timers == NULL); } +static void test_str(void) { + struct mg_str s = mg_strdup(mg_str("a")); + ASSERT(mg_strcmp(s, mg_str("a")) == 0); + free((void *) s.ptr); + ASSERT(mg_strcmp(mg_str(""), mg_str(NULL)) == 0); + ASSERT(mg_strcmp(mg_str("a"), mg_str("b")) < 0); + ASSERT(mg_strcmp(mg_str("b"), mg_str("a")) > 0); + ASSERT(mg_strstr(mg_str("abc"), mg_str("d")) == NULL); + ASSERT(mg_strstr(mg_str("abc"), mg_str("b")) != NULL); + ASSERT(mg_strcmp(mg_str("hi"), mg_strstrip(mg_str(" \thi\r\n"))) == 0); +} + +static void test_util(void) { + char buf[100], *s = mg_hexdump("abc", 3); + ASSERT(s != NULL); + free(s); + ASSERT(mg_file_write("data.txt", "%s", "hi") == 2); + ASSERT(strcmp(mg_ntoa(0x100007f, buf, sizeof(buf)), "127.0.0.1") == 0); + ASSERT(strcmp(mg_hex("abc", 3, buf), "616263") == 0); +} + int main(void) { mg_log_set("3"); + test_str(); + test_util(); test_timer(); test_http_range(); test_url();