Fix test mocking (ASAN build errors) 53/244353/3
authorKonrad Kuchciak <k.kuchciak@samsung.com>
Thu, 17 Sep 2020 12:00:42 +0000 (14:00 +0200)
committerKonrad Kuchciak <k.kuchciak@samsung.com>
Fri, 18 Sep 2020 06:03:15 +0000 (08:03 +0200)
Change-Id: I7e3027a9ef3d183bc47ddb4779eb58fe56db7222
Signed-off-by: Konrad Kuchciak <k.kuchciak@samsung.com>
CMakeLists.txt
src/test/CMakeLists.txt
src/test/test_diagnostics.h
src/test/test_diagnostics_data_read.c
src/test/test_diagnostics_get_data.c
src/test/test_diagnostics_request_client_data.c
src/test/test_diagnostics_set_notification_cb.c

index 2c7a2a20ab5b7931c8ea9b963e7f76b5545d477d..a7eb9c851eaf5ba8b98946cbce9305feb682361d 100644 (file)
@@ -11,7 +11,7 @@ pkg_check_modules(pkgs REQUIRED "${dependency}")
 FOREACH(flag ${pkgs_CFLAGS})
        SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}")
 ENDFOREACH(flag)
-SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -flto -Wall -Werror -Winline")
+SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -Wall -Werror -Winline")
 SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS}")
 
 INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/include)
index 51bae9e64b8b8cfca7797d62694892f3b17ddd4d..b01331e95ed51485ee0fa1778dafc95b3590e566 100644 (file)
@@ -3,26 +3,24 @@ ADD_CUSTOM_TARGET(ctest ALL make test WORKING_DIRECTORY ./ USES_TERMINAL)
 
 INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/src/library)
 
-function(add_mocked_test name test_flags)
-       add_executable(test_${name} test_${name}.c ${ADD_MOCKED_TEST_SOURCES} 
-               ${COMPILE_OPTIONS} ${DEFAULT_C_COMPILE_FLAGS} ${ADD_MOCKED_TEST_COMPILE_OPTIONS}
-                ${LINK_LIBRARIES} ${CMOCKA_LIBRARIES} ${ADD_MOCKED_TEST_LINK_LIBRARIES}
-                ${LINK_OPTIONS} ${ADD_MOCKED_TEST_LINK_OPTIONS})
-       add_test(test_${name} ${CMAKE_CURRENT_BINARY_DIR}/test_${name})
-       target_link_libraries(test_${name} cmocka ${target}_static ${pkgs_LDFLAGS} ${test_flags})
+function(add_mocked_test name wraps)
+       add_executable(test_${name} test_${name}.c)
+
+       target_link_libraries(test_${name} cmocka ${target}_static ${pkgs_LDFLAGS} ${wraps})
        if(ENABLE_COVERAGE)
                target_link_libraries(test_${name} -fprofile-arcs -ftest-coverage -lgcov)
        endif(ENABLE_COVERAGE)
+
+       add_test(test_${name} ${CMAKE_CURRENT_BINARY_DIR}/test_${name})
        ADD_DEPENDENCIES(ctest test_${name})
 endfunction(add_mocked_test)
 
-# register test files (source filename: test_[name].c)
-add_mocked_test(diagnostics_set_notification_cb "-Wl,--wrap,dbus_subscribe,--wrap,system_info_get_platform_bool")
-add_mocked_test(diagnostics_request_client_data "-Wl,--wrap,dumpsys_dump")
-add_mocked_test(diagnostics_get_client_id "")
+add_mocked_test(diagnostics_set_notification_cb "-Wl,--wrap=system_info_get_platform_bool,--wrap=dbus_subscribe")
+add_mocked_test(diagnostics_request_client_data "-Wl,--wrap=system_info_get_platform_bool,--wrap=dumpsys_dump")
+add_mocked_test(diagnostics_get_client_id "-Wl,--wrap=system_info_get_platform_bool")
 add_mocked_test(diagnostics_get_report_path "")
-add_mocked_test(diagnostics_get_data "-Wl,--wrap,dbus_get_file_from_report")
-add_mocked_test(diagnostics_data_read "-Wl,--wrap,poll,--wrap,read")
+add_mocked_test(diagnostics_get_data "-Wl,--wrap=system_info_get_platform_bool,--wrap=dbus_get_file_from_report")
+add_mocked_test(diagnostics_data_read "-Wl,--wrap=system_info_get_platform_bool,--wrap=poll,--wrap=read")
 add_mocked_test(diagnostics_create "")
 add_mocked_test(diagnostics_data_create "")
 add_mocked_test(diagnostics_signal_handler "")
index ec3247357f7bfa894fd4b126bf037c9bf771a44d..b7228c6a6f319622fe2163a4dd7788719ad3e01c 100644 (file)
@@ -19,6 +19,7 @@
 
 #include <stdlib.h>
 #include <system_info.h>
