From ba8625a7016ba9bdbb4de1f7c461f67bccd1e9e2 Mon Sep 17 00:00:00 2001 From: Alexandru Ardelean Date: Mon, 10 Jul 2017 12:01:56 +0300 Subject: [PATCH 1/7] tests: add `set -e` specifier to bail early on build run Seems that test1 is failing, but travis is not catching it. Likely, this is because the `cppcheck` returns success and we need to bail on the `make check` step. Signed-off-by: Alexandru Ardelean --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index 2aa2eeff05..7aed15dafd 100644 --- a/.travis.yml +++ b/.travis.yml @@ -16,6 +16,7 @@ os: before_install: - echo $LANG - echo $LC_ALL + - set -e install: - sh autogen.sh From 9d47ae824cb210c08d9343336438375ad717fd4f Mon Sep 17 00:00:00 2001 From: Alexandru Ardelean Date: Mon, 10 Jul 2017 13:30:49 +0300 Subject: [PATCH 2/7] tests: compress test_utile_file with test_basic More code compression/de-duplication. Signed-off-by: Alexandru Ardelean --- tests/test_basic.test | 6 +++++- tests/test_util_file.test | 16 +--------------- 2 files changed, 6 insertions(+), 16 deletions(-) mode change 100755 => 120000 tests/test_util_file.test diff --git a/tests/test_basic.test b/tests/test_basic.test index 3912732c8c..154e036d51 100755 --- a/tests/test_basic.test +++ b/tests/test_basic.test @@ -11,5 +11,9 @@ fi filename=$(basename "$0") filename="${filename%.*}" -run_output_test $filename +# This is only for the test_util_file.test ; +# more stuff could be extended +cp -f "$srcdir/valid.json" . + +run_output_test $filename "$srcdir" exit $? diff --git a/tests/test_util_file.test b/tests/test_util_file.test deleted file mode 100755 index 541aada705..0000000000 --- a/tests/test_util_file.test +++ /dev/null @@ -1,15 +0,0 @@ -#!/bin/sh - -# Common definitions -if test -z "$srcdir"; then - srcdir="${0%/*}" - test "$srcdir" = "$0" && srcdir=. - test -z "$srcdir" && srcdir=. -fi -. "$srcdir/test-defs.sh" - -cp -f "$srcdir/valid.json" . -run_output_test test_util_file "$srcdir" -_err=$? - -exit $_err diff --git a/tests/test_util_file.test b/tests/test_util_file.test new file mode 120000 index 0000000000..58a13f4f32 --- /dev/null +++ b/tests/test_util_file.test @@ -0,0 +1 @@ +test_basic.test \ No newline at end of file From effab3f91ab30eae502e43eb0c46e2ff85f7178c Mon Sep 17 00:00:00 2001 From: Alexandru Ardelean Date: Mon, 10 Jul 2017 17:47:00 +0300 Subject: [PATCH 3/7] travis,tests: run cppcheck only if it exists ugh... seems cppcheck is not packaged for OS X And `set -e` exposes this. And also `cppcheck` seems to exit with non-zero exit codes by default [even if errs found]. Signed-off-by: Alexandru Ardelean --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 7aed15dafd..1471983e91 100644 --- a/.travis.yml +++ b/.travis.yml @@ -29,4 +29,4 @@ script: after_success: - make check - - cppcheck --quiet *.h *.c tests/ + - if type cppcheck &> /dev/null ; then cppcheck --error-exitcode=1 --quiet *.h *.c tests/ ; fi From 7b9432d5648c902800dea1208143febb9db632cc Mon Sep 17 00:00:00 2001 From: Alexandru Ardelean Date: Mon, 10 Jul 2017 18:00:41 +0300 Subject: [PATCH 4/7] tests: fix leak in `test_util_file` ; found by cppcheck Which now seems to fail the build. Signed-off-by: Alexandru Ardelean --- tests/test_util_file.c | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_util_file.c b/tests/test_util_file.c index f98dce2e5c..5dbdb4774b 100644 --- a/tests/test_util_file.c +++ b/tests/test_util_file.c @@ -106,6 +106,7 @@ static void stat_and_cat(const char *file) buf[sb.st_size] = '\0'; printf("file[%s], size=%d, contents=%s\n", file, (int)sb.st_size, buf); free(buf); + close(d); } int main(int argc, char **argv) From 85f57859fd2ba20c7366f00e1fbaf94c4b264010 Mon Sep 17 00:00:00 2001 From: Alexandru Ardelean Date: Thu, 13 Jul 2017 09:33:39 +0300 Subject: [PATCH 5/7] configure.ac: check for `uselocale` function only on Linux platforms On Apple this seems to fail the `test_locale` test, which would imply that the `uselocale` function does not behave as expected. Signed-off-by: Alexandru Ardelean --- configure.ac | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/configure.ac b/configure.ac index 3a2779e35f..bbaacf5196 100644 --- a/configure.ac +++ b/configure.ac @@ -7,6 +7,8 @@ AM_INIT_AUTOMAKE AC_PROG_MAKE_SET +AC_CANONICAL_HOST + AC_ARG_ENABLE(rdrand, AS_HELP_STRING([--enable-rdrand], [Enable RDRAND Hardware RNG Hash Seed generation on supported x86/x64 platforms.]), @@ -57,7 +59,7 @@ AS_IF([test "x$ac_cv___thread" != xno], AC_FUNC_VPRINTF AC_FUNC_MEMCMP AC_CHECK_FUNCS([realloc]) -AC_CHECK_FUNCS(strcasecmp strdup strerror snprintf vsnprintf vasprintf open vsyslog strncasecmp setlocale uselocale) +AC_CHECK_FUNCS(strcasecmp strdup strerror snprintf vsnprintf vasprintf open vsyslog strncasecmp setlocale) AC_CHECK_DECLS([INFINITY], [], [], [[#include ]]) AC_CHECK_DECLS([nan], [], [], [[#include ]]) AC_CHECK_DECLS([isnan], [], [], [[#include ]]) @@ -65,6 +67,14 @@ AC_CHECK_DECLS([isinf], [], [], [[#include ]]) AC_CHECK_DECLS([_isnan], [], [], [[#include ]]) AC_CHECK_DECLS([_finite], [], [], [[#include ]]) +case "${host_os}" in + linux*) + AC_CHECK_FUNCS([uselocale]) + ;; + *) # Nothing + ;; +esac + if test "$ac_cv_have_decl_isnan" = "yes" ; then AC_TRY_LINK([#include ], [float f = 0.0; return isnan(f)], [], [LIBS="$LIBS -lm"]) fi From fb72160cafc1e5f0aeb702ba76428a992b9e8792 Mon Sep 17 00:00:00 2001 From: Alexandru Ardelean Date: Thu, 13 Jul 2017 10:11:15 +0300 Subject: [PATCH 6/7] build: make `strerror()` override-able If we want to override `strerror()` in libjson-c to make tests consistent across platforms, we need to do it build-wide as configure/build option. Apple linkers make it really hard to override functions at link-time, and this seems to be locked down on travis-ci.org [ for security reasons I assume ]. While I got it to work locally, it did not work when running on travis. Signed-off-by: Alexandru Ardelean --- Makefile.am | 6 ++++++ configure.ac | 16 ++++++++++++++++ json_object.c | 3 ++- json_pointer.c | 3 ++- json_util.c | 3 ++- random_seed.c | 2 +- tests/strerror_override.c => strerror_override.c | 4 ++-- strerror_override.h | 12 ++++++++++++ tests/Makefile.am | 2 +- tests/test_json_pointer.c | 2 +- tests/test_util_file.c | 2 +- 11 files changed, 46 insertions(+), 9 deletions(-) rename tests/strerror_override.c => strerror_override.c (96%) create mode 100644 strerror_override.h diff --git a/Makefile.am b/Makefile.am index 126ceeb625..6f8652502f 100644 --- a/Makefile.am +++ b/Makefile.am @@ -53,6 +53,12 @@ libjson_c_la_SOURCES = \ printbuf.c \ random_seed.c +if ENABLE_STRERROR_OVERRIDE +libjson_cinclude_HEADERS+= \ + strerror_override.h +libjson_c_la_SOURCES+= \ + strerror_override.c +endif distclean-local: -rm -rf $(testsubdir) diff --git a/configure.ac b/configure.ac index bbaacf5196..7af60bf555 100644 --- a/configure.ac +++ b/configure.ac @@ -23,6 +23,22 @@ else AC_MSG_RESULT([RDRAND Hardware RNG Hash Seed disabled. Use --enable-rdrand to enable]) fi +AC_ARG_ENABLE(strerror-override, + AS_HELP_STRING([--enable-strerror-override], + [Override strerror() function with internal version.]), +[if test x$enableval = xyes; then + enable_strerror_override=yes + AC_DEFINE(ENABLE_STRERROR_OVERRIDE, 1, [Override strerror() with internal version]) +fi]) + +AM_CONDITIONAL([ENABLE_STRERROR_OVERRIDE], [test "x$enable_strerror_override" = "xyes"]) + +if test "x$enable_strerror_override" = "xyes"; then + AC_MSG_RESULT([Overriding `strerror()` function with internal version]) +else + AC_MSG_RESULT([Using libc's `strerror()` function]) +fi + # enable silent build by default m4_ifdef([AM_SILENT_RULES], [AM_SILENT_RULES([yes])]) diff --git a/json_object.c b/json_object.c index ddc96bc190..8c80426fa9 100644 --- a/json_object.c +++ b/json_object.c @@ -12,13 +12,14 @@ #include "config.h" +#include "strerror_override.h" + #include #include #include #include #include #include -#include #include "debug.h" #include "printbuf.h" diff --git a/json_pointer.c b/json_pointer.c index d106e9e9ca..2b2a9ef507 100644 --- a/json_pointer.c +++ b/json_pointer.c @@ -8,10 +8,11 @@ #include "config.h" +#include "strerror_override.h" + #include #include #include -#include #include #include diff --git a/json_util.c b/json_util.c index 9a2f9ff5b6..3a717b7994 100644 --- a/json_util.c +++ b/json_util.c @@ -12,13 +12,14 @@ #include "config.h" #undef realloc +#include "strerror_override.h" + #include #include #include #include #include #include -#include #include #ifdef HAVE_SYS_TYPES_H diff --git a/random_seed.c b/random_seed.c index 3b61b770e8..cb086d3b95 100644 --- a/random_seed.c +++ b/random_seed.c @@ -9,6 +9,7 @@ * */ +#include "strerror_override.h" #include #include "config.h" #include "random_seed.h" @@ -128,7 +129,6 @@ static int get_rdrand_seed() #include #include #include -#include #include #include diff --git a/tests/strerror_override.c b/strerror_override.c similarity index 96% rename from tests/strerror_override.c rename to strerror_override.c index 980cbb9c60..6cad0c09e3 100644 --- a/tests/strerror_override.c +++ b/strerror_override.c @@ -1,4 +1,4 @@ -#include +#include "strerror_override.h" /* * Override strerror() to get consistent output across platforms. @@ -54,7 +54,7 @@ static struct { #define PREFIX "ERRNO=" static char errno_buf[128] = PREFIX; -char *strerror(int errno_in) +char *_json_c_strerror(int errno_in) { int start_idx; char digbuf[20]; diff --git a/strerror_override.h b/strerror_override.h new file mode 100644 index 0000000000..96e6bc6da2 --- /dev/null +++ b/strerror_override.h @@ -0,0 +1,12 @@ +#ifndef __STRERROR_OVERRIDE_H__ +#define __STRERROR_OVERRIDE_H__ + +#include "config.h" +#include + +#if ENABLE_STRERROR_OVERRIDE +char *_json_c_strerror(int errno_in); +#define strerror _json_c_strerror +#endif + +#endif /* __STRERROR_OVERRIDE_H__ */ diff --git a/tests/Makefile.am b/tests/Makefile.am index 824ed30448..917d20f679 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -52,7 +52,7 @@ EXTRA_DIST+= test2Formatted_plain.expected EXTRA_DIST+= test2Formatted_pretty.expected EXTRA_DIST+= test2Formatted_spaced.expected -test_util_file_SOURCES = test_util_file.c strerror_override.c +test_util_file_SOURCES = test_util_file.c testsubdir=testSubDir TESTS_ENVIRONMENT = top_builddir=$(top_builddir) diff --git a/tests/test_json_pointer.c b/tests/test_json_pointer.c index c3733dea33..cc96ab93b8 100644 --- a/tests/test_json_pointer.c +++ b/tests/test_json_pointer.c @@ -1,4 +1,4 @@ -#include +#include "strerror_override.h" #include #include #include diff --git a/tests/test_util_file.c b/tests/test_util_file.c index 5dbdb4774b..b4918bee57 100644 --- a/tests/test_util_file.c +++ b/tests/test_util_file.c @@ -1,4 +1,4 @@ -#include +#include "strerror_override.h" #include #include #include From bc2e30453ba2e8da963d7443b6f19aae218458af Mon Sep 17 00:00:00 2001 From: Alexandru Ardelean Date: Thu, 13 Jul 2017 10:24:27 +0300 Subject: [PATCH 7/7] build,travis: enable strerror override option in build To get consistent output between Linux & OS X. Signed-off-by: Alexandru Ardelean --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 1471983e91..df2014d553 100644 --- a/.travis.yml +++ b/.travis.yml @@ -22,7 +22,7 @@ install: - sh autogen.sh before_script: - - ./configure + - ./configure --enable-strerror-override script: - make