From 12b036647203d6c588444ea72b05461612745d98 Mon Sep 17 00:00:00 2001 From: Mateusz Moscicki Date: Mon, 11 May 2020 13:32:49 +0200 Subject: [PATCH] Add unit tests Change-Id: I6e85ba73472b1bf0bc363cd9c09cfedd41dec321 --- CMakeLists.txt | 1 + packaging/dumpsys.spec | 15 +- src/service/dumpsys-service.c | 6 + tests/CMakeLists.txt | 1 + tests/unit/CMakeLists.txt | 146 ++++++++++++ tests/unit/test_dumpsys_client_system_api.c | 279 +++++++++++++++++++++++ tests/unit/test_dumpsys_client_user_api.c | 231 +++++++++++++++++++ tests/unit/test_dumpsys_service.c | 333 ++++++++++++++++++++++++++++ tests/unit/test_libdumpsys.c | 317 ++++++++++++++++++++++++++ 9 files changed, 1328 insertions(+), 1 deletion(-) create mode 100644 tests/unit/CMakeLists.txt create mode 100644 tests/unit/test_dumpsys_client_system_api.c create mode 100644 tests/unit/test_dumpsys_client_user_api.c create mode 100644 tests/unit/test_dumpsys_service.c create mode 100644 tests/unit/test_libdumpsys.c diff --git a/CMakeLists.txt b/CMakeLists.txt index c6ced1c..789b91b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,5 +14,6 @@ ADD_SUBDIRECTORY(src/client-api) ADD_SUBDIRECTORY(src/dumpsys) ADD_SUBDIRECTORY(src/service) IF("${TESTS}" STREQUAL "ON") + ENABLE_TESTING() ADD_SUBDIRECTORY(tests) ENDIF() diff --git a/packaging/dumpsys.spec b/packaging/dumpsys.spec index 3f5f67c..fc23bcd 100644 --- a/packaging/dumpsys.spec +++ b/packaging/dumpsys.spec @@ -56,6 +56,10 @@ This package provides library and header files. %if %{with tests} %package tests Summary: Contains scripts for testing dumpsys +BuildRequires: pkgconfig(glib-2.0) +BuildRequires: pkgconfig(dlog) +BuildRequires: cmocka-devel cmocka +BuildRequires: lcov %description tests %endif @@ -69,10 +73,15 @@ export CFLAGS+=" -Werror -fvisibility=hidden" %cmake . \ -DVERSION=%{version} \ -DTESTS=%{on_off tests} \ - -DDUMPSYS_SYSTEM_TESTS_PATH=%{_libdir}/dumpsys_system_tests/ + -DDUMPSYS_SYSTEM_TESTS_PATH=%{_libdir}/dumpsys_system_tests/ \ + -DDUMPSYS_TESTS_PATH=%{_libdir}/dumpsys_unit_tests/ make %{?jobs:-j%jobs} +%if %{with tests} +make coverage +%endif + %install rm -rf %{buildroot} %make_install @@ -134,4 +143,8 @@ ln -s ../dumpsys-service.service %{buildroot}/%{_unitdir}/basic.target.wants/dum %{_libdir}/dumpsys_system_tests/run.sh %{_libdir}/dumpsys_system_tests/dumpsys_system/dumpsys_system.sh %{_libdir}/dumpsys_system_tests/dumpsys_notfound/dumpsys_notfound.sh +%{_libdir}/dumpsys_unit_tests/test_libdumpsys +%{_libdir}/dumpsys_unit_tests/test_dumpsys_service +%{_libdir}/dumpsys_unit_tests/test_dumpsys_client_user_api +%{_libdir}/dumpsys_unit_tests/test_dumpsys_client_system_api %endif diff --git a/src/service/dumpsys-service.c b/src/service/dumpsys-service.c index a6b6782..b37197c 100644 --- a/src/service/dumpsys-service.c +++ b/src/service/dumpsys-service.c @@ -39,7 +39,11 @@ #include "common.h" +#ifdef TEST +#define FIFO_BASE_DIR "/tmp" +#else #define FIFO_BASE_DIR "/run/dumpsys/priv/fifo" +#endif static GDBusNodeInfo *introspection_data; static const gchar introspection_xml[] = @@ -422,6 +426,7 @@ static bool dbus_init(void) return true; } +#ifndef UNIT_TESTS int main(int argc, char *argv[]) { loop = g_main_loop_new(NULL, false); @@ -438,3 +443,4 @@ end: return EXIT_SUCCESS; } +#endif // UNIT_TESTS diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 53c0353..588c3ef 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -7,3 +7,4 @@ ADD_DEFINITIONS(-D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE=1) # Sub modules ADD_SUBDIRECTORY(system) +ADD_SUBDIRECTORY(unit) diff --git a/tests/unit/CMakeLists.txt b/tests/unit/CMakeLists.txt new file mode 100644 index 0000000..da3327b --- /dev/null +++ b/tests/unit/CMakeLists.txt @@ -0,0 +1,146 @@ +cmake_minimum_required(VERSION 2.8.12) + +INCLUDE(FindPkgConfig) + +pkg_check_modules(dumpsys_service_pkgs REQUIRED + dlog + gio-2.0 + glib-2.0 + gio-unix-2.0 + pkgmgr-info) + +FOREACH(flag ${dumpsys_service_pkgs_CFLAGS}) + SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}") +ENDFOREACH(flag) + +ADD_DEFINITIONS(-DUNIT_TESTS) + +STRING(REPLACE "-O2" "" CMAKE_C_FLAGS ${CMAKE_C_FLAGS}) +STRING(REPLACE "-D_FORTIFY_SOURCE=2 " "" CMAKE_C_FLAGS ${CMAKE_C_FLAGS}) +SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O0 -g -fprofile-arcs -ftest-coverage --coverage") +SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS} -pthread -fPIE -Wno-unused-function -Wno-unused-const-variable") + +function(add_mocked_test) + set(oneValueArgs NAME WRAP_LIST) + set(multiValueArgs SOURCE_DIRS) + cmake_parse_arguments(T "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) + + add_executable(test_${T_NAME} test_${T_NAME}.c) + + add_definitions(-DTEST) + target_link_libraries(test_${T_NAME} ${dumpsys_service_pkgs_LIBRARIES} -pie -lcmocka -lgcov) + target_include_directories(test_${T_NAME} SYSTEM PUBLIC ${T_SOURCE_DIRS}) + set_target_properties(test_${T_NAME} PROPERTIES LINK_FLAGS ${T_WRAP_LIST}) + add_test(test_${T_NAME} ${CMAKE_CURRENT_BINARY_DIR}/test_${T_NAME}) + + install(TARGETS test_${T_NAME} DESTINATION ${DUMPSYS_TESTS_PATH}) +endfunction(add_mocked_test) + +add_mocked_test(NAME dumpsys_client_user_api + SOURCE_DIRS ../../src/dumpsys/ + ../../src/client-api/ + ../../src/shared + WRAP_LIST "-Wl,--wrap=g_bus_get_sync \ + -Wl,--wrap=g_bus_own_name_on_connection \ + -Wl,--wrap=g_bus_unown_name \ + -Wl,--wrap=g_dbus_connection_register_object \ + -Wl,--wrap=g_dbus_connection_unregister_object \ + -Wl,--wrap=pkgmgrinfo_appinfo_filter_foreach_appinfo \ + -Wl,--wrap=pkgmgrinfo_appinfo_filter_count") + +add_mocked_test(NAME dumpsys_service + SOURCE_DIRS ../../src/dumpsys/ + ../../src/service/ + ../../src/shared + WRAP_LIST "-Wl,--wrap=g_bus_get_sync \ + -Wl,--wrap=g_dbus_connection_register_object \ + -Wl,--wrap=g_dbus_connection_send_message \ + -Wl,--wrap=g_dbus_connection_send_message_with_reply_sync \ + -Wl,--wrap=g_dbus_method_invocation_get_message \ + -Wl,--wrap=g_dbus_method_invocation_return_error \ + -Wl,--wrap=g_unix_fd_list_append \ + -Wl,--wrap=g_unix_fd_list_new") + +add_mocked_test(NAME dumpsys_client_system_api + SOURCE_DIRS ../../src/dumpsys/ + ../../src/client-api/ + ../../src/shared + WRAP_LIST "-Wl,--wrap=g_bus_get_sync \ + -Wl,--wrap=g_bus_own_name_on_connection \ + -Wl,--wrap=g_bus_unown_name \ + -Wl,--wrap=g_dbus_connection_register_object \ + -Wl,--wrap=g_dbus_connection_send_message \ + -Wl,--wrap=g_dbus_connection_send_message_with_reply_sync \ + -Wl,--wrap=g_dbus_connection_unregister_object \ + -Wl,--wrap=g_dbus_message_get_unix_fd_list \ + -Wl,--wrap=g_dbus_method_invocation_get_message \ + -Wl,--wrap=g_dbus_method_invocation_return_value \ + -Wl,--wrap=g_task_run_in_thread \ + -Wl,--wrap=g_unix_fd_list_get") + +add_mocked_test(NAME libdumpsys + SOURCE_DIRS ../../src/dumpsys/ + ../../src/shared + WRAP_LIST "-Wl,--wrap=g_bus_get_sync \ + -Wl,--wrap=g_dbus_message_get_message_type \ + -Wl,--wrap=g_dbus_message_get_unix_fd_list \ + -Wl,--wrap=g_unix_fd_list_get \ + -Wl,--wrap=g_dbus_connection_send_message_with_reply_sync") + +function(setup_coverage_target) + set(oneValueArgs BASE_DIRECTORY NAME) + set(multiValueArgs EXCLUDE RUN_TESTS DEPENDENCIES LCOV_ARGS GENHTML_ARGS) + cmake_parse_arguments(COV "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) + find_program(GCOV_PATH NAMES gcov) + find_program(LCOV_PATH NAMES lcov) + find_program(GENHTML_PATH NAMES genhtml) + + if(NOT GCOV_PATH) + message(FATAL_ERROR "gcov not found! Aborting...") + endif() + + if(NOT LCOV_PATH) + message(FATAL_ERROR "lcov not found! Aborting...") + endif() + + if(NOT GENHTML_PATH) + message(FATAL_ERROR "genhtml not found! Aborting...") + endif() + + if(${COV_BASE_DIRECTORY}) + get_filename_component(BASEDIR ${COV_BASE_DIRECTORY} ABSOLUTE) + else() + set(BASEDIR ${PROJECT_SOURCE_DIR}) + endif() + + set(LCOV_EXCLUDES "") + foreach(EXCLUDE ${COV_EXCLUDE} ${COVERAGE_EXCLUDES}) + get_filename_component(EXCLUDE ${EXCLUDE} ABSOLUTE BASE_DIR ${BASEDIR}) + list(APPEND LCOV_EXCLUDES "${EXCLUDE}") + endforeach() + list(REMOVE_DUPLICATES LCOV_EXCLUDES) + + add_custom_target(${COV_NAME} + COMMAND ${LCOV_PATH} ${COV_LCOV_ARGS} --gcov-tool ${GCOV_PATH} -directory . -b ${BASEDIR} --zerocounters + COMMAND ${COV_RUN_TESTS} + COMMAND ${LCOV_PATH} ${COV_LCOV_ARGS} --gcov-tool ${GCOV_PATH} --directory . -b ${BASEDIR} --capture --output-file ${COV_NAME}.total + COMMAND ${LCOV_PATH} ${COV_LCOV_ARGS} --gcov-tool ${GCOV_PATH} --remove ${COV_NAME}.total ${LCOV_EXCLUDES} --output-file ${COV_NAME}.info + COMMAND ${GENHTML_PATH} ${GENHTML_EXTRA_ARGS} ${COV_GENHTML_ARGS} -o ${COV_NAME} ${COV_NAME}.info + + BYPRODUCTS + ${COV_NAME}.total + ${COV_NAME}.info + ${COV_NAME} + + WORKING_DIRECTORY ${PROJECT_BINARY_DIR} + + DEPENDS ${COV_DEPENDENCIES} + ) + +endfunction() # setup_coverage_target + +setup_coverage_target( + NAME coverage + RUN_TESTS make test + EXCLUDE "unit/*" +) diff --git a/tests/unit/test_dumpsys_client_system_api.c b/tests/unit/test_dumpsys_client_system_api.c new file mode 100644 index 0000000..5674e0c --- /dev/null +++ b/tests/unit/test_dumpsys_client_system_api.c @@ -0,0 +1,279 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "dumpsys-system.c" + +GDBusConnection* __wrap_g_bus_get_sync(GBusType type, GCancellable *cancellable, GError **error) +{ + return (GDBusConnection*)g_object_new(G_TYPE_DBUS_CONNECTION, NULL); +} + +bool __wrap_g_dbus_connection_unregister_object(GDBusConnection *connection, guint id) +{ + return true; +} + +void __wrap_g_bus_unown_name(guint owner_id) +{ +} + +guint __wrap_g_bus_own_name_on_connection(GDBusConnection *connection, + const gchar *name, + GBusNameOwnerFlags flags, + GBusNameAcquiredCallback name_acquired_handler, + GBusNameLostCallback name_lost_handler, + gpointer user_data, + GDestroyNotify user_data_free_func) +{ + return 234; +} + +guint __wrap_g_dbus_connection_register_object(GDBusConnection *connection, + const gchar *object_path, + GDBusInterfaceInfo *interface_info, + const GDBusInterfaceVTable *vtable, + gpointer user_data, + GDestroyNotify user_data_free_func, + GError **error) +{ + return 345; +} + +GUnixFDList* __wrap_g_dbus_message_get_unix_fd_list(GDBusMessage *msg) +{ + return mock_ptr_type(GUnixFDList*); +} + +int __wrap_g_unix_fd_list_get(GUnixFDList *list, int fd, GError *error) +{ + check_expected(list); + return mock_type(int); +} + +gboolean __wrap_g_dbus_connection_send_message (GDBusConnection *connection, + GDBusMessage *message, + GDBusSendMessageFlags flags, + volatile guint32 *out_serial, + GError **error) +{ + check_expected(connection); + check_expected(flags); + return mock_type(gboolean); +} + +GDBusMessage* __wrap_g_dbus_connection_send_message_with_reply_sync(GDBusConnection *conn, + GDBusMessage *message, + GDBusSendMessageFlags flags, + gint timeout_msec, + volatile guint32 *out_serial, + GCancellable *cancellable, + GError **error) +{ + check_expected_ptr(conn); + check_expected(flags); + check_expected(timeout_msec); + return mock_ptr_type(GDBusMessage*); +} + +void __wrap_g_task_run_in_thread(GTask *task, GTaskThreadFunc task_func) +{ +} + +GDBusMessage* __wrap_g_dbus_method_invocation_get_message(GDBusMethodInvocation *invocation) +{ + check_expected(invocation); + return mock_ptr_type(GDBusMessage*); +} +void __wrap_g_dbus_method_invocation_return_value (GDBusMethodInvocation *invocation, GVariant *parameters) +{ + check_expected(invocation); +} + +void dumpsys_system_register_dump_cb_ok(void **state) +{ + void *t_handler = (void*)123; + char t_name[] = "some_name"; + dumpsys_system_dump_cb t_callback = (dumpsys_system_dump_cb)234; + assert_int_equal(dumpsys_system_register_dump_cb(t_callback, t_name, &t_handler), TIZEN_ERROR_NONE); + assert_ptr_equal(t_callback, t_handler); + dumpsys_system_unregister_dump_cb(t_handler); +} + +void dumpsys_system_register_dump_cb_fail1(void **state) +{ + void *t_handler = (void*)123; + char t_name[] = "some_name"; + dumpsys_system_dump_cb t_callback = (dumpsys_system_dump_cb)234; + dumpsys_system_register_dump_cb(t_callback, t_name, &t_handler); + assert_int_equal(dumpsys_system_register_dump_cb(t_callback, t_name, &t_handler), TIZEN_ERROR_ALREADY_IN_PROGRESS); + dumpsys_system_unregister_dump_cb(t_handler); +} + +void dumpsys_system_register_dump_cb_fail2(void **state) +{ + void *t_handler = (void*)123; + char t_name[] = "some_name"; + assert_int_equal(dumpsys_system_register_dump_cb(NULL, t_name, &t_handler), TIZEN_ERROR_INVALID_PARAMETER); + dumpsys_system_unregister_dump_cb(t_handler); +} + +void dumpsys_system_register_dump_cb_fail3(void **state) +{ + void *t_handler = (void*)123; + dumpsys_system_dump_cb t_callback = (dumpsys_system_dump_cb)234; + assert_int_equal(dumpsys_system_register_dump_cb(t_callback, NULL, &t_handler), TIZEN_ERROR_INVALID_PARAMETER); +} + +void dumpsys_system_register_dump_cb_fail4(void **state) +{ + char t_name[] = "some_name"; + dumpsys_system_dump_cb t_callback = (dumpsys_system_dump_cb)234; + assert_int_equal(dumpsys_system_register_dump_cb(t_callback, t_name, NULL), TIZEN_ERROR_INVALID_PARAMETER); +} + +void dumpsys_system_unregister_dump_cb_ok(void **state) +{ + void *t_handler; + char t_name[] = "some_name"; + dumpsys_system_dump_cb t_callback = (dumpsys_system_dump_cb)234; + + dumpsys_system_register_dump_cb(t_callback, t_name, &t_handler); + assert_int_equal(dumpsys_system_unregister_dump_cb(t_handler), TIZEN_ERROR_NONE); +} + +void dumpsys_system_unregister_dump_cb_fail1(void **state) +{ + assert_int_equal(dumpsys_system_unregister_dump_cb(NULL), TIZEN_ERROR_INVALID_PARAMETER); +} + +void dumpsys_system_unregister_dump_cb_fail2(void **state) +{ + char t_name[] = "some_name"; + void *t_handler, *t_fake_handler = (void*)123; + dumpsys_system_dump_cb t_callback = (dumpsys_system_dump_cb)234; + + dumpsys_system_register_dump_cb(t_callback, t_name, &t_handler); + assert_int_equal(dumpsys_system_unregister_dump_cb(t_fake_handler), TIZEN_ERROR_ALREADY_IN_PROGRESS); + assert_int_equal(dumpsys_system_unregister_dump_cb(t_handler), TIZEN_ERROR_NONE); +} + +int dump_callback(const int fd, const int argc, char **argv) +{ + return 123; +} + +void dump_call_handler_ok(void **state) +{ + const char test_arg[] = "test arg"; + const char test_name[] = "testname"; + + GVariantBuilder *test_builder = g_variant_builder_new(G_VARIANT_TYPE("as")); + g_variant_builder_add(test_builder, "s", test_arg); + GVariant *test_params = g_variant_new("(ass)", test_builder, test_name); + GDBusMessage *test_message = g_dbus_message_new_method_call("abc.def.ghi", "/Jkl/Mno/Pqr", "stu.wxy", "z"); + g_dbus_message_set_serial(test_message, 123); + g_dbus_message_set_body(test_message, test_params); + + GUnixFDList *test_list1 = g_unix_fd_list_new(); + will_return(__wrap_g_dbus_message_get_unix_fd_list, test_list1); + expect_value(__wrap_g_unix_fd_list_get, list, test_list1); + will_return(__wrap_g_unix_fd_list_get, 123); + + GDBusMethodInvocation *test_invocation = (GDBusMethodInvocation*)234; + expect_value(__wrap_g_dbus_method_invocation_get_message, invocation, test_invocation); + will_return(__wrap_g_dbus_method_invocation_get_message, test_message); + + expect_value(__wrap_g_dbus_method_invocation_return_value, invocation, test_invocation); + + char t_name[] = "some_name"; + void *t_handler; + + dumpsys_system_register_dump_cb(dump_callback, t_name, &t_handler); + + dump_handler(test_invocation); + dumpsys_system_unregister_dump_cb(t_handler); +} + +void dump_call_handler_fail(void **state) +{ + GDBusMethodInvocation *test_invocation = (GDBusMethodInvocation*)234; + + expect_value(__wrap_g_dbus_method_invocation_get_message, invocation, test_invocation); + will_return(__wrap_g_dbus_method_invocation_get_message, NULL); + + char t_name[] = "some_name"; + void *t_handler; + dumpsys_system_dump_cb t_callback = (dumpsys_system_dump_cb)234; + + dumpsys_system_register_dump_cb(t_callback, t_name, &t_handler); + + dump_handler(test_invocation); + dumpsys_system_unregister_dump_cb(t_handler); +} + +void get_args_ok(void **state) +{ + GVariantBuilder *builder = g_variant_builder_new(G_VARIANT_TYPE("as")); + g_variant_builder_add(builder, "s", "111"); + g_variant_builder_add(builder, "s", "222"); + g_variant_builder_add(builder, "s", "33333"); + GVariant *array = g_variant_new("(as)", builder); + g_variant_builder_unref(builder); + char **argv; + int argc = get_args(array, &argv); + assert_int_equal(argc, 3); + assert_string_equal(argv[0], "111"); + assert_string_equal(argv[1], "222"); + assert_string_equal(argv[2], "33333"); + g_variant_unref(array); +} + +void get_name_false(void **state) +{ + char *name; + assert_false(get_name(&name)); +} + +void get_name_ok(void **state) +{ + char t_name[] = "some_name"; + void *t_handler, *t_fake_handler = (void*)123; + dumpsys_system_dump_cb t_callback = (dumpsys_system_dump_cb)234; + + dumpsys_system_register_dump_cb(t_callback, t_name, &t_handler); + assert_int_equal(dumpsys_system_unregister_dump_cb(t_fake_handler), TIZEN_ERROR_ALREADY_IN_PROGRESS); + + char *name; + assert_true(get_name(&name)); + dumpsys_system_unregister_dump_cb(t_handler); + free(name); +} + +int main(int argc, char *argv[]) +{ + const struct CMUnitTest tests[] = { + cmocka_unit_test(dumpsys_system_register_dump_cb_ok), + cmocka_unit_test(dumpsys_system_register_dump_cb_fail1), + cmocka_unit_test(dumpsys_system_register_dump_cb_fail2), + cmocka_unit_test(dumpsys_system_register_dump_cb_fail3), + cmocka_unit_test(dumpsys_system_register_dump_cb_fail4), + cmocka_unit_test(dumpsys_system_unregister_dump_cb_ok), + cmocka_unit_test(dumpsys_system_unregister_dump_cb_fail1), + cmocka_unit_test(dumpsys_system_unregister_dump_cb_fail2), + cmocka_unit_test(dump_call_handler_ok), + cmocka_unit_test(dump_call_handler_fail), + cmocka_unit_test(get_args_ok), + cmocka_unit_test(get_name_false), + cmocka_unit_test(get_name_ok), + }; + return cmocka_run_group_tests(tests, NULL, NULL); +} + diff --git a/tests/unit/test_dumpsys_client_user_api.c b/tests/unit/test_dumpsys_client_user_api.c new file mode 100644 index 0000000..e623191 --- /dev/null +++ b/tests/unit/test_dumpsys_client_user_api.c @@ -0,0 +1,231 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "dumpsys-user.c" + +int __wrap_pkgmgrinfo_appinfo_filter_count(pkgmgrinfo_pkginfo_filter_h handle, int *count) +{ + *count = 1; + return 0; +} + +int __wrap_pkgmgrinfo_appinfo_filter_foreach_appinfo(pkgmgrinfo_pkginfo_filter_h handle, pkgmgrinfo_app_list_cb app_cb, void *user_data) +{ + char **s = (char**)user_data; + *s = strdup("some.app.id"); + return 0; +} + +__attribute__((visibility("default"))) extern GDBusConnection* __wrap_g_bus_get_sync(GBusType bus_type, GCancellable *cancellable, GError **error); +GDBusConnection* __wrap_g_bus_get_sync(GBusType bus_type, GCancellable *cancellable, GError **error) +{ + return (GDBusConnection*)g_object_new(G_TYPE_DBUS_CONNECTION, NULL); +} + +bool __wrap_g_dbus_connection_unregister_object(GDBusConnection *connection, guint id) +{ + return true; +} + +void __wrap_g_bus_unown_name(guint owner_id) +{ +} + +guint __wrap_g_bus_own_name_on_connection(GDBusConnection *connection, + const gchar *name, + GBusNameOwnerFlags flags, + GBusNameAcquiredCallback name_acquired_handler, + GBusNameLostCallback name_lost_handler, + gpointer user_data, + GDestroyNotify user_data_free_func) +{ + return 234; +} + +guint __wrap_g_dbus_connection_register_object(GDBusConnection *connection, + const gchar *object_path, + GDBusInterfaceInfo *interface_info, + const GDBusInterfaceVTable *vtable, + gpointer user_data, + GDestroyNotify user_data_free_func, + GError **error) +{ + return 345; +} + +void dumpsys_register_dump_cb_ok(void **state) +{ + void *t_handler = (void*)123; + dumpsys_dump_cb t_callback = (dumpsys_dump_cb)234; + assert_int_equal(dumpsys_register_dump_cb(t_callback, &t_handler), TIZEN_ERROR_NONE); + assert_ptr_equal(t_callback, t_handler); + dumpsys_unregister_dump_cb(t_handler); +} + +void dumpsys_register_dump_cb_fail1(void **state) +{ + void *t_handler = (void*)123; + dumpsys_dump_cb t_callback = (dumpsys_dump_cb)234; + dumpsys_register_dump_cb(t_callback, &t_handler); + assert_int_equal(dumpsys_register_dump_cb(t_callback, &t_handler), TIZEN_ERROR_ALREADY_IN_PROGRESS); + dumpsys_unregister_dump_cb(t_handler); +} + +void dumpsys_register_dump_cb_fail2(void **state) +{ + void *t_handler = (void*)123; + assert_int_equal(dumpsys_register_dump_cb(NULL, &t_handler), TIZEN_ERROR_INVALID_PARAMETER); + dumpsys_unregister_dump_cb(t_handler); +} + +void dumpsys_register_dump_cb_fail3(void **state) +{ + dumpsys_dump_cb t_callback = (dumpsys_dump_cb)234; + assert_int_equal(dumpsys_register_dump_cb(t_callback, NULL), TIZEN_ERROR_INVALID_PARAMETER); +} + +void dumpsys_unregister_dump_cb_ok(void **state) +{ + void *t_handler; + dumpsys_dump_cb t_callback = (dumpsys_dump_cb)234; + + dumpsys_register_dump_cb(t_callback, &t_handler); + assert_int_equal(dumpsys_unregister_dump_cb(t_handler), TIZEN_ERROR_NONE); +} + +void dumpsys_unregister_dump_cb_fail1(void **state) +{ + assert_int_equal(dumpsys_unregister_dump_cb(NULL), TIZEN_ERROR_INVALID_PARAMETER); +} + +void dumpsys_unregister_dump_cb_fail2(void **state) +{ + void *t_handler, *t_fake_handler = (void*)123; + dumpsys_dump_cb t_callback = (dumpsys_dump_cb)234; + + dumpsys_register_dump_cb(t_callback, &t_handler); + assert_int_equal(dumpsys_unregister_dump_cb(t_fake_handler), TIZEN_ERROR_ALREADY_IN_PROGRESS); + assert_int_equal(dumpsys_unregister_dump_cb(t_handler), TIZEN_ERROR_NONE); +} + +void dumpsys_get_args_count_ok(void **state) +{ + static const int ARGC = 33; + struct dumpsys_dump_data t_handler; + t_handler.argc = ARGC; + int t_count; + assert_int_equal(dumpsys_get_args_count(&t_handler, &t_count), TIZEN_ERROR_NONE); + assert_int_equal(t_count, ARGC); +} + +void dumpsys_get_args_count_fail1(void **state) +{ + int t_count; + assert_int_equal(dumpsys_get_args_count(NULL, &t_count), TIZEN_ERROR_INVALID_PARAMETER); +} + +void dumpsys_get_args_count_fail2(void **state) +{ + struct dumpsys_dump_data t_handler; + assert_int_equal(dumpsys_get_args_count(&t_handler, NULL), TIZEN_ERROR_INVALID_PARAMETER); +} + +void dumpsys_get_args_array_ok(void **state) +{ + char **ARRAY = (char**)123; + char **t_array; + struct dumpsys_dump_data t_handler; + t_handler.argv = ARRAY; + assert_int_equal(dumpsys_get_args_array(&t_handler, &t_array), TIZEN_ERROR_NONE); + assert_ptr_equal(t_array, ARRAY); +} + +void dumpsys_get_args_array_fail1(void **state) +{ + char **t_array = (char**)123; + assert_int_equal(dumpsys_get_args_array(NULL, &t_array), TIZEN_ERROR_INVALID_PARAMETER); +} + +void dumpsys_get_args_array_fail2(void **state) +{ + struct dumpsys_dump_data t_handler; + assert_int_equal(dumpsys_get_args_array(&t_handler, NULL), TIZEN_ERROR_INVALID_PARAMETER); +} + +void dumpsys_write_ok(void **state) +{ + int t_fd[2]; + char t_buff[] = "some text"; + struct dumpsys_dump_data t_handler; + + assert_int_equal(pipe2(t_fd, O_NONBLOCK), 0); + t_handler.fd = t_fd[1]; + + assert_int_equal(dumpsys_write(&t_handler, t_buff, sizeof(t_buff)), TIZEN_ERROR_NONE); + + char buff[sizeof(t_buff)]; + assert_int_equal(read(t_fd[0], buff, sizeof(buff)), sizeof(t_buff)); + assert_memory_equal(t_buff, buff, sizeof(t_buff)); +} + +void dumpsys_write_fail1(void **state) +{ + char t_buff[] = "some text"; + assert_int_equal(dumpsys_write(NULL, t_buff, sizeof(t_buff)), TIZEN_ERROR_INVALID_PARAMETER); +} + +void dumpsys_write_fail2(void **state) +{ + struct dumpsys_dump_data t_handler; + assert_int_equal(dumpsys_write(&t_handler, NULL, 0), TIZEN_ERROR_INVALID_PARAMETER); +} + +void get_args_ok(void **state) +{ + GVariantBuilder *builder = g_variant_builder_new(G_VARIANT_TYPE("as")); + g_variant_builder_add(builder, "s", "111"); + g_variant_builder_add(builder, "s", "222"); + g_variant_builder_add(builder, "s", "33333"); + GVariant *array = g_variant_new("(as)", builder); + g_variant_builder_unref(builder); + char **argv; + int argc = get_args(array, &argv); + assert_int_equal(argc, 3); + assert_string_equal(argv[0], "111"); + assert_string_equal(argv[1], "222"); + assert_string_equal(argv[2], "33333"); + g_variant_unref(array); +} + +int main(int argc, char *argv[]) +{ + const struct CMUnitTest tests[] = { + cmocka_unit_test(dumpsys_register_dump_cb_ok), + cmocka_unit_test(dumpsys_register_dump_cb_fail1), + cmocka_unit_test(dumpsys_register_dump_cb_fail2), + cmocka_unit_test(dumpsys_register_dump_cb_fail3), + cmocka_unit_test(dumpsys_unregister_dump_cb_ok), + cmocka_unit_test(dumpsys_unregister_dump_cb_fail1), + cmocka_unit_test(dumpsys_unregister_dump_cb_fail2), + cmocka_unit_test(dumpsys_get_args_count_ok), + cmocka_unit_test(dumpsys_get_args_count_fail1), + cmocka_unit_test(dumpsys_get_args_count_fail2), + cmocka_unit_test(dumpsys_get_args_array_ok), + cmocka_unit_test(dumpsys_get_args_array_fail1), + cmocka_unit_test(dumpsys_get_args_array_fail2), + cmocka_unit_test(dumpsys_write_ok), + cmocka_unit_test(dumpsys_write_fail1), + cmocka_unit_test(dumpsys_write_fail2), + cmocka_unit_test(get_args_ok), + }; + return cmocka_run_group_tests(tests, NULL, NULL); +} + diff --git a/tests/unit/test_dumpsys_service.c b/tests/unit/test_dumpsys_service.c new file mode 100644 index 0000000..e558759 --- /dev/null +++ b/tests/unit/test_dumpsys_service.c @@ -0,0 +1,333 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "dumpsys-service.c" + + +extern GUnixFDList* __real_g_unix_fd_list_new(); + +GUnixFDList* __wrap_g_unix_fd_list_new() +{ + return mock_ptr_type(GUnixFDList*); +} + +int __wrap_g_unix_fd_list_append(GUnixFDList *list, int fd, GError *error) +{ + check_expected(list); + return mock_type(int); +} + +gboolean __wrap_g_dbus_connection_send_message (GDBusConnection *connection, + GDBusMessage *message, + GDBusSendMessageFlags flags, + volatile guint32 *out_serial, + GError **error) +{ + check_expected(connection); + check_expected(flags); + return mock_type(gboolean); +} + +GDBusMessage* __wrap_g_dbus_connection_send_message_with_reply_sync(GDBusConnection *conn, + GDBusMessage *message, + GDBusSendMessageFlags flags, + gint timeout_msec, + volatile guint32 *out_serial, + GCancellable *cancellable, + GError **error) +{ + check_expected_ptr(conn); + check_expected(flags); + check_expected(timeout_msec); + return mock_ptr_type(GDBusMessage*); +} + +GDBusMessage* __wrap_g_dbus_method_invocation_get_message(GDBusMethodInvocation *invocation) +{ + check_expected(invocation); + return mock_ptr_type(GDBusMessage*); +} + +GDBusConnection* __wrap_g_bus_get_sync(GBusType bus_type, GCancellable *cancellable, GError **error) +{ + return mock_type(GDBusConnection*); +} + +guint __wrap_g_dbus_connection_register_object(GDBusConnection *connection, + const gchar *object_path, + GDBusInterfaceInfo *interface_info, + const GDBusInterfaceVTable *vtable, + gpointer user_data, + GDestroyNotify user_data_free_func, + GError **error) +{ + check_expected(connection); + return mock_type(guint); +} + +void __wrap_g_dbus_method_invocation_return_error (GDBusMethodInvocation *invocation, + GQuark domain, + gint code, + const gchar *format, + ...) +{ + check_expected(invocation); + check_expected(domain); + check_expected(code); +} + +void reset_fd_flag_ok(void **state) +{ + (void)state; + + int pip[2]; + int flags; + + assert_int_equal(pipe2(pip, O_NONBLOCK), 0); + flags = fcntl(pip[0], F_GETFL); + assert_int_equal(flags, O_NONBLOCK); + assert_true(reset_fd_flag(pip[0], O_NONBLOCK)); + flags = fcntl(pip[0], F_GETFL); + assert_int_equal(flags, 0); +} + +void reset_fd_flag_fail1(void **state) +{ + (void)state; + + assert_false(reset_fd_flag(55, 666)); +} + +void make_fifo_ok(void **state) +{ + (void)state; + + const char test_string[] = "ABCD"; + int write_fd, read_fd; + assert_true(make_fifo(&write_fd, &read_fd)); + assert_true(write_fd >= 0); + assert_true(read_fd >= 0); + + int res; + res = write(write_fd, test_string, sizeof(test_string)); + assert_int_equal(res, sizeof(test_string)); + + char buff[255]; + res = read(read_fd, buff, sizeof(buff)); + assert_int_equal(res, sizeof(test_string)); + assert_memory_equal(buff, test_string, sizeof(test_string)); + assert_int_equal(close(read_fd), 0); + assert_int_equal(close(write_fd), 0); +} + +void prepare_unix_fd_list_ok(void **state) +{ + GUnixFDList *test_list = (GUnixFDList*)321; + int test_fd = 123; + + will_return(__wrap_g_unix_fd_list_new, test_list); + + expect_value(__wrap_g_unix_fd_list_append, list, test_list); + will_return(__wrap_g_unix_fd_list_append, 1); + + assert_ptr_equal(prepare_unix_fd_list(test_fd), test_list); +} + +void prepare_message_ok(void **state) +{ + const char test_arg[] = "test arg"; + GVariantBuilder *test_builder = g_variant_builder_new(G_VARIANT_TYPE("as")); + g_variant_builder_add(test_builder, "s", test_arg); + + GVariant *test_params = g_variant_new("(ass)", test_builder, "testname"); + GUnixFDList *test_list = __real_g_unix_fd_list_new(); + + GDBusMessage *message = prepare_message(test_params, test_list); + + assert_true(message != NULL); + + GVariant *body = g_dbus_message_get_body(message); + GVariant *array = g_variant_get_child_value(body, 0); + + GVariantIter *iter; + + g_variant_get(array, "as", &iter); + + gchar *str; + assert_true(g_variant_iter_loop(iter, "s", &str)); + g_variant_iter_free(iter); + g_variant_unref(array); + assert_memory_equal(str, test_arg, sizeof(test_arg)); + + + + GUnixFDList *test_returned_list = g_dbus_message_get_unix_fd_list(message); + assert_ptr_equal(test_list, test_returned_list); + + g_object_unref(message); + g_variant_unref(test_params); + g_object_unref(test_list); +} + +void forward_dump_ok(void **state) +{ + GDBusConnection *test_conn = (GDBusConnection*)123; + GVariant *test_body = g_variant_new("(ss)", "test", "testname"); + GDBusMessage *test_reply = g_dbus_message_new(); + int test_write_fd = 123; + + GUnixFDList *test_list = __real_g_unix_fd_list_new(); + + will_return(__wrap_g_unix_fd_list_new, test_list); + + expect_value(__wrap_g_unix_fd_list_append, list, test_list); + will_return(__wrap_g_unix_fd_list_append, 1); + + expect_value(__wrap_g_dbus_connection_send_message_with_reply_sync, conn, test_conn); + expect_value(__wrap_g_dbus_connection_send_message_with_reply_sync, flags, G_DBUS_SEND_MESSAGE_FLAGS_NONE); + expect_value(__wrap_g_dbus_connection_send_message_with_reply_sync, timeout_msec, -1); + will_return(__wrap_g_dbus_connection_send_message_with_reply_sync, test_reply); + + assert_true(forward_dump(test_conn, test_body, test_write_fd)); +} + +void send_reply_ok(void **state) +{ + GDBusConnection *test_conn = (GDBusConnection*)123; + GDBusMethodInvocation *test_invocation = (GDBusMethodInvocation*)234; + GDBusMessage *test_message = g_dbus_message_new_method_call("abc.def.ghi", "/Jkl/Mno/Pqr", "stu.wxy", "z"); + g_dbus_message_set_serial(test_message, 123); + + expect_value(__wrap_g_dbus_connection_send_message, connection, test_conn); + expect_value(__wrap_g_dbus_connection_send_message, flags, G_DBUS_SEND_MESSAGE_FLAGS_NONE); + will_return(__wrap_g_dbus_connection_send_message, true); + + + GUnixFDList *test_list1 = __real_g_unix_fd_list_new(); + will_return(__wrap_g_unix_fd_list_new, test_list1); + expect_value(__wrap_g_unix_fd_list_append, list, test_list1); + will_return(__wrap_g_unix_fd_list_append, 1); + + + int test_fd = 123; + bool test_result = true; + + send_reply(test_conn, test_invocation, test_message, test_fd, test_result); +} + +void dump_call_handler_ok(void **state) +{ + const char test_arg[] = "test arg"; + const char test_name[] = "testname"; + + GVariantBuilder *test_builder = g_variant_builder_new(G_VARIANT_TYPE("as")); + g_variant_builder_add(test_builder, "s", test_arg); + GVariant *test_params = g_variant_new("(ass)", test_builder, test_name); + GDBusMessage *test_message = g_dbus_message_new_method_call("abc.def.ghi", "/Jkl/Mno/Pqr", "stu.wxy", "z"); + g_dbus_message_set_serial(test_message, 123); + g_dbus_message_set_body(test_message, test_params); + + GUnixFDList *test_list1 = __real_g_unix_fd_list_new(); + will_return(__wrap_g_unix_fd_list_new, test_list1); + expect_value(__wrap_g_unix_fd_list_append, list, test_list1); + will_return(__wrap_g_unix_fd_list_append, 1); + + + GUnixFDList *test_list2 = __real_g_unix_fd_list_new(); + will_return(__wrap_g_unix_fd_list_new, test_list2); + expect_value(__wrap_g_unix_fd_list_append, list, test_list2); + will_return(__wrap_g_unix_fd_list_append, 1); + + GDBusConnection *test_conn = (GDBusConnection*)123; + GDBusMessage *test_reply = g_dbus_message_new(); + expect_value(__wrap_g_dbus_connection_send_message_with_reply_sync, conn, test_conn); + expect_value(__wrap_g_dbus_connection_send_message_with_reply_sync, flags, G_DBUS_SEND_MESSAGE_FLAGS_NONE); + expect_value(__wrap_g_dbus_connection_send_message_with_reply_sync, timeout_msec, -1); + will_return(__wrap_g_dbus_connection_send_message_with_reply_sync, test_reply); + + GDBusMethodInvocation *test_invocation = (GDBusMethodInvocation*)234; + expect_value(__wrap_g_dbus_method_invocation_get_message, invocation, test_invocation); + will_return(__wrap_g_dbus_method_invocation_get_message, test_message); + + expect_value(__wrap_g_dbus_connection_send_message, connection, test_conn); + expect_value(__wrap_g_dbus_connection_send_message, flags, G_DBUS_SEND_MESSAGE_FLAGS_NONE); + will_return(__wrap_g_dbus_connection_send_message, true); + + dump_call_handler(test_conn, test_params, test_invocation); + + g_variant_unref(test_params); +} + +void dump_call_handler_fail(void **state) +{ + GDBusConnection *test_conn = (GDBusConnection*)123; + GVariant *test_params = g_variant_new("s", "test param"); + GDBusMethodInvocation *test_invocation = (GDBusMethodInvocation*)234; + + expect_value(__wrap_g_dbus_method_invocation_return_error, invocation, test_invocation); + expect_value(__wrap_g_dbus_method_invocation_return_error, domain, CS_ERROR); + expect_value(__wrap_g_dbus_method_invocation_return_error, code, CS_ERR_PARAM); + dump_call_handler(test_conn, test_params, test_invocation); + + g_variant_unref(test_params); +} + +void dbus_init_ok(void **state) +{ + GDBusConnection *t_conn = (GDBusConnection*)g_object_new(G_TYPE_DBUS_CONNECTION, NULL); + will_return(__wrap_g_bus_get_sync, t_conn); + assert_true(dbus_init()); + g_object_unref(t_conn); +} + +void dbus_init_fail1(void **state) +{ + will_return(__wrap_g_bus_get_sync, NULL); + assert_false(dbus_init()); +} + +void on_bus_acquired_ok(void **state) +{ + GDBusConnection *t_conn = (GDBusConnection*)g_object_new(G_TYPE_DBUS_CONNECTION, NULL); + expect_value(__wrap_g_dbus_connection_register_object, connection, t_conn); + will_return(__wrap_g_dbus_connection_register_object, 123); + on_bus_acquired(t_conn, "some", NULL); + g_object_unref(t_conn); +} + +void on_bus_acquired_fail(void **state) +{ + GDBusConnection *t_conn = (GDBusConnection*)g_object_new(G_TYPE_DBUS_CONNECTION, NULL); + expect_value(__wrap_g_dbus_connection_register_object, connection, t_conn); + will_return(__wrap_g_dbus_connection_register_object, 0); + on_bus_acquired(t_conn, "some", NULL); + g_object_unref(t_conn); +} + +int main(int argc, char *argv[]) +{ + const struct CMUnitTest tests[] = { + cmocka_unit_test(reset_fd_flag_ok), + cmocka_unit_test(reset_fd_flag_fail1), + cmocka_unit_test(make_fifo_ok), + cmocka_unit_test(prepare_unix_fd_list_ok), + cmocka_unit_test(prepare_message_ok), + cmocka_unit_test(forward_dump_ok), + cmocka_unit_test(send_reply_ok), + cmocka_unit_test(dump_call_handler_ok), + cmocka_unit_test(dump_call_handler_fail), + cmocka_unit_test(dbus_init_ok), + cmocka_unit_test(dbus_init_fail1), + cmocka_unit_test(on_bus_acquired_ok), + cmocka_unit_test(on_bus_acquired_fail), + }; + return cmocka_run_group_tests(tests, NULL, NULL); +} + diff --git a/tests/unit/test_libdumpsys.c b/tests/unit/test_libdumpsys.c new file mode 100644 index 0000000..d0aef31 --- /dev/null +++ b/tests/unit/test_libdumpsys.c @@ -0,0 +1,317 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "libdumpsys.c" + +GDBusConnection* __wrap_g_bus_get_sync(GBusType bus_type, GCancellable *cancellable, GError **error) +{ + check_expected(bus_type); + check_expected(cancellable); + check_expected(*error); + return mock_type(GDBusConnection*); +} + +GDBusMessage* __wrap_g_dbus_connection_send_message_with_reply_sync(GDBusConnection *conn, + GDBusMessage *message, + GDBusSendMessageFlags flags, + gint timeout_msec, + volatile guint32 *out_serial, + GCancellable *cancellable, + GError **error) +{ + check_expected(conn); + check_expected(flags); + check_expected(timeout_msec); + check_expected(cancellable); + check_expected(*error); + *error = mock_type(GError*); + return mock_type(GDBusMessage*); +} + +GDBusMessageType __wrap_g_dbus_message_get_message_type(GDBusMessage *message) +{ + check_expected(message); + return mock_type(GDBusMessageType); +} + +GUnixFDList* __wrap_g_dbus_message_get_unix_fd_list(GDBusMessage *message) +{ + check_expected(message); + return mock_type(GUnixFDList*); +} + +gint __wrap_g_unix_fd_list_get(GUnixFDList *list, + gint index, + GError **error) +{ + check_expected(list); + check_expected(index); + *error = mock_type(GError*); + return mock_type(gint); +} + +void dumpsys_dump_ok(void **state) +{ + static char *t_service_name = "some.serivce.name"; + static char *t_argv[] = {"arg1", "arg2"}; + GDBusConnection *t_connection = (GDBusConnection*)123; + GVariant *t_body = g_variant_new("(i)", 123); + GDBusMessage *t_reply = g_dbus_message_new(); + GUnixFDList *t_fd_list = g_unix_fd_list_new(); + int t_fd = 456; + GError *t_error = NULL; + g_unix_fd_list_append(t_fd_list, t_fd, &t_error); + g_dbus_message_set_body(t_reply, t_body); + g_dbus_message_set_unix_fd_list(t_reply, t_fd_list); + int t_out_fd; + + expect_value(__wrap_g_bus_get_sync, bus_type, G_BUS_TYPE_SYSTEM); + expect_value(__wrap_g_bus_get_sync, cancellable, NULL); + expect_value(__wrap_g_bus_get_sync, *error, NULL); + will_return(__wrap_g_bus_get_sync, t_connection); + + expect_value(__wrap_g_dbus_connection_send_message_with_reply_sync, conn, t_connection); + expect_value(__wrap_g_dbus_connection_send_message_with_reply_sync, flags, G_DBUS_SEND_MESSAGE_FLAGS_NONE); + expect_value(__wrap_g_dbus_connection_send_message_with_reply_sync, timeout_msec, SERVICE_TIMEOUT_MS); + expect_value(__wrap_g_dbus_connection_send_message_with_reply_sync, cancellable, NULL); + expect_value(__wrap_g_dbus_connection_send_message_with_reply_sync, *error, NULL); + will_return(__wrap_g_dbus_connection_send_message_with_reply_sync, NULL); + will_return(__wrap_g_dbus_connection_send_message_with_reply_sync, t_reply); + + expect_value(__wrap_g_dbus_message_get_message_type, message, t_reply); + will_return(__wrap_g_dbus_message_get_message_type, G_DBUS_MESSAGE_TYPE_METHOD_RETURN); + + expect_value(__wrap_g_dbus_message_get_unix_fd_list, message, t_reply); + will_return(__wrap_g_dbus_message_get_unix_fd_list, t_fd_list); + + expect_value(__wrap_g_unix_fd_list_get, list, t_fd_list); + expect_value(__wrap_g_unix_fd_list_get, index, 0); + will_return(__wrap_g_unix_fd_list_get, NULL); + will_return(__wrap_g_unix_fd_list_get, t_fd); + + assert_int_equal(dumpsys_dump(t_service_name, 2, t_argv, &t_out_fd), TIZEN_ERROR_NONE); + assert_int_equal(t_out_fd, t_fd); + g_object_unref(t_reply); +} + +void dumpsys_dump_fail1(void **state) +{ + static char *t_argv[] = {"arg1", "arg2"}; + int t_out_fd; + assert_int_equal(dumpsys_dump(NULL, 2, t_argv, &t_out_fd), TIZEN_ERROR_INVALID_PARAMETER); +} + +void dumpsys_dump_fail2(void **state) +{ + static char *t_service_name = "some.serivce.name"; + static char *t_argv[] = {"arg1", "arg2"}; + int t_out_fd; + assert_int_equal(dumpsys_dump(t_service_name, -1, t_argv, &t_out_fd), TIZEN_ERROR_INVALID_PARAMETER); +} + +void dumpsys_dump_fail3(void **state) +{ + static char *t_service_name = "some.serivce.name"; + int t_out_fd; + assert_int_equal(dumpsys_dump(t_service_name, 2, NULL, &t_out_fd), TIZEN_ERROR_INVALID_PARAMETER); +} + +void dumpsys_dump_fail4(void **state) +{ + static char *t_service_name = "some.serivce.name"; + static char *t_argv[] = {"arg1", "arg2"}; + assert_int_equal(dumpsys_dump(t_service_name, 2, t_argv, NULL), TIZEN_ERROR_INVALID_PARAMETER); +} + +void dumpsys_dump_fail5(void **state) +{ + static char *t_service_name = "some.serivce.name"; + static char *t_argv[] = {"arg1", "arg2"}; + int t_out_fd; + + + expect_value(__wrap_g_bus_get_sync, bus_type, G_BUS_TYPE_SYSTEM); + expect_value(__wrap_g_bus_get_sync, cancellable, NULL); + expect_value(__wrap_g_bus_get_sync, *error, NULL); + will_return(__wrap_g_bus_get_sync, NULL); + + assert_int_equal(dumpsys_dump(t_service_name, 2, t_argv, &t_out_fd), TIZEN_ERROR_CONNECTION_REFUSED); +} + +void dumpsys_dump_fail6(void **state) +{ + static char *t_service_name = "some.serivce.name"; + static char *t_argv[] = {"arg1", "arg2"}; + GDBusConnection *t_connection = (GDBusConnection*)123; + GVariant *t_body = g_variant_new("(i)", 123); + GDBusMessage *t_reply = g_dbus_message_new(); + GUnixFDList *t_fd_list = g_unix_fd_list_new(); + int t_fd = 456; + GError *t_error = NULL; + g_unix_fd_list_append(t_fd_list, t_fd, &t_error); + g_dbus_message_set_body(t_reply, t_body); + g_dbus_message_set_unix_fd_list(t_reply, t_fd_list); + int t_out_fd; + + expect_value(__wrap_g_bus_get_sync, bus_type, G_BUS_TYPE_SYSTEM); + expect_value(__wrap_g_bus_get_sync, cancellable, NULL); + expect_value(__wrap_g_bus_get_sync, *error, NULL); + will_return(__wrap_g_bus_get_sync, t_connection); + + expect_value(__wrap_g_dbus_connection_send_message_with_reply_sync, conn, t_connection); + expect_value(__wrap_g_dbus_connection_send_message_with_reply_sync, flags, G_DBUS_SEND_MESSAGE_FLAGS_NONE); + expect_value(__wrap_g_dbus_connection_send_message_with_reply_sync, timeout_msec, SERVICE_TIMEOUT_MS); + expect_value(__wrap_g_dbus_connection_send_message_with_reply_sync, cancellable, NULL); + expect_value(__wrap_g_dbus_connection_send_message_with_reply_sync, *error, NULL); + will_return(__wrap_g_dbus_connection_send_message_with_reply_sync, g_error_new(1, 2, "error")); // some error + will_return(__wrap_g_dbus_connection_send_message_with_reply_sync, t_reply); + + + + expect_value(__wrap_g_dbus_message_get_message_type, message, t_reply); + will_return(__wrap_g_dbus_message_get_message_type, G_DBUS_MESSAGE_TYPE_METHOD_RETURN); + + assert_int_equal(dumpsys_dump(t_service_name, 2, t_argv, &t_out_fd), TIZEN_ERROR_NETWORK_UNREACHABLE); + g_object_unref(t_reply); +} + +void dumpsys_dump_fail7(void **state) +{ + static char *t_service_name = "some.serivce.name"; + static char *t_argv[] = {"arg1", "arg2"}; + GDBusConnection *t_connection = (GDBusConnection*)123; + GVariant *t_body = g_variant_new("(i)", DUMP_FAIL); + GDBusMessage *t_reply = g_dbus_message_new(); + GUnixFDList *t_fd_list = g_unix_fd_list_new(); + int t_fd = 456; + GError *t_error = NULL; + g_unix_fd_list_append(t_fd_list, t_fd, &t_error); + g_dbus_message_set_body(t_reply, t_body); + g_dbus_message_set_unix_fd_list(t_reply, t_fd_list); + int t_out_fd; + + expect_value(__wrap_g_bus_get_sync, bus_type, G_BUS_TYPE_SYSTEM); + expect_value(__wrap_g_bus_get_sync, cancellable, NULL); + expect_value(__wrap_g_bus_get_sync, *error, NULL); + will_return(__wrap_g_bus_get_sync, t_connection); + + expect_value(__wrap_g_dbus_connection_send_message_with_reply_sync, conn, t_connection); + expect_value(__wrap_g_dbus_connection_send_message_with_reply_sync, flags, G_DBUS_SEND_MESSAGE_FLAGS_NONE); + expect_value(__wrap_g_dbus_connection_send_message_with_reply_sync, timeout_msec, SERVICE_TIMEOUT_MS); + expect_value(__wrap_g_dbus_connection_send_message_with_reply_sync, cancellable, NULL); + expect_value(__wrap_g_dbus_connection_send_message_with_reply_sync, *error, NULL); + will_return(__wrap_g_dbus_connection_send_message_with_reply_sync, NULL); + will_return(__wrap_g_dbus_connection_send_message_with_reply_sync, t_reply); + + expect_value(__wrap_g_dbus_message_get_message_type, message, t_reply); + will_return(__wrap_g_dbus_message_get_message_type, G_DBUS_MESSAGE_TYPE_METHOD_RETURN); + + assert_int_equal(dumpsys_dump(t_service_name, 2, t_argv, &t_out_fd), TIZEN_ERROR_NO_ROUTE_TO_HOST); + g_object_unref(t_reply); +} + +void dumpsys_dump_fail8(void **state) +{ + static char *t_service_name = "some.serivce.name"; + static char *t_argv[] = {"arg1", "arg2"}; + GDBusConnection *t_connection = (GDBusConnection*)123; + GVariant *t_body = g_variant_new("(i)", 123); + GDBusMessage *t_reply = g_dbus_message_new(); + GUnixFDList *t_fd_list = g_unix_fd_list_new(); + int t_fd = 456; + GError *t_error = NULL; + g_unix_fd_list_append(t_fd_list, t_fd, &t_error); + g_dbus_message_set_body(t_reply, t_body); + g_dbus_message_set_unix_fd_list(t_reply, t_fd_list); + int t_out_fd; + + expect_value(__wrap_g_bus_get_sync, bus_type, G_BUS_TYPE_SYSTEM); + expect_value(__wrap_g_bus_get_sync, cancellable, NULL); + expect_value(__wrap_g_bus_get_sync, *error, NULL); + will_return(__wrap_g_bus_get_sync, t_connection); + + expect_value(__wrap_g_dbus_connection_send_message_with_reply_sync, conn, t_connection); + expect_value(__wrap_g_dbus_connection_send_message_with_reply_sync, flags, G_DBUS_SEND_MESSAGE_FLAGS_NONE); + expect_value(__wrap_g_dbus_connection_send_message_with_reply_sync, timeout_msec, SERVICE_TIMEOUT_MS); + expect_value(__wrap_g_dbus_connection_send_message_with_reply_sync, cancellable, NULL); + expect_value(__wrap_g_dbus_connection_send_message_with_reply_sync, *error, NULL); + will_return(__wrap_g_dbus_connection_send_message_with_reply_sync, NULL); + will_return(__wrap_g_dbus_connection_send_message_with_reply_sync, t_reply); + + expect_value(__wrap_g_dbus_message_get_message_type, message, t_reply); + will_return(__wrap_g_dbus_message_get_message_type, G_DBUS_MESSAGE_TYPE_METHOD_RETURN); + + expect_value(__wrap_g_dbus_message_get_unix_fd_list, message, t_reply); + will_return(__wrap_g_dbus_message_get_unix_fd_list, NULL); + + assert_int_equal(dumpsys_dump(t_service_name, 2, t_argv, &t_out_fd), TIZEN_ERROR_INVALID_OPERATION); + g_object_unref(t_reply); +} + +void dumpsys_dump_fail9(void **state) +{ + static char *t_service_name = "some.serivce.name"; + static char *t_argv[] = {"arg1", "arg2"}; + GDBusConnection *t_connection = (GDBusConnection*)123; + GVariant *t_body = g_variant_new("(i)", 123); + GDBusMessage *t_reply = g_dbus_message_new(); + GUnixFDList *t_fd_list = g_unix_fd_list_new(); + int t_fd = 456; + GError *t_error = NULL; + g_unix_fd_list_append(t_fd_list, t_fd, &t_error); + g_dbus_message_set_body(t_reply, t_body); + g_dbus_message_set_unix_fd_list(t_reply, t_fd_list); + int t_out_fd; + + expect_value(__wrap_g_bus_get_sync, bus_type, G_BUS_TYPE_SYSTEM); + expect_value(__wrap_g_bus_get_sync, cancellable, NULL); + expect_value(__wrap_g_bus_get_sync, *error, NULL); + will_return(__wrap_g_bus_get_sync, t_connection); + + expect_value(__wrap_g_dbus_connection_send_message_with_reply_sync, conn, t_connection); + expect_value(__wrap_g_dbus_connection_send_message_with_reply_sync, flags, G_DBUS_SEND_MESSAGE_FLAGS_NONE); + expect_value(__wrap_g_dbus_connection_send_message_with_reply_sync, timeout_msec, SERVICE_TIMEOUT_MS); + expect_value(__wrap_g_dbus_connection_send_message_with_reply_sync, cancellable, NULL); + expect_value(__wrap_g_dbus_connection_send_message_with_reply_sync, *error, NULL); + will_return(__wrap_g_dbus_connection_send_message_with_reply_sync, NULL); + will_return(__wrap_g_dbus_connection_send_message_with_reply_sync, t_reply); + + expect_value(__wrap_g_dbus_message_get_message_type, message, t_reply); + will_return(__wrap_g_dbus_message_get_message_type, G_DBUS_MESSAGE_TYPE_METHOD_RETURN); + + expect_value(__wrap_g_dbus_message_get_unix_fd_list, message, t_reply); + will_return(__wrap_g_dbus_message_get_unix_fd_list, t_fd_list); + + expect_value(__wrap_g_unix_fd_list_get, list, t_fd_list); + expect_value(__wrap_g_unix_fd_list_get, index, 0); + will_return(__wrap_g_unix_fd_list_get, g_error_new(1, 1, "some error")); + will_return(__wrap_g_unix_fd_list_get, t_fd); + + assert_int_equal(dumpsys_dump(t_service_name, 2, t_argv, &t_out_fd), TIZEN_ERROR_INVALID_OPERATION); + g_object_unref(t_reply); +} + +int main(int argc, char *argv[]) +{ + const struct CMUnitTest tests[] = { + cmocka_unit_test(dumpsys_dump_ok), + cmocka_unit_test(dumpsys_dump_fail1), + cmocka_unit_test(dumpsys_dump_fail2), + cmocka_unit_test(dumpsys_dump_fail3), + cmocka_unit_test(dumpsys_dump_fail4), + cmocka_unit_test(dumpsys_dump_fail5), + cmocka_unit_test(dumpsys_dump_fail6), + cmocka_unit_test(dumpsys_dump_fail7), + cmocka_unit_test(dumpsys_dump_fail8), + cmocka_unit_test(dumpsys_dump_fail9), + }; + return cmocka_run_group_tests(tests, NULL, NULL); +} -- 2.7.4