mirror of
https://github.com/cesanta/mongoose.git
synced 2026-10-30 12:50:58 +00:00
Sync src with amalgamated versions
Now src contains exactly what's embedded in mongoose.c and .h, nothing more. Added `tools/amalgam.sh` to aamlgamate both files at once. There are no functional changes to mongoose.c, .h in this PR, only slight filename changes.
This commit is contained in:
1 parent
73a2b92278
commit
b0a1cd7b0d
198 files changed
+2870
-23966
No files matched your search
+2
-16
@@ -4,24 +4,11 @@ PROG = unit_test
|
||||
|
||||
MONGOOSE_DIR = ..
|
||||
|
||||
SRC_DIR = ../src
|
||||
|
||||
COMMON_DIR_PUB = ../src/common
|
||||
COMMON_DIR_DEV = ../../common
|
||||
|
||||
# Our dev repo and public mongoose repo have different layouts, so here
|
||||
# we make it work on both
|
||||
ifneq ("$(wildcard ../../common.mk)","")
|
||||
COMMON_PARENT = ../..
|
||||
else
|
||||
COMMON_PARENT = $(SRC_DIR)
|
||||
endif
|
||||
|
||||
TEST_SOURCES = unit_test.c test_util.c test_main.c fuzz.c
|
||||
AMALGAMATED_SOURCES = ../mongoose.c
|
||||
KRYPTON_PATH = $(REPO_ROOT)/krypton
|
||||
|
||||
VPATH = $(MONGOOSE_DIR) $(SRC_DIR) $(COMMON_DIR_PUB) $(COMMON_DIR_DEV) $(COMMON_PARENT)
|
||||
VPATH = $(MONGOOSE_DIR)
|
||||
|
||||
# or Krypton, or mbedTLS
|
||||
SSL ?= OpenSSL
|
||||
@@ -49,7 +36,7 @@ UNIX_FEATURE_FLAGS=-DMG_ENABLE_IPV6 -DMG_ENABLE_SSL
|
||||
# `mg_http_parse_header()` is removed from mongoose.
|
||||
CFLAGS = -W -Wall -Wundef -Werror -Wno-deprecated-declarations -g -O0 -Wno-multichar -D__USE_MISC \
|
||||
$(COMMON_FEATURE_FLAGS) $(UNIX_FEATURE_FLAGS) \
|
||||
$(patsubst %,-I%,$(subst :, ,$(VPATH))) \
|
||||
-I$(MONGOOSE_DIR) -I../src \
|
||||
-include unit_test.h -pthread $(CFLAGS_EXTRA)
|
||||
|
||||
LDFLAGS = -lm
|
||||
@@ -67,7 +54,6 @@ LDFLAGS += -lmbedcrypto -lmbedtls -lmbedx509
|
||||
endif
|
||||
|
||||
include test.mk
|
||||
include $(SRC_DIR)/mg_modules.mk
|
||||
|
||||
# http://crossgcc.rts-software.org/doku.php?id=compiling_for_win32
|
||||
MINGW_GCC=/usr/local/gcc-4.8.0-qt-4.8.4-for-mingw32/win32-gcc/bin/i586-mingw32-gcc
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
/*
|
||||
* Copyright (c) 2014-2018 Cesanta Software Limited
|
||||
* All rights reserved
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the ""License"");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an ""AS IS"" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "common/cs_dbg.h"
|
||||
#include "test_main.h"
|
||||
#include "test_util.h"
|
||||
|
||||
#if defined(_MSC_VER) && _MSC_VER >= 1900
|
||||
#include <crtdbg.h>
|
||||
|
||||
int __cdecl CrtDbgHook(int nReportType, char *szMsg, int *pnRet) {
|
||||
(void) nReportType;
|
||||
(void) szMsg;
|
||||
(void) pnRet;
|
||||
|
||||
fprintf(stderr, "CRT debug hook: type: %d, msg: %s\n", nReportType, szMsg);
|
||||
/* Return true - Abort,Retry,Ignore dialog will *not* be displayed */
|
||||
return 1;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifndef __cdecl
|
||||
#define __cdecl
|
||||
#endif
|
||||
|
||||
int g_num_tests = 0;
|
||||
int g_num_checks = 0;
|
||||
const char *g_argv_0 = NULL;
|
||||
|
||||
int __cdecl main(int argc, char *argv[]) {
|
||||
const char *fail_msg;
|
||||
const char *filter = argc > 1 ? argv[1] : "";
|
||||
char *seed_str = getenv("TEST_SEED");
|
||||
double started;
|
||||
int seed = 0;
|
||||
|
||||
if (seed_str != NULL) {
|
||||
seed = atoi(seed_str);
|
||||
} else {
|
||||
seed = (int) time(NULL);
|
||||
}
|
||||
printf("seed: %d\n", seed);
|
||||
srand(seed);
|
||||
|
||||
setvbuf(stdout, NULL, _IONBF, 0);
|
||||
setvbuf(stderr, NULL, _IONBF, 0);
|
||||
#if defined(_MSC_VER) && _MSC_VER >= 1900
|
||||
/* NOTE: not available on wine/vc6 */
|
||||
_CrtSetReportHook2(_CRT_RPTHOOK_INSTALL, CrtDbgHook);
|
||||
#endif
|
||||
|
||||
g_argv_0 = argv[0];
|
||||
|
||||
tests_setup();
|
||||
|
||||
started = cs_time();
|
||||
fail_msg = tests_run(filter);
|
||||
printf("%s, ran %d tests (%d checks) in %.3lfs\n", fail_msg ? "FAIL" : "PASS",
|
||||
g_num_tests, g_num_checks, cs_time() - started);
|
||||
|
||||
tests_teardown();
|
||||
|
||||
if (fail_msg != NULL) {
|
||||
/* Prevent leak analyzer from running: there will be "leaks" because of
|
||||
* premature return from the test, and in this case we don't care. */
|
||||
_exit(EXIT_FAILURE);
|
||||
}
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* Copyright (c) 2014-2018 Cesanta Software Limited
|
||||
* All rights reserved
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the ""License"");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an ""AS IS"" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef CS_COMMON_TEST_MAIN_H_
|
||||
#define CS_COMMON_TEST_MAIN_H_
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
void tests_setup(void);
|
||||
const char *tests_run(const char *filter);
|
||||
void tests_teardown(void);
|
||||
extern const char *g_argv_0;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* CS_COMMON_TEST_MAIN_H_ */
|
||||
@@ -0,0 +1,105 @@
|
||||
/*
|
||||
* Copyright (c) 2014-2018 Cesanta Software Limited
|
||||
* All rights reserved
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the ""License"");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an ""AS IS"" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "test_util.h"
|
||||
|
||||
#include <ctype.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#ifndef _WIN32
|
||||
#include <sys/time.h>
|
||||
#else
|
||||
#include <windows.h>
|
||||
#endif
|
||||
|
||||
int num_tests = 0;
|
||||
|
||||
static char *_escape(const char *s, size_t n) {
|
||||
size_t i, j;
|
||||
char *res = (char *) malloc(n * 4 + 1);
|
||||
for (i = j = 0; s[i] != '\0' && i < n; i++) {
|
||||
if (!iscntrl((int) s[i])) {
|
||||
res[j++] = s[i];
|
||||
} else {
|
||||
j += sprintf(res + j, "\\x%02x", s[i]);
|
||||
}
|
||||
}
|
||||
res[j] = '\0';
|
||||
return res;
|
||||
}
|
||||
|
||||
void _strfail(const char *a, const char *e, int len) {
|
||||
char *ae, *ee;
|
||||
if (len < 0) {
|
||||
len = strlen(a);
|
||||
if (strlen(e) > (size_t) len) len = strlen(e);
|
||||
}
|
||||
ae = _escape(a, len);
|
||||
ee = _escape(e, len);
|
||||
printf("Expected: %s\nActual : %s\n", ee, ae);
|
||||
free(ae);
|
||||
free(ee);
|
||||
}
|
||||
|
||||
int _assert_streq(const char *actual, const char *expected) {
|
||||
if (strcmp(actual, expected) != 0) {
|
||||
_strfail(actual, expected, -1);
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
int _assert_streq_nz(const char *actual, const char *expected) {
|
||||
size_t n = strlen(expected);
|
||||
if (strncmp(actual, expected, n) != 0) {
|
||||
_strfail(actual, expected, n);
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
#if MG_ENABLE_POLL_UNTIL
|
||||
int c_str_ne(void *a, void *b) {
|
||||
int r = strcmp((const char *) a, (const char *) b);
|
||||
return r;
|
||||
}
|
||||
|
||||
int c_int_ne(void *a, void *b) {
|
||||
return *((int *) a) != (intptr_t) b;
|
||||
}
|
||||
|
||||
int c_int_eq(void *a, void *b) {
|
||||
return *((int *) a) == (intptr_t) b;
|
||||
}
|
||||
|
||||
void poll_until(struct mg_mgr *mgr, double timeout, int (*cond)(void *, void *),
|
||||
void *cond_arg1, void *cond_arg2) {
|
||||
int i;
|
||||
double start = cs_time();
|
||||
while (cs_time() - start < timeout) {
|
||||
mg_mgr_poll(mgr, 2);
|
||||
if (cond != NULL && cond(cond_arg1, cond_arg2)) {
|
||||
/* A few more cycles to test for overshoots. */
|
||||
for (i = 0; i < 5; i++) {
|
||||
mg_mgr_poll(mgr, 2);
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,251 @@
|
||||
/*
|
||||
* Copyright (c) 2014-2018 Cesanta Software Limited
|
||||
* All rights reserved
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the ""License"");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an ""AS IS"" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef CS_COMMON_TEST_UTIL_H_
|
||||
#define CS_COMMON_TEST_UTIL_H_
|
||||
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "common/cs_time.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
extern int g_num_tests;
|
||||
extern int g_num_checks;
|
||||
|
||||
#ifdef MG_TEST_ABORT_ON_FAIL
|
||||
#define MG_TEST_ABORT abort()
|
||||
#else
|
||||
#define MG_TEST_ABORT
|
||||
#endif
|
||||
|
||||
int _assert_streq(const char *actual, const char *expected);
|
||||
int _assert_streq_nz(const char *actual, const char *expected);
|
||||
void _strfail(const char *a, const char *e, int len);
|
||||
|
||||
#define FAIL(str, line) \
|
||||
do { \
|
||||
printf("%s:%d:1 [%s] (in %s)\n", __FILE__, line, str, __func__); \
|
||||
MG_TEST_ABORT; \
|
||||
return str; \
|
||||
} while (0)
|
||||
|
||||
#define ASSERT(expr) \
|
||||
do { \
|
||||
g_num_checks++; \
|
||||
if (!(expr)) FAIL(#expr, __LINE__); \
|
||||
} while (0)
|
||||
#define ASSERT_TRUE(expr) ASSERT(expr)
|
||||
#define ASSERT_FALSE(expr) ASSERT(!(expr))
|
||||
|
||||
/*
|
||||
* Run test function, use its name as the test name to print
|
||||
*/
|
||||
#define RUN_TEST(test) RUN_TEST_WNAME(test, #test)
|
||||
|
||||
/*
|
||||
* Run test function, use the provided name as the test name to print
|
||||
*/
|
||||
#define RUN_TEST_WNAME(test, test_name) \
|
||||
do { \
|
||||
const char *msg = NULL; \
|
||||
if (strstr(test_name, filter)) { \
|
||||
double elapsed = cs_time(); \
|
||||
msg = test(); \
|
||||
elapsed = cs_time() - elapsed; \
|
||||
printf(" [%.3f] %s\n", elapsed, test_name); \
|
||||
fflush(stdout); \
|
||||
g_num_tests++; \
|
||||
} \
|
||||
if (msg) return msg; \
|
||||
} while (0)
|
||||
|
||||
/* VC6 doesn't have long long. */
|
||||
#if (defined(_MSC_VER) && _MSC_VER <= 1200)
|
||||
#define LONG_TYPE int64_t
|
||||
#else
|
||||
#define LONG_TYPE long long
|
||||
#endif
|
||||
#define AS_DOUBLE(d) (double)(d)
|
||||
|
||||
#define RANGE_CHECK(x) (fabs(x) > 4503599627370496.0 /* 2 << 51 */)
|
||||
|
||||
/*
|
||||
* Numeric equality assertion.
|
||||
* Args are cast to double to ensure aruemnts are only evaluated once.
|
||||
* This will work fine for all integer types except large 64-bit values.
|
||||
*/
|
||||
#define ASSERT_EQ(actual, expected) \
|
||||
do { \
|
||||
const double ad = AS_DOUBLE(actual); \
|
||||
const double ed = AS_DOUBLE(expected); \
|
||||
if (RANGE_CHECK(ad) || RANGE_CHECK(ed)) { \
|
||||
FAIL("loss of precision, use ASSERT_EQ64", __LINE__); \
|
||||
} \
|
||||
g_num_checks++; \
|
||||
if (!(ad == ed)) { \
|
||||
printf("%lf != %lf\n", ad, ed); \
|
||||
FAIL(#actual " != " #expected, __LINE__); \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
#define ASSERT_EQ64(actual, expected) \
|
||||
do { \
|
||||
const LONG_TYPE a64 = (LONG_TYPE)(actual); \
|
||||
const LONG_TYPE e64 = (LONG_TYPE)(expected); \
|
||||
g_num_checks++; \
|
||||
if (!(a64 == e64)) { \
|
||||
printf("%lld != %lld\n", a64, e64); \
|
||||
FAIL(#actual " != " #expected, __LINE__); \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
#define ASSERT_NE(actual, expected) \
|
||||
do { \
|
||||
const double ad = AS_DOUBLE(actual); \
|
||||
const double ed = AS_DOUBLE(expected); \
|
||||
g_num_checks++; \
|
||||
if (RANGE_CHECK(ad) || RANGE_CHECK(ed)) { \
|
||||
FAIL("loss of precision, use ASSERT_NE64", __LINE__); \
|
||||
} \
|
||||
if (!(ad != ed)) { \
|
||||
printf("%lf == %lf\n", ad, ed); \
|
||||
FAIL(#actual " == " #expected, __LINE__); \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
#define ASSERT_NE64(actual, expected) \
|
||||
do { \
|
||||
const LONG_TYPE a64 = (LONG_TYPE)(actual); \
|
||||
const LONG_TYPE e64 = (LONG_TYPE)(expected); \
|
||||
g_num_checks++; \
|
||||
if (!(a64 != e64)) { \
|
||||
printf("%lld == %lld\n", a64, e64); \
|
||||
FAIL(#actual " == " #expected, __LINE__); \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
/* "Less than" assertion. */
|
||||
#define ASSERT_LT(a, b) \
|
||||
do { \
|
||||
const double ad = AS_DOUBLE(a); \
|
||||
const double bd = AS_DOUBLE(b); \
|
||||
g_num_checks++; \
|
||||
if (RANGE_CHECK(ad) || RANGE_CHECK(bd)) { \
|
||||
FAIL("loss of precision in comparison", __LINE__); \
|
||||
} \
|
||||
if (!(ad < bd)) { \
|
||||
printf("%lf >= %lf\n", ad, bd); \
|
||||
FAIL(#a " < " #b, __LINE__); \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
/* "Greater than" assertion. */
|
||||
#define ASSERT_GT(a, b) \
|
||||
do { \
|
||||
const double ad = AS_DOUBLE(a); \
|
||||
const double bd = AS_DOUBLE(b); \
|
||||
g_num_checks++; \
|
||||
if (!(ad > bd)) { \
|
||||
printf("%lf <= %lf\n", ad, bd); \
|
||||
FAIL(#a " > " #b, __LINE__); \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
/* Assert that actual == expected, where both are NUL-terminated. */
|
||||
#define ASSERT_STREQ(actual, expected) \
|
||||
do { \
|
||||
g_num_checks++; \
|
||||
if (!_assert_streq((actual), (expected))) { \
|
||||
FAIL("ASSERT_STREQ(" #actual ", " #expected ")", __LINE__); \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
/* Assert that actual == expected, where both are pointers */
|
||||
#define ASSERT_PTREQ(actual, expected) \
|
||||
do { \
|
||||
const void *ap = (const void *) (actual); \
|
||||
const void *ep = (const void *) (expected); \
|
||||
g_num_checks++; \
|
||||
if (!(ap == ep)) { \
|
||||
printf("%p != %p\n", ap, ep); \
|
||||
FAIL("ASSERT_PTREQ(" #actual ", " #expected ")", __LINE__); \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
/* Assert that actual != expected, where both are pointers */
|
||||
#define ASSERT_PTRNE(actual, expected) \
|
||||
do { \
|
||||
const void *ap = (const void *) (actual); \
|
||||
const void *ep = (const void *) (expected); \
|
||||
g_num_checks++; \
|
||||
if (!(ap != ep)) { \
|
||||
printf("%p == %p\n", ap, ep); \
|
||||
FAIL("ASSERT_PTRNE(" #actual ", " #expected ")", __LINE__); \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
/* Same as STREQ, but only expected is NUL-terminated. */
|
||||
#define ASSERT_STREQ_NZ(actual, expected) \
|
||||
do { \
|
||||
g_num_checks++; \
|
||||
if (!_assert_streq_nz(actual, expected)) { \
|
||||
FAIL("ASSERT_STREQ_NZ(" #actual ", " #expected ")", __LINE__); \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
#define ASSERT_MG_STREQ(actual, expected) \
|
||||
do { \
|
||||
g_num_checks++; \
|
||||
if ((actual).len != strlen(expected) || \
|
||||
memcmp((actual).p, expected, (actual).len) != 0) { \
|
||||
printf("'%.*s' (%d) != '%s'\n", (int)(actual).len, (actual).p, \
|
||||
(int)(actual).len, expected); \
|
||||
FAIL("ASSERT_MG_STREQ(" #actual ", " #expected ")", __LINE__); \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
#define CHECK_CALL(call) \
|
||||
do { \
|
||||
const char *r = (call); \
|
||||
if (r != NULL) { \
|
||||
fprintf(stderr, "... %s:%d in %s\n", __FILE__, __LINE__, __func__); \
|
||||
return r; \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
#ifndef MG_ENABLE_POLL_UNTIL
|
||||
#define MG_ENABLE_POLL_UNTIL 0
|
||||
#endif
|
||||
|
||||
#if MG_ENABLE_POLL_UNTIL
|
||||
#include "mongoose.h"
|
||||
int c_str_ne(void *a, void *b);
|
||||
int c_int_ne(void *a, void *b);
|
||||
int c_int_eq(void *a, void *b);
|
||||
void poll_until(struct mg_mgr *mgr, double timeout, int (*cond)(void *, void *),
|
||||
void *cond_arg1, void *cond_arg2);
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* CS_COMMON_TEST_UTIL_H_ */
|
||||
+5
-4
@@ -17,11 +17,12 @@
|
||||
|
||||
#include "unit_test.h"
|
||||
|
||||
#include "common/cs_md5.h"
|
||||
#include "common/test_main.h"
|
||||
#include "common/test_util.h"
|
||||
#include "mongoose.h"
|
||||
#include "src/mg_internal.h"
|
||||
|
||||
#include "common/cs_md5.h"
|
||||
#include "../src/mg_internal.h"
|
||||
#include "test_main.h"
|
||||
#include "test_util.h"
|
||||
|
||||
#if defined(__STDC_VERSION__) && __STDC_VERSION__ < 199901L && !defined(WIN32)
|
||||
#define __func__ ""
|
||||
|
||||
Reference in new issue
Block a user