From 9c2bba29d0b5bdded4c17a193b6419b78af318dd Mon Sep 17 00:00:00 2001 From: Inkyun Kil Date: Wed, 23 Nov 2022 16:42:18 +0900 Subject: [PATCH 01/16] Rewrite legacy code to C++ for vconf handler Change-Id: I9e83b91c111b145c131b502374c96776bb818c1d Signed-off-by: Inkyun Kil --- src/modules/dbus_event/dbus_event_module.cc | 11 +- src/modules/dbus_event/dbus_event_module.hh | 5 + ...{esd_system_event.cc => vconf_event_handler.cc} | 301 ++++++++++----------- src/modules/dbus_event/vconf_event_handler.hh | 53 ++++ tests/mock/eventsystem_mock.cc | 24 ++ tests/mock/eventsystem_mock.h | 34 +++ tests/unit_tests/CMakeLists.txt | 9 +- tests/unit_tests/src/test_vconf_handler.cc | 67 +++++ 8 files changed, 337 insertions(+), 167 deletions(-) rename src/modules/dbus_event/{esd_system_event.cc => vconf_event_handler.cc} (61%) create mode 100644 src/modules/dbus_event/vconf_event_handler.hh create mode 100644 tests/mock/eventsystem_mock.cc create mode 100644 tests/mock/eventsystem_mock.h create mode 100644 tests/unit_tests/src/test_vconf_handler.cc diff --git a/src/modules/dbus_event/dbus_event_module.cc b/src/modules/dbus_event/dbus_event_module.cc index 9090cd0..1077f59 100644 --- a/src/modules/dbus_event/dbus_event_module.cc +++ b/src/modules/dbus_event/dbus_event_module.cc @@ -30,8 +30,6 @@ } \ } while (0) -int __esd_register_vconf_callbacks(); - namespace { #include "introspection_cb.hh" @@ -1313,7 +1311,9 @@ void __esd_on_name_acquired(GDBusConnection *connection, eventsystem_send_system_event(SYS_EVENT_ESD_STATUS, b); bundle_free(b); - __esd_register_vconf_callbacks(); + esd::module::DbusEventModule* mod = + static_cast(user_data); + mod->SetVconfHandler(); __esd_trusted_busname_print_items(); @@ -1811,6 +1811,11 @@ bool DbusEventModule::BeforeLoop() { return true; } +void DbusEventModule::SetVconfHandler() { + vconf_handler_ = std::make_unique(); + vconf_handler_->SetDefaultEvents(); +} + void DbusEventModule::HandleMethodCallCb(GDBusConnection *connection, const gchar *sender, const gchar *object_path, const gchar *interface_name, const gchar *method_name, diff --git a/src/modules/dbus_event/dbus_event_module.hh b/src/modules/dbus_event/dbus_event_module.hh index 29788a9..c4d6399 100644 --- a/src/modules/dbus_event/dbus_event_module.hh +++ b/src/modules/dbus_event/dbus_event_module.hh @@ -22,6 +22,8 @@ #include +#include "vconf_event_handler.hh" + namespace esd::module { class DbusEventModule : public api::IModule { @@ -32,6 +34,8 @@ class DbusEventModule : public api::IModule { bool Startup(api::ToolBox* tools) override; void Shutdown() override; + void SetVconfHandler(); + static void HandleMethodCallCb(GDBusConnection *connection, const gchar *sender, const gchar *object_path, const gchar *interface_name, const gchar *method_name, @@ -86,6 +90,7 @@ class DbusEventModule : public api::IModule { private: api::ToolBox* tools_ = nullptr; + std::unique_ptr vconf_handler_; }; } // namespace esd::module diff --git a/src/modules/dbus_event/esd_system_event.cc b/src/modules/dbus_event/vconf_event_handler.cc similarity index 61% rename from src/modules/dbus_event/esd_system_event.cc rename to src/modules/dbus_event/vconf_event_handler.cc index 816353e..b76bf84 100644 --- a/src/modules/dbus_event/esd_system_event.cc +++ b/src/modules/dbus_event/vconf_event_handler.cc @@ -1,97 +1,28 @@ -#include -#include -#include -#include -#include -#include - +/* + * Copyright (c) 2022 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 "vconf_event_handler.hh" + +#include #include +#include -namespace { - -/* table item : sent system-event by esd */ -static GHashTable *esd_sent_table; - -typedef struct __esd_sent_table_item { - char *event_name; - bundle *event_data; -} esd_sent_item; - -struct esd_vconf_handler { - const char *key; - void (*esd_vconfcb_fn) (keynode_t *node, void *user_data); -}; - -int __esd_event_data_compare(bundle *b1, bundle *b2, const char *key) { - int ret = 0; - int tmp1 = 0; - int tmp2 = 0; - char *str1 = NULL; - char *str2 = NULL; - - if (bundle_get_count(b1) == bundle_get_count(b2)) { - tmp1 = bundle_get_str(b1, key, &str1); - tmp2 = bundle_get_str(b2, key, &str2); - if (tmp1 == BUNDLE_ERROR_NONE && tmp2 == BUNDLE_ERROR_NONE) { - if (strcmp(str1, str2) != 0) { - _D("new event_data : value check"); - ret = 1; - } - } - } else { - _D("new event_data : bundle_count check"); - ret = 1; - } - - if (ret == 0) - _D("same event_data"); - - return ret; -} - -int __esd_send_system_event(const char *event_name, bundle *b, const char *key) { - int ret = ES_R_OK; - esd_sent_item *item = - (esd_sent_item *)g_hash_table_lookup(esd_sent_table, event_name); - - if (item && __esd_event_data_compare(item->event_data, b, key) == 0) { - _D("skip send: same with previous data"); - } else { - ret = eventsystem_send_system_event(event_name, b); - if (ret != ES_R_OK) { - _E("failed to send event"); - goto out; - } - - if (item) { - bundle_free(item->event_data); - item->event_data = bundle_dup(b); - } else { - item = (esd_sent_item *)calloc(1, sizeof(esd_sent_item)); - if (item == NULL) { - _E("memory alloc failed"); - ret = ES_R_ERROR; - goto out; - } - item->event_name = strdup(event_name); - if (item->event_name == NULL) { - _E("out of memory"); - free(item); - item = nullptr; - ret = ES_R_ERROR; - goto out; - } - item->event_data = bundle_dup(b); - } - - g_hash_table_insert(esd_sent_table, item->event_name, item); - } +namespace esd::module { -out: - return ret; -} - -void __esd_vconfcb_location_use_mylocation(keynode_t *node, void *user_data) { +void VconfEventHandler::LocationUseMyLocationCb(keynode_t *node, void *user_data) { int ret = 0; int enabled = 0; bundle *b = NULL; @@ -116,14 +47,15 @@ void __esd_vconfcb_location_use_mylocation(keynode_t *node, void *user_data) { b = bundle_create(); bundle_add_str(b, key, val); - if (__esd_send_system_event(SYS_EVENT_LOCATION_ENABLE_STATE, b, key) != ES_R_OK) + if (SendSytemEvent(std::string(SYS_EVENT_LOCATION_ENABLE_STATE), b, + std::string(key)) != ES_R_OK) _E("failed to send event"); if (b) bundle_free(b); -} +}; -void __esd_vconfcb_location_enabled(keynode_t *node, void *user_data) { +void VconfEventHandler::LocationEnabledCb(keynode_t *node, void *user_data) { int ret = 0; int enabled = 0; bundle *b = NULL; @@ -148,14 +80,15 @@ void __esd_vconfcb_location_enabled(keynode_t *node, void *user_data) { b = bundle_create(); bundle_add_str(b, key, val); - if (__esd_send_system_event(SYS_EVENT_GPS_ENABLE_STATE, b, key) != ES_R_OK) + if (SendSytemEvent(std::string(SYS_EVENT_GPS_ENABLE_STATE), b, + std::string(key)) != ES_R_OK) _E("failed to send event"); if (b) bundle_free(b); -} +}; -void __esd_vconfcb_location_network_enabled(keynode_t *node, void *user_data) { +void VconfEventHandler::LocationNetworkEnabledCb(keynode_t *node, void *user_data) { int ret = 0; int enabled = 0; bundle *b = NULL; @@ -180,14 +113,15 @@ void __esd_vconfcb_location_network_enabled(keynode_t *node, void *user_data) { b = bundle_create(); bundle_add_str(b, key, val); - if (__esd_send_system_event(SYS_EVENT_NPS_ENABLE_STATE, b, key) != ES_R_OK) + if (SendSytemEvent(std::string(SYS_EVENT_NPS_ENABLE_STATE), b, + std::string(key)) != ES_R_OK) _E("failed to send event"); if (b) bundle_free(b); -} +}; -void __esd_vconfcb_language_set(keynode_t *node, void *user_data) { +void VconfEventHandler::LangsetCb(keynode_t *node, void *user_data) { char *str = 0; bundle *b = NULL; const char *key = NULL; @@ -205,16 +139,17 @@ void __esd_vconfcb_language_set(keynode_t *node, void *user_data) { b = bundle_create(); bundle_add_str(b, key, str); - if (__esd_send_system_event(SYS_EVENT_LANGUAGE_SET, b, key) != ES_R_OK) + if (SendSytemEvent(std::string(SYS_EVENT_LANGUAGE_SET), b, + std::string(key)) != ES_R_OK) _E("failed to send event"); if (b) bundle_free(b); if (str) free(str); -} +}; -void __esd_vconfcb_hour_format(keynode_t *node, void *user_data) { +void VconfEventHandler::RegionFormatHourCb(keynode_t *node, void *user_data) { int ret = 0; int hours = 0; bundle *b = NULL; @@ -239,14 +174,15 @@ void __esd_vconfcb_hour_format(keynode_t *node, void *user_data) { b = bundle_create(); bundle_add_str(b, key, val); - if (__esd_send_system_event(SYS_EVENT_HOUR_FORMAT, b, key) != ES_R_OK) + if (SendSytemEvent(std::string(SYS_EVENT_HOUR_FORMAT), b, + std::string(key)) != ES_R_OK) _E("failed to send event"); if (b) bundle_free(b); -} +}; -void __esd_vconfcb_region_format(keynode_t *node, void *user_data) { +void VconfEventHandler::RegionFormatCb(keynode_t *node, void *user_data) { char *str = 0; bundle *b = NULL; const char *key = NULL; @@ -264,16 +200,17 @@ void __esd_vconfcb_region_format(keynode_t *node, void *user_data) { b = bundle_create(); bundle_add_str(b, key, str); - if (__esd_send_system_event(SYS_EVENT_REGION_FORMAT, b, key) != ES_R_OK) + if (SendSytemEvent(std::string(SYS_EVENT_REGION_FORMAT), b, + std::string(key)) != ES_R_OK) _E("failed to send event"); if (b) bundle_free(b); if (str) free(str); -} +}; -void __esd_vconfcb_vibration_status(keynode_t *node, void *user_data) { +void VconfEventHandler::VibrationStatusCb(keynode_t *node, void *user_data) { int ret = 0; int vibration_on = 0; int sound_on = 0; @@ -300,7 +237,8 @@ void __esd_vconfcb_vibration_status(keynode_t *node, void *user_data) { val = EVT_VAL_VIBRATION_ON; b = bundle_create(); bundle_add_str(b, key, val); - if (__esd_send_system_event(SYS_EVENT_VIBRATION_STATE, b, key) != ES_R_OK) + if (SendSytemEvent(std::string(SYS_EVENT_VIBRATION_STATE), b, + std::string(key)) != ES_R_OK) _E("failed to send event"); if (b) @@ -310,7 +248,8 @@ void __esd_vconfcb_vibration_status(keynode_t *node, void *user_data) { val = EVT_VAL_SILENTMODE_OFF; b = bundle_create(); bundle_add_str(b, key, val); - if (__esd_send_system_event(SYS_EVENT_SILENT_MODE, b, key) != ES_R_OK) + if (SendSytemEvent(std::string(SYS_EVENT_SILENT_MODE), b, + std::string(key)) != ES_R_OK) _E("failed to send event"); if (b) @@ -320,7 +259,8 @@ void __esd_vconfcb_vibration_status(keynode_t *node, void *user_data) { val = EVT_VAL_VIBRATION_OFF; b = bundle_create(); bundle_add_str(b, key, val); - if (__esd_send_system_event(SYS_EVENT_VIBRATION_STATE, b, key) != ES_R_OK) + if (SendSytemEvent(std::string(SYS_EVENT_VIBRATION_STATE), b, + std::string(key)) != ES_R_OK) _E("failed to send event"); if (b) @@ -331,16 +271,17 @@ void __esd_vconfcb_vibration_status(keynode_t *node, void *user_data) { val = EVT_VAL_SILENTMODE_ON; b = bundle_create(); bundle_add_str(b, key, val); - if (__esd_send_system_event(SYS_EVENT_SILENT_MODE, b, key) != ES_R_OK) + if (SendSytemEvent(std::string(SYS_EVENT_SILENT_MODE), b, + std::string(key)) != ES_R_OK) _E("failed to send event"); if (b) bundle_free(b); } } -} +}; -void __esd_vconfcb_sound_status(keynode_t *node, void *user_data) { +void VconfEventHandler::SoundStatusCb(keynode_t *node, void *user_data) { int ret = 0; int vibration_on = 0; int sound_on = 0; @@ -367,7 +308,8 @@ void __esd_vconfcb_sound_status(keynode_t *node, void *user_data) { val = EVT_VAL_VIBRATION_OFF; b = bundle_create(); bundle_add_str(b, key, val); - if (__esd_send_system_event(SYS_EVENT_VIBRATION_STATE, b, key) != ES_R_OK) + if (SendSytemEvent(std::string(SYS_EVENT_VIBRATION_STATE), b, + std::string(key)) != ES_R_OK) _E("failed to send event"); if (b) @@ -377,7 +319,8 @@ void __esd_vconfcb_sound_status(keynode_t *node, void *user_data) { val = EVT_VAL_SILENTMODE_OFF; b = bundle_create(); bundle_add_str(b, key, val); - if (__esd_send_system_event(SYS_EVENT_SILENT_MODE, b, key) != ES_R_OK) + if (SendSytemEvent(std::string(SYS_EVENT_SILENT_MODE), b, + std::string(key)) != ES_R_OK) _E("failed to send event"); if (b) @@ -388,16 +331,17 @@ void __esd_vconfcb_sound_status(keynode_t *node, void *user_data) { val = EVT_VAL_SILENTMODE_ON; b = bundle_create(); bundle_add_str(b, key, val); - if (__esd_send_system_event(SYS_EVENT_SILENT_MODE, b, key) != ES_R_OK) + if (SendSytemEvent(std::string(SYS_EVENT_SILENT_MODE), b, + std::string(key)) != ES_R_OK) _E("failed to send event"); if (b) bundle_free(b); } } -} +}; -void __esd_vconfcb_auto_rotate(keynode_t *node, void *user_data) { +void VconfEventHandler::AutoRotateCb(keynode_t *node, void *user_data) { int ret = 0; int enabled = 0; bundle *b = NULL; @@ -422,14 +366,15 @@ void __esd_vconfcb_auto_rotate(keynode_t *node, void *user_data) { b = bundle_create(); bundle_add_str(b, key, val); - if (__esd_send_system_event(SYS_EVENT_SCREEN_AUTOROTATE_STATE, b, key) != ES_R_OK) + if (SendSytemEvent(std::string(SYS_EVENT_SCREEN_AUTOROTATE_STATE), b, + std::string(key)) != ES_R_OK) _E("failed to send event"); if (b) bundle_free(b); -} +}; -void __esd_vconfcb_mobiledata_state(keynode_t *node, void *user_data) { +void VconfEventHandler::MobileDataStateCb(keynode_t *node, void *user_data) { int ret = 0; int enabled = 0; bundle *b = NULL; @@ -454,14 +399,15 @@ void __esd_vconfcb_mobiledata_state(keynode_t *node, void *user_data) { b = bundle_create(); bundle_add_str(b, key, val); - if (__esd_send_system_event(SYS_EVENT_MOBILE_DATA_STATE, b, key) != ES_R_OK) + if (SendSytemEvent(std::string(SYS_EVENT_MOBILE_DATA_STATE), b, + std::string(key)) != ES_R_OK) _E("failed to send event"); if (b) bundle_free(b); -} +}; -void __esd_vconfcb_roaming_state(keynode_t *node, void *user_data) { +void VconfEventHandler::RoamingStateCb(keynode_t *node, void *user_data) { int ret = 0; int enabled = 0; bundle *b = NULL; @@ -486,14 +432,15 @@ void __esd_vconfcb_roaming_state(keynode_t *node, void *user_data) { b = bundle_create(); bundle_add_str(b, key, val); - if (__esd_send_system_event(SYS_EVENT_DATA_ROAMING_STATE, b, key) != ES_R_OK) + if (SendSytemEvent(std::string(SYS_EVENT_DATA_ROAMING_STATE), b, + std::string(key)) != ES_R_OK) _E("failed to send event"); if (b) bundle_free(b); -} +}; -void __esd_vconfcb_font_set(keynode_t *node, void *user_data) { +void VconfEventHandler::FontSetCb(keynode_t *node, void *user_data) { char *str = 0; bundle *b = NULL; const char *key = NULL; @@ -511,51 +458,83 @@ void __esd_vconfcb_font_set(keynode_t *node, void *user_data) { b = bundle_create(); bundle_add_str(b, key, str); - if (__esd_send_system_event(SYS_EVENT_FONT_SET, b, key) != ES_R_OK) + if (SendSytemEvent(std::string(SYS_EVENT_FONT_SET), b, + std::string(key)) != ES_R_OK) _E("failed to send event"); if (b) bundle_free(b); if (str) free(str); +}; + +void VconfEventHandler::SetDefaultEvents() { + vconf_notify_key_changed(VCONFKEY_LOCATION_USE_MY_LOCATION, LocationUseMyLocationCb, NULL); + vconf_notify_key_changed(VCONFKEY_LOCATION_ENABLED, LocationEnabledCb, NULL); + vconf_notify_key_changed(VCONFKEY_LOCATION_NETWORK_ENABLED, LocationNetworkEnabledCb, NULL); + vconf_notify_key_changed(VCONFKEY_LANGSET, LangsetCb, NULL); + vconf_notify_key_changed(VCONFKEY_REGIONFORMAT_TIME1224, RegionFormatHourCb, NULL); + vconf_notify_key_changed(VCONFKEY_REGIONFORMAT, RegionFormatCb, NULL); + vconf_notify_key_changed(VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL, VibrationStatusCb, NULL); + vconf_notify_key_changed(VCONFKEY_SETAPPL_SOUND_STATUS_BOOL, SoundStatusCb, NULL); + vconf_notify_key_changed(VCONFKEY_SETAPPL_AUTO_ROTATE_SCREEN_BOOL, AutoRotateCb, NULL); + vconf_notify_key_changed(VCONFKEY_3G_ENABLE, MobileDataStateCb, NULL); + vconf_notify_key_changed(VCONFKEY_SETAPPL_STATE_DATA_ROAMING_BOOL, RoamingStateCb, NULL); + vconf_notify_key_changed(VCONFKEY_SETAPPL_ACCESSIBILITY_FONT_NAME, FontSetCb, NULL); } -struct esd_vconf_handler vconf_callbacks[] = { - {VCONFKEY_LOCATION_USE_MY_LOCATION, __esd_vconfcb_location_use_mylocation}, - {VCONFKEY_LOCATION_ENABLED, __esd_vconfcb_location_enabled}, - {VCONFKEY_LOCATION_NETWORK_ENABLED, __esd_vconfcb_location_network_enabled}, - {VCONFKEY_LANGSET, __esd_vconfcb_language_set}, - {VCONFKEY_REGIONFORMAT_TIME1224, __esd_vconfcb_hour_format}, - {VCONFKEY_REGIONFORMAT, __esd_vconfcb_region_format}, - {VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL, __esd_vconfcb_vibration_status}, - {VCONFKEY_SETAPPL_SOUND_STATUS_BOOL, __esd_vconfcb_sound_status}, - {VCONFKEY_SETAPPL_AUTO_ROTATE_SCREEN_BOOL, __esd_vconfcb_auto_rotate}, - {VCONFKEY_3G_ENABLE, __esd_vconfcb_mobiledata_state}, - {VCONFKEY_SETAPPL_STATE_DATA_ROAMING_BOOL, __esd_vconfcb_roaming_state}, - {VCONFKEY_SETAPPL_ACCESSIBILITY_FONT_NAME, __esd_vconfcb_font_set}, -}; +std::map VconfEventHandler::event_table_; + +int VconfEventHandler::SendSytemEvent(std::string event_name, bundle *b, std::string key) { + int ret = ES_R_OK; + bundle* data = nullptr; + const auto& it = event_table_.find(event_name); + if (it != event_table_.end()) + data = it->second; + + if (data && EventDataCompare(data, b, key) == 0) { + _D("skip send: same with previous data"); + } else { + ret = eventsystem_send_system_event(event_name.c_str(), b); + if (ret != ES_R_OK) { + _E("failed to send event"); + return ret; + } -int vconfcbs_size = sizeof(vconf_callbacks)/sizeof(struct esd_vconf_handler); + if (data) + bundle_free(data); -} // namespace + event_table_[event_name] = bundle_dup(b); + } + + return ret; +} -int __esd_register_vconf_callbacks() { - int i = 0; +int VconfEventHandler::EventDataCompare(bundle* b1, bundle* b2, std::string key) { int ret = 0; - int result = ES_R_OK; - - esd_sent_table = g_hash_table_new(g_str_hash, g_str_equal); - - _D("vconf callbacks size(%d)", vconfcbs_size); - for (i = 0; i < vconfcbs_size; i++) { - ret = vconf_notify_key_changed(vconf_callbacks[i].key, - vconf_callbacks[i].esd_vconfcb_fn, NULL); - if (ret != VCONF_OK) { - _E("failed to register vconf callback (%s)", vconf_callbacks[i].key); - result = ES_R_ERROR; - break; + int tmp1 = 0; + int tmp2 = 0; + char *str1 = NULL; + char *str2 = NULL; + + if (bundle_get_count(b1) == bundle_get_count(b2)) { + tmp1 = bundle_get_str(b1, key.c_str(), &str1); + tmp2 = bundle_get_str(b2, key.c_str(), &str2); + if (tmp1 == BUNDLE_ERROR_NONE && tmp2 == BUNDLE_ERROR_NONE) { + if (strcmp(str1, str2) != 0) { + _D("new event_data : value check"); + ret = 1; + } } + } else { + _D("new event_data : bundle_count check"); + ret = 1; } - return result; + if (ret == 0) + _D("same event_data"); + + return ret; } + +} // namespace \ No newline at end of file diff --git a/src/modules/dbus_event/vconf_event_handler.hh b/src/modules/dbus_event/vconf_event_handler.hh new file mode 100644 index 0000000..5828600 --- /dev/null +++ b/src/modules/dbus_event/vconf_event_handler.hh @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2022 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 EVENTSYSTEM_MODULES_DBUS_EVENT_VCONF_EVENT_HANDLER_HH_ +#define EVENTSYSTEM_MODULES_DBUS_EVENT_VCONF_EVENT_HANDLER_HH_ + +#include +#include +#include +#include + +namespace esd::module { + +class VconfEventHandler { + public: + static int SendSytemEvent(std::string event_name, bundle *b, std::string key); + static int EventDataCompare(bundle *b1, bundle *b2, std::string key); + void SetDefaultEvents(); + + private: + static void LocationUseMyLocationCb(keynode_t *node, void *user_data); + static void LocationEnabledCb(keynode_t *node, void *user_data); + static void LocationNetworkEnabledCb(keynode_t *node, void *user_data); + static void LangsetCb(keynode_t *node, void *user_data); + static void RegionFormatHourCb(keynode_t *node, void *user_data); + static void RegionFormatCb(keynode_t *node, void *user_data); + static void VibrationStatusCb(keynode_t *node, void *user_data); + static void SoundStatusCb(keynode_t *node, void *user_data); + static void AutoRotateCb(keynode_t *node, void *user_data); + static void MobileDataStateCb(keynode_t *node, void *user_data); + static void RoamingStateCb(keynode_t *node, void *user_data); + static void FontSetCb(keynode_t *node, void *user_data); + + private: + static std::map event_table_; +}; + +} // namespace + +#endif // EVENTSYSTEM_MODULES_DBUS_EVENT_VCONF_EVENT_HANDLER_HH_ \ No newline at end of file diff --git a/tests/mock/eventsystem_mock.cc b/tests/mock/eventsystem_mock.cc new file mode 100644 index 0000000..d992e1a --- /dev/null +++ b/tests/mock/eventsystem_mock.cc @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2022 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 "eventsystem_mock.h" +#include "mock_hook.h" +#include "test_fixture.h" + +extern "C" int eventsystem_send_system_event(const char* arg0, bundle* arg1) { + return MOCK_HOOK_P2(EventSystemMock, eventsystem_send_system_event, arg0, arg1); +} + diff --git a/tests/mock/eventsystem_mock.h b/tests/mock/eventsystem_mock.h new file mode 100644 index 0000000..75fa309 --- /dev/null +++ b/tests/mock/eventsystem_mock.h @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2022 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_EVENTSYSTEM_MOCK_H_ +#define MOCK_EVENTSYSTEM_MOCK_H_ + +#include +#include +#include + +#include "module_mock.h" + +class EventSystemMock : public virtual ModuleMock { + public: + virtual ~EventSystemMock() {} + + MOCK_METHOD2(eventsystem_send_system_event, int (const char*, bundle*)); + +}; + +#endif // MOCK_EVENTSYSTEM_MOCK_H_ \ No newline at end of file diff --git a/tests/unit_tests/CMakeLists.txt b/tests/unit_tests/CMakeLists.txt index 90e8a31..56e001d 100644 --- a/tests/unit_tests/CMakeLists.txt +++ b/tests/unit_tests/CMakeLists.txt @@ -33,21 +33,24 @@ FOREACH(flag ${${PROJECT_NAME}-unittests_CFLAGS}) 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) -INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/../../src/lib/modules/dbus_event) +INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/../../src/lib) +INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/../../src/modules/dbus_event) 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/modules/dbus_event/* LIB_SOURCES) +AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR}/../../src/lib LIB_SOURCES) +AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR}/../../src/modules/dbus_event DBUS_SOURCES) ADD_EXECUTABLE(${PROJECT_NAME} ${TEST_SOURCES} ${LIB_SOURCES} + ${DBUS_SOURCES} ${MOCK_SOURCES} ) diff --git a/tests/unit_tests/src/test_vconf_handler.cc b/tests/unit_tests/src/test_vconf_handler.cc new file mode 100644 index 0000000..07d57b8 --- /dev/null +++ b/tests/unit_tests/src/test_vconf_handler.cc @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2022 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 "test_fixture.h" +#include "eventsystem_mock.h" +#include "vconf_event_handler.hh" + +// using namespace tizen_base; +using ::testing::_; +using ::testing::Return; + +class Mocks : public ::testing::NiceMock {}; + +class VconfHandlerTest : public TestFixture { + public: + VconfHandlerTest() : TestFixture(std::make_unique()) {} + virtual ~VconfHandlerTest() {} + + virtual void SetUp() { + handler_ = std::make_unique(); + } + + virtual void TearDown() { + } + + std::unique_ptr handler_; +}; + +TEST_F(VconfHandlerTest, SendSytemEvent) { +EXPECT_CALL(GetMock(), + eventsystem_send_system_event(_, _)) + .WillOnce(Return(ES_R_OK)); + bundle* b = bundle_create(); + int ret = handler_->SendSytemEvent(SYS_EVENT_WIFI_STATE, b, + std::string("key")); + EXPECT_EQ(ret, ES_R_OK); +} + +TEST_F(VconfHandlerTest, EventDataCompare) { + bundle* b1 = bundle_create(); + bundle* b2 = bundle_create(); + bundle_add_str(b1, "test_key", "val"); + bundle_add_str(b2, "test_key", "val"); + int ret = handler_->EventDataCompare(b1, b2, std::string("test_key")); + EXPECT_NE(ret, 1); + bundle_free(b1); + bundle_free(b2); +} \ No newline at end of file -- 2.7.4 From 7ea7603accc9479e4e947abd03441cd8c0066266 Mon Sep 17 00:00:00 2001 From: Inkyun Kil Date: Thu, 24 Nov 2022 16:31:13 +0900 Subject: [PATCH 02/16] Rewrite legacy code to C++ for checking privilege For SRP, codes for checking privilege should be separated from module code Change-Id: Ief43c480ec42a42147edc26f4d3f237f42032b08 Signed-off-by: Inkyun Kil --- packaging/esd.spec | 3 +- src/modules/dbus_event/dbus_event_module.cc | 151 ++----------------------- src/modules/dbus_event/dbus_event_module.hh | 2 + src/modules/dbus_event/privilege_checker.cc | 133 ++++++++++++++++++++++ src/modules/dbus_event/privilege_checker.hh | 47 ++++++++ tests/mock/cynara_mock.cc | 49 ++++++++ tests/mock/cynara_mock.h | 41 +++++++ tests/mock/eventsystem_mock.h | 2 +- tests/mock/security_manager_mock.cc | 25 ++++ tests/mock/security_manager_mock.h | 33 ++++++ tests/unit_tests/src/test_privilege_checker.cc | 110 ++++++++++++++++++ tests/unit_tests/src/test_vconf_handler.cc | 7 +- 12 files changed, 458 insertions(+), 145 deletions(-) create mode 100644 src/modules/dbus_event/privilege_checker.cc create mode 100644 src/modules/dbus_event/privilege_checker.hh create mode 100644 tests/mock/cynara_mock.cc create mode 100644 tests/mock/cynara_mock.h create mode 100644 tests/mock/security_manager_mock.cc create mode 100644 tests/mock/security_manager_mock.h create mode 100644 tests/unit_tests/src/test_privilege_checker.cc diff --git a/packaging/esd.spec b/packaging/esd.spec index a4c2315..96642cd 100644 --- a/packaging/esd.spec +++ b/packaging/esd.spec @@ -93,7 +93,7 @@ MAJORVER=`echo %{version} | awk 'BEGIN {FS="."}{print $1}'` make %{?jobs:-j%jobs} %check -ctest -v +ctest -V %install rm -rf %{buildroot} @@ -158,6 +158,7 @@ ln -sf ../esd.service %{buildroot}%{_unitdir}/multi-user.target.wants/esd.servic %manifest %{name}.manifest %license LICENSE %{_moddir}/mod/libesd-mod-dbus-event.so + ################################################# # unittests ################################################# diff --git a/src/modules/dbus_event/dbus_event_module.cc b/src/modules/dbus_event/dbus_event_module.cc index 1077f59..b825c5e 100644 --- a/src/modules/dbus_event/dbus_event_module.cc +++ b/src/modules/dbus_event/dbus_event_module.cc @@ -16,10 +16,6 @@ #include #include #include -#include -#include -#include -#include #include "log.hh" #include "dbus_event_module.hh" @@ -60,20 +56,6 @@ const char *event_launch_support_list[] = { SYS_EVENT_WIFI_STATE }; -struct privilege_info { - const char *event_name; - const char *privilege_name; -}; - -const struct privilege_info privilege_check_list[] = { - {SYS_EVENT_DISPLAY_STATE, "http://tizen.org/privilege/display"}, - {SYS_EVENT_WIFI_STATE, "http://tizen.org/privilege/network.get"}, - {SYS_EVENT_INCOMMING_MSG, "http://tizen.org/privilege/message.read"}, - {SYS_EVENT_OUTGOING_MSG, "http://tizen.org/privilege/message.read"} -}; - -int privilege_check_size = sizeof(privilege_check_list)/sizeof(struct privilege_info); - const char *earlier_event_list[] = { SYS_EVENT_ESD_STATUS, SYS_EVENT_LOW_MEMORY, @@ -170,26 +152,6 @@ typedef struct __esd_appctrl_cb_data { void __esd_event_handler(char *event_name, bundle *data, void *user_data); int __esd_add_appinfo_handler(const pkgmgrinfo_appinfo_h handle, void *data); -cynara *r_cynara; - -int __esd_init_cynara() { - int ret; - - ret = cynara_initialize(&r_cynara, NULL); - if (ret != CYNARA_API_SUCCESS) { - _E("cynara initialize failed."); - return ret; - } - - return 0; -} - -void __esd_finish_cynara() { - if (r_cynara) - cynara_finish(r_cynara); - r_cynara = NULL; -} - void free_saved_event(struct __last_event_item *item) { if (!item) return; @@ -513,63 +475,6 @@ int __esd_check_trusted_events(GDBusConnection *conn, const char *list_name) { return ES_R_OK; } -int __esd_check_privilege_name(const char *event_name, char **privilege_name) { - int i = 0; - - *privilege_name = NULL; - - for (i = 0; i < privilege_check_size; i++) { - if (strcmp(event_name, privilege_check_list[i].event_name) == 0) { - *privilege_name = (char *)privilege_check_list[i].privilege_name; - _D("[%d] privilege_name(%s)", i, *privilege_name); - break; - } - } - - return ES_R_OK; -} - -bool __esd_check_valid_privilege_by_cynara(const char *appid, const char *client, - const char *session, const char *user, const char *privilege_name) { - int ret = 0; - bool has_privilege = false; - - _D("check privilege, (%s, %s, %s, %s, %s)", appid, client, session, user, privilege_name); - - ret = cynara_check(r_cynara, client, session, user, privilege_name); - if (ret == CYNARA_API_ACCESS_ALLOWED) { - _D("valid privilege"); - has_privilege = true; - } else if (ret == CYNARA_API_ACCESS_DENIED) { - _E("invalid privilege"); - } else { - _E("failed to check privilege, error(%d)", ret); - } - - return has_privilege; -} - -int __esd_check_app_privileged_event(uid_t uid, const char *appid, const char *pkgid, const char *event_name) { - char *privilege_name = NULL; - int ret = 0; - int result = 0; - - _D("event_name(%s), uid(%d), appid(%s), pkgid(%s)", event_name, uid, appid, pkgid); - - __esd_check_privilege_name(event_name, &privilege_name); - - if (privilege_name) { - ret = security_manager_app_has_privilege(appid, privilege_name, uid, &result); - if (ret != SECURITY_MANAGER_SUCCESS) - _E("failed to check privilege(%d)", ret); - _D("result(%d)", result); - } else { - result = 1; - } - - return result; -} - void __esd_print_appid_with_eventid(gpointer data, gpointer user_data) { esd_list_item_s *item = (esd_list_item_s *)data; char *event_name = (char *)user_data; @@ -1360,7 +1265,7 @@ int __esd_appcontrol_cb(const char *operation, _D("appid(%s), event_name(%s)", appid, event_name); if (!__esd_check_event_launch_support(event_name)) _E("failed to add item (not support event)"); - else if (!__esd_check_app_privileged_event(uid, appid, pkgid, event_name)) + else if (!esd::module::PrivilegeChecker::AppHasPrivilege(uid, std::string(appid), std::string(event_name))) _E("failed to add item (no privilege)"); else if (__esd_add_launch_item(uid, event_name, appid, pkgid)) _E("failed to add item"); @@ -1539,12 +1444,10 @@ bool DbusEventModule::Init() { pkgmgr_client *client; esd_pkgmgr_event *pkg_event; - __esd_init_cynara(); - + auto checker = std::make_unique(); client = pkgmgr_client_new(PC_LISTENING); if (client == NULL) { _E("set pkgmgr client failed"); - __esd_finish_cynara(); return false; } @@ -1554,7 +1457,6 @@ bool DbusEventModule::Init() { ret = pkgmgr_client_free(client); if (ret != PKGMGR_R_OK) _E("pkgmgr_client_free failed(%d)", ret); - __esd_finish_cynara(); return false; } @@ -1565,11 +1467,11 @@ bool DbusEventModule::Init() { if (ret != PKGMGR_R_OK) _E("pkgmgr_client_free failed(%d)", ret); free(pkg_event); - __esd_finish_cynara(); return false; } s_info.client = client; + checker_ = std::move(checker); _I("esd init done"); @@ -1650,8 +1552,7 @@ void DbusEventModule::Fini() { _E("pkgmgr_client_free failed(%d)", ret); } - __esd_finish_cynara(); - + checker_->Reset(); _D("esd finalize end"); } @@ -2018,62 +1919,30 @@ void DbusEventModule::CheckPrivilegeValidMethodCall(GDBusConnection* connection, const gchar* sender, GVariant* parameters, GDBusMethodInvocation* invocation) { GVariant *param = NULL; - int result = 0; + int result = 1; char *event_name = NULL; - char *privilege_name = NULL; char app_id[128] = {0, }; int sender_pid = 0; uid_t sender_uid = 0; - char *client = NULL; - char *session = NULL; - char *user = NULL; - int ret = 0; + std::string privilege_name; g_variant_get(parameters, "(&s)", &event_name); - __esd_check_privilege_name(event_name, &privilege_name); - _D("event_name(%s), privilege_name(%s)", event_name, privilege_name); + privilege_name = checker_->CheckPrivilegeName(std::string(event_name)); + _D("event_name(%s), privilege_name(%s)", event_name, privilege_name.c_str()); - if (privilege_name) { + if (!privilege_name.empty()) { sender_pid = __get_sender_pid(connection, sender); sender_uid = (uid_t)__get_sender_uid(connection, sender); if (__esd_get_appid_by_pid_for_uid(sender_pid, sender_uid, app_id, sizeof(app_id)) < 0) { result = ES_R_ERROR; } else { - ret = cynara_creds_gdbus_get_client(connection, sender, CLIENT_METHOD_DEFAULT, &client); - if (ret != CYNARA_API_SUCCESS) { - _E("failed to get client"); - result = ES_R_EINVAL; - goto out; - } - - ret = cynara_creds_gdbus_get_user(connection, sender, USER_METHOD_DEFAULT, &user); - if (ret != CYNARA_API_SUCCESS) { - _E("failed to get user"); - result = ES_R_EINVAL; - goto out; - } - - session = cynara_session_from_pid(sender_pid); - if (session == NULL) { - _E("failed to get session"); - result = ES_R_EINVAL; - goto out; - } - - _D("app_id(%s), client(%s), session(%s), user(%s)", app_id, client, session, user); - if (__esd_check_valid_privilege_by_cynara(app_id, client, session, user, privilege_name)) + if(checker_->Check(connection, sender, sender_pid, privilege_name)) result = 1; else result = ES_R_EINVAL; } - } else { - result = 1; } -out: - g_free(client); - g_free(user); - g_free(session); param = g_variant_new("(i)", result); _D("event_name(%s), result(%d)", event_name, result); g_dbus_method_invocation_return_value(invocation, param); diff --git a/src/modules/dbus_event/dbus_event_module.hh b/src/modules/dbus_event/dbus_event_module.hh index c4d6399..2a8361c 100644 --- a/src/modules/dbus_event/dbus_event_module.hh +++ b/src/modules/dbus_event/dbus_event_module.hh @@ -23,6 +23,7 @@ #include #include "vconf_event_handler.hh" +#include "privilege_checker.hh" namespace esd::module { @@ -91,6 +92,7 @@ class DbusEventModule : public api::IModule { private: api::ToolBox* tools_ = nullptr; std::unique_ptr vconf_handler_; + std::unique_ptr checker_; }; } // namespace esd::module diff --git a/src/modules/dbus_event/privilege_checker.cc b/src/modules/dbus_event/privilege_checker.cc new file mode 100644 index 0000000..c3c5c4c --- /dev/null +++ b/src/modules/dbus_event/privilege_checker.cc @@ -0,0 +1,133 @@ +/* + * Copyright (c) 2022 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 "privilege_checker.hh" + +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +#include + +namespace { + +const std::map kPrivilegeInfoMap = { + {std::string(SYS_EVENT_DISPLAY_STATE), std::string("http://tizen.org/privilege/display")}, + {std::string(SYS_EVENT_WIFI_STATE), std::string("http://tizen.org/privilege/network.get")}, + {std::string(SYS_EVENT_INCOMMING_MSG), std::string("http://tizen.org/privilege/message.read")}, + {std::string(SYS_EVENT_OUTGOING_MSG), std::string("http://tizen.org/privilege/message.read")} +}; + +} // namespace + +namespace esd::module { + +PrivilegeChecker::PrivilegeChecker() : cynara_(nullptr, cynara_finish) { + cynara* _cynara = nullptr; + int ret = cynara_initialize(&_cynara, nullptr); + if (ret != CYNARA_API_SUCCESS) + _E("init cynara failed: %d", ret); + else + cynara_.reset(_cynara); +} + +PrivilegeChecker::~PrivilegeChecker() = default; + +bool PrivilegeChecker::Check(GDBusConnection* connection, + const gchar* sender, int sender_pid, std::string privilege) { + char *client = nullptr; + char *session = nullptr; + char *user = nullptr; + bool allowed = false; + + if (!cynara_.get()) + return allowed; + + int ret = cynara_creds_gdbus_get_client(connection, sender, + CLIENT_METHOD_DEFAULT, &client); + if (ret != CYNARA_API_SUCCESS) { + _E("failed to get client"); + return allowed; + } + std::unique_ptr client_auto(client, g_free); + + ret = cynara_creds_gdbus_get_user(connection, sender, USER_METHOD_DEFAULT, + &user); + if (ret != CYNARA_API_SUCCESS) { + _E("failed to get user"); + return allowed; + } + std::unique_ptr user_auto(user, g_free); + + session = cynara_session_from_pid(sender_pid); + if (session == NULL) { + _E("failed to get session"); + return allowed; + } + std::unique_ptr session_auto(session, g_free); + + ret = cynara_check(cynara_.get(), client, session, user, privilege.c_str()); + if (ret == CYNARA_API_ACCESS_ALLOWED) + allowed = true; + else + _E("cynara access check(%s) failed: %d", privilege.c_str(), ret); + + return allowed; +} + +void PrivilegeChecker::Reset() { + cynara_.reset(); +} + +bool PrivilegeChecker::AppHasPrivilege(uid_t uid, std::string appid, + std::string event_name) { + int ret = 0; + int result = 0; + + _D("event_name(%s), uid(%d), appid(%s)", event_name.c_str(), uid, appid.c_str()); + + std::string privilege_name = CheckPrivilegeName(event_name); + if (!privilege_name.empty()) { + ret = security_manager_app_has_privilege(appid.c_str(), privilege_name.c_str(), + uid, &result); + if (ret != SECURITY_MANAGER_SUCCESS) + _E("failed to check privilege(%d)", ret); + _D("result(%d)", result); + } else { + result = 1; + } + + return result; +} + +std::string PrivilegeChecker::CheckPrivilegeName(std::string event_name) { + const auto& it = kPrivilegeInfoMap.find(event_name); + if (it == kPrivilegeInfoMap.end()) + return ""; + else + return std::string(it->second); +} + +} // namespace esd::module \ No newline at end of file diff --git a/src/modules/dbus_event/privilege_checker.hh b/src/modules/dbus_event/privilege_checker.hh new file mode 100644 index 0000000..d95c02a --- /dev/null +++ b/src/modules/dbus_event/privilege_checker.hh @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2022 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 EVENTSYSTEM_MODULES_DBUS_EVENT_PRIVILEGE_CHECKER_HH_ +#define EVENTSYSTEM_MODULES_DBUS_EVENT_PRIVILEGE_CHECKER_HH_ + +#include +#include + +#include +#include + +namespace esd::module { + +class PrivilegeChecker { + public: + PrivilegeChecker(); + PrivilegeChecker(const PrivilegeChecker&) = delete; + PrivilegeChecker& operator=(const PrivilegeChecker&) = delete; + ~PrivilegeChecker(); + bool Check(GDBusConnection* connection, + const gchar* sender, int sender_pid, std::string privilege); + void Reset(); + + static bool AppHasPrivilege(uid_t uid, std::string appid, std::string event_name); + static std::string CheckPrivilegeName(std::string event_name); + + private: + std::unique_ptr cynara_; +}; + +} // namespace esd::module + +#endif // EVENTSYSTEM_MODULES_DBUS_EVENT_PRIVILEGE_CHECKER_HH_ diff --git a/tests/mock/cynara_mock.cc b/tests/mock/cynara_mock.cc new file mode 100644 index 0000000..90c34e5 --- /dev/null +++ b/tests/mock/cynara_mock.cc @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2022 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 "cynara_mock.h" +#include "mock_hook.h" +#include "test_fixture.h" + +extern "C" int cynara_finish(cynara* arg0) { + return MOCK_HOOK_P1(CynaraMock, cynara_finish, arg0); +} + +extern "C" int cynara_check(cynara* arg0, const char* arg1, const char* arg2, + const char* arg3, const char* arg4) { + return MOCK_HOOK_P5(CynaraMock, cynara_check, arg0, arg1, arg2, arg3, arg4); +} + +extern "C" int cynara_initialize(cynara** arg0, const cynara_configuration* arg1) { + return MOCK_HOOK_P2(CynaraMock, cynara_initialize, arg0, arg1); +} + +extern "C" int cynara_creds_gdbus_get_user(GDBusConnection* arg0, + const gchar* arg1, enum cynara_user_creds arg2, gchar** arg3) { + return MOCK_HOOK_P4(CynaraMock, cynara_creds_gdbus_get_user, arg0, arg1, + arg2, arg3); +} + +extern "C" int cynara_creds_gdbus_get_client(GDBusConnection* arg0, + const gchar* arg1, enum cynara_client_creds arg2, gchar** arg3) { + return MOCK_HOOK_P4(CynaraMock, cynara_creds_gdbus_get_client, arg0, arg1, + arg2, arg3); +} + +extern "C" char* cynara_session_from_pid(pid_t arg0) { + return MOCK_HOOK_P1(CynaraMock, cynara_session_from_pid, arg0); +} + diff --git a/tests/mock/cynara_mock.h b/tests/mock/cynara_mock.h new file mode 100644 index 0000000..d5d0d4d --- /dev/null +++ b/tests/mock/cynara_mock.h @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2022 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_CYNARA_MOCK_H_ +#define MOCK_CYNARA_MOCK_H_ + +#include +#include +#include + +#include "module_mock.h" + +class CynaraMock : public virtual ModuleMock { + public: + virtual ~CynaraMock() {} + + MOCK_METHOD1(cynara_finish, int (cynara*)); + MOCK_METHOD5(cynara_check, + int (cynara*, const char*, const char*, const char*, const char*)); + MOCK_METHOD2(cynara_initialize, int (cynara**, const cynara_configuration*)); + MOCK_METHOD4(cynara_creds_gdbus_get_user, int (GDBusConnection*, + const gchar*, enum cynara_user_creds, gchar**)); + MOCK_METHOD4(cynara_creds_gdbus_get_client, int (GDBusConnection*, + const gchar*, enum cynara_client_creds, gchar**)); + MOCK_METHOD1(cynara_session_from_pid, char* (pid_t)); +}; + +#endif // MOCK_CYNARA_MOCK_H_ \ No newline at end of file diff --git a/tests/mock/eventsystem_mock.h b/tests/mock/eventsystem_mock.h index 75fa309..e2a9fb9 100644 --- a/tests/mock/eventsystem_mock.h +++ b/tests/mock/eventsystem_mock.h @@ -27,7 +27,7 @@ class EventSystemMock : public virtual ModuleMock { public: virtual ~EventSystemMock() {} - MOCK_METHOD2(eventsystem_send_system_event, int (const char*, bundle*)); + MOCK_METHOD2(eventsystem_send_system_event, int(const char*, bundle*)); }; diff --git a/tests/mock/security_manager_mock.cc b/tests/mock/security_manager_mock.cc new file mode 100644 index 0000000..478cc58 --- /dev/null +++ b/tests/mock/security_manager_mock.cc @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2022 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 "security_manager_mock.h" +#include "mock_hook.h" +#include "test_fixture.h" + +extern "C" int security_manager_app_has_privilege(const char* arg0, + const char* arg1, uid_t arg2, int* arg3) { + return MOCK_HOOK_P4(SecurityManagerMock, security_manager_app_has_privilege, + arg0, arg1, arg2, arg3); +} \ No newline at end of file diff --git a/tests/mock/security_manager_mock.h b/tests/mock/security_manager_mock.h new file mode 100644 index 0000000..fcfa5e9 --- /dev/null +++ b/tests/mock/security_manager_mock.h @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2022 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_SECURITY_MANAGER_MOCK_H_ +#define MOCK_SECURITY_MANAGER_MOCK_H_ + +#include +#include + +#include "module_mock.h" + +class SecurityManagerMock : public virtual ModuleMock { + public: + virtual ~SecurityManagerMock() {} + + MOCK_METHOD4(security_manager_app_has_privilege, int (const char*, + const char*, uid_t, int*)); +}; + +#endif // MOCK_SECURITY_MANAGER_MOCK_H_ \ No newline at end of file diff --git a/tests/unit_tests/src/test_privilege_checker.cc b/tests/unit_tests/src/test_privilege_checker.cc new file mode 100644 index 0000000..c4dbf3c --- /dev/null +++ b/tests/unit_tests/src/test_privilege_checker.cc @@ -0,0 +1,110 @@ +/* + * Copyright (c) 2022 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 "cynara_mock.h" +#include "security_manager_mock.h" +#include "test_fixture.h" +#include "privilege_checker.hh" + + +// using namespace tizen_base; +using ::testing::_; +using ::testing::DoAll; +using ::testing::Invoke; +using ::testing::Return; +using ::testing::SetArgPointee; + +namespace { + +class Mocks : public ::testing::NiceMock, + public ::testing::NiceMock{}; + +} //namespace + +class PrivilegeCheckerTest : public TestFixture { + public: + PrivilegeCheckerTest() : TestFixture(std::make_unique()) {} + virtual ~PrivilegeCheckerTest() {} + + virtual void SetUp() { + EXPECT_CALL(GetMock(), + cynara_initialize(_, _)).WillRepeatedly( + Invoke([this](cynara** cynara, const cynara_configuration* conf) { + *cynara = this->cynara_; + return CYNARA_API_SUCCESS; + })); + + cynara_ = (cynara*)malloc(1); + checker_ = std::make_unique(); + } + + virtual void TearDown() { + EXPECT_CALL(GetMock(), + cynara_finish(_)).WillRepeatedly(Return(CYNARA_API_SUCCESS)); + checker_.reset(); + free(cynara_); + } + + cynara* cynara_; + std::unique_ptr checker_; +}; + +TEST_F(PrivilegeCheckerTest, CheckPrivilegeName) { + std::string priv = checker_->CheckPrivilegeName(std::string(SYS_EVENT_WIFI_STATE)); + EXPECT_NE(priv, ""); +} + +TEST_F(PrivilegeCheckerTest, CheckPrivilegeName_N) { + std::string event_name("wifi_state"); + std::string priv = checker_->CheckPrivilegeName(event_name); + EXPECT_EQ(priv, ""); +} + +TEST_F(PrivilegeCheckerTest, AppHasPrivilege) { + EXPECT_CALL(GetMock(), + security_manager_app_has_privilege(_, _, _, _)) + .WillOnce(DoAll(SetArgPointee<3>(1), Return(SECURITY_MANAGER_SUCCESS))); + uid_t uid = 5001; + std::string app_id("app_id"); + std::string pkg_id("pkg_id"); + bool ret = checker_->AppHasPrivilege(uid, app_id, SYS_EVENT_WIFI_STATE); + EXPECT_EQ(ret, true); +} + +TEST_F(PrivilegeCheckerTest, Check) { + char* session = strdup("session"); + EXPECT_CALL(GetMock(), + cynara_creds_gdbus_get_client(_, _, _, _)) + .WillOnce(Return(CYNARA_API_SUCCESS)); + EXPECT_CALL(GetMock(), + cynara_creds_gdbus_get_user(_, _, _, _)) + .WillOnce(Return(CYNARA_API_SUCCESS)); + EXPECT_CALL(GetMock(), + cynara_session_from_pid(_)) + .WillOnce(Return(session)); + EXPECT_CALL(GetMock(), + cynara_check(_, _, _, _, _)) + .WillOnce(Return(CYNARA_API_ACCESS_ALLOWED)); + + std::string privilege("test"); + bool ret = checker_->Check(nullptr, nullptr, 10, privilege); + EXPECT_EQ(ret, true); +} \ No newline at end of file diff --git a/tests/unit_tests/src/test_vconf_handler.cc b/tests/unit_tests/src/test_vconf_handler.cc index 07d57b8..16d72b6 100644 --- a/tests/unit_tests/src/test_vconf_handler.cc +++ b/tests/unit_tests/src/test_vconf_handler.cc @@ -28,8 +28,12 @@ using ::testing::_; using ::testing::Return; +namespace { + class Mocks : public ::testing::NiceMock {}; +} // namespace + class VconfHandlerTest : public TestFixture { public: VconfHandlerTest() : TestFixture(std::make_unique()) {} @@ -46,8 +50,7 @@ class VconfHandlerTest : public TestFixture { }; TEST_F(VconfHandlerTest, SendSytemEvent) { -EXPECT_CALL(GetMock(), - eventsystem_send_system_event(_, _)) + EXPECT_CALL(GetMock(), eventsystem_send_system_event(_, _)) .WillOnce(Return(ES_R_OK)); bundle* b = bundle_create(); int ret = handler_->SendSytemEvent(SYS_EVENT_WIFI_STATE, b, -- 2.7.4 From 4b8871c183dcf24bba137571ca210b1d2304c1dd Mon Sep 17 00:00:00 2001 From: Inkyun Kil Date: Tue, 22 Nov 2022 14:26:30 +0900 Subject: [PATCH 03/16] Release version 1.0.1 changes: - Refactor esd - Use new database.hpp - Add explicit types when using method broker - Add gmock tests - Use tizen-database package - Rewrite legacy code to C++ for vconf handler - Rewrite legacy code to C++ for checking privilege Change-Id: I189276d0590a71b3c91a062f4a41789df6a5fae5 Signed-off-by: Inkyun Kil --- packaging/esd.spec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packaging/esd.spec b/packaging/esd.spec index 96642cd..7de5de3 100644 --- a/packaging/esd.spec +++ b/packaging/esd.spec @@ -1,6 +1,6 @@ Name: esd Summary: Event system daemon -Version: 1.0.0 +Version: 1.0.1 Release: 1 Group: Application Framework/Service License: Apache-2.0 -- 2.7.4 From 6d2f542fc3021bff1c81c595b4c0bad093883f7c Mon Sep 17 00:00:00 2001 From: Inkyun Kil Date: Tue, 13 Dec 2022 10:21:15 +0900 Subject: [PATCH 04/16] Fix issue for cion initialization - Fix issue for checking if table is exist Requires: - https://review.tizen.org/gerrit/#/c/platform/core/base/bundle/+/285443/ Change-Id: I02461a8caf3986699bebf327d8b20a359e3ed345 Signed-off-by: Inkyun Kil --- src/modules/cion/cion_module.cc | 51 +++++++++++++---------------------------- 1 file changed, 16 insertions(+), 35 deletions(-) diff --git a/src/modules/cion/cion_module.cc b/src/modules/cion/cion_module.cc index ec945f0..86fa6fa 100644 --- a/src/modules/cion/cion_module.cc +++ b/src/modules/cion/cion_module.cc @@ -35,8 +35,6 @@ namespace { constexpr const char CREATE_CION_TABLE[] = R"__cion( PRAGMA user_version = 50; PRAGMA journal_mode = PERSIST; -PRAGMA foreign_keys = ON; -BEGIN EXCLUSIVE TRANSACTION; CREATE TABLE IF NOT EXISTS cion_uuid ( appid TEXT NOT NULL, uuid TEXT NOT NULL, @@ -50,33 +48,18 @@ CREATE TABLE IF NOT EXISTS cion_display_name ( PRIMARY KEY(service_name, appid) , FOREIGN KEY(appid) REFERENCES cion_uuid (appid) ON DELETE CASCADE ); -COMMIT TRANSACTION; )__cion"; bool CreateTable(const tizen_base::Database& db) { - auto q = tizen_base::Database::Sql(CREATE_CION_TABLE); - auto r = db.Exec(q); - if (!static_cast(r)) - return false; - - return true; -} - -bool IsTableExist(const tizen_base::Database& db) { - auto q = tizen_base::Database::Sql( - "SELECT name FROM sqlite_master WHERE type='table'" - " ORDER BY name ASC"); - auto r = db.Exec(q); - if (!static_cast(r)) + try { + auto q = tizen_base::Database::Sql(CREATE_CION_TABLE); + db.OneStepExec(q); + } catch (const tizen_base::DbException& e) { + _E("Exception(%s) occurs", e.msg()); return false; - - for (const auto& i : r) { - auto table_name = static_cast(i.Get(0)); - if (table_name == "cion") - return true; } - return false; + return true; } } // namespace @@ -185,24 +168,22 @@ void CionModule::RemoveEnabledApp(const std::string& service_name, bool CionModule::DbInit() { try { tizen_base::Database db(DBPATH, SQLITE_OPEN_READWRITE); - if (IsTableExist(db)) { - if (!CreateTable(db)) { - _E("Fail to create table"); - return false; - } + if (!CreateTable(db)) { + _E("Fail to create table"); + return false; } - } catch(const std::runtime_error&) { + } catch (const tizen_base::DbException& e) { + _E("runtime error(%s) occurs", e.msg()); unlink(DBPATH); try { tizen_base::Database db(DBPATH, SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE); - if (IsTableExist(db)) { - if (!CreateTable(db)) { - _E("Fail to create table"); - return false; - } + if (!CreateTable(db)) { + _E("Fail to create table"); + return false; } - } catch(const std::runtime_error&) { + } catch (const tizen_base::DbException& e) { + _E("runtime error(%s) occurs", e.msg()); unlink(DBPATH); return false; } -- 2.7.4 From 52d869552d340941d714c398190e5967ec914a0e Mon Sep 17 00:00:00 2001 From: Inkyun Kil Date: Tue, 13 Dec 2022 14:08:42 +0900 Subject: [PATCH 05/16] Release version 1.0.2 changes: - Fix issue for cion initialization Change-Id: Ib778e9c6d72bc52d7477b08b59a39ba10ffb9273 Signed-off-by: Inkyun Kil --- packaging/esd.spec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packaging/esd.spec b/packaging/esd.spec index 7de5de3..c762f06 100644 --- a/packaging/esd.spec +++ b/packaging/esd.spec @@ -1,6 +1,6 @@ Name: esd Summary: Event system daemon -Version: 1.0.1 +Version: 1.0.2 Release: 1 Group: Application Framework/Service License: Apache-2.0 -- 2.7.4 From 600f9b1f37a53d28562cccc736fd535545ef036e Mon Sep 17 00:00:00 2001 From: Hwankyu Jhun Date: Tue, 13 Dec 2022 05:33:03 +0000 Subject: [PATCH 06/16] Enable foreign_keys This patch is for foreign_keys setting for cion db. Change-Id: Id3e101f78ed12330eb5eb0fa01f756e6376a3530 Signed-off-by: Hwankyu Jhun --- src/modules/cion/cion_module.cc | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/modules/cion/cion_module.cc b/src/modules/cion/cion_module.cc index 86fa6fa..758ea7f 100644 --- a/src/modules/cion/cion_module.cc +++ b/src/modules/cion/cion_module.cc @@ -35,6 +35,7 @@ namespace { constexpr const char CREATE_CION_TABLE[] = R"__cion( PRAGMA user_version = 50; PRAGMA journal_mode = PERSIST; +PRAGMA foreign_keys = ON; CREATE TABLE IF NOT EXISTS cion_uuid ( appid TEXT NOT NULL, uuid TEXT NOT NULL, @@ -50,6 +51,8 @@ CREATE TABLE IF NOT EXISTS cion_display_name ( ); )__cion"; +constexpr const char PRAGMA_FOREIGN_KEYS[] = "PRAGMA foreign_keys;"; + bool CreateTable(const tizen_base::Database& db) { try { auto q = tizen_base::Database::Sql(CREATE_CION_TABLE); @@ -62,6 +65,19 @@ bool CreateTable(const tizen_base::Database& db) { return true; } +bool IsEnabledForeignKeys(const tizen_base::Database& db) { + try { + auto sql = tizen_base::Database::Sql(PRAGMA_FOREIGN_KEYS); + auto result = db.Exec(sql); + for (const auto& i : result) + return static_cast(i.Get(0)) ? true : false; + } catch (const tizen_base::DbException& e) { + _E("Exception(%s) occurs", e.msg()); + } + + return false; +} + } // namespace bool CionModule::Startup(api::ToolBox* tools) { @@ -172,6 +188,8 @@ bool CionModule::DbInit() { _E("Fail to create table"); return false; } + + _W("foreign_keys: %s", IsEnabledForeignKeys(db) ? "ON" : "OFF"); } catch (const tizen_base::DbException& e) { _E("runtime error(%s) occurs", e.msg()); unlink(DBPATH); @@ -182,6 +200,8 @@ bool CionModule::DbInit() { _E("Fail to create table"); return false; } + + _W("foreign_keys: %s", IsEnabledForeignKeys(db) ? "ON" : "OFF"); } catch (const tizen_base::DbException& e) { _E("runtime error(%s) occurs", e.msg()); unlink(DBPATH); -- 2.7.4 From ff44a22cee1a69a28ce3b12ac4eec872b29dc504 Mon Sep 17 00:00:00 2001 From: Hwankyu Jhun Date: Tue, 13 Dec 2022 06:26:27 +0000 Subject: [PATCH 07/16] Release version 1.0.3 Changes: - Enable foreign_keys Change-Id: Ic42a3bf69f0941caec5541835ad04df80263656f Signed-off-by: Hwankyu Jhun --- packaging/esd.spec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packaging/esd.spec b/packaging/esd.spec index c762f06..e8d92e7 100644 --- a/packaging/esd.spec +++ b/packaging/esd.spec @@ -1,6 +1,6 @@ Name: esd Summary: Event system daemon -Version: 1.0.2 +Version: 1.0.3 Release: 1 Group: Application Framework/Service License: Apache-2.0 -- 2.7.4 From 166e0a1c3d5e97f9e03025631cfb6269c59af202 Mon Sep 17 00:00:00 2001 From: "jh9216.park" Date: Tue, 13 Dec 2022 05:42:24 -0500 Subject: [PATCH 08/16] Add unit tests for cion module - add unittests - fix bugs - Requires : https://review.tizen.org/gerrit/#/c/platform/core/base/bundle/+/285480/ Change-Id: I3c1c44254533661ee9922e43486d8e6c43b7aa35 Signed-off-by: jh9216.park --- CMakeLists.txt | 12 +- packaging/esd.spec | 5 +- src/lib/method_broker.hh | 4 + src/modules/cion/cion_module.cc | 30 +++-- src/modules/cion/cion_module.hh | 10 +- src/modules/dbus_event/CMakeLists.txt | 1 + tests/CMakeLists.txt | 3 +- tests/cion_unit_tests/CMakeLists.txt | 41 ++++++ tests/{unit_tests => cion_unit_tests}/src/main.cc | 0 tests/cion_unit_tests/src/test_cion_module.cc | 141 +++++++++++++++++++++ tests/dbus_event_unit_tests/CMakeLists.txt | 39 ++++++ tests/dbus_event_unit_tests/src/main.cc | 37 ++++++ .../src/test_privilege_checker.cc | 2 +- .../src/test_vconf_handler.cc | 2 +- tests/mock/cion_mock.cc | 83 ++++++++++++ tests/mock/pkgmgr_info_mock.cc | 5 + tests/mock/pkgmgr_info_mock.h | 2 + tests/mock/tzplatform_config_mock.cc | 24 ++++ tests/mock/tzplatform_config_mock.h | 37 ++++++ tests/unit_tests/CMakeLists.txt | 62 --------- 20 files changed, 456 insertions(+), 84 deletions(-) create mode 100644 tests/cion_unit_tests/CMakeLists.txt rename tests/{unit_tests => cion_unit_tests}/src/main.cc (100%) create mode 100644 tests/cion_unit_tests/src/test_cion_module.cc create mode 100644 tests/dbus_event_unit_tests/CMakeLists.txt create mode 100644 tests/dbus_event_unit_tests/src/main.cc rename tests/{unit_tests => dbus_event_unit_tests}/src/test_privilege_checker.cc (98%) rename tests/{unit_tests => dbus_event_unit_tests}/src/test_vconf_handler.cc (97%) create mode 100644 tests/mock/cion_mock.cc create mode 100644 tests/mock/tzplatform_config_mock.cc create mode 100644 tests/mock/tzplatform_config_mock.h delete mode 100644 tests/unit_tests/CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt index f566092..e13f6ce 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -21,6 +21,8 @@ SET(TARGET_LIB_ESD "libesd") ## Target modules SET(TARGET_ESD_MOD_CION "esd-mod-cion") SET(TARGET_ESD_MOD_DBUS_EVENT "esd-mod-dbus-event") +SET(TARGET_ESD_MOD_CION_UNITTESTS "esd-mod-cion-unittests") +SET(TARGET_ESD_MOD_DBUS_EVENT_UNITTESTS "esd-mod-dbus-event-unittests") #ENABLE_TESTING() #SET(TARGET_ESD_UNIT_TESTS "esd-unit-tests") @@ -52,14 +54,18 @@ PKG_CHECK_MODULES(VCONF_DEPS REQUIRED vconf) PKG_CHECK_MODULES(PKGMGR_DEPS REQUIRED pkgmgr) PKG_CHECK_MODULES(EVENTSYSTEM_DEPS REQUIRED eventsystem) PKG_CHECK_MODULES(DATABASE_DEPS REQUIRED tizen-database) +PKG_CHECK_MODULES(GMOCK_DEPS REQUIRED gmock) +PKG_CHECK_MODULES(APPSVC_DEPS REQUIRED appsvc) ADD_SUBDIRECTORY(src) IF(NOT DEFINED MINIMUM_BUILD) ADD_SUBDIRECTORY(tests) ENABLE_TESTING() -SET(UNIT_TESTS ${PROJECT_NAME}-unittests) -ADD_TEST(NAME ${UNIT_TESTS} COMMAND ${UNIT_TESTS}) -ADD_DEPENDENCIES(${UNIT_TESTS} ${PROJECT_NAME}) +ADD_TEST(NAME ${TARGET_ESD_MOD_CION_UNITTESTS} COMMAND ${TARGET_ESD_MOD_CION_UNITTESTS}) +ADD_DEPENDENCIES(${TARGET_ESD_MOD_CION_UNITTESTS} ${PROJECT_NAME}) + +ADD_TEST(NAME ${TARGET_ESD_MOD_DBUS_EVENT_UNITTESTS} COMMAND ${TARGET_ESD_MOD_DBUS_EVENT_UNITTESTS}) +ADD_DEPENDENCIES(${TARGET_ESD_MOD_DBUS_EVENT_UNITTESTS} ${PROJECT_NAME}) ENDIF(NOT DEFINED MINIMUM_BUILD) diff --git a/packaging/esd.spec b/packaging/esd.spec index e8d92e7..660ba30 100644 --- a/packaging/esd.spec +++ b/packaging/esd.spec @@ -106,7 +106,8 @@ setup() { test_main() { echo "test_main start" - /usr/bin/%{name}-unittests + /usr/bin/esd-mod-cion-unittests + /usr/bin/esd-mod-dbus_event-unittests } teardown() { @@ -163,5 +164,5 @@ ln -sf ../esd.service %{buildroot}%{_unitdir}/multi-user.target.wants/esd.servic # unittests ################################################# %files -n %{name}-unittests -%{_bindir}/%{name}-unittests +%{_bindir}/esd-*-unittests %{_bindir}/tizen-unittests/%{name}/run-unittest.sh diff --git a/src/lib/method_broker.hh b/src/lib/method_broker.hh index 00d1d35..bc667e7 100644 --- a/src/lib/method_broker.hh +++ b/src/lib/method_broker.hh @@ -54,6 +54,10 @@ class MethodBroker { std::shared_ptr>(name, std::make_shared(cmd))); } + void Unregister(std::string name) { + fmap_.erase(std::move(name)); + } + template bool Invoke(std::string name, ArgTypes... args) { using ParamsType = Params; diff --git a/src/modules/cion/cion_module.cc b/src/modules/cion/cion_module.cc index 758ea7f..691aa7d 100644 --- a/src/modules/cion/cion_module.cc +++ b/src/modules/cion/cion_module.cc @@ -81,6 +81,7 @@ bool IsEnabledForeignKeys(const tizen_base::Database& db) { } // namespace bool CionModule::Startup(api::ToolBox* tools) { + tools_ = tools; tools->GetMethodBroker().Register("Cion.GetUuidWithGenerate", api::Params&>( [this](const std::string& appid, std::optional& uuid) { @@ -116,7 +117,14 @@ bool CionModule::Startup(api::ToolBox* tools) { } void CionModule::Shutdown() { - + if (!tools_) + return; + tools_->GetMethodBroker().Unregister("Cion.GetUuidWithGenerate"); + tools_->GetMethodBroker().Unregister("Cion.SetDisplayName"); + tools_->GetMethodBroker().Unregister("Cion.GetDisplayName"); + tools_->GetMethodBroker().Unregister("Cion.SetEnabled"); + tools_->GetMethodBroker().Unregister("Cion.GetEnabled"); + tools_ = nullptr; } bool CionModule::Init() { @@ -256,7 +264,7 @@ void CionModule::GetUuidWithGenerate(const std::string& appid, try { tizen_base::Database db(DBPATH, SQLITE_OPEN_READWRITE); std::string uuid_str; - int ret = 0; + bool ret = true; if (!GetUuidFromDb(db, appid, uuid_str)) { uuid_str = GenUuid(); @@ -264,8 +272,10 @@ void CionModule::GetUuidWithGenerate(const std::string& appid, _E("get uuid generate"); } - if (ret == 0) + if (ret) uuid = std::move(uuid_str); + else + uuid = std::nullopt; } catch(const std::runtime_error&) { uuid = std::nullopt; } @@ -278,12 +288,14 @@ void CionModule::SetDisplayName(const std::string& appid, tizen_base::Database db(DBPATH, SQLITE_OPEN_READWRITE); std::string uuid; - ret = 0; + bool result; + ret = -1; if (!GetUuidFromDb(db, appid, uuid)) { uuid = GenUuid(); - ret = SetUuidToDb(db, appid, uuid); - if (ret != 0) + result = SetUuidToDb(db, appid, uuid); + if (!result) { return; + } } auto q = tizen_base::Database::Sql( @@ -298,8 +310,8 @@ void CionModule::SetDisplayName(const std::string& appid, .Bind(service_name) .Bind(appid); auto r = db.Exec(q); - if (!static_cast(r)) - ret = -1; + if (static_cast(r)) + ret = 0; } catch(const std::runtime_error&) { ret = -1; } @@ -350,7 +362,7 @@ void CionModule::SetEnabled(const std::string& appid, auto q2 = tizen_base::Database::Sql( "SELECT display_name FROM cion_display_name " - "WHERE appid = %Q AND service_name = %Q") + "WHERE appid = ? AND service_name = ?") .Bind(appid) .Bind(service_name); diff --git a/src/modules/cion/cion_module.hh b/src/modules/cion/cion_module.hh index 8c2f23a..067406b 100644 --- a/src/modules/cion/cion_module.hh +++ b/src/modules/cion/cion_module.hh @@ -36,11 +36,6 @@ class CionModule : public api::IModule { bool Startup(api::ToolBox* tools) override; void Shutdown() override; - - void AddEnabledApp(const std::string& service_name, - const std::string& app_id, const std::string& display_name); - void RemoveEnabledApp(const std::string& service_name, - const std::string& app_id); std::optional> GetEnabledServiceList(); void GetUuidWithGenerate(const std::string& appid, std::optional& uuid); @@ -48,6 +43,10 @@ class CionModule : public api::IModule { private: bool Init(); bool DbInit(); + void AddEnabledApp(const std::string& service_name, + const std::string& app_id, const std::string& display_name); + void RemoveEnabledApp(const std::string& service_name, + const std::string& app_id); void ChangeListenStatusCionServer(); void SetDisplayName(const std::string& appid, const std::string& service_name, @@ -68,6 +67,7 @@ class CionModule : public api::IModule { private: std::shared_ptr esd_cion_server_; bool is_listening_ = false; + api::ToolBox* tools_ = nullptr; }; } // namespace esd::module diff --git a/src/modules/dbus_event/CMakeLists.txt b/src/modules/dbus_event/CMakeLists.txt index 869df0b..88fd52a 100644 --- a/src/modules/dbus_event/CMakeLists.txt +++ b/src/modules/dbus_event/CMakeLists.txt @@ -23,6 +23,7 @@ APPLY_PKG_CONFIG(${TARGET_ESD_MOD_DBUS_EVENT} PUBLIC CAPI_SYSTEM_INFO_DEPS PKGMGR_DEPS EVENTSYSTEM_DEPS + APPSVC_DEPS ) INSTALL(TARGETS ${TARGET_ESD_MOD_DBUS_EVENT} DESTINATION ${ESD_MODULES_DIR}/mod diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index c90fac8..9bfec7e 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1 +1,2 @@ -ADD_SUBDIRECTORY(unit_tests) +ADD_SUBDIRECTORY(cion_unit_tests) +ADD_SUBDIRECTORY(dbus_event_unit_tests) diff --git a/tests/cion_unit_tests/CMakeLists.txt b/tests/cion_unit_tests/CMakeLists.txt new file mode 100644 index 0000000..520dd1e --- /dev/null +++ b/tests/cion_unit_tests/CMakeLists.txt @@ -0,0 +1,41 @@ +AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR}/../../src/modules/cion/ ESD_MOD_CION_SRCS) +AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR}/../../src/lib/ ESD_LIB_SRCS) +AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR}/src/ ESD_MOD_CION_TEST_SRCS) +AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR}/../mock/ ESD_MOD_CION_MOCK_SRCS) +ADD_EXECUTABLE(${TARGET_ESD_MOD_CION_UNITTESTS} + ${ESD_MOD_CION_SRCS} + ${ESD_LIB_SRCS} + ${ESD_MOD_CION_TEST_SRCS} + ${ESD_MOD_CION_MOCK_SRCS} +) + +TARGET_INCLUDE_DIRECTORIES(${TARGET_ESD_MOD_CION_UNITTESTS} PUBLIC + ${CMAKE_CURRENT_SOURCE_DIR}/../../src/modules) +TARGET_INCLUDE_DIRECTORIES(${TARGET_ESD_MOD_CION_UNITTESTS} PUBLIC + ${CMAKE_CURRENT_SOURCE_DIR}/../mock) + +TARGET_LINK_LIBRARIES(${TARGET_ESD_MOD_CION_UNITTESTS} PRIVATE ${TARGET_LIB_ESD}) + +APPLY_PKG_CONFIG(${TARGET_ESD_MOD_CION_UNITTESTS} PUBLIC + AUL_DEPS + BUNDLE_DEPS + PARCEL_DEPS + PKGMGR_INFO_DEPS + DLOG_DEPS + GIO_DEPS + GLIB_DEPS + CION_DEPS + VCONF_DEPS + CAPI_SYSTEM_INFO_DEPS + LIBTZPLATFORM_CONFIG_DEPS + UUID_DEPS + SQLITE3_DEPS + DATABASE_DEPS + SECURITY_MANAGER_DEPS + CYNARA_CLIENT_DEPS + CYNARA_CREDS_DBUS_DEPS + CYNARA_SESSION_DEPS + GMOCK_DEPS +) + +INSTALL(TARGETS ${TARGET_ESD_MOD_CION_UNITTESTS} DESTINATION bin) diff --git a/tests/unit_tests/src/main.cc b/tests/cion_unit_tests/src/main.cc similarity index 100% rename from tests/unit_tests/src/main.cc rename to tests/cion_unit_tests/src/main.cc diff --git a/tests/cion_unit_tests/src/test_cion_module.cc b/tests/cion_unit_tests/src/test_cion_module.cc new file mode 100644 index 0000000..68e1bfb --- /dev/null +++ b/tests/cion_unit_tests/src/test_cion_module.cc @@ -0,0 +1,141 @@ +/* + * Copyright (c) 2022 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 "cion/cion_module.hh" +#include "pkgmgr_info_mock.h" +#include "tzplatform_config_mock.h" +#include "test_fixture.h" + +using ::testing::_; +using ::testing::DoAll; +using ::testing::Invoke; +using ::testing::Return; +using ::testing::SetArgPointee; + +namespace { + +class Mocks : public ::testing::NiceMock, + public ::testing::NiceMock {}; + +const char *__fake_tzplatform_mkpath(enum tzplatform_variable id, const char *path) { + return ".cion_test.db"; +} + +} //namespace + +class CionModuleTest : public TestFixture { + public: + CionModuleTest() : TestFixture(std::make_unique()) {} + virtual ~CionModuleTest() {} + + virtual void SetUp() { + remove(".cion_test.db"); + tools_ = std::make_unique(); + mod_ = std::make_unique(); + EXPECT_CALL(GetMock(), tzplatform_mkpath(_, _)) + .WillRepeatedly(Invoke(__fake_tzplatform_mkpath)); + mod_->Startup(tools_.get()); + } + + virtual void TearDown() { + mod_->Shutdown(); + mod_.reset(); + tools_.reset(); + } + + std::unique_ptr mod_; + std::unique_ptr tools_; +}; + +TEST_F(CionModuleTest, GetUuidWithGenerate) { + EXPECT_CALL(GetMock(), tzplatform_mkpath(_, _)) + .WillRepeatedly(Invoke(__fake_tzplatform_mkpath)); + + std::string app_id = "org.tizen.test"; + std::optional uuid; + bool ret = tools_->GetMethodBroker() + .Invoke&>( + "Cion.GetUuidWithGenerate", app_id, uuid); + EXPECT_TRUE(ret); + EXPECT_TRUE(uuid); +} + +TEST_F(CionModuleTest, SetDisplayNameGetDisplayName) { + EXPECT_CALL(GetMock(), tzplatform_mkpath(_, _)) + .WillRepeatedly(Invoke(__fake_tzplatform_mkpath)); + + std::string app_id = "org.tizen.test"; + std::string service_name = "test_service"; + std::string display_name = "TEST_SERVICE"; + int ret = -1; + bool r = tools_->GetMethodBroker() + .Invoke( + "Cion.SetDisplayName", app_id, service_name, display_name, ret); + EXPECT_TRUE(r); + EXPECT_EQ(ret, 0); + + ret = -1; + std::string result_display_name; + r = tools_->GetMethodBroker() + .Invoke( + "Cion.GetDisplayName", app_id, service_name, result_display_name, + ret); + EXPECT_TRUE(r); + EXPECT_EQ(ret, 0); + EXPECT_EQ(display_name, result_display_name); +} + +TEST_F(CionModuleTest, SetEnabledGetEnabled) { + EXPECT_CALL(GetMock(), tzplatform_mkpath(_, _)) + .WillRepeatedly(Invoke(__fake_tzplatform_mkpath)); + EXPECT_CALL(GetMock(), pkgmgrinfo_pkginfo_get_version(_, _)) + .WillRepeatedly(Invoke([](pkgmgrinfo_pkginfo_h handle, char** version) { + *version = const_cast("1.0.0"); + return PMINFO_R_OK; + })); + + std::string app_id = "org.tizen.test"; + std::string service_name = "test_service"; + std::string display_name = "TEST_SERVICE"; + int ret = -1; + bool r = tools_->GetMethodBroker() + .Invoke( + "Cion.SetDisplayName", app_id, service_name, display_name, ret); + ASSERT_EQ(ret, 0); + + bool enabled = true; + ret = -1; + r = tools_->GetMethodBroker() + .Invoke( + "Cion.SetEnabled", app_id, service_name, enabled, ret); + EXPECT_TRUE(r); + EXPECT_EQ(ret, 0); + + ret = -1; + std::string result_display_name; + bool result_enabled = false; + r = tools_->GetMethodBroker() + .Invoke( + "Cion.GetEnabled", app_id, service_name, result_enabled, ret); + EXPECT_TRUE(r); + EXPECT_EQ(ret, 0); + EXPECT_EQ(enabled, result_enabled); +} diff --git a/tests/dbus_event_unit_tests/CMakeLists.txt b/tests/dbus_event_unit_tests/CMakeLists.txt new file mode 100644 index 0000000..be32285 --- /dev/null +++ b/tests/dbus_event_unit_tests/CMakeLists.txt @@ -0,0 +1,39 @@ +AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR}/../../src/modules/dbus_event/ ESD_MOD_DBUS_EVENT_SRC) +AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR}/../../src/lib/ ESD_LIB_SRCS) +AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR}/src/ ESD_MOD_DBUS_EVENT_TEST_SRCS) +AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR}/../mock/ ESD_MOD_DBUS_EVENT_MOCK_SRCS) +ADD_EXECUTABLE(${TARGET_ESD_MOD_DBUS_EVENT_UNITTESTS} + ${ESD_MOD_DBUS_EVENT_SRC} + ${ESD_LIB_SRCS} + ${ESD_MOD_DBUS_EVENT_TEST_SRCS} + ${ESD_MOD_DBUS_EVENT_MOCK_SRCS} +) + +TARGET_INCLUDE_DIRECTORIES(${TARGET_ESD_MOD_DBUS_EVENT_UNITTESTS} PUBLIC + ${CMAKE_CURRENT_SOURCE_DIR}/../../src/modules) +TARGET_INCLUDE_DIRECTORIES(${TARGET_ESD_MOD_DBUS_EVENT_UNITTESTS} PUBLIC + ${CMAKE_CURRENT_SOURCE_DIR}/../mock) + +TARGET_LINK_LIBRARIES(${TARGET_ESD_MOD_DBUS_EVENT_UNITTESTS} PRIVATE ${TARGET_LIB_ESD}) + +APPLY_PKG_CONFIG(${TARGET_ESD_MOD_DBUS_EVENT_UNITTESTS} PUBLIC + AUL_DEPS + DLOG_DEPS + GIO_DEPS + GLIB_DEPS + VCONF_DEPS + PKGMGR_INFO_DEPS + SECURITY_MANAGER_DEPS + CYNARA_CLIENT_DEPS + CYNARA_CREDS_DBUS_DEPS + CYNARA_SESSION_DEPS + LIBTZPLATFORM_CONFIG_DEPS + CERT_SVC_VCORE_DEPS + CAPI_SYSTEM_INFO_DEPS + PKGMGR_DEPS + EVENTSYSTEM_DEPS + GMOCK_DEPS + APPSVC_DEPS +) + +INSTALL(TARGETS ${TARGET_ESD_MOD_DBUS_EVENT_UNITTESTS} DESTINATION bin) diff --git a/tests/dbus_event_unit_tests/src/main.cc b/tests/dbus_event_unit_tests/src/main.cc new file mode 100644 index 0000000..d473442 --- /dev/null +++ b/tests/dbus_event_unit_tests/src/main.cc @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2022 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 + +int main(int argc, char** argv) { + int ret = -1; + + try { + testing::InitGoogleTest(&argc, argv); + } catch(...) { + std::cout << "Exception occurred" << std::endl; + } + + try { + ret = RUN_ALL_TESTS(); + } catch (const ::testing::internal::GoogleTestFailureException& e) { + ret = -1; + std::cout << "GoogleTestFailureException was thrown:" << e.what() << std::endl; + } + + return ret; +} diff --git a/tests/unit_tests/src/test_privilege_checker.cc b/tests/dbus_event_unit_tests/src/test_privilege_checker.cc similarity index 98% rename from tests/unit_tests/src/test_privilege_checker.cc rename to tests/dbus_event_unit_tests/src/test_privilege_checker.cc index c4dbf3c..c5c5ce9 100644 --- a/tests/unit_tests/src/test_privilege_checker.cc +++ b/tests/dbus_event_unit_tests/src/test_privilege_checker.cc @@ -22,7 +22,7 @@ #include "cynara_mock.h" #include "security_manager_mock.h" #include "test_fixture.h" -#include "privilege_checker.hh" +#include "dbus_event/privilege_checker.hh" // using namespace tizen_base; diff --git a/tests/unit_tests/src/test_vconf_handler.cc b/tests/dbus_event_unit_tests/src/test_vconf_handler.cc similarity index 97% rename from tests/unit_tests/src/test_vconf_handler.cc rename to tests/dbus_event_unit_tests/src/test_vconf_handler.cc index 16d72b6..7472c39 100644 --- a/tests/unit_tests/src/test_vconf_handler.cc +++ b/tests/dbus_event_unit_tests/src/test_vconf_handler.cc @@ -22,7 +22,7 @@ #include "test_fixture.h" #include "eventsystem_mock.h" -#include "vconf_event_handler.hh" +#include "dbus_event/vconf_event_handler.hh" // using namespace tizen_base; using ::testing::_; diff --git a/tests/mock/cion_mock.cc b/tests/mock/cion_mock.cc new file mode 100644 index 0000000..39bb275 --- /dev/null +++ b/tests/mock/cion_mock.cc @@ -0,0 +1,83 @@ +/* + * Copyright (c) 2022 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 + +namespace cion { +namespace channel { + +class ServerChannel::Impl { +}; + +ServerChannel::ServerChannel(std::string service_name, std::string display_name) { +} + +ServerChannel::ServerChannel(std::string service_name, std::string display_name, + SecurityInfo security) { +} + +ServerChannel::~ServerChannel() = default; + +void ServerChannel::Listen() { + +} + +void ServerChannel::Stop() { + +} + +void ServerChannel::Disconnect(std::shared_ptr) { + +} + +void ServerChannel::Accept(std::shared_ptr peer) { + +} + +void ServerChannel::Reject(std::shared_ptr peer, std::string reason) { + +} + +void ServerChannel::SendPayloadAsync(IPayload* data, + PayloadAsyncResult::PayloadAsyncResultCallback func) { +} + +void ServerChannel::SendPayloadAsync(IPayload* data, std::shared_ptr peer, + PayloadAsyncResult::PayloadAsyncResultCallback func) { +} + +std::list> ServerChannel::GetConnectedPeerList() { + return {}; +} + +std::string GetServiceName() { + return ""; +} + +std::string GetDisplayName() { + return ""; +} + +void SetDisplayName(std::string display_name) { +} + +void SetOndemandLaunchEnable(bool enable) { +} + +} +} diff --git a/tests/mock/pkgmgr_info_mock.cc b/tests/mock/pkgmgr_info_mock.cc index e459f52..969f45e 100644 --- a/tests/mock/pkgmgr_info_mock.cc +++ b/tests/mock/pkgmgr_info_mock.cc @@ -47,3 +47,8 @@ extern "C" int pkgmgrinfo_appinfo_get_usr_appinfo(const char* arg1, uid_t arg2, arg2, arg3); } +extern "C" int pkgmgrinfo_pkginfo_get_version(pkgmgrinfo_pkginfo_h handle, + char** version) { + return MOCK_HOOK_P2(PkgmgrInfoMock, pkgmgrinfo_pkginfo_get_version, + handle, version); +} diff --git a/tests/mock/pkgmgr_info_mock.h b/tests/mock/pkgmgr_info_mock.h index b49a879..24a9ab1 100644 --- a/tests/mock/pkgmgr_info_mock.h +++ b/tests/mock/pkgmgr_info_mock.h @@ -36,6 +36,8 @@ class PkgmgrInfoMock : public virtual ModuleMock { int (pkgmgrinfo_pkginfo_h)); MOCK_METHOD3(pkgmgrinfo_appinfo_get_usr_appinfo, int (const char *, uid_t, pkgmgrinfo_appinfo_h*)); + MOCK_METHOD2(pkgmgrinfo_pkginfo_get_version, + int (pkgmgrinfo_pkginfo_h, char**)); }; #endif // MOCK_PKMGR_INFO_MOCK_H_ \ No newline at end of file diff --git a/tests/mock/tzplatform_config_mock.cc b/tests/mock/tzplatform_config_mock.cc new file mode 100644 index 0000000..05e3211 --- /dev/null +++ b/tests/mock/tzplatform_config_mock.cc @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2022 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" const char* tzplatform_mkpath(enum tzplatform_variable arg1, const char* arg2) { + return MOCK_HOOK_P2(TzplatformConfigMock, tzplatform_mkpath, arg1, arg2); +} \ 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..a251057 --- /dev/null +++ b/tests/mock/tzplatform_config_mock.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2022 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 UNIT_TESTS_MOCK_TZPLATFORM_CONFIG_MOCK_H_ +#define UNIT_TESTS_MOCK_TZPLATFORM_CONFIG_MOCK_H_ + +#include +#include + +#include "module_mock.h" + +class TzplatformConfigMock : public virtual ModuleMock { + public: + TzplatformConfigMock() { + using ::testing::_; + using ::testing::Return; + using ::testing::Invoke; + } + + MOCK_METHOD2(tzplatform_mkpath, const char* (enum tzplatform_variable, + const char*)); +}; + +#endif // UNIT_TESTS_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 deleted file mode 100644 index 56e001d..0000000 --- a/tests/unit_tests/CMakeLists.txt +++ /dev/null @@ -1,62 +0,0 @@ -CMAKE_MINIMUM_REQUIRED(VERSION 2.8) -PROJECT(${PROJECT_NAME}-unittests C CXX) - -INCLUDE(FindPkgConfig) -PKG_CHECK_MODULES(${PROJECT_NAME}-unittests REQUIRED - aul - bundle - parcel - dlog - pkgmgr-info - appsvc - gio-2.0 - glib-2.0 - pkgmgr - eventsystem - vconf - libtzplatform-config - systemd - cert-svc-vcore - cynara-client - cynara-creds-gdbus - cynara-session - security-manager - uuid - sqlite3 - cion - capi-system-info - gmock -) - -FOREACH(flag ${${PROJECT_NAME}-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++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}/../../src/lib) -INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/../../src/modules/dbus_event) -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 LIB_SOURCES) -AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR}/../../src/modules/dbus_event DBUS_SOURCES) - -ADD_EXECUTABLE(${PROJECT_NAME} - ${TEST_SOURCES} - ${LIB_SOURCES} - ${DBUS_SOURCES} - ${MOCK_SOURCES} -) - -SET_TARGET_PROPERTIES(${PROJECT_NAME} PROPERTIES COMPILE_FLAGS "${EXTRA_CFLAGS}") -TARGET_LINK_LIBRARIES(${PROJECT_NAME} - ${${PROJECT_NAME}-unittests_LDFLAGS} -) - -INSTALL(TARGETS ${PROJECT_NAME} DESTINATION /usr/bin/) -- 2.7.4 From bc0034418757cfa78a5ecc53ba68bf9f754da480 Mon Sep 17 00:00:00 2001 From: Inkyun Kil Date: Wed, 14 Dec 2022 11:57:43 +0900 Subject: [PATCH 09/16] Release version 1.0.4 changes: - Add unit tests for cion module Change-Id: Ib0f8ab8d0cbabc3999e4493a4c5317c75e1b864b Signed-off-by: Inkyun Kil --- packaging/esd.spec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packaging/esd.spec b/packaging/esd.spec index 660ba30..5d97f31 100644 --- a/packaging/esd.spec +++ b/packaging/esd.spec @@ -1,6 +1,6 @@ Name: esd Summary: Event system daemon -Version: 1.0.3 +Version: 1.0.4 Release: 1 Group: Application Framework/Service License: Apache-2.0 -- 2.7.4 From be9ce63981db113fce5b1a3d134927ba7b51f59b Mon Sep 17 00:00:00 2001 From: Inkyun Kil Date: Thu, 15 Dec 2022 11:19:10 +0900 Subject: [PATCH 10/16] Adds gcov option for checking coverage Change-Id: Ie33e6d8187c698020185ba29f06cb2c61cda4256 Signed-off-by: Inkyun Kil --- packaging/esd.spec | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/packaging/esd.spec b/packaging/esd.spec index 5d97f31..739c8f6 100644 --- a/packaging/esd.spec +++ b/packaging/esd.spec @@ -33,6 +33,10 @@ BuildRequires: pkgconfig(capi-system-info) BuildRequires: pkgconfig(tizen-database) BuildRequires: pkgconfig(gmock) +%if 0%{?gcov:1} +BuildRequires: lcov +%endif + Requires(post): /sbin/ldconfig Requires(postun): /sbin/ldconfig @@ -74,11 +78,27 @@ Requires: %{name} %description -n %{name}-unittests GTest for API +%if 0%{?gcov:1} +%package gcov +Summary: Event system daemon(gcov) +Group: Application Framework/Service + +%description gcov +gcov objects of a Event System +%endif + %prep %setup -q cp %{SOURCE1001} . %build +%if 0%{?gcov:1} +export CFLAGS+=" -fprofile-arcs -ftest-coverage" +export CXXFLAGS+=" -fprofile-arcs -ftest-coverage" +export FFLAGS+=" -fprofile-arcs -ftest-coverage" +export LDFLAGS+=" -lgcov" +%endif + export CFLAGS="$CFLAGS -DTIZEN_DEBUG_ENABLE" export CXXFLAGS="$CXXFLAGS -DTIZEN_DEBUG_ENABLE" export FFLAGS="$FFLAGS -DTIZEN_DEBUG_ENABLE" @@ -95,6 +115,11 @@ make %{?jobs:-j%jobs} %check ctest -V +%if 0%{?gcov:1} +lcov -c --ignore-errors graph --no-external -b . -d . -o %{name}.info +genhtml %{name}.info -o out --legend --show-details +%endif + %install rm -rf %{buildroot} %make_install @@ -130,6 +155,13 @@ mkdir -p %{buildroot}%{_unitdir}/multi-user.target.wants install -m 0644 %SOURCE1 %{buildroot}%{_unitdir}/esd.service ln -sf ../esd.service %{buildroot}%{_unitdir}/multi-user.target.wants/esd.service +%if 0%{?gcov:1} +builddir=$(basename $PWD) +gcno_obj_dir=%{buildroot}%{_datadir}/gcov/obj/%{name}/"$builddir" +mkdir -p "$gcno_obj_dir" +find . -name '*.gcno' -exec cp --parents '{}' "$gcno_obj_dir" ';' +%endif + %post -p /sbin/ldconfig %postun -p /sbin/ldconfig @@ -166,3 +198,8 @@ ln -sf ../esd.service %{buildroot}%{_unitdir}/multi-user.target.wants/esd.servic %files -n %{name}-unittests %{_bindir}/esd-*-unittests %{_bindir}/tizen-unittests/%{name}/run-unittest.sh + +%if 0%{?gcov:1} +%files gcov +%{_datadir}/gcov/* +%endif \ No newline at end of file -- 2.7.4 From 98d7d980f9e32ffca705dd6957733b477468d65a Mon Sep 17 00:00:00 2001 From: "jh9216.park" Date: Thu, 8 Dec 2022 02:41:31 -0500 Subject: [PATCH 11/16] Refactor dbus-event module Change-Id: I9beef08846a1c7710eef8cb3c9c767838eb0eda3 Signed-off-by: jh9216.park --- src/modules/dbus_event/certificate_matcher.cc | 118 ++ src/modules/dbus_event/certificate_matcher.hh | 35 + src/modules/dbus_event/dbus_event_module.cc | 1724 ++++++------------------- src/modules/dbus_event/dbus_event_module.hh | 33 +- src/modules/dbus_event/earlier_item.cc | 164 +++ src/modules/dbus_event/earlier_item.hh | 57 + src/modules/dbus_event/esd_list_item.cc | 49 + src/modules/dbus_event/esd_list_item.hh | 56 + src/modules/dbus_event/event_launch.cc | 199 +++ src/modules/dbus_event/event_launch.hh | 69 + src/modules/dbus_event/last_event_item.hh | 63 + 11 files changed, 1238 insertions(+), 1329 deletions(-) create mode 100644 src/modules/dbus_event/certificate_matcher.cc create mode 100644 src/modules/dbus_event/certificate_matcher.hh create mode 100644 src/modules/dbus_event/earlier_item.cc create mode 100644 src/modules/dbus_event/earlier_item.hh create mode 100644 src/modules/dbus_event/esd_list_item.cc create mode 100644 src/modules/dbus_event/esd_list_item.hh create mode 100644 src/modules/dbus_event/event_launch.cc create mode 100644 src/modules/dbus_event/event_launch.hh create mode 100644 src/modules/dbus_event/last_event_item.hh diff --git a/src/modules/dbus_event/certificate_matcher.cc b/src/modules/dbus_event/certificate_matcher.cc new file mode 100644 index 0000000..2902b96 --- /dev/null +++ b/src/modules/dbus_event/certificate_matcher.cc @@ -0,0 +1,118 @@ +/* + * Copyright (c) 2022 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 "certificate_matcher.hh" + +#include +#include +#include +#include +#include + +#include + +#include "log.hh" + +namespace esd::module { + +int CertificateMatcher::Match(uid_t uid, const std::string& appid, + uid_t from_uid, const std::string& from_appid) { + _D("uid(%d), appid(%s), from_uid(%d), from_appid(%s)", uid, appid.c_str(), + from_uid, from_appid.c_str()); + + pkgmgrinfo_cert_compare_result_type_e res; + int ret = pkgmgrinfo_pkginfo_compare_usr_app_cert_info(appid.c_str(), + from_appid.c_str(), from_uid, &res); + if (ret < 0) { + _E("failed to check certificate"); + return ES_R_ERROR; + } + + if (res != PMINFO_CERT_COMPARE_MATCH) { + _D("certificat not match (%s)", appid.c_str()); + return ES_R_EINVAL; + } + + return ES_R_OK; +} + +bool CertificateMatcher::IsPlatformCert(const std::string& pkgid, uid_t uid) { + _D("Checking if %s has a platform certification", pkgid.c_str()); + + int r; + const char *cert_value; + pkgmgrinfo_certinfo_h certinfo; + CertSvcInstance instance; + CertSvcCertificate certificate; + CertSvcVisibility visibility = CERTSVC_VISIBILITY_PUBLIC; + + r = pkgmgrinfo_pkginfo_create_certinfo(&certinfo); + std::unique_ptr::type, + decltype(pkgmgrinfo_pkginfo_destroy_certinfo)*> + certinfo_auto(certinfo, pkgmgrinfo_pkginfo_destroy_certinfo); + if (r != PMINFO_R_OK) { + _E("Failed to create certinfo"); + return false; + } + + r = pkgmgrinfo_pkginfo_load_certinfo(pkgid.c_str(), certinfo, uid); + if (r != PMINFO_R_OK) { + _E("Failed to load certinfo"); + return false; + } + + r = pkgmgrinfo_pkginfo_get_cert_value(certinfo, + PMINFO_DISTRIBUTOR_ROOT_CERT, &cert_value); + if (r != PMINFO_R_OK || cert_value == nullptr) { + _E("Failed to get cert value"); + return false; + } + + r = certsvc_instance_new(&instance); + if (r != CERTSVC_SUCCESS) { + _E("certsvc_instance_new() is failed."); + return false; + } + + r = certsvc_certificate_new_from_memory(instance, + reinterpret_cast(cert_value), + strlen(cert_value), + CERTSVC_FORM_DER_BASE64, + &certificate); + if (r != CERTSVC_SUCCESS) { + _E("certsvc_certificate_new_from_memory() is failed."); + certsvc_instance_free(instance); + return false; + } + + r = certsvc_certificate_get_visibility(certificate, &visibility); + if (r != CERTSVC_SUCCESS) + _E("certsvc_certificate_get_visibility() is failed."); + + certsvc_instance_free(instance); + certsvc_certificate_free(certificate); + + _D("visibility is %d", visibility); + if (visibility & CERTSVC_VISIBILITY_PLATFORM) { + _D("%s has a platform certification", pkgid.c_str()); + return true; + } + + return false; +} + +} // namespace esd::module diff --git a/src/modules/dbus_event/certificate_matcher.hh b/src/modules/dbus_event/certificate_matcher.hh new file mode 100644 index 0000000..6e0dc5d --- /dev/null +++ b/src/modules/dbus_event/certificate_matcher.hh @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2022 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 EVENTSYSTEM_MODULES_DBUS_EVENT_CERTIFICATE_MATCHER_HH_ +#define EVENTSYSTEM_MODULES_DBUS_EVENT_CERTIFICATE_MATCHER_HH_ + +#include + +#include + +namespace esd::module { + +class CertificateMatcher { + public: + static int Match(uid_t uid, const std::string& appid, + uid_t from_uid, const std::string& from_appid); + static bool IsPlatformCert(const std::string& pkgid, uid_t uid); +}; + +} // namespace esd::module + +#endif // EVENTSYSTEM_MODULES_DBUS_EVENT_CERTIFICATE_MATCHER_HH_ diff --git a/src/modules/dbus_event/dbus_event_module.cc b/src/modules/dbus_event/dbus_event_module.cc index b825c5e..9469ae9 100644 --- a/src/modules/dbus_event/dbus_event_module.cc +++ b/src/modules/dbus_event/dbus_event_module.cc @@ -1,3 +1,21 @@ +/* + * Copyright (c) 2022 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 "dbus_event_module.hh" + #include #include #include @@ -6,7 +24,6 @@ #include #include #include -#include #include #include #include @@ -14,17 +31,14 @@ #include #include #include -#include -#include -#include "log.hh" -#include "dbus_event_module.hh" +#include + +#include +#include +#include -#define FREE_AND_NULL(ptr) do { \ - if (ptr) { \ - free((void *)ptr); \ - ptr = NULL; \ - } \ -} while (0) +#include "certificate_matcher.hh" +#include "log.hh" namespace { @@ -32,22 +46,22 @@ namespace { constexpr const char ESD_BOOT_COMPLETED[] = "/tmp/esd_ready"; constexpr const char SYSTEMD_DBUS_DEST[] = "org.freedesktop.systemd1"; -constexpr const char SYSTEMD_DBUS_IFACE_MANAGER[] = "org.freedesktop.systemd1.Manager"; +constexpr const char SYSTEMD_DBUS_IFACE_MANAGER[] = + "org.freedesktop.systemd1.Manager"; constexpr const char SYSTEMD_DBUS_PATH[] = "/org/freedesktop/systemd1"; -constexpr const char SYSTEMD_DBUS_SIGNAL_STARTUP_FINISHED[] = "StartupFinished"; -constexpr const char SYSTEMD_DBUS_SIGNAL_USER_STARTUP_FINISHED[] = "UserSessionStartupFinished"; +constexpr const char SYSTEMD_DBUS_SIGNAL_STARTUP_FINISHED[] = + "StartupFinished"; +constexpr const char SYSTEMD_DBUS_SIGNAL_USER_STARTUP_FINISHED[] = + "UserSessionStartupFinished"; -#define DEFAULT_USER tzplatform_getuid(TZ_SYS_DEFAULT_USER) -#define GLOBAL_USER tzplatform_getuid(TZ_SYS_GLOBALAPP_USER) -constexpr const int ROOT_USER = 0; +const uid_t DEFAULT_USER = tzplatform_getuid(TZ_SYS_DEFAULT_USER); +const uid_t GLOBAL_USER = tzplatform_getuid(TZ_SYS_GLOBALAPP_USER); constexpr const char SYS_EVENT_NAME_PREFIX[] = "tizen.system.event"; constexpr const char SYS_EVENT_OBJ_PATH[] = "/tizen/system/event"; constexpr const char REQUEST_LAST_DATA[] = "request_last_data"; -GHashTable *event_launch_table; /* table of events for launch_on_event*/ - -const char *event_launch_support_list[] = { +const std::set event_launch_support_list = { SYS_EVENT_BATTERY_CHARGER_STATUS, SYS_EVENT_USB_STATUS, SYS_EVENT_EARJACK_STATUS, @@ -56,212 +70,35 @@ const char *event_launch_support_list[] = { SYS_EVENT_WIFI_STATE }; -const char *earlier_event_list[] = { - SYS_EVENT_ESD_STATUS, - SYS_EVENT_LOW_MEMORY, - SYS_EVENT_BOOT_COMPLETED, - SYS_EVENT_SYSTEM_SHUTDOWN, - SYS_EVENT_BATTERY_CHARGER_STATUS -}; - -GHashTable *earlier_event_table; /* table of events for earlier_data */ - -typedef struct __earlier_table_item { - char *event_name; - guint reg_id; - bundle *earlier_data; /* event-data from earlier occurrence */ -} earlier_item; - -GHashTable *user_last_event_table; /* table of user events for last data */ - -struct __last_event_item { - char *key; - char *app_id; - char *event_name; - char *own_name; - uid_t uid; -}; +std::map> trusted_busname_table; -GHashTable *trusted_busname_table; /* table of dbus bus-names for trusted user-event */ +std::map> event_launch_table; -typedef struct __trusted_busname_item { - char *app_id; - char *bus_name; - int pid; - uid_t uid; -} trusted_item; - -typedef struct __eventlaunch_item_param { - char *app_id; -} eventlaunch_item_param_s; - -enum trusted_result { - TRUSTED_UNKNOWN, - TRUSTED_ALLOWED, - TRUSTED_DENIED, -}; - -typedef struct esd_list_item { - char *pkg_id; - char *app_id; - int trusted_info; - uid_t uid; -} esd_list_item_s; +struct AppCtrlData { + AppCtrlData(std::string_view appid, std::string_view pkgid, uid_t uid) + : appid_(appid), pkgid_(pkgid), uid_(uid) { + } -typedef struct __event_launch_table_item { - char *event_name; - char *package_name; /* just for passing pointer to app-list removal func */ - GList *app_list_evtlaunch; /* app-list for on-event-launch */ - guint reg_id; - uid_t uid; -} event_launch_item; - -enum __pkg_event_type { - UNKNOWN = 0, - INSTALL, - UNINSTALL, - UPDATE, + std::string_view appid_; + std::string_view pkgid_; + uid_t uid_; }; -typedef struct __pkgmgr_event { - int type; - char *pkgid; -} esd_pkgmgr_event; - -typedef struct __esd_event_param { - char *event_name; - bundle *event_data; - uid_t sender_uid; - char *sender_appid; - bool is_user_event; - bool trusted; - void *user_data; -} esd_event_param; - -typedef struct esd_info { - pkgmgr_client *client; -} esd_info_s; -static esd_info_s s_info; - -typedef struct __esd_appctrl_cb_data { - char *appid; - char *pkgid; - uid_t uid; -} esd_appctrl_cb_data; - -void __esd_event_handler(char *event_name, bundle *data, void *user_data); int __esd_add_appinfo_handler(const pkgmgrinfo_appinfo_h handle, void *data); -void free_saved_event(struct __last_event_item *item) { - if (!item) - return; - - free(item->event_name); - free(item->own_name); - free(item->app_id); - free(item); -} - -int __esd_check_earlier_support(const char *event_name) { - int i = 0; - int size = sizeof(earlier_event_list)/sizeof(*earlier_event_list); - - for (i = 0; i < size; i++) { - if (strcmp(earlier_event_list[i], event_name) == 0) - return true; - } - - return false; -} - -bool __esd_check_platform_cert(const char *pkgid, uid_t uid) { - _D("Checking if %s has a platform certification", pkgid); - - int r; - const char *cert_value; - pkgmgrinfo_certinfo_h certinfo; - CertSvcInstance instance; - CertSvcCertificate certificate; - CertSvcVisibility visibility = CERTSVC_VISIBILITY_PUBLIC; - - r = pkgmgrinfo_pkginfo_create_certinfo(&certinfo); - if (r != PMINFO_R_OK) { - _E("Failed to create certinfo"); - return false; - } - - r = pkgmgrinfo_pkginfo_load_certinfo(pkgid, certinfo, uid); - if (r != PMINFO_R_OK) { - _E("Failed to load certinfo"); - pkgmgrinfo_pkginfo_destroy_certinfo(certinfo); - return false; - } - - r = pkgmgrinfo_pkginfo_get_cert_value(certinfo, - PMINFO_DISTRIBUTOR_ROOT_CERT, &cert_value); - if (r != PMINFO_R_OK || cert_value == NULL) { - _E("Failed to get cert value"); - pkgmgrinfo_pkginfo_destroy_certinfo(certinfo); - return false; - } - - r = certsvc_instance_new(&instance); - if (r != CERTSVC_SUCCESS) { - _E("certsvc_instance_new() is failed."); - pkgmgrinfo_pkginfo_destroy_certinfo(certinfo); - return false; - } - - r = certsvc_certificate_new_from_memory(instance, - (const unsigned char *)cert_value, - strlen(cert_value), - CERTSVC_FORM_DER_BASE64, - &certificate); - if (r != CERTSVC_SUCCESS) { - _E("certsvc_certificate_new_from_memory() is failed."); - pkgmgrinfo_pkginfo_destroy_certinfo(certinfo); - certsvc_instance_free(instance); - return false; - } - - r = certsvc_certificate_get_visibility(certificate, &visibility); - if (r != CERTSVC_SUCCESS) - _E("certsvc_certificate_get_visibility() is failed."); - - pkgmgrinfo_pkginfo_destroy_certinfo(certinfo); - certsvc_certificate_free(certificate); - certsvc_instance_free(instance); - - _D("visibility is %d", visibility); - if (visibility & CERTSVC_VISIBILITY_PLATFORM) { - _D("%s has a platform certification", pkgid); - return true; - } - - return false; -} - -int __esd_check_event_launch_support(const char *event_name) { - int i = 0; - int size = sizeof(event_launch_support_list)/sizeof(*event_launch_support_list); - - for (i = 0; i < size; i++) { - if (strcmp(event_launch_support_list[i], event_name) == 0) - return true; - } - - return false; -} - -int __get_sender_unixinfo(GDBusConnection *conn, const char *sender_name, const char *type) { - GDBusMessage *msg = NULL; - GDBusMessage *reply = NULL; - GError *err = NULL; +int __get_sender_unixinfo(GDBusConnection *conn, const char *sender_name, + const char *type) { + GDBusMessage *msg = nullptr; + GDBusMessage *reply = nullptr; + GError *err = nullptr; GVariant *body; int ret = -1; unsigned int value; - msg = g_dbus_message_new_method_call("org.freedesktop.DBus", "/org/freedesktop/DBus", + msg = g_dbus_message_new_method_call( + "org.freedesktop.DBus", "/org/freedesktop/DBus", "org.freedesktop.DBus", type); if (!msg) { _E("Can't allocate new method call"); @@ -270,10 +107,10 @@ int __get_sender_unixinfo(GDBusConnection *conn, const char *sender_name, const g_dbus_message_set_body(msg, g_variant_new("(s)", sender_name)); reply = g_dbus_connection_send_message_with_reply_sync(conn, msg, - G_DBUS_SEND_MESSAGE_FLAGS_NONE, -1, NULL, NULL, &err); + G_DBUS_SEND_MESSAGE_FLAGS_NONE, -1, nullptr, nullptr, &err); if (!reply) { - if (err != NULL) { + if (err != nullptr) { _E("Failed to get info [%s]", err->message); g_error_free(err); } @@ -282,7 +119,7 @@ int __get_sender_unixinfo(GDBusConnection *conn, const char *sender_name, const body = g_dbus_message_get_body(reply); g_variant_get(body, "(u)", &value); - ret = (int)value; + ret = static_cast(value); out: if (msg) @@ -296,7 +133,8 @@ out: int __get_sender_pid(GDBusConnection *conn, const char *sender_name) { int pid = 0; - pid = __get_sender_unixinfo(conn, sender_name, "GetConnectionUnixProcessID"); + pid = __get_sender_unixinfo(conn, sender_name, + "GetConnectionUnixProcessID"); if (pid < 0) { _E("failed to get pid"); pid = 0; @@ -319,26 +157,6 @@ int __get_sender_uid(GDBusConnection *conn, const char *sender_name) { return uid; } -int __esd_check_certificate_match(uid_t uid, const char *app_id, uid_t from_uid, const char *from_appid) { - pkgmgrinfo_cert_compare_result_type_e res; - int ret = 0; - - _D("uid(%d), app_id(%s), from_uid(%d), from_appid(%s)", uid, app_id, from_uid, from_appid); - - ret = pkgmgrinfo_pkginfo_compare_usr_app_cert_info(app_id, from_appid, from_uid, &res); - if (ret < 0) { - _E("failed to check certificate"); - return ES_R_ERROR; - } - - if (res != PMINFO_CERT_COMPARE_MATCH) { - _D("certificat not match (%s)", app_id); - return ES_R_EINVAL; - } - - return ES_R_OK; -} - bool __esd_check_application_validation(uid_t uid, const char *appid) { int ret = 0; pkgmgrinfo_appinfo_h handle; @@ -356,67 +174,27 @@ bool __esd_check_application_validation(uid_t uid, const char *appid) { } void __esd_trusted_busname_print_items() { - GHashTableIter iter; - gpointer key; - gpointer value; - - g_hash_table_iter_init(&iter, trusted_busname_table); - - while (g_hash_table_iter_next(&iter, &key, &value)) { - trusted_item *item = (trusted_item *)value; - if (item) - _D("uid(%d), appid(%s), pid(%d), busname(%s)", item->uid, item->app_id, item->pid, item->bus_name); + for (const auto& i : trusted_busname_table) { + auto [bus_name, pid, uid] = i.second; + _D("uid(%d), appid(%s), pid(%d), busname(%s)", uid, + i.first.c_str(), pid, bus_name.c_str()); } } -int __esd_trusted_busname_add_item(uid_t uid, const char *appid, const char *busname, int pid) { - char *app_id = NULL; - char *bus_name = NULL; - trusted_item *item = NULL; - trusted_item *new_item; - - app_id = strdup(appid); - if (app_id == NULL) { - _E("out of memory"); - return ES_R_ENOMEM; - } - - bus_name = strdup(busname); - if (bus_name == NULL) { - _E("out of memory"); - FREE_AND_NULL(app_id); - return ES_R_ENOMEM; - } - - item = (trusted_item *)g_hash_table_lookup(trusted_busname_table, app_id); - - if (item && item->bus_name && strcmp(item->bus_name, bus_name) == 0 && - (item->uid == uid)) { - _D("already exist (%s, %s)", app_id, bus_name); - FREE_AND_NULL(app_id); - FREE_AND_NULL(bus_name); - } else { - new_item = (trusted_item *)calloc(1, sizeof(trusted_item)); - if (new_item == NULL) { - _E("memory alloc failed"); - FREE_AND_NULL(app_id); - FREE_AND_NULL(bus_name); - return ES_R_ENOMEM; - } - new_item->uid = uid; - new_item->app_id = app_id; - new_item->bus_name = bus_name; - new_item->pid = pid; - g_hash_table_insert(trusted_busname_table, new_item->app_id, new_item); - _D("added busname(%s)", new_item->bus_name); +int __esd_trusted_busname_add_item(uid_t uid, const char *appid, + const char *busname, int pid) { + if (trusted_busname_table.find(appid) != trusted_busname_table.end()) { + _D("already exist (%s, %s)", appid, busname); + return ES_R_OK; } + trusted_busname_table[appid] = std::make_tuple(busname, pid, uid); return ES_R_OK; } int __esd_check_trusted_events(GDBusConnection *conn, const char *list_name) { GVariant *result; - GError *error = NULL; + GError *error = nullptr; GVariantIter *iter; gchar *str; char tmp_appid[128] = {0, }; @@ -426,10 +204,10 @@ int __esd_check_trusted_events(GDBusConnection *conn, const char *list_name) { result = g_dbus_connection_call_sync(conn, "org.freedesktop.DBus", "/org/freedesktop/DBus", "org.freedesktop.DBus", - list_name, NULL, G_VARIANT_TYPE("(as)"), G_DBUS_CALL_FLAGS_NONE, - -1, NULL, &error); + list_name, nullptr, G_VARIANT_TYPE("(as)"), G_DBUS_CALL_FLAGS_NONE, + -1, nullptr, &error); - if (result == NULL) { + if (result == nullptr) { _E("get (%s) error(%s)", list_name, error->message); g_error_free(error); return ES_R_ERROR; @@ -437,17 +215,17 @@ int __esd_check_trusted_events(GDBusConnection *conn, const char *list_name) { g_variant_get(result, "(as)", &iter); while (g_variant_iter_loop(iter, "s", &str)) { - if (!strstr((const char *)str, "event.busname.session")) + if (!strstr(reinterpret_cast(str), "event.busname.session")) continue; _D("list(%s), name(%s)", list_name, str); - pid = __get_sender_pid(conn, (const char *)str); + pid = __get_sender_pid(conn, reinterpret_cast(str)); if (pid <= 0) { _E("failed to get pid(%d)", pid); continue; } - uid = __get_sender_uid(conn, (const char *)str); + uid = __get_sender_uid(conn, reinterpret_cast(str)); if (uid < 0) { _E("failed to get uid(%d)", uid); continue; @@ -455,7 +233,8 @@ int __esd_check_trusted_events(GDBusConnection *conn, const char *list_name) { _D("uid(%d)", uid); memset(tmp_appid, 0, sizeof(tmp_appid)); - ret = aul_app_get_appid_bypid_for_uid(pid, tmp_appid, sizeof(tmp_appid), (uid_t)uid); + ret = aul_app_get_appid_bypid_for_uid(pid, tmp_appid, sizeof(tmp_appid), + (uid_t)uid); if (ret != AUL_R_OK) { _E("failed to get appid by pid(%d)", pid); continue; @@ -464,7 +243,8 @@ int __esd_check_trusted_events(GDBusConnection *conn, const char *list_name) { _D("appid(%s)", tmp_appid); if (__esd_check_application_validation((uid_t)uid, tmp_appid)) { _D("add to table"); - ret = __esd_trusted_busname_add_item((uid_t)uid, tmp_appid, (const char *)str, pid); + ret = __esd_trusted_busname_add_item((uid_t)uid, tmp_appid, + reinterpret_cast(str), pid); if (ret < 0) _E("failed to add item"); } @@ -475,461 +255,62 @@ int __esd_check_trusted_events(GDBusConnection *conn, const char *list_name) { return ES_R_OK; } -void __esd_print_appid_with_eventid(gpointer data, gpointer user_data) { - esd_list_item_s *item = (esd_list_item_s *)data; - char *event_name = (char *)user_data; - - _D("event_name(%s)-uid(%d)-app_id(%s)-pkg_id(%s)", event_name, item->uid, item->app_id, item->pkg_id); -} - -void __esd_print_interested_event(gpointer data, gpointer user_data) { - event_launch_item *el_item = (event_launch_item *)data; - char *event_name = (char *)el_item->event_name; - _D("event_name = (%s)", event_name); - g_list_foreach(el_item->app_list_evtlaunch, __esd_print_appid_with_eventid, event_name); -} - void __esd_launch_table_print_items() { - GHashTableIter iter; - gpointer key; - gpointer value; - - g_hash_table_iter_init(&iter, event_launch_table); - - while (g_hash_table_iter_next(&iter, &key, &value)) - __esd_print_interested_event(value, NULL); -} - -int __esd_find_compare_by_list_item(gconstpointer data, gconstpointer user_data) { - esd_list_item_s *item_1 = (esd_list_item_s *)user_data; - esd_list_item_s *item_2 = (esd_list_item_s *)data; - - return (item_1->uid != item_2->uid) | - strcmp(item_1->app_id, item_2->app_id) | - strcmp(item_1->pkg_id, item_2->pkg_id); + for (auto& i : event_launch_table) + i.second->Print(i.first); } -int __esd_add_list_item(uid_t uid, event_launch_item *el_item, - const char *app_id, const char *pkg_id) { - esd_list_item_s *item_of_list = NULL; +int __esd_add_launch_item(uid_t uid, const char* event_name, + const char* appid, const char* pkgid) { + auto it = event_launch_table.find(event_name); - item_of_list = (esd_list_item_s *)calloc(1, sizeof(esd_list_item_s)); - if (item_of_list == NULL) { - _E("out_of_memory"); - return ES_R_ENOMEM; - } - item_of_list->uid = uid; - item_of_list->app_id = (char *)app_id; - item_of_list->pkg_id = (char *)pkg_id; - item_of_list->trusted_info = TRUSTED_UNKNOWN; - el_item->app_list_evtlaunch = - g_list_append(el_item->app_list_evtlaunch, item_of_list); - - return ES_R_OK; -} - -int __esd_add_launch_item(uid_t uid, const char *event_name, - const char *appid, const char *pkgid) { - GList *app_list = NULL; - guint subscription_id = 0; - char *app_id = NULL; - char *pkg_id = NULL; - esd_list_item_s *item_of_list = NULL; - event_launch_item *eli; - event_launch_item *el_item = - (event_launch_item *)g_hash_table_lookup(event_launch_table, event_name); - - if (el_item) { - item_of_list = (esd_list_item_s *)calloc(1, sizeof(esd_list_item_s)); - if (item_of_list == NULL) { - _E("memory alloc failed"); - return ES_R_ENOMEM; - } - item_of_list->uid = uid; - item_of_list->app_id = (char *)appid; - item_of_list->pkg_id = (char *)pkgid; - - app_list = g_list_find_custom(el_item->app_list_evtlaunch, - item_of_list, (GCompareFunc)__esd_find_compare_by_list_item); - free(item_of_list); - if (app_list == NULL) { - _D("add new item (list item only)"); - app_id = strdup((char *)appid); - if (!app_id) { - _E("out_of_memory"); - return ES_R_ENOMEM; - } - pkg_id = strdup((char *)pkgid); - if (!pkg_id) { - _E("out_of_memory"); - FREE_AND_NULL(app_id); - return ES_R_ENOMEM; - } - if (__esd_add_list_item(uid, el_item, app_id, pkg_id) < 0) { - _E("failed to add list item"); - FREE_AND_NULL(app_id); - FREE_AND_NULL(pkg_id); - return ES_R_ERROR; - } - } + if (it != event_launch_table.end()) { + auto& el = (*it).second; + el->GetAppListEventLaunch().push_back(esd::module::EsdListItem( + pkgid, appid, uid)); } else { _D("add new item (all)"); - eli = (event_launch_item *)calloc(1, sizeof(event_launch_item)); - if (!eli) { - _E("memory alloc failed"); - return ES_R_ENOMEM; - } - - eli->event_name = strdup(event_name); - if (!eli->event_name) { - _E("out_of_memory"); - FREE_AND_NULL(eli); - return ES_R_ENOMEM; - } - - app_id = strdup((char *)appid); - if (!app_id) { - _E("out_of_memory"); - FREE_AND_NULL(eli->event_name); - FREE_AND_NULL(eli); - return ES_R_ENOMEM; - } - - pkg_id = strdup((char *)pkgid); - if (!pkg_id) { - _E("out_of_memory"); - FREE_AND_NULL(app_id); - FREE_AND_NULL(eli->event_name); - FREE_AND_NULL(eli); - return ES_R_ENOMEM; - } - - if (__esd_add_list_item(uid, eli, app_id, pkg_id) < 0) { - _E("failed to add list item"); - FREE_AND_NULL(app_id); - FREE_AND_NULL(pkg_id); - FREE_AND_NULL(eli->event_name); - FREE_AND_NULL(eli); - return ES_R_ERROR; - } - - g_hash_table_insert(event_launch_table, eli->event_name, eli); - - eventsystem_register_event(eli->event_name, &subscription_id, - (eventsystem_handler)__esd_event_handler, NULL); - if (subscription_id == 0) { + auto el = std::make_unique(pkgid, appid, uid); + if (!el->Register(event_name)) { _E("signal subscription error, event_name(%s), app_id(%s)", - eli->event_name, app_id); + event_name, appid); return ES_R_ERROR; - } else { - eli->reg_id = subscription_id; } - } - - return ES_R_OK; -} - -void __esd_remove_all_private_usr_app_list(gpointer data, gpointer user_data) { - esd_list_item_s *item = (esd_list_item_s *)data; - event_launch_item *eli = (event_launch_item *)user_data; - - if (item->uid != GLOBAL_USER && !strcmp(eli->package_name, item->pkg_id)) { - _D("uid(%d), app_id(%s), pkg_id(%s)", item->uid, item->app_id, eli->package_name); - eli->app_list_evtlaunch = g_list_remove_all(eli->app_list_evtlaunch, data); - } -} - -int __esd_launch_table_remove_private_usr_items() { - GHashTableIter iter; - gpointer key; - gpointer value; - event_launch_item *eli = NULL; - GList *first_list = NULL; - - g_hash_table_iter_init(&iter, event_launch_table); - while (g_hash_table_iter_next(&iter, &key, &value)) { - eli = (event_launch_item *)value; - g_list_foreach(eli->app_list_evtlaunch, __esd_remove_all_private_usr_app_list, eli); - - first_list = g_list_first(eli->app_list_evtlaunch); - if (first_list == NULL) { - if (eli->reg_id) - eventsystem_unregister_event(eli->reg_id); - - g_hash_table_iter_remove(&iter); - } + event_launch_table[event_name] = std::move(el); } return ES_R_OK; } -void __esd_remove_app_list(gpointer data, gpointer user_data) { - bool skip = false; - esd_list_item_s *item = (esd_list_item_s *)data; - event_launch_item *eli = (event_launch_item *)user_data; - - if (eli->uid != GLOBAL_USER && eli->uid != item->uid) - skip = true; - - if (!skip && !strcmp(eli->package_name, item->pkg_id)) { - _D("pkg_id(%s), app_id(%s)", eli->package_name, item->app_id); - eli->app_list_evtlaunch = - g_list_remove_all(eli->app_list_evtlaunch, data); - } -} - -int __esd_remove_launch_item(uid_t uid, gpointer data, const char *pkg_id) { - event_launch_item *eli = (event_launch_item *)data; - GList *first_list = NULL; - - eli->uid = uid; - eli->package_name = (char *)pkg_id; - g_list_foreach(eli->app_list_evtlaunch, __esd_remove_app_list, eli); - - first_list = g_list_first(eli->app_list_evtlaunch); - if (first_list == NULL) { - if (eli->reg_id) - eventsystem_unregister_event(eli->reg_id); - - return ES_R_REMOVE; - } - +int __esd_launch_table_remove_private_usr_items() { + event_launch_table.clear(); return ES_R_OK; } int __esd_launch_table_remove_items(uid_t uid, const char *pkg_id) { - GHashTableIter iter; - gpointer key; - gpointer value; - - g_hash_table_iter_init(&iter, event_launch_table); - - while (g_hash_table_iter_next(&iter, &key, &value)) { - if (__esd_remove_launch_item(uid, value, pkg_id) == ES_R_REMOVE) { - _D("remove item itself"); - g_hash_table_iter_remove(&iter); + for (auto& i : event_launch_table) { + if (uid != GLOBAL_USER && i.second->GetUid() == uid && + i.second->GetPackageName() == pkg_id) { + event_launch_table.erase(i.first); + break; } } return ES_R_OK; } -void __esd_event_launch_with_appid(gpointer data, gpointer user_data) { - esd_list_item_s *item = (esd_list_item_s *)data; - uid_t uid = item->uid; - char *app_id = item->app_id; - esd_event_param *eep = (esd_event_param *)user_data; - static unsigned int req_id; - int pid; - char event_uri[1024]; - bundle *b; - int ret; - - _D("launch_on_event: app_id(%s), event_name(%s), uid(%d), is_user(%d), trusted(%d)", - app_id, eep->event_name, uid, eep->is_user_event, eep->trusted); - - if (eep->is_user_event && eep->trusted) { - if (item->trusted_info == TRUSTED_UNKNOWN) { - ret = __esd_check_certificate_match(uid, app_id, eep->sender_uid, eep->sender_appid); - if (ret == ES_R_EINVAL) { - item->trusted_info = TRUSTED_DENIED; - return; - } else if (ret == ES_R_ERROR) { - return; - } else { - item->trusted_info = TRUSTED_ALLOWED; - } - } else if (item->trusted_info == TRUSTED_DENIED) { - return; - } - } - - if (!aul_app_is_running_for_uid(app_id, uid)) { - b = bundle_dup(eep->event_data); - if (eep->is_user_event) - snprintf(event_uri, sizeof(event_uri), "%s%s", USER_EVENT_NAME_PREFIX, eep->event_name); - else - snprintf(event_uri, sizeof(event_uri), "%s%s", SYSTEM_EVENT_NAME_PREFIX, eep->event_name); - - appsvc_set_operation(b, APPSVC_OPERATION_LAUNCH_ON_EVENT); - appsvc_set_uri(b, event_uri); - appsvc_set_appid(b, app_id); - - pid = aul_svc_run_service_async_for_uid(b, req_id++, NULL, eep->user_data, uid); - _D("uid(%d), pid(%d)", uid, pid); - - bundle_free(b); - } else { - _D("already is running or launch failed"); - } -} - -void __esd_check_event_launch_with_eventid(gpointer data, gpointer user_data) { - event_launch_item *el_item = (event_launch_item *)data; - esd_event_param *eep = (esd_event_param *)user_data; - - if (strcmp(eep->event_name, (char *)el_item->event_name) == 0) { - g_list_foreach(el_item->app_list_evtlaunch, - __esd_event_launch_with_appid, user_data); - } -} - -void __esd_launch_event_handler(char *event_name, bundle *data, - const bool is_user_event, gboolean trusted, - const uid_t sender_uid, char *sender_appid, void *user_data) { - const char *val; - const char *msg_type; - const char *msg_id; - esd_event_param *eep; - event_launch_item *el_item; - - _D("event_name(%s)", event_name); - - el_item = (event_launch_item *)g_hash_table_lookup(event_launch_table, event_name); - if (el_item == NULL) - return; - - if (el_item->app_list_evtlaunch != NULL) { - if (is_user_event == false) { - if (strcmp(SYS_EVENT_BATTERY_CHARGER_STATUS, event_name) == 0) { - val = bundle_get_val(data, EVT_KEY_BATTERY_CHARGER_STATUS); - _D("charger val(%s)", val); - if (val && (strcmp(EVT_VAL_BATTERY_CHARGER_CONNECTED, val) != 0)) - return; - } else if (strcmp(SYS_EVENT_USB_STATUS, event_name) == 0) { - val = bundle_get_val(data, EVT_KEY_USB_STATUS); - _D("usb val(%s)", val); - if (val && (strcmp(EVT_VAL_USB_CONNECTED, val) != 0)) - return; - } else if (strcmp(SYS_EVENT_EARJACK_STATUS, event_name) == 0) { - val = bundle_get_val(data, EVT_KEY_EARJACK_STATUS); - _D("earjack val(%s)", val); - if (val && (strcmp(EVT_VAL_EARJACK_CONNECTED, val) != 0)) - return; - } else if (strcmp(SYS_EVENT_INCOMMING_MSG, event_name) == 0) { - msg_type = bundle_get_val(data, EVT_KEY_MSG_TYPE); - _D("msg_type(%s)", msg_type); - if (msg_type == NULL) - return; - - msg_id = bundle_get_val(data, EVT_KEY_MSG_ID); - _D("msg_id(%s)", msg_id); - if (msg_id == NULL) - return; - } else if (strcmp(SYS_EVENT_WIFI_STATE, event_name) == 0) { - val = bundle_get_val(data, EVT_KEY_WIFI_STATE); - if (val == NULL) - return; - _D("wifi_state(%s)", val); - if (strcmp(EVT_VAL_WIFI_CONNECTED, val) != 0) - return; - } - } - - eep = (esd_event_param *)calloc(1, sizeof(esd_event_param)); - if (!eep) { - _E("memory alloc failed"); - return; - } - eep->event_name = event_name; - eep->event_data = data; - eep->sender_uid = sender_uid; - eep->sender_appid = sender_appid; - eep->is_user_event = is_user_event; - eep->trusted = (bool)trusted; - eep->user_data = (void *)user_data; - __esd_check_event_launch_with_eventid(el_item, eep); - free(eep); - } -} - -void __esd_print_earlier_event(gpointer data, gpointer user_data) { - earlier_item *item = (earlier_item *)data; - char *event_name = (char *)item->event_name; - const char *val; - - _D("event_name = (%s)", event_name); - - if (strcmp(event_name, SYS_EVENT_BOOT_COMPLETED) == 0) { - if (item->earlier_data) { - val = bundle_get_val(item->earlier_data, EVT_KEY_BOOT_COMPLETED); - _D("boot_completed(%s)", val); - } - } else if (strcmp(event_name, SYS_EVENT_SYSTEM_SHUTDOWN) == 0) { - if (item->earlier_data) { - val = bundle_get_val(item->earlier_data, EVT_KEY_SYSTEM_SHUTDOWN); - _D("shutdown(%s)", val); - } - } else if (strcmp(event_name, SYS_EVENT_LOW_MEMORY) == 0) { - if (item->earlier_data) { - val = bundle_get_val(item->earlier_data, EVT_KEY_LOW_MEMORY); - _D("low_memory(%s)", val); - } - } else if (strcmp(event_name, SYS_EVENT_BATTERY_CHARGER_STATUS) == 0) { - if (item->earlier_data) { - val = bundle_get_val(item->earlier_data, EVT_KEY_BATTERY_CHARGER_STATUS); - _D("charger_status(%s)", val); - } - } -} - -void __esd_earlier_table_print_items() { - GHashTableIter iter; - gpointer key; - gpointer value; - - g_hash_table_iter_init(&iter, earlier_event_table); - - while (g_hash_table_iter_next(&iter, &key, &value)) - __esd_print_earlier_event(value, NULL); -} - -void __esd_earlier_event_handler(char *event_name, bundle *data, void *user_data) { - earlier_item *item; - _D("event_name(%s)", event_name); - - item = (earlier_item *)g_hash_table_lookup(earlier_event_table, event_name); - if (item) { - /* update earlier value */ - if (item->earlier_data != NULL) - bundle_free(item->earlier_data); - - item->earlier_data = bundle_dup(data); - } -} - -void __esd_event_handler(char *event_name, bundle *data, void *user_data) { - _D("event_name(%s)", event_name); - - if (__esd_check_earlier_support(event_name)) - __esd_earlier_event_handler(event_name, data, user_data); - - if (__esd_check_event_launch_support(event_name)) - __esd_launch_event_handler(event_name, data, - false, TRUE, ROOT_USER, NULL, user_data); -} - -void __esd_trusted_busname_remove_item(char *bus_name) { - GHashTableIter iter; - gpointer key; - gpointer value; - trusted_item *item; - - g_hash_table_iter_init(&iter, trusted_busname_table); - - while (g_hash_table_iter_next(&iter, &key, &value)) { - item = (trusted_item *)value; - if (item) { - if (strcmp(bus_name, item->bus_name) == 0) { - _D("remove trusted busname item(%s, %s)", item->app_id, item->bus_name); - FREE_AND_NULL(item->app_id); - FREE_AND_NULL(item->bus_name); - FREE_AND_NULL(item); - g_hash_table_iter_remove(&iter); - - __esd_trusted_busname_print_items(); - } +void __esd_trusted_busname_remove_item(const char* bus_name) { + for (auto it = trusted_busname_table.begin(); + it != trusted_busname_table.end();) { + auto [bus_name_org, pid, uid] = (*it).second; + if (bus_name_org == bus_name) { + _D("remove trusted busname item(%s, %s)", (*it).first.c_str(), + bus_name_org.c_str()); + it = trusted_busname_table.erase(it); + __esd_trusted_busname_print_items(); + } else { + ++it; } } } @@ -938,9 +319,9 @@ void __esd_filter_name_owner_changed(GDBusConnection *connection, const gchar *sender_name, const gchar *object_path, const gchar *interface_name, const gchar *signal_name, GVariant *parameters, gpointer user_data) { - char *name = NULL; - char *old_owner = NULL; - char *new_owner = NULL; + char *name = nullptr; + char *old_owner = nullptr; + char *new_owner = nullptr; int old_len = 0; int new_len = 0; @@ -967,8 +348,9 @@ int __esd_dbus_name_monitor(GDBusConnection *connection) { name_owner_changed_id = g_dbus_connection_signal_subscribe(connection, "org.freedesktop.DBus", "org.freedesktop.DBus", - "NameOwnerChanged", "/org/freedesktop/DBus", NULL, G_DBUS_SIGNAL_FLAGS_NONE, - __esd_filter_name_owner_changed, NULL, NULL); + "NameOwnerChanged", "/org/freedesktop/DBus", nullptr, + G_DBUS_SIGNAL_FLAGS_NONE, + __esd_filter_name_owner_changed, nullptr, nullptr); _I("name_owner_changed_id(%d)", name_owner_changed_id); @@ -977,7 +359,7 @@ int __esd_dbus_name_monitor(GDBusConnection *connection) { int __esd_get_user_items(uid_t uid) { int ret = 0; - pkgmgrinfo_appinfo_filter_h handle = NULL; + pkgmgrinfo_appinfo_filter_h handle = nullptr; _I("get user items for uid(%d)", uid); /* reset user's item */ @@ -1051,7 +433,8 @@ void __esd_signal_handler(GDBusConnection *connection, static GDBusNodeInfo *introspection_data; -int __esd_get_appid_by_pid_for_uid(int pid, uid_t uid, char *app_id, int buf_size) { +int __esd_get_appid_by_pid_for_uid(int pid, uid_t uid, char *app_id, + int buf_size) { int retval = ES_R_OK; int ret = 0; @@ -1099,57 +482,6 @@ int __esd_get_appid_by_pid(int pid, char *app_id, int buf_size) { return ret; } -int check_user_event_sender_valid(const char *event_name, const char *app_id) { - char *valid_name = NULL; - char *temp_name = NULL; - char *tmp = NULL; - int retval = ES_R_OK; - int len = 0; - int valid_name_len = 0; - - temp_name = strdup(event_name); - if (temp_name == NULL) { - _E("out of memory"); - return ES_R_ENOMEM; - } - - tmp = strrchr(temp_name, '.'); - if (tmp == NULL || strlen(tmp) == 0) { - _E("invalid event name"); - FREE_AND_NULL(temp_name); - return ES_R_EINVAL; - } - len = strlen(tmp); - if (len <= 1 || len > 128) { - _E("invalid length(%d) of user-defined name", len); - FREE_AND_NULL(temp_name); - return ES_R_EINVAL; - } - *tmp = '\0'; - - _D("app_id(%s), len(%zu)", app_id, strlen(app_id)); - - valid_name_len = strlen(USER_EVENT_NAME_PREFIX) + strlen(app_id) + 1; - valid_name = (char *)calloc(1, valid_name_len); - if (valid_name == NULL) { - _E("memory alloc failed"); - FREE_AND_NULL(temp_name); - return ES_R_ENOMEM; - } - snprintf(valid_name, valid_name_len, "%s%s", USER_EVENT_NAME_PREFIX, app_id); - _D("valid_name(%s)", valid_name); - - if (strcmp(temp_name, valid_name) != 0) { - _E("appid misamatch"); - retval = ES_R_EINVAL; - } - - FREE_AND_NULL(temp_name); - FREE_AND_NULL(valid_name); - - return retval; -} - static const GDBusInterfaceVTable interface_vtable = { esd::module::DbusEventModule::HandleMethodCallCb, nullptr, @@ -1163,42 +495,42 @@ void __esd_on_bus_acquired(GDBusConnection *connection, guint reg_id = 0; guint boot_id = 0; guint user_boot_id = 0; - GError *error = NULL; + GError *error = nullptr; reg_id = g_dbus_connection_register_object(connection, ESD_OBJECT_PATH, introspection_data->interfaces[0], &interface_vtable, - user_data, NULL, &error); + user_data, nullptr, &error); if (reg_id == 0) { _E("g_dbus_connection_register_object error(%s)", error->message); g_error_free(error); } boot_id = g_dbus_connection_signal_subscribe(connection, - NULL, + nullptr, SYSTEMD_DBUS_IFACE_MANAGER, SYSTEMD_DBUS_SIGNAL_STARTUP_FINISHED, SYSTEMD_DBUS_PATH, - NULL, + nullptr, G_DBUS_SIGNAL_FLAGS_NONE, __esd_signal_handler, - NULL, - NULL); + nullptr, + nullptr); if (boot_id == 0) _E("g_dbus_connection_signal_subscribe() is failed."); user_boot_id = g_dbus_connection_signal_subscribe(connection, - NULL, + nullptr, SYSTEMD_DBUS_IFACE_MANAGER, SYSTEMD_DBUS_SIGNAL_USER_STARTUP_FINISHED, SYSTEMD_DBUS_PATH, - NULL, + nullptr, G_DBUS_SIGNAL_FLAGS_NONE, __esd_signal_handler, - NULL, - NULL); + nullptr, + nullptr); if (user_boot_id == 0) _E("g_dbus_connection_signal_subscribe() is failed."); @@ -1221,9 +553,7 @@ void __esd_on_name_acquired(GDBusConnection *connection, mod->SetVconfHandler(); __esd_trusted_busname_print_items(); - __esd_get_user_items(DEFAULT_USER); - __esd_dbus_name_monitor(connection); } @@ -1231,80 +561,64 @@ void __esd_on_name_lost(GDBusConnection *connection, const gchar *name, gpointer user_data) { } -void __esd_pkgmgr_event_free(esd_pkgmgr_event *pkg_event) { - pkg_event->type = UNKNOWN; - if (pkg_event->pkgid) { - free(pkg_event->pkgid); - pkg_event->pkgid = NULL; - } -} - -int __esd_appcontrol_cb(const char *operation, - const char *uri, const char *mime, void *data) { - esd_appctrl_cb_data *cb_data = (esd_appctrl_cb_data *)data; - char *appid = NULL; - char *pkgid = NULL; - char *event_name = NULL; - uid_t uid = 0; - - if (cb_data == NULL) { +int __esd_appcontrol_cb(const char* operation, + const char* uri, const char* mime, void* data) { + if (data == nullptr) { _E("invalid data"); return 0; } - appid = cb_data->appid; - pkgid = cb_data->pkgid; - uid = cb_data->uid; + + AppCtrlData* cb_data = reinterpret_cast(data); + std::string_view appid = cb_data->appid_; + std::string_view pkgid = cb_data->pkgid_; + std::string event_name; + uid_t uid = cb_data->uid_; _D("uid(%d), appid(%s), pkgid(%s), operation(%s), uri(%s), mime(%s)", - uid, appid, pkgid, operation, uri, mime); + uid, appid.data(), pkgid.data(), operation, uri, mime); if (!strcmp(operation, APPSVC_OPERATION_LAUNCH_ON_EVENT)) { - if (uri && !strncmp(uri, SYSTEM_EVENT_NAME_PREFIX, strlen(SYSTEM_EVENT_NAME_PREFIX))) { - event_name = strdup(&uri[8]); - if (event_name) { - _D("appid(%s), event_name(%s)", appid, event_name); - if (!__esd_check_event_launch_support(event_name)) - _E("failed to add item (not support event)"); - else if (!esd::module::PrivilegeChecker::AppHasPrivilege(uid, std::string(appid), std::string(event_name))) - _E("failed to add item (no privilege)"); - else if (__esd_add_launch_item(uid, event_name, appid, pkgid)) - _E("failed to add item"); - - } else { - _E("out of memory"); + if (uri && !strncmp(uri, SYSTEM_EVENT_NAME_PREFIX, + strlen(SYSTEM_EVENT_NAME_PREFIX))) { + event_name = &uri[8]; + _D("appid(%s), event_name(%s)", appid.data(), event_name.c_str()); + if (event_launch_support_list.find(event_name) == + event_launch_support_list.end()) { + _E("failed to add item (not support event)"); + } else if (!esd::module::PrivilegeChecker::AppHasPrivilege( + uid, std::string(appid), event_name)) { + _E("failed to add item (no privilege)"); + } else if (__esd_add_launch_item(uid, event_name.c_str(), appid.data(), + pkgid.data())) { + _E("failed to add item"); } - } else if (uri && !strncmp(uri, USER_EVENT_NAME_PREFIX, strlen(USER_EVENT_NAME_PREFIX))) { - event_name = strdup(uri); - if (event_name) { - _D("appid(%s), event_name(%s)", appid, event_name); - if (__esd_check_platform_cert(pkgid, uid)) { - if (__esd_add_launch_item(uid, event_name, appid, pkgid)) - _E("failed to add item"); + } else if (uri && !strncmp(uri, USER_EVENT_NAME_PREFIX, + strlen(USER_EVENT_NAME_PREFIX))) { + event_name = uri; + _D("appid(%s), event_name(%s)", appid.data(), event_name.c_str()); + if (esd::module::CertificateMatcher::IsPlatformCert(std::string(pkgid), + uid)) { + if (__esd_add_launch_item(uid, event_name.c_str(), appid.data(), + pkgid.data())) { + _E("failed to add item"); } - } else { - _E("out of memory"); } } - FREE_AND_NULL(event_name); } return 0; } int __esd_add_appinfo_handler(const pkgmgrinfo_appinfo_h handle, void *data) { - char *appid = NULL; - char *pkgid = NULL; - int ret = 0; - uid_t *p_uid = NULL; - - if (data == NULL) { + if (data == nullptr) { _E("invalid data"); return ES_R_ERROR; } - p_uid = (uid_t *)data; - - ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgid); + char* appid = nullptr; + char* pkgid = nullptr; + uid_t* p_uid = reinterpret_cast(data); + int ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgid); if (ret < 0) { _E("failed to get appid"); return ES_R_ERROR; @@ -1316,33 +630,9 @@ int __esd_add_appinfo_handler(const pkgmgrinfo_appinfo_h handle, void *data) { return ES_R_ERROR; } - esd_appctrl_cb_data *cb_data = (esd_appctrl_cb_data *)calloc(1, sizeof(esd_appctrl_cb_data)); - - if (cb_data == NULL) { - _E("memory alloc failed"); - return ES_R_ENOMEM; - } - cb_data->appid = strdup(appid); - if (cb_data->appid == NULL) { - _E("out_of_memory"); - FREE_AND_NULL(cb_data); - return ES_R_ENOMEM; - } - cb_data->pkgid = strdup(pkgid); - if (cb_data->pkgid == NULL) { - _E("out_of_memory"); - FREE_AND_NULL(cb_data->appid); - FREE_AND_NULL(cb_data); - return ES_R_ENOMEM; - } - cb_data->uid = *p_uid; - - ret = pkgmgrinfo_appinfo_foreach_appcontrol(handle, - (pkgmgrinfo_app_control_list_cb)__esd_appcontrol_cb, cb_data); - - FREE_AND_NULL(cb_data->pkgid); - FREE_AND_NULL(cb_data->appid); - FREE_AND_NULL(cb_data); + AppCtrlData cb_data(appid, pkgid, *p_uid); + ret = pkgmgrinfo_appinfo_foreach_appcontrol(handle, __esd_appcontrol_cb, + &cb_data); if (ret < 0) { _E("failed to get appcontrol info"); @@ -1352,77 +642,6 @@ int __esd_add_appinfo_handler(const pkgmgrinfo_appinfo_h handle, void *data) { return ES_R_OK; } -int __esd_pkgmgr_event_callback(uid_t target_uid, int req_id, - const char *pkg_type, const char *pkgid, const char *key, - const char *val, const void *pmsg, void *data) { - esd_pkgmgr_event *pkg_event = (esd_pkgmgr_event *)data; - pkgmgrinfo_pkginfo_h handle = NULL; - int ret = 0; - - _D("target_uid(%d), req_id(%d), pkg_type(%s), pkgid(%s), key(%s), val(%s)", - target_uid, req_id, pkg_type, pkgid, key, val); - - if (strncmp(key, "start", strlen(key)) == 0) { - if (strcmp(val, "install") == 0) { - _D("install start"); - pkg_event->type = INSTALL; - } else if (strcmp(val, "uninstall") == 0) { - _D("unistall start"); - pkg_event->type = UNINSTALL; - } else if (strcmp(val, "update") == 0) { - _D("update start"); - pkg_event->type = UPDATE; - } else { - _D("val(%s) start", val); - __esd_pkgmgr_event_free(pkg_event); - } - } else if (strcmp(key, "end") == 0 && strcmp(val, "ok") == 0) { - if (pkg_event->type == INSTALL || pkg_event->type == UPDATE) { - _D("install end (ok)"); - ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgid, target_uid, &handle); - if (ret < 0) { - _E("failed to get pkginfo"); - __esd_pkgmgr_event_free(pkg_event); - return 0; - } - ret = pkgmgrinfo_appinfo_get_usr_list(handle, - PMINFO_SVC_APP, __esd_add_appinfo_handler, &target_uid, target_uid); - if (ret < 0) { - _E("failed to get appinfo"); - pkgmgrinfo_pkginfo_destroy_pkginfo(handle); - __esd_pkgmgr_event_free(pkg_event); - return 0; - } - ret = pkgmgrinfo_pkginfo_destroy_pkginfo(handle); - if (ret < 0) { - _E("failed to destroy pkginfo"); - __esd_pkgmgr_event_free(pkg_event); - return 0; - } - } else if (pkg_event->type == UNINSTALL) { - _D("uninstall end (ok)"); - __esd_launch_table_remove_items(target_uid, pkgid); - __esd_launch_table_print_items(); - } - __esd_pkgmgr_event_free(pkg_event); - } else if (strcmp(key, "end") == 0 && strcmp(val, "fail") == 0) { - _E("pkg_event(%d) falied", pkg_event->type); - __esd_pkgmgr_event_free(pkg_event); - } else { - if (strcmp(key, "install_percent") != 0) - __esd_pkgmgr_event_free(pkg_event); - } - - return 0; -} - -void __esd_remove_esd_list_item(gpointer data, gpointer user_data) { - esd_list_item_s *item = (esd_list_item_s *)data; - - free(item->app_id); - free(item->pkg_id); -} - } // namespace namespace esd::module { @@ -1439,115 +658,44 @@ void DbusEventModule::Shutdown() { } bool DbusEventModule::Init() { - int req_id = 0; - int ret = 0; - pkgmgr_client *client; - esd_pkgmgr_event *pkg_event; - auto checker = std::make_unique(); - client = pkgmgr_client_new(PC_LISTENING); - if (client == NULL) { + pkgmgr_client_ = pkgmgr_client_new(PC_LISTENING); + if (pkgmgr_client_ == nullptr) { _E("set pkgmgr client failed"); return false; } - pkg_event = (esd_pkgmgr_event *)calloc(1, sizeof(esd_pkgmgr_event)); - if (pkg_event == NULL) { - _E("memory alloc failed"); - ret = pkgmgr_client_free(client); - if (ret != PKGMGR_R_OK) - _E("pkgmgr_client_free failed(%d)", ret); - return false; - } - - req_id = pkgmgr_client_listen_status(client, __esd_pkgmgr_event_callback, pkg_event); + int req_id = pkgmgr_client_listen_status(pkgmgr_client_, + PkgmgrEventCb, this); if (req_id < 0) { _E("pkgmgr client listen failed"); - ret = pkgmgr_client_free(client); + int ret = pkgmgr_client_free(pkgmgr_client_); + pkgmgr_client_ = nullptr; if (ret != PKGMGR_R_OK) _E("pkgmgr_client_free failed(%d)", ret); - free(pkg_event); return false; } - s_info.client = client; checker_ = std::move(checker); - _I("esd init done"); return true; } void DbusEventModule::Fini() { - gpointer key; - gpointer value; - GHashTableIter iter; - trusted_item *item; - event_launch_item *el_item; - int ret = 0; - earlier_item *er_item; - _D("esd finalize"); - if (trusted_busname_table) { - g_hash_table_iter_init(&iter, trusted_busname_table); - while (g_hash_table_iter_next(&iter, &key, &value)) { - item = (trusted_item *)value; - if (item) { - free(item->app_id); - free(item->bus_name); - free(item); - } else { - _E("item is null"); - } - g_hash_table_iter_remove(&iter); - } - g_hash_table_unref(trusted_busname_table); - } - - if (earlier_event_table) { - g_hash_table_iter_init(&iter, earlier_event_table); - while (g_hash_table_iter_next(&iter, &key, &value)) { - er_item = (earlier_item *)value; - if (er_item) { - eventsystem_unregister_event(er_item->reg_id); - free(er_item->event_name); - bundle_free(er_item->earlier_data); - free(er_item); - } else { - _E("ealier item is NULL"); - } - g_hash_table_iter_remove(&iter); - } - g_hash_table_unref(earlier_event_table); - } - - g_hash_table_destroy(user_last_event_table); - - if (event_launch_table) { - g_hash_table_iter_init(&iter, event_launch_table); - while (g_hash_table_iter_next(&iter, &key, &value)) { - el_item = (event_launch_item *)value; - if (el_item) { - eventsystem_unregister_event(el_item->reg_id); - free(el_item->event_name); - g_list_foreach(el_item->app_list_evtlaunch, - __esd_remove_esd_list_item, NULL); - g_list_free(el_item->app_list_evtlaunch); - free(el_item); - } else { - _E("item is NULL"); - } - g_hash_table_iter_remove(&iter); - } - g_hash_table_unref(event_launch_table); - } + trusted_busname_table.clear(); + earlier_event_table_.clear(); + user_last_event_table_.clear(); + event_launch_table.clear(); if (introspection_data) g_dbus_node_info_unref(introspection_data); - if (s_info.client) { - ret = pkgmgr_client_free(s_info.client); + if (pkgmgr_client_) { + int ret = pkgmgr_client_free(pkgmgr_client_); + pkgmgr_client_ = nullptr; if (ret != PKGMGR_R_OK) _E("pkgmgr_client_free failed(%d)", ret); } @@ -1557,136 +705,10 @@ void DbusEventModule::Fini() { } bool DbusEventModule::BeforeLoop() { - int ret = 0; - GError *error = NULL; - guint owner_id = 0; - - guint subscription_id = 0; - int i; - int size; - char *event_name; - int fd; - int val; - int status; - int charger_status; - int charge_now; - earlier_item *item; - - earlier_event_table = g_hash_table_new(g_str_hash, g_str_equal); - user_last_event_table = g_hash_table_new_full(g_str_hash, - g_str_equal, NULL, (GDestroyNotify)free_saved_event); - - _I("register events for earlier_data"); - size = sizeof(earlier_event_list)/sizeof(*earlier_event_list); - for (i = 0; i < size; i++) { - event_name = (char *)earlier_event_list[i]; - _I("event_name(%s)", event_name); - - item = (earlier_item *)calloc(1, sizeof(earlier_item)); - if (item == NULL) { - _E("memery alloc failed"); - return false; - } - item->event_name = strdup(event_name); - if (item->event_name == NULL) { - _E("out of memory"); - free(item); - return false; - } - - /* set initial data */ - if (strcmp(event_name, SYS_EVENT_BOOT_COMPLETED) == 0) { - fd = open(ESD_BOOT_COMPLETED, O_RDONLY); - if (fd < 0) { - _D("open file error(%d)", fd); - } else { - item->earlier_data = bundle_create(); - bundle_add_str(item->earlier_data, EVT_KEY_BOOT_COMPLETED, - EVT_VAL_BOOT_COMPLETED_TRUE); - close(fd); - } - } else if (strcmp(event_name, SYS_EVENT_SYSTEM_SHUTDOWN) == 0) { - ret = vconf_get_int(VCONFKEY_SYSMAN_POWER_OFF_STATUS, &val); - if (ret != VCONF_OK) { - _E("failed to get power_off status (%d)", ret); - } else { - if (val == VCONFKEY_SYSMAN_POWER_OFF_DIRECT || - val == VCONFKEY_SYSMAN_POWER_OFF_RESTART) { - /* power-off requested */ - item->earlier_data = bundle_create(); - bundle_add_str(item->earlier_data, EVT_KEY_SYSTEM_SHUTDOWN, - EVT_VAL_SYSTEM_SHUTDOWN_TRUE); - } - } - } else if (strcmp(event_name, SYS_EVENT_LOW_MEMORY) == 0) { - ret = vconf_get_int(VCONFKEY_SYSMAN_LOW_MEMORY, &status); - if (ret != VCONF_OK) { - _E("failed to get low_memory status (%d)", ret); - } else { - item->earlier_data = bundle_create(); - if (status == VCONFKEY_SYSMAN_LOW_MEMORY_SOFT_WARNING) - bundle_add_str(item->earlier_data, EVT_KEY_LOW_MEMORY, - EVT_VAL_MEMORY_SOFT_WARNING); - else if (status == VCONFKEY_SYSMAN_LOW_MEMORY_HARD_WARNING) - bundle_add_str(item->earlier_data, EVT_KEY_LOW_MEMORY, - EVT_VAL_MEMORY_HARD_WARNING); - else - bundle_add_str(item->earlier_data, EVT_KEY_LOW_MEMORY, - EVT_VAL_MEMORY_NORMAL); - } - } else if (strcmp(event_name, SYS_EVENT_BATTERY_CHARGER_STATUS) == 0) { - ret = vconf_get_int(VCONFKEY_SYSMAN_CHARGER_STATUS, &charger_status); - if (ret != VCONF_OK) { - _E("failed to get charger_status (%d)", ret); - } else { - ret = vconf_get_int(VCONFKEY_SYSMAN_BATTERY_CHARGE_NOW, &charge_now); - if (ret != VCONF_OK) - _E("failed to get charge_now (%d)", ret); - } - - if (ret == VCONF_OK) { - item->earlier_data = bundle_create(); - if (charger_status == VCONFKEY_SYSMAN_CHARGER_CONNECTED) { - if (charge_now == 0) { - bundle_add_str(item->earlier_data, - EVT_KEY_BATTERY_CHARGER_STATUS, - EVT_VAL_BATTERY_CHARGER_DISCHARGING); - } else { - bundle_add_str(item->earlier_data, - EVT_KEY_BATTERY_CHARGER_STATUS, - EVT_VAL_BATTERY_CHARGER_CHARGING); - } - } else { - bundle_add_str(item->earlier_data, - EVT_KEY_BATTERY_CHARGER_STATUS, - EVT_VAL_BATTERY_CHARGER_DISCONNECTED); - } - } - } - - eventsystem_register_event(event_name, &subscription_id, - (eventsystem_handler)__esd_event_handler, NULL); - if (subscription_id == 0) { - _E("signal subscription error, event_name(%s)", event_name); - if (item->earlier_data) - bundle_free(item->earlier_data); - free(item->event_name); - free(item); - - return false; - } else { - item->reg_id = subscription_id; - } + GError* error = nullptr; + earlier_event_table_ = EarlierItem::CreateMap(); - g_hash_table_insert(earlier_event_table, event_name, item); - } - - __esd_earlier_table_print_items(); - - event_launch_table = g_hash_table_new(g_str_hash, g_str_equal); - trusted_busname_table = g_hash_table_new(g_str_hash, g_str_equal); - - /* gdbus setup for method call */ + event_launch_table.clear(); introspection_data = g_dbus_node_info_new_for_xml(INTROSPECTION_XML, &error); if (!introspection_data) { _E("g_dbus_node_info_new_for_xml error(%s)", error->message); @@ -1694,13 +716,14 @@ bool DbusEventModule::BeforeLoop() { return false; } - owner_id = g_bus_own_name(G_BUS_TYPE_SYSTEM, + guint owner_id = g_bus_own_name(G_BUS_TYPE_SYSTEM, ESD_BUS_NAME, G_BUS_NAME_OWNER_FLAGS_NONE, __esd_on_bus_acquired, __esd_on_name_acquired, __esd_on_name_lost, - this, NULL); + this, nullptr); + if (!owner_id) { _E("g_bus_own_name error"); g_dbus_node_info_unref(introspection_data); @@ -1708,7 +731,6 @@ bool DbusEventModule::BeforeLoop() { } _I("esd before_loop done"); - return true; } @@ -1722,22 +744,32 @@ void DbusEventModule::HandleMethodCallCb(GDBusConnection *connection, const gchar *interface_name, const gchar *method_name, GVariant *parameters, GDBusMethodInvocation *invocation, gpointer user_data) { - static std::map> dispatcher = { - { "CheckSenderValidation", &DbusEventModule::CheckSenderValidMethodCall }, - { "GetTrustedPeerList", &DbusEventModule::GetTrustedPeerMethodCall }, - { "SetupTrustedPeer", &DbusEventModule::SetupTrustedPeerMethodCall }, - { "CheckPrivilegeValidation", &DbusEventModule::CheckPrivilegeValidMethodCall }, - { "CheckUserSendValidation", &DbusEventModule::CheckSendEventValidMethodCall }, - { "GetEarlierData", &DbusEventModule::GetEarlierDataMethodCall }, - { "KeepLastData", &DbusEventModule::KeepLastDataMethodCall }, - { "CheckLastData", &DbusEventModule::CheckLastDataMethodCall }, - { "LaunchOnEventFromUserEvent", &DbusEventModule::LaunchOnEventFromUserEventMethodCall }, + { "CheckSenderValidation", + &DbusEventModule::CheckSenderValidMethodCall }, + { "GetTrustedPeerList", + &DbusEventModule::GetTrustedPeerMethodCall }, + { "SetupTrustedPeer", + &DbusEventModule::SetupTrustedPeerMethodCall }, + { "CheckPrivilegeValidation", + &DbusEventModule::CheckPrivilegeValidMethodCall }, + { "CheckUserSendValidation", + &DbusEventModule::CheckSendEventValidMethodCall }, + { "GetEarlierData", + &DbusEventModule::GetEarlierDataMethodCall }, + { "KeepLastData", + &DbusEventModule::KeepLastDataMethodCall }, + { "CheckLastData", + &DbusEventModule::CheckLastDataMethodCall }, + { "LaunchOnEventFromUserEvent", + &DbusEventModule::LaunchOnEventFromUserEventMethodCall }, { "CionGetUuid", &DbusEventModule::GetUuidMethodCall }, - { "CionSetDisplayName", &DbusEventModule::SetDisplayNameMethodCall }, - { "CionGetDisplayName", &DbusEventModule::GetDisplayNameMethodCall }, + { "CionSetDisplayName", + &DbusEventModule::SetDisplayNameMethodCall }, + { "CionGetDisplayName", + &DbusEventModule::GetDisplayNameMethodCall }, { "CionSetEnabled", &DbusEventModule::SetEnabledMethodCall }, { "CionGetEnabled", &DbusEventModule::GetEnabledMethodCall } }; @@ -1761,12 +793,12 @@ std::string DbusEventModule::GetSenderAppId(GDBusConnection* connection, return app_id; } -void DbusEventModule::CheckSendEventValidMethodCall(GDBusConnection* connection, - const gchar* sender, GVariant* parameters, +void DbusEventModule::CheckSendEventValidMethodCall( + GDBusConnection* connection, const gchar* sender, GVariant* parameters, GDBusMethodInvocation* invocation) { - GVariant *param = NULL; + GVariant *param = nullptr; int result = 0; - char *event_name = NULL; + char *event_name = nullptr; char app_id[128] = {0, }; int sender_pid = 0; uid_t sender_uid = 0; @@ -1776,10 +808,11 @@ void DbusEventModule::CheckSendEventValidMethodCall(GDBusConnection* connection, sender_pid = __get_sender_pid(connection, sender); sender_uid = (uid_t)__get_sender_uid(connection, sender); - if (__esd_get_appid_by_pid_for_uid(sender_pid, sender_uid, app_id, sizeof(app_id)) < 0) { + if (__esd_get_appid_by_pid_for_uid(sender_pid, sender_uid, app_id, + sizeof(app_id)) < 0) { result = ES_R_ERROR; } else { - if (check_user_event_sender_valid(event_name, app_id) < 0) { + if (CheckUserEventSenderValid(event_name, app_id) < 0) { _E("invalid sender"); result = ES_R_EINVAL; } else { @@ -1795,9 +828,9 @@ void DbusEventModule::CheckSendEventValidMethodCall(GDBusConnection* connection, void DbusEventModule::CheckSenderValidMethodCall(GDBusConnection* connection, const gchar* sender, GVariant* parameters, GDBusMethodInvocation* invocation) { - GVariant *param = NULL; + GVariant *param = nullptr; int result = 0; - char *event_name = NULL; + char *event_name = nullptr; char app_id[128] = {0, }; int event_sender_pid = 0; uid_t sender_uid = 0; @@ -1806,10 +839,11 @@ void DbusEventModule::CheckSenderValidMethodCall(GDBusConnection* connection, _D("event_sender_pid(%d), event_name(%s)", event_sender_pid, event_name); sender_uid = (uid_t)__get_sender_uid(connection, sender); - if (__esd_get_appid_by_pid_for_uid(event_sender_pid, sender_uid, app_id, sizeof(app_id)) < 0) { + if (__esd_get_appid_by_pid_for_uid(event_sender_pid, sender_uid, app_id, + sizeof(app_id)) < 0) { result = ES_R_ERROR; } else { - if (check_user_event_sender_valid(event_name, app_id) < 0) { + if (CheckUserEventSenderValid(event_name, app_id) < 0) { _E("invalid sender"); result = ES_R_EINVAL; } else { @@ -1825,44 +859,31 @@ void DbusEventModule::CheckSenderValidMethodCall(GDBusConnection* connection, void DbusEventModule::GetTrustedPeerMethodCall(GDBusConnection* connection, const gchar* sender, GVariant* parameters, GDBusMethodInvocation* invocation) { - GVariant *param = NULL; + GVariant *param = nullptr; int result = 0; - GVariantBuilder *builder = NULL; - GHashTableIter iter; - gpointer key, value; - char *event_name = NULL; + GVariantBuilder *builder = nullptr; + char *event_name = nullptr; char app_id[128] = {0, }; - int sender_pid = 0; - uid_t sender_uid = 0; - int ret = 0; - uid_t uid = 0; - char *_appid = NULL; - char *_busname = NULL; - trusted_item *item; g_variant_get(parameters, "(&s)", &event_name); _D("event_name(%s)", event_name); - sender_pid = __get_sender_pid(connection, sender); - sender_uid = (uid_t)__get_sender_uid(connection, sender); - if (__esd_get_appid_by_pid_for_uid(sender_pid, sender_uid, app_id, sizeof(app_id)) < 0) { + int sender_pid = __get_sender_pid(connection, sender); + uid_t sender_uid = (uid_t)__get_sender_uid(connection, sender); + if (__esd_get_appid_by_pid_for_uid(sender_pid, sender_uid, app_id, + sizeof(app_id)) < 0) { result = ES_R_ERROR; } else { builder = g_variant_builder_new(G_VARIANT_TYPE("as")); - g_hash_table_iter_init(&iter, trusted_busname_table); - while (g_hash_table_iter_next(&iter, &key, &value)) { - item = (trusted_item *)value; - uid = item->uid; - _appid = item->app_id; - _busname = item->bus_name; - + for (const auto& i : trusted_busname_table) { + auto [busname, pid, uid] = i.second; if (uid != GLOBAL_USER && uid != sender_uid) continue; - ret = __esd_check_certificate_match(uid, _appid, sender_uid, app_id); + int ret = CertificateMatcher::Match(uid, i.first, sender_uid, app_id); if (ret == ES_R_OK) - g_variant_builder_add(builder, "s", _busname); + g_variant_builder_add(builder, "s", busname.c_str()); } result = 1; @@ -1878,10 +899,10 @@ void DbusEventModule::GetTrustedPeerMethodCall(GDBusConnection* connection, void DbusEventModule::SetupTrustedPeerMethodCall(GDBusConnection* connection, const gchar* sender, GVariant* parameters, GDBusMethodInvocation* invocation) { - GVariant *param = NULL; + GVariant *param = nullptr; int result = 0; - char *event_name = NULL; - char *destination_name = NULL; + char *event_name = nullptr; + char *destination_name = nullptr; char app_id[128] = {0, }; int sender_pid = 0; uid_t sender_uid = 0; @@ -1893,10 +914,12 @@ void DbusEventModule::SetupTrustedPeerMethodCall(GDBusConnection* connection, if (destination_name && destination_name[0] != '\0') { sender_pid = __get_sender_pid(connection, sender); sender_uid = (uid_t)__get_sender_uid(connection, sender); - if (__esd_get_appid_by_pid_for_uid(sender_pid, sender_uid, app_id, sizeof(app_id)) < 0) { + if (__esd_get_appid_by_pid_for_uid(sender_pid, sender_uid, app_id, + sizeof(app_id)) < 0) { result = ES_R_ERROR; } else { - ret = __esd_trusted_busname_add_item(sender_uid, app_id, destination_name, + ret = __esd_trusted_busname_add_item(sender_uid, app_id, + destination_name, sender_pid); if (ret < 0) { _E("failed to add trusted busname item"); @@ -1915,12 +938,12 @@ void DbusEventModule::SetupTrustedPeerMethodCall(GDBusConnection* connection, g_dbus_method_invocation_return_value(invocation, param); } -void DbusEventModule::CheckPrivilegeValidMethodCall(GDBusConnection* connection, - const gchar* sender, GVariant* parameters, +void DbusEventModule::CheckPrivilegeValidMethodCall( + GDBusConnection* connection, const gchar* sender, GVariant* parameters, GDBusMethodInvocation* invocation) { - GVariant *param = NULL; + GVariant *param = nullptr; int result = 1; - char *event_name = NULL; + char *event_name = nullptr; char app_id[128] = {0, }; int sender_pid = 0; uid_t sender_uid = 0; @@ -1933,10 +956,11 @@ void DbusEventModule::CheckPrivilegeValidMethodCall(GDBusConnection* connection, if (!privilege_name.empty()) { sender_pid = __get_sender_pid(connection, sender); sender_uid = (uid_t)__get_sender_uid(connection, sender); - if (__esd_get_appid_by_pid_for_uid(sender_pid, sender_uid, app_id, sizeof(app_id)) < 0) { + if (__esd_get_appid_by_pid_for_uid(sender_pid, sender_uid, app_id, + sizeof(app_id)) < 0) { result = ES_R_ERROR; } else { - if(checker_->Check(connection, sender, sender_pid, privilege_name)) + if (checker_->Check(connection, sender, sender_pid, privilege_name)) result = 1; else result = ES_R_EINVAL; @@ -1951,38 +975,26 @@ void DbusEventModule::CheckPrivilegeValidMethodCall(GDBusConnection* connection, void DbusEventModule::GetEarlierDataMethodCall(GDBusConnection* connection, const gchar* sender, GVariant* parameters, GDBusMethodInvocation* invocation) { - GVariant *param = NULL; + GVariant *param = nullptr; int result = ES_R_ERROR; - char *event_name = NULL; - bundle *b = NULL; - bundle_raw *raw = NULL; - int len = 0; - earlier_item *item; + char* event_name = nullptr; + tizen_base::Bundle b; g_variant_get(parameters, "(&s)", &event_name); _D("event_name(%s)", event_name); - item = (earlier_item *)g_hash_table_lookup(earlier_event_table, event_name); - if (item != NULL) { - if (item->earlier_data) { - b = bundle_dup(item->earlier_data); - bundle_add_str(b, "is_earlier_data", "true"); - result = ES_R_OK; - } + auto it = earlier_event_table_.find(event_name); + if (it != earlier_event_table_.end()) { + b = (*it).second->GetData(); + b.Add("is_earlier_data", "true"); + result = ES_R_OK; } - if (result == ES_R_ERROR) - b = bundle_create(); - - bundle_encode(b, &raw, &len); - bundle_free(b); - - param = g_variant_new("(iis)", result, len, raw); + auto raw = b.ToRaw(); + param = g_variant_new("(iis)", result, raw.second, raw.first.get()); - _D("result(%d), len(%d)", result, len); + _D("result(%d), len(%d)", result, raw.second); g_dbus_method_invocation_return_value(invocation, param); - - bundle_free_encoded_rawdata(&raw); } void DbusEventModule::KeepLastDataMethodCall(GDBusConnection* connection, @@ -1990,20 +1002,20 @@ void DbusEventModule::KeepLastDataMethodCall(GDBusConnection* connection, GDBusMethodInvocation* invocation) { GVariant *param; int result = ES_R_OK; - char *event_name; - char *own_name; - char *key; + char* event_name; + char* own_name; char app_id[128]; int sender_pid; uid_t sender_uid; - struct __last_event_item *item; g_variant_get(parameters, "(&s&s)", &event_name, &own_name); if (!event_name || !own_name) { result = ES_R_ERROR; _E("invalid event_name and own_name"); - goto out; + param = g_variant_new("(i)", result); + g_dbus_method_invocation_return_value(invocation, param); + return; } sender_pid = __get_sender_pid(connection, sender); @@ -2012,62 +1024,44 @@ void DbusEventModule::KeepLastDataMethodCall(GDBusConnection* connection, sizeof(app_id)) < 0) { _E("failed to get appid by pid"); result = ES_R_ERROR; - goto out; - } - - key = (char *)malloc(sizeof(event_name) + 10); - if (!key) { - result = ES_R_ENOMEM; - _E("out of memory"); - goto out; + param = g_variant_new("(i)", result); + g_dbus_method_invocation_return_value(invocation, param); + return; } - snprintf(key, sizeof(event_name) + 10, "%s_%d", event_name, sender_uid); - item = (struct __last_event_item *)g_hash_table_lookup(user_last_event_table, - key); - if (!item) { - item = (struct __last_event_item *)calloc(1, sizeof(*item)); - if (!item) { - result = ES_R_ERROR; - goto out; - } - item->key = key; - item->event_name = strdup(event_name); - item->own_name = strdup(own_name); - item->uid = sender_uid; - item->app_id = strdup(app_id); - g_hash_table_insert(user_last_event_table, - item->key, item); + std::string key(event_name); + key += "_" + std::to_string(sender_uid); + auto it = user_last_event_table_.find(key); + if (it == user_last_event_table_.end()) { + user_last_event_table_[key] = LastEventItem(app_id, event_name, + own_name, sender_uid); } else { - free(item->own_name); - item->own_name = strdup(own_name); + user_last_event_table_[key].SetOwnName(own_name); } -out: param = g_variant_new("(i)", result); - g_dbus_method_invocation_return_value(invocation, param); } void DbusEventModule::CheckLastDataMethodCall(GDBusConnection* connection, const gchar* sender, GVariant* parameters, GDBusMethodInvocation* invocation) { - GVariant *param; + GVariant* param; int result = ES_R_OK; - char *event_name; - char *own_name; - char *key; + char* event_name; + char* own_name; char app_id[128]; int sender_pid; uid_t sender_uid; - struct __last_event_item *item; g_variant_get(parameters, "(&s&s)", &event_name, &own_name); if (!event_name || !own_name) { result = ES_R_ERROR; _E("invalid event_name and own_name"); - goto out; + param = g_variant_new("(i)", result); + g_dbus_method_invocation_return_value(invocation, param); + return; } sender_pid = __get_sender_pid(connection, sender); @@ -2076,37 +1070,34 @@ void DbusEventModule::CheckLastDataMethodCall(GDBusConnection* connection, sizeof(app_id)) < 0) { result = ES_R_ERROR; _E("failed to get appid by pid"); - goto out; - } - - key = (char *)malloc(sizeof(event_name) + 10); - if (!key) { - result = ES_R_ENOMEM; - _E("out of memory"); - goto out; + param = g_variant_new("(i)", result); + g_dbus_method_invocation_return_value(invocation, param); + return; } - snprintf(key, sizeof(event_name) + 10, "%s_%d", event_name, sender_uid); - item = (struct __last_event_item *)g_hash_table_lookup(user_last_event_table, - key); - free(key); - if (item) { + std::string key(event_name); + key += "_" + std::to_string(sender_uid); + auto it = user_last_event_table_.find(key); + if (it != user_last_event_table_.end()) { GVariant *gv; bundle *b; bundle_raw *raw; int len; int ret; - GError *error = NULL; + GError *error = nullptr; b = bundle_create(); if (!b) { result = ES_R_ERROR; - goto out; + param = g_variant_new("(i)", result); + g_dbus_method_invocation_return_value(invocation, param); + return; } + bundle_add_str(b, EVT_KEY_KEPT_EVENT_NAME, event_name); bundle_add_str(b, EVT_KEY_KEPT_OWN_NAME, own_name); - if (__esd_check_certificate_match(item->uid, item->app_id, - sender_uid, app_id) == ES_R_OK) + if (CertificateMatcher::Match((*it).second.GetUid(), + (*it).second.GetAppId(), sender_uid, app_id) == ES_R_OK) bundle_add_str(b, EVT_KEY_KEPT_IS_TRUSTED, "true"); else bundle_add_str(b, EVT_KEY_KEPT_IS_TRUSTED, "false"); @@ -2114,7 +1105,7 @@ void DbusEventModule::CheckLastDataMethodCall(GDBusConnection* connection, bundle_encode(b, &raw, &len); gv = g_variant_new("(us)", len, raw); ret = g_dbus_connection_emit_signal(connection, - item->own_name, + (*it).second.GetOwnName().c_str(), SYS_EVENT_OBJ_PATH, SYS_EVENT_NAME_PREFIX, REQUEST_LAST_DATA, @@ -2128,14 +1119,12 @@ void DbusEventModule::CheckLastDataMethodCall(GDBusConnection* connection, bundle_free(b); } -out: param = g_variant_new("(i)", result); - g_dbus_method_invocation_return_value(invocation, param); } -void DbusEventModule::LaunchOnEventFromUserEventMethodCall(GDBusConnection* connection, - const gchar* sender, GVariant* parameters, +void DbusEventModule::LaunchOnEventFromUserEventMethodCall( + GDBusConnection* connection, const gchar* sender, GVariant* parameters, GDBusMethodInvocation* invocation) { GVariant *param; int result = ES_R_OK; @@ -2146,14 +1135,15 @@ void DbusEventModule::LaunchOnEventFromUserEventMethodCall(GDBusConnection* conn char *event_name; char app_id[128]; char *buf; - bundle *b; g_variant_get(parameters, "(&s&sib)", &event_name, &buf, &len, &trusted); if (!event_name) { result = ES_R_ERROR; _E("invalid event_name"); - goto out; + param = g_variant_new("(i)", result); + g_dbus_method_invocation_return_value(invocation, param); + return; } sender_pid = __get_sender_pid(connection, sender); @@ -2162,24 +1152,19 @@ void DbusEventModule::LaunchOnEventFromUserEventMethodCall(GDBusConnection* conn sizeof(app_id)) < 0) { _E("failed to get appid by pid"); result = ES_R_ERROR; - goto out; + param = g_variant_new("(i)", result); + g_dbus_method_invocation_return_value(invocation, param); + return; } - b = bundle_decode((bundle_raw *)buf, len); - if (b == NULL) { - _E("Out of memory"); - result = ES_R_ENOMEM; - goto out; + tizen_base::Bundle b(buf); + auto it = event_launch_table.find(event_name); + if (it != event_launch_table.end()) { + (*it).second->Notify(event_name, b, true, static_cast(trusted), + sender_uid, app_id); } - __esd_launch_event_handler(event_name, b, true, trusted, - sender_uid, app_id, NULL); - - bundle_free(b); - -out: param = g_variant_new("(i)", result); - g_dbus_method_invocation_return_value(invocation, param); } @@ -2230,8 +1215,9 @@ void DbusEventModule::SetDisplayNameMethodCall(GDBusConnection* connection, g_variant_get(parameters, "(&s&s)", &service_name, &display_name); if (!tools_->GetMethodBroker() - .Invoke( - "Cion.SetDisplayName", app_id, service_name, display_name, ret)) { + .Invoke("Cion.SetDisplayName", app_id, service_name, display_name, + ret)) { result = ES_R_ERROR; } @@ -2296,7 +1282,8 @@ void DbusEventModule::SetEnabledMethodCall(GDBusConnection* connection, if (!tools_->GetMethodBroker() .Invoke( - "Cion.SetEnabled", app_id, service_name, (bool)enabled, ret)) { + "Cion.SetEnabled", app_id, service_name, static_cast(enabled), + ret)) { result = ES_R_ERROR; } @@ -2340,5 +1327,90 @@ void DbusEventModule::GetEnabledMethodCall(GDBusConnection* connection, g_dbus_method_invocation_return_value(invocation, param); } +int DbusEventModule::PkgmgrEventCb(uid_t target_uid, int req_id, + const char *pkg_type, const char *pkgid, const char *key, + const char *val, const void *pmsg, void *data) { + DbusEventModule* obj = reinterpret_cast(data); + pkgmgrinfo_pkginfo_h handle = nullptr; + int ret = 0; + + _D("target_uid(%d), req_id(%d), pkg_type(%s), pkgid(%s), key(%s), val(%s)", + target_uid, req_id, pkg_type, pkgid, key, val); + + if (strncmp(key, "start", strlen(key)) == 0) { + if (strcmp(val, "install") == 0) { + _D("install start"); + obj->pkg_event_type_ = INSTALL; + } else if (strcmp(val, "uninstall") == 0) { + _D("unistall start"); + obj->pkg_event_type_ = UNINSTALL; + } else if (strcmp(val, "update") == 0) { + _D("update start"); + obj->pkg_event_type_ = UPDATE; + } else { + _D("val(%s) start", val); + } + } else if (strcmp(key, "end") == 0 && strcmp(val, "ok") == 0) { + if (obj->pkg_event_type_ == INSTALL || obj->pkg_event_type_ == UPDATE) { + _D("install end (ok)"); + ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgid, target_uid, &handle); + if (ret < 0) { + _E("failed to get pkginfo"); + return 0; + } + + ret = pkgmgrinfo_appinfo_get_usr_list(handle, + PMINFO_SVC_APP, __esd_add_appinfo_handler, &target_uid, target_uid); + if (ret < 0) { + _E("failed to get appinfo"); + pkgmgrinfo_pkginfo_destroy_pkginfo(handle); + return 0; + } + + ret = pkgmgrinfo_pkginfo_destroy_pkginfo(handle); + if (ret < 0) { + _E("failed to destroy pkginfo"); + return 0; + } + } else if (obj->pkg_event_type_ == UNINSTALL) { + _D("uninstall end (ok)"); + __esd_launch_table_remove_items(target_uid, pkgid); + __esd_launch_table_print_items(); + } + } else if (strcmp(key, "end") == 0 && strcmp(val, "fail") == 0) { + _E("pkg_event(%d) falied", obj->pkg_event_type_); + } + + return 0; +} + +int DbusEventModule::CheckUserEventSenderValid(std::string_view event_name, + std::string_view app_id) { + auto it = event_name.rfind('.'); + + if (it == std::string::npos) { + _E("invalid event name"); + return ES_R_EINVAL; + } + + event_name = event_name.substr(0, it); + auto len = event_name.length(); + if (len <= 1 || len > 128) { + _E("invalid length(%d) of user-defined name", len); + return ES_R_EINVAL; + } + + _D("app_id(%s), len(%zu)", app_id.data(), app_id.length()); + + std::string valid_name = USER_EVENT_NAME_PREFIX + std::string(app_id); + _D("valid_name(%s)", valid_name.c_str()); + + if (valid_name != event_name) { + _E("appid misamatch"); + return ES_R_EINVAL; + } + + return ES_R_OK; +} -} // namespace esd::module \ No newline at end of file +} // namespace esd::module diff --git a/src/modules/dbus_event/dbus_event_module.hh b/src/modules/dbus_event/dbus_event_module.hh index 2a8361c..d6661cf 100644 --- a/src/modules/dbus_event/dbus_event_module.hh +++ b/src/modules/dbus_event/dbus_event_module.hh @@ -19,9 +19,17 @@ #include #include +#include -#include +#include +#include +#include +#include +#include "earlier_item.hh" +#include "event_launch.hh" +#include +#include "last_event_item.hh" #include "vconf_event_handler.hh" #include "privilege_checker.hh" @@ -44,13 +52,21 @@ class DbusEventModule : public api::IModule { gpointer user_data); private: + enum PkgEventType { + UNKNOWN = 0, + INSTALL, + UNINSTALL, + UPDATE, + }; + bool Init(); void Fini(); bool BeforeLoop(); void GetUuidMethodCall(GDBusConnection* connection, const gchar* sender, GVariant* parameters, GDBusMethodInvocation* invocation); void SetDisplayNameMethodCall(GDBusConnection* connection, - const gchar* sender, GVariant* parameters, GDBusMethodInvocation* invocation); + const gchar* sender, GVariant* parameters, + GDBusMethodInvocation* invocation); void GetDisplayNameMethodCall(GDBusConnection* connection, const gchar* sender, GVariant* parameters, GDBusMethodInvocation* invocation); @@ -88,13 +104,24 @@ class DbusEventModule : public api::IModule { const gchar* sender, GVariant* parameters, GDBusMethodInvocation* invocation); std::string GetSenderAppId(GDBusConnection* connection, const gchar* sender); + int CheckUserEventSenderValid(std::string_view event_name, + std::string_view app_id); + + static int PkgmgrEventCb(uid_t target_uid, int req_id, const char* pkg_type, + const char* pkgid, const char* key, const char* val, const void* pmsg, + void* data); private: api::ToolBox* tools_ = nullptr; std::unique_ptr vconf_handler_; std::unique_ptr checker_; + std::map> earlier_event_table_; + std::map user_last_event_table_; + pkgmgr_client* pkgmgr_client_ = nullptr; + std::string pkg_event_pkgid_; + int pkg_event_type_; }; } // namespace esd::module -#endif // EVENTSYSTEM_MODULES_DBUS_EVENT_DBUS_EVENT_MODULE_HH_ \ No newline at end of file +#endif // EVENTSYSTEM_MODULES_DBUS_EVENT_DBUS_EVENT_MODULE_HH_ diff --git a/src/modules/dbus_event/earlier_item.cc b/src/modules/dbus_event/earlier_item.cc new file mode 100644 index 0000000..3997a14 --- /dev/null +++ b/src/modules/dbus_event/earlier_item.cc @@ -0,0 +1,164 @@ +/* + * Copyright (c) 2022 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 "earlier_item.hh" + +#include +#include +#include +#include +#include +#include + +#include + +#include "log.hh" + +namespace { + +constexpr const char ESD_BOOT_COMPLETED[] = "/tmp/esd_ready"; + +const std::set earlier_event_list = { + SYS_EVENT_ESD_STATUS, + SYS_EVENT_LOW_MEMORY, + SYS_EVENT_BOOT_COMPLETED, + SYS_EVENT_SYSTEM_SHUTDOWN, + SYS_EVENT_BATTERY_CHARGER_STATUS +}; + +} // namespace + +namespace esd::module { + +EarlierItem::EarlierItem(tizen_base::Bundle data) + : data_(std::move(data)) { +} + +EarlierItem::~EarlierItem() { + if (reg_id_) + eventsystem_unregister_event(reg_id_); +} + +bool EarlierItem::Register(std::string_view event_name) { + if (eventsystem_register_event(event_name.data(), ®_id_, + EsdEventHandlerCb, this) != 0) { + return false; + } + + return true; +} + +const tizen_base::Bundle& EarlierItem::GetData() const { + return data_; +} + +void EarlierItem::OnEvent(const char* event_name, bundle* data) { + _D("event_name(%s)", event_name); + data_ = tizen_base::Bundle(data); +} + +void EarlierItem::EsdEventHandlerCb(const char* event_name, bundle* data, + void* user_data) { + _D("event_name(%s)", event_name); + EarlierItem* ei = reinterpret_cast(user_data); + if (earlier_event_list.find(event_name) != earlier_event_list.end()) + ei->OnEvent(event_name, data); +} + +std::map> EarlierItem::CreateMap() { + int fd; + int ret; + std::map> events; + int val; + int status; + int charger_status; + int charge_now; + + _I("register events for earlier_data"); + for (const auto& event_name : earlier_event_list) { + _I("event_name(%s)", event_name.data()); + + tizen_base::Bundle b; + if (event_name == SYS_EVENT_BOOT_COMPLETED) { + fd = open(ESD_BOOT_COMPLETED, O_RDONLY); + if (fd < 0) { + _D("open file error(%d)", fd); + } else { + b.Add(EVT_KEY_BOOT_COMPLETED, EVT_VAL_BOOT_COMPLETED_TRUE); + close(fd); + } + } else if (event_name == SYS_EVENT_SYSTEM_SHUTDOWN) { + ret = vconf_get_int(VCONFKEY_SYSMAN_POWER_OFF_STATUS, &val); + if (ret != VCONF_OK) { + _E("failed to get power_off status (%d)", ret); + } else { + if (val == VCONFKEY_SYSMAN_POWER_OFF_DIRECT || + val == VCONFKEY_SYSMAN_POWER_OFF_RESTART) { + b.Add(EVT_KEY_SYSTEM_SHUTDOWN, EVT_VAL_SYSTEM_SHUTDOWN_TRUE); + } + } + } else if (event_name == SYS_EVENT_LOW_MEMORY) { + ret = vconf_get_int(VCONFKEY_SYSMAN_LOW_MEMORY, &status); + if (ret != VCONF_OK) { + _E("failed to get low_memory status (%d)", ret); + } else { + if (status == VCONFKEY_SYSMAN_LOW_MEMORY_SOFT_WARNING) + b.Add(EVT_KEY_LOW_MEMORY, EVT_VAL_MEMORY_SOFT_WARNING); + else if (status == VCONFKEY_SYSMAN_LOW_MEMORY_HARD_WARNING) + b.Add(EVT_KEY_LOW_MEMORY, EVT_VAL_MEMORY_HARD_WARNING); + else + b.Add(EVT_KEY_LOW_MEMORY, EVT_VAL_MEMORY_NORMAL); + } + } else if (event_name == SYS_EVENT_BATTERY_CHARGER_STATUS) { + ret = vconf_get_int(VCONFKEY_SYSMAN_CHARGER_STATUS, &charger_status); + if (ret != VCONF_OK) { + _E("failed to get charger_status (%d)", ret); + } else { + ret = vconf_get_int(VCONFKEY_SYSMAN_BATTERY_CHARGE_NOW, &charge_now); + if (ret != VCONF_OK) + _E("failed to get charge_now (%d)", ret); + } + + if (ret == VCONF_OK) { + if (charger_status == VCONFKEY_SYSMAN_CHARGER_CONNECTED) { + if (charge_now == 0) { + b.Add(EVT_KEY_BATTERY_CHARGER_STATUS, + EVT_VAL_BATTERY_CHARGER_DISCHARGING); + } else { + b.Add(EVT_KEY_BATTERY_CHARGER_STATUS, + EVT_VAL_BATTERY_CHARGER_CHARGING); + } + } else { + b.Add(EVT_KEY_BATTERY_CHARGER_STATUS, + EVT_VAL_BATTERY_CHARGER_DISCONNECTED); + } + } + } + + auto ei = std::make_unique(std::move(b)); + if (!ei->Register(event_name)) { + _E("signal subscription error, event_name(%s)", event_name.data()); + continue; + } + + events[std::string(event_name)] = std::move(ei); + } + + return events; +} + +} // namespace esd::module diff --git a/src/modules/dbus_event/earlier_item.hh b/src/modules/dbus_event/earlier_item.hh new file mode 100644 index 0000000..3fbd86b --- /dev/null +++ b/src/modules/dbus_event/earlier_item.hh @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2022 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 EVENTSYSTEM_MODULES_DBUS_EVENT_EARLIER_ITEM_HH_ +#define EVENTSYSTEM_MODULES_DBUS_EVENT_EARLIER_ITEM_HH_ + +#include +#include + +#include +#include +#include +#include +#include +#include + +namespace esd::module { + +class EarlierItem { + public: + EarlierItem() = default; + explicit EarlierItem(tizen_base::Bundle data); + EarlierItem(const EarlierItem&) = delete; + EarlierItem& operator = (const EarlierItem&) = delete; + ~EarlierItem(); + + bool Register(std::string_view event_name); + const tizen_base::Bundle& GetData() const; + + static std::map> CreateMap(); + + private: + void OnEvent(const char* event_name, bundle* data); + static void EsdEventHandlerCb(const char* event_name, bundle* data, + void* user_data); + + private: + guint reg_id_ = 0; + tizen_base::Bundle data_; +}; + +} // namespace esd::module + +#endif // EVENTSYSTEM_MODULES_DBUS_EVENT_EARLIER_ITEM_HH_ diff --git a/src/modules/dbus_event/esd_list_item.cc b/src/modules/dbus_event/esd_list_item.cc new file mode 100644 index 0000000..67be5e1 --- /dev/null +++ b/src/modules/dbus_event/esd_list_item.cc @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2022 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 "esd_list_item.hh" + +#include "log.hh" + +namespace esd::module { + +EsdListItem::EsdListItem(std::string pkg_id, std::string app_id, uid_t uid) + : pkg_id_(std::move(pkg_id)), app_id_(std::move(app_id)), uid_(uid) { +} + +void EsdListItem::Print(const std::string& event_name) { + _D("event_name(%s)-uid(%d)-app_id(%s)-pkg_id(%s)", + event_name.c_str(), uid_, app_id_.c_str(), pkg_id_.c_str()); +} + +const std::string& EsdListItem::GetAppId() const { + return app_id_; +} + +int EsdListItem::GetTrustedInfo() const { + return trusted_info_; +} + +void EsdListItem::SetTrustedInfo(int info) { + trusted_info_ = info; +} + +uid_t EsdListItem::GetUid() const { + return uid_; +} + +} // namespace esd::module diff --git a/src/modules/dbus_event/esd_list_item.hh b/src/modules/dbus_event/esd_list_item.hh new file mode 100644 index 0000000..6f06cdd --- /dev/null +++ b/src/modules/dbus_event/esd_list_item.hh @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2022 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 EVENTSYSTEM_MODULES_DBUS_EVENT_ESD_LIST_ITEM_HH_ +#define EVENTSYSTEM_MODULES_DBUS_EVENT_ESD_LIST_ITEM_HH_ + +#include + +#include +#include +#include +#include +#include + +namespace esd::module { + +class EsdListItem { + public: + enum TrustedResult { + TRUSTED_UNKNOWN, + TRUSTED_ALLOWED, + TRUSTED_DENIED, + }; + + EsdListItem() = default; + EsdListItem(std::string pkg_id, std::string app_id, uid_t uid); + + void Print(const std::string& event_name); + const std::string& GetAppId() const; + int GetTrustedInfo() const; + void SetTrustedInfo(int info); + uid_t GetUid() const; + + private: + std::string pkg_id_; + std::string app_id_; + int trusted_info_ = TRUSTED_UNKNOWN; + uid_t uid_ = 0; +}; + +} // namespace esd::module + +#endif // EVENTSYSTEM_MODULES_DBUS_EVENT_ESD_LIST_ITEM_HH_ diff --git a/src/modules/dbus_event/event_launch.cc b/src/modules/dbus_event/event_launch.cc new file mode 100644 index 0000000..0df70a4 --- /dev/null +++ b/src/modules/dbus_event/event_launch.cc @@ -0,0 +1,199 @@ +/* + * Copyright (c) 2022 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 "event_launch.hh" + +#include +#include +#include +#include +#include + +#include + +#include "certificate_matcher.hh" +#include "log.hh" + +namespace { + +constexpr const int ROOT_USER = 0; + +const std::set event_launch_support_list = { + SYS_EVENT_BATTERY_CHARGER_STATUS, + SYS_EVENT_USB_STATUS, + SYS_EVENT_EARJACK_STATUS, + SYS_EVENT_INCOMMING_MSG, + SYS_EVENT_OUTGOING_MSG, + SYS_EVENT_WIFI_STATE +}; + +} // namespace + +namespace esd::module { + +EventLaunch::EventLaunch(std::string pkgid, std::string appid, uid_t uid) + : package_name_(std::move(pkgid)), + uid_(uid) { + app_list_evtlaunch_.push_back(EsdListItem(package_name_, std::move(appid), + uid)); +} + +EventLaunch::~EventLaunch() { + if (reg_id_) + eventsystem_unregister_event(reg_id_); +} + +bool EventLaunch::Register(std::string_view event_name) { + if (eventsystem_register_event(event_name.data(), ®_id_, + EsdEventHandlerCb, this) != 0) { + return false; + } + + return true; +} + +void EventLaunch::Print(const std::string& event_name) { + for (auto& i : app_list_evtlaunch_) + i.Print(event_name); +} + +uid_t EventLaunch::GetUid() const { + return uid_; +} + +const std::string& EventLaunch::GetPackageName() const { + return package_name_; +} + +std::list& EventLaunch::GetAppListEventLaunch() { + return app_list_evtlaunch_; +} + +void EventLaunch::EsdEventHandlerCb(const char* event_name, bundle* data, + void* user_data) { + _D("event_name(%s)", event_name); + EventLaunch* el = reinterpret_cast(user_data); + if (event_launch_support_list.find(event_name) != + event_launch_support_list.end()) { + el->OnEvent(event_name, tizen_base::Bundle(data, false, false), false, + true, ROOT_USER, ""); + } +} + +bool EventLaunch::CheckEvent(std::string_view event_name, + const tizen_base::Bundle& data) { + if (event_name == SYS_EVENT_BATTERY_CHARGER_STATUS) { + auto val = data.GetString(EVT_KEY_BATTERY_CHARGER_STATUS); + _D("charger val(%s)", val.c_str()); + if (val != EVT_VAL_BATTERY_CHARGER_CONNECTED) + return false; + } else if (event_name == SYS_EVENT_USB_STATUS) { + auto val = data.GetString(EVT_KEY_USB_STATUS); + _D("usb val(%s)", val.c_str()); + if (val != EVT_VAL_USB_CONNECTED) + return false; + } else if (event_name == SYS_EVENT_EARJACK_STATUS) { + auto val = data.GetString(EVT_KEY_EARJACK_STATUS); + _D("earjack val(%s)", val.c_str()); + if (val != EVT_VAL_EARJACK_CONNECTED) + return false; + } else if (event_name == SYS_EVENT_INCOMMING_MSG) { + auto msg_type = data.GetString(EVT_KEY_MSG_TYPE); + _D("msg_type(%s)", msg_type.c_str()); + if (msg_type.empty()) + return false; + auto msg_id = data.GetString(EVT_KEY_MSG_ID); + _D("msg_id(%s)", msg_id.c_str()); + if (msg_id.empty()) + return false; + } else if (event_name == SYS_EVENT_WIFI_STATE) { + auto val = data.GetString(EVT_KEY_WIFI_STATE); + if (val.empty()) + return false; + _D("wifi_state(%s)", val.c_str()); + if (val != EVT_VAL_WIFI_CONNECTED) + return false; + } + + return true; +} + +void EventLaunch::Notify(std::string_view event_name, + const tizen_base::Bundle& data, bool is_user_event, bool trusted, + uid_t sender_uid, std::string_view sender_appid) { + OnEvent(event_name, data, is_user_event, trusted, sender_uid, sender_appid); +} + +void EventLaunch::OnEvent(std::string_view event_name, + const tizen_base::Bundle& data, bool is_user_event, bool trusted, + uid_t sender_uid, std::string_view sender_appid) { + _D("event_name(%s)", event_name.data()); + + if (app_list_evtlaunch_.empty()) + return; + + if (!is_user_event && !CheckEvent(event_name, data)) + return; + + static int req_id; + for (auto& i : app_list_evtlaunch_) { + _D("launch_on_event: app_id(%s), event_name(%s), uid(%d), is_user(%d)," + " trusted(%d)", + i.GetAppId().c_str(), event_name.data(), i.GetUid(), + is_user_event, trusted); + + if (is_user_event && trusted) { + if (i.GetTrustedInfo() == EsdListItem::TRUSTED_UNKNOWN) { + int ret = CertificateMatcher::Match(i.GetUid(), + i.GetAppId(), sender_uid, sender_appid.data()); + if (ret == ES_R_EINVAL) { + i.SetTrustedInfo(EsdListItem::TRUSTED_DENIED); + continue; + } else if (ret == ES_R_ERROR) { + continue; + } else { + i.SetTrustedInfo(EsdListItem::TRUSTED_ALLOWED); + } + } else if (i.GetTrustedInfo() == EsdListItem::TRUSTED_DENIED) { + continue; + } + } + + if (aul_app_is_running_for_uid(i.GetAppId().c_str(), i.GetUid())) { + _D("already is running or launch failed"); + continue; + } + + tizen_base::Bundle b(data); + std::string event_uri; + if (is_user_event) { + event_uri = USER_EVENT_NAME_PREFIX + std::string(event_name); + } else { + event_uri = SYSTEM_EVENT_NAME_PREFIX + std::string(event_name); + } + + appsvc_set_operation(b.GetHandle(), APPSVC_OPERATION_LAUNCH_ON_EVENT); + appsvc_set_uri(b.GetHandle(), event_uri.c_str()); + appsvc_set_appid(b.GetHandle(), i.GetAppId().c_str()); + + int pid = aul_svc_run_service_async_for_uid(b.GetHandle(), req_id++, + nullptr, nullptr, i.GetUid()); + _D("uid(%d), pid(%d)", i.GetUid(), pid); + } +} + +} // namespace esd::module diff --git a/src/modules/dbus_event/event_launch.hh b/src/modules/dbus_event/event_launch.hh new file mode 100644 index 0000000..e9b0871 --- /dev/null +++ b/src/modules/dbus_event/event_launch.hh @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2022 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 EVENTSYSTEM_MODULES_DBUS_EVENT_EVENT_LAUNCH_HH_ +#define EVENTSYSTEM_MODULES_DBUS_EVENT_EVENT_LAUNCH_HH_ + +#include +#include + +#include +#include +#include +#include +#include +#include + +#include "esd_list_item.hh" + +namespace esd::module { + +class EventLaunch { + public: + EventLaunch() = default; + EventLaunch(std::string pkgid, std::string appid, uid_t uid); + EventLaunch(const EventLaunch&) = delete; + EventLaunch& operator = (const EventLaunch&) = delete; + ~EventLaunch(); + + bool Register(std::string_view event_name); + void Print(const std::string& event_name); + uid_t GetUid() const; + const std::string& GetPackageName() const; + std::list& GetAppListEventLaunch(); + void Notify(std::string_view event_name, const tizen_base::Bundle& data, + bool is_user_event, bool trusted, uid_t sender_uid, + std::string_view sender_appid); + + private: + bool CheckEvent(std::string_view event_name, const tizen_base::Bundle& data); + void OnEvent(std::string_view event_name, const tizen_base::Bundle& data, + bool is_user_event, bool trusted, uid_t sender_uid, + std::string_view sender_appid); + + static void EsdEventHandlerCb(const char* event_name, bundle* data, + void* user_data); + + private: + std::string package_name_; + std::list app_list_evtlaunch_; + guint reg_id_ = 0; + uid_t uid_ = 0; +}; + +} // namespace esd::module + +#endif // EVENTSYSTEM_MODULES_DBUS_EVENT_EVENT_LAUNCH_HH_ diff --git a/src/modules/dbus_event/last_event_item.hh b/src/modules/dbus_event/last_event_item.hh new file mode 100644 index 0000000..4bc9be4 --- /dev/null +++ b/src/modules/dbus_event/last_event_item.hh @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2022 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 EVENTSYSTEM_MODULES_DBUS_EVENT_LAST_EVENT_ITEM_HH_ +#define EVENTSYSTEM_MODULES_DBUS_EVENT_LAST_EVENT_ITEM_HH_ + +#include +#include + +namespace esd::module { + +class LastEventItem { + public: + LastEventItem() = default; + LastEventItem(std::string app_id, std::string event_name, + std::string own_name, uid_t uid) + : app_id_(std::move(app_id)), event_name_(std::move(event_name)), + own_name_(std::move(own_name)), uid_(uid) { + } + + const std::string& GetAppId() const { + return app_id_; + } + + const std::string& GetEventName() const { + return event_name_; + } + + const std::string& GetOwnName() const { + return own_name_; + } + + uid_t GetUid() const { + return uid_; + } + + void SetOwnName(std::string own_name) { + own_name_ = std::move(own_name); + } + + private: + std::string app_id_; + std::string event_name_; + std::string own_name_; + uid_t uid_; +}; + +} // namespace esd::module + +#endif // EVENTSYSTEM_MODULES_DBUS_EVENT_LAST_EVENT_ITEM_HH_ -- 2.7.4 From fed90c92a76c4aba806a710999e61efaff5c6e37 Mon Sep 17 00:00:00 2001 From: Inkyun Kil Date: Thu, 5 Jan 2023 16:58:41 +0900 Subject: [PATCH 12/16] Improve unittests and adds omitted code - Improve coverage - Add some tests - Adds omitted code for ondemand launch app - bundle -> bundle_cpp Change-Id: I6dfc36bb4b1f4a389d0431304741b3cda5110670 Signed-off-by: Inkyun Kil --- src/modules/dbus_event/dbus_event_module.cc | 43 ++-- src/modules/dbus_event/event_launch.cc | 9 + tests/cion_unit_tests/CMakeLists.txt | 2 +- tests/cion_unit_tests/src/test_cion_module.cc | 6 +- tests/dbus_event_unit_tests/CMakeLists.txt | 2 +- .../src/test_dbus_event_module.cc | 256 +++++++++++++++++++++ .../dbus_event_unit_tests/src/test_earlier_item.cc | 72 ++++++ .../dbus_event_unit_tests/src/test_event_launch.cc | 100 ++++++++ .../src/test_privilege_checker.cc | 6 +- .../src/test_vconf_handler.cc | 10 +- tests/mock/aul_mock.cc | 14 +- tests/mock/aul_mock.h | 6 +- tests/mock/eventsystem_mock.cc | 5 + tests/mock/eventsystem_mock.h | 3 +- tests/mock/gio_mock.cc | 54 ++++- tests/mock/gio_mock.h | 29 ++- tests/mock/pkgmgr_info_mock.cc | 82 +++++-- tests/mock/pkgmgr_info_mock.h | 21 +- tests/mock/tzplatform_config_mock.h | 6 +- tests/mock/vconf_mock.cc | 33 +++ tests/mock/vconf_mock.h | 35 +++ 21 files changed, 698 insertions(+), 96 deletions(-) create mode 100644 tests/dbus_event_unit_tests/src/test_dbus_event_module.cc create mode 100644 tests/dbus_event_unit_tests/src/test_earlier_item.cc create mode 100644 tests/dbus_event_unit_tests/src/test_event_launch.cc create mode 100644 tests/mock/vconf_mock.cc create mode 100644 tests/mock/vconf_mock.h diff --git a/src/modules/dbus_event/dbus_event_module.cc b/src/modules/dbus_event/dbus_event_module.cc index 9469ae9..8ed90ff 100644 --- a/src/modules/dbus_event/dbus_event_module.cc +++ b/src/modules/dbus_event/dbus_event_module.cc @@ -406,18 +406,15 @@ void __esd_signal_handler(GDBusConnection *connection, GVariant *parameters, gpointer user_data) { int handle; - bundle *b; guint64 uid = 0; if (!g_strcmp0(signal_name, SYSTEMD_DBUS_SIGNAL_STARTUP_FINISHED)) { _I("System session finished"); - b = bundle_create(); - bundle_add_str(b, EVT_KEY_BOOT_COMPLETED, - EVT_VAL_BOOT_COMPLETED_TRUE); - eventsystem_send_system_event(SYS_EVENT_BOOT_COMPLETED, b); - bundle_free(b); + tizen_base::Bundle b; + b.Add(EVT_KEY_BOOT_COMPLETED, EVT_VAL_BOOT_COMPLETED_TRUE); + eventsystem_send_system_event(SYS_EVENT_BOOT_COMPLETED, b.GetHandle()); handle = creat(ESD_BOOT_COMPLETED, 0640); if (handle != -1) @@ -538,15 +535,13 @@ void __esd_on_bus_acquired(GDBusConnection *connection, void __esd_on_name_acquired(GDBusConnection *connection, const gchar *name, gpointer user_data) { - bundle *b; + tizen_base::Bundle b; __esd_check_trusted_events(connection, "ListNames"); __esd_check_trusted_events(connection, "ListActivatableNames"); - b = bundle_create(); - bundle_add_str(b, EVT_KEY_ESD_STATUS, EVT_VAL_ESD_STARTED); - eventsystem_send_system_event(SYS_EVENT_ESD_STATUS, b); - bundle_free(b); + b.Add(EVT_KEY_ESD_STATUS, EVT_VAL_ESD_STARTED); + eventsystem_send_system_event(SYS_EVENT_ESD_STATUS, b.GetHandle()); esd::module::DbusEventModule* mod = static_cast(user_data); @@ -1080,30 +1075,20 @@ void DbusEventModule::CheckLastDataMethodCall(GDBusConnection* connection, auto it = user_last_event_table_.find(key); if (it != user_last_event_table_.end()) { GVariant *gv; - bundle *b; - bundle_raw *raw; - int len; + tizen_base::Bundle b; int ret; GError *error = nullptr; - b = bundle_create(); - if (!b) { - result = ES_R_ERROR; - param = g_variant_new("(i)", result); - g_dbus_method_invocation_return_value(invocation, param); - return; - } - - bundle_add_str(b, EVT_KEY_KEPT_EVENT_NAME, event_name); - bundle_add_str(b, EVT_KEY_KEPT_OWN_NAME, own_name); + b.Add(EVT_KEY_KEPT_EVENT_NAME, event_name); + b.Add(EVT_KEY_KEPT_OWN_NAME, own_name); if (CertificateMatcher::Match((*it).second.GetUid(), (*it).second.GetAppId(), sender_uid, app_id) == ES_R_OK) - bundle_add_str(b, EVT_KEY_KEPT_IS_TRUSTED, "true"); + b.Add(EVT_KEY_KEPT_IS_TRUSTED, "true"); else - bundle_add_str(b, EVT_KEY_KEPT_IS_TRUSTED, "false"); + b.Add(EVT_KEY_KEPT_IS_TRUSTED, "false"); - bundle_encode(b, &raw, &len); - gv = g_variant_new("(us)", len, raw); + auto raw = b.ToRaw(); + gv = g_variant_new("(us)", raw.second, raw.first.get()); ret = g_dbus_connection_emit_signal(connection, (*it).second.GetOwnName().c_str(), SYS_EVENT_OBJ_PATH, @@ -1115,8 +1100,6 @@ void DbusEventModule::CheckLastDataMethodCall(GDBusConnection* connection, _E("Unable to emit signal: %s", error->message); g_error_free(error); } - bundle_free_encoded_rawdata(&raw); - bundle_free(b); } param = g_variant_new("(i)", result); diff --git a/src/modules/dbus_event/event_launch.cc b/src/modules/dbus_event/event_launch.cc index 0df70a4..a03436b 100644 --- a/src/modules/dbus_event/event_launch.cc +++ b/src/modules/dbus_event/event_launch.cc @@ -120,6 +120,15 @@ bool EventLaunch::CheckEvent(std::string_view event_name, _D("msg_id(%s)", msg_id.c_str()); if (msg_id.empty()) return false; + } else if (event_name == SYS_EVENT_OUTGOING_MSG) { + auto msg_type = data.GetString(EVT_KEY_OUT_MSG_TYPE); + _D("msg_type(%s)", msg_type.c_str()); + if (msg_type.empty()) + return false; + auto msg_id = data.GetString(EVT_KEY_OUT_MSG_ID); + _D("msg_id(%s)", msg_id.c_str()); + if (msg_id.empty()) + return false; } else if (event_name == SYS_EVENT_WIFI_STATE) { auto val = data.GetString(EVT_KEY_WIFI_STATE); if (val.empty()) diff --git a/tests/cion_unit_tests/CMakeLists.txt b/tests/cion_unit_tests/CMakeLists.txt index 520dd1e..2ae201c 100644 --- a/tests/cion_unit_tests/CMakeLists.txt +++ b/tests/cion_unit_tests/CMakeLists.txt @@ -12,7 +12,7 @@ ADD_EXECUTABLE(${TARGET_ESD_MOD_CION_UNITTESTS} TARGET_INCLUDE_DIRECTORIES(${TARGET_ESD_MOD_CION_UNITTESTS} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../../src/modules) TARGET_INCLUDE_DIRECTORIES(${TARGET_ESD_MOD_CION_UNITTESTS} PUBLIC - ${CMAKE_CURRENT_SOURCE_DIR}/../mock) + ${CMAKE_CURRENT_SOURCE_DIR}/../) TARGET_LINK_LIBRARIES(${TARGET_ESD_MOD_CION_UNITTESTS} PRIVATE ${TARGET_LIB_ESD}) diff --git a/tests/cion_unit_tests/src/test_cion_module.cc b/tests/cion_unit_tests/src/test_cion_module.cc index 68e1bfb..2312d65 100644 --- a/tests/cion_unit_tests/src/test_cion_module.cc +++ b/tests/cion_unit_tests/src/test_cion_module.cc @@ -17,9 +17,9 @@ #include #include "cion/cion_module.hh" -#include "pkgmgr_info_mock.h" -#include "tzplatform_config_mock.h" -#include "test_fixture.h" +#include "mock/pkgmgr_info_mock.h" +#include "mock/tzplatform_config_mock.h" +#include "mock/test_fixture.h" using ::testing::_; using ::testing::DoAll; diff --git a/tests/dbus_event_unit_tests/CMakeLists.txt b/tests/dbus_event_unit_tests/CMakeLists.txt index be32285..57bb450 100644 --- a/tests/dbus_event_unit_tests/CMakeLists.txt +++ b/tests/dbus_event_unit_tests/CMakeLists.txt @@ -12,7 +12,7 @@ ADD_EXECUTABLE(${TARGET_ESD_MOD_DBUS_EVENT_UNITTESTS} TARGET_INCLUDE_DIRECTORIES(${TARGET_ESD_MOD_DBUS_EVENT_UNITTESTS} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../../src/modules) TARGET_INCLUDE_DIRECTORIES(${TARGET_ESD_MOD_DBUS_EVENT_UNITTESTS} PUBLIC - ${CMAKE_CURRENT_SOURCE_DIR}/../mock) + ${CMAKE_CURRENT_SOURCE_DIR}/..) TARGET_LINK_LIBRARIES(${TARGET_ESD_MOD_DBUS_EVENT_UNITTESTS} PRIVATE ${TARGET_LIB_ESD}) diff --git a/tests/dbus_event_unit_tests/src/test_dbus_event_module.cc b/tests/dbus_event_unit_tests/src/test_dbus_event_module.cc new file mode 100644 index 0000000..5662feb --- /dev/null +++ b/tests/dbus_event_unit_tests/src/test_dbus_event_module.cc @@ -0,0 +1,256 @@ +/* + * 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 + +#include "mock/test_fixture.h" +#include "mock/aul_mock.h" +#include "mock/cynara_mock.h" +#include "mock/eventsystem_mock.h" +#include "mock/gio_mock.h" +#include "mock/pkgmgr_info_mock.h" +#include "mock/vconf_mock.h" +#include "dbus_event/dbus_event_module.hh" + +using ::testing::_; +using ::testing::DoAll; +using ::testing::Invoke; +using ::testing::InvokeArgument; +using ::testing::Return; + +namespace { + +struct AppCtrlData { + AppCtrlData(std::string_view appid, std::string_view pkgid, uid_t uid) + : appid_(appid), pkgid_(pkgid), uid_(uid) { + } + + std::string_view appid_; + std::string_view pkgid_; + uid_t uid_; +}; + +std::string pkgid_str("pkgid"); + +class Mocks : public ::testing::NiceMock, + public ::testing::NiceMock, + public ::testing::NiceMock, + public ::testing::NiceMock, + public ::testing::NiceMock, + public ::testing::NiceMock {}; + +} // namespace + +class DbusEventMoudleTest : public TestFixture { + public: + DbusEventMoudleTest() : TestFixture(std::make_unique()) {} + virtual ~DbusEventMoudleTest() {} + + virtual void SetUp() { + info_ = reinterpret_cast(malloc(sizeof(GDBusNodeInfo))); + pc_ = reinterpret_cast(malloc(sizeof(pkgmgr_client))); + + EXPECT_CALL(GetMock(), g_dbus_node_info_new_for_xml(_, _)) + .WillOnce(Return(info_)); + EXPECT_CALL(GetMock(), g_dbus_method_invocation_return_value(_, _)) + .WillRepeatedly(Return()); + EXPECT_CALL(GetMock(), eventsystem_register_event(_, _, _, _)) + .WillRepeatedly(Return(ES_R_OK)); + + module_ = std::make_unique(); + tools_ = std::make_unique(); + } + + virtual void TearDown() { + module_->Shutdown(); + module_.reset(); + tools_.reset(); + free(info_); + free(pc_); + } + + std::unique_ptr module_; + std::unique_ptr tools_; + GDBusNodeInfo* info_; + pkgmgr_client* pc_; +}; + +TEST_F(DbusEventMoudleTest, Startup) { + EXPECT_CALL(GetMock(), pkgmgr_client_listen_status(_, _, _)) + .WillOnce(DoAll(InvokeArgument<1>(0, 0, "pkgtype", "pkgid", "start", + "install", "psms", module_.get()), Return(1))); + EXPECT_CALL(GetMock(), pkgmgr_client_new(_)) + .WillOnce(Return(pc_)); + EXPECT_CALL(GetMock(), g_bus_own_name(_, _, _, _, _, _, _, _)) + .WillOnce(Return(1)); + EXPECT_CALL(GetMock(), vconf_get_int(_, _)) + .WillRepeatedly(Return(VCONF_OK)); + EXPECT_CALL(GetMock(), vconf_get_bool(_, _)) + .WillRepeatedly(Return(VCONF_OK)); + EXPECT_CALL(GetMock(), vconf_notify_key_changed(_, _, _)) + .WillRepeatedly(DoAll( + InvokeArgument<1>(nullptr, nullptr), + InvokeArgument<1>(nullptr, nullptr), + InvokeArgument<1>(nullptr, nullptr), + InvokeArgument<1>(nullptr, nullptr), + InvokeArgument<1>(nullptr, nullptr), + InvokeArgument<1>(nullptr, nullptr), + InvokeArgument<1>(nullptr, nullptr), + InvokeArgument<1>(nullptr, nullptr), + InvokeArgument<1>(nullptr, nullptr), + InvokeArgument<1>(nullptr, nullptr), + InvokeArgument<1>(nullptr, nullptr), + InvokeArgument<1>(nullptr, nullptr), Return(1))); + + bool ret = module_->Startup(tools_.get()); + EXPECT_EQ(ret, true); + module_->SetVconfHandler(); +} + +TEST_F(DbusEventMoudleTest, HandleMethodCallCb) { + EXPECT_CALL(GetMock(), pkgmgr_client_listen_status(_, _, _)) + .WillOnce(DoAll(InvokeArgument<1>(0, 0, "pkgtype", "pkgid", "start", + "install", "psms", module_.get()), Return(1))); + EXPECT_CALL(GetMock(), pkgmgr_client_new(_)) + .WillOnce(Return(pc_)); + EXPECT_CALL(GetMock(), g_bus_own_name(_, _, _, _, _, _, _, _)) + .WillOnce(Return(1)); + bool ret = module_->Startup(tools_.get()); + ASSERT_EQ(ret, true); + + GDBusMessage* msg = g_dbus_message_new(); + GDBusMessage* reply = g_dbus_message_new(); + GVariant* body = g_variant_new("(u)", 5001); + 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(reply)); + EXPECT_CALL(GetMock(), g_dbus_message_get_body(_)) + .WillRepeatedly(Return(body)); + EXPECT_CALL(GetMock(), g_dbus_connection_emit_signal(_, _, _, _, _, + _, _)).WillOnce(Return(1)); + EXPECT_CALL(GetMock(), aul_app_get_appid_bypid_for_uid(_, _, _, _)) + .WillRepeatedly(Invoke([](int pid, char* appid, int size, uid_t uid) { + snprintf(appid, size, "%s", "org.tizen.helloworld"); + return AUL_R_OK; + })); + 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; + })); + + GVariant* var = g_variant_new("(is)", 5001, SYS_EVENT_DISPLAY_STATE); + module_->HandleMethodCallCb(nullptr, nullptr, nullptr, nullptr, + "CheckSenderValidation", var, nullptr, module_.get()); + + GVariant* var2 = g_variant_new("(s)", SYS_EVENT_DISPLAY_STATE); + module_->HandleMethodCallCb(nullptr, nullptr, nullptr, nullptr, + "GetTrustedPeerList", var2, nullptr, module_.get()); + + GVariant* var3 = g_variant_new("(ss)", SYS_EVENT_DISPLAY_STATE, "dest"); + module_->HandleMethodCallCb(nullptr, nullptr, nullptr, nullptr, + "SetupTrustedPeer", var3, nullptr, module_.get()); + + GVariant* var5 = g_variant_new("(s)", SYS_EVENT_DISPLAY_STATE); + module_->HandleMethodCallCb(nullptr, nullptr, nullptr, nullptr, + "CheckUserSendValidation", var5, nullptr, module_.get()); + + module_->HandleMethodCallCb(nullptr, nullptr, nullptr, nullptr, + "CionGetUuid", nullptr, nullptr, module_.get()); + + GVariant* var10 = g_variant_new("(ss)", "service", "display"); + module_->HandleMethodCallCb(nullptr, nullptr, nullptr, nullptr, + "CionSetDisplayName", var10, nullptr, module_.get()); + + GVariant* var11 = g_variant_new("(s)", "service"); + module_->HandleMethodCallCb(nullptr, nullptr, nullptr, nullptr, + "CionGetDisplayName", var11, nullptr, module_.get()); + + GVariant* var12 = g_variant_new("(sb)", "service", true); + module_->HandleMethodCallCb(nullptr, nullptr, nullptr, nullptr, + "CionSetEnabled", var12, nullptr, module_.get()); + + GVariant* var13 = g_variant_new("(s)", "service"); + module_->HandleMethodCallCb(nullptr, nullptr, nullptr, nullptr, + "CionGetEnabled", var13, nullptr, module_.get()); + + GVariant* var4 = g_variant_new("(s)", SYS_EVENT_DISPLAY_STATE); + module_->HandleMethodCallCb(nullptr, nullptr, nullptr, nullptr, + "CheckPrivilegeValidation", var4, nullptr, module_.get()); + + GVariant* var6 = g_variant_new("(s)", SYS_EVENT_BOOT_COMPLETED); + module_->HandleMethodCallCb(nullptr, nullptr, nullptr, nullptr, + "GetEarlierData", var6, nullptr, module_.get()); + + GVariant* var7 = g_variant_new("(ss)", SYS_EVENT_DISPLAY_STATE, "own"); + module_->HandleMethodCallCb(nullptr, nullptr, nullptr, nullptr, + "KeepLastData", var7, nullptr, module_.get()); + + GVariant* var8 = g_variant_new("(ss)", SYS_EVENT_DISPLAY_STATE, "own"); + module_->HandleMethodCallCb(nullptr, nullptr, nullptr, nullptr, + "CheckLastData", var8, nullptr, module_.get()); + + tizen_base::Bundle b; + b.Add("key", "value"); + auto raw = b.ToRaw(); + GVariant* var9 = g_variant_new("(ssib)", SYS_EVENT_DISPLAY_STATE, + raw.first.get(), raw.second, true); + module_->HandleMethodCallCb(nullptr, nullptr, nullptr, nullptr, + "LaunchOnEventFromUserEvent", var9, nullptr, module_.get()); +} + +TEST_F(DbusEventMoudleTest, PkgmgrEventCb) { + EXPECT_CALL(GetMock(), g_bus_own_name(_, _, _, _, _, _, _, _)) + .WillOnce(Return(1)); + EXPECT_CALL(GetMock(), pkgmgr_client_listen_status(_, _, _)) + .WillOnce(DoAll( + InvokeArgument<1>(0, 0, "pkgtype", "pkgid", "start", + "install", "psms", module_.get()), + InvokeArgument<1>(0, 0, "pkgtype", "pkgid", "end", + "ok", "psms", module_.get()), Return(1))); + EXPECT_CALL(GetMock(), pkgmgr_client_new(_)) + .WillOnce(Return(pc_)); + EXPECT_CALL(GetMock(), pkgmgrinfo_pkginfo_get_usr_pkginfo(_, + _, _)).WillOnce(Return(0)); + EXPECT_CALL(GetMock(), pkgmgrinfo_pkginfo_destroy_pkginfo(_)) + .WillOnce(Return(0)); + uid_t uid = 5001; + EXPECT_CALL(GetMock(), pkgmgrinfo_appinfo_get_usr_list(_, _, + _, _, _)) + .WillOnce(DoAll(InvokeArgument<2>(nullptr, &uid), Return(1))); + EXPECT_CALL(GetMock(), pkgmgrinfo_appinfo_get_pkgid(_, _)) + .WillOnce(Invoke([](pkgmgrinfo_appinfo_h handle, char **pkgid) { + *pkgid = const_cast(pkgid_str.c_str()); + return 0; + })); + EXPECT_CALL(GetMock(), pkgmgrinfo_appinfo_get_appid(_, _)) + .WillOnce(Invoke([](pkgmgrinfo_appinfo_h handle, char **appid) { + *appid = const_cast(pkgid_str.c_str()); + return 0; + })); + + AppCtrlData cb_data("appid", "pkgid", 5001); + EXPECT_CALL(GetMock(), pkgmgrinfo_appinfo_foreach_appcontrol( + _, _, _)) + .WillOnce(DoAll(InvokeArgument<1>("operation", "uri", "mime", &cb_data), Return(1))); + + bool ret = module_->Startup(tools_.get()); + EXPECT_EQ(ret, true); +} \ No newline at end of file diff --git a/tests/dbus_event_unit_tests/src/test_earlier_item.cc b/tests/dbus_event_unit_tests/src/test_earlier_item.cc new file mode 100644 index 0000000..512f26a --- /dev/null +++ b/tests/dbus_event_unit_tests/src/test_earlier_item.cc @@ -0,0 +1,72 @@ +/* + * 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 + +#include "mock/test_fixture.h" +#include "mock/eventsystem_mock.h" +#include "mock/vconf_mock.h" +#include "dbus_event/earlier_item.hh" + +using ::testing::_; +using ::testing::Return; + +namespace { + +class Mocks : public ::testing::NiceMock, + public ::testing::NiceMock {}; + +} // namespace + +class EarlierItemTest : public TestFixture { + public: + EarlierItemTest() : TestFixture(std::make_unique()) {} + virtual ~EarlierItemTest() {} + + virtual void SetUp() { + tizen_base::Bundle b; + b.Add("key", "value"); + item_ = std::make_unique(b); + } + + virtual void TearDown() { + } + + std::unique_ptr item_; +}; + +TEST_F(EarlierItemTest, Register) { + EXPECT_CALL(GetMock(), eventsystem_register_event(_, _, _, _)) + .WillOnce(Return(ES_R_OK)); + bool ret = item_->Register(std::string_view("str")); + EXPECT_EQ(ret, true); +} + +TEST_F(EarlierItemTest, GetData) { + tizen_base::Bundle b = item_->GetData(); + EXPECT_STREQ(b.GetString("key").c_str(), "value"); +} + +TEST_F(EarlierItemTest, CreateMap) { + EXPECT_CALL(GetMock(), vconf_get_int(_, _)) + .WillRepeatedly(Return(VCONF_OK)); + std::map> items = + item_->CreateMap(); + EXPECT_EQ(items.size(), 5); +} \ No newline at end of file diff --git a/tests/dbus_event_unit_tests/src/test_event_launch.cc b/tests/dbus_event_unit_tests/src/test_event_launch.cc new file mode 100644 index 0000000..2dc0fc8 --- /dev/null +++ b/tests/dbus_event_unit_tests/src/test_event_launch.cc @@ -0,0 +1,100 @@ +/* + * 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 + +#include "mock/test_fixture.h" +#include "mock/aul_mock.h" +#include "mock/eventsystem_mock.h" +#include "dbus_event/event_launch.hh" + +using ::testing::_; +using ::testing::DoAll; +using ::testing::InvokeArgument; +using ::testing::Return; + +namespace { + +class Mocks : public ::testing::NiceMock, + public ::testing::NiceMock {}; + +} // namespace + +class EventLaunchTest : public TestFixture { + public: + EventLaunchTest() : TestFixture(std::make_unique()) {} + virtual ~EventLaunchTest() {} + + virtual void SetUp() { + event_launch_ = std::make_unique( + std::string("pkgid"), std::string("appid"), 5001); + } + + virtual void TearDown() { + } + + std::unique_ptr event_launch_; +}; + +TEST_F(EventLaunchTest, Register) { + EXPECT_CALL(GetMock(), eventsystem_register_event(_, _, _, _)) + .WillOnce(DoAll(InvokeArgument<2>("event_name", nullptr, + event_launch_.get()), Return(0))); + bool ret = event_launch_->Register(SYS_EVENT_OUTGOING_MSG); + EXPECT_EQ(ret, true); +} + +TEST_F(EventLaunchTest, GetUid) { + uid_t ret = event_launch_->GetUid(); + EXPECT_EQ(ret, 5001); +} + +TEST_F(EventLaunchTest, GetPackageName) { + std::string pkg_name = event_launch_->GetPackageName(); + EXPECT_STREQ(pkg_name.c_str(), "pkgid"); +} + +TEST_F(EventLaunchTest, GetAppListEventLaunch) { + std::list list = + event_launch_->GetAppListEventLaunch(); + EXPECT_NE(list.size(), 0); +} + +TEST_F(EventLaunchTest, Notify) { + EXPECT_CALL(GetMock(), aul_app_is_running_for_uid(_, _)) + .WillOnce(Return(0)); + EXPECT_CALL(GetMock(), aul_svc_run_service_async_for_uid(_, _, _, _, _)) + .WillOnce(Return(0)); + + tizen_base::Bundle b; + event_launch_->Notify(std::string_view(SYS_EVENT_BATTERY_CHARGER_STATUS), + b, false, false, 5001, std::string_view("sender")); + event_launch_->Notify(std::string_view(SYS_EVENT_USB_STATUS), + b, false, false, 5001, std::string_view("sender")); + event_launch_->Notify(std::string_view(SYS_EVENT_EARJACK_STATUS), + b, false, false, 5001, std::string_view("sender")); + event_launch_->Notify(std::string_view(SYS_EVENT_INCOMMING_MSG), + b, false, false, 5001, std::string_view("sender")); + event_launch_->Notify(std::string_view(SYS_EVENT_OUTGOING_MSG), + b, false, false, 5001, std::string_view("sender")); + event_launch_->Notify(std::string_view(SYS_EVENT_WIFI_STATE), + b, false, false, 5001, std::string_view("sender")); + event_launch_->Notify(std::string_view("User"), + b, false, true, 5001, std::string_view("sender")); +} \ No newline at end of file diff --git a/tests/dbus_event_unit_tests/src/test_privilege_checker.cc b/tests/dbus_event_unit_tests/src/test_privilege_checker.cc index c5c5ce9..5ce7689 100644 --- a/tests/dbus_event_unit_tests/src/test_privilege_checker.cc +++ b/tests/dbus_event_unit_tests/src/test_privilege_checker.cc @@ -19,9 +19,9 @@ #include #include -#include "cynara_mock.h" -#include "security_manager_mock.h" -#include "test_fixture.h" +#include "mock/cynara_mock.h" +#include "mock/security_manager_mock.h" +#include "mock/test_fixture.h" #include "dbus_event/privilege_checker.hh" diff --git a/tests/dbus_event_unit_tests/src/test_vconf_handler.cc b/tests/dbus_event_unit_tests/src/test_vconf_handler.cc index 7472c39..6d3c4be 100644 --- a/tests/dbus_event_unit_tests/src/test_vconf_handler.cc +++ b/tests/dbus_event_unit_tests/src/test_vconf_handler.cc @@ -20,17 +20,21 @@ #include #include -#include "test_fixture.h" -#include "eventsystem_mock.h" +#include "mock/test_fixture.h" +#include "mock/eventsystem_mock.h" +#include "mock/vconf_mock.h" #include "dbus_event/vconf_event_handler.hh" // using namespace tizen_base; using ::testing::_; +using ::testing::DoAll; +using ::testing::InvokeArgument; using ::testing::Return; namespace { -class Mocks : public ::testing::NiceMock {}; +class Mocks : public ::testing::NiceMock, + public ::testing::NiceMock {}; } // namespace diff --git a/tests/mock/aul_mock.cc b/tests/mock/aul_mock.cc index d4129dd..87a0cd8 100644 --- a/tests/mock/aul_mock.cc +++ b/tests/mock/aul_mock.cc @@ -18,10 +18,6 @@ #include "mock_hook.h" #include "test_fixture.h" -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); -} - extern "C" int aul_svc_run_service_async_for_uid(bundle* arg0, int arg1, aul_svc_res_fn arg2, void* arg3, uid_t arg4) { return MOCK_HOOK_P5(AulMock, aul_svc_run_service_async_for_uid, arg0, arg1, @@ -32,3 +28,13 @@ extern "C" int aul_app_is_running_for_uid(const char* arg0, uid_t arg1) { return MOCK_HOOK_P2(AulMock, aul_app_is_running_for_uid, arg0, arg1); } +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 index 9f15d18..63f6535 100644 --- a/tests/mock/aul_mock.h +++ b/tests/mock/aul_mock.h @@ -20,8 +20,9 @@ #include #include #include -#include #include +#include +#include #include #include "module_mock.h" @@ -30,10 +31,11 @@ class AulMock : public virtual ModuleMock { public: virtual ~AulMock() {} - MOCK_METHOD3(aul_app_get_appid_bypid, int (int, char*, int)); MOCK_METHOD5(aul_svc_run_service_async_for_uid, int(bundle*, int, aul_svc_res_fn, void*, uid_t)); MOCK_METHOD2(aul_app_is_running_for_uid, int(const char*, uid_t)); + 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/eventsystem_mock.cc b/tests/mock/eventsystem_mock.cc index d992e1a..149d558 100644 --- a/tests/mock/eventsystem_mock.cc +++ b/tests/mock/eventsystem_mock.cc @@ -22,3 +22,8 @@ extern "C" int eventsystem_send_system_event(const char* arg0, bundle* arg1) { return MOCK_HOOK_P2(EventSystemMock, eventsystem_send_system_event, arg0, arg1); } +extern "C" int eventsystem_register_event(const char* arg0, unsigned int* arg1, + eventsystem_handler arg2, void* arg3) { + return MOCK_HOOK_P4(EventSystemMock, eventsystem_register_event, arg0, arg1, + arg2, arg3); +} diff --git a/tests/mock/eventsystem_mock.h b/tests/mock/eventsystem_mock.h index e2a9fb9..d040ff4 100644 --- a/tests/mock/eventsystem_mock.h +++ b/tests/mock/eventsystem_mock.h @@ -28,7 +28,8 @@ class EventSystemMock : public virtual ModuleMock { virtual ~EventSystemMock() {} MOCK_METHOD2(eventsystem_send_system_event, int(const char*, bundle*)); - + MOCK_METHOD4(eventsystem_register_event, int(const char*, unsigned int*, + eventsystem_handler, void*)); }; #endif // MOCK_EVENTSYSTEM_MOCK_H_ \ No newline at end of file diff --git a/tests/mock/gio_mock.cc b/tests/mock/gio_mock.cc index 34c4086..b461fd2 100644 --- a/tests/mock/gio_mock.cc +++ b/tests/mock/gio_mock.cc @@ -20,12 +20,12 @@ #include "mock_hook.h" #include "test_fixture.h" -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" 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" guint g_dbus_connection_signal_subscribe(GDBusConnection* arg0, @@ -36,10 +36,24 @@ extern "C" guint g_dbus_connection_signal_subscribe(GDBusConnection* arg0, arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9); } -extern "C" GDBusMessage* g_dbus_message_new_method_call(const gchar* arg0, - const gchar* arg1, const gchar* arg2, const gchar* arg3) { - return MOCK_HOOK_P4(GioMock, g_dbus_message_new_method_call, arg0, arg1, arg2, - arg3); +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" 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" gboolean g_dbus_connection_emit_signal(GDBusConnection* arg1, @@ -49,11 +63,27 @@ extern "C" gboolean g_dbus_connection_emit_signal(GDBusConnection* arg1, arg3, arg4, arg5, arg6, arg7); } +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" 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" 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" void g_object_unref(gpointer arg0) { + return MOCK_HOOK_P1(GioMock, g_object_unref, arg0); +} + +extern "C" GDBusMessage* g_dbus_message_new_method_call(const gchar* arg0, + const gchar* arg1, const gchar* arg2, const gchar* arg3) { + return MOCK_HOOK_P4(GioMock, g_dbus_message_new_method_call, arg0, arg1, arg2, + arg3); } diff --git a/tests/mock/gio_mock.h b/tests/mock/gio_mock.h index 0239667..cf28e11 100644 --- a/tests/mock/gio_mock.h +++ b/tests/mock/gio_mock.h @@ -26,25 +26,42 @@ class GioMock : public virtual ModuleMock { public: virtual ~GioMock() {} - MOCK_METHOD7(g_dbus_connection_send_message_with_reply_sync, - GDBusMessage*(GDBusConnection*, GDBusMessage*, GDBusSendMessageFlags, - gint, volatile guint32*, GCancellable*, GError**)); + MOCK_METHOD7(g_dbus_connection_register_object, guint (GDBusConnection*, + const gchar*, GDBusInterfaceInfo*, const GDBusInterfaceVTable*, gpointer, + GDestroyNotify, 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_METHOD4(g_dbus_message_new_method_call, - GDBusMessage*(const gchar*, const gchar*, const gchar*, const gchar*)); + MOCK_METHOD8(g_bus_own_name, + guint(GBusType, const gchar*, + GBusNameOwnerFlags, GBusAcquiredCallback, GBusNameAcquiredCallback, + GBusNameLostCallback, gpointer, GDestroyNotify)); + + 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_METHOD7(g_dbus_connection_emit_signal, gboolean (GDBusConnection*, const gchar*, const gchar*, const gchar*, const gchar*, GVariant*, GError**)); + MOCK_METHOD2(g_dbus_node_info_new_for_xml, GDBusNodeInfo* (const gchar*, + GError**)); + + MOCK_METHOD2(g_dbus_method_invocation_return_value, + void (GDBusMethodInvocation*, GVariant*)); + MOCK_METHOD2(g_dbus_message_set_body, void(GDBusMessage*, GVariant*)); - MOCK_METHOD1(g_dbus_message_get_body, GVariant*(GDBusMessage*)); + MOCK_METHOD1(g_object_unref, void(gpointer)); + + MOCK_METHOD4(g_dbus_message_new_method_call, + GDBusMessage*(const gchar*, const gchar*, const gchar*, const gchar*)); }; diff --git a/tests/mock/pkgmgr_info_mock.cc b/tests/mock/pkgmgr_info_mock.cc index 969f45e..363703a 100644 --- a/tests/mock/pkgmgr_info_mock.cc +++ b/tests/mock/pkgmgr_info_mock.cc @@ -18,37 +18,77 @@ #include "mock_hook.h" #include "test_fixture.h" -extern "C" int pkgmgrinfo_pkginfo_get_usr_pkginfo(const char* arg1, uid_t arg2, - pkgmgrinfo_pkginfo_h* arg3) { - return MOCK_HOOK_P3(PkgmgrInfoMock, pkgmgrinfo_pkginfo_get_usr_pkginfo, arg1, - arg2, arg3); +extern "C" int pkgmgr_client_free(pkgmgr_client* arg0) { + return MOCK_HOOK_P1(PkgmgrInfoMock, pkgmgr_client_free, arg0); } -extern "C" int pkgmgrinfo_appinfo_get_pkgid(pkgmgrinfo_pkginfo_h arg1, - char** arg2) { - return MOCK_HOOK_P2(PkgmgrInfoMock, pkgmgrinfo_appinfo_get_pkgid, arg1, - arg2); +extern "C" int pkgmgrinfo_appinfo_get_appid( + pkgmgrinfo_appinfo_h arg0, char** arg1) { + return MOCK_HOOK_P2(PkgmgrInfoMock, pkgmgrinfo_appinfo_get_appid, + arg0, arg1); } -extern "C" int pkgmgrinfo_appinfo_destroy_appinfo( - pkgmgrinfo_appinfo_h arg0) { - return MOCK_HOOK_P1(PkgmgrInfoMock, pkgmgrinfo_appinfo_destroy_appinfo, +extern "C" int pkgmgrinfo_pkginfo_destroy_pkginfo(pkgmgrinfo_pkginfo_h arg0) { + return MOCK_HOOK_P1(PkgmgrInfoMock, pkgmgrinfo_pkginfo_destroy_pkginfo, arg0); } -extern "C" int pkgmgrinfo_pkginfo_destroy_pkginfo(pkgmgrinfo_pkginfo_h arg1) { - return MOCK_HOOK_P1(PkgmgrInfoMock, pkgmgrinfo_pkginfo_destroy_pkginfo, - arg1); +extern "C" int pkgmgrinfo_pkginfo_compare_usr_app_cert_info(const char* arg0, + const char* arg1, uid_t arg2, pkgmgrinfo_cert_compare_result_type_e* arg3) { + return MOCK_HOOK_P4(PkgmgrInfoMock, pkgmgrinfo_pkginfo_compare_usr_app_cert_info, + arg0, arg1, arg2, arg3); } -extern "C" int pkgmgrinfo_appinfo_get_usr_appinfo(const char* arg1, uid_t arg2, - pkgmgrinfo_appinfo_h* arg3) { - return MOCK_HOOK_P3(PkgmgrInfoMock, pkgmgrinfo_appinfo_get_usr_appinfo, arg1, - arg2, arg3); +extern "C" int pkgmgrinfo_appinfo_foreach_appcontrol(pkgmgrinfo_appinfo_h arg0, + pkgmgrinfo_app_control_list_cb arg1, void* arg2) { + return MOCK_HOOK_P3(PkgmgrInfoMock, pkgmgrinfo_appinfo_foreach_appcontrol, + arg0, arg1, arg2); } -extern "C" int pkgmgrinfo_pkginfo_get_version(pkgmgrinfo_pkginfo_h handle, - char** version) { +extern "C" int pkgmgrinfo_pkginfo_get_version(pkgmgrinfo_pkginfo_h arg0, + char **arg1) { return MOCK_HOOK_P2(PkgmgrInfoMock, pkgmgrinfo_pkginfo_get_version, - handle, version); + arg0, arg1); +} + +extern "C" int pkgmgrinfo_appinfo_get_usr_list(pkgmgrinfo_pkginfo_h arg0, + pkgmgrinfo_app_component arg1, pkgmgrinfo_app_list_cb arg2, void* arg3, + uid_t arg4) { + return MOCK_HOOK_P5(PkgmgrInfoMock, pkgmgrinfo_appinfo_get_usr_list, arg0, + arg1, arg2, arg3, arg4); +} + +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" pkgmgr_client* pkgmgr_client_new(pkgmgr_client_type arg0) { + return MOCK_HOOK_P1(PkgmgrInfoMock, pkgmgr_client_new, arg0); +} + +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 pkgmgr_client_listen_status(pkgmgr_client* arg0, + pkgmgr_handler arg1, void* arg2) { + return MOCK_HOOK_P3(PkgmgrInfoMock, pkgmgr_client_listen_status, arg0, arg1, + arg2); +} + +extern "C" int pkgmgrinfo_appinfo_destroy_appinfo( + pkgmgrinfo_appinfo_h arg0) { + return MOCK_HOOK_P1(PkgmgrInfoMock, pkgmgrinfo_appinfo_destroy_appinfo, + 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); +} + diff --git a/tests/mock/pkgmgr_info_mock.h b/tests/mock/pkgmgr_info_mock.h index 24a9ab1..3d703a1 100644 --- a/tests/mock/pkgmgr_info_mock.h +++ b/tests/mock/pkgmgr_info_mock.h @@ -18,6 +18,7 @@ #define MOCK_PKGMGR_INFO_MOCK_H_ #include +#include #include #include "module_mock.h" @@ -26,18 +27,30 @@ class PkgmgrInfoMock : public virtual ModuleMock { public: virtual ~PkgmgrInfoMock() {} + MOCK_METHOD1(pkgmgr_client_free, int (pkgmgr_client*)); + MOCK_METHOD2(pkgmgrinfo_appinfo_get_appid, int (pkgmgrinfo_appinfo_h, + char**)); + MOCK_METHOD1(pkgmgrinfo_pkginfo_destroy_pkginfo, + int (pkgmgrinfo_pkginfo_h)); + MOCK_METHOD4(pkgmgrinfo_pkginfo_compare_usr_app_cert_info, int (const char*, + const char*, uid_t, pkgmgrinfo_cert_compare_result_type_e*)); + MOCK_METHOD3(pkgmgrinfo_appinfo_foreach_appcontrol, int ( + pkgmgrinfo_appinfo_h , pkgmgrinfo_app_control_list_cb, void*)); + MOCK_METHOD2(pkgmgrinfo_pkginfo_get_version, int (pkgmgrinfo_pkginfo_h, + char**)); + MOCK_METHOD5(pkgmgrinfo_appinfo_get_usr_list, int (pkgmgrinfo_pkginfo_h, + pkgmgrinfo_app_component, pkgmgrinfo_app_list_cb, void*, uid_t)); MOCK_METHOD3(pkgmgrinfo_pkginfo_get_usr_pkginfo, int (const char *, uid_t, pkgmgrinfo_pkginfo_h*)); + MOCK_METHOD1(pkgmgr_client_new, pkgmgr_client* (pkgmgr_client_type)); MOCK_METHOD2(pkgmgrinfo_appinfo_get_pkgid, int (pkgmgrinfo_pkginfo_h, char **)); + MOCK_METHOD3(pkgmgr_client_listen_status, int (pkgmgr_client*, + pkgmgr_handler, void*)); MOCK_METHOD1(pkgmgrinfo_appinfo_destroy_appinfo, int (pkgmgrinfo_appinfo_h)); - MOCK_METHOD1(pkgmgrinfo_pkginfo_destroy_pkginfo, - int (pkgmgrinfo_pkginfo_h)); MOCK_METHOD3(pkgmgrinfo_appinfo_get_usr_appinfo, int (const char *, uid_t, pkgmgrinfo_appinfo_h*)); - MOCK_METHOD2(pkgmgrinfo_pkginfo_get_version, - int (pkgmgrinfo_pkginfo_h, char**)); }; #endif // MOCK_PKMGR_INFO_MOCK_H_ \ No newline at end of file diff --git a/tests/mock/tzplatform_config_mock.h b/tests/mock/tzplatform_config_mock.h index a251057..8696222 100644 --- a/tests/mock/tzplatform_config_mock.h +++ b/tests/mock/tzplatform_config_mock.h @@ -24,11 +24,7 @@ class TzplatformConfigMock : public virtual ModuleMock { public: - TzplatformConfigMock() { - using ::testing::_; - using ::testing::Return; - using ::testing::Invoke; - } + virtual ~TzplatformConfigMock() {} MOCK_METHOD2(tzplatform_mkpath, const char* (enum tzplatform_variable, const char*)); diff --git a/tests/mock/vconf_mock.cc b/tests/mock/vconf_mock.cc new file mode 100644 index 0000000..90cb11b --- /dev/null +++ b/tests/mock/vconf_mock.cc @@ -0,0 +1,33 @@ +/* + * 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 "vconf_mock.h" +#include "mock_hook.h" +#include "test_fixture.h" + +extern "C" int vconf_get_int(const char* arg0, int* arg1) { + return MOCK_HOOK_P2(VconfMock, vconf_get_int, arg0, arg1); +} + +extern "C" int vconf_get_bool(const char* arg0, int* arg1) { + return MOCK_HOOK_P2(VconfMock, vconf_get_bool, arg0, arg1); +} + +extern "C" int vconf_notify_key_changed(const char* arg0, + vconf_callback_fn arg1, void* arg2) { + return MOCK_HOOK_P3(VconfMock, vconf_notify_key_changed, arg0, arg1, arg2); +} + diff --git a/tests/mock/vconf_mock.h b/tests/mock/vconf_mock.h new file mode 100644 index 0000000..bea8279 --- /dev/null +++ b/tests/mock/vconf_mock.h @@ -0,0 +1,35 @@ +/* + * 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_VCONF_MOCK_H_ +#define MOCK_VCONF_MOCK_H_ + +#include +#include + +#include "module_mock.h" + +class VconfMock : public virtual ModuleMock { + public: + virtual ~VconfMock() {} + + MOCK_METHOD2(vconf_get_int, int (const char*, int*)); + MOCK_METHOD2(vconf_get_bool, int (const char*, int*)); + MOCK_METHOD3(vconf_notify_key_changed, int (const char*, + vconf_callback_fn, void*)); +}; + +#endif // MOCK_VCONF_MOCK_H_ \ No newline at end of file -- 2.7.4 From 396e8fdf415c03745e7e1e97d51940811b0ef068 Mon Sep 17 00:00:00 2001 From: Inkyun Kil Date: Thu, 2 Feb 2023 15:20:21 +0900 Subject: [PATCH 13/16] For previous version compatible patch - Some code makes build errors in previous gtest versions(1.8). (EXPECT_TRUE, DoAll) functions have some issue Change-Id: I55a598c42a9affdab0c871acdaa33bd0d38ff7d8 Signed-off-by: Inkyun Kil --- tests/cion_unit_tests/src/test_cion_module.cc | 2 +- .../src/test_dbus_event_module.cc | 59 ++-------------------- 2 files changed, 4 insertions(+), 57 deletions(-) diff --git a/tests/cion_unit_tests/src/test_cion_module.cc b/tests/cion_unit_tests/src/test_cion_module.cc index 2312d65..9bc4b07 100644 --- a/tests/cion_unit_tests/src/test_cion_module.cc +++ b/tests/cion_unit_tests/src/test_cion_module.cc @@ -72,7 +72,7 @@ TEST_F(CionModuleTest, GetUuidWithGenerate) { .Invoke&>( "Cion.GetUuidWithGenerate", app_id, uuid); EXPECT_TRUE(ret); - EXPECT_TRUE(uuid); + EXPECT_TRUE(static_cast(uuid)); } TEST_F(CionModuleTest, SetDisplayNameGetDisplayName) { diff --git a/tests/dbus_event_unit_tests/src/test_dbus_event_module.cc b/tests/dbus_event_unit_tests/src/test_dbus_event_module.cc index 5662feb..98a363c 100644 --- a/tests/dbus_event_unit_tests/src/test_dbus_event_module.cc +++ b/tests/dbus_event_unit_tests/src/test_dbus_event_module.cc @@ -93,8 +93,7 @@ class DbusEventMoudleTest : public TestFixture { TEST_F(DbusEventMoudleTest, Startup) { EXPECT_CALL(GetMock(), pkgmgr_client_listen_status(_, _, _)) - .WillOnce(DoAll(InvokeArgument<1>(0, 0, "pkgtype", "pkgid", "start", - "install", "psms", module_.get()), Return(1))); + .WillOnce(Return(1)); EXPECT_CALL(GetMock(), pkgmgr_client_new(_)) .WillOnce(Return(pc_)); EXPECT_CALL(GetMock(), g_bus_own_name(_, _, _, _, _, _, _, _)) @@ -104,19 +103,7 @@ TEST_F(DbusEventMoudleTest, Startup) { EXPECT_CALL(GetMock(), vconf_get_bool(_, _)) .WillRepeatedly(Return(VCONF_OK)); EXPECT_CALL(GetMock(), vconf_notify_key_changed(_, _, _)) - .WillRepeatedly(DoAll( - InvokeArgument<1>(nullptr, nullptr), - InvokeArgument<1>(nullptr, nullptr), - InvokeArgument<1>(nullptr, nullptr), - InvokeArgument<1>(nullptr, nullptr), - InvokeArgument<1>(nullptr, nullptr), - InvokeArgument<1>(nullptr, nullptr), - InvokeArgument<1>(nullptr, nullptr), - InvokeArgument<1>(nullptr, nullptr), - InvokeArgument<1>(nullptr, nullptr), - InvokeArgument<1>(nullptr, nullptr), - InvokeArgument<1>(nullptr, nullptr), - InvokeArgument<1>(nullptr, nullptr), Return(1))); + .WillRepeatedly(Return(1)); bool ret = module_->Startup(tools_.get()); EXPECT_EQ(ret, true); @@ -125,8 +112,7 @@ TEST_F(DbusEventMoudleTest, Startup) { TEST_F(DbusEventMoudleTest, HandleMethodCallCb) { EXPECT_CALL(GetMock(), pkgmgr_client_listen_status(_, _, _)) - .WillOnce(DoAll(InvokeArgument<1>(0, 0, "pkgtype", "pkgid", "start", - "install", "psms", module_.get()), Return(1))); + .WillOnce(Return(1)); EXPECT_CALL(GetMock(), pkgmgr_client_new(_)) .WillOnce(Return(pc_)); EXPECT_CALL(GetMock(), g_bus_own_name(_, _, _, _, _, _, _, _)) @@ -214,43 +200,4 @@ TEST_F(DbusEventMoudleTest, HandleMethodCallCb) { raw.first.get(), raw.second, true); module_->HandleMethodCallCb(nullptr, nullptr, nullptr, nullptr, "LaunchOnEventFromUserEvent", var9, nullptr, module_.get()); -} - -TEST_F(DbusEventMoudleTest, PkgmgrEventCb) { - EXPECT_CALL(GetMock(), g_bus_own_name(_, _, _, _, _, _, _, _)) - .WillOnce(Return(1)); - EXPECT_CALL(GetMock(), pkgmgr_client_listen_status(_, _, _)) - .WillOnce(DoAll( - InvokeArgument<1>(0, 0, "pkgtype", "pkgid", "start", - "install", "psms", module_.get()), - InvokeArgument<1>(0, 0, "pkgtype", "pkgid", "end", - "ok", "psms", module_.get()), Return(1))); - EXPECT_CALL(GetMock(), pkgmgr_client_new(_)) - .WillOnce(Return(pc_)); - EXPECT_CALL(GetMock(), pkgmgrinfo_pkginfo_get_usr_pkginfo(_, - _, _)).WillOnce(Return(0)); - EXPECT_CALL(GetMock(), pkgmgrinfo_pkginfo_destroy_pkginfo(_)) - .WillOnce(Return(0)); - uid_t uid = 5001; - EXPECT_CALL(GetMock(), pkgmgrinfo_appinfo_get_usr_list(_, _, - _, _, _)) - .WillOnce(DoAll(InvokeArgument<2>(nullptr, &uid), Return(1))); - EXPECT_CALL(GetMock(), pkgmgrinfo_appinfo_get_pkgid(_, _)) - .WillOnce(Invoke([](pkgmgrinfo_appinfo_h handle, char **pkgid) { - *pkgid = const_cast(pkgid_str.c_str()); - return 0; - })); - EXPECT_CALL(GetMock(), pkgmgrinfo_appinfo_get_appid(_, _)) - .WillOnce(Invoke([](pkgmgrinfo_appinfo_h handle, char **appid) { - *appid = const_cast(pkgid_str.c_str()); - return 0; - })); - - AppCtrlData cb_data("appid", "pkgid", 5001); - EXPECT_CALL(GetMock(), pkgmgrinfo_appinfo_foreach_appcontrol( - _, _, _)) - .WillOnce(DoAll(InvokeArgument<1>("operation", "uri", "mime", &cb_data), Return(1))); - - bool ret = module_->Startup(tools_.get()); - EXPECT_EQ(ret, true); } \ No newline at end of file -- 2.7.4 From e14c2b4e99068fe6d76e1af07e9c997ca67b9482 Mon Sep 17 00:00:00 2001 From: Inkyun Kil Date: Mon, 6 Feb 2023 12:25:15 +0900 Subject: [PATCH 14/16] Release version 1.0.5 changes: - For previous version compatible patch Change-Id: I06df2fa2077f0a35e9c44e90b0b2a1b064752557 Signed-off-by: Inkyun Kil --- packaging/esd.spec | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packaging/esd.spec b/packaging/esd.spec index 739c8f6..b5bec18 100644 --- a/packaging/esd.spec +++ b/packaging/esd.spec @@ -1,6 +1,6 @@ Name: esd Summary: Event system daemon -Version: 1.0.4 +Version: 1.0.5 Release: 1 Group: Application Framework/Service License: Apache-2.0 @@ -202,4 +202,4 @@ find . -name '*.gcno' -exec cp --parents '{}' "$gcno_obj_dir" ';' %if 0%{?gcov:1} %files gcov %{_datadir}/gcov/* -%endif \ No newline at end of file +%endif -- 2.7.4 From c3f8a13a7e222eb8e67d0b04ab85eb15a6647007 Mon Sep 17 00:00:00 2001 From: Inkyun Kil Date: Wed, 8 Feb 2023 11:51:17 +0900 Subject: [PATCH 15/16] Fix build issue for aarch64 Change-Id: I93648784c276626c17290240cd42d01ec0c50fd8 Signed-off-by: Inkyun Kil --- src/modules/dbus_event/dbus_event_module.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/modules/dbus_event/dbus_event_module.cc b/src/modules/dbus_event/dbus_event_module.cc index 8ed90ff..b1c063d 100644 --- a/src/modules/dbus_event/dbus_event_module.cc +++ b/src/modules/dbus_event/dbus_event_module.cc @@ -1379,7 +1379,7 @@ int DbusEventModule::CheckUserEventSenderValid(std::string_view event_name, event_name = event_name.substr(0, it); auto len = event_name.length(); if (len <= 1 || len > 128) { - _E("invalid length(%d) of user-defined name", len); + _E("invalid length(%ld) of user-defined name", len); return ES_R_EINVAL; } -- 2.7.4 From ed29db0b94718b24091f1cb1be805c16ffe3c2c5 Mon Sep 17 00:00:00 2001 From: Inkyun Kil Date: Wed, 8 Feb 2023 11:57:01 +0900 Subject: [PATCH 16/16] Release version 1.0.6 changes: - Fix build issue for aarch64 Change-Id: Ia4784c7aafe233768b72272da0a4cea0f92104a4 Signed-off-by: Inkyun Kil --- packaging/esd.spec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packaging/esd.spec b/packaging/esd.spec index b5bec18..c1fa1dd 100644 --- a/packaging/esd.spec +++ b/packaging/esd.spec @@ -1,6 +1,6 @@ Name: esd Summary: Event system daemon -Version: 1.0.5 +Version: 1.0.6 Release: 1 Group: Application Framework/Service License: Apache-2.0 -- 2.7.4