From 1ce7209c69b303f27a68ee7ef00bd29341314ae2 Mon Sep 17 00:00:00 2001 From: Mario Limonciello Date: Thu, 14 Nov 2019 09:48:43 -0600 Subject: [PATCH] Adjust unit tests to not wrap string functions (Fixes: #1547) This has caused multiple segmentation fault failures for unit tests on s390x with optimizations set to `-O` or higher. The failures appear to be related to the use of `--wrap` with `strlen` and `strchr`. Likely the compiler chooses to optimize out these functions. Instead explicitly create strings that represent the boundary conditions and use those for the unit tests. Suggested-by: Andreas Fuchs Signed-off-by: Mario Limonciello --- Makefile-test.am | 2 +- test/unit/tctildr.c | 39 +++++++++------------------------------ 2 files changed, 10 insertions(+), 31 deletions(-) diff --git a/Makefile-test.am b/Makefile-test.am index b0e23e3e4..161cb5f55 100644 --- a/Makefile-test.am +++ b/Makefile-test.am @@ -253,7 +253,7 @@ test_unit_tctildr_CFLAGS = $(CMOCKA_CFLAGS) $(TESTS_CFLAGS) test_unit_tctildr_LDADD = $(CMOCKA_LIBS) $(libutil) test_unit_tctildr_LDFLAGS = -Wl,--wrap=calloc,--wrap=free \ -Wl,--wrap=tctildr_finalize_data,--wrap=tctildr_get_tcti \ - -Wl,--wrap=tctildr_get_info,--wrap=strlen,--wrap=strchr,--wrap=strcpy + -Wl,--wrap=tctildr_get_info test_unit_tctildr_SOURCES = test/unit/tctildr.c \ src/tss2-tcti/tctildr.c diff --git a/test/unit/tctildr.c b/test/unit/tctildr.c index c92f3121a..5b5da31a4 100644 --- a/test/unit/tctildr.c +++ b/test/unit/tctildr.c @@ -8,6 +8,7 @@ #endif #include +#include #include #include #include @@ -171,38 +172,15 @@ test_conf_parse_null (void **state) TSS2_RC rc = tctildr_conf_parse (NULL, NULL, NULL); assert_int_equal (rc, TSS2_TCTI_RC_BAD_REFERENCE); } -#define NAME_CONF_STR (char*)0xf100d -size_t __real_strlen (const char *s); -size_t -__wrap_strlen (const char *s) -{ - if (s != NAME_CONF_STR) - return __real_strlen (s); - return mock_type (size_t); -} -char* __real_strchr (const char *s, int c); -char* -__wrap_strchr (const char *s, int c) -{ - if (s != NAME_CONF_STR) - return __real_strchr (s, c); - return mock_type (char*); -} -char* __real_strcpy(char *dest, const char *src); -char* -__wrap_strcpy(char *dest, const char *src) -{ - if (src != NAME_CONF_STR) - return __real_strcpy (dest, src); - return mock_type (char*); -} void test_conf_parse_bad_length (void **state) { char name_buf[0], conf_buf[0]; - will_return (__wrap_strlen, PATH_MAX); - TSS2_RC rc = tctildr_conf_parse (NAME_CONF_STR, name_buf, conf_buf); + char name[PATH_MAX+1]; + memset(&name[0], 'a', sizeof(name)); + name[PATH_MAX] = '\0'; + TSS2_RC rc = tctildr_conf_parse (name, name_buf, conf_buf); assert_int_equal (rc, TSS2_TCTI_RC_BAD_VALUE); } void @@ -257,9 +235,10 @@ static void tctildr_init_conf_fail_test (void **state) { TSS2_RC rc; - - will_return (__wrap_strlen, PATH_MAX); - rc = Tss2_TctiLdr_Initialize (NAME_CONF_STR, NULL); + char name[PATH_MAX+1]; + memset(&name[0], 'a', sizeof(name)); + name[PATH_MAX] = '\0'; + rc = Tss2_TctiLdr_Initialize (name, NULL); assert_int_equal (rc, TSS2_TCTI_RC_BAD_VALUE); } static void