From: Konrad Kuchciak Date: Thu, 17 Dec 2020 08:31:59 +0000 (+0100) Subject: Adjust and add unit tests to the new APIs X-Git-Tag: submit/tizen/20210322.183501~2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=refs%2Fchanges%2F05%2F249905%2F18;p=platform%2Fcore%2Fapi%2Fdiagnostics.git Adjust and add unit tests to the new APIs Overall coverage rate: lines......: 90.2% (403 of 447 lines) functions..: 93.8% (30 of 32 functions) Change-Id: I320397ebec0da412d2f01fa6412111a6c8c633a6 Signed-off-by: Konrad Kuchciak --- diff --git a/CMakeLists.txt b/CMakeLists.txt index 7d41126..875ff22 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -20,7 +20,7 @@ ADD_SUBDIRECTORY(src/library) # cmocka tasts and coverage option(ENABLE_COVERAGE "Measure the coverage and generate report" OFF) -option(ENABLE_TESTS "Run unit tests after build" OFF) +option(ENABLE_TESTS "Run unit tests after build" ON) if(ENABLE_TESTS) FIND_PACKAGE(CMocka CONFIG REQUIRED) enable_testing() diff --git a/src/library/CMakeLists.txt b/src/library/CMakeLists.txt index 5124018..972c547 100644 --- a/src/library/CMakeLists.txt +++ b/src/library/CMakeLists.txt @@ -12,7 +12,7 @@ TARGET_LINK_LIBRARIES(${target} ${pkgs_LDFLAGS}) ADD_LIBRARY(${target}_static STATIC ${SOURCES}) TARGET_INCLUDE_DIRECTORIES(${target}_static PRIVATE ${CMAKE_SOURCE_DIR}/src/library) -# TARGET_COMPILE_OPTIONS(${target}_static BEFORE PRIVATE -include ../test/test_diagnostics_add_function_defs.h) +TARGET_COMPILE_OPTIONS(${target}_static BEFORE PRIVATE -include ../test/test_diagnostics_add_function_defs.h) TARGET_COMPILE_DEFINITIONS(${target}_static PUBLIC -DUNIT_TEST) if(ENABLE_COVERAGE) TARGET_COMPILE_OPTIONS(${target}_static PUBLIC -g -fprofile-arcs -ftest-coverage) diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt index b45083e..b8b74b0 100644 --- a/src/test/CMakeLists.txt +++ b/src/test/CMakeLists.txt @@ -15,17 +15,25 @@ function(add_mocked_test name wraps) ADD_DEPENDENCIES(ctest test_${name}) endfunction(add_mocked_test) -add_mocked_test(diagnostics_common "-Wl,--wrap=calloc,--wrap=system_info_get_platform_bool,--wrap=dumpsys_dump,--wrap=poll,--wrap=read,--wrap=g_bus_get_sync,--wrap=g_dbus_connection_send_message_with_reply_sync") -add_mocked_test(diagnostics_set_notification_cb "-Wl,--wrap=system_info_get_platform_bool,--wrap=g_bus_get_sync,--wrap=g_dbus_connection_signal_subscribe") -add_mocked_test(diagnostics_unset_notification_cb "-Wl,--wrap=system_info_get_platform_bool,--wrap=g_bus_get_sync,--wrap=g_dbus_connection_signal_subscribe,--wrap=g_dbus_connection_signal_unsubscribe") +add_mocked_test(diagnostics_common "-Wl,--wrap=calloc,--wrap=realloc,--wrap=strdup,--wrap=strndup,--wrap=system_info_get_platform_bool") +add_mocked_test(diagnostics_set_notification_cb "-Wl,--wrap=system_info_get_platform_bool") +add_mocked_test(diagnostics_unset_notification_cb "-Wl,--wrap=system_info_get_platform_bool") +add_mocked_test(diagnostics_set_data_request_cb "-Wl,--wrap=system_info_get_platform_bool,--wrap=aul_app_get_appid_bypid,--wrap=dumpsys_system_register_dump_cb") +add_mocked_test(diagnostics_unset_data_request_cb "-Wl,--wrap=system_info_get_platform_bool,--wrap=dumpsys_system_unregister_dump_cb") +add_mocked_test(diagnostics_subscribe_event "-Wl,--wrap=system_info_get_platform_bool,--wrap=aul_app_get_appid_bypid,--wrap=bundle_encode,--wrap=g_bus_get_sync,--wrap=g_dbus_connection_signal_subscribe,--wrap=g_dbus_connection_signal_unsubscribe") +add_mocked_test(diagnostics_send_event "-Wl,--wrap=system_info_get_platform_bool,--wrap=aul_app_get_appid_bypid,--wrap=bundle_encode,--wrap=g_bus_get_sync,--wrap=g_dbus_connection_emit_signal") add_mocked_test(diagnostics_request_client_data "-Wl,--wrap=system_info_get_platform_bool,--wrap=dumpsys_dump") +add_mocked_test(diagnostics_set_client_id "-Wl,--wrap=system_info_get_platform_bool,--wrap=aul_app_get_appid_bypid") 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=system_info_get_platform_bool,--wrap=g_bus_get_sync,--wrap=g_dbus_connection_send_message_with_reply_sync") +add_mocked_test(diagnostics_get_event_name "-Wl,--wrap=system_info_get_platform_bool") +add_mocked_test(diagnostics_get_event_data "-Wl,--wrap=system_info_get_platform_bool") +add_mocked_test(diagnostics_get_data "-Wl,--wrap=system_info_get_platform_bool,--wrap=dumpsys_dump") add_mocked_test(diagnostics_data_read "-Wl,--wrap=system_info_get_platform_bool,--wrap=poll,--wrap=read") +add_mocked_test(diagnostics_data_write "-Wl,--wrap=system_info_get_platform_bool,--wrap=write") add_mocked_test(diagnostics_create "") add_mocked_test(diagnostics_data_create "") add_mocked_test(diagnostics_signal_handler "") +add_mocked_test(diagnostics_dumpsys_handler "") add_mocked_test(diagnostics_destroy "") if(ENABLE_COVERAGE) diff --git a/src/test/test_diagnostics.h b/src/test/test_diagnostics.h index 956a3ec..e269627 100644 --- a/src/test/test_diagnostics.h +++ b/src/test/test_diagnostics.h @@ -17,23 +17,27 @@ #ifndef __TIZEN_TEST_DIAGNOSTICS_H__ #define __TIZEN_TEST_DIAGNOSTICS_H__ +#include #include -#include +#include +#include +#include +#include +#include +#include #include +#include +#include +#include +#include +#include +#include +#include -#include "signal.h" #include "dbus.h" -#define DBUS_SIGNAL_CRASH_APPID "org.tizen.multi-assistant-service" -#define DBUS_SIGNAL_ABNOR_APPID "top" -#define DBUS_SIGNAL_CRASH_COMM "org.tizen.multi" -#define DBUS_SIGNAL_CRASH_EXEC_PATH "/usr/apps/org.tizen.multi-assistant-service/bin/org.tizen.multi-assistant-service" -#define DBUS_SIGNAL_CRASH_PID 1589 -#define DBUS_SIGNAL_ABNOR_PID 10076 -#define DBUS_SIGNAL_CRASH_REPORT_PATH "/opt/usr/share/crash/dump//org.tizen.multi-assistant-service_1589_20190101181719.zip" -#define DBUS_SIGNAL_ABNOR_REPORT_PATH "/opt/usr/share/crash/livedump//top_10076_20190101183649.zip" -#define DBUS_SIGNAL_CRASH_SIGNAL 6 -#define DBUS_SIGNAL_ABNOR_IS_PROC_DEAD FALSE +#define TEST_CLIENT_ID "org.tizen.foo.bar" +#define TEST_EVENT_NAME "TizenOnFire" #ifdef __cplusplus extern "C" { @@ -41,12 +45,33 @@ extern "C" { struct _diagnostics_ctx_s { char *client_id; - struct dbus_signal_s *signal; - signal_type_e signal_type; + char *event_name; + bundle *event_data; }; struct _diagnostics_data_s { int fd; + char **params; + int params_size; +}; + +struct _diagnostics_event_s { + char *event_name; + char *client_id; + int subscription_id; +}; + +struct _diagnostics_notification_cb_info_s { + diagnostics_notification_cb cb; + void *user_data; + struct _diagnostics_event_s **events; + int events_count; +}; + +struct _dumpsys_cb_info_s { + diagnostics_request_cb cb; + void *user_data; + void *handler; }; int __wrap_system_info_get_platform_bool(const char *key, bool *value) @@ -67,12 +92,6 @@ int __wrap_dumpsys_dump(const char *service_name, int argc, const char **argv, i 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(); -} - GDBusConnection *__wrap_g_bus_get_sync(GBusType bus_type, GCancellable *cancellable, GError **error) { GDBusConnection *conn = mock_ptr_type(GDBusConnection *); @@ -102,6 +121,27 @@ void __wrap_g_dbus_connection_signal_unsubscribe(GDBusConnection *connection, gu return; } +gboolean __wrap_g_dbus_connection_emit_signal(GDBusConnection *connection, + const gchar *destination_bus_name, + const gchar *object_path, + const gchar *interface_name, + const gchar *signal_name, + GVariant *parameters, + GError **error) +{ + gboolean ret = (gboolean)mock(); + + if (ret == FALSE) + *error = g_error_new_literal(1, 1, ""); + + return ret; +} + +int __wrap_bundle_encode(bundle *b, bundle_raw **r, int *len) +{ + return (int)mock(); +} + GDBusMessage *__wrap_g_dbus_connection_send_message_with_reply_sync(GDBusConnection *connection, GDBusMessage *message, GDBusSendMessageFlags flags, @@ -141,51 +181,50 @@ ssize_t __wrap_read(int fd, void *buf, size_t nbytes) (void) buf; (void) nbytes; - return (int)mock(); + int ret = (int)mock(); + + if (ret < 0) + errno = -ret; + + return ret; } -GVariant *build_signal_parameters_crash() +ssize_t __wrap_write(int fd, const void *buf, size_t nbytes) { - GVariantBuilder builder; - - g_variant_builder_init(&builder, G_VARIANT_TYPE("(sssssiia{sv})")); - g_variant_builder_add(&builder, "s", "org.tizen.multi-assistant-xxx"); - g_variant_builder_add(&builder, "s", DBUS_SIGNAL_CRASH_EXEC_PATH); - g_variant_builder_add(&builder, "s", DBUS_SIGNAL_CRASH_APPID); - g_variant_builder_add(&builder, "s", "org.tizen.multi-yyy-xxx"); - g_variant_builder_add(&builder, "s", DBUS_SIGNAL_CRASH_REPORT_PATH); - g_variant_builder_add(&builder, "i", DBUS_SIGNAL_CRASH_PID); - g_variant_builder_add(&builder, "i", 1589); - g_variant_builder_open(&builder, G_VARIANT_TYPE("a{sv}")); - g_variant_builder_add(&builder, "{sv}", SIG_CRASH_EX_SYSSIGNAL, g_variant_new_int32(DBUS_SIGNAL_CRASH_SIGNAL)); - g_variant_builder_add(&builder, "{sv}", SIG_CRASH_EX_SYSTIDCOMM, g_variant_new_string(DBUS_SIGNAL_CRASH_COMM)); - g_variant_builder_add(&builder, "{sv}", SIG_CRASH_EX_ARMPC, g_variant_new_int32(3055767608)); - g_variant_builder_add(&builder, "{sv}", SIG_CRASH_EX_ARMLR, g_variant_new_int32(0)); - g_variant_builder_close(&builder); - - return g_variant_builder_end(&builder); + (void) fd; + (void) buf; + (void) nbytes; + + int ret = (int)mock(); + + if (ret < 0) + errno = -ret; + + return ret; } -struct _diagnostics_ctx_s *build_ctx_crash() +int __wrap_aul_app_get_appid_bypid(int pid, char *appid, int len) { - struct _diagnostics_ctx_s *ctx = calloc(1, sizeof(struct _diagnostics_ctx_s)); - ctx->signal = calloc(1, sizeof(struct dbus_signal_s)); + (void) pid; + (void) appid; + (void) len; - ctx->signal_type = SIG_TYPE_CRASH; - ctx->signal->signal_name = g_strdup(DBUS_MEMBER_CRASH); - ctx->signal->sender_name = g_strdup(DBUS_SENDER_CRASH); - ctx->signal->parameters = build_signal_parameters_crash(); + int ret = (int)mock(); - ctx->client_id = ctx->signal->sender_name; + if (ret == 0) + snprintf(appid, len, "%s", TEST_CLIENT_ID); - return ctx; + return ret; } -void destroy_ctx(struct _diagnostics_ctx_s *ctx) +int __wrap_dumpsys_system_register_dump_cb(dumpsys_system_dump_cb callback, const char *name, void **handler) { - g_variant_unref(ctx->signal->parameters); - dbus_signal_cleanup(ctx->signal); - free(ctx); + return (int)mock(); +} + +int __wrap_dumpsys_system_unregister_dump_cb(void *handler) +{ + return (int)mock(); } #ifdef __cplusplus diff --git a/src/test/test_diagnostics_add_function_defs.h b/src/test/test_diagnostics_add_function_defs.h index 041d7d2..1acd9bd 100644 --- a/src/test/test_diagnostics_add_function_defs.h +++ b/src/test/test_diagnostics_add_function_defs.h @@ -18,27 +18,33 @@ #define __TIZEN_TEST_DIAGNOSTICS_ADD_FUNCTION_DEFS_H__ #include "diagnostics.h" - #include "dbus.h" #define STATIC +#define MAX_CLIENT_ID_LEN 255 +#define MAX_INT_DIGITS 11 #ifdef __cplusplus extern "C" { -# endif +#endif -extern struct _diagnostics_cb_info_s cb_info; +extern struct _diagnostics_notification_cb_info_s notification_cb_info; +extern struct _dumpsys_cb_info_s dumpsys_cb_info; +extern char *magic_token; +extern char app_id[MAX_CLIENT_ID_LEN + 1]; +extern char service_name[MAX_CLIENT_ID_LEN + 1]; -int get_report_path(diagnostics_ctx_h ctx, const char **path, size_t *len); -struct _diagnostics_ctx_s *diagnostics_create(struct dbus_signal_s *signal); -struct _diagnostics_data_s *diagnostics_data_create(int fd); +struct _diagnostics_ctx_s *diagnostics_create(const char *client_id, const char *event_name, bundle *event_data); +struct _diagnostics_ctx_s *diagnostics_create_from_params(char **params, int params_size); +struct _diagnostics_data_s *diagnostics_data_create(int fd, char **params, int params_size); void signal_handler(GDBusConnection *connection, - const gchar *sender_name, - const gchar *object_path, - const gchar *interface_name, - const gchar *signal_name, - GVariant *parameters, - gpointer user_data); + const gchar *sender_name, + const gchar *object_path, + const gchar *interface_name, + const gchar *signal_name, + GVariant *parameters, + gpointer user_data); +int dumpsys_handler(const int fd, const int argc, char **argv); #ifdef __cplusplus } diff --git a/src/test/test_diagnostics_common.c b/src/test/test_diagnostics_common.c index 7729c66..a72c78d 100644 --- a/src/test/test_diagnostics_common.c +++ b/src/test/test_diagnostics_common.c @@ -14,136 +14,171 @@ * limitations under the License. */ -#include -#include -#include -#include - #include "diagnostics.h" -#include -#include - -#include - #include "test_diagnostics.h" #include "test_diagnostics_add_function_defs.h" -struct _diagnostics_cb_info_s { - diagnostics_notification_cb cb; - void *user_data; -}; +void *__real_calloc(size_t nmemb, size_t size); +void *__wrap_calloc(size_t nmemb, size_t size) +{ + int fake = (int) mock(); + if (fake) + return NULL; -struct _diagnostics_ctx_s *ctx = NULL; + return __real_calloc(nmemb, size); +} -void signal_callback(void *_ctx, void *user_data) +void *__real_realloc(void *ptr, size_t size); +void *__wrap_realloc(void *ptr, size_t size) { - ctx = _ctx; + int fake = (int) mock(); + if (fake) + return NULL; - assert_ptr_equal(user_data, NULL); + return __real_realloc(ptr, size); } -void *__real_calloc(size_t nmemb, size_t size); -void *__wrap_calloc(size_t nmemb, size_t size) +char *__real_strdup(const char *s); +char *__wrap_strdup(const char *s) { int fake = (int) mock(); if (fake) return NULL; - return __real_calloc(nmemb, size); + return __real_strdup(s); } +char *__real_strndup(const char *s); +char *__wrap_strndup(const char *s) +{ + int fake = (int) mock(); + if (fake) + return NULL; + + return __real_strndup(s); +} + +/* + * diagnostics_create() memory allocation fail + */ + static void test_diagnostics_common_n1(void **state) { (void) state; - GVariant *parameters = build_signal_parameters_crash(); - - cb_info.cb = signal_callback; - cb_info.user_data = NULL; will_return(__wrap_calloc, 1); - signal_handler(NULL, DBUS_SENDER_CRASH, DBUS_OBJECT_PATH, DBUS_INTERFACE_NAME, DBUS_MEMBER_CRASH, parameters, NULL); - assert_null(ctx); - - g_variant_unref(parameters); + assert_null(diagnostics_create(TEST_CLIENT_ID, TEST_EVENT_NAME, NULL)); } static void test_diagnostics_common_n2(void **state) { (void) state; - struct dbus_signal_s *signal; - struct _diagnostics_ctx_s *ctx; will_return(__wrap_calloc, 0); - will_return(__wrap_calloc, 1); + will_return(__wrap_strndup, 1); - signal = dbus_signal_create(DBUS_SENDER_CRASH, DBUS_OBJECT_PATH, DBUS_INTERFACE_NAME, DBUS_MEMBER_CRASH, build_signal_parameters_crash()); - assert_non_null(signal); + assert_null(diagnostics_create(TEST_CLIENT_ID, TEST_EVENT_NAME, NULL)); +} - ctx = diagnostics_create(signal); - assert_null(ctx); +static void test_diagnostics_common_n3(void **state) +{ + (void) state; - dbus_signal_cleanup(signal); + will_return(__wrap_calloc, 0); + will_return(__wrap_strndup, 0); + will_return(__wrap_strndup, 1); + + assert_null(diagnostics_create(TEST_CLIENT_ID, TEST_EVENT_NAME, NULL)); } -static void test_diagnostics_common_n3(void **state) +/* + * diagnostics_data_create() memory allocation fail + */ + +static void test_diagnostics_common_n4(void **state) { (void) state; int fd = 0; + char *params[1] = {"foo"}; + int params_size = 1; will_return(__wrap_calloc, 1); - assert_null(diagnostics_data_create(fd)); + + assert_null(diagnostics_data_create(fd, params, params_size)); } -static void test_diagnostics_common_n4(void **state) +static void test_diagnostics_common_n5(void **state) { (void) state; - const char *client_id = ""; - int params_size = 3; - const char *params[] = {"a", "b", "c"}; - struct _diagnostics_data_s *data = NULL; - int ret; + int fd = 0; + char *params[1] = {"foo"}; + int params_size = 1; - will_return(__wrap_dumpsys_dump, DIAGNOSTICS_ERROR_NONE); + will_return(__wrap_calloc, 0); will_return(__wrap_calloc, 1); - ret = diagnostics_request_client_data(client_id, params, params_size, (void**)&data); + assert_null(diagnostics_data_create(fd, params, params_size)); +} + +static void test_diagnostics_common_n6(void **state) +{ + (void) state; + int fd = 0; + char *params[1] = {"foo"}; + int params_size = 1; + + will_return(__wrap_calloc, 0); + will_return(__wrap_calloc, 0); + will_return(__wrap_strdup, 1); - assert_int_equal(ret, DIAGNOSTICS_ERROR_OUT_OF_MEMORY); - assert_null(data); + assert_null(diagnostics_data_create(fd, params, params_size)); } -static void test_diagnostics_common_n5(void **state) +/* + * diagnostics_subscribe_event() memory allocation fail + */ + +static void test_diagnostics_common_n7(void **state) { (void) state; - struct _diagnostics_ctx_s *ctx = NULL; - struct _diagnostics_data_s *data = NULL; - const char *params[] = {"cs_full"}; - static int dummy = 1; - GDBusConnection *conn = (GDBusConnection *)&dummy; - GDBusMessage *reply; - GUnixFDList *fd_list; - int fd = 1; - int ret; - - will_return_count(__wrap_calloc, 0, 2); - - ctx = build_ctx_crash(); - reply = g_dbus_message_new(); - fd_list = g_unix_fd_list_new_from_array(&fd, 1); - g_dbus_message_set_message_type(reply, G_DBUS_MESSAGE_TYPE_METHOD_RETURN); - g_dbus_message_set_unix_fd_list(reply, fd_list); - - will_return(__wrap_g_bus_get_sync, conn); - will_return(__wrap_g_dbus_connection_send_message_with_reply_sync, reply); + will_return(__wrap_calloc, 1); - ret = diagnostics_get_data(ctx, params, 1, (void**)&data); + assert_int_equal(diagnostics_subscribe_event(TEST_EVENT_NAME, TEST_CLIENT_ID), DIAGNOSTICS_ERROR_OUT_OF_MEMORY); +} + +static void test_diagnostics_common_n8(void **state) +{ + (void) state; + + will_return(__wrap_calloc, 0); + will_return(__wrap_strndup, 1); + + assert_int_equal(diagnostics_subscribe_event(TEST_EVENT_NAME, TEST_CLIENT_ID), DIAGNOSTICS_ERROR_OUT_OF_MEMORY); +} - assert_int_equal(ret, DIAGNOSTICS_ERROR_OUT_OF_MEMORY); - assert_null(data); +static void test_diagnostics_common_n9(void **state) +{ + (void) state; + + will_return(__wrap_calloc, 0); + will_return(__wrap_strndup, 0); + will_return(__wrap_strndup, 1); + + assert_int_equal(diagnostics_subscribe_event(TEST_EVENT_NAME, TEST_CLIENT_ID), DIAGNOSTICS_ERROR_OUT_OF_MEMORY); +} + +static void test_diagnostics_common_n10(void **state) +{ + (void) state; + + will_return(__wrap_calloc, 0); + will_return(__wrap_strndup, 0); + will_return(__wrap_strndup, 0); + will_return(__wrap_realloc, 1); - destroy_ctx(ctx); + assert_int_equal(diagnostics_subscribe_event(TEST_EVENT_NAME, TEST_CLIENT_ID), DIAGNOSTICS_ERROR_OUT_OF_MEMORY); } int main(void) @@ -154,6 +189,11 @@ int main(void) cmocka_unit_test(test_diagnostics_common_n3), cmocka_unit_test(test_diagnostics_common_n4), cmocka_unit_test(test_diagnostics_common_n5), + cmocka_unit_test(test_diagnostics_common_n6), + cmocka_unit_test(test_diagnostics_common_n7), + cmocka_unit_test(test_diagnostics_common_n8), + cmocka_unit_test(test_diagnostics_common_n9), + cmocka_unit_test(test_diagnostics_common_n10), }; return cmocka_run_group_tests(tests, NULL, NULL); } \ No newline at end of file diff --git a/src/test/test_diagnostics_create.c b/src/test/test_diagnostics_create.c index 58b0491..8054410 100644 --- a/src/test/test_diagnostics_create.c +++ b/src/test/test_diagnostics_create.c @@ -14,60 +14,97 @@ * limitations under the License. */ -#include -#include -#include -#include - #include "diagnostics.h" -#include -#include - -#include - #include "test_diagnostics.h" #include "test_diagnostics_add_function_defs.h" static void test_diagnostics_create_n1(void **state) { (void) state; - struct dbus_signal_s *signal = NULL; - assert_null(diagnostics_create(signal)); + assert_null(diagnostics_create(NULL, TEST_EVENT_NAME, NULL)); } static void test_diagnostics_create_n2(void **state) { (void) state; - struct dbus_signal_s *signal; - struct _diagnostics_ctx_s *ctx; - signal = dbus_signal_create(DBUS_SENDER_CRASH, DBUS_OBJECT_PATH, DBUS_INTERFACE_NAME, "", build_signal_parameters_crash()); - assert_non_null(signal); + assert_null(diagnostics_create(TEST_CLIENT_ID, NULL, NULL)); +} + +static void test_diagnostics_create_n3(void **state) +{ + (void) state; + char **params = NULL; + int params_size = 0; + + assert_null(diagnostics_create_from_params(params, params_size)); +} - ctx = diagnostics_create(signal); - assert_null(ctx); +static void test_diagnostics_create_n4(void **state) +{ + (void) state; + char *params[1] = {"foo"}; + int params_size = 1; - dbus_signal_cleanup(signal); + assert_null(diagnostics_create_from_params(params, params_size)); } static void test_diagnostics_create_p1(void **state) { (void) state; - struct dbus_signal_s *signal; struct _diagnostics_ctx_s *ctx; - signal = dbus_signal_create(DBUS_SENDER_CRASH, DBUS_OBJECT_PATH, DBUS_INTERFACE_NAME, DBUS_MEMBER_CRASH, build_signal_parameters_crash()); - assert_non_null(signal); + ctx = diagnostics_create(TEST_CLIENT_ID, TEST_EVENT_NAME, NULL); + assert_non_null(ctx); + assert_string_equal(ctx->client_id, TEST_CLIENT_ID); + assert_string_equal(ctx->event_name, TEST_EVENT_NAME); + assert_ptr_equal(ctx->event_data, NULL); + + diagnostics_destroy(ctx); +} - ctx = diagnostics_create(signal); +static void test_diagnostics_create_p2(void **state) +{ + (void) state; + struct _diagnostics_ctx_s *ctx; + bundle *event_data = bundle_create(); + bundle_raw *event_data_raw = NULL; + int event_data_len = 0; + char len_str[MAX_INT_DIGITS] = {0,}; + char *params[6]; + int params_size = 6; + char *val = NULL; + + params[0] = "foo"; + params[1] = magic_token; + params[2] = TEST_CLIENT_ID; + params[3] = TEST_EVENT_NAME; + + /* Add some event data */ + bundle_add_str(event_data, "key1", "val1"); + assert_int_equal(bundle_encode(event_data, &event_data_raw, &event_data_len), BUNDLE_ERROR_NONE); + assert_string_not_equal((char *)event_data_raw, ""); + params[4] = (char *) event_data_raw; + + snprintf(len_str, MAX_INT_DIGITS, "%d", event_data_len); + assert_string_not_equal(len_str, ""); + params[5] = len_str; + + /* Create and verify ctx */ + ctx = diagnostics_create_from_params(params, params_size); assert_non_null(ctx); - assert_string_equal(ctx->client_id, DBUS_SENDER_CRASH); - assert_ptr_equal(ctx->signal, ctx->signal); - assert_int_equal(ctx->signal_type, SIG_TYPE_CRASH); + assert_string_equal(ctx->client_id, TEST_CLIENT_ID); + assert_string_equal(ctx->event_name, TEST_EVENT_NAME); + assert_non_null(ctx->event_data); + + /* Verify event data */ + bundle_get_str(ctx->event_data, "key1", &val); + assert_string_equal(val, "val1"); - dbus_signal_cleanup(signal); - free(ctx); + free(event_data_raw); + bundle_free(event_data); + diagnostics_destroy(ctx); } int main(void) @@ -75,7 +112,10 @@ int main(void) const struct CMUnitTest tests[] = { cmocka_unit_test(test_diagnostics_create_n1), cmocka_unit_test(test_diagnostics_create_n2), + cmocka_unit_test(test_diagnostics_create_n3), + cmocka_unit_test(test_diagnostics_create_n4), cmocka_unit_test(test_diagnostics_create_p1), + cmocka_unit_test(test_diagnostics_create_p2), }; return cmocka_run_group_tests(tests, NULL, NULL); } diff --git a/src/test/test_diagnostics_data_create.c b/src/test/test_diagnostics_data_create.c index 7ce3e4c..e318362 100644 --- a/src/test/test_diagnostics_data_create.c +++ b/src/test/test_diagnostics_data_create.c @@ -14,17 +14,7 @@ * limitations under the License. */ -#include -#include -#include -#include - #include "diagnostics.h" -#include -#include - -#include - #include "test_diagnostics.h" #include "test_diagnostics_add_function_defs.h" @@ -32,26 +22,56 @@ static void test_diagnostics_data_create_n1(void **state) { (void) state; int fd = -1; + char **params = NULL; + int params_size = 0; + + assert_null(diagnostics_data_create(fd, params, params_size)); +} + +static void test_diagnostics_data_create_n2(void **state) +{ + (void) state; + int fd = 0; + char **params = NULL; + int params_size = 1; + + assert_null(diagnostics_data_create(fd, params, params_size)); +} + +static void test_diagnostics_data_create_n3(void **state) +{ + (void) state; + int fd = 0; + char *params[1] = {"foo"}; + int params_size = -1; - assert_null(diagnostics_data_create(fd)); + assert_null(diagnostics_data_create(fd, params, params_size)); } static void test_diagnostics_data_create_p1(void **state) { (void) state; int fd = 0; - struct _diagnostics_data_s *result = diagnostics_data_create(fd); + char *params[1] = {"foo"}; + int params_size = 1; + struct _diagnostics_data_s *data = diagnostics_data_create(fd, params, params_size); + + assert_non_null(data); + assert_int_equal(data->fd, fd); + assert_int_equal(data->params_size, params_size); - assert_non_null(result); - assert_int_equal(result->fd, fd); + for (int i = 0; i < params_size; i++) + assert_string_equal(data->params[i], params[i]); - free(result); + diagnostics_data_destroy(data); } int main(void) { const struct CMUnitTest tests[] = { cmocka_unit_test(test_diagnostics_data_create_n1), + cmocka_unit_test(test_diagnostics_data_create_n2), + cmocka_unit_test(test_diagnostics_data_create_n3), cmocka_unit_test(test_diagnostics_data_create_p1), }; return cmocka_run_group_tests(tests, NULL, NULL); diff --git a/src/test/test_diagnostics_data_read.c b/src/test/test_diagnostics_data_read.c index 4bb547d..f306d97 100644 --- a/src/test/test_diagnostics_data_read.c +++ b/src/test/test_diagnostics_data_read.c @@ -14,21 +14,9 @@ * limitations under the License. */ -#include -#include -#include -#include - #include "diagnostics.h" -#include -#include - -#include "signal.h" -#include "dbus.h" - -#include - #include "test_diagnostics.h" +#include "test_diagnostics_add_function_defs.h" static void test_diagnostics_data_read_n1(void **state) { @@ -53,7 +41,7 @@ static void test_diagnostics_data_read_n2(void **state) assert_int_equal(diagnostics_data_read(&data, NULL, count, timeout, &bytes_read), DIAGNOSTICS_ERROR_INVALID_PARAMETER); } -static void test_diagnostics_data_read_n4(void **state) +static void test_diagnostics_data_read_n3(void **state) { (void) state; struct _diagnostics_data_s data; @@ -64,7 +52,7 @@ static void test_diagnostics_data_read_n4(void **state) assert_int_equal(diagnostics_data_read(&data, buf, count, timeout, NULL), DIAGNOSTICS_ERROR_INVALID_PARAMETER); } -static void test_diagnostics_data_read_n5(void **state) +static void test_diagnostics_data_read_n4(void **state) { (void) state; struct _diagnostics_data_s data; @@ -79,7 +67,7 @@ static void test_diagnostics_data_read_n5(void **state) assert_int_equal(diagnostics_data_read(&data, buf, count, timeout, &bytes_read), DIAGNOSTICS_ERROR_TIMED_OUT); } -static void test_diagnostics_data_read_n6(void **state) +static void test_diagnostics_data_read_n5(void **state) { (void) state; struct _diagnostics_data_s data; @@ -94,7 +82,7 @@ static void test_diagnostics_data_read_n6(void **state) assert_int_equal(diagnostics_data_read(&data, buf, count, timeout, &bytes_read), DIAGNOSTICS_ERROR_IO_ERROR); } -static void test_diagnostics_data_read_n7(void **state) +static void test_diagnostics_data_read_n6(void **state) { (void) state; struct _diagnostics_data_s data; @@ -111,6 +99,23 @@ static void test_diagnostics_data_read_n7(void **state) assert_int_equal(bytes_read, 0); } +static void test_diagnostics_data_read_n7(void **state) +{ + (void) state; + struct _diagnostics_data_s data; + data.fd = 0; + char buf[256]; + size_t count = sizeof(buf); + int timeout = 100; + size_t bytes_read = 0; + + will_return(__wrap_poll, POLLIN); + will_return(__wrap_read, -EAGAIN); + + assert_int_equal(diagnostics_data_read(&data, buf, count, timeout, &bytes_read), DIAGNOSTICS_ERROR_TRY_AGAIN); + assert_int_equal(bytes_read, 0); +} + static void test_diagnostics_data_read_n8(void **state) { (void) state; @@ -165,6 +170,7 @@ int main(void) const struct CMUnitTest tests[] = { cmocka_unit_test(test_diagnostics_data_read_n1), cmocka_unit_test(test_diagnostics_data_read_n2), + cmocka_unit_test(test_diagnostics_data_read_n3), cmocka_unit_test(test_diagnostics_data_read_n4), cmocka_unit_test(test_diagnostics_data_read_n5), cmocka_unit_test(test_diagnostics_data_read_n6), diff --git a/src/test/test_diagnostics_data_write.c b/src/test/test_diagnostics_data_write.c new file mode 100644 index 0000000..5693881 --- /dev/null +++ b/src/test/test_diagnostics_data_write.c @@ -0,0 +1,108 @@ +/* + * Copyright (c) 2020 Samsung Electronics Co., Ltd. + * + * 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 "diagnostics.h" +#include "test_diagnostics.h" +#include "test_diagnostics_add_function_defs.h" + +static void test_diagnostics_data_write_n1(void **state) +{ + (void) state; + char buf[256]; + size_t count = sizeof(buf); + size_t bytes_written = 0; + + assert_int_equal(diagnostics_data_write(NULL, buf, count, &bytes_written), DIAGNOSTICS_ERROR_INVALID_PARAMETER); +} + +static void test_diagnostics_data_write_n2(void **state) +{ + (void) state; + struct _diagnostics_data_s data; + char buf[256]; + size_t count = sizeof(buf); + size_t bytes_written = 0; + + assert_int_equal(diagnostics_data_write(&data, NULL, count, &bytes_written), DIAGNOSTICS_ERROR_INVALID_PARAMETER); +} + +static void test_diagnostics_data_write_n3(void **state) +{ + (void) state; + struct _diagnostics_data_s data; + char buf[256]; + size_t count = sizeof(buf); + + assert_int_equal(diagnostics_data_write(&data, buf, count, NULL), DIAGNOSTICS_ERROR_INVALID_PARAMETER); +} + +static void test_diagnostics_data_write_n4(void **state) +{ + (void) state; + struct _diagnostics_data_s data; + data.fd = 0; + char buf[256]; + size_t count = sizeof(buf); + size_t bytes_written = 0; + + will_return(__wrap_write, -1); + + assert_int_equal(diagnostics_data_write(&data, buf, count, &bytes_written), DIAGNOSTICS_ERROR_IO_ERROR); + assert_int_equal(bytes_written, 0); +} + +static void test_diagnostics_data_write_n5(void **state) +{ + (void) state; + struct _diagnostics_data_s data; + data.fd = 0; + char buf[256]; + size_t count = sizeof(buf); + size_t bytes_written = 0; + + will_return(__wrap_write, -EAGAIN); + + assert_int_equal(diagnostics_data_write(&data, buf, count, &bytes_written), DIAGNOSTICS_ERROR_TRY_AGAIN); + assert_int_equal(bytes_written, 0); +} + +static void test_diagnostics_data_write_p1(void **state) +{ + (void) state; + struct _diagnostics_data_s data; + data.fd = 0; + char buf[256]; + size_t count = sizeof(buf); + size_t bytes_written = 0; + + will_return(__wrap_write, 2); + + assert_int_equal(diagnostics_data_write(&data, buf, count, &bytes_written), DIAGNOSTICS_ERROR_NONE); + assert_int_equal(bytes_written, 2); +} + +int main(void) +{ + const struct CMUnitTest tests[] = { + cmocka_unit_test(test_diagnostics_data_write_n1), + cmocka_unit_test(test_diagnostics_data_write_n2), + cmocka_unit_test(test_diagnostics_data_write_n3), + cmocka_unit_test(test_diagnostics_data_write_n4), + cmocka_unit_test(test_diagnostics_data_write_n5), + cmocka_unit_test(test_diagnostics_data_write_p1), + }; + return cmocka_run_group_tests(tests, NULL, NULL); +} diff --git a/src/test/test_diagnostics_destroy.c b/src/test/test_diagnostics_destroy.c index d0eff54..aae884d 100644 --- a/src/test/test_diagnostics_destroy.c +++ b/src/test/test_diagnostics_destroy.c @@ -14,21 +14,11 @@ * limitations under the License. */ -#include -#include -#include -#include - -#include "diagnostics.h" -#include -#include - -#include "signal.h" -#include "dbus.h" - #include +#include "diagnostics.h" #include "test_diagnostics.h" +#include "test_diagnostics_add_function_defs.h" static void test_diagnostics_destroy_n1(void **state) { @@ -51,7 +41,7 @@ static void test_diagnostics_data_destroy_n1(void **state) static void test_diagnostics_destroy_p1(void **state) { (void) state; - diagnostics_ctx_h ctx = build_ctx_crash(); + struct _diagnostics_ctx_s *ctx = diagnostics_create("client_id", "event_name", NULL); assert_int_equal(diagnostics_destroy(ctx), DIAGNOSTICS_ERROR_NONE); } @@ -59,7 +49,7 @@ static void test_diagnostics_destroy_p1(void **state) static void test_diagnostics_data_destroy_p2(void **state) { (void) state; - diagnostics_data_h data = calloc(1, sizeof(struct _diagnostics_data_s)); + struct _diagnostics_data_s *data = diagnostics_data_create(1, NULL, 0); assert_int_equal(diagnostics_data_destroy(data), DIAGNOSTICS_ERROR_NONE); } diff --git a/src/test/test_diagnostics_dumpsys_handler.c b/src/test/test_diagnostics_dumpsys_handler.c new file mode 100644 index 0000000..18e94a1 --- /dev/null +++ b/src/test/test_diagnostics_dumpsys_handler.c @@ -0,0 +1,79 @@ +/* + * Copyright (c) 2020 Samsung Electronics Co., Ltd. + * + * 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 "diagnostics.h" +#include "test_diagnostics.h" +#include "test_diagnostics_add_function_defs.h" + +void callback(diagnostics_data_h data, char **params, int params_size, diagnostics_ctx_h ctx, void *user_data) +{ + (void) data; + (void) params; + (void) params_size; + (void) ctx; + (void) user_data; + + assert_ptr_equal(user_data, NULL); +} + +static void test_diagnostics_dumpsys_handler_n1(void **state) +{ + (void) state; + int fd = 0; + int argc = 0; + char **argv = NULL; + + dumpsys_cb_info.cb = NULL; + dumpsys_cb_info.user_data = NULL; + + assert_int_equal(dumpsys_handler(fd, argc, argv), -1); +} + +static void test_diagnostics_dumpsys_handler_n2(void **state) +{ + (void) state; + int fd = -1; + int argc = 0; + char **argv = NULL; + + dumpsys_cb_info.cb = callback; + dumpsys_cb_info.user_data = NULL; + + assert_int_equal(dumpsys_handler(fd, argc, argv), -1); +} + +static void test_diagnostics_dumpsys_handler_p1(void **state) +{ + (void) state; + int fd = 0; + int argc = 0; + char **argv = NULL; + + dumpsys_cb_info.cb = callback; + dumpsys_cb_info.user_data = NULL; + + assert_int_equal(dumpsys_handler(fd, argc, argv), 0); +} + +int main(void) +{ + const struct CMUnitTest tests[] = { + cmocka_unit_test(test_diagnostics_dumpsys_handler_n1), + cmocka_unit_test(test_diagnostics_dumpsys_handler_n2), + cmocka_unit_test(test_diagnostics_dumpsys_handler_p1), + }; + return cmocka_run_group_tests(tests, NULL, NULL); +} diff --git a/src/test/test_diagnostics_get_client_id.c b/src/test/test_diagnostics_get_client_id.c index 5c31661..372805b 100644 --- a/src/test/test_diagnostics_get_client_id.c +++ b/src/test/test_diagnostics_get_client_id.c @@ -14,19 +14,9 @@ * limitations under the License. */ -#include -#include -#include -#include - #include "diagnostics.h" -#include -#include - -#include "signal.h" -#include "dbus.h" - #include "test_diagnostics.h" +#include "test_diagnostics_add_function_defs.h" static void test_diagnostics_get_client_id_n1(void **state) { @@ -39,24 +29,26 @@ static void test_diagnostics_get_client_id_n1(void **state) static void test_diagnostics_get_client_id_n2(void **state) { (void) state; - struct _diagnostics_ctx_s *ctx = build_ctx_crash(); + struct _diagnostics_ctx_s *ctx = diagnostics_create(TEST_CLIENT_ID, TEST_EVENT_NAME, NULL); assert_int_equal(diagnostics_get_client_id(ctx, NULL), DIAGNOSTICS_ERROR_INVALID_PARAMETER); - destroy_ctx(ctx); + diagnostics_destroy(ctx); } static void test_diagnostics_get_client_id_p1(void **state) { (void) state; - diagnostics_ctx_h ctx = build_ctx_crash(); + struct _diagnostics_ctx_s *ctx = diagnostics_create(TEST_CLIENT_ID, TEST_EVENT_NAME, NULL); char *client_id; - int result = diagnostics_get_client_id(ctx, &client_id); + int ret; - assert_int_equal(result, DIAGNOSTICS_ERROR_NONE); - assert_string_equal(client_id, DBUS_SENDER_CRASH); + ret = diagnostics_get_client_id(ctx, &client_id); + assert_int_equal(ret, DIAGNOSTICS_ERROR_NONE); + assert_string_equal(client_id, TEST_CLIENT_ID); - destroy_ctx(ctx); + free(client_id); + diagnostics_destroy(ctx); } int main(void) diff --git a/src/test/test_diagnostics_get_data.c b/src/test/test_diagnostics_get_data.c index 9a88098..cc23e7d 100644 --- a/src/test/test_diagnostics_get_data.c +++ b/src/test/test_diagnostics_get_data.c @@ -14,179 +14,39 @@ * limitations under the License. */ -#include -#include -#include -#include - #include "diagnostics.h" -#include -#include - -#include "signal.h" -#include "dbus.h" - -#include -#include -#include -#include - #include "test_diagnostics.h" - -static int dummy = 1; -GDBusConnection *conn = (GDBusConnection *)&dummy; +#include "test_diagnostics_add_function_defs.h" static void test_diagnostics_get_data_n1(void **state) { (void) state; - diagnostics_data_h data; - const char *params[] = {"cs_full"}; - - assert_int_equal(diagnostics_get_data(NULL, params, 1, (void**)&data), DIAGNOSTICS_ERROR_INVALID_PARAMETER); -} - -static void test_diagnostics_get_data_n2(void **state) -{ - (void) state; - diagnostics_ctx_h ctx = build_ctx_crash(); - diagnostics_data_h data; - const char *params[] = {"cs_full"}; - - assert_int_equal(diagnostics_get_data(ctx, params, -1, (void**)&data), DIAGNOSTICS_ERROR_INVALID_PARAMETER); - - destroy_ctx(ctx); -} - -static void test_diagnostics_get_data_n3(void **state) -{ - (void) state; - diagnostics_ctx_h ctx = build_ctx_crash(); - const char *params[] = {"cs_full"}; - - assert_int_equal(diagnostics_get_data(ctx, params, 1, NULL), DIAGNOSTICS_ERROR_INVALID_PARAMETER); - - destroy_ctx(ctx); -} - -static void test_diagnostics_get_data_n4(void **state) -{ - (void) state; - struct _diagnostics_ctx_s *ctx = build_ctx_crash(); - struct _diagnostics_data_s *data = NULL; - const char *params[] = {"cs_foo"}; - - assert_int_equal(diagnostics_get_data(ctx, params, 1, (void**)&data), DIAGNOSTICS_ERROR_NOT_SUPPORTED); - assert_null(data); - - destroy_ctx(ctx); -} - -static void test_diagnostics_get_data_n5(void **state) -{ - (void) state; - struct _diagnostics_ctx_s *ctx = build_ctx_crash(); - struct _diagnostics_data_s *data = NULL; - const char *params[] = {"cs_full"}; - GDBusConnection *conn = NULL; + diagnostics_data_h data = NULL; - will_return(__wrap_g_bus_get_sync, conn); - - assert_int_equal(diagnostics_get_data(ctx, params, 1, (void**)&data), DIAGNOSTICS_ERROR_IO_ERROR); + assert_int_equal(diagnostics_get_data(NULL, NULL, 0, &data), DIAGNOSTICS_ERROR_INVALID_PARAMETER); assert_null(data); - - destroy_ctx(ctx); -} - -static void test_diagnostics_get_data_n6(void **state) -{ - (void) state; - struct _diagnostics_ctx_s *ctx = build_ctx_crash(); - struct _diagnostics_data_s *data = NULL; - const char *params[] = {"cs_full"}; - GDBusMessage *reply = NULL; - - will_return(__wrap_g_bus_get_sync, conn); - will_return(__wrap_g_dbus_connection_send_message_with_reply_sync, reply); - - assert_int_equal(diagnostics_get_data(ctx, params, 1, (void**)&data), DIAGNOSTICS_ERROR_IO_ERROR); - assert_null(data); - - destroy_ctx(ctx); -} - -static void test_diagnostics_get_data_n7(void **state) -{ - (void) state; - struct _diagnostics_ctx_s *ctx = build_ctx_crash(); - struct _diagnostics_data_s *data = NULL; - const char *params[] = {"cs_full"}; - GDBusMessage *reply = g_dbus_message_new(); - - g_dbus_message_set_message_type(reply, G_DBUS_MESSAGE_TYPE_METHOD_RETURN); - - will_return(__wrap_g_bus_get_sync, conn); - will_return(__wrap_g_dbus_connection_send_message_with_reply_sync, reply); - - assert_int_equal(diagnostics_get_data(ctx, params, 1, (void**)&data), DIAGNOSTICS_ERROR_IO_ERROR); - assert_null(data); - - destroy_ctx(ctx); -} - -static void test_diagnostics_get_data_n8(void **state) -{ - (void) state; - struct _diagnostics_ctx_s *ctx = build_ctx_crash(); - struct _diagnostics_data_s *data = NULL; - const char *params[] = {"cs_full"}; - GDBusMessage *reply = g_dbus_message_new(); - GUnixFDList *fd_list = g_unix_fd_list_new(); - - g_dbus_message_set_message_type(reply, G_DBUS_MESSAGE_TYPE_METHOD_RETURN); - g_dbus_message_set_unix_fd_list(reply, fd_list); - - will_return(__wrap_g_bus_get_sync, conn); - will_return(__wrap_g_dbus_connection_send_message_with_reply_sync, reply); - - assert_int_equal(diagnostics_get_data(ctx, params, 1, (void**)&data), DIAGNOSTICS_ERROR_IO_ERROR); - assert_null(data); - - destroy_ctx(ctx); } 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"}; - int fd = 1; - GDBusMessage *reply = g_dbus_message_new(); - GUnixFDList *fd_list = g_unix_fd_list_new_from_array(&fd, 1); - - g_dbus_message_set_message_type(reply, G_DBUS_MESSAGE_TYPE_METHOD_RETURN); - g_dbus_message_set_unix_fd_list(reply, fd_list); + diagnostics_data_h data = NULL; + diagnostics_ctx_h ctx = diagnostics_create(TEST_CLIENT_ID, TEST_EVENT_NAME, bundle_create()); - will_return(__wrap_g_bus_get_sync, conn); - will_return(__wrap_g_dbus_connection_send_message_with_reply_sync, reply); + will_return(__wrap_dumpsys_dump, DIAGNOSTICS_ERROR_NONE); - assert_int_equal(diagnostics_get_data(ctx, params, 1, (void**)&data), DIAGNOSTICS_ERROR_NONE); + assert_non_null(ctx); + assert_int_equal(diagnostics_get_data(ctx, NULL, 0, &data), DIAGNOSTICS_ERROR_NONE); assert_non_null(data); - destroy_ctx(ctx); + diagnostics_destroy(ctx); + diagnostics_data_destroy(data); } int main(void) { const struct CMUnitTest tests[] = { cmocka_unit_test(test_diagnostics_get_data_n1), - cmocka_unit_test(test_diagnostics_get_data_n2), - cmocka_unit_test(test_diagnostics_get_data_n3), - cmocka_unit_test(test_diagnostics_get_data_n4), - cmocka_unit_test(test_diagnostics_get_data_n5), - cmocka_unit_test(test_diagnostics_get_data_n6), - cmocka_unit_test(test_diagnostics_get_data_n7), - cmocka_unit_test(test_diagnostics_get_data_n8), cmocka_unit_test(test_diagnostics_get_data_p1), }; return cmocka_run_group_tests(tests, NULL, NULL); diff --git a/src/test/test_diagnostics_get_event_data.c b/src/test/test_diagnostics_get_event_data.c new file mode 100644 index 0000000..cb0bae8 --- /dev/null +++ b/src/test/test_diagnostics_get_event_data.c @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2020 Samsung Electronics Co., Ltd. + * + * 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 "diagnostics.h" +#include "test_diagnostics.h" +#include "test_diagnostics_add_function_defs.h" + +static void test_diagnostics_get_event_data_n1(void **state) +{ + (void) state; + bundle *event_data; + + assert_int_equal(diagnostics_get_event_data(NULL, &event_data), DIAGNOSTICS_ERROR_INVALID_PARAMETER); +} + +static void test_diagnostics_get_event_data_n2(void **state) +{ + (void) state; + bundle *event_data = bundle_create(); + struct _diagnostics_ctx_s *ctx = diagnostics_create(TEST_CLIENT_ID, TEST_EVENT_NAME, event_data); + + assert_int_equal(diagnostics_get_event_data(ctx, NULL), DIAGNOSTICS_ERROR_INVALID_PARAMETER); + + diagnostics_destroy(ctx); +} + +static void test_diagnostics_get_event_data_p1(void **state) +{ + (void) state; + bundle *event_data_in = bundle_create(); + bundle *event_data_out = NULL; + struct _diagnostics_ctx_s *ctx = diagnostics_create(TEST_CLIENT_ID, TEST_EVENT_NAME, event_data_in); + int ret; + + ret = diagnostics_get_event_data(ctx, &event_data_out); + assert_int_equal(ret, DIAGNOSTICS_ERROR_NONE); + assert_non_null(event_data_out); + + bundle_free(event_data_out); + diagnostics_destroy(ctx); +} + +int main(void) +{ + const struct CMUnitTest tests[] = { + cmocka_unit_test(test_diagnostics_get_event_data_n1), + cmocka_unit_test(test_diagnostics_get_event_data_n2), + cmocka_unit_test(test_diagnostics_get_event_data_p1), + }; + return cmocka_run_group_tests(tests, NULL, NULL); +} diff --git a/src/test/test_diagnostics_get_event_name.c b/src/test/test_diagnostics_get_event_name.c new file mode 100644 index 0000000..80f2887 --- /dev/null +++ b/src/test/test_diagnostics_get_event_name.c @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2020 Samsung Electronics Co., Ltd. + * + * 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 "diagnostics.h" +#include "test_diagnostics.h" +#include "test_diagnostics_add_function_defs.h" + +static void test_diagnostics_get_event_name_n1(void **state) +{ + (void) state; + char *event_name; + + assert_int_equal(diagnostics_get_event_name(NULL, &event_name), DIAGNOSTICS_ERROR_INVALID_PARAMETER); +} + +static void test_diagnostics_get_event_name_n2(void **state) +{ + (void) state; + struct _diagnostics_ctx_s *ctx = diagnostics_create(TEST_CLIENT_ID, TEST_EVENT_NAME, NULL); + + assert_int_equal(diagnostics_get_event_name(ctx, NULL), DIAGNOSTICS_ERROR_INVALID_PARAMETER); + + diagnostics_destroy(ctx); +} + +static void test_diagnostics_get_event_name_p1(void **state) +{ + (void) state; + struct _diagnostics_ctx_s *ctx = diagnostics_create(TEST_CLIENT_ID, TEST_EVENT_NAME, NULL); + char *event_name; + int ret; + + ret = diagnostics_get_event_name(ctx, &event_name); + assert_int_equal(ret, DIAGNOSTICS_ERROR_NONE); + assert_string_equal(event_name, TEST_EVENT_NAME); + + free(event_name); + diagnostics_destroy(ctx); +} + +int main(void) +{ + const struct CMUnitTest tests[] = { + cmocka_unit_test(test_diagnostics_get_event_name_n1), + cmocka_unit_test(test_diagnostics_get_event_name_n2), + cmocka_unit_test(test_diagnostics_get_event_name_p1), + }; + return cmocka_run_group_tests(tests, NULL, NULL); +} diff --git a/src/test/test_diagnostics_get_report_path.c b/src/test/test_diagnostics_get_report_path.c deleted file mode 100644 index 8422c2c..0000000 --- a/src/test/test_diagnostics_get_report_path.c +++ /dev/null @@ -1,89 +0,0 @@ -/* - * Copyright (c) 2020 Samsung Electronics Co., Ltd. - * - * 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 -#include -#include -#include - -#include "diagnostics.h" -#include -#include - -#include "signal.h" -#include "dbus.h" - -#include - -#include "test_diagnostics.h" -#include "test_diagnostics_add_function_defs.h" - -static void test_diagnostics_get_report_path_n1(void **state) -{ - (void) state; - const char *path; - size_t len; - - assert_int_equal(get_report_path(NULL, &path, &len), DIAGNOSTICS_ERROR_INVALID_PARAMETER); -} - -static void test_diagnostics_get_report_path_n2(void **state) -{ - (void) state; - diagnostics_ctx_h br_ptr = build_ctx_crash(); - size_t len; - - assert_int_equal(get_report_path(br_ptr, NULL, &len), DIAGNOSTICS_ERROR_INVALID_PARAMETER); - - destroy_ctx(br_ptr); -} - -static void test_diagnostics_get_report_path_n3(void **state) -{ - (void) state; - diagnostics_ctx_h ctx = build_ctx_crash(); - ((struct _diagnostics_ctx_s *)ctx)->signal_type = SIG_TYPE_CRASH + 10; - const char *path; - size_t len; - - assert_int_equal(get_report_path(ctx, &path, &len), DIAGNOSTICS_ERROR_NOT_SUPPORTED); - - destroy_ctx(ctx); -} - -static void test_diagnostics_get_report_path_p1(void **state) -{ - (void) state; - diagnostics_ctx_h ctx = build_ctx_crash(); - const char *path; - size_t len; - int result = get_report_path(ctx, &path, &len); - - assert_int_equal(result, DIAGNOSTICS_ERROR_NONE); - assert_string_equal(path, DBUS_SIGNAL_CRASH_REPORT_PATH); - destroy_ctx(ctx); -} - -int main(void) -{ - const struct CMUnitTest tests[] = { - cmocka_unit_test(test_diagnostics_get_report_path_n1), - cmocka_unit_test(test_diagnostics_get_report_path_n2), - cmocka_unit_test(test_diagnostics_get_report_path_n3), - cmocka_unit_test(test_diagnostics_get_report_path_p1), - }; - return cmocka_run_group_tests(tests, NULL, NULL); -} diff --git a/src/test/test_diagnostics_request_client_data.c b/src/test/test_diagnostics_request_client_data.c index af04f61..1a24265 100644 --- a/src/test/test_diagnostics_request_client_data.c +++ b/src/test/test_diagnostics_request_client_data.c @@ -14,20 +14,7 @@ * limitations under the License. */ -#include -#include -#include -#include - #include "diagnostics.h" -#include -#include - -#include "signal.h" -#include "dbus.h" - -#include - #include "test_diagnostics.h" static void test_diagnostics_request_client_data_n1(void **state) diff --git a/src/test/test_diagnostics_send_event.c b/src/test/test_diagnostics_send_event.c new file mode 100644 index 0000000..2c75a7e --- /dev/null +++ b/src/test/test_diagnostics_send_event.c @@ -0,0 +1,103 @@ +/* + * Copyright (c) 2020 Samsung Electronics Co., Ltd. + * + * 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 "diagnostics.h" +#include "test_diagnostics.h" +#include "test_diagnostics_add_function_defs.h" + +static void test_diagnostics_send_event_n1(void **state) +{ + (void) state; + + assert_int_equal(diagnostics_send_event(NULL, NULL), DIAGNOSTICS_ERROR_INVALID_PARAMETER); +} + +static void test_diagnostics_send_event_n2(void **state) +{ + (void) state; + + snprintf(app_id, MAX_CLIENT_ID_LEN, "%s", ""); + snprintf(service_name, MAX_CLIENT_ID_LEN, "%s", ""); + will_return(__wrap_aul_app_get_appid_bypid, -1); + + assert_int_equal(diagnostics_send_event(TEST_CLIENT_ID, NULL), DIAGNOSTICS_ERROR_INVALID_PARAMETER); +} + +static void test_diagnostics_send_event_n3(void **state) +{ + (void) state; + bundle *event_data = bundle_create(); + + snprintf(app_id, MAX_CLIENT_ID_LEN, "%s", TEST_CLIENT_ID); + will_return(__wrap_bundle_encode, -1); + + assert_int_equal(diagnostics_send_event(TEST_CLIENT_ID, event_data), DIAGNOSTICS_ERROR_IO_ERROR); +} + +static void test_diagnostics_send_event_n4(void **state) +{ + (void) state; + bundle *event_data = bundle_create(); + + snprintf(app_id, MAX_CLIENT_ID_LEN, "%s", TEST_CLIENT_ID); + will_return(__wrap_bundle_encode, 0); + will_return(__wrap_g_bus_get_sync, NULL); + + assert_int_equal(diagnostics_send_event(TEST_CLIENT_ID, event_data), DIAGNOSTICS_ERROR_IO_ERROR); +} + +static void test_diagnostics_send_event_n5(void **state) +{ + (void) state; + bundle *event_data = bundle_create(); + int dummy = 1; + GDBusConnection *conn = (GDBusConnection *)&dummy; + + snprintf(app_id, MAX_CLIENT_ID_LEN, "%s", TEST_CLIENT_ID); + will_return(__wrap_bundle_encode, 0); + will_return(__wrap_g_bus_get_sync, conn); + will_return(__wrap_g_dbus_connection_emit_signal, FALSE); + + assert_int_equal(diagnostics_send_event(TEST_CLIENT_ID, event_data), DIAGNOSTICS_ERROR_IO_ERROR); +} + +static void test_diagnostics_send_event_p1(void **state) +{ + (void) state; + bundle *event_data = bundle_create(); + int dummy = 1; + GDBusConnection *conn = (GDBusConnection *)&dummy; + + snprintf(app_id, MAX_CLIENT_ID_LEN, "%s", TEST_CLIENT_ID); + will_return(__wrap_bundle_encode, 0); + will_return(__wrap_g_bus_get_sync, conn); + will_return(__wrap_g_dbus_connection_emit_signal, TRUE); + + assert_int_equal(diagnostics_send_event(TEST_CLIENT_ID, event_data), DIAGNOSTICS_ERROR_NONE); +} + +int main(void) +{ + const struct CMUnitTest tests[] = { + cmocka_unit_test(test_diagnostics_send_event_n1), + cmocka_unit_test(test_diagnostics_send_event_n2), + cmocka_unit_test(test_diagnostics_send_event_n3), + cmocka_unit_test(test_diagnostics_send_event_n4), + cmocka_unit_test(test_diagnostics_send_event_n5), + cmocka_unit_test(test_diagnostics_send_event_p1), + }; + return cmocka_run_group_tests(tests, NULL, NULL); +} diff --git a/src/test/test_diagnostics_set_client_id.c b/src/test/test_diagnostics_set_client_id.c new file mode 100644 index 0000000..35832d0 --- /dev/null +++ b/src/test/test_diagnostics_set_client_id.c @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2020 Samsung Electronics Co., Ltd. + * + * 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 "diagnostics.h" +#include "test_diagnostics.h" +#include "test_diagnostics_add_function_defs.h" + +static void test_diagnostics_set_client_id_n1(void **state) +{ + (void) state; + + assert_int_equal(diagnostics_set_client_id(NULL), DIAGNOSTICS_ERROR_INVALID_PARAMETER); +} + +static void test_diagnostics_set_client_id_n2(void **state) +{ + (void) state; + + snprintf(app_id, MAX_CLIENT_ID_LEN, "%s", ""); + snprintf(service_name, MAX_CLIENT_ID_LEN, "%s", ""); + will_return(__wrap_aul_app_get_appid_bypid, 0); + + assert_int_equal(diagnostics_set_client_id(TEST_CLIENT_ID), DIAGNOSTICS_ERROR_RESOURCE_BUSY); + assert_string_not_equal(service_name, TEST_CLIENT_ID); +} + +static void test_diagnostics_set_client_id_n3(void **state) +{ + (void) state; + + snprintf(app_id, MAX_CLIENT_ID_LEN, "%s", ""); + snprintf(service_name, MAX_CLIENT_ID_LEN, "aaa"); + will_return(__wrap_aul_app_get_appid_bypid, -1); + + assert_int_equal(diagnostics_set_client_id(TEST_CLIENT_ID), DIAGNOSTICS_ERROR_RESOURCE_BUSY); + assert_string_not_equal(service_name, TEST_CLIENT_ID); +} + +static void test_diagnostics_set_client_id_p1(void **state) +{ + (void) state; + + snprintf(app_id, MAX_CLIENT_ID_LEN, "%s", ""); + snprintf(service_name, MAX_CLIENT_ID_LEN, "%s", ""); + will_return(__wrap_aul_app_get_appid_bypid, -1); + + assert_int_equal(diagnostics_set_client_id(TEST_CLIENT_ID), DIAGNOSTICS_ERROR_NONE); + assert_string_equal(service_name, TEST_CLIENT_ID); +} + +int main(void) +{ + const struct CMUnitTest tests[] = { + cmocka_unit_test(test_diagnostics_set_client_id_n1), + cmocka_unit_test(test_diagnostics_set_client_id_n2), + cmocka_unit_test(test_diagnostics_set_client_id_n3), + cmocka_unit_test(test_diagnostics_set_client_id_p1), + }; + return cmocka_run_group_tests(tests, NULL, NULL); +} diff --git a/src/test/test_diagnostics_set_data_request_cb.c b/src/test/test_diagnostics_set_data_request_cb.c new file mode 100644 index 0000000..84a76ff --- /dev/null +++ b/src/test/test_diagnostics_set_data_request_cb.c @@ -0,0 +1,91 @@ +/* + * Copyright (c) 2020 Samsung Electronics Co., Ltd. + * + * 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 "diagnostics.h" +#include "test_diagnostics.h" +#include "test_diagnostics_add_function_defs.h" + +void callback(diagnostics_data_h data, char **params, int params_size, diagnostics_ctx_h ctx, void *user_data) +{ + (void) data; + (void) params; + (void) params_size; + (void) ctx; + (void) user_data; +} + +static void test_diagnostics_set_data_request_cb_n1(void **state) +{ + (void) state; + + assert_int_equal(diagnostics_set_data_request_cb(NULL, NULL), DIAGNOSTICS_ERROR_INVALID_PARAMETER); +} + +static void test_diagnostics_set_data_request_cb_n2(void **state) +{ + (void) state; + + dumpsys_cb_info.cb = callback; + assert_int_equal(diagnostics_set_data_request_cb(callback, NULL), DIAGNOSTICS_ERROR_RESOURCE_BUSY); +} + +static void test_diagnostics_set_data_request_cb_n3(void **state) +{ + (void) state; + + dumpsys_cb_info.cb = NULL; + memset(app_id, 0, MAX_CLIENT_ID_LEN); + memset(service_name, 0, MAX_CLIENT_ID_LEN); + will_return(__wrap_aul_app_get_appid_bypid, -1); + + assert_int_equal(diagnostics_set_data_request_cb(callback, NULL), DIAGNOSTICS_ERROR_INVALID_PARAMETER); +} + +static void test_diagnostics_set_data_request_cb_n4(void **state) +{ + (void) state; + + dumpsys_cb_info.cb = NULL; + will_return(__wrap_aul_app_get_appid_bypid, 0); + will_return(__wrap_dumpsys_system_register_dump_cb, -1); + + assert_int_equal(diagnostics_set_data_request_cb(callback, NULL), DIAGNOSTICS_ERROR_IO_ERROR); +} + +static void test_diagnostics_set_data_request_cb_p1(void **state) +{ + (void) state; + + dumpsys_cb_info.cb = NULL; + memset(app_id, 'a', MAX_CLIENT_ID_LEN); + will_return(__wrap_dumpsys_system_register_dump_cb, 0); + + assert_int_equal(diagnostics_set_data_request_cb(callback, NULL), DIAGNOSTICS_ERROR_NONE); + assert_ptr_equal(dumpsys_cb_info.cb, callback); + assert_ptr_equal(dumpsys_cb_info.user_data, NULL); +} + +int main(void) +{ + const struct CMUnitTest tests[] = { + cmocka_unit_test(test_diagnostics_set_data_request_cb_n1), + cmocka_unit_test(test_diagnostics_set_data_request_cb_n2), + cmocka_unit_test(test_diagnostics_set_data_request_cb_n3), + cmocka_unit_test(test_diagnostics_set_data_request_cb_n4), + cmocka_unit_test(test_diagnostics_set_data_request_cb_p1), + }; + return cmocka_run_group_tests(tests, NULL, NULL); +} diff --git a/src/test/test_diagnostics_set_notification_cb.c b/src/test/test_diagnostics_set_notification_cb.c index aaf8e8e..63ef8ec 100644 --- a/src/test/test_diagnostics_set_notification_cb.c +++ b/src/test/test_diagnostics_set_notification_cb.c @@ -14,81 +14,39 @@ * limitations under the License. */ -#include -#include -#include -#include - #include "diagnostics.h" -#include -#include - #include "test_diagnostics.h" #include "test_diagnostics_add_function_defs.h" -struct _diagnostics_cb_info_s { - diagnostics_notification_cb cb; - void *user_data; -}; - -static int dummy = 1; -GDBusConnection *conn = (GDBusConnection *)&dummy; - void callback(void *ctx, void *user_data) { - (void)ctx; - (void)user_data; + (void) ctx; + (void) user_data; } static void test_diagnostics_set_notification_cb_n1(void **state) { (void) state; + assert_int_equal(diagnostics_set_notification_cb(NULL, NULL), DIAGNOSTICS_ERROR_INVALID_PARAMETER); } static void test_diagnostics_set_notification_cb_n2(void **state) { (void) state; - GDBusConnection *conn = NULL; - - will_return(__wrap_g_bus_get_sync, conn); - - assert_int_equal(diagnostics_set_notification_cb(callback, NULL), DIAGNOSTICS_ERROR_IO_ERROR); -} - -static void test_diagnostics_set_notification_cb_n3(void **state) -{ - (void) state; - - will_return(__wrap_g_bus_get_sync, conn); - will_return(__wrap_g_dbus_connection_signal_subscribe, 0); - assert_int_equal(diagnostics_set_notification_cb(callback, NULL), DIAGNOSTICS_ERROR_IO_ERROR); + notification_cb_info.cb = callback; + assert_int_equal(diagnostics_set_notification_cb(callback, NULL), DIAGNOSTICS_ERROR_RESOURCE_BUSY); } static void test_diagnostics_set_notification_cb_p1(void **state) { (void) state; - will_return(__wrap_g_bus_get_sync, conn); - will_return(__wrap_g_dbus_connection_signal_subscribe, 1); - + notification_cb_info.cb = NULL; assert_int_equal(diagnostics_set_notification_cb(callback, NULL), DIAGNOSTICS_ERROR_NONE); - assert_ptr_equal(cb_info.cb, callback); - assert_ptr_equal(cb_info.user_data, NULL); - - cb_info.cb = NULL; -} - -static void test_diagnostics_set_notification_cb_p2(void **state) -{ - (void) state; - - will_return(__wrap_g_bus_get_sync, conn); - will_return(__wrap_g_dbus_connection_signal_subscribe, 1); - - 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); + assert_ptr_equal(notification_cb_info.cb, callback); + assert_ptr_equal(notification_cb_info.user_data, NULL); } int main(void) @@ -96,9 +54,7 @@ int main(void) const struct CMUnitTest tests[] = { cmocka_unit_test(test_diagnostics_set_notification_cb_n1), cmocka_unit_test(test_diagnostics_set_notification_cb_n2), - cmocka_unit_test(test_diagnostics_set_notification_cb_n3), cmocka_unit_test(test_diagnostics_set_notification_cb_p1), - cmocka_unit_test(test_diagnostics_set_notification_cb_p2), }; return cmocka_run_group_tests(tests, NULL, NULL); } diff --git a/src/test/test_diagnostics_signal_handler.c b/src/test/test_diagnostics_signal_handler.c index 02fe8ec..9db6e0a 100644 --- a/src/test/test_diagnostics_signal_handler.c +++ b/src/test/test_diagnostics_signal_handler.c @@ -14,25 +14,10 @@ * limitations under the License. */ -#include -#include -#include -#include - #include "diagnostics.h" -#include -#include - -#include - #include "test_diagnostics.h" #include "test_diagnostics_add_function_defs.h" -struct _diagnostics_cb_info_s { - diagnostics_notification_cb cb; - void *user_data; -}; - struct _diagnostics_ctx_s *ctx = NULL; void signal_callback(void *_ctx, void *user_data) @@ -45,12 +30,12 @@ void signal_callback(void *_ctx, void *user_data) static void test_diagnostics_signal_handler_n1(void **state) { (void) state; - GVariant *parameters = build_signal_parameters_crash(); + GVariant *parameters = g_variant_new("(ssi)", TEST_CLIENT_ID, "", 0); - cb_info.cb = NULL; - cb_info.user_data = NULL; + notification_cb_info.cb = NULL; + notification_cb_info.user_data = NULL; - signal_handler(NULL, DBUS_SENDER_CRASH, DBUS_OBJECT_PATH, DBUS_INTERFACE_NAME, DBUS_MEMBER_CRASH, parameters, NULL); + signal_handler(NULL, NULL, DBUS_EVENT_OBJECT_PATH, DBUS_EVENT_INTERFACE_NAME, TEST_EVENT_NAME, parameters, NULL); assert_null(ctx); g_variant_unref(parameters); @@ -59,12 +44,13 @@ static void test_diagnostics_signal_handler_n1(void **state) static void test_diagnostics_signal_handler_n2(void **state) { (void) state; - GVariant *parameters = build_signal_parameters_crash(); + GVariant *parameters = g_variant_new("(ssi)", TEST_CLIENT_ID, "", 0); - cb_info.cb = signal_callback; - cb_info.user_data = NULL; + notification_cb_info.cb = signal_callback; + notification_cb_info.user_data = NULL; + notification_cb_info.events_count = 0; - signal_handler(NULL, DBUS_SENDER_CRASH, DBUS_OBJECT_PATH, DBUS_INTERFACE_NAME, "foo", parameters, NULL); + signal_handler(NULL, NULL, DBUS_EVENT_OBJECT_PATH, DBUS_EVENT_INTERFACE_NAME, TEST_EVENT_NAME, parameters, NULL); assert_null(ctx); g_variant_unref(parameters); @@ -73,19 +59,21 @@ static void test_diagnostics_signal_handler_n2(void **state) static void test_diagnostics_signal_handler_p1(void **state) { (void) state; - GVariant *parameters = build_signal_parameters_crash(); + GVariant *parameters = g_variant_new("(ssi)", TEST_CLIENT_ID, "", 0); + struct _diagnostics_event_s event = {TEST_EVENT_NAME, TEST_CLIENT_ID, 0}; + struct _diagnostics_event_s *events[1] = {&event}; - cb_info.cb = signal_callback; - cb_info.user_data = NULL; + notification_cb_info.cb = signal_callback; + notification_cb_info.user_data = NULL; + notification_cb_info.events = events; + notification_cb_info.events_count = 1; - signal_handler(NULL, DBUS_SENDER_CRASH, DBUS_OBJECT_PATH, DBUS_INTERFACE_NAME, DBUS_MEMBER_CRASH, parameters, NULL); + signal_handler(NULL, NULL, DBUS_EVENT_OBJECT_PATH, DBUS_EVENT_INTERFACE_NAME, TEST_EVENT_NAME, parameters, NULL); assert_non_null(ctx); - assert_string_equal(ctx->signal->sender_name, DBUS_SENDER_CRASH); - assert_string_equal(ctx->signal->object_path, DBUS_OBJECT_PATH); - assert_string_equal(ctx->signal->interface_name, DBUS_INTERFACE_NAME); - assert_string_equal(ctx->signal->signal_name, DBUS_MEMBER_CRASH); - assert_non_null(ctx->signal->parameters); + assert_string_equal(ctx->client_id, TEST_CLIENT_ID); + assert_string_equal(ctx->event_name, TEST_EVENT_NAME); + assert_ptr_equal(ctx->event_data, NULL); g_variant_unref(parameters); } diff --git a/src/test/test_diagnostics_subscribe_event.c b/src/test/test_diagnostics_subscribe_event.c new file mode 100644 index 0000000..c6a72de --- /dev/null +++ b/src/test/test_diagnostics_subscribe_event.c @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2020 Samsung Electronics Co., Ltd. + * + * 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 "diagnostics.h" +#include "test_diagnostics.h" +#include "test_diagnostics_add_function_defs.h" + +static void test_diagnostics_subscribe_event_n1(void **state) +{ + (void) state; + + assert_int_equal(diagnostics_subscribe_event(NULL, TEST_CLIENT_ID), DIAGNOSTICS_ERROR_INVALID_PARAMETER); + assert_int_equal(notification_cb_info.events_count, 0); +} + +static void test_diagnostics_subscribe_event_n2(void **state) +{ + (void) state; + + assert_int_equal(diagnostics_subscribe_event(TEST_EVENT_NAME, NULL), DIAGNOSTICS_ERROR_INVALID_PARAMETER); + assert_int_equal(notification_cb_info.events_count, 0); +} + +static void test_diagnostics_subscribe_event_n3(void **state) +{ + (void) state; + int dummy = 1; + GDBusConnection *conn = (GDBusConnection *)&dummy; + + will_return(__wrap_g_bus_get_sync, conn); + will_return(__wrap_g_dbus_connection_signal_subscribe, FALSE); + + assert_int_equal(diagnostics_subscribe_event(TEST_EVENT_NAME, TEST_CLIENT_ID), DIAGNOSTICS_ERROR_IO_ERROR); + assert_int_equal(notification_cb_info.events_count, 0); +} + +static void test_diagnostics_subscribe_event_p1(void **state) +{ + (void) state; + int dummy = 1; + GDBusConnection *conn = (GDBusConnection *)&dummy; + + will_return_always(__wrap_g_bus_get_sync, conn); + will_return(__wrap_g_dbus_connection_signal_subscribe, TRUE); + + assert_int_equal(diagnostics_subscribe_event(TEST_EVENT_NAME, TEST_CLIENT_ID), DIAGNOSTICS_ERROR_NONE); + assert_int_equal(notification_cb_info.events_count, 1); + + assert_int_equal(diagnostics_unset_notification_cb(), DIAGNOSTICS_ERROR_NONE); +} + +int main(void) +{ + const struct CMUnitTest tests[] = { + cmocka_unit_test(test_diagnostics_subscribe_event_n1), + cmocka_unit_test(test_diagnostics_subscribe_event_n2), + cmocka_unit_test(test_diagnostics_subscribe_event_n3), + cmocka_unit_test(test_diagnostics_subscribe_event_p1), + }; + return cmocka_run_group_tests(tests, NULL, NULL); +} diff --git a/src/test/test_diagnostics_unset_data_request_cb.c b/src/test/test_diagnostics_unset_data_request_cb.c new file mode 100644 index 0000000..9a5de2e --- /dev/null +++ b/src/test/test_diagnostics_unset_data_request_cb.c @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2020 Samsung Electronics Co., Ltd. + * + * 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 "diagnostics.h" +#include "test_diagnostics.h" + +static void test_diagnostics_unset_data_request_cb_n1(void **state) +{ + (void) state; + + will_return(__wrap_dumpsys_system_unregister_dump_cb, -1); + + assert_int_equal(diagnostics_unset_data_request_cb(), DIAGNOSTICS_ERROR_IO_ERROR); +} + +static void test_diagnostics_unset_data_request_cb_p1(void **state) +{ + (void) state; + + will_return(__wrap_dumpsys_system_unregister_dump_cb, 0); + + assert_int_equal(diagnostics_unset_data_request_cb(), DIAGNOSTICS_ERROR_NONE); +} + +int main(void) +{ + const struct CMUnitTest tests[] = { + cmocka_unit_test(test_diagnostics_unset_data_request_cb_n1), + cmocka_unit_test(test_diagnostics_unset_data_request_cb_p1), + }; + return cmocka_run_group_tests(tests, NULL, NULL); +} diff --git a/src/test/test_diagnostics_unset_notification_cb.c b/src/test/test_diagnostics_unset_notification_cb.c index 698fb15..8636bee 100644 --- a/src/test/test_diagnostics_unset_notification_cb.c +++ b/src/test/test_diagnostics_unset_notification_cb.c @@ -14,36 +14,13 @@ * limitations under the License. */ -#include -#include -#include -#include - #include "diagnostics.h" -#include -#include - #include "test_diagnostics.h" -static int dummy = 1; -GDBusConnection *conn = (GDBusConnection *)&dummy; - -void callback(void *ctx, void *user_data) -{ - (void)ctx; - (void)user_data; -} - static void test_diagnostics_unset_notification_cb_p1(void **state) { (void) state; - will_return(__wrap_g_bus_get_sync, conn); - will_return(__wrap_g_dbus_connection_signal_subscribe, 1); - - assert_int_equal(diagnostics_set_notification_cb(callback, NULL), DIAGNOSTICS_ERROR_NONE); - - will_return(__wrap_g_bus_get_sync, conn); assert_int_equal(diagnostics_unset_notification_cb(), DIAGNOSTICS_ERROR_NONE); }