+#include <poll.h>
 
 #include "signal.h"
 #include "dbus.h"
@@ -48,12 +49,59 @@ struct _diagnostics_data_s {
        int fd;
 };
 
-int system_info_get_platform_bool(const char *key, bool *value)
+int __wrap_system_info_get_platform_bool(const char *key, bool *value)
 {
        *value = true;
        return SYSTEM_INFO_ERROR_NONE;
 }
 
+int __wrap_dumpsys_dump(const char *service_name, int argc, const char **argv, int *out_fd)
+{
+       (void) service_name;
+       (void) argc;
+       (void) argv;
+       (void) out_fd;
+
+       *out_fd = 1;
+
+       return (int)mock();
+}
+
+int __wrap_dbus_subscribe(void (*signal_handler)(GDBusConnection *, const gchar *, const gchar *, const gchar *, const gchar *, GVariant *, gpointer))
+{
+       (void) signal_handler;
+       return (int)mock();
+}
+
+int __wrap_dbus_get_file_from_report(const char *report_path, const int entry, int *fd)
+{
+       (void) report_path;
+       (void) entry;
+       *fd = 1;
+
+       return (int)mock();
+}
+
+int __wrap_poll(struct pollfd *__fds, nfds_t __nfds, int __timeout)
+{
+       (void) __fds;
+       (void) __nfds;
+       (void) __timeout;
+
+       __fds->revents = POLLIN;
+
+       return (int)mock();
+}
+
+ssize_t __wrap_read(int fd, void *buf, size_t nbytes)
+{
+       (void) fd;
+       (void) buf;
+       (void) nbytes;
+
+       return (int)mock();
+}
+
 struct _diagnostics_ctx_s *build_ctx_crash()
 {
        struct _diagnostics_ctx_s *ctx = calloc(1, sizeof(struct _diagnostics_ctx_s));
index 60f7a84dc6b52e5a6d6b3b77fdf5741ddd0b223b..cf002225e72f013e8bdcb9bba2f974b74aa5e712 100644 (file)
@@ -27,7 +27,6 @@
 #include "dbus.h"
 
 #include <stdio.h>
-#include <poll.h>
 
 #include "test_diagnostics.h"
 
@@ -62,17 +61,6 @@ static void test_diagnostics_data_read_n4(void **state) {
        assert_int_equal(diagnostics_data_read(&data, buf, count, timeout, NULL), DIAGNOSTICS_ERROR_INVALID_PARAMETER);
 }
 
-int poll(struct pollfd *__fds, nfds_t __nfds, int __timeout)
-{
-       (void) __fds;
-       (void) __nfds;
-       (void) __timeout;
-
-       __fds->revents = POLLIN;
-
-       return (int)mock();
-}
-
 static void test_diagnostics_data_read_n5(void **state) {
        (void) state;
        struct _diagnostics_data_s data;
@@ -82,7 +70,7 @@ static void test_diagnostics_data_read_n5(void **state) {
        int timeout = 100;
        size_t bytes_read = 0;
 
-       will_return(poll, 0);
+       will_return(__wrap_poll, 0);
 
        assert_int_equal(diagnostics_data_read(&data, buf, count, timeout, &bytes_read), DIAGNOSTICS_ERROR_TIMED_OUT);
 }
@@ -96,20 +84,11 @@ static void test_diagnostics_data_read_n6(void **state) {
        int timeout = 100;
        size_t bytes_read = 0;
 
-       will_return(poll, -1);
+       will_return(__wrap_poll, -1);
 
        assert_int_equal(diagnostics_data_read(&data, buf, count, timeout, &bytes_read), DIAGNOSTICS_ERROR_IO_ERROR);
 }
 
-ssize_t read(int fd, void *buf, size_t nbytes)
-{
-       (void) fd;
-       (void) buf;
-       (void) nbytes;
-
-       return (int)mock();
-}
-
 static void test_diagnostics_data_read_p1(void **state) {
        (void) state;
        struct _diagnostics_data_s data;
@@ -119,8 +98,8 @@ static void test_diagnostics_data_read_p1(void **state) {
        int timeout = 100;
        size_t bytes_read = 0;
 
-       will_return(poll, 1);
-       will_return(read, 2);
+       will_return(__wrap_poll, 1);
+       will_return(__wrap_read, 2);
 
        assert_int_equal(diagnostics_data_read(&data, buf, count, timeout, &bytes_read), DIAGNOSTICS_ERROR_NONE);
        assert_int_equal(bytes_read, 2);
index f45c46ad23dd9905616ca071838704abb36a8e35..f1a0a6f7ea760b45ae14107481cda464999f9dd8 100644 (file)
@@ -59,22 +59,13 @@ static void test_diagnostics_get_data_n3(void **state) {
        destroy_ctx(br_ptr);
 }
 
-int dbus_get_file_from_report(const char *report_path, const int entry, int *fd)
-{
-       (void) report_path;
-       (void) entry;
-       *fd = 1;
-
-       return (int)mock();;
-}
-
 static void test_diagnostics_get_data_p1(void **state) {
        (void) state;
        struct _diagnostics_ctx_s *ctx = build_ctx_crash();
        struct _diagnostics_data_s *data = NULL;
        const char *params[] = {"cs_full"};
 
-       will_return(dbus_get_file_from_report, 0);
+       will_return(__wrap_dbus_get_file_from_report, 0);
 
        int result = diagnostics_get_data(ctx, params, 1, (void**)&data);
 
index f7df89f2b1f0f86aa7be8fa211038e907e629c17..cb175cc2ef837f1146227c8aff39015978e82820 100644 (file)
@@ -55,7 +55,7 @@ static void test_diagnostics_request_client_data_n3(void **state) {
        int params_size = 0;
        diagnostics_data_h data;
 
-       will_return(dumpsys_dump, DIAGNOSTICS_ERROR_INVALID_PARAMETER);
+       will_return(__wrap_dumpsys_dump, DIAGNOSTICS_ERROR_INVALID_PARAMETER);
 
        assert_int_equal(diagnostics_request_client_data(client_id, NULL, params_size, (void**)&data), DIAGNOSTICS_ERROR_IO_ERROR);
 }
@@ -69,18 +69,6 @@ static void test_diagnostics_request_client_data_n4(void **state) {
        assert_int_equal(diagnostics_request_client_data(client_id, params, params_size, NULL), DIAGNOSTICS_ERROR_INVALID_PARAMETER);
 }
 
-int dumpsys_dump(const char *service_name, int argc, const char **argv, int *out_fd)
-{
-       (void) service_name;
-       (void) argc;
-       (void) argv;
-       (void) out_fd;
-
-       *out_fd = 1;
-
-       return (int)mock();
-}
-
 static void test_diagnostics_request_client_data_n5(void **state) {
        (void) state;
        const char *client_id = "";
@@ -88,7 +76,7 @@ static void test_diagnostics_request_client_data_n5(void **state) {
        const char *params[] = {"a", "b", "c"};
        diagnostics_data_h data = NULL;
 
-       will_return(dumpsys_dump, TIZEN_ERROR_CONNECTION_REFUSED);
+       will_return(__wrap_dumpsys_dump, TIZEN_ERROR_CONNECTION_REFUSED);
 
        int result = diagnostics_request_client_data(client_id, params, params_size, (void**)&data);
 
@@ -103,7 +91,7 @@ static void test_diagnostics_request_client_data_p1(void **state) {
        const char *params[] = {"a", "b", "c"};
        struct _diagnostics_data_s *data = NULL;
 
-       will_return(dumpsys_dump, DIAGNOSTICS_ERROR_NONE);
+       will_return(__wrap_dumpsys_dump, DIAGNOSTICS_ERROR_NONE);
 
        int result = diagnostics_request_client_data(client_id, params, params_size, (void**)&data);
 
index 3d6e12c24569cc08f79d182cbd97ddca1c9f490a..af2d70c64c89b183fd62f12c00564ac89646ce9e 100644 (file)
@@ -42,22 +42,10 @@ static void test_diagnostics_set_notification_cb_n1(void **state) {
        assert_int_equal(diagnostics_set_notification_cb(NULL, NULL), DIAGNOSTICS_ERROR_INVALID_PARAMETER);
 }
 
-int dbus_subscribe(void (*signal_handler)(GDBusConnection *,
-                                                                                 const gchar *,
-                                                                                 const gchar *,
-                                                                                 const gchar *,
-                                                                                 const gchar *,
-                                                                                 GVariant *,
-                                                                                 gpointer))
-{
-       (void) signal_handler;
-       return (int)mock();
-}
-
 static void test_diagnostics_set_notification_cb_p1(void **state) {
        (void) state;
 
-       will_return(dbus_subscribe, DIAGNOSTICS_ERROR_NONE);
+       will_return(__wrap_dbus_subscribe, DIAGNOSTICS_ERROR_NONE);
 
        assert_int_equal(diagnostics_set_notification_cb(callback, NULL), DIAGNOSTICS_ERROR_NONE);
        assert_ptr_equal(cb_info.cb, callback);
@@ -69,7 +57,7 @@ static void test_diagnostics_set_notification_cb_p1(void **state) {
 static void test_diagnostics_set_notification_cb_p2(void **state) {
        (void) state;
 
-       will_return(dbus_subscribe, DIAGNOSTICS_ERROR_NONE);
+       will_return(__wrap_dbus_subscribe, DIAGNOSTICS_ERROR_NONE);
 
        assert_int_equal(diagnostics_set_notification_cb(callback, NULL), DIAGNOSTICS_ERROR_NONE);
        assert_int_equal(diagnostics_set_notification_cb(callback, NULL), DIAGNOSTICS_ERROR_RESOURCE_BUSY);