From 63f8ce1c71c398d5e6036c3a324d593fe36732fb Mon Sep 17 00:00:00 2001 From: Inkyun Kil Date: Thu, 26 Jan 2023 09:15:09 +0900 Subject: [PATCH 01/16] Trivial updates - Fix wrong return value - Remove needless apis Change-Id: Ibc9924cfefe472a5978de50edbaf0df031ea5ebc Signed-off-by: Inkyun Kil --- include/badge_service.h | 3 --- include/notification_service.h | 2 -- src/service_common.c | 2 +- 3 files changed, 1 insertion(+), 6 deletions(-) diff --git a/include/badge_service.h b/include/badge_service.h index 75c0814..84013b4 100755 --- a/include/badge_service.h +++ b/include/badge_service.h @@ -37,9 +37,6 @@ int badge_set_display_option(GVariant *parameters, GVariant **reply_body, uid_t int badge_get_display_option(GVariant *parameters, GVariant **reply_body, const gchar *sender, uid_t uid); int badge_update_badge_setting(GVariant *parameters, GVariant **reply_body, uid_t uid); int badge_get_setting_by_appid(GVariant *parameters, GVariant **reply_body, uid_t uid); -int badge_get_setting_array(GVariant *parameters, GVariant **reply_body, uid_t uid); -int badge_set_setting_property(GVariant *parameters, GVariant **reply_body, uid_t uid); -int badge_get_setting_property(GVariant *parameters, GVariant **reply_body, uid_t uid); int badge_init_display(GVariant *parameters, GVariant **reply_body, uid_t uid); int badge_register_dbus_interface(); diff --git a/include/notification_service.h b/include/notification_service.h index d4c411f..fb52119 100755 --- a/include/notification_service.h +++ b/include/notification_service.h @@ -34,8 +34,6 @@ int notification_refresh_noti(GVariant *parameters, GVariant **reply_body, uid_t int notification_del_noti_single(GVariant *parameters, GVariant **reply_body, uid_t uid); int notification_del_noti_multiple(GVariant *parameters, GVariant **reply_body, uid_t uid); int notification_del_noti_by_display_applist(GVariant *parameters, GVariant **reply_body, uid_t uid); -int notification_set_noti_property(GVariant *parameters, GVariant **reply_body, uid_t uid); -int notification_get_noti_property(GVariant *parameters, GVariant **reply_body, uid_t uid); int notification_get_noti_count(GVariant *parameters, GVariant **reply_body, uid_t uid); int notification_get_noti_all_count(GVariant *parameters, GVariant **reply_body, uid_t uid); int notification_update_noti_setting(GVariant *parameters, GVariant **reply_body, uid_t uid); diff --git a/src/service_common.c b/src/service_common.c index a79ddf6..7035207 100644 --- a/src/service_common.c +++ b/src/service_common.c @@ -269,7 +269,7 @@ int service_register(GVariant *parameters, GVariant **reply_body, const gchar *s m_info = (monitoring_info_s *)calloc(1, sizeof(monitoring_info_s)); if (m_info == NULL) { ERR("Failed to alloc memory"); - return SERVICE_COMMON_ERROR_IO_ERROR; + return SERVICE_COMMON_ERROR_OUT_OF_MEMORY; } m_info->bus_name = strdup(bus_name); -- 2.7.4 From 1dd53f453b9b90f04cb6dbf3c1df63cd434803b5 Mon Sep 17 00:00:00 2001 From: Inkyun Kil Date: Wed, 18 Jan 2023 14:19:59 +0900 Subject: [PATCH 02/16] Improve coverage - Add unittests for notification_service - Add unittests for shortcut_service - Add unittests for badge_service - Add unittests for service_common Change-Id: I3bbcaa61006a7a62de43de4ff5b0dc4c1197294b --- CMakeLists.txt | 43 +- src/CMakeLists.txt | 44 ++ tests/mock/aul_mock.cc | 29 ++ tests/mock/aul_mock.h | 34 ++ tests/mock/badge_mock.cc | 81 ++++ tests/mock/badge_mock.h | 54 +++ tests/mock/gio_mock.cc | 110 +++-- tests/mock/gio_mock.h | 73 ++- tests/mock/notification_mock.cc | 245 ++++++++++ tests/mock/notification_mock.h | 105 +++++ tests/mock/pkgmgr_info_mock.cc | 63 +++ tests/mock/pkgmgr_info_mock.h | 46 ++ tests/mock/shortcut_mock.cc | 27 ++ tests/mock/shortcut_mock.h | 36 ++ tests/mock/tzplatform_config_mock.cc | 24 + tests/mock/tzplatform_config_mock.h | 32 ++ tests/unit_tests/CMakeLists.txt | 35 +- tests/unit_tests/src/test_badge_service.cc | 201 +++++++- .../unit_tests/src/test_notification_ex_service.cc | 83 ++++ tests/unit_tests/src/test_notification_service.cc | 515 +++++++++++++++++++++ tests/unit_tests/src/test_service_common.cc | 105 +++++ tests/unit_tests/src/test_shortcut_service.cc | 110 +++++ 22 files changed, 1976 insertions(+), 119 deletions(-) create mode 100644 src/CMakeLists.txt create mode 100644 tests/mock/aul_mock.cc create mode 100644 tests/mock/aul_mock.h create mode 100644 tests/mock/badge_mock.cc create mode 100644 tests/mock/badge_mock.h create mode 100644 tests/mock/notification_mock.cc create mode 100644 tests/mock/notification_mock.h create mode 100644 tests/mock/pkgmgr_info_mock.cc create mode 100644 tests/mock/pkgmgr_info_mock.h create mode 100644 tests/mock/shortcut_mock.cc create mode 100644 tests/mock/shortcut_mock.h create mode 100644 tests/mock/tzplatform_config_mock.cc create mode 100644 tests/mock/tzplatform_config_mock.h create mode 100644 tests/unit_tests/src/test_notification_ex_service.cc create mode 100644 tests/unit_tests/src/test_notification_service.cc create mode 100644 tests/unit_tests/src/test_service_common.cc create mode 100644 tests/unit_tests/src/test_shortcut_service.cc diff --git a/CMakeLists.txt b/CMakeLists.txt index d9355ec..eac03db 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,49 +9,8 @@ SET(INCLUDEDIR "\${prefix}/include/${PROJECT_NAME}") SET(VERSION_MAJOR 1) SET(VERSION "${VERSION_MAJOR}.0.0") -INCLUDE(FindPkgConfig) -pkg_check_modules(pkgs REQUIRED - dlog - aul - vconf - sqlite3 - glib-2.0 - gio-2.0 - gio-unix-2.0 - bundle - pkgmgr - notification - notification-ex - badge - libsmack - shortcut - pkgmgr-info - libsystemd - capi-appfw-app-manager - alarm-service - libtzplatform-config - security-manager -) +ADD_SUBDIRECTORY(src) -FOREACH(flag ${pkgs_CFLAGS}) - SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}") -ENDFOREACH(flag) -SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -Wall -Werror -Winline") - -SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${EXTRA_CFLAGS} -std=c++11") -SET(CMAKE_CXX_FLAGS_DEBUG "-O0 -g") -SET(CMAKE_CXX_FLAGS_RELEASE "-O2") - -INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/include) - -AUX_SOURCE_DIRECTORY(src SOURCES) -ADD_EXECUTABLE(${PROJECT_NAME} ${SOURCES}) - -SET_TARGET_PROPERTIES(${PROJECT_NAME} PROPERTIES COMPILE_FLAGS "${EXTRA_CFLAGS} -fPIE") -SET_TARGET_PROPERTIES(${PROJECT_NAME} PROPERTIES LINK_FLAGS "-pie") -TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${pkgs_LDFLAGS}) - -INSTALL(TARGETS ${PROJECT_NAME} DESTINATION /usr/bin PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE) CONFIGURE_FILE(data-provider-master.conf.in data-provider-master.conf @ONLY) CONFIGURE_FILE(org.tizen.data-provider-master.service.in org.tizen.data_provider_service.service @ONLY) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 0000000..e439599 --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1,44 @@ + +INCLUDE(FindPkgConfig) +pkg_check_modules(pkgs REQUIRED + dlog + aul + vconf + sqlite3 + glib-2.0 + gio-2.0 + gio-unix-2.0 + bundle + pkgmgr + notification + notification-ex + badge + libsmack + shortcut + pkgmgr-info + libsystemd + capi-appfw-app-manager + alarm-service + libtzplatform-config + security-manager +) + +FOREACH(flag ${pkgs_CFLAGS}) + SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}") +ENDFOREACH(flag) +SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -Wall -Werror -Winline") + +SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${EXTRA_CFLAGS} -std=c++14") +SET(CMAKE_CXX_FLAGS_DEBUG "-O0 -g") +SET(CMAKE_CXX_FLAGS_RELEASE "-O2") + +INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/../include) + +AUX_SOURCE_DIRECTORY(../src SOURCES) +ADD_EXECUTABLE(${PROJECT_NAME} ${SOURCES}) + +SET_TARGET_PROPERTIES(${PROJECT_NAME} PROPERTIES COMPILE_FLAGS "${EXTRA_CFLAGS} -fPIE") +SET_TARGET_PROPERTIES(${PROJECT_NAME} PROPERTIES LINK_FLAGS "-pie") +TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${pkgs_LDFLAGS}) + +INSTALL(TARGETS ${PROJECT_NAME} DESTINATION /usr/bin PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE) \ No newline at end of file diff --git a/tests/mock/aul_mock.cc b/tests/mock/aul_mock.cc new file mode 100644 index 0000000..03f0b6f --- /dev/null +++ b/tests/mock/aul_mock.cc @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2023 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 "aul_mock.h" +#include "mock_hook.h" +#include "test_fixture.h" + +extern "C" int aul_app_get_appid_bypid_for_uid(int arg0, char* arg1, int arg2, + uid_t arg3) { + return MOCK_HOOK_P4(AulMock, aul_app_get_appid_bypid_for_uid, arg0, arg1, + arg2, arg3); +} + +extern "C" int aul_app_get_appid_bypid(int pid, char* appid, int size) { + return MOCK_HOOK_P3(AulMock, aul_app_get_appid_bypid, pid, appid, size); +} diff --git a/tests/mock/aul_mock.h b/tests/mock/aul_mock.h new file mode 100644 index 0000000..399648b --- /dev/null +++ b/tests/mock/aul_mock.h @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2023 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. + */ + +#ifndef MOCK_AUL_MOCK_H_ +#define MOCK_AUL_MOCK_H_ + +#include +#include +#include + +#include "module_mock.h" + +class AulMock : public virtual ModuleMock { + public: + virtual ~AulMock() {} + + MOCK_METHOD4(aul_app_get_appid_bypid_for_uid, int (int, char*, int, uid_t)); + MOCK_METHOD3(aul_app_get_appid_bypid, int (int, char*, int)); +}; + +#endif // MOCK_AUL_MOCK_H_ \ No newline at end of file diff --git a/tests/mock/badge_mock.cc b/tests/mock/badge_mock.cc new file mode 100644 index 0000000..3726ae3 --- /dev/null +++ b/tests/mock/badge_mock.cc @@ -0,0 +1,81 @@ +/* + * Copyright (c) 2023 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 "badge_mock.h" +#include "mock_hook.h" +#include "test_fixture.h" + +extern "C" int badge_db_init(void) { + return MOCK_HOOK_P0(BadgeMock, badge_db_init); +} + +extern "C" int badge_db_is_existing(const char* arg0, bool* arg1, uid_t arg2) { + return MOCK_HOOK_P3(BadgeMock, badge_db_is_existing, arg0, arg1, arg2); +} + +extern "C" int badge_db_get_list(GList** arg0, uid_t arg1) { + return MOCK_HOOK_P2(BadgeMock, badge_db_get_list, arg0, arg1); +} + +extern "C" int badge_db_insert(const char* arg0, const char* arg1, + const char* arg2, uid_t arg3) { + return MOCK_HOOK_P4(BadgeMock, badge_db_insert, arg0, arg1, arg2, arg3); +} + +extern "C" int badge_db_delete(const char* arg0, const char* arg1, + uid_t arg2, pid_t arg3) { + return MOCK_HOOK_P4(BadgeMock, badge_db_delete, arg0, arg1, arg2, arg3); +} + +extern "C" int badge_db_set_count(const char* arg0, const char* arg1, + unsigned int arg2, uid_t arg3, pid_t arg4) { + return MOCK_HOOK_P5(BadgeMock, badge_db_set_count, arg0, arg1, arg2, arg3, arg4); +} + +extern "C" int badge_db_get_count(const char* arg0, unsigned int* arg1, + uid_t arg2) { + return MOCK_HOOK_P3(BadgeMock, badge_db_get_count, arg0, arg1, arg2); +} + +extern "C" int badge_db_set_display_option(const char* arg0, unsigned int arg1, + uid_t arg2) { + return MOCK_HOOK_P3(BadgeMock, badge_db_set_display_option, arg0, arg1, arg2); +} + +extern "C" int badge_db_get_display_option(const char* arg0, unsigned int* arg1, + uid_t arg2) { + return MOCK_HOOK_P3(BadgeMock, badge_db_get_display_option, arg0, arg1, arg2); +} + +extern "C" int badge_db_get_allow_to_display_by_appid(char* arg0, int* arg1, + uid_t arg2) { + return MOCK_HOOK_P3(BadgeMock, badge_db_get_allow_to_display_by_appid, arg0, + arg1, arg2); +} + +extern "C" int badge_db_update_setting(char* arg0, char* arg1, int arg2, + uid_t arg3) { + return MOCK_HOOK_P4(BadgeMock, badge_db_update_setting, arg0, arg1, arg2, arg3); +} + +extern "C" int badge_setting_get_appid(badge_setting_h arg0, char** arg1) { + return MOCK_HOOK_P2(BadgeMock, badge_setting_get_appid, arg0, arg1); +} + +extern "C" int badge_db_get_setting_by_appid(const char* arg0, + badge_setting_h* arg1, uid_t arg2) { + return MOCK_HOOK_P3(BadgeMock, badge_db_get_setting_by_appid, arg0, arg1, arg2); +} \ No newline at end of file diff --git a/tests/mock/badge_mock.h b/tests/mock/badge_mock.h new file mode 100644 index 0000000..689ea76 --- /dev/null +++ b/tests/mock/badge_mock.h @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2023 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. + */ + +#ifndef MOCK_BADGE_MOCK_H_ +#define MOCK_BADGE_MOCK_H_ + +#include +#include +#include +#include +#include + +#include "module_mock.h" + +class BadgeMock : public virtual ModuleMock { + public: + virtual ~BadgeMock() {} + + MOCK_METHOD0(badge_db_init, int (void)); + MOCK_METHOD3(badge_db_is_existing, int (const char*, bool*, uid_t)); + MOCK_METHOD2(badge_db_get_list, int (GList**, uid_t)); + MOCK_METHOD4(badge_db_insert, int (const char*, const char*, const char*, + uid_t)); + MOCK_METHOD4(badge_db_delete, int (const char*, const char*, uid_t, pid_t)); + MOCK_METHOD5(badge_db_set_count, int (const char*, const char*, unsigned int, + uid_t, pid_t)); + MOCK_METHOD3(badge_db_get_count, int (const char*, unsigned int*, uid_t)); + MOCK_METHOD3(badge_db_set_display_option, int (const char*, unsigned int, + uid_t)); + MOCK_METHOD3(badge_db_get_display_option, int (const char*, unsigned int*, + uid_t)); + MOCK_METHOD3(badge_db_get_allow_to_display_by_appid, int (char*, int*, + uid_t)); + MOCK_METHOD4(badge_db_update_setting, int (char*, char*, int, uid_t)); + MOCK_METHOD2(badge_setting_get_appid, int (badge_setting_h, char**)); + MOCK_METHOD3(badge_db_get_setting_by_appid, int (const char*, + badge_setting_h*, uid_t)); + +}; + +#endif // MOCK_BADGE_MOCK_H_ diff --git a/tests/mock/gio_mock.cc b/tests/mock/gio_mock.cc index b0f7aa9..38979e2 100644 --- a/tests/mock/gio_mock.cc +++ b/tests/mock/gio_mock.cc @@ -20,17 +20,8 @@ #include "mock_hook.h" #include "test_fixture.h" -extern "C" GDBusConnection* g_bus_get_sync(GBusType type, - GCancellable* cancellable, GError** error) { - return MOCK_HOOK_P3(GioMock, g_bus_get_sync, type, cancellable, error); -} - -extern "C" GDBusMessage* g_dbus_connection_send_message_with_reply_sync( - GDBusConnection* conn, GDBusMessage* msg, GDBusSendMessageFlags flags, - gint timeout, volatile guint32* out_serial, GCancellable* cancellable, - GError** error) { - return MOCK_HOOK_P7(GioMock, g_dbus_connection_send_message_with_reply_sync, - conn, msg, flags, timeout, out_serial, cancellable, error); +extern "C" void g_dbus_node_info_unref(GDBusNodeInfo* arg0) { + return MOCK_HOOK_P1(GioMock, g_dbus_node_info_unref, arg0); } extern "C" GDBusMessage* g_dbus_message_new_method_call(const gchar* arg0, @@ -39,12 +30,11 @@ extern "C" GDBusMessage* g_dbus_message_new_method_call(const gchar* arg0, arg3); } -extern "C" void g_dbus_message_set_body(GDBusMessage* arg0, GVariant* arg1) { - return MOCK_HOOK_P2(GioMock, g_dbus_message_set_body, arg0, arg1); -} - -extern "C" GVariant* g_dbus_message_get_body(GDBusMessage* arg0) { - return MOCK_HOOK_P1(GioMock, g_dbus_message_get_body, arg0); +extern "C" gboolean g_dbus_connection_emit_signal(GDBusConnection* arg1, + const gchar* arg2, const gchar* arg3, const gchar* arg4, const gchar* arg5, + GVariant* arg6, GError** arg7) { + return MOCK_HOOK_P7(GioMock, g_dbus_connection_emit_signal, arg1, arg2, + arg3, arg4, arg5, arg6, arg7); } extern "C" guint g_dbus_connection_signal_subscribe(GDBusConnection* arg0, @@ -55,36 +45,84 @@ extern "C" guint g_dbus_connection_signal_subscribe(GDBusConnection* arg0, arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9); } -extern "C" void g_dbus_connection_send_message_with_reply(GDBusConnection* arg0, - GDBusMessage* arg1, GDBusSendMessageFlags arg2, gint arg3, - volatile guint32* arg4, GCancellable* arg5, GAsyncReadyCallback arg6, - gpointer arg7) { - return MOCK_HOOK_P8(GioMock, g_dbus_connection_send_message_with_reply, - arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7); +extern "C" void g_dbus_connection_signal_unsubscribe( + GDBusConnection* arg0, guint arg1) { + return MOCK_HOOK_P2(GioMock, g_dbus_connection_signal_unsubscribe, + arg0, arg1); } -extern "C" guint g_bus_watch_name_on_connection( - GDBusConnection* arg0, const gchar* arg1, GBusNameWatcherFlags arg2, - GBusNameAppearedCallback arg3, GBusNameVanishedCallback arg4, - gpointer arg5, GDestroyNotify arg6) { - return MOCK_HOOK_P7(GioMock, g_bus_watch_name_on_connection, +extern "C" GDBusMessage* g_dbus_connection_send_message_with_reply_sync( + GDBusConnection* conn, GDBusMessage* msg, GDBusSendMessageFlags flags, + gint timeout, volatile guint32* out_serial, GCancellable* cancellable, + GError** error) { + return MOCK_HOOK_P7(GioMock, g_dbus_connection_send_message_with_reply_sync, + conn, msg, flags, timeout, out_serial, cancellable, error); +} + +extern "C" GVariant* g_dbus_message_get_body(GDBusMessage* arg0) { + return MOCK_HOOK_P1(GioMock, g_dbus_message_get_body, arg0); +} + +extern "C" void g_dbus_method_invocation_return_value( + GDBusMethodInvocation* arg0, GVariant* arg1) { + MOCK_HOOK_P2(GioMock, g_dbus_method_invocation_return_value, arg0, arg1); +} + +extern "C" guint g_bus_watch_name_on_connection(GDBusConnection* arg0, + const gchar* arg1, GBusNameWatcherFlags arg2, GBusNameAppearedCallback arg3, + GBusNameVanishedCallback arg4, gpointer arg5, GDestroyNotify arg6) { + return MOCK_HOOK_P7(GioMock, g_bus_watch_name_on_connection, arg0, arg1, + arg2, arg3, arg4, arg5, arg6); +} + +extern "C" guint g_bus_own_name_on_connection(GDBusConnection* arg0, + const gchar* arg1, GBusNameOwnerFlags arg2, GBusNameAcquiredCallback arg3, + GBusNameLostCallback arg4, gpointer arg5, GDestroyNotify arg6) { + return MOCK_HOOK_P7(GioMock, g_bus_own_name_on_connection, arg0, arg1, arg2, arg3, arg4, arg5, arg6); } +extern "C" GDBusNodeInfo* g_dbus_node_info_new_for_xml(const gchar* arg0, + GError** arg1) { + return MOCK_HOOK_P2(GioMock, g_dbus_node_info_new_for_xml, arg0, arg1); +} + +extern "C" guint g_bus_own_name(GBusType arg0, const gchar* arg1, + GBusNameOwnerFlags arg2, GBusAcquiredCallback arg3, + GBusNameAcquiredCallback arg4, GBusNameLostCallback arg5, gpointer arg6, + GDestroyNotify arg7) { + return MOCK_HOOK_P8(GioMock, g_bus_own_name, arg0, arg1, arg2, arg3, arg4, + arg5, arg6, arg7); +} + extern "C" void g_bus_unwatch_name(guint arg0) { return MOCK_HOOK_P1(GioMock, g_bus_unwatch_name, arg0); } -extern "C" void g_dbus_connection_signal_unsubscribe( - GDBusConnection* arg0, guint arg1) { - return MOCK_HOOK_P2(GioMock, g_dbus_connection_signal_unsubscribe, - arg0, arg1); +extern "C" void g_dbus_message_set_body(GDBusMessage* arg0, GVariant* arg1) { + return MOCK_HOOK_P2(GioMock, g_dbus_message_set_body, arg0, arg1); } -extern "C" const gchar* g_dbus_connection_get_unique_name(GDBusConnection* connection) { - return MOCK_HOOK_P1(GioMock, g_dbus_connection_get_unique_name, connection); +extern "C" guint g_dbus_connection_register_object(GDBusConnection* arg0, + const gchar* arg1, GDBusInterfaceInfo* arg2, + const GDBusInterfaceVTable* arg3, gpointer arg4, + GDestroyNotify arg5, GError** arg6) { + return MOCK_HOOK_P7(GioMock, g_dbus_connection_register_object, + arg0, arg1, arg2, arg3, arg4, arg5, arg6); } -extern "C" gboolean g_source_remove(guint tag) { - return MOCK_HOOK_P1(GioMock, g_source_remove, tag); +extern "C" guint g_bus_watch_name(GBusType arg0, const gchar* arg1, + GBusNameWatcherFlags arg2, GBusNameAppearedCallback arg3, + GBusNameVanishedCallback arg4, gpointer arg5, GDestroyNotify arg6) { + return MOCK_HOOK_P7(GioMock, g_bus_watch_name, arg0, arg1, arg2, arg3, arg4, + arg5, arg6); +} + +extern "C" void g_object_unref(gpointer arg0) { + return MOCK_HOOK_P1(GioMock, g_object_unref, arg0); +} + +extern "C" GDBusConnection* g_bus_get_sync(GBusType type, + GCancellable* cancellable, GError** error) { + return MOCK_HOOK_P3(GioMock, g_bus_get_sync, type, cancellable, error); } diff --git a/tests/mock/gio_mock.h b/tests/mock/gio_mock.h index 97d29a8..3792e0d 100644 --- a/tests/mock/gio_mock.h +++ b/tests/mock/gio_mock.h @@ -26,31 +26,66 @@ class GioMock : public virtual ModuleMock { public: virtual ~GioMock() {} - MOCK_METHOD3(g_bus_get_sync, - GDBusConnection*(GBusType, GCancellable*, GError**)); + MOCK_METHOD1(g_dbus_node_info_unref, void (GDBusNodeInfo*)); + MOCK_METHOD4(g_dbus_message_new_method_call, GDBusMessage*(const gchar*, const gchar*, const gchar*, const gchar*)); - MOCK_METHOD2(g_dbus_message_set_body, void(GDBusMessage*, GVariant*)); - MOCK_METHOD1(g_dbus_message_get_body, GVariant*(GDBusMessage*)); - MOCK_METHOD7(g_dbus_connection_send_message_with_reply_sync, - GDBusMessage*(GDBusConnection*, GDBusMessage*, GDBusSendMessageFlags, - gint, volatile guint32*, GCancellable*, GError**)); + + MOCK_METHOD7(g_dbus_connection_emit_signal, gboolean (GDBusConnection*, + const gchar*, const gchar*, const gchar*, const gchar*, GVariant*, + GError**)); + MOCK_METHOD10(g_dbus_connection_signal_subscribe, guint(GDBusConnection*, const gchar*, const gchar*, const gchar*, const gchar*, const gchar*, GDBusSignalFlags, GDBusSignalCallback, gpointer, GDestroyNotify)); - MOCK_METHOD8(g_dbus_connection_send_message_with_reply, - void(GDBusConnection*, GDBusMessage*, GDBusSendMessageFlags, gint, - volatile guint32*, GCancellable*, GAsyncReadyCallback, gpointer)); - MOCK_METHOD7(g_bus_watch_name_on_connection, - guint(GDBusConnection*, const gchar*, GBusNameWatcherFlags, - GBusNameAppearedCallback, GBusNameVanishedCallback, gpointer, - GDestroyNotify)); - MOCK_METHOD1(g_bus_unwatch_name, void(guint)); + MOCK_METHOD2(g_dbus_connection_signal_unsubscribe, - void(GDBusConnection*, guint)); - MOCK_METHOD1(g_dbus_connection_get_unique_name, const gchar*(GDBusConnection*)); - MOCK_METHOD1(g_source_remove, gboolean(guint)); + void(GDBusConnection*, guint)); + + MOCK_METHOD7(g_dbus_connection_send_message_with_reply_sync, + GDBusMessage*(GDBusConnection*, GDBusMessage*, GDBusSendMessageFlags, + gint, volatile guint32*, GCancellable*, GError**)); + + MOCK_METHOD1(g_dbus_message_get_body, GVariant*(GDBusMessage*)); + + MOCK_METHOD2(g_dbus_method_invocation_return_value, + void (GDBusMethodInvocation*, GVariant*)); + + MOCK_METHOD7(g_bus_watch_name_on_connection, guint(GDBusConnection*, + const gchar*, GBusNameWatcherFlags, GBusNameAppearedCallback, + GBusNameVanishedCallback, gpointer, GDestroyNotify)); + + MOCK_METHOD7(g_bus_own_name_on_connection, + guint(GDBusConnection*, const gchar*, GBusNameOwnerFlags, + GBusNameAcquiredCallback, GBusNameLostCallback, + gpointer, GDestroyNotify)); + + MOCK_METHOD2(g_dbus_node_info_new_for_xml, GDBusNodeInfo* (const gchar*, + GError**)); + + MOCK_METHOD8(g_bus_own_name, + guint(GBusType, const gchar*, + GBusNameOwnerFlags, GBusAcquiredCallback, GBusNameAcquiredCallback, + GBusNameLostCallback, gpointer, GDestroyNotify)); + + MOCK_METHOD1(g_bus_unwatch_name, void(guint)); + + MOCK_METHOD2(g_dbus_message_set_body, void(GDBusMessage*, GVariant*)); + + MOCK_METHOD7(g_dbus_connection_register_object, guint (GDBusConnection*, + const gchar*, GDBusInterfaceInfo*, const GDBusInterfaceVTable*, gpointer, + GDestroyNotify, GError**)); + + MOCK_METHOD7(g_bus_watch_name, guint(GBusType, const gchar*, + GBusNameWatcherFlags, GBusNameAppearedCallback, + GBusNameVanishedCallback, gpointer, GDestroyNotify)); + + MOCK_METHOD1(g_object_unref, void(gpointer)); + + MOCK_METHOD3(g_bus_get_sync, + GDBusConnection*(GBusType, GCancellable*, GError**)); + }; -#endif // MOCK_GIO_MOCK_H_ +#endif // MOCK_GIO_MOCK_H_ \ No newline at end of file diff --git a/tests/mock/notification_mock.cc b/tests/mock/notification_mock.cc new file mode 100644 index 0000000..10a28e7 --- /dev/null +++ b/tests/mock/notification_mock.cc @@ -0,0 +1,245 @@ +/* + * Copyright (c) 2023 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 "notification_mock.h" +#include "mock_hook.h" +#include "test_fixture.h" + +extern "C" notification_h notification_create(notification_type_e arg0) { + return MOCK_HOOK_P1(NotificationMock, notification_create, arg0); +} + +extern "C" int notification_free(notification_h arg0) { + return MOCK_HOOK_P1(NotificationMock, notification_free, arg0); +} + +extern "C" int notification_get_id(notification_h arg0, int* arg1, int* arg2) { + return MOCK_HOOK_P3(NotificationMock, notification_get_id, arg0, arg1, arg2); +} + +extern "C" int notification_noti_check_tag(notification_h arg0) { + return MOCK_HOOK_P1(NotificationMock, notification_noti_check_tag, arg0); +} + +extern "C" int notification_noti_update(notification_h arg0) { + return MOCK_HOOK_P1(NotificationMock, notification_noti_update, arg0); +} + +extern "C" int notification_noti_delete_by_priv_id_get_changes( + const char* arg0, int arg1, int* arg2, uid_t arg3) { + return MOCK_HOOK_P4(NotificationMock, + notification_noti_delete_by_priv_id_get_changes, arg0, arg1, arg2, arg3); +} + +extern "C" int notification_noti_delete_by_display_applist( + int arg0, int* arg1, notification_deleted_list_info_s** arg2, uid_t arg3) { + return MOCK_HOOK_P4(NotificationMock, + notification_noti_delete_by_display_applist, arg0, arg1, arg2, arg3); +} + +extern "C" int notification_noti_delete_all(notification_type_e arg0, + const char* arg1, int* arg2, int** arg3, uid_t arg4) { + return MOCK_HOOK_P5(NotificationMock, + notification_noti_delete_all, arg0, arg1, arg2, arg3, arg4); +} + +extern "C" int notification_noti_get_count( + notification_type_e arg0, const char* arg1, int arg2, int arg3, int* arg4, + uid_t arg5) { + return MOCK_HOOK_P6(NotificationMock, + notification_noti_get_count, arg0, arg1, arg2, arg3, arg4, arg5); +} + +extern "C" int notification_noti_get_all_count( + notification_type_e arg0, int* arg1, uid_t arg2) { + return MOCK_HOOK_P3(NotificationMock, notification_noti_get_all_count, arg0, + arg1, arg2); +} + +extern "C" int notification_noti_get_by_tag( + notification_h arg0, char* arg1, char* arg2, uid_t arg3) { + return MOCK_HOOK_P4(NotificationMock, notification_noti_get_by_tag, arg0, + arg1, arg2, arg3); +} + +extern "C" int notification_noti_get_by_priv_id( + notification_h arg0, int arg1) { + return MOCK_HOOK_P2(NotificationMock, notification_noti_get_by_priv_id, arg0, + arg1); +} + +extern "C" int notification_get_event_flag(notification_h arg0, + bool* arg1) { + return MOCK_HOOK_P2(NotificationMock, notification_get_event_flag, + arg0, arg1); +} + +extern "C" int notification_noti_get_detail_list(const char* arg0, int arg1, + int arg2, int arg3, notification_list_h* arg4, uid_t arg5) { + return MOCK_HOOK_P6(NotificationMock, notification_noti_get_detail_list, + arg0, arg1, arg2, arg3, arg4, arg5); +} + +extern "C" int notification_noti_add_template(notification_h arg0, + char* arg1) { + return MOCK_HOOK_P2(NotificationMock, notification_noti_add_template, + arg0, arg1); +} + +extern "C" int notification_noti_get_package_template(notification_h arg0, + char* arg1, char* arg2) { + return MOCK_HOOK_P3(NotificationMock, notification_noti_get_package_template, + arg0, arg1, arg2); +} + +extern "C" int notification_get_uid(notification_h arg0, uid_t* arg1) { + return MOCK_HOOK_P2(NotificationMock, notification_get_uid, arg0, arg1); +} + +extern "C" int notification_set_uid(notification_h arg0, uid_t arg1) { + return MOCK_HOOK_P2(NotificationMock, notification_set_uid, arg0, arg1); +} + +extern "C" int notification_ipc_make_noti_from_gvariant(notification_h arg0, + GVariant* arg1) { + return MOCK_HOOK_P2(NotificationMock, notification_ipc_make_noti_from_gvariant, + arg0, arg1); +} + +extern "C" GVariant* notification_ipc_make_gvariant_from_noti(notification_h arg0, + bool arg1) { + return MOCK_HOOK_P2(NotificationMock, notification_ipc_make_gvariant_from_noti, + arg0, arg1); +} + +extern "C" int notification_db_init(void) { + return MOCK_HOOK_P0(NotificationMock, notification_db_init); +} + +extern "C" int notification_init_default_viewer(void) { + return MOCK_HOOK_P0(NotificationMock, notification_init_default_viewer); +} + +extern "C" int notification_system_setting_init_system_setting_table( + uid_t arg0) { + return MOCK_HOOK_P1(NotificationMock, + notification_system_setting_init_system_setting_table, arg0); +} + +extern "C" int notification_system_setting_get_dnd_schedule_enabled_uid( + uid_t** arg0, int* arg1) { + return MOCK_HOOK_P2(NotificationMock, + notification_system_setting_get_dnd_schedule_enabled_uid, arg0, arg1); +} + +extern "C" int notification_setting_db_update(const char* arg0, const char* arg1, + int arg2, int arg3, int arg4, int arg5, int arg6, uid_t arg7) { + return MOCK_HOOK_P8(NotificationMock, + notification_setting_db_update, arg0, arg1, arg2, arg3, arg4, arg5, arg6, + arg7); +} + +extern "C" int noti_system_setting_get_do_not_disturb(int* arg0, uid_t arg1) { + return MOCK_HOOK_P2(NotificationMock, + noti_system_setting_get_do_not_disturb, arg0, arg1); +} + +extern "C" int notification_setting_db_update_system_setting(int arg0, int arg1, + int arg2, int arg3, int arg4, int arg5, int arg6, int arg7, int arg8, + uid_t arg9) { + return MOCK_HOOK_P10(NotificationMock, + notification_setting_db_update_system_setting, arg0, arg1, arg2, arg3, + arg4, arg5, arg6, arg7, arg8, arg9); +} + +extern "C" int noti_setting_get_setting_array(notification_setting_h* arg0, int* arg1, + uid_t arg2) { + return MOCK_HOOK_P3(NotificationMock, + noti_setting_get_setting_array, arg0, arg1, arg2); +} + +extern "C" int noti_setting_service_get_setting_by_app_id(const char* arg0, + notification_setting_h* arg1, uid_t arg2) { + return MOCK_HOOK_P3(NotificationMock, + noti_setting_service_get_setting_by_app_id, arg0, arg1, arg2); +} + +extern "C" int noti_system_setting_load_system_setting( + notification_system_setting_h* arg0, uid_t arg1) { + return MOCK_HOOK_P2(NotificationMock, + noti_system_setting_load_system_setting, arg0, arg1); +} + +extern "C" bool notification_validate_private_sharing(notification_h arg0) { + return MOCK_HOOK_P1(NotificationMock, + notification_validate_private_sharing, arg0); +} + +extern "C" int notification_set_private_sharing(notification_h arg0, uid_t arg1) { + return MOCK_HOOK_P2(NotificationMock, notification_set_private_sharing, + arg0, arg1); +} + +extern "C" int notification_launch_default_viewer(int arg0, + notification_op_type_e arg1, uid_t arg2) { + return MOCK_HOOK_P3(NotificationMock, notification_launch_default_viewer, + arg0, arg1, arg2); +} + +extern "C" int notification_get_pkgname(notification_h arg0, char** arg1) { + return MOCK_HOOK_P2(NotificationMock, notification_get_pkgname, arg0, arg1); +} + +extern "C" int notification_get_text(notification_h arg0, + notification_text_type_e arg1, char** arg2) { + return MOCK_HOOK_P3(NotificationMock, notification_get_text, arg0, arg1, arg2); +} + +extern "C" int notification_get_tag(notification_h arg0, const char** arg1) { + return MOCK_HOOK_P2(NotificationMock, notification_get_tag, arg0, arg1); +} + +extern "C" int notification_get_dnd_and_allow_to_notify(const char* arg0, + int* arg1, int* arg2, int* arg3, uid_t arg4) { + return MOCK_HOOK_P5(NotificationMock, notification_get_dnd_and_allow_to_notify, + arg0, arg1, arg2, arg3, arg4); +} + +extern "C" GVariant* notification_ipc_make_gvariant_from_system_setting( + struct notification_system_setting* arg0) { + return MOCK_HOOK_P1(NotificationMock, notification_ipc_make_gvariant_from_system_setting, + arg0); +} + +extern "C" int notification_system_setting_load_dnd_allow_exception( + dnd_allow_exception_h* arg0, int* arg1, uid_t arg2) { + return MOCK_HOOK_P3(NotificationMock, + notification_system_setting_load_dnd_allow_exception, arg0, arg1, arg2); +} + +extern "C" int notification_system_setting_update_dnd_allow_exception( + int arg0, int arg1, uid_t arg2) { + return MOCK_HOOK_P3(NotificationMock, + notification_system_setting_update_dnd_allow_exception, arg0, arg1, arg2); +} + +extern "C" int notification_set_indirect_request( + notification_h arg0, pid_t arg1, uid_t arg2) { + return MOCK_HOOK_P3(NotificationMock, + notification_set_indirect_request, arg0, arg1, arg2); +} \ No newline at end of file diff --git a/tests/mock/notification_mock.h b/tests/mock/notification_mock.h new file mode 100644 index 0000000..b84ee7c --- /dev/null +++ b/tests/mock/notification_mock.h @@ -0,0 +1,105 @@ +/* + * Copyright (c) 2023 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. + */ + +#ifndef MOCK_NOTIFICATION_MOCK_H_ +#define MOCK_NOTIFICATION_MOCK_H_ + +#include + +#include "module_mock.h" + +#include + +#include +#include +#include +#include +#include +#include + +class NotificationMock : public virtual ModuleMock { + public: + virtual ~NotificationMock() {} + + MOCK_METHOD1(notification_create, notification_h (notification_type_e)); + MOCK_METHOD1(notification_free, int (notification_h)); + MOCK_METHOD3(notification_get_id, int (notification_h, int*, int*)); + MOCK_METHOD1(notification_noti_check_tag, int (notification_h)); + MOCK_METHOD1(notification_noti_update, int (notification_h)); + MOCK_METHOD4(notification_noti_delete_by_priv_id_get_changes, + int (const char*, int, int*, uid_t)); + MOCK_METHOD4(notification_noti_delete_by_display_applist, + int (int, int*, notification_deleted_list_info_s**, uid_t)); + MOCK_METHOD5(notification_noti_delete_all, + int (notification_type_e, const char*, int*, int**, uid_t)); + MOCK_METHOD6(notification_noti_get_count, + int (notification_type_e, const char*, int, int, int*, uid_t)); + MOCK_METHOD3(notification_noti_get_all_count, + int (notification_type_e, int*, uid_t)); + MOCK_METHOD4(notification_noti_get_by_tag, + int (notification_h, char*, char*, uid_t)); + MOCK_METHOD2(notification_noti_get_by_priv_id, + int (notification_h, int)); + MOCK_METHOD6(notification_noti_get_detail_list, + int (const char*, int, int, int, notification_list_h*, uid_t)); + MOCK_METHOD2(notification_noti_add_template, + int (notification_h, char*)); + MOCK_METHOD3(notification_noti_get_package_template, + int (notification_h, char*, char*)); + MOCK_METHOD2(notification_get_event_flag, int (notification_h, bool*)); + MOCK_METHOD2(notification_get_uid, int (notification_h, uid_t*)); + MOCK_METHOD2(notification_set_uid, int (notification_h, uid_t)); + MOCK_METHOD2(notification_ipc_make_noti_from_gvariant, + int (notification_h, GVariant*)); + MOCK_METHOD2(notification_ipc_make_gvariant_from_noti, + GVariant* (notification_h, bool)); + MOCK_METHOD0(notification_db_init, int (void)); + MOCK_METHOD0(notification_init_default_viewer, int (void)); + MOCK_METHOD1(notification_system_setting_init_system_setting_table, int (uid_t)); + MOCK_METHOD2(notification_system_setting_get_dnd_schedule_enabled_uid, + int (uid_t**, int*)); + MOCK_METHOD8(notification_setting_db_update, + int (const char*, const char*, int, int, int, int, int, uid_t)); + MOCK_METHOD2(noti_system_setting_get_do_not_disturb, int (int*, uid_t)); + MOCK_METHOD10(notification_setting_db_update_system_setting, int (int, int, int, + int, int, int, int, int, int, uid_t)); + MOCK_METHOD3(noti_setting_get_setting_array, int (notification_setting_h*, + int*, uid_t)); + MOCK_METHOD3(noti_setting_service_get_setting_by_app_id, int (const char*, + notification_setting_h*, uid_t)); + MOCK_METHOD2(noti_system_setting_load_system_setting, int ( + notification_system_setting_h*, uid_t)); + MOCK_METHOD1(notification_validate_private_sharing, bool (notification_h)); + MOCK_METHOD2(notification_set_private_sharing, int (notification_h, uid_t)); + MOCK_METHOD3(notification_launch_default_viewer, int (int, + notification_op_type_e, uid_t)); + MOCK_METHOD2(notification_get_pkgname, int (notification_h, char**)); + MOCK_METHOD3(notification_get_text, int (notification_h, + notification_text_type_e, char**)); + MOCK_METHOD2(notification_get_tag, int (notification_h, const char**)); + MOCK_METHOD5(notification_get_dnd_and_allow_to_notify, int (const char*, int*, + int*, int*, uid_t)); + MOCK_METHOD1(notification_ipc_make_gvariant_from_system_setting, GVariant* + (struct notification_system_setting*)); + MOCK_METHOD3(notification_system_setting_load_dnd_allow_exception, int + (dnd_allow_exception_h*, int*, uid_t)); + MOCK_METHOD3(notification_system_setting_update_dnd_allow_exception, int + (int, int, uid_t)); + MOCK_METHOD3(notification_set_indirect_request, int (notification_h, pid_t, + uid_t)); +}; + +#endif // MOCK_NOTIFICATION_MOCK_H_ diff --git a/tests/mock/pkgmgr_info_mock.cc b/tests/mock/pkgmgr_info_mock.cc new file mode 100644 index 0000000..8739d8c --- /dev/null +++ b/tests/mock/pkgmgr_info_mock.cc @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2023 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 "pkgmgr_info_mock.h" +#include "mock_hook.h" +#include "test_fixture.h" + +extern "C" int pkgmgrinfo_appinfo_destroy_appinfo( + pkgmgrinfo_appinfo_h arg0) { + return MOCK_HOOK_P1(PkgmgrInfoMock, pkgmgrinfo_appinfo_destroy_appinfo, + arg0); +} + +extern "C" int pkgmgr_client_free(pkgmgr_client* arg0) { + return MOCK_HOOK_P1(PkgmgrInfoMock, pkgmgr_client_free, arg0); +} + +extern "C" int pkgmgrinfo_appinfo_get_usr_appinfo(const char* arg0, uid_t arg1, + pkgmgrinfo_appinfo_h* arg2) { + return MOCK_HOOK_P3(PkgmgrInfoMock, pkgmgrinfo_appinfo_get_usr_appinfo, arg0, + arg1, arg2); +} + +extern "C" int pkgmgrinfo_appinfo_get_pkgid(pkgmgrinfo_pkginfo_h arg0, + char** arg1) { + return MOCK_HOOK_P2(PkgmgrInfoMock, pkgmgrinfo_appinfo_get_pkgid, arg0, + arg1); +} + +extern "C" int pkgmgrinfo_pkginfo_get_usr_pkginfo(const char* arg0, uid_t arg1, + pkgmgrinfo_pkginfo_h* arg2) { + return MOCK_HOOK_P3(PkgmgrInfoMock, pkgmgrinfo_pkginfo_get_usr_pkginfo, arg0, + arg1, arg2); +} + +extern "C" int pkgmgrinfo_pkginfo_destroy_pkginfo(pkgmgrinfo_pkginfo_h arg0) { + return MOCK_HOOK_P1(PkgmgrInfoMock, pkgmgrinfo_pkginfo_destroy_pkginfo, + arg0); +} + +extern "C" pkgmgr_client* pkgmgr_client_new(pkgmgr_client_type arg0) { + return MOCK_HOOK_P1(PkgmgrInfoMock, pkgmgr_client_new, arg0); +} + +extern "C" int pkgmgr_client_listen_status(pkgmgr_client* arg0, + pkgmgr_handler arg1, void* arg2) { + return MOCK_HOOK_P3(PkgmgrInfoMock, pkgmgr_client_listen_status, arg0, arg1, + arg2); +} + diff --git a/tests/mock/pkgmgr_info_mock.h b/tests/mock/pkgmgr_info_mock.h new file mode 100644 index 0000000..4fdf9cd --- /dev/null +++ b/tests/mock/pkgmgr_info_mock.h @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2023 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. + */ + +#ifndef MOCK_PKGMGR_INFO_MOCK_H_ +#define MOCK_PKGMGR_INFO_MOCK_H_ + +#include +#include +#include + +#include "module_mock.h" + +class PkgmgrInfoMock : public virtual ModuleMock { + public: + virtual ~PkgmgrInfoMock() {} + + MOCK_METHOD1(pkgmgrinfo_appinfo_destroy_appinfo, + int (pkgmgrinfo_appinfo_h)); + MOCK_METHOD1(pkgmgr_client_free, int (pkgmgr_client*)); + MOCK_METHOD3(pkgmgrinfo_appinfo_get_usr_appinfo, + int (const char *, uid_t, pkgmgrinfo_appinfo_h*)); + MOCK_METHOD2(pkgmgrinfo_appinfo_get_pkgid, + int (pkgmgrinfo_pkginfo_h, char **)); + MOCK_METHOD3(pkgmgrinfo_pkginfo_get_usr_pkginfo, + int (const char *, uid_t, pkgmgrinfo_pkginfo_h*)); + MOCK_METHOD1(pkgmgrinfo_pkginfo_destroy_pkginfo, + int (pkgmgrinfo_pkginfo_h)); + MOCK_METHOD1(pkgmgr_client_new, pkgmgr_client* (pkgmgr_client_type)); + MOCK_METHOD3(pkgmgr_client_listen_status, int (pkgmgr_client*, + pkgmgr_handler, void*)); +}; + +#endif // MOCK_PKMGR_INFO_MOCK_H_ \ No newline at end of file diff --git a/tests/mock/shortcut_mock.cc b/tests/mock/shortcut_mock.cc new file mode 100644 index 0000000..0f39498 --- /dev/null +++ b/tests/mock/shortcut_mock.cc @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2023 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 "shortcut_mock.h" +#include "mock_hook.h" +#include "test_fixture.h" + +extern "C" int shortcut_db_init(void) { + return MOCK_HOOK_P0(ShortcutMock, shortcut_db_init); +} + +extern "C" int shortcut_db_get_list(const char* arg0, GList** arg1) { + return MOCK_HOOK_P2(ShortcutMock, shortcut_db_get_list, arg0, arg1); +} diff --git a/tests/mock/shortcut_mock.h b/tests/mock/shortcut_mock.h new file mode 100644 index 0000000..4b8420b --- /dev/null +++ b/tests/mock/shortcut_mock.h @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2023 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. + */ + +#ifndef MOCK_SHORTCUT_MOCK_H_ +#define MOCK_SHORTCUT_MOCK_H_ + +#include +#include +#include +#include +#include + +#include "module_mock.h" + +class ShortcutMock : public virtual ModuleMock { + public: + virtual ~ShortcutMock() {} + + MOCK_METHOD0(shortcut_db_init, int (void)); + MOCK_METHOD2(shortcut_db_get_list, int (const char*, GList**)); +}; + +#endif // MOCK_SHORTCUT_MOCK_H_ diff --git a/tests/mock/tzplatform_config_mock.cc b/tests/mock/tzplatform_config_mock.cc new file mode 100644 index 0000000..7c7369f --- /dev/null +++ b/tests/mock/tzplatform_config_mock.cc @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2023 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 "tzplatform_config_mock.h" + +#include "mock_hook.h" +#include "test_fixture.h" + +extern "C" uid_t tzplatform_getuid(enum tzplatform_variable arg0) { + return MOCK_HOOK_P1(TzplatformConfigMock, tzplatform_getuid, arg0); +} \ No newline at end of file diff --git a/tests/mock/tzplatform_config_mock.h b/tests/mock/tzplatform_config_mock.h new file mode 100644 index 0000000..83ab94f --- /dev/null +++ b/tests/mock/tzplatform_config_mock.h @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2023 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. + */ + +#ifndef MOCK_TZPLATFORM_CONFIG_MOCK_H_ +#define MOCK_TZPLATFORM_CONFIG_MOCK_H_ + +#include +#include + +#include "module_mock.h" + +class TzplatformConfigMock : public virtual ModuleMock { + public: + virtual ~TzplatformConfigMock() {} + + MOCK_METHOD1(tzplatform_getuid, uid_t (enum tzplatform_variable)); +}; + +#endif // MOCK_TZPLATFORM_CONFIG_MOCK_H_ \ No newline at end of file diff --git a/tests/unit_tests/CMakeLists.txt b/tests/unit_tests/CMakeLists.txt index 70a35d6..22543ac 100644 --- a/tests/unit_tests/CMakeLists.txt +++ b/tests/unit_tests/CMakeLists.txt @@ -5,33 +5,50 @@ INCLUDE(FindPkgConfig) PKG_CHECK_MODULES(data-provider-master-unittests REQUIRED gmock dlog - bundle + aul + vconf + sqlite3 glib-2.0 gio-2.0 - sqlite3 - aul + gio-unix-2.0 + bundle + pkgmgr + notification + notification-ex + badge + libsmack + shortcut + pkgmgr-info + libsystemd + capi-appfw-app-manager + alarm-service + libtzplatform-config + security-manager ) FOREACH(flag ${data-provider-master-unittests_CFLAGS}) SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}") ENDFOREACH(flag) -SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -fvisibility=hidden -Wall -Werror") -SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${EXTRA_CFLAGS} -std=c++14") +SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${EXTRA_CFLAGS} -std=c++17") SET(CMAKE_CXX_FLAGS_DEBUG "-O0 -g") SET(CMAKE_CXX_FLAGS_RELEASE "-O2") +INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/../..) INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/../../include) -INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/../../src) -INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/../mock) AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR}/src TEST_SOURCES) AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR}/../mock MOCK_SOURCES) -# AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR}/../../src LIB_SOURCES) +SET(DPM_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/../../src/badge_service.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../src/notification_ex_service.cc + ${CMAKE_CURRENT_SOURCE_DIR}/../../src/notification_service.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../src/pkgmgr.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../src/shortcut_service.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../src/service_common.c) ADD_EXECUTABLE(${PROJECT_NAME} ${TEST_SOURCES} - ${LIB_SOURCES} + ${DPM_SOURCES} ${MOCK_SOURCES} ) diff --git a/tests/unit_tests/src/test_badge_service.cc b/tests/unit_tests/src/test_badge_service.cc index 822d969..c788f74 100644 --- a/tests/unit_tests/src/test_badge_service.cc +++ b/tests/unit_tests/src/test_badge_service.cc @@ -14,25 +14,27 @@ * limitations under the License. */ -#include #include -#include -#include -#include +#include "include/service_common.h" +#include "include/badge_service.h" +#include "tests/mock/badge_mock.h" +#include "tests/mock/gio_mock.h" +#include "tests/mock/tzplatform_config_mock.h" +#include "tests/mock/test_fixture.h" -#include "service_common.h" -#include "badge_service.h" -#include "gio_mock.h" -#include "test_fixture.h" - -// using namespace tizen_base; using ::testing::_; using ::testing::DoAll; using ::testing::Return; using ::testing::SetArgPointee; -class Mocks : public ::testing::NiceMock {}; +namespace { + +class Mocks : public ::testing::NiceMock, + public ::testing::NiceMock, + public ::testing::NiceMock {}; + +} //namespace class BadgeServiceTest : public TestFixture { public: @@ -40,6 +42,8 @@ class BadgeServiceTest : public TestFixture { virtual ~BadgeServiceTest() {} virtual void SetUp() { + EXPECT_CALL(GetMock(), tzplatform_getuid(_)) + .WillRepeatedly(Return(6001)); } virtual void TearDown() { @@ -47,8 +51,179 @@ class BadgeServiceTest : public TestFixture { }; TEST_F(BadgeServiceTest, badge_service_init) { - int ret; - ret = BADGE_ERROR_NONE; + GDBusNodeInfo* info = reinterpret_cast( + malloc(sizeof(GDBusNodeInfo))); + info->interfaces = reinterpret_cast( + malloc(sizeof(GDBusInterfaceInfo*))); + GDBusConnection* conn = reinterpret_cast(calloc(1, 4)); + EXPECT_CALL(GetMock(), badge_db_init()) + .WillOnce(Return(0)); + EXPECT_CALL(GetMock(), g_bus_own_name(_, _, _, _, _, _, _, _)) + .WillOnce(Return(1)); + EXPECT_CALL(GetMock(), g_bus_get_sync(_, _, _)) + .WillOnce(Return(conn)); + EXPECT_CALL(GetMock(), g_dbus_node_info_new_for_xml(_, _)) + .WillOnce(Return(info)); + EXPECT_CALL(GetMock(), g_dbus_node_info_unref(_)) + .WillOnce(Return()); + EXPECT_CALL(GetMock(), g_dbus_connection_register_object(_, _, _, _, _, _, _)) + .WillOnce(Return(1)); + + int ret = badge_service_init(); + EXPECT_EQ(ret, BADGE_ERROR_NONE); + + free(info->interfaces); + free(info); + free(conn); +} + +TEST_F(BadgeServiceTest, badge_service_fini) { + int ret = badge_service_fini(); + EXPECT_EQ(ret, BADGE_ERROR_NONE); +} + +TEST_F(BadgeServiceTest, badge_get_badge_existing) { + EXPECT_CALL(GetMock(), badge_db_is_existing(_, _, _)) + .WillOnce(Return(0)); + GVariant* param = g_variant_new("(si)", "pkgname", 5001); + GVariant* reply; + int ret = badge_get_badge_existing(param, &reply, 5001); + EXPECT_EQ(ret, BADGE_ERROR_NONE); + + g_object_unref(param); + g_object_unref(reply); +} + +TEST_F(BadgeServiceTest, badge_get_badge_list) { + EXPECT_CALL(GetMock(), badge_db_get_list(_, _)) + .WillOnce(Return(0)); + GVariant* param = g_variant_new("(i)", 5001); + GVariant* reply; + int ret = badge_get_badge_list(param, &reply, 5001); + EXPECT_EQ(ret, BADGE_ERROR_NONE); + + g_object_unref(param); + g_object_unref(reply); + +} + +TEST_F(BadgeServiceTest, badge_insert) { + EXPECT_CALL(GetMock(), badge_db_insert(_, _, _, _)) + .WillOnce(Return(0)); + EXPECT_CALL(GetMock(), badge_db_get_allow_to_display_by_appid(_, _, + _)).WillOnce(Return(0)); + GVariant* param = g_variant_new("(sssi)", "pkgname", "writable", "call", 5001); + GVariant* reply; + int ret = badge_insert(param, &reply, 5001); EXPECT_EQ(ret, BADGE_ERROR_NONE); + g_object_unref(param); + g_object_unref(reply); } + +TEST_F(BadgeServiceTest, badge_delete) { + EXPECT_CALL(GetMock(), badge_db_delete(_, _, _, _)) + .WillOnce(Return(0)); + GVariant* param = g_variant_new("(ssi)", "pkgname", "caller", 5001); + GVariant* reply; + int ret = badge_delete(param, &reply, 5001, 10); + EXPECT_EQ(ret, BADGE_ERROR_NONE); + + g_object_unref(param); + g_object_unref(reply); +} + +TEST_F(BadgeServiceTest, badge_set_badge_count) { + EXPECT_CALL(GetMock(), badge_db_set_count(_, _, _, _, _)) + .WillOnce(Return(0)); + EXPECT_CALL(GetMock(), badge_db_get_allow_to_display_by_appid(_, _, + _)).WillOnce(Return(0)); + GVariant* param = g_variant_new("(ssii)", "pkgname", "caller", 10, 5001); + GVariant* reply; + int ret = badge_set_badge_count(param, &reply, 5001, 10); + EXPECT_EQ(ret, BADGE_ERROR_NONE); + + g_object_unref(param); + g_object_unref(reply); +} + +TEST_F(BadgeServiceTest, badge_get_badge_count) { + EXPECT_CALL(GetMock(), badge_db_get_count(_, _, _)) + .WillOnce(Return(0)); + GVariant* param = g_variant_new("(si)", "pkgname", 5001); + GVariant* reply; + int ret = badge_get_badge_count(param, &reply, 5001); + EXPECT_EQ(ret, BADGE_ERROR_NONE); + + g_object_unref(param); + g_object_unref(reply); +} + +TEST_F(BadgeServiceTest, badge_set_display_option) { + EXPECT_CALL(GetMock(), badge_db_set_display_option(_, _, _)) + .WillOnce(Return(0)); + EXPECT_CALL(GetMock(), badge_db_get_allow_to_display_by_appid(_, _, + _)).WillOnce(Return(0)); + GVariant* param = g_variant_new("(ssii)", "pkgname", "caller", 1, 5001); + GVariant* reply; + int ret = badge_set_display_option(param, &reply, 5001); + EXPECT_EQ(ret, BADGE_ERROR_NONE); + + g_object_unref(param); + g_object_unref(reply); +} + +TEST_F(BadgeServiceTest, badge_get_display_option) { + EXPECT_CALL(GetMock(), badge_db_get_display_option(_, _, _)) + .WillOnce(Return(0)); + EXPECT_CALL(GetMock(), badge_db_get_allow_to_display_by_appid(_, _, + _)).WillOnce(Return(0)); + GVariant* param = g_variant_new("(si)", "pkgname", 5001); + GVariant* reply; + int ret = badge_get_display_option(param, &reply, "sender", 5001); + EXPECT_EQ(ret, BADGE_ERROR_NONE); + + g_object_unref(param); + g_object_unref(reply); +} + +TEST_F(BadgeServiceTest, badge_update_badge_setting) { + EXPECT_CALL(GetMock(), badge_db_update_setting(_, _, _, _)) + .WillOnce(Return(0)); + GVariant* param = g_variant_new("(ssii)", "pkgname", "caller", 1, 5001); + GVariant* reply; + int ret = badge_update_badge_setting(param, &reply, 5001); + EXPECT_EQ(ret, BADGE_ERROR_NONE); + + g_object_unref(param); + g_object_unref(reply); +} + +TEST_F(BadgeServiceTest, badge_get_setting_by_appid) { + struct badge_setting* setting = + reinterpret_cast(malloc(sizeof(struct badge_setting))); + setting->pkgname = strdup("pkgname"); + setting->appid = strdup("appid"); + setting->allow_to_display = 1; + EXPECT_CALL(GetMock(), badge_db_get_setting_by_appid(_, _, _)) + .WillOnce(DoAll(SetArgPointee<1>(setting), Return(0))); + GVariant* param = g_variant_new("(si)", "appid", 5001); + GVariant* reply; + int ret = badge_get_setting_by_appid(param, &reply, 5001); + EXPECT_EQ(ret, BADGE_ERROR_NONE); + + g_object_unref(param); + g_object_unref(reply); +} + +TEST_F(BadgeServiceTest, badge_init_display) { + EXPECT_CALL(GetMock(), badge_db_get_list(_, _)) + .WillOnce(Return(0)); + GVariant* param = g_variant_new("(i)", 5001); + GVariant* reply; + int ret = badge_init_display(param, &reply, 5001); + EXPECT_EQ(ret, BADGE_ERROR_NONE); + + g_object_unref(param); + g_object_unref(reply); +} \ No newline at end of file diff --git a/tests/unit_tests/src/test_notification_ex_service.cc b/tests/unit_tests/src/test_notification_ex_service.cc new file mode 100644 index 0000000..5dec0a6 --- /dev/null +++ b/tests/unit_tests/src/test_notification_ex_service.cc @@ -0,0 +1,83 @@ +/* + * Copyright (c) 2023 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 "tests/mock/aul_mock.h" +#include "tests/mock/gio_mock.h" +#include "tests/mock/notification_mock.h" +#include "tests/mock/test_fixture.h" + +#include "include/service_common.h" +#include "include/notification_ex_service.h" + +using ::testing::_; +using ::testing::Invoke; +using ::testing::Return; + +namespace { + +class Mocks : public ::testing::NiceMock, + public ::testing::NiceMock, + public ::testing::NiceMock {}; + +} //namespace + +class NotificationEXServiceTest : public TestFixture { + public: + NotificationEXServiceTest() : TestFixture(std::make_unique()) {} + virtual ~NotificationEXServiceTest() {} + + virtual void SetUp() { + } + + virtual void TearDown() { + } +}; + +TEST_F(NotificationEXServiceTest, notification_ex_service) { + GDBusConnection* conn = reinterpret_cast(calloc(1, 4)); + GDBusMessage* msg = static_cast(g_object_new(G_TYPE_OBJECT, NULL)); + GDBusMessage* msg2 = static_cast(g_object_new(G_TYPE_OBJECT, NULL)); + g_dbus_message_set_body(msg2, g_variant_new("(i)", 1)); + EXPECT_CALL(GetMock(), g_bus_own_name_on_connection(_, _, _, _, _, + _, _)).WillOnce(Return(1)); + EXPECT_CALL(GetMock(), g_bus_get_sync(_, _, _)) + .WillOnce(Return(conn)); + EXPECT_CALL(GetMock(), aul_app_get_appid_bypid(_, _, _)) + .WillRepeatedly(Invoke([](int pid, char* appid, int size) { + snprintf(appid, size, "%s", "org.tizen.helloworld"); + return AUL_R_OK; + })); + EXPECT_CALL(GetMock(), + g_dbus_message_new_method_call(_, _, _, _)) + .WillRepeatedly(Return(msg)); + EXPECT_CALL(GetMock(), + g_dbus_connection_send_message_with_reply_sync( + _, _, _, _, _, _, _)) + .WillRepeatedly(Return(msg2)); + EXPECT_CALL(GetMock(), g_dbus_message_get_body(_)) + .WillRepeatedly(Return(g_variant_new("(i)", 1))); + + int ret = notification_ex_service_init(0); + EXPECT_EQ(ret, 0); + GDBusConnection* inst_conn = notification_ex_service_get_gdbus_connection(); + EXPECT_EQ(conn, inst_conn); + ret = notification_ex_service_fini(); + EXPECT_EQ(ret, 0); + + free(conn); +} diff --git a/tests/unit_tests/src/test_notification_service.cc b/tests/unit_tests/src/test_notification_service.cc new file mode 100644 index 0000000..17c3d00 --- /dev/null +++ b/tests/unit_tests/src/test_notification_service.cc @@ -0,0 +1,515 @@ +/* + * Copyright (c) 2023 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 "tests/mock/aul_mock.h" +#include "tests/mock/notification_mock.h" +#include "tests/mock/gio_mock.h" +#include "tests/mock/tzplatform_config_mock.h" +#include "tests/mock/test_fixture.h" + +#include "include/service_common.h" +#include "include/notification_service.h" + +using ::testing::_; +using ::testing::DoAll; +using ::testing::Return; +using ::testing::SetArgPointee; + +namespace { + +class Mocks : public ::testing::NiceMock, + public ::testing::NiceMock, + public ::testing::NiceMock, + public ::testing::NiceMock {}; + +} //namespace + +class NotificationServiceTest : public TestFixture { + public: + NotificationServiceTest() : TestFixture(std::make_unique()) {} + virtual ~NotificationServiceTest() {} + + virtual void SetUp() { + EXPECT_CALL(GetMock(), tzplatform_getuid(_)) + .WillRepeatedly(Return(6001)); + } + + virtual void TearDown() { + } +}; + +TEST_F(NotificationServiceTest, notification_service_init) { + GDBusNodeInfo* info = reinterpret_cast( + malloc(sizeof(GDBusNodeInfo))); + info->interfaces = reinterpret_cast( + malloc(sizeof(GDBusInterfaceInfo*))); + EXPECT_CALL(GetMock(), notification_db_init()) + .WillOnce(Return(NOTIFICATION_ERROR_NONE)); + EXPECT_CALL(GetMock(), notification_init_default_viewer()) + .WillOnce(Return(NOTIFICATION_ERROR_NONE)); + EXPECT_CALL(GetMock(), + notification_system_setting_init_system_setting_table(_)) + .WillOnce(Return(NOTIFICATION_ERROR_NONE)); + EXPECT_CALL(GetMock(), + notification_system_setting_get_dnd_schedule_enabled_uid(_, _)) + .WillOnce(Return(NOTIFICATION_ERROR_NONE)); + EXPECT_CALL(GetMock(), g_bus_own_name(_, _, _, _, _, _, _, _)) + .WillOnce(Return(1)); + EXPECT_CALL(GetMock(), g_dbus_node_info_new_for_xml(_, _)) + .WillOnce(Return(info)); + EXPECT_CALL(GetMock(), g_dbus_node_info_unref(_)) + .WillOnce(Return()); + EXPECT_CALL(GetMock(), g_dbus_connection_register_object(_, _, _, _, _, _, _)) + .WillOnce(Return(1)); + + int ret = notification_service_init(0); + EXPECT_EQ(ret, NOTIFICATION_ERROR_NONE); + + free(info->interfaces); + free(info); +} + +TEST_F(NotificationServiceTest, notification_service_fini) { + int ret = notification_service_fini(); + EXPECT_EQ(ret, NOTIFICATION_ERROR_NONE); +} + +TEST_F(NotificationServiceTest, notification_add_noti) { + notification_h info = reinterpret_cast( + malloc(sizeof(notification_h))); + EXPECT_CALL(GetMock(), notification_create(_)) + .WillOnce(Return(info)); + EXPECT_CALL(GetMock(), notification_free(_)) + .WillOnce(Return(NOTIFICATION_ERROR_NONE)); + EXPECT_CALL(GetMock(), notification_get_uid(_, _)) + .WillOnce(DoAll(SetArgPointee<1>(5001), Return(NOTIFICATION_ERROR_NONE))); + EXPECT_CALL(GetMock(), notification_noti_check_tag(_)) + .WillOnce(Return(NOTIFICATION_ERROR_NONE)); + EXPECT_CALL(GetMock(), notification_get_event_flag(_, _)) + .WillOnce(Return(NOTIFICATION_ERROR_NONE)); + EXPECT_CALL(GetMock(), + notification_ipc_make_noti_from_gvariant(_, _)) + .WillOnce(Return(NOTIFICATION_ERROR_NONE)); + + GVariant* body = g_variant_new("(i)", 0); + GVariant* param = g_variant_new("(v)", body); + GVariant* reply; + int ret = notification_add_noti(param, &reply, "sender", 100, 5001); + EXPECT_EQ(ret, NOTIFICATION_ERROR_NONE); + + g_object_unref(param); +} + +TEST_F(NotificationServiceTest, notification_update_noti) { + notification_h info = reinterpret_cast( + malloc(sizeof(notification_h))); + GVariant* body = g_variant_new("(i)", 0); + GVariant* param = g_variant_new("(v)", body); + EXPECT_CALL(GetMock(), notification_create(_)) + .WillOnce(Return(info)); + EXPECT_CALL(GetMock(), notification_free(_)) + .WillOnce(Return(NOTIFICATION_ERROR_NONE)); + EXPECT_CALL(GetMock(), notification_noti_update(_)) + .WillOnce(Return(NOTIFICATION_ERROR_NONE)); + EXPECT_CALL(GetMock(), + notification_ipc_make_gvariant_from_noti(_, _)) + .WillOnce(Return(body)); + EXPECT_CALL(GetMock(), notification_get_uid(_, _)) + .WillOnce(DoAll(SetArgPointee<1>(5001), Return(NOTIFICATION_ERROR_NONE))); + EXPECT_CALL(GetMock(), + notification_ipc_make_noti_from_gvariant(_, _)) + .WillOnce(Return(NOTIFICATION_ERROR_NONE)); + EXPECT_CALL(GetMock(), + notification_validate_private_sharing(_)) + .WillOnce(Return(true)); + EXPECT_CALL(GetMock(), + notification_launch_default_viewer(_, _, _)) + .WillOnce(Return(NOTIFICATION_ERROR_NONE)); + EXPECT_CALL(GetMock(), notification_get_pkgname(_, _)) + .WillOnce(Return(NOTIFICATION_ERROR_NONE)); + EXPECT_CALL(GetMock(), notification_get_tag(_, _)) + .WillOnce(Return(NOTIFICATION_ERROR_NONE)); + EXPECT_CALL(GetMock(), notification_get_text(_, _, _)) + .WillRepeatedly(Return(NOTIFICATION_ERROR_NONE)); + + GVariant* reply; + int ret = notification_update_noti(param, &reply, 100, 5001); + EXPECT_EQ(ret, NOTIFICATION_ERROR_NONE); + + g_object_unref(param); +} + +TEST_F(NotificationServiceTest, notification_refresh_noti) { + GVariant* param = g_variant_new("(i)", 5001); + GVariant* reply; + int ret = notification_refresh_noti(param, &reply, 5001); + EXPECT_EQ(ret, NOTIFICATION_ERROR_NONE); + + g_object_unref(param); +} + +TEST_F(NotificationServiceTest, notification_del_noti_single) { + EXPECT_CALL(GetMock(), + notification_noti_delete_by_priv_id_get_changes(_, _, _, _)) + .WillOnce(DoAll(SetArgPointee<2>(0), Return(NOTIFICATION_ERROR_NONE))); + GVariant* param = g_variant_new("(sii)", "appid", 10, 5001); + GVariant* reply; + int ret = notification_del_noti_single(param, &reply, 5001); + EXPECT_EQ(ret, NOTIFICATION_ERROR_NONE); + + g_object_unref(param); +} + +TEST_F(NotificationServiceTest, notification_del_noti_by_display_applist) { + EXPECT_CALL(GetMock(), + notification_noti_delete_by_display_applist(_, _, _, _)) + .WillOnce(DoAll(SetArgPointee<1>(0), Return(NOTIFICATION_ERROR_NONE))); + GVariant* param = g_variant_new("(ii)", 10, 5001); + GVariant* reply; + int ret = notification_del_noti_by_display_applist(param, &reply, 5001); + EXPECT_EQ(ret, NOTIFICATION_ERROR_NONE); + + g_object_unref(param); +} + +TEST_F(NotificationServiceTest, notification_get_noti_count) { + EXPECT_CALL(GetMock(), + notification_noti_get_count(_, _, _, _, _, _)) + .WillOnce(Return(NOTIFICATION_ERROR_NONE)); + GVariant* param = g_variant_new("(isiii)", 1, "appid", 1, 1, 5001); + GVariant* reply; + int ret = notification_get_noti_count(param, &reply, 5001); + EXPECT_EQ(ret, NOTIFICATION_ERROR_NONE); + + g_object_unref(param); +} + +TEST_F(NotificationServiceTest, notification_get_noti_all_count) { + EXPECT_CALL(GetMock(), + notification_noti_get_all_count(_, _, _)) + .WillOnce(Return(NOTIFICATION_ERROR_NONE)); + GVariant* param = g_variant_new("(ii)", 1, 5001); + GVariant* reply; + int ret = notification_get_noti_all_count(param, &reply, 5001); + EXPECT_EQ(ret, NOTIFICATION_ERROR_NONE); + + g_object_unref(param); +} + +TEST_F(NotificationServiceTest, notification_update_noti_setting) { + EXPECT_CALL(GetMock(), + notification_setting_db_update(_, _, _, _, _, _, _, _)) + .WillOnce(Return(NOTIFICATION_ERROR_NONE)); + GVariant* param = g_variant_new("(ssiiiiii)", "pkgname", "appid", 0, 0, 0, 0, + 1, 5001); + GVariant* reply; + int ret = notification_update_noti_setting(param, &reply, 5001); + EXPECT_EQ(ret, NOTIFICATION_ERROR_NONE); + + g_object_unref(param); +} + +TEST_F(NotificationServiceTest, notification_update_noti_sys_setting) { + EXPECT_CALL(GetMock(), + noti_system_setting_get_do_not_disturb(_, _)) + .WillOnce(Return(NOTIFICATION_ERROR_NONE)); + EXPECT_CALL(GetMock(), + notification_setting_db_update_system_setting(_, _, _, _, _, _, _, _, _, _)) + .WillOnce(Return(NOTIFICATION_ERROR_NONE)); + GVariant* param = g_variant_new("(iiiiiiiiii)", 2, 0, 0, 0, 0, 0, + 0, 0, 0, 5001); + GVariant* reply; + int ret = notification_update_noti_sys_setting(param, &reply, 5001); + EXPECT_EQ(ret, NOTIFICATION_ERROR_NONE); + + g_object_unref(param); +} + +TEST_F(NotificationServiceTest, notification_load_noti_by_tag) { + notification_h info = reinterpret_cast( + malloc(sizeof(notification_h))); + GVariant* body = g_variant_new("(i)", 0); + EXPECT_CALL(GetMock(), notification_create(_)) + .WillOnce(Return(info)); + EXPECT_CALL(GetMock(), + notification_noti_get_by_tag(_, _, _, _)) + .WillOnce(Return(NOTIFICATION_ERROR_NONE)); + EXPECT_CALL(GetMock(), + notification_ipc_make_gvariant_from_noti(_, _)) + .WillOnce(Return(body)); + GVariant* param = g_variant_new("(ssi)", "appid", "tag", 5001); + GVariant* reply; + int ret = notification_load_noti_by_tag(param, &reply, 5001); + EXPECT_EQ(ret, NOTIFICATION_ERROR_NONE); + + g_object_unref(param); + g_object_unref(body); +} + +TEST_F(NotificationServiceTest, notification_load_noti_by_priv_id) { + notification_h info = reinterpret_cast( + malloc(sizeof(notification_h))); + GVariant* body = g_variant_new("(i)", 0); + EXPECT_CALL(GetMock(), notification_create(_)) + .WillOnce(Return(info)); + EXPECT_CALL(GetMock(), + notification_noti_get_by_priv_id(_, _)) + .WillOnce(Return(NOTIFICATION_ERROR_NONE)); + EXPECT_CALL(GetMock(), + notification_ipc_make_gvariant_from_noti(_, _)) + .WillOnce(Return(body)); + GVariant* param = g_variant_new("(sii)", "appid", 0, 5001); + GVariant* reply; + int ret = notification_load_noti_by_priv_id(param, &reply, 5001); + EXPECT_EQ(ret, NOTIFICATION_ERROR_NONE); + + g_object_unref(param); + g_object_unref(body); +} + +TEST_F(NotificationServiceTest, notification_load_detail_list) { + EXPECT_CALL(GetMock(), + notification_noti_get_detail_list(_, _, _, _, _, _)) + .WillOnce(Return(NOTIFICATION_ERROR_NONE)); + GVariant* param = g_variant_new("(siiii)", "appid", 0, 0, 0, 5001); + GVariant* reply; + int ret = notification_load_detail_list(param, &reply, 5001); + EXPECT_EQ(ret, NOTIFICATION_ERROR_NONE); + + g_object_unref(param); +} + +TEST_F(NotificationServiceTest, notification_get_setting_array) { + EXPECT_CALL(GetMock(), + noti_setting_get_setting_array(_, _, _)) + .WillOnce(Return(NOTIFICATION_ERROR_NONE)); + GVariant* param = g_variant_new("(i)", 5001); + GVariant* reply; + int ret = notification_get_setting_array(param, &reply, 5001); + EXPECT_EQ(ret, NOTIFICATION_ERROR_NONE); + + g_object_unref(param); +} + +TEST_F(NotificationServiceTest, notification_load_system_setting) { + GVariant* param = g_variant_new("(i)", 5001); + EXPECT_CALL(GetMock(), + noti_system_setting_load_system_setting(_, _)) + .WillOnce(Return(NOTIFICATION_ERROR_NONE)); + EXPECT_CALL(GetMock(), + notification_ipc_make_gvariant_from_system_setting(_)) + .WillOnce(Return(param)); + GVariant* reply; + int ret = notification_load_system_setting(param, &reply, 5001); + EXPECT_EQ(ret, NOTIFICATION_ERROR_NONE); + + g_object_unref(param); +} + +TEST_F(NotificationServiceTest, notification_add_noti_template) { + notification_h info = reinterpret_cast( + malloc(sizeof(notification_h))); + EXPECT_CALL(GetMock(), notification_create(_)) + .WillOnce(Return(info)); + EXPECT_CALL(GetMock(), + notification_noti_add_template(_, _)) + .WillOnce(Return(NOTIFICATION_ERROR_NONE)); + GVariant* reply = g_variant_new("(i)", 10);; + GVariant* body = g_variant_new("(v)", reply); + GVariant* param = g_variant_new("(vs)", body, "template"); + int ret = notification_add_noti_template(param, &reply, 5001); + EXPECT_EQ(ret, NOTIFICATION_ERROR_NONE); + + g_object_unref(param); +} + +TEST_F(NotificationServiceTest, notification_get_noti_template) { + notification_h info = reinterpret_cast( + malloc(sizeof(notification_h))); + EXPECT_CALL(GetMock(), notification_create(_)) + .WillOnce(Return(info)); + EXPECT_CALL(GetMock(), + aul_app_get_appid_bypid_for_uid(_, _, _, _)) + .WillOnce(Return(AUL_R_OK)); + EXPECT_CALL(GetMock(), + notification_noti_get_package_template(_, _, _)) + .WillOnce(Return(NOTIFICATION_ERROR_NONE)); + GVariant* param = g_variant_new("(s)", "template"); + GVariant* reply = g_variant_new("(si)", "appid", 5001);; + EXPECT_CALL(GetMock(), + notification_ipc_make_gvariant_from_noti(_, _)) + .WillOnce(Return(reply)); + int ret = notification_get_noti_template(param, &reply, 10, 5001); + EXPECT_EQ(ret, NOTIFICATION_ERROR_NONE); + + g_object_unref(param); + g_object_unref(reply); +} + +TEST_F(NotificationServiceTest, notification_get_noti_package_template) { + notification_h info = reinterpret_cast( + malloc(sizeof(notification_h))); + EXPECT_CALL(GetMock(), notification_create(_)) + .WillOnce(Return(info)); + EXPECT_CALL(GetMock(), + notification_noti_get_package_template(_, _, _)) + .WillOnce(Return(NOTIFICATION_ERROR_NONE)); + GVariant* param = g_variant_new("(ss)", "appid", "template"); + GVariant* reply = g_variant_new("(si)", "appid", 5001);; + EXPECT_CALL(GetMock(), + notification_ipc_make_gvariant_from_noti(_, _)) + .WillOnce(Return(reply)); + int ret = notification_get_noti_package_template(param, &reply, 5001); + EXPECT_EQ(ret, NOTIFICATION_ERROR_NONE); + + g_object_unref(param); + g_object_unref(reply); +} + +TEST_F(NotificationServiceTest, notification_get_block_state) { + EXPECT_CALL(GetMock(), + notification_get_dnd_and_allow_to_notify(_, _, _, _, _)) + .WillOnce(Return(NOTIFICATION_ERROR_NONE)); + GVariant* param = g_variant_new("(si)", "appid", 5001); + GVariant* reply; + int ret = notification_get_block_state(param, &reply, 5001); + EXPECT_EQ(ret, NOTIFICATION_ERROR_NONE); + + g_object_unref(param); + g_object_unref(reply); +} + +TEST_F(NotificationServiceTest, notification_load_dnd_allow_exception) { + EXPECT_CALL(GetMock(), + notification_system_setting_load_dnd_allow_exception(_, _, _)) + .WillOnce(Return(NOTIFICATION_ERROR_NONE)); + GVariant* param = g_variant_new("(i)", 5001); + GVariant* reply; + int ret = notification_load_dnd_allow_exception(param, &reply, 5001); + EXPECT_EQ(ret, NOTIFICATION_ERROR_NONE); + + g_object_unref(param); + g_object_unref(reply); +} + +TEST_F(NotificationServiceTest, notification_update_dnd_allow_exception) { + EXPECT_CALL(GetMock(), + notification_system_setting_update_dnd_allow_exception(_, _, _)) + .WillOnce(Return(NOTIFICATION_ERROR_NONE)); + GVariant* param = g_variant_new("(iii)", 1, 5001, 5001); + GVariant* reply; + int ret = notification_update_dnd_allow_exception(param, &reply, 5001); + EXPECT_EQ(ret, NOTIFICATION_ERROR_NONE); + + g_object_unref(param); + g_object_unref(reply); +} + +TEST_F(NotificationServiceTest, notification_send_noti_event) { + notification_h info = reinterpret_cast( + malloc(sizeof(notification_h))); + EXPECT_CALL(GetMock(), notification_create(_)) + .WillOnce(Return(info)); + GVariant* reply = g_variant_new("(i)", 10); + GVariant* reply2; + GVariant* body = g_variant_new("(v)", reply); + GVariant* param2 = g_variant_new("(i)", 10); + GVariant* param = g_variant_new("(vi)", body, 10); + EXPECT_CALL(GetMock(), notification_get_id(_, _, _)) + .WillOnce(DoAll(SetArgPointee<2>(10), Return(NOTIFICATION_ERROR_NONE))); + EXPECT_CALL(GetMock(), + g_dbus_connection_emit_signal(_, _, _, _, _, _, _)) + .WillOnce(Return(TRUE)); + EXPECT_CALL(GetMock(), + g_bus_watch_name(_, _, _, _, _, _, _)) + .WillOnce(Return(1)); + int ret = notification_reset_event_receiver(param2, &reply2, "sender"); + EXPECT_EQ(ret, NOTIFICATION_ERROR_NONE); + ret = notification_send_noti_event(param, &reply); + EXPECT_EQ(ret, NOTIFICATION_ERROR_NONE); + + g_object_unref(param); + g_object_unref(reply); + g_object_unref(param2); + g_object_unref(reply2); +} + +TEST_F(NotificationServiceTest, notification_send_noti_event_by_priv_id) { + notification_h info = reinterpret_cast( + malloc(sizeof(notification_h))); + EXPECT_CALL(GetMock(), + g_dbus_connection_emit_signal(_, _, _, _, _, _, _)) + .WillOnce(Return(TRUE)); + EXPECT_CALL(GetMock(), + g_bus_watch_name(_, _, _, _, _, _, _)) + .WillOnce(Return(1)); + EXPECT_CALL(GetMock(), notification_create(_)) + .WillOnce(Return(info)); + GVariant* param2 = g_variant_new("(i)", 20); + GVariant* reply2; + int ret = notification_reset_event_receiver(param2, &reply2, "sender"); + EXPECT_EQ(ret, NOTIFICATION_ERROR_NONE); + + GVariant* reply = g_variant_new("(i)", 10); + GVariant* param = g_variant_new("(ii)", 20, 1); + EXPECT_CALL(GetMock(), notification_get_event_flag(_, _)) + .WillOnce(DoAll(SetArgPointee<1>(true), Return(NOTIFICATION_ERROR_NONE))); + EXPECT_CALL(GetMock(), notification_noti_get_by_priv_id(_, _)) + .WillOnce(Return(NOTIFICATION_ERROR_NONE)); + EXPECT_CALL(GetMock(), + notification_ipc_make_gvariant_from_noti(_, _)) + .WillOnce(Return(reply)); + + ret = notification_send_noti_event_by_priv_id(param, &reply); + EXPECT_EQ(ret, NOTIFICATION_ERROR_NONE); + + g_object_unref(param); + g_object_unref(reply); + g_object_unref(param2); + g_object_unref(reply2); +} + +TEST_F(NotificationServiceTest, notification_reset_event_receiver) { + GVariant* param = g_variant_new("(i)", 1); + GVariant* reply; + int ret = notification_reset_event_receiver(param, &reply, "sender"); + EXPECT_EQ(ret, NOTIFICATION_ERROR_NONE); + + g_object_unref(param); + g_object_unref(reply); +} + +TEST_F(NotificationServiceTest, notification_check_event_receiver) { + GVariant* param = g_variant_new("(i)", 1); + GVariant* reply; + int ret = notification_reset_event_receiver(param, &reply, "sender"); + EXPECT_EQ(ret, NOTIFICATION_ERROR_NONE); + ret = notification_check_event_receiver(param, &reply); + EXPECT_EQ(ret, NOTIFICATION_ERROR_NONE); + + g_object_unref(param); + g_object_unref(reply); +} + +TEST_F(NotificationServiceTest, notification_delete_noti_by_app_id) { + EXPECT_CALL(GetMock(), + notification_noti_delete_all(_, _, _, _, _)) + .WillOnce(Return(NOTIFICATION_ERROR_NONE)); + int ret = notification_delete_noti_by_app_id("appid", 5001); + EXPECT_EQ(ret, NOTIFICATION_ERROR_NONE); +} \ No newline at end of file diff --git a/tests/unit_tests/src/test_service_common.cc b/tests/unit_tests/src/test_service_common.cc new file mode 100644 index 0000000..e0c93e7 --- /dev/null +++ b/tests/unit_tests/src/test_service_common.cc @@ -0,0 +1,105 @@ +/* + * Copyright (c) 2023 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 "tests/mock/aul_mock.h" +#include "tests/mock/gio_mock.h" +#include "tests/mock/test_fixture.h" + +#include "include/service_common.h" + +using ::testing::_; +using ::testing::Invoke; +using ::testing::Return; + +namespace { + +class Mocks : public ::testing::NiceMock, + public ::testing::NiceMock {}; + +} //namespace + +class ServiceCommonServiceTest : public TestFixture { + public: + ServiceCommonServiceTest() : TestFixture(std::make_unique()) {} + virtual ~ServiceCommonServiceTest() {} + + virtual void SetUp() { + } + + virtual void TearDown() { + } + +}; + +TEST_F(ServiceCommonServiceTest, get_sender_uid) { + GDBusMessage* msg = static_cast(g_object_new(G_TYPE_OBJECT, NULL)); + GDBusMessage* msg2 = static_cast(g_object_new(G_TYPE_OBJECT, NULL)); + EXPECT_CALL(GetMock(), + g_dbus_message_new_method_call(_, _, _, _)) + .WillRepeatedly(Return(msg)); + EXPECT_CALL(GetMock(), + g_dbus_connection_send_message_with_reply_sync( + _, _, _, _, _, _, _)) + .WillRepeatedly(Return(msg2)); + EXPECT_CALL(GetMock(), g_dbus_message_get_body(_)) + .WillRepeatedly(Return(g_variant_new("(u)", 11))); + + uid_t uid = get_sender_uid("sender"); + EXPECT_EQ(uid, 11); +} + +TEST_F(ServiceCommonServiceTest, get_sender_pid) { + GDBusMessage* msg = static_cast(g_object_new(G_TYPE_OBJECT, NULL)); + GDBusMessage* msg2 = static_cast(g_object_new(G_TYPE_OBJECT, NULL)); + EXPECT_CALL(GetMock(), + g_dbus_message_new_method_call(_, _, _, _)) + .WillRepeatedly(Return(msg)); + EXPECT_CALL(GetMock(), + g_dbus_connection_send_message_with_reply_sync( + _, _, _, _, _, _, _)) + .WillRepeatedly(Return(msg2)); + EXPECT_CALL(GetMock(), g_dbus_message_get_body(_)) + .WillRepeatedly(Return(g_variant_new("(u)", 11))); + + pid_t pid = get_sender_pid("sender"); + EXPECT_EQ(pid, 11); +} + +TEST_F(ServiceCommonServiceTest, service_register) { + GVariant* param = g_variant_new("(i)", 5001); + GVariant* reply; + GHashTable* hash = g_hash_table_new(g_direct_hash, g_direct_equal); + EXPECT_CALL(GetMock(), + g_bus_watch_name_on_connection( + _, _, _, _, _, _, _)) + .WillRepeatedly(Return(1)); + + int ret = service_register(param, &reply, "sender", nullptr, nullptr, &hash, + 5001); + EXPECT_EQ(ret, SERVICE_COMMON_ERROR_NONE); +} + +TEST_F(ServiceCommonServiceTest, delete_monitoring_list) { + GHashTable* hash = g_hash_table_new(g_direct_hash, g_direct_equal); + GList* list = nullptr; + list = g_list_append(list, strdup("sender")); + g_hash_table_insert(hash, GUINT_TO_POINTER(5001), list); + + int ret = delete_monitoring_list(&hash, "sender", 5001); + EXPECT_EQ(ret, SERVICE_COMMON_ERROR_NONE); +} \ No newline at end of file diff --git a/tests/unit_tests/src/test_shortcut_service.cc b/tests/unit_tests/src/test_shortcut_service.cc new file mode 100644 index 0000000..24487a8 --- /dev/null +++ b/tests/unit_tests/src/test_shortcut_service.cc @@ -0,0 +1,110 @@ +/* + * Copyright (c) 2023 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/service_common.h" +#include "include/shortcut_service.h" +#include "tests/mock/shortcut_mock.h" +#include "tests/mock/gio_mock.h" +#include "tests/mock/test_fixture.h" + +using ::testing::_; +using ::testing::DoAll; +using ::testing::Return; +using ::testing::SetArgPointee; + +namespace { + +class Mocks : public ::testing::NiceMock, + public ::testing::NiceMock {}; + +} //namespace + +class ShortcutServiceTest : public TestFixture { + public: + ShortcutServiceTest() : TestFixture(std::make_unique()) {} + virtual ~ShortcutServiceTest() {} + + virtual void SetUp() { + } + + virtual void TearDown() { + } +}; + +TEST_F(ShortcutServiceTest, shortcut_service_init) { + GDBusNodeInfo* info = reinterpret_cast( + malloc(sizeof(GDBusNodeInfo))); + info->interfaces = reinterpret_cast( + malloc(sizeof(GDBusInterfaceInfo*))); + EXPECT_CALL(GetMock(), shortcut_db_init()) + .WillOnce(Return(0)); + EXPECT_CALL(GetMock(), g_bus_own_name(_, _, _, _, _, _, _, _)) + .WillOnce(Return(1)); + EXPECT_CALL(GetMock(), g_dbus_node_info_new_for_xml(_, _)) + .WillOnce(Return(info)); + EXPECT_CALL(GetMock(), g_dbus_node_info_unref(_)) + .WillOnce(Return()); + EXPECT_CALL(GetMock(), g_dbus_connection_register_object(_, _, _, _, _, _, _)) + .WillOnce(Return(1)); + + int ret = shortcut_service_init(); + EXPECT_EQ(ret, SHORTCUT_ERROR_NONE); + + free(info->interfaces); + free(info); +} + +TEST_F(ShortcutServiceTest, shortcut_get_shortcut_service_list) { + GVariantBuilder* b = g_variant_builder_new(G_VARIANT_TYPE("a{sv}")); + g_variant_builder_add(b, "{sv}", "package_name", g_variant_new_string("name")); + GVariant* body = g_variant_builder_end(b); + GVariant* param = g_variant_new("(v)", body); + + GVariant* reply; + int ret = shortcut_get_shortcut_service_list(param, &reply, 5001); + EXPECT_EQ(ret, SHORTCUT_ERROR_NONE); + + g_object_unref(param); + g_object_unref(reply); +} + +TEST_F(ShortcutServiceTest, shortcut_send_return_value) { + EXPECT_CALL(GetMock(), g_dbus_method_invocation_return_value(_, _)) + .WillOnce(Return()); + GDBusMethodInvocation* invoc = reinterpret_cast( + malloc(sizeof(GDBusMethodInvocation*))); + GVariant* param = g_variant_new("(sis)", "request_id", 10, "appid"); + GVariant* param2 = g_variant_new("(is)", 1, "request_id"); + GVariant* reply; + + shortcut_add(param, invoc, 5001); + shortcut_add_widget(param, invoc, 5001); + shortcut_remove(param, invoc, 5001); + + int ret = shortcut_send_return_value(param2, &reply); + EXPECT_EQ(ret, SHORTCUT_ERROR_NONE); + + g_object_unref(param); + g_object_unref(reply); + free(invoc); +} + +TEST_F(ShortcutServiceTest, shortcut_service_fini) { + int ret = shortcut_service_fini(); + EXPECT_EQ(ret, SHORTCUT_ERROR_NONE); +} -- 2.7.4 From e4e94792dc8ded953386081e5d52eec5f9fe16b1 Mon Sep 17 00:00:00 2001 From: Inkyun Kil Date: Tue, 7 Feb 2023 10:26:58 +0900 Subject: [PATCH 03/16] Refactor legacy C code for pkgmgr_event changes: - C -> C++ - Reduces needless function call - Improve trivial structure Change-Id: Ief1e95c6ad3b942908b3093980418d0d20bc5c1a Signed-off-by: Inkyun Kil --- include/pkgmgr.h | 51 ---- src/CMakeLists.txt | 5 +- src/notification_service.c | 2 - src/pkgmgr.c | 502 -------------------------------- src/pkgmgr_app_event_args.cc | 61 ++++ src/pkgmgr_app_event_args.hh | 55 ++++ src/pkgmgr_client.cc | 102 +++++++ src/pkgmgr_client.hh | 59 ++++ src/pkgmgr_event_args.cc | 61 ++++ src/pkgmgr_event_args.hh | 53 ++++ src/service_common.c | 549 ----------------------------------- src/service_common.cc | 620 ++++++++++++++++++++++++++++++++++++++++ tests/unit_tests/CMakeLists.txt | 6 +- 13 files changed, 1018 insertions(+), 1108 deletions(-) delete mode 100755 include/pkgmgr.h delete mode 100644 src/pkgmgr.c create mode 100644 src/pkgmgr_app_event_args.cc create mode 100644 src/pkgmgr_app_event_args.hh create mode 100644 src/pkgmgr_client.cc create mode 100644 src/pkgmgr_client.hh create mode 100644 src/pkgmgr_event_args.cc create mode 100644 src/pkgmgr_event_args.hh delete mode 100644 src/service_common.c create mode 100644 src/service_common.cc diff --git a/include/pkgmgr.h b/include/pkgmgr.h deleted file mode 100755 index 2fc3c3f..0000000 --- a/include/pkgmgr.h +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright 2016 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. - */ - -#ifndef PKGMGR_H_ -#define PKGMGR_H_ - -#ifdef __cplusplus -extern "C" { -#endif - -enum pkgmgr_event_type { - PKGMGR_EVENT_DOWNLOAD, - PKGMGR_EVENT_INSTALL, - PKGMGR_EVENT_UPDATE, - PKGMGR_EVENT_UNINSTALL, - PKGMGR_EVENT_RECOVER, - PKGMGR_EVENT_APP_ENABLE, - PKGMGR_EVENT_APP_DISABLE, -}; - -enum pkgmgr_status { - PKGMGR_STATUS_START, - PKGMGR_STATUS_PROCESSING, - PKGMGR_STATUS_COMMAND, - PKGMGR_STATUS_END, - PKGMGR_STATUS_ERROR, -}; - -extern int pkgmgr_init(void); -extern int pkgmgr_fini(void); - -extern int pkgmgr_add_event_callback(enum pkgmgr_event_type type, int (*cb)(uid_t uid, const char *pkgname, enum pkgmgr_status status, double value, void *data), void *data); -extern void *pkgmgr_del_event_callback(enum pkgmgr_event_type type, int (*cb)(uid_t uid, const char *pkgname, enum pkgmgr_status status, double value, void *data), void *data); -#ifdef __cplusplus -} -#endif - -#endif //PKGMGR_H_ \ No newline at end of file diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index e439599..fbf76de 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -28,13 +28,14 @@ FOREACH(flag ${pkgs_CFLAGS}) ENDFOREACH(flag) SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -Wall -Werror -Winline") -SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${EXTRA_CFLAGS} -std=c++14") +SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${EXTRA_CFLAGS} -std=c++17") SET(CMAKE_CXX_FLAGS_DEBUG "-O0 -g") SET(CMAKE_CXX_FLAGS_RELEASE "-O2") INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/../include) +INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/.) -AUX_SOURCE_DIRECTORY(../src SOURCES) +AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} SOURCES) ADD_EXECUTABLE(${PROJECT_NAME} ${SOURCES}) SET_TARGET_PROPERTIES(${PROJECT_NAME} PROPERTIES COMPILE_FLAGS "${EXTRA_CFLAGS} -fPIE") diff --git a/src/notification_service.c b/src/notification_service.c index 88dd344..50bb5b5 100644 --- a/src/notification_service.c +++ b/src/notification_service.c @@ -27,7 +27,6 @@ #include #include -#include "pkgmgr.h" #include "service_common.h" #include "notification_service.h" #include "debug.h" @@ -2669,7 +2668,6 @@ HAPI int notification_service_init(int restart_count) HAPI int notification_service_fini(void) { - pkgmgr_fini(); DBG("Successfully Finalized"); return NOTIFICATION_ERROR_NONE; } diff --git a/src/pkgmgr.c b/src/pkgmgr.c deleted file mode 100644 index af1512a..0000000 --- a/src/pkgmgr.c +++ /dev/null @@ -1,502 +0,0 @@ -/* - * Copyright 2016 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 - -#include "service_common.h" -#include "debug.h" -#include "pkgmgr.h" -#include "conf.h" - -#define PKGMGR_EVENT_MIN PKGMGR_EVENT_DOWNLOAD -#define PKGMGR_EVENT_MAX PKGMGR_EVENT_APP_DISABLE - -struct item { - uid_t uid; - char *pkgname; - char *icon; - - enum pkgmgr_event_type type; - enum pkgmgr_status status; -}; - -struct event_item { - int (*cb)(uid_t uid, const char *pkgname, enum pkgmgr_status status, double value, void *data); - void *data; -}; - -static struct { - pkgmgr_client *listen_pc; - GList *item_list; - -} s_info = { - .listen_pc = NULL, - .item_list = NULL, -}; - -static GList *event_list[PKGMGR_EVENT_MAX + 1]; - -static inline void invoke_callback(uid_t uid, const char *pkgname, struct item *item, double value) -{ - GList *l; - struct event_item *e_item; - - for (l = event_list[item->type]; l != NULL; l = g_list_next(l)) { - e_item = (struct event_item *)g_list_nth_data(l, 0); - if (e_item->cb) - e_item->cb(uid, pkgname, item->status, value, e_item->data); - } -} - -static inline int is_valid_status(struct item *item, const char *status) -{ - const char *expected_status; - - switch (item->type) { - case PKGMGR_EVENT_DOWNLOAD: - expected_status = "download"; - break; - case PKGMGR_EVENT_UNINSTALL: - expected_status = "uninstall"; - break; - case PKGMGR_EVENT_INSTALL: - expected_status = "install"; - break; - case PKGMGR_EVENT_UPDATE: - expected_status = "update"; - break; - case PKGMGR_EVENT_RECOVER: - expected_status = "recover"; - break; - case PKGMGR_EVENT_APP_ENABLE: - expected_status = "app_enable_app"; - break; - case PKGMGR_EVENT_APP_DISABLE: - expected_status = "app_disable_app"; - break; - default: - return 0; - } - - return !strcasecmp(status, expected_status); -} - -static struct item *find_item(const char *pkgname, uid_t uid) -{ - GList *l; - struct item *item; - - if (!pkgname) { - ERR("Invalid pkgname"); - return NULL; - } - - for (l = s_info.item_list; l != NULL; l = g_list_next(l)) { - item = g_list_nth_data(l, 0); - if (uid != item->uid || strcmp(item->pkgname, pkgname)) - continue; - - return item; - } - - DBG("Package [%s] is not found", pkgname); - return NULL; -} - -static int start_cb(uid_t uid, const char *pkgname, const char *val, void *data) -{ - struct item *item; - - INFO("pkgname[%s] val[%s]", pkgname, val); - - item = calloc(1, sizeof(*item)); - if (!item) { - ERR("calloc [%d]", errno); - return SERVICE_COMMON_ERROR_OUT_OF_MEMORY; - } - - item->pkgname = strdup(pkgname); - if (!item->pkgname) { - ERR("strdup [%d]", errno); - free(item); - return SERVICE_COMMON_ERROR_OUT_OF_MEMORY; - } - - item->uid = uid; - item->status = PKGMGR_STATUS_START; - - if (!strcasecmp(val, "download")) { - item->type = PKGMGR_EVENT_DOWNLOAD; - } else if (!strcasecmp(val, "uninstall")) { - item->type = PKGMGR_EVENT_UNINSTALL; - } else if (!strcasecmp(val, "install")) { - item->type = PKGMGR_EVENT_INSTALL; - } else if (!strcasecmp(val, "update")) { - item->type = PKGMGR_EVENT_UPDATE; - } else if (!strcasecmp(val, "recover")) { - item->type = PKGMGR_EVENT_RECOVER; - } else if (!strcasecmp(val, "enable_app")) { - item->type = PKGMGR_EVENT_APP_ENABLE; - } else if (!strcasecmp(val, "disable_app")) { - item->type = PKGMGR_EVENT_APP_DISABLE; - } else { - free(item->pkgname); - free(item); - ERR("Invalid val [%s]", val); - return SERVICE_COMMON_ERROR_INVALID_PARAMETER; - } - s_info.item_list = g_list_append(s_info.item_list, item); - - invoke_callback(uid, pkgname, item, 0.0f); - return SERVICE_COMMON_ERROR_NONE; -} - -static int icon_path_cb(uid_t uid, const char *pkgname, const char *val, void *data) -{ - struct item *item; - - DBG("pkgname[%s] val[%s]", pkgname, val); - - item = find_item(pkgname, uid); - if (!item) - return SERVICE_COMMON_ERROR_NOT_EXIST; - - if (item->icon) - free(item->icon); - - item->icon = strdup(val); - if (!item->icon) { - ERR("strdup [%d]", errno); - return SERVICE_COMMON_ERROR_OUT_OF_MEMORY; - } - - return SERVICE_COMMON_ERROR_NONE; -} - -static int command_cb(uid_t uid, const char *pkgname, const char *val, void *data) -{ - struct item *item; - - DBG("pkgname[%s] val[%s]", pkgname, val); - - item = find_item(pkgname, uid); - if (!item) - return SERVICE_COMMON_ERROR_NOT_EXIST; - - if (!is_valid_status(item, val)) { - DBG("Invalid status[%d] val[%s]", item->type, val); - return SERVICE_COMMON_ERROR_INVALID_PARAMETER; - } - - item->status = PKGMGR_STATUS_COMMAND; - invoke_callback(uid, pkgname, item, 0.0f); - return SERVICE_COMMON_ERROR_NONE; -} - -static int error_cb(uid_t uid, const char *pkgname, const char *val, void *data) -{ - /* val = error */ - struct item *item; - - INFO("pkgname[%s] val[%s]", pkgname, val); - - item = find_item(pkgname, uid); - if (!item) - return SERVICE_COMMON_ERROR_NOT_EXIST; - - item->status = PKGMGR_STATUS_ERROR; - invoke_callback(uid, pkgname, item, 0.0f); - return SERVICE_COMMON_ERROR_NONE; -} - -static int change_pkgname_cb(uid_t uid, const char *pkgname, const char *val, void *data) -{ - struct item *item; - char *new_pkgname; - - INFO("pkgname[%s] val[%s]", pkgname, val); - - item = find_item(pkgname, uid); - if (!item) - return SERVICE_COMMON_ERROR_NOT_EXIST; - - new_pkgname = strdup(val); - if (!new_pkgname) { - ERR("strdup: %d", errno); - return SERVICE_COMMON_ERROR_OUT_OF_MEMORY; - } - - free(item->pkgname); - item->pkgname = new_pkgname; - return SERVICE_COMMON_ERROR_NONE; -} - -static int download_cb(uid_t uid, const char *pkgname, const char *val, void *data) -{ - /* val = integer */ - struct item *item; - double value; - - INFO("pkgname[%s] val[%s]", pkgname, val); - - item = find_item(pkgname, uid); - if (!item) { - DBG("ITEM is not started from the start_cb"); - return SERVICE_COMMON_ERROR_INVALID_PARAMETER; - } - - if (item->type != PKGMGR_EVENT_DOWNLOAD) { - DBG("TYPE is not \"download\" [%d]", item->type); - item->type = PKGMGR_EVENT_DOWNLOAD; - } - - switch (item->status) { - case PKGMGR_STATUS_START: - case PKGMGR_STATUS_COMMAND: - item->status = PKGMGR_STATUS_PROCESSING; - case PKGMGR_STATUS_PROCESSING: - break; - default: - ERR("Invalid status, pkgname[%s] val[%s]", pkgname, val); - return SERVICE_COMMON_ERROR_INVALID_PARAMETER; - } - - if (val) { - if (sscanf(val, "%lf", &value) != 1) - value = (double)SERVICE_COMMON_ERROR_INVALID_PARAMETER; - } else { - value = (double)SERVICE_COMMON_ERROR_INVALID_PARAMETER; - } - - invoke_callback(uid, pkgname, item, value); - return SERVICE_COMMON_ERROR_NONE; -} - -static int progress_cb(uid_t uid, const char *pkgname, const char *val, void *data) -{ - /* val = integer */ - struct item *item; - double value; - - item = find_item(pkgname, uid); - if (!item) { - ERR("ITEM is not started from the start_cb"); - return SERVICE_COMMON_ERROR_INVALID_PARAMETER; - } - - switch (item->status) { - case PKGMGR_STATUS_START: - case PKGMGR_STATUS_COMMAND: - item->status = PKGMGR_STATUS_PROCESSING; - case PKGMGR_STATUS_PROCESSING: - break; - default: - ERR("Invalid status, pkgname[%s] val[%s]", pkgname, val); - return SERVICE_COMMON_ERROR_INVALID_PARAMETER; - } - - if (val) { - if (sscanf(val, "%lf", &value) != 1) - value = (double)SERVICE_COMMON_ERROR_INVALID_PARAMETER; - } else { - value = (double)SERVICE_COMMON_ERROR_INVALID_PARAMETER; - } - - invoke_callback(uid, pkgname, item, value); - return SERVICE_COMMON_ERROR_NONE; -} - -static int end_cb(uid_t uid, const char *pkgname, const char *val, void *data) -{ - struct item *item; - - DBG("[%s] %s", pkgname, val); - - item = find_item(pkgname, uid); - if (!item) - return SERVICE_COMMON_ERROR_NOT_EXIST; - - item->status = !strcasecmp(val, "ok") ? PKGMGR_STATUS_END : PKGMGR_STATUS_ERROR; - - invoke_callback(uid, pkgname, item, 0.0f); - - s_info.item_list = g_list_remove(s_info.item_list, item); - free(item->icon); - free(item->pkgname); - free(item); - return SERVICE_COMMON_ERROR_NONE; -} - -static struct pkgmgr_handler { - const char *key; - int (*func)(uid_t uid, const char *package, const char *val, void *data); -} handler[] = { - { "install_percent", progress_cb }, - { "download_percent", download_cb }, - { "start", start_cb }, - { "end", end_cb }, - { "change_pkg_name", change_pkgname_cb }, - { "icon_path", icon_path_cb }, - { "command", command_cb }, - { "error", error_cb }, - { NULL, NULL }, -}; - -static int status_cb(uid_t target_uid, int req_id, const char *type, - const char *pkgname, const char *key, const char *val, - const void *pmsg, void *data) -{ - register int i; - int ret; - - for (i = 0; handler[i].key; i++) { - if (strcasecmp(key, handler[i].key)) - continue; - - ret = handler[i].func(target_uid, pkgname, val, data); - if (ret < 0) { - DBG("REQ[%d] UID[%d] pkgname[%s] type[%s] key[%s] val[%s] ret[%d]", - req_id, target_uid, pkgname, type, key, val, ret); - } - } - - return SERVICE_COMMON_ERROR_NONE; -} - -static int app_status_cb(uid_t target_uid, int req_id, const char *pkg_type, - const char *pkgid, const char *appid, const char *key, const char *val, - const void *pmsg, void *data) -{ - register int i; - int ret; - - for (i = 0; handler[i].key; i++) { - if (strcasecmp(key, handler[i].key)) - continue; - - ret = handler[i].func(target_uid, appid, val, data); - if (ret < 0) { - DBG("REQ[%d] UID[%d] appid[%s] type[%s] key[%s] val[%s] ret[%d]", - req_id, target_uid, appid, pkg_type, key, val, ret); - } - } - - return SERVICE_COMMON_ERROR_NONE; -} - -static void __free_event_data(gpointer data) -{ - struct event_item *item = (struct event_item *)data; - - if (item) - free(item); -} - -static void __free_item_data(gpointer data) -{ - struct item *ctx = (struct item *)data; - - if (ctx) { - if (ctx->pkgname) - free(ctx->pkgname); - if (ctx->icon) - free(ctx->icon); - free(ctx); - } -} - -HAPI int pkgmgr_init(void) -{ - if (s_info.listen_pc) - return SERVICE_COMMON_ERROR_ALREADY_EXIST; - - s_info.listen_pc = pkgmgr_client_new(PC_LISTENING); - if (!s_info.listen_pc) - return SERVICE_COMMON_ERROR_FAULT; - - /* the pkgmgr api returns an id greater than 1 */ - if (pkgmgr_client_listen_status(s_info.listen_pc, status_cb, NULL) <= 0) - return SERVICE_COMMON_ERROR_FAULT; - - if (pkgmgr_client_listen_app_status(s_info.listen_pc, app_status_cb, NULL) <= 0) - return SERVICE_COMMON_ERROR_FAULT; - - return SERVICE_COMMON_ERROR_NONE; -} - -HAPI int pkgmgr_fini(void) -{ - int i; - - if (!s_info.listen_pc) - return SERVICE_COMMON_ERROR_INVALID_PARAMETER; - - if (pkgmgr_client_free(s_info.listen_pc) != PKGMGR_R_OK) - return SERVICE_COMMON_ERROR_FAULT; - - s_info.listen_pc = NULL; - - for (i = PKGMGR_EVENT_MIN; i <= PKGMGR_EVENT_MAX; i++) - g_list_free_full(event_list[i], __free_event_data); - - g_list_free_full(s_info.item_list, __free_item_data); - - return SERVICE_COMMON_ERROR_NONE; -} - -HAPI int pkgmgr_add_event_callback(enum pkgmgr_event_type type, int (*cb)(uid_t uid, const char *pkgname, enum pkgmgr_status status, double value, void *data), void *data) -{ - struct event_item *item; - - item = calloc(1, sizeof(*item)); - if (!item) { - ERR("calloc [%d]", errno); - return SERVICE_COMMON_ERROR_OUT_OF_MEMORY; - } - - item->cb = cb; - item->data = data; - - event_list[type] = g_list_prepend(event_list[type], item); - - return SERVICE_COMMON_ERROR_NONE; -} - -HAPI void *pkgmgr_del_event_callback(enum pkgmgr_event_type type, int (*cb)(uid_t uid, const char *pkgname, enum pkgmgr_status status, double value, void *data), void *data) -{ - struct event_item *item; - GList *l; - void *cbdata = NULL; - - for (l = event_list[type]; l != NULL; l = g_list_next(l)) { - item = g_list_nth_data(l, 0); - if (item->cb == cb && item->data == data) { - event_list[type] = g_list_remove(event_list[type], item); - cbdata = item->data; - free(item); - break; - } - } - - return cbdata; -} - -/* End of a file */ diff --git a/src/pkgmgr_app_event_args.cc b/src/pkgmgr_app_event_args.cc new file mode 100644 index 0000000..76b6775 --- /dev/null +++ b/src/pkgmgr_app_event_args.cc @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2023 Samsung Electronics Co., Ltd All Rights Reserved + * + * 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 "pkgmgr_app_event_args.hh" + +namespace dpm { + +PkgmgrAppEventArgs::PkgmgrAppEventArgs(uid_t target_uid, + std::string pkg_type, std::string pkgid, std::string appid, + std::string event_status, std::string event_name) + : target_uid_(target_uid), + pkg_type_(std::move(pkg_type)), + pkgid_(std::move(pkgid)), + appid_(std::move(appid)), + event_status_(std::move(event_status)), + event_name_(std::move(event_name)) { + tag_ = std::to_string(target_uid_) + "-" + pkgid_ + "-" + appid_; +} + +uid_t PkgmgrAppEventArgs::GetTargetUid() const { + return target_uid_; +} + +const std::string& PkgmgrAppEventArgs::GetPkgType() const { + return pkg_type_; +} + +const std::string& PkgmgrAppEventArgs::GetPkgId() const { + return pkgid_; +} + +const std::string& PkgmgrAppEventArgs::GetAppId() const { + return appid_; +} + +const std::string& PkgmgrAppEventArgs::GetEventStatus() const { + return event_status_; +} + +const std::string& PkgmgrAppEventArgs::GetEventName() const { + return event_name_; +} + +const std::string& PkgmgrAppEventArgs::GetTag() const { + return tag_; +} + +} // namespace dpm diff --git a/src/pkgmgr_app_event_args.hh b/src/pkgmgr_app_event_args.hh new file mode 100644 index 0000000..1c3f52f --- /dev/null +++ b/src/pkgmgr_app_event_args.hh @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2023 Samsung Electronics Co., Ltd All Rights Reserved + * + * 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. + */ + +#ifndef PKGMGR_EVENT_PKGMGR_APP_EVENT_ARGS_HH_ +#define PKGMGR_EVENT_PKGMGR_APP_EVENT_ARGS_HH_ + +#include +#include + +#include + +namespace dpm { + +class PkgmgrAppEventArgs { + public: + PkgmgrAppEventArgs(uid_t target_uid, std::string pkg_type, + std::string pkgid, std::string appid, std::string event_status, + std::string event_name); + virtual ~PkgmgrAppEventArgs() = default; + + uid_t GetTargetUid() const; + int GetReqId() const; + const std::string& GetPkgType() const; + const std::string& GetPkgId() const; + const std::string& GetAppId() const; + const std::string& GetEventStatus() const; + const std::string& GetEventName() const; + const std::string& GetTag() const; + + private: + uid_t target_uid_; + std::string pkg_type_; + std::string pkgid_; + std::string appid_; + std::string event_status_; + std::string event_name_; + std::string tag_; +}; + +} // namespace dpm + +#endif // PKGMGR_EVENT_PKGMGR_APP_EVENT_ARGS_HH_ diff --git a/src/pkgmgr_client.cc b/src/pkgmgr_client.cc new file mode 100644 index 0000000..8bbe930 --- /dev/null +++ b/src/pkgmgr_client.cc @@ -0,0 +1,102 @@ +/* + * Copyright (c) 2023 Samsung Electronics Co., Ltd All Rights Reserved + * + * 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 "pkgmgr_client.hh" +#include +#include + +namespace dpm { + +PkgmgrClient::PkgmgrClient() = default; + +PkgmgrClient::~PkgmgrClient() { + Ignore(); +} + +int PkgmgrClient::Listen(IEvent* listener) { + WARN("[PKGMGR] BEGIN"); + listener_ = listener; + + if (handle_ != nullptr) + return -1; + + auto* handle = pkgmgr_client_new(PC_LISTENING); + if (handle == nullptr) { + ERR("pkgmgr_client_new() is failed"); + return -1; + } + auto handle_auto = + std::unique_ptr( + handle, pkgmgr_client_free); + + int ret = pkgmgr_client_set_status_type(handle, PKGMGR_CLIENT_STATUS_ALL); + if (ret < 0) { + ERR("pkgmgr_client_set_status_type() is failed. error(%d)", ret); + return -1; + } + + ret = pkgmgr_client_listen_status(handle, PkgmgrHandler, this); + if (ret < 0) { + ERR("pkgmgr_client_listen_status() is failed. error(%d)", ret); + return -1; + } + + ret = pkgmgr_client_listen_app_status(handle, PkgmgrAppHandler, this); + if (ret < 0) { + ERR("pkgmgr_client_listen_app_status() is failed. error(%d)", ret); + return -1; + } + + handle_ = handle_auto.release(); + WARN("[PKGMGR] END"); + + return 0; +} + +void PkgmgrClient::Ignore() { + if (handle_ != nullptr) { + pkgmgr_client_free(handle_); + handle_ = nullptr; + } +} + +int PkgmgrClient::PkgmgrHandler(uid_t target_uid, int req_id, + const char* pkg_type, const char* pkgid, const char* key, const char* val, + const void* pmsg, void* user_data) { + auto* client = static_cast(user_data); + auto* listener = client->listener_; + if (listener != nullptr) { + listener->OnPkgmgrEvent(std::make_shared( + target_uid, req_id, pkg_type, pkgid, key, val)); + } + + return 0; +} + +int PkgmgrClient::PkgmgrAppHandler(uid_t target_uid, int req_id, + const char* pkg_type, const char* pkgid, const char* appid, + const char* key, const char* val, const void* pmsg, void* user_data) { + auto* client = static_cast(user_data); + auto* listener = client->listener_; + if (listener != nullptr) { + listener->OnPkgmgrAppEvent(std::make_shared( + target_uid, pkg_type, pkgid, appid, key, val)); + } + + return 0; +} + +} // namespace dpm diff --git a/src/pkgmgr_client.hh b/src/pkgmgr_client.hh new file mode 100644 index 0000000..d29e05a --- /dev/null +++ b/src/pkgmgr_client.hh @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2023 Samsung Electronics Co., Ltd All Rights Reserved + * + * 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. + */ + +#ifndef PKGMGR_EVENT_PKGMGR_CLIENT_HH_ +#define PKGMGR_EVENT_PKGMGR_CLIENT_HH_ + +#include + +#include + +#include "debug.h" +#include "pkgmgr_app_event_args.hh" +#include "pkgmgr_event_args.hh" + +namespace dpm { + +class PkgmgrClient { + public: + class IEvent { + public: + virtual void OnPkgmgrEvent(std::shared_ptr args) = 0; + virtual void OnPkgmgrAppEvent(std::shared_ptr args) = 0; + }; + + PkgmgrClient(); + virtual ~PkgmgrClient(); + + int Listen(IEvent* listener); + void Ignore(); + + private: + static int PkgmgrHandler(uid_t target_uid, int req_id, const char* pkg_type, + const char* pkgid, const char* key, const char* val, const void* pmsg, + void *user_data); + static int PkgmgrAppHandler(uid_t target_uid, int req_id, + const char* pkg_type, const char* pkgid, const char* appid, + const char* key, const char* val, const void* pmsg, void* user_data); + + private: + pkgmgr_client* handle_ = nullptr; + IEvent* listener_ = nullptr; +}; + +} // namespace dpm + +#endif // PKGMGR_EVENT_PKGMGR_CLIENT_HH_ diff --git a/src/pkgmgr_event_args.cc b/src/pkgmgr_event_args.cc new file mode 100644 index 0000000..9725d1b --- /dev/null +++ b/src/pkgmgr_event_args.cc @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2023 Samsung Electronics Co., Ltd All Rights Reserved + * + * 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 "pkgmgr_event_args.hh" + +namespace dpm { + +PkgmgrEventArgs::PkgmgrEventArgs(uid_t target_uid, int req_id, + std::string pkg_type, std::string pkgid, std::string event_status, + std::string event_name) + : target_uid_(target_uid), + req_id_(req_id), + pkg_type_(std::move(pkg_type)), + pkgid_(std::move(pkgid)), + event_status_(std::move(event_status)), + event_name_(std::move(event_name)) { + tag_ = std::to_string(target_uid) + "-" + pkgid_; +} + +uid_t PkgmgrEventArgs::GetTargetUid() const { + return target_uid_; +} + +int PkgmgrEventArgs::GetReqId() const { + return req_id_; +} + +const std::string& PkgmgrEventArgs::GetPkgType() const { + return pkg_type_; +} + +const std::string& PkgmgrEventArgs::GetPkgId() const { + return pkgid_; +} + +const std::string& PkgmgrEventArgs::GetEventStatus() const { + return event_status_; +} + +const std::string& PkgmgrEventArgs::GetEventName() const { + return event_name_; +} + +const std::string& PkgmgrEventArgs::GetTag() const { + return tag_; +} + +} // namespace dpm diff --git a/src/pkgmgr_event_args.hh b/src/pkgmgr_event_args.hh new file mode 100644 index 0000000..2920474 --- /dev/null +++ b/src/pkgmgr_event_args.hh @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2023 Samsung Electronics Co., Ltd All Rights Reserved + * + * 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. + */ + +#ifndef PKGMGR_EVENT_PKGMGR_EVENT_ARGS_HH_ +#define PKGMGR_EVENT_PKGMGR_EVENT_ARGS_HH_ + +#include +#include + +#include + +namespace dpm { + +class PkgmgrEventArgs { + public: + PkgmgrEventArgs(uid_t target_uid, int req_id, std::string pkg_type, + std::string pkgid, std::string event_status, std::string event_name); + virtual ~PkgmgrEventArgs() = default; + + uid_t GetTargetUid() const; + int GetReqId() const; + const std::string& GetPkgType() const; + const std::string& GetPkgId() const; + const std::string& GetEventStatus() const; + const std::string& GetEventName() const; + const std::string& GetTag() const; + + private: + uid_t target_uid_; + int req_id_; + std::string pkg_type_; + std::string pkgid_; + std::string event_status_; + std::string event_name_; + std::string tag_; +}; + +} // namespace dpm + +#endif // PKGMGR_EVENT_PKGMGR_EVENT_ARGS_HH_ diff --git a/src/service_common.c b/src/service_common.c deleted file mode 100644 index 7035207..0000000 --- a/src/service_common.c +++ /dev/null @@ -1,549 +0,0 @@ -/* - * Copyright 2016 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 -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "debug.h" -#include "pkgmgr.h" -#include "service_common.h" -#include "notification_service.h" -#include "badge_service.h" -#include "shortcut_service.h" - -#define DBUS_NAME "org.freedesktop.DBus" -#define DBUS_OBJECT_PATH "/org/freedesktop/DBus" -#define DBUS_INTERFACE_NAME "org.freedesktop.DBus" - -#define PROVIDER_BUS_NAME "org.tizen.data_provider_service" -#define PROVIDER_OBJECT_PATH "/org/tizen/data_provider_service" - -static GDBusConnection *_gdbus_conn; -static GHashTable *_noti_pkg_privilege_info; -static GHashTable *_badge_pkg_privilege_info; - -uid_t get_sender_uid(const char *sender_name) -{ - GDBusMessage *msg = NULL; - GDBusMessage *reply = NULL; - GError *err = NULL; - GVariant *body; - uid_t uid = 0; - - msg = g_dbus_message_new_method_call(DBUS_NAME, DBUS_OBJECT_PATH, - DBUS_INTERFACE_NAME, "GetConnectionUnixUser"); - if (!msg) { - LOGE("Failed to alloc new method call"); - goto out; - } - - g_dbus_message_set_body(msg, g_variant_new("(s)", sender_name)); - reply = g_dbus_connection_send_message_with_reply_sync(_gdbus_conn, msg, - G_DBUS_SEND_MESSAGE_FLAGS_NONE, -1, NULL, NULL, &err); - - if (!reply) { - if (err != NULL) { - LOGE("Failed to get uid [%s]", err->message); - g_error_free(err); - } - goto out; - } - - body = g_dbus_message_get_body(reply); - g_variant_get(body, "(u)", &uid); - -out: - if (msg) - g_object_unref(msg); - if (reply) - g_object_unref(reply); - - return uid; -} - -pid_t get_sender_pid(const char *sender_name) -{ - GDBusMessage *msg = NULL; - GDBusMessage *reply = NULL; - GError *err = NULL; - GVariant *body; - pid_t pid = 0; - - msg = g_dbus_message_new_method_call(DBUS_NAME, DBUS_OBJECT_PATH, - DBUS_INTERFACE_NAME, "GetConnectionUnixProcessID"); - if (!msg) { - LOGE("Failed to alloc new method call"); - goto out; - } - - g_dbus_message_set_body(msg, g_variant_new("(s)", sender_name)); - reply = g_dbus_connection_send_message_with_reply_sync(_gdbus_conn, msg, - G_DBUS_SEND_MESSAGE_FLAGS_NONE, -1, NULL, NULL, &err); - - if (!reply) { - if (err != NULL) { - LOGE("Failed to get uid [%s]", err->message); - g_error_free(err); - } - goto out; - } - - body = g_dbus_message_get_body(reply); - g_variant_get(body, "(u)", &pid); - -out: - if (msg) - g_object_unref(msg); - if (reply) - g_object_unref(reply); - - return pid; -} - -bool is_existed_busname(const char *sender_name) -{ - GDBusMessage *msg = NULL; - GDBusMessage *reply = NULL; - GError *err = NULL; - GVariant *body; - bool is_existed = false; - - msg = g_dbus_message_new_method_call(DBUS_NAME, DBUS_OBJECT_PATH, - DBUS_INTERFACE_NAME, "NameHasOwner"); - if (!msg) { - LOGE("Failed to alloc new method call"); - goto out; - } - - g_dbus_message_set_body(msg, g_variant_new("(s)", sender_name)); - reply = g_dbus_connection_send_message_with_reply_sync(_gdbus_conn, msg, - G_DBUS_SEND_MESSAGE_FLAGS_NONE, -1, NULL, NULL, &err); - - if (!reply) { - if (err != NULL) { - LOGE("Failed to get uid [%s]", err->message); - g_error_free(err); - } - goto out; - } - - body = g_dbus_message_get_body(reply); - g_variant_get(body, "(b)", &is_existed); - -out: - if (msg) - g_object_unref(msg); - if (reply) - g_object_unref(reply); - - return is_existed; -} - -int send_notify(GVariant *body, char *cmd, GHashTable **monitoring_hash, char *interface_name, uid_t uid) -{ - GError *err = NULL; - GList *monitoring_list = NULL; - GList *target_list; - char *target_bus_name; - int monitoring_count = 0; - bool is_existed = false; - - monitoring_list = (GList *)g_hash_table_lookup(*monitoring_hash, GUINT_TO_POINTER(uid)); - target_list = g_list_first(monitoring_list); - for (; target_list != NULL; ) { - err = NULL; - target_bus_name = target_list->data; - target_list = target_list->next; - - if (g_variant_is_floating(body)) - g_variant_ref(body); - - if (g_dbus_connection_emit_signal(_gdbus_conn, - target_bus_name, - PROVIDER_OBJECT_PATH, - interface_name, - cmd, - body, - &err) == FALSE) { - if (err != NULL) { - ERR("Emit signal err [%s]", err->message); - g_error_free(err); - } - is_existed = is_existed_busname(target_bus_name); - if (is_existed == false) - delete_monitoring_list(monitoring_hash, target_bus_name, uid); - ERR("Fail, emit signal to [%s]", target_bus_name); - } - monitoring_count++; - DBG("Success, emit signal to [%s]", target_bus_name); - } - - DBG("Success, cmd[%s] monitoring count[%d]", cmd, monitoring_count); - return SERVICE_COMMON_ERROR_NONE; -} - -int send_event_notify_by_busname(GVariant *body, char *cmd, char *busname, char *interface_name) -{ - GError *err = NULL; - - if (g_variant_is_floating(body)) - g_variant_ref(body); - - if (g_dbus_connection_emit_signal(_gdbus_conn, - busname, - PROVIDER_OBJECT_PATH, - interface_name, - cmd, - body, - &err) == FALSE) { - if (err != NULL) { - ERR("Emit signal err [%s]", - err->message); - g_error_free(err); - } - ERR("Failed to emit signal to [%s]", busname); - return SERVICE_COMMON_ERROR_IO_ERROR; - } - DBG("Success, Emit signal to [%s] cmd[%s]", busname, cmd); - return SERVICE_COMMON_ERROR_NONE; -} - -/* register service */ - -static int _monitoring_app_list_compare_cb(gconstpointer a, gconstpointer b) -{ - return strcmp(a, b); -} - -int service_register(GVariant *parameters, GVariant **reply_body, const gchar *sender, - GBusNameAppearedCallback name_appeared_handler, - GBusNameVanishedCallback name_vanished_handler, - GHashTable **monitoring_hash, - uid_t uid) -{ - GList *added_list = NULL; - const char *bus_name = sender; - monitoring_info_s *m_info = NULL; - uid_t request_uid = 0; - GList *monitoring_list = NULL; - - if (sender == NULL) - return SERVICE_COMMON_ERROR_IO_ERROR; - - g_variant_get(parameters, "(i)", &request_uid); - if (uid > NORMAL_UID_BASE && uid != request_uid) - return SERVICE_COMMON_ERROR_IO_ERROR; - - DBG("service_register : uid %d , request_uid %d", uid, request_uid); - monitoring_list = (GList *)g_hash_table_lookup(*monitoring_hash, GUINT_TO_POINTER(request_uid)); - added_list = g_list_find_custom(monitoring_list, bus_name, - (GCompareFunc)_monitoring_app_list_compare_cb); - - if (added_list == NULL) { - DBG("add new sender to list"); - m_info = (monitoring_info_s *)calloc(1, sizeof(monitoring_info_s)); - if (m_info == NULL) { - ERR("Failed to alloc memory"); - return SERVICE_COMMON_ERROR_OUT_OF_MEMORY; - } - - m_info->bus_name = strdup(bus_name); - m_info->uid = request_uid; - m_info->watcher_id = g_bus_watch_name_on_connection( - _gdbus_conn, - bus_name, - G_BUS_NAME_WATCHER_FLAGS_NONE, - name_appeared_handler, - name_vanished_handler, - m_info, - NULL); - if (m_info->watcher_id == 0) { - ERR("Fail to watch name [%s]", bus_name); - free(m_info->bus_name); - free(m_info); - return SERVICE_COMMON_ERROR_IO_ERROR; - } - DBG("Watch on [%s] success", bus_name); - - monitoring_list = g_list_append(monitoring_list, strdup(bus_name)); - DBG("Success, sender[%s] length[%d]", - sender, g_list_length(monitoring_list)); - if (g_hash_table_lookup(*monitoring_hash, GUINT_TO_POINTER(request_uid)) == NULL) - g_hash_table_insert(*monitoring_hash, GUINT_TO_POINTER(request_uid), monitoring_list); - } else { - ERR("Sender [%s] already exist", sender); - } - - *reply_body = g_variant_new("()"); - if (*reply_body == NULL) { - if (m_info) { - if (m_info->bus_name) - free(m_info->bus_name); - free(m_info); - } - monitoring_list = g_list_remove(monitoring_list, bus_name); - ERR("Failed to make reply"); - return SERVICE_COMMON_ERROR_OUT_OF_MEMORY; - } - return SERVICE_COMMON_ERROR_NONE; -} - -int delete_monitoring_list(GHashTable **monitoring_hash, const char *sender, uid_t uid) -{ - GList *monitoring_list = NULL; - GList *del_list = NULL; - char *bus_name; - - monitoring_list = (GList *)g_hash_table_lookup(*monitoring_hash, GUINT_TO_POINTER(uid)); - if (monitoring_list == NULL) { - ERR("No uid[%d] in monitoring hash", uid); - return SERVICE_COMMON_ERROR_IO_ERROR; - } - - monitoring_list = g_list_first(monitoring_list); - del_list = g_list_find_custom(monitoring_list, sender, - (GCompareFunc)_monitoring_app_list_compare_cb); - - if (del_list) { - DBG("Find delete list - uid[%d] sender[%s]", uid, sender); - bus_name = g_list_nth_data(del_list, 0); - if (bus_name) - free(bus_name); - monitoring_list = g_list_delete_link(monitoring_list, del_list); - - if (monitoring_list == NULL) { - g_hash_table_steal(*monitoring_hash, GUINT_TO_POINTER(uid)); - } else { - monitoring_list = g_list_first(monitoring_list); - g_hash_table_replace(*monitoring_hash, GUINT_TO_POINTER(uid), monitoring_list); - } - } - return SERVICE_COMMON_ERROR_NONE; -} - -static int _dbus_init(void) -{ - GError *error = NULL; - - if (_gdbus_conn == NULL) { - _gdbus_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error); - if (_gdbus_conn == NULL) { - if (error != NULL) { - ERR("Failed to get dbus [%s]", error->message); - g_error_free(error); - } - return SERVICE_COMMON_ERROR_IO_ERROR; - } - } - - return SERVICE_COMMON_ERROR_NONE; -} - -int service_common_register_dbus_interface(char *introspection_xml, GDBusInterfaceVTable interface_vtable) -{ - int result; - int owner_id, noti_registration_id; - GError *error = NULL; - GDBusNodeInfo *introspection_data = NULL; - - result = _dbus_init(); - if (result != SERVICE_COMMON_ERROR_NONE) { - ERR("Can't init dbus [%d]", result); - result = SERVICE_COMMON_ERROR_IO_ERROR; - goto out; - } - - owner_id = g_bus_own_name(G_BUS_TYPE_SYSTEM, - PROVIDER_BUS_NAME, - G_BUS_NAME_OWNER_FLAGS_NONE, - NULL, - NULL, - NULL, - NULL, NULL); - if (!owner_id) { - ERR("Failed to own name"); - result = SERVICE_COMMON_ERROR_IO_ERROR; - goto out; - } - - DBG("Acquiring the own name [%d]", owner_id); - introspection_data = g_dbus_node_info_new_for_xml(introspection_xml, &error); - if (!introspection_data) { - ERR("g_dbus_node_info_new_for_xml is failed."); - result = SERVICE_COMMON_ERROR_IO_ERROR; - if (error != NULL) { - ERR("g_dbus_node_info_new_for_xml err [%s]", error->message); - g_error_free(error); - } - goto out; - } - - noti_registration_id = g_dbus_connection_register_object(_gdbus_conn, - PROVIDER_OBJECT_PATH, introspection_data->interfaces[0], - &interface_vtable, NULL, NULL, NULL); - - DBG("registration id[%d]", noti_registration_id); - if (noti_registration_id == 0) { - ERR("Failed to g_dbus_connection_register_object"); - result = SERVICE_COMMON_ERROR_IO_ERROR; - goto out; - } - -out: - if (introspection_data) - g_dbus_node_info_unref(introspection_data); - - return result; -} - -static int _init_pkg_privilege_info() { - - if (_noti_pkg_privilege_info != NULL) - return 0; - - _noti_pkg_privilege_info = - g_hash_table_new_full(g_str_hash, g_str_equal, free, NULL); - _badge_pkg_privilege_info = - g_hash_table_new_full(g_str_hash, g_str_equal, free, NULL); - DBG("init pkg privilege info done"); - return 0; -} - -static int _package_install_cb(uid_t uid, const char *pkgname, enum pkgmgr_status status, double value, void *data) -{ - int ret; - gpointer tmp; - int privilege_info; - - if (status != PKGMGR_STATUS_END) - return 0; - - if (uid == tzplatform_getuid(TZ_SYS_GLOBALAPP_USER)) - uid = tzplatform_getuid(TZ_SYS_DEFAULT_USER); - - _init_pkg_privilege_info(); - if (g_hash_table_contains(_noti_pkg_privilege_info, pkgname)) { - tmp = g_hash_table_lookup(_noti_pkg_privilege_info, pkgname); - privilege_info = GPOINTER_TO_UINT(tmp); - if (privilege_info == 1) - notification_setting_db_update_pkg_disabled(pkgname, false, uid); - - g_hash_table_remove(_noti_pkg_privilege_info, pkgname); - } else { - /* In consideration of the reboot status, change the disable information. */ - ret = notification_setting_db_update_pkg_disabled(pkgname, false, uid); - if (ret != NOTIFICATION_ERROR_NONE) - notification_setting_insert_package_for_uid(pkgname, uid); - } - - if (g_hash_table_contains(_badge_pkg_privilege_info, pkgname)) { - tmp = g_hash_table_lookup(_badge_pkg_privilege_info, pkgname); - privilege_info = GPOINTER_TO_UINT(tmp); - if (privilege_info == 1) - badge_db_update_pkg_disabled(pkgname, false, uid); - - g_hash_table_remove(_badge_pkg_privilege_info, pkgname); - } else { - /* In consideration of the reboot status, change the disable information. */ - ret = badge_db_update_pkg_disabled(pkgname, false, uid); - if (ret != BADGE_ERROR_NONE) - badge_setting_insert_package_for_uid(pkgname, uid); - } - - return 0; -} - -static int _package_uninstall_cb(uid_t uid, const char *pkgname, enum pkgmgr_status status, double value, void *data) -{ - int ret; - pkgmgrinfo_pkginfo_h pkginfo; - - if (status != PKGMGR_STATUS_END) - return 0; - - if (uid == tzplatform_getuid(TZ_SYS_GLOBALAPP_USER)) - uid = tzplatform_getuid(TZ_SYS_DEFAULT_USER); - - ret = pkgmgrinfo_pkginfo_get_usr_disabled_pkginfo(pkgname, uid, &pkginfo); - if (ret == PMINFO_R_OK) { - pkgmgrinfo_pkginfo_destroy_pkginfo(pkginfo); - - _init_pkg_privilege_info(); - ret = notification_setting_db_update_pkg_disabled(pkgname, true, uid); - if (ret == NOTIFICATION_ERROR_NONE) - g_hash_table_insert(_noti_pkg_privilege_info, strdup(pkgname), GUINT_TO_POINTER(1)); - else - g_hash_table_insert(_noti_pkg_privilege_info, strdup(pkgname), GUINT_TO_POINTER(0)); - - ret = badge_db_update_pkg_disabled(pkgname, true, uid); - if (ret == BADGE_ERROR_NONE) - g_hash_table_insert(_badge_pkg_privilege_info, strdup(pkgname), GUINT_TO_POINTER(1)); - else - g_hash_table_insert(_badge_pkg_privilege_info, strdup(pkgname), GUINT_TO_POINTER(0)); - } else { - notification_setting_delete_package_for_uid(pkgname, uid); - badge_db_delete_by_pkgname(pkgname, uid); - badge_setting_delete_package_for_uid(pkgname, uid); - notification_noti_delete_template(pkgname); - } - - return 0; -} - -static int _app_enabled_cb(uid_t uid, const char *app_id, enum pkgmgr_status status, double value, void *data) -{ - if (status == PKGMGR_STATUS_END) - notification_setting_db_update_app_disabled(app_id, false, uid); - - return 0; -} - -static int _app_disabled_cb(uid_t uid, const char *app_id, enum pkgmgr_status status, double value, void *data) -{ - if (status == PKGMGR_STATUS_END) { - notification_delete_noti_by_app_id(app_id, uid); - notification_setting_db_update_app_disabled(app_id, true, uid); - } - - return 0; -} - -void service_common_init(void) -{ - pkgmgr_init(); - pkgmgr_add_event_callback(PKGMGR_EVENT_INSTALL, _package_install_cb, NULL); - pkgmgr_add_event_callback(PKGMGR_EVENT_UPDATE, _package_install_cb, NULL); - pkgmgr_add_event_callback(PKGMGR_EVENT_UNINSTALL, _package_uninstall_cb, NULL); - pkgmgr_add_event_callback(PKGMGR_EVENT_APP_ENABLE, _app_enabled_cb, NULL); - pkgmgr_add_event_callback(PKGMGR_EVENT_APP_DISABLE, _app_disabled_cb, NULL); -} - -void service_common_set_connection(GDBusConnection *conn) -{ - _gdbus_conn = conn; -} \ No newline at end of file diff --git a/src/service_common.cc b/src/service_common.cc new file mode 100644 index 0000000..529ae5b --- /dev/null +++ b/src/service_common.cc @@ -0,0 +1,620 @@ +/* +* Copyright 2023 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 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include "debug.h" +#include "pkgmgr_client.hh" +#include "service_common.h" +#include "notification_service.h" +#include "badge_service.h" +#include "shortcut_service.h" + +#define DBUS_NAME "org.freedesktop.DBus" +#define DBUS_OBJECT_PATH "/org/freedesktop/DBus" +#define DBUS_INTERFACE_NAME "org.freedesktop.DBus" + +#define PROVIDER_BUS_NAME "org.tizen.data_provider_service" +#define PROVIDER_OBJECT_PATH "/org/tizen/data_provider_service" + +using namespace dpm; + +static GDBusConnection *_gdbus_conn; +static GHashTable *_noti_pkg_privilege_info; +static GHashTable *_badge_pkg_privilege_info; +std::unique_ptr pkgmgr_client_; +std::list> pkgmgr_event_list_; +std::list> pkgmgr_app_event_list_; + +const std::set pkgmgr_installer_key_list_ = { + "install_percent", + "start", + "end" +}; + +const std::set pkgmgr_installer_val_list_ = { + "install", + "uninstall", + "update", + "ebable_app", + "disable_app" +}; + +static int _package_install_cb(uid_t uid, const char *pkgname); +static int _package_uninstall_cb(uid_t uid, const char *pkgname); +static int _app_enabled_cb(uid_t uid, const char *app_id); +static int _app_disabled_cb(uid_t uid, const char *app_id); + +class PackageEventListener : public PkgmgrClient::IEvent { +public: + void OnPkgmgrEvent(std::shared_ptr args) override; + void OnPkgmgrAppEvent(std::shared_ptr args) override; +}; + +void PackageEventListener::OnPkgmgrEvent(std::shared_ptr args) { + if (pkgmgr_installer_key_list_.find(args->GetEventStatus()) == + pkgmgr_installer_key_list_.end()) + return; + + if (args->GetEventStatus().compare(std::string("start")) == 0 && + pkgmgr_installer_val_list_.find(args->GetEventName()) != + pkgmgr_installer_val_list_.end()) { + pkgmgr_event_list_.push_back(args); + return; + } + + if (args->GetEventName().compare(std::string("ok")) == 0) { + for (auto& a : pkgmgr_event_list_) { + if (a->GetTargetUid() == args->GetTargetUid() && + a->GetPkgId().compare(args->GetPkgId()) == 0) { + if (a->GetEventName().compare(std::string("install")) == 0) + _package_install_cb(args->GetTargetUid(), args->GetPkgId().c_str()); + else if (a->GetEventName().compare(std::string("uninstall")) == 0) + _package_uninstall_cb(args->GetTargetUid(), args->GetPkgId().c_str()); + else if (a->GetEventName().compare(std::string("enable_app")) == 0) + _app_enabled_cb(args->GetTargetUid(), args->GetPkgId().c_str()); + else if (a->GetEventName().compare(std::string("disable_app")) == 0) + _app_disabled_cb(args->GetTargetUid(), args->GetPkgId().c_str()); + } + } + } +} + +void PackageEventListener::OnPkgmgrAppEvent(std::shared_ptr args) { + if (pkgmgr_installer_key_list_.find(args->GetEventStatus()) == + pkgmgr_installer_key_list_.end()) + return; + + if (args->GetEventStatus().compare(std::string("start")) == 0 && + pkgmgr_installer_val_list_.find(args->GetEventName()) != + pkgmgr_installer_val_list_.end()) { + pkgmgr_app_event_list_.push_back(args); + return; + } + + if (args->GetEventName().compare(std::string("ok")) == 0) { + for (auto& a : pkgmgr_app_event_list_) { + if (a->GetTargetUid() == args->GetTargetUid() && + a->GetPkgId().compare(args->GetPkgId()) == 0) { + if (a->GetEventName().compare(std::string("install")) == 0) + _package_install_cb(args->GetTargetUid(), args->GetPkgId().c_str()); + else if (a->GetEventName().compare(std::string("uninstall")) == 0) + _package_uninstall_cb(args->GetTargetUid(), args->GetPkgId().c_str()); + else if (a->GetEventName().compare(std::string("enable_app")) == 0) + _app_enabled_cb(args->GetTargetUid(), args->GetPkgId().c_str()); + else if (a->GetEventName().compare(std::string("disable_app")) == 0) + _app_disabled_cb(args->GetTargetUid(), args->GetPkgId().c_str()); + } + } + } +} + +uid_t get_sender_uid(const char *sender_name) { + GDBusMessage *msg = nullptr; + GDBusMessage *reply = nullptr; + GError *err = nullptr; + GVariant *body; + uid_t uid = 0; + + msg = g_dbus_message_new_method_call(DBUS_NAME, DBUS_OBJECT_PATH, + DBUS_INTERFACE_NAME, "GetConnectionUnixUser"); + if (!msg) { + LOGE("Failed to alloc new method call"); + goto out; + } + + g_dbus_message_set_body(msg, g_variant_new("(s)", sender_name)); + reply = g_dbus_connection_send_message_with_reply_sync(_gdbus_conn, msg, + G_DBUS_SEND_MESSAGE_FLAGS_NONE, -1, nullptr, nullptr, &err); + + if (!reply) { + if (err != nullptr) { + LOGE("Failed to get uid [%s]", err->message); + g_error_free(err); + } + goto out; + } + + body = g_dbus_message_get_body(reply); + g_variant_get(body, "(u)", &uid); + +out: + if (msg) + g_object_unref(msg); + if (reply) + g_object_unref(reply); + + return uid; +} + +pid_t get_sender_pid(const char *sender_name) { + GDBusMessage *msg = nullptr; + GDBusMessage *reply = nullptr; + GError *err = nullptr; + GVariant *body; + pid_t pid = 0; + + msg = g_dbus_message_new_method_call(DBUS_NAME, DBUS_OBJECT_PATH, + DBUS_INTERFACE_NAME, "GetConnectionUnixProcessID"); + if (!msg) { + LOGE("Failed to alloc new method call"); + goto out; + } + + g_dbus_message_set_body(msg, g_variant_new("(s)", sender_name)); + reply = g_dbus_connection_send_message_with_reply_sync(_gdbus_conn, msg, + G_DBUS_SEND_MESSAGE_FLAGS_NONE, -1, nullptr, nullptr, &err); + + if (!reply) { + if (err != nullptr) { + LOGE("Failed to get uid [%s]", err->message); + g_error_free(err); + } + goto out; + } + + body = g_dbus_message_get_body(reply); + g_variant_get(body, "(u)", &pid); + +out: + if (msg) + g_object_unref(msg); + if (reply) + g_object_unref(reply); + + return pid; +} + +bool is_existed_busname(const char *sender_name) { + GDBusMessage *msg = nullptr; + GDBusMessage *reply = nullptr; + GError *err = nullptr; + GVariant *body; + bool is_existed = false; + + msg = g_dbus_message_new_method_call(DBUS_NAME, DBUS_OBJECT_PATH, + DBUS_INTERFACE_NAME, "NameHasOwner"); + if (!msg) { + LOGE("Failed to alloc new method call"); + goto out; + } + + g_dbus_message_set_body(msg, g_variant_new("(s)", sender_name)); + reply = g_dbus_connection_send_message_with_reply_sync(_gdbus_conn, msg, + G_DBUS_SEND_MESSAGE_FLAGS_NONE, -1, nullptr, nullptr, &err); + + if (!reply) { + if (err != nullptr) { + LOGE("Failed to get uid [%s]", err->message); + g_error_free(err); + } + goto out; + } + + body = g_dbus_message_get_body(reply); + g_variant_get(body, "(b)", &is_existed); + +out: + if (msg) + g_object_unref(msg); + if (reply) + g_object_unref(reply); + + return is_existed; +} + +int send_notify(GVariant *body, char *cmd, GHashTable **monitoring_hash, + char *interface_name, uid_t uid) { + GError *err = nullptr; + GList *monitoring_list = nullptr; + GList *target_list; + char *target_bus_name; + int monitoring_count = 0; + bool is_existed = false; + + monitoring_list = (GList *)g_hash_table_lookup(*monitoring_hash, GUINT_TO_POINTER(uid)); + target_list = g_list_first(monitoring_list); + for (; target_list != nullptr; ) { + err = nullptr; + target_bus_name = static_cast(target_list->data); + target_list = target_list->next; + + if (g_variant_is_floating(body)) + g_variant_ref(body); + + if (g_dbus_connection_emit_signal(_gdbus_conn, + target_bus_name, + PROVIDER_OBJECT_PATH, + interface_name, + cmd, + body, + &err) == FALSE) { + if (err != nullptr) { + ERR("Emit signal err [%s]", err->message); + g_error_free(err); + } + is_existed = is_existed_busname(target_bus_name); + if (is_existed == false) + delete_monitoring_list(monitoring_hash, target_bus_name, uid); + ERR("Fail, emit signal to [%s]", target_bus_name); + } + monitoring_count++; + DBG("Success, emit signal to [%s]", target_bus_name); + } + + DBG("Success, cmd[%s] monitoring count[%d]", cmd, monitoring_count); + return SERVICE_COMMON_ERROR_NONE; +} + +int send_event_notify_by_busname(GVariant *body, char *cmd, char *busname, + char *interface_name) { + GError *err = nullptr; + + if (g_variant_is_floating(body)) + g_variant_ref(body); + + if (g_dbus_connection_emit_signal(_gdbus_conn, + busname, + PROVIDER_OBJECT_PATH, + interface_name, + cmd, + body, + &err) == FALSE) { + if (err != nullptr) { + ERR("Emit signal err [%s]", + err->message); + g_error_free(err); + } + ERR("Failed to emit signal to [%s]", busname); + return SERVICE_COMMON_ERROR_IO_ERROR; + } + DBG("Success, Emit signal to [%s] cmd[%s]", busname, cmd); + return SERVICE_COMMON_ERROR_NONE; +} + +/* register service */ + +static int _monitoring_app_list_compare_cb(gconstpointer a, gconstpointer b) { + return strcmp(static_cast(a), reinterpret_cast(b)); +} + +int service_register(GVariant *parameters, GVariant **reply_body, const gchar *sender, + GBusNameAppearedCallback name_appeared_handler, + GBusNameVanishedCallback name_vanished_handler, + GHashTable **monitoring_hash, + uid_t uid) { + GList *added_list = nullptr; + const char *bus_name = sender; + monitoring_info_s *m_info = nullptr; + uid_t request_uid = 0; + GList *monitoring_list = nullptr; + + if (sender == nullptr) + return SERVICE_COMMON_ERROR_IO_ERROR; + + g_variant_get(parameters, "(i)", &request_uid); + if (uid > NORMAL_UID_BASE && uid != request_uid) + return SERVICE_COMMON_ERROR_IO_ERROR; + + DBG("service_register : uid %d , request_uid %d", uid, request_uid); + monitoring_list = (GList *)g_hash_table_lookup(*monitoring_hash, GUINT_TO_POINTER(request_uid)); + added_list = g_list_find_custom(monitoring_list, bus_name, + (GCompareFunc)_monitoring_app_list_compare_cb); + + if (added_list == nullptr) { + DBG("add new sender to list"); + m_info = (monitoring_info_s *)calloc(1, sizeof(monitoring_info_s)); + if (m_info == nullptr) { + ERR("Failed to alloc memory"); + return SERVICE_COMMON_ERROR_OUT_OF_MEMORY; + } + + m_info->bus_name = strdup(bus_name); + m_info->uid = request_uid; + m_info->watcher_id = g_bus_watch_name_on_connection( + _gdbus_conn, + bus_name, + G_BUS_NAME_WATCHER_FLAGS_NONE, + name_appeared_handler, + name_vanished_handler, + m_info, + nullptr); + if (m_info->watcher_id == 0) { + ERR("Fail to watch name [%s]", bus_name); + free(m_info->bus_name); + free(m_info); + return SERVICE_COMMON_ERROR_IO_ERROR; + } + DBG("Watch on [%s] success", bus_name); + + monitoring_list = g_list_append(monitoring_list, strdup(bus_name)); + DBG("Success, sender[%s] length[%d]", + sender, g_list_length(monitoring_list)); + if (g_hash_table_lookup(*monitoring_hash, GUINT_TO_POINTER(request_uid)) == nullptr) + g_hash_table_insert(*monitoring_hash, GUINT_TO_POINTER(request_uid), monitoring_list); + } else { + ERR("Sender [%s] already exist", sender); + } + + *reply_body = g_variant_new("()"); + if (*reply_body == nullptr) { + if (m_info) { + if (m_info->bus_name) + free(m_info->bus_name); + free(m_info); + } + monitoring_list = g_list_remove(monitoring_list, bus_name); + ERR("Failed to make reply"); + return SERVICE_COMMON_ERROR_OUT_OF_MEMORY; + } + return SERVICE_COMMON_ERROR_NONE; +} + +int delete_monitoring_list(GHashTable **monitoring_hash, const char *sender, + uid_t uid) { + GList *monitoring_list = nullptr; + GList *del_list = nullptr; + char *bus_name; + + monitoring_list = (GList *)g_hash_table_lookup(*monitoring_hash, GUINT_TO_POINTER(uid)); + if (monitoring_list == nullptr) { + ERR("No uid[%d] in monitoring hash", uid); + return SERVICE_COMMON_ERROR_IO_ERROR; + } + + monitoring_list = g_list_first(monitoring_list); + del_list = g_list_find_custom(monitoring_list, sender, + (GCompareFunc)_monitoring_app_list_compare_cb); + + if (del_list) { + DBG("Find delete list - uid[%d] sender[%s]", uid, sender); + bus_name = static_cast(g_list_nth_data(del_list, 0)); + if (bus_name) + free(bus_name); + monitoring_list = g_list_delete_link(monitoring_list, del_list); + + if (monitoring_list == nullptr) { + g_hash_table_steal(*monitoring_hash, GUINT_TO_POINTER(uid)); + } else { + monitoring_list = g_list_first(monitoring_list); + g_hash_table_replace(*monitoring_hash, GUINT_TO_POINTER(uid), monitoring_list); + } + } + return SERVICE_COMMON_ERROR_NONE; +} + +static int _dbus_init(void) { + GError *error = nullptr; + + if (_gdbus_conn == nullptr) { + _gdbus_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, nullptr, &error); + if (_gdbus_conn == nullptr) { + if (error != nullptr) { + ERR("Failed to get dbus [%s]", error->message); + g_error_free(error); + } + return SERVICE_COMMON_ERROR_IO_ERROR; + } + } + + return SERVICE_COMMON_ERROR_NONE; +} + +int service_common_register_dbus_interface(char *introspection_xml, + GDBusInterfaceVTable interface_vtable) { + int result; + int owner_id, noti_registration_id; + GError *error = nullptr; + GDBusNodeInfo *introspection_data = nullptr; + + result = _dbus_init(); + if (result != SERVICE_COMMON_ERROR_NONE) { + ERR("Can't init dbus [%d]", result); + result = SERVICE_COMMON_ERROR_IO_ERROR; + goto out; + } + + owner_id = g_bus_own_name(G_BUS_TYPE_SYSTEM, + PROVIDER_BUS_NAME, + G_BUS_NAME_OWNER_FLAGS_NONE, + nullptr, + nullptr, + nullptr, + nullptr, nullptr); + if (!owner_id) { + ERR("Failed to own name"); + result = SERVICE_COMMON_ERROR_IO_ERROR; + goto out; + } + + DBG("Acquiring the own name [%d]", owner_id); + introspection_data = g_dbus_node_info_new_for_xml(introspection_xml, &error); + if (!introspection_data) { + ERR("g_dbus_node_info_new_for_xml is failed."); + result = SERVICE_COMMON_ERROR_IO_ERROR; + if (error != nullptr) { + ERR("g_dbus_node_info_new_for_xml err [%s]", error->message); + g_error_free(error); + } + goto out; + } + + noti_registration_id = g_dbus_connection_register_object(_gdbus_conn, + PROVIDER_OBJECT_PATH, introspection_data->interfaces[0], + &interface_vtable, nullptr, nullptr, nullptr); + + DBG("registration id[%d]", noti_registration_id); + if (noti_registration_id == 0) { + ERR("Failed to g_dbus_connection_register_object"); + result = SERVICE_COMMON_ERROR_IO_ERROR; + goto out; + } + +out: + if (introspection_data) + g_dbus_node_info_unref(introspection_data); + + return result; +} + +static int _init_pkg_privilege_info() { + if (_noti_pkg_privilege_info != nullptr) + return 0; + + _noti_pkg_privilege_info = + g_hash_table_new_full(g_str_hash, g_str_equal, free, nullptr); + _badge_pkg_privilege_info = + g_hash_table_new_full(g_str_hash, g_str_equal, free, nullptr); + DBG("init pkg privilege info done"); + return 0; +} + +static int _package_install_cb(uid_t uid, const char *pkgname) { + int ret; + gpointer tmp; + int privilege_info; + + if (uid == tzplatform_getuid(TZ_SYS_GLOBALAPP_USER)) + uid = tzplatform_getuid(TZ_SYS_DEFAULT_USER); + + _init_pkg_privilege_info(); + if (g_hash_table_contains(_noti_pkg_privilege_info, pkgname)) { + tmp = g_hash_table_lookup(_noti_pkg_privilege_info, pkgname); + privilege_info = GPOINTER_TO_UINT(tmp); + if (privilege_info == 1) + notification_setting_db_update_pkg_disabled(pkgname, false, uid); + + g_hash_table_remove(_noti_pkg_privilege_info, pkgname); + } else { + /* In consideration of the reboot status, change the disable information. */ + ret = notification_setting_db_update_pkg_disabled(pkgname, false, uid); + if (ret != NOTIFICATION_ERROR_NONE) + notification_setting_insert_package_for_uid(pkgname, uid); + } + + if (g_hash_table_contains(_badge_pkg_privilege_info, pkgname)) { + tmp = g_hash_table_lookup(_badge_pkg_privilege_info, pkgname); + privilege_info = GPOINTER_TO_UINT(tmp); + if (privilege_info == 1) + badge_db_update_pkg_disabled(pkgname, false, uid); + + g_hash_table_remove(_badge_pkg_privilege_info, pkgname); + } else { + /* In consideration of the reboot status, change the disable information. */ + ret = badge_db_update_pkg_disabled(pkgname, false, uid); + if (ret != BADGE_ERROR_NONE) + badge_setting_insert_package_for_uid(pkgname, uid); + } + + return 0; +} + +static int _package_uninstall_cb(uid_t uid, const char *pkgname) { + int ret; + pkgmgrinfo_pkginfo_h pkginfo; + + if (uid == tzplatform_getuid(TZ_SYS_GLOBALAPP_USER)) + uid = tzplatform_getuid(TZ_SYS_DEFAULT_USER); + + ret = pkgmgrinfo_pkginfo_get_usr_disabled_pkginfo(pkgname, uid, &pkginfo); + if (ret == PMINFO_R_OK) { + pkgmgrinfo_pkginfo_destroy_pkginfo(pkginfo); + + _init_pkg_privilege_info(); + ret = notification_setting_db_update_pkg_disabled(pkgname, true, uid); + if (ret == NOTIFICATION_ERROR_NONE) + g_hash_table_insert(_noti_pkg_privilege_info, strdup(pkgname), GUINT_TO_POINTER(1)); + else + g_hash_table_insert(_noti_pkg_privilege_info, strdup(pkgname), GUINT_TO_POINTER(0)); + + ret = badge_db_update_pkg_disabled(pkgname, true, uid); + if (ret == BADGE_ERROR_NONE) + g_hash_table_insert(_badge_pkg_privilege_info, strdup(pkgname), GUINT_TO_POINTER(1)); + else + g_hash_table_insert(_badge_pkg_privilege_info, strdup(pkgname), GUINT_TO_POINTER(0)); + } else { + notification_setting_delete_package_for_uid(pkgname, uid); + badge_db_delete_by_pkgname(pkgname, uid); + badge_setting_delete_package_for_uid(pkgname, uid); + notification_noti_delete_template(pkgname); + } + + return 0; +} + +static int _app_enabled_cb(uid_t uid, const char *app_id) { + notification_setting_db_update_app_disabled(app_id, false, uid); + return 0; +} + +static int _app_disabled_cb(uid_t uid, const char *app_id) { + notification_delete_noti_by_app_id(app_id, uid); + notification_setting_db_update_app_disabled(app_id, true, uid); + return 0; +} + +std::unique_ptr listener_; + +void service_common_init(void) { + pkgmgr_client_ = std::make_unique(); + listener_ = std::make_unique(); + pkgmgr_client_->Listen(listener_.get()); +} + +void service_common_set_connection(GDBusConnection *conn) { + _gdbus_conn = conn; +} \ No newline at end of file diff --git a/tests/unit_tests/CMakeLists.txt b/tests/unit_tests/CMakeLists.txt index 22543ac..3df4016 100644 --- a/tests/unit_tests/CMakeLists.txt +++ b/tests/unit_tests/CMakeLists.txt @@ -42,9 +42,11 @@ AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR}/../mock MOCK_SOURCES) SET(DPM_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/../../src/badge_service.c ${CMAKE_CURRENT_SOURCE_DIR}/../../src/notification_ex_service.cc ${CMAKE_CURRENT_SOURCE_DIR}/../../src/notification_service.c - ${CMAKE_CURRENT_SOURCE_DIR}/../../src/pkgmgr.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../src/pkgmgr_client.cc + ${CMAKE_CURRENT_SOURCE_DIR}/../../src/pkgmgr_event_args.cc + ${CMAKE_CURRENT_SOURCE_DIR}/../../src/pkgmgr_app_event_args.cc ${CMAKE_CURRENT_SOURCE_DIR}/../../src/shortcut_service.c - ${CMAKE_CURRENT_SOURCE_DIR}/../../src/service_common.c) + ${CMAKE_CURRENT_SOURCE_DIR}/../../src/service_common.cc) ADD_EXECUTABLE(${PROJECT_NAME} ${TEST_SOURCES} -- 2.7.4 From fdac1e9d65b790904105d9893af9204495205e28 Mon Sep 17 00:00:00 2001 From: SukhyungKang Date: Thu, 17 Aug 2023 14:24:53 +0900 Subject: [PATCH 04/16] change to use reference Change-Id: I8d0418f34ad301c8a51996ccd0c6022aa19388e1 Signed-off-by: SukhyungKang --- src/notification_ex_service.cc | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/notification_ex_service.cc b/src/notification_ex_service.cc index a7b00fa..4c7eb80 100644 --- a/src/notification_ex_service.cc +++ b/src/notification_ex_service.cc @@ -498,8 +498,7 @@ class DPMReporter : public Reporter { std::string receiver_group = info.GetChannel(); std::string appid = info.GetOwner(); - std::multimap::iterator it; - for(auto receiver_group_ : facade_->receiver_group_list_) { + for(auto& receiver_group_ : facade_->receiver_group_list_) { if (receiver_group_.first.compare(receiver_group) == 0 && receiver_group_.second.compare(appid) == 0) return; -- 2.7.4 From 81dee5fbc5ced72f570499d588665c5ab5789bf6 Mon Sep 17 00:00:00 2001 From: SukhyungKang Date: Thu, 17 Aug 2023 15:17:03 +0900 Subject: [PATCH 05/16] Release version 1.6.28 Changes: - Add gmock tests - Trivial updates - Improve coverage - Refactor legacy C code for pkgmgr_event - change to use reference Change-Id: I6af6de0d424f5382480a0e4da8eeb1220a3eb253 Signed-off-by: SukhyungKang --- packaging/data-provider-master.spec | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packaging/data-provider-master.spec b/packaging/data-provider-master.spec index f7b719a..1d8484d 100644 --- a/packaging/data-provider-master.spec +++ b/packaging/data-provider-master.spec @@ -2,7 +2,7 @@ Name: data-provider-master Summary: Master service provider for badge, shortcut, notification -Version: 1.6.27 +Version: 1.6.28 Release: 1 Group: Applications/Core Applications License: Apache-2.0 @@ -166,4 +166,4 @@ install -m 0755 run-unittest.sh %{buildroot}%{_bindir}/tizen-unittests/%{name}/ %{_datadir}/gcov/obj/* %endif -# End of a file \ No newline at end of file +# End of a file -- 2.7.4 From c9f94beb253a91106b7197de481770b3753927f3 Mon Sep 17 00:00:00 2001 From: SukhyungKang Date: Thu, 9 Nov 2023 17:06:57 +0900 Subject: [PATCH 06/16] Run script to upgrade notification db Change-Id: If3008c154f9fd7111cae80f889037473816a8c0c Signed-off-by: SukhyungKang --- packaging/data-provider-master.service | 1 + 1 file changed, 1 insertion(+) diff --git a/packaging/data-provider-master.service b/packaging/data-provider-master.service index c5aa30e..d2103a5 100644 --- a/packaging/data-provider-master.service +++ b/packaging/data-provider-master.service @@ -9,6 +9,7 @@ Type=dbus SmackProcessLabel=System Capabilities=cap_dac_override=i SecureBits=keep-caps +ExecStartPre=/usr/bin/bash -c /etc/notification/upgrade.sh ExecStart=/usr/bin/data-provider-master TimeoutStopSec=3s Restart=on-failure -- 2.7.4 From abd2b450bd50ecf4a43b2755deb4828b269f90d2 Mon Sep 17 00:00:00 2001 From: SukhyungKang Date: Mon, 13 Nov 2023 11:15:50 +0900 Subject: [PATCH 07/16] Release version 1.6.29 Changes: - Run script to upgrade notification db Change-Id: Ib63e7a258be88f06ed4cac401c086ea2768626e0 Signed-off-by: SukhyungKang --- packaging/data-provider-master.spec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packaging/data-provider-master.spec b/packaging/data-provider-master.spec index 1d8484d..1a68490 100644 --- a/packaging/data-provider-master.spec +++ b/packaging/data-provider-master.spec @@ -2,7 +2,7 @@ Name: data-provider-master Summary: Master service provider for badge, shortcut, notification -Version: 1.6.28 +Version: 1.6.29 Release: 1 Group: Applications/Core Applications License: Apache-2.0 -- 2.7.4 From 4fddf084528f6af380f9d406cc8dfdbae0c23927 Mon Sep 17 00:00:00 2001 From: SukhyungKang Date: Wed, 15 Nov 2023 19:06:28 +0900 Subject: [PATCH 08/16] Add busname option to activate sysinit.target - systemd can not activate dbus in TV fast boot mode, so DefaultDependencies=no is add to busname file to activate dbus from sysinit.target Change-Id: I2f17b46fe5ecd8cd6d55ec48c35f85a2089dae8d Signed-off-by: SukhyungKang --- CMakeLists.txt | 1 + org.tizen.data_provider_service.busname.in | 9 +++++++++ packaging/data-provider-master.spec | 7 +++++++ 3 files changed, 17 insertions(+) create mode 100644 org.tizen.data_provider_service.busname.in diff --git a/CMakeLists.txt b/CMakeLists.txt index eac03db..377f572 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,6 +14,7 @@ ADD_SUBDIRECTORY(src) CONFIGURE_FILE(data-provider-master.conf.in data-provider-master.conf @ONLY) CONFIGURE_FILE(org.tizen.data-provider-master.service.in org.tizen.data_provider_service.service @ONLY) +CONFIGURE_FILE(org.tizen.data_provider_service.busname.in org.tizen.data_provider_service.busname @ONLY) INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/org.tizen.data_provider_service.service DESTINATION ${SHARE_INSTALL_PREFIX}/dbus-1/system-services/) INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_NAME}.conf DESTINATION ${SYSCONF_INSTALL_DIR}/dbus-1/system.d/) diff --git a/org.tizen.data_provider_service.busname.in b/org.tizen.data_provider_service.busname.in new file mode 100644 index 0000000..67fe55e --- /dev/null +++ b/org.tizen.data_provider_service.busname.in @@ -0,0 +1,9 @@ +[Unit] +Description=DBUS1:org.tizen.data_provider_service +Documentation=man:systemd +DefaultDependencies=no + +[BusName] +Name=org.tizen.data_provider_service +Service=data-provider-master.service +AllowWorld=talk diff --git a/packaging/data-provider-master.spec b/packaging/data-provider-master.spec index 1a68490..e9721be 100644 --- a/packaging/data-provider-master.spec +++ b/packaging/data-provider-master.spec @@ -106,6 +106,11 @@ rm -rf %{buildroot} mkdir -p %{buildroot}%{_prefix}/lib/systemd/system install -m 0644 %SOURCE1 %{buildroot}%{_unitdir}/data-provider-master.service +install -m 0644 org.tizen.data_provider_service.busname %{buildroot}%{_unitdir}/org.tizen.data_provider_service.busname + +mkdir -p %{buildroot}%{_unitdir}/busnames.target.wants +ln -s ../org.tizen.data_provider_service.busname %{buildroot}%{_unitdir}/busnames.target.wants/org.tizen.data_provider_service.busname + %if 0%{?gcov:1} builddir=$(basename $PWD) gcno_obj_dir=%{buildroot}%{_datadir}/gcov/obj/%{name}/"$builddir" @@ -147,6 +152,8 @@ install -m 0755 run-unittest.sh %{buildroot}%{_bindir}/tizen-unittests/%{name}/ %attr(0755,root,root) %{_bindir}/data-provider-master %attr(0644,root,root) %{_unitdir}/data-provider-master.service %attr(0644,root,root) %{_datadir}/dbus-1/system-services/org.tizen.data_provider_service.service +%attr(0644,root,root) %{_unitdir}/org.tizen.data_provider_service.busname +%{_unitdir}/busnames.target.wants/org.tizen.data_provider_service.busname %config %{_sysconfdir}/dbus-1/system.d/data-provider-master.conf %{_prefix}/bin/%{name} #%defattr(-,owner,users,-) -- 2.7.4 From f5709f90030c490a8c39839aea43b8283cb3c6d5 Mon Sep 17 00:00:00 2001 From: SukhyungKang Date: Thu, 16 Nov 2023 10:51:37 +0900 Subject: [PATCH 09/16] Release version 1.6.30 Changes: - Add busname option to activate sysinit.target Change-Id: I3c60d5351582689e83ad96828410d773c37ef68e Signed-off-by: SukhyungKang --- packaging/data-provider-master.spec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packaging/data-provider-master.spec b/packaging/data-provider-master.spec index e9721be..cfc2079 100644 --- a/packaging/data-provider-master.spec +++ b/packaging/data-provider-master.spec @@ -2,7 +2,7 @@ Name: data-provider-master Summary: Master service provider for badge, shortcut, notification -Version: 1.6.29 +Version: 1.6.30 Release: 1 Group: Applications/Core Applications License: Apache-2.0 -- 2.7.4 From 722ff8a21147e112e0e971e68e5e2819dc51683d Mon Sep 17 00:00:00 2001 From: SukhyungKang Date: Fri, 8 Dec 2023 14:31:35 +0900 Subject: [PATCH 10/16] add dbus connection flush after emit signal Change-Id: I6ffa14f3ca4c30b21cd3c1c372f7c16506c0fe60 Signed-off-by: SukhyungKang --- src/service_common.cc | 24 ++++++++++++++++++----- tests/mock/gio_mock.cc | 5 +++++ tests/mock/gio_mock.h | 3 +++ tests/unit_tests/src/test_notification_service.cc | 5 +++++ 4 files changed, 32 insertions(+), 5 deletions(-) diff --git a/src/service_common.cc b/src/service_common.cc index 529ae5b..4983cce 100644 --- a/src/service_common.cc +++ b/src/service_common.cc @@ -285,18 +285,24 @@ int send_notify(GVariant *body, char *cmd, GHashTable **monitoring_hash, &err) == FALSE) { if (err != nullptr) { ERR("Emit signal err [%s]", err->message); - g_error_free(err); + g_clear_error(&err); } is_existed = is_existed_busname(target_bus_name); if (is_existed == false) delete_monitoring_list(monitoring_hash, target_bus_name, uid); ERR("Fail, emit signal to [%s]", target_bus_name); } + + if (g_dbus_connection_flush_sync(_gdbus_conn, NULL, &err) == FALSE) { + ERR("Failed to flush connection[%s]", err ? err->message : ""); + g_clear_error(&err); + } + monitoring_count++; - DBG("Success, emit signal to [%s]", target_bus_name); + WARN("Success, emit signal to [%s]", target_bus_name); } - DBG("Success, cmd[%s] monitoring count[%d]", cmd, monitoring_count); + WARN("Success, cmd[%s] monitoring count[%d]", cmd, monitoring_count); return SERVICE_COMMON_ERROR_NONE; } @@ -317,12 +323,20 @@ int send_event_notify_by_busname(GVariant *body, char *cmd, char *busname, if (err != nullptr) { ERR("Emit signal err [%s]", err->message); - g_error_free(err); + g_clear_error(&err); } ERR("Failed to emit signal to [%s]", busname); return SERVICE_COMMON_ERROR_IO_ERROR; } - DBG("Success, Emit signal to [%s] cmd[%s]", busname, cmd); + + if (g_dbus_connection_flush_sync(_gdbus_conn, NULL, &err) == FALSE) { + ERR("Failed to flush connection[%s]", err ? err->message : ""); + g_clear_error(&err); + + return SERVICE_COMMON_ERROR_IO_ERROR; + } + + WARN("Success, Emit signal to [%s] cmd[%s]", busname, cmd); return SERVICE_COMMON_ERROR_NONE; } diff --git a/tests/mock/gio_mock.cc b/tests/mock/gio_mock.cc index 38979e2..c0e071b 100644 --- a/tests/mock/gio_mock.cc +++ b/tests/mock/gio_mock.cc @@ -126,3 +126,8 @@ extern "C" GDBusConnection* g_bus_get_sync(GBusType type, GCancellable* cancellable, GError** error) { return MOCK_HOOK_P3(GioMock, g_bus_get_sync, type, cancellable, error); } + +extern "C" gboolean g_dbus_connection_flush_sync(GDBusConnection* arg0, + GCancellable* arg1, GError** arg2) { + return MOCK_HOOK_P3(GioMock, g_dbus_connection_flush_sync, arg0, arg1, arg2); +} diff --git a/tests/mock/gio_mock.h b/tests/mock/gio_mock.h index 3792e0d..6804628 100644 --- a/tests/mock/gio_mock.h +++ b/tests/mock/gio_mock.h @@ -86,6 +86,9 @@ class GioMock : public virtual ModuleMock { MOCK_METHOD3(g_bus_get_sync, GDBusConnection*(GBusType, GCancellable*, GError**)); + MOCK_METHOD3(g_dbus_connection_flush_sync, + gboolean(GDBusConnection*, GCancellable*, GError**)); + }; #endif // MOCK_GIO_MOCK_H_ \ No newline at end of file diff --git a/tests/unit_tests/src/test_notification_service.cc b/tests/unit_tests/src/test_notification_service.cc index 17c3d00..ab86258 100644 --- a/tests/unit_tests/src/test_notification_service.cc +++ b/tests/unit_tests/src/test_notification_service.cc @@ -438,6 +438,9 @@ TEST_F(NotificationServiceTest, notification_send_noti_event) { EXPECT_CALL(GetMock(), g_bus_watch_name(_, _, _, _, _, _, _)) .WillOnce(Return(1)); + EXPECT_CALL(GetMock(), g_dbus_connection_flush_sync(_, _, _)) + .WillRepeatedly(Return(true)); + int ret = notification_reset_event_receiver(param2, &reply2, "sender"); EXPECT_EQ(ret, NOTIFICATION_ERROR_NONE); ret = notification_send_noti_event(param, &reply); @@ -460,6 +463,8 @@ TEST_F(NotificationServiceTest, notification_send_noti_event_by_priv_id) { .WillOnce(Return(1)); EXPECT_CALL(GetMock(), notification_create(_)) .WillOnce(Return(info)); + EXPECT_CALL(GetMock(), g_dbus_connection_flush_sync(_, _, _)) + .WillRepeatedly(Return(true)); GVariant* param2 = g_variant_new("(i)", 20); GVariant* reply2; int ret = notification_reset_event_receiver(param2, &reply2, "sender"); -- 2.7.4 From 8cda0972d5db5db2eac8721e35a6fcdfbfc977ce Mon Sep 17 00:00:00 2001 From: SukhyungKang Date: Fri, 8 Dec 2023 16:19:33 +0900 Subject: [PATCH 11/16] Release version 1.6.31 Changes: - add dbus connection flush after emit signal Change-Id: I2d261b1148094231895a2e7e7d9807cb7a3fbf1d Signed-off-by: SukhyungKang --- packaging/data-provider-master.spec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packaging/data-provider-master.spec b/packaging/data-provider-master.spec index cfc2079..3c7cf24 100644 --- a/packaging/data-provider-master.spec +++ b/packaging/data-provider-master.spec @@ -2,7 +2,7 @@ Name: data-provider-master Summary: Master service provider for badge, shortcut, notification -Version: 1.6.30 +Version: 1.6.31 Release: 1 Group: Applications/Core Applications License: Apache-2.0 -- 2.7.4 From 297e2fec2e40426c855439ede3d84a6ab8208307 Mon Sep 17 00:00:00 2001 From: SukhyungKang Date: Wed, 20 Dec 2023 18:12:11 +0900 Subject: [PATCH 12/16] Remove event after using to prevent leak Change-Id: Id9cdaf2dfd4fce11ff280e29ddeeda03f84b061a Signed-off-by: SukhyungKang --- src/service_common.cc | 58 ++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 44 insertions(+), 14 deletions(-) diff --git a/src/service_common.cc b/src/service_common.cc index 4983cce..795c1de 100644 --- a/src/service_common.cc +++ b/src/service_common.cc @@ -98,17 +98,32 @@ void PackageEventListener::OnPkgmgrEvent(std::shared_ptr args) } if (args->GetEventName().compare(std::string("ok")) == 0) { - for (auto& a : pkgmgr_event_list_) { - if (a->GetTargetUid() == args->GetTargetUid() && - a->GetPkgId().compare(args->GetPkgId()) == 0) { - if (a->GetEventName().compare(std::string("install")) == 0) + auto iter = pkgmgr_event_list_.begin(); + while(iter != pkgmgr_event_list_.end()) { + if ((*iter)->GetTargetUid() == args->GetTargetUid() && + (*iter)->GetPkgId().compare(args->GetPkgId()) == 0) { + if ((*iter)->GetEventName().compare(std::string("install")) == 0) _package_install_cb(args->GetTargetUid(), args->GetPkgId().c_str()); - else if (a->GetEventName().compare(std::string("uninstall")) == 0) + else if ((*iter)->GetEventName().compare(std::string("uninstall")) == 0) _package_uninstall_cb(args->GetTargetUid(), args->GetPkgId().c_str()); - else if (a->GetEventName().compare(std::string("enable_app")) == 0) + else if ((*iter)->GetEventName().compare(std::string("enable_app")) == 0) _app_enabled_cb(args->GetTargetUid(), args->GetPkgId().c_str()); - else if (a->GetEventName().compare(std::string("disable_app")) == 0) + else if ((*iter)->GetEventName().compare(std::string("disable_app")) == 0) _app_disabled_cb(args->GetTargetUid(), args->GetPkgId().c_str()); + + iter = pkgmgr_event_list_.erase(iter); + } else { + iter++; + } + } + } else if (args->GetEventName().compare(std::string("fail")) == 0) { + auto iter = pkgmgr_event_list_.begin(); + while(iter != pkgmgr_event_list_.end()) { + if ((*iter)->GetTargetUid() == args->GetTargetUid() && + (*iter)->GetPkgId().compare(args->GetPkgId()) == 0) { + iter = pkgmgr_event_list_.erase(iter); + } else { + iter++; } } } @@ -127,17 +142,32 @@ void PackageEventListener::OnPkgmgrAppEvent(std::shared_ptr } if (args->GetEventName().compare(std::string("ok")) == 0) { - for (auto& a : pkgmgr_app_event_list_) { - if (a->GetTargetUid() == args->GetTargetUid() && - a->GetPkgId().compare(args->GetPkgId()) == 0) { - if (a->GetEventName().compare(std::string("install")) == 0) + auto iter = pkgmgr_app_event_list_.begin(); + while(iter != pkgmgr_app_event_list_.end()) { + if ((*iter)->GetTargetUid() == args->GetTargetUid() && + (*iter)->GetPkgId().compare(args->GetPkgId()) == 0) { + if ((*iter)->GetEventName().compare(std::string("install")) == 0) _package_install_cb(args->GetTargetUid(), args->GetPkgId().c_str()); - else if (a->GetEventName().compare(std::string("uninstall")) == 0) + else if ((*iter)->GetEventName().compare(std::string("uninstall")) == 0) _package_uninstall_cb(args->GetTargetUid(), args->GetPkgId().c_str()); - else if (a->GetEventName().compare(std::string("enable_app")) == 0) + else if ((*iter)->GetEventName().compare(std::string("enable_app")) == 0) _app_enabled_cb(args->GetTargetUid(), args->GetPkgId().c_str()); - else if (a->GetEventName().compare(std::string("disable_app")) == 0) + else if ((*iter)->GetEventName().compare(std::string("disable_app")) == 0) _app_disabled_cb(args->GetTargetUid(), args->GetPkgId().c_str()); + + iter = pkgmgr_app_event_list_.erase(iter); + } else { + iter++; + } + } + } else if (args->GetEventName().compare(std::string("fail")) == 0) { + auto iter = pkgmgr_app_event_list_.begin(); + while(iter != pkgmgr_app_event_list_.end()) { + if ((*iter)->GetTargetUid() == args->GetTargetUid() && + (*iter)->GetPkgId().compare(args->GetPkgId()) == 0) { + iter = pkgmgr_app_event_list_.erase(iter); + } else { + iter++; } } } -- 2.7.4 From 655b2c6841a9ce5caecf0a60a66053387e1e481c Mon Sep 17 00:00:00 2001 From: SukhyungKang Date: Thu, 28 Dec 2023 12:34:41 +0900 Subject: [PATCH 13/16] Release version 1.6.32 Changes: - Remove event after using to prevent leak Change-Id: If8c6461a416119cc0502ca589b1f86d0b51251b7 Signed-off-by: SukhyungKang --- packaging/data-provider-master.spec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packaging/data-provider-master.spec b/packaging/data-provider-master.spec index 3c7cf24..c43bf02 100644 --- a/packaging/data-provider-master.spec +++ b/packaging/data-provider-master.spec @@ -2,7 +2,7 @@ Name: data-provider-master Summary: Master service provider for badge, shortcut, notification -Version: 1.6.31 +Version: 1.6.32 Release: 1 Group: Applications/Core Applications License: Apache-2.0 -- 2.7.4 From 5ebc8ead09bde2045be1b4af8a90ae50e703bc71 Mon Sep 17 00:00:00 2001 From: Hwankyu Jhun Date: Mon, 8 Apr 2024 18:02:34 +0900 Subject: [PATCH 14/16] Add a new environment variable for using blink feature "AUL_BLINK=1" is added to the service file. Requires: - https://review.tizen.org/gerrit/#/c/platform/core/appfw/aul-1/+/309293/ Change-Id: Ifad747a4e719270c82439fcbcb3a949d0da69e67 Signed-off-by: Hwankyu Jhun --- packaging/data-provider-master.service | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packaging/data-provider-master.service b/packaging/data-provider-master.service index d2103a5..e85748c 100644 --- a/packaging/data-provider-master.service +++ b/packaging/data-provider-master.service @@ -9,7 +9,8 @@ Type=dbus SmackProcessLabel=System Capabilities=cap_dac_override=i SecureBits=keep-caps -ExecStartPre=/usr/bin/bash -c /etc/notification/upgrade.sh +Environment=AUL_BLINK=1 +ExecStartPre=/usr/bin/bash -c 'LD_PRELOAD=libaul-blink.so.0 /etc/notification/upgrade.sh' ExecStart=/usr/bin/data-provider-master TimeoutStopSec=3s Restart=on-failure -- 2.7.4 From c90ff98bdaecfcb4465372875c392628372f5a68 Mon Sep 17 00:00:00 2001 From: Hwankyu Jhun Date: Thu, 11 Apr 2024 13:06:40 +0900 Subject: [PATCH 15/16] Modify service file To disable non-essential dependencies, we should set 'DefaultDependencies=no' to the service file. The DPM requires dbus.socket to use gdbus API. Change-Id: I4371e54c31d144f28ba9b65b87eb86438ab139d6 Signed-off-by: Hwankyu Jhun --- packaging/data-provider-master.service | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packaging/data-provider-master.service b/packaging/data-provider-master.service index e85748c..a2f5cf4 100644 --- a/packaging/data-provider-master.service +++ b/packaging/data-provider-master.service @@ -1,5 +1,8 @@ [Unit] Description=Data Provider Master +Requires=dbus.socket +After=dbus.socket +DefaultDependencies=no [Service] User=app_fw -- 2.7.4 From 25af677dde5192f75df1f67e0c85274867c6686c Mon Sep 17 00:00:00 2001 From: Hwankyu Jhun Date: Thu, 11 Apr 2024 20:40:30 +0900 Subject: [PATCH 16/16] Release version 1.6.33 Changes: - Add a new environment variable for using blink feature - Modify service file Change-Id: I92888480fdd1541ad1c5b9a5b55bf1623c624b4a Signed-off-by: Hwankyu Jhun --- packaging/data-provider-master.spec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packaging/data-provider-master.spec b/packaging/data-provider-master.spec index c43bf02..ac9905c 100644 --- a/packaging/data-provider-master.spec +++ b/packaging/data-provider-master.spec @@ -2,7 +2,7 @@ Name: data-provider-master Summary: Master service provider for badge, shortcut, notification -Version: 1.6.32 +Version: 1.6.33 Release: 1 Group: Applications/Core Applications License: Apache-2.0 -- 2.7.4