From 012ada2336b46bcd5c860db44bcc584106d9fbdd Mon Sep 17 00:00:00 2001 From: SEUNGTAEK HAN Date: Wed, 22 Mar 2017 14:06:47 +0900 Subject: [PATCH] Sync with tizen_3.0 branch Change-Id: I680967befe9772617d3b65d011f0928708804b32 --- CMakeLists.txt | 1 + client/CMakeLists.txt | 12 +- client/inc/PrivacyChecker.h | 0 client/inc/PrivacyGuardClient.h | 18 +- client/inc/SocketClient.h | 0 client/inc/privacy_guard_client_internal.h | 23 +- client/src/PrivacyChecker.cpp | 0 client/src/PrivacyGuardClient.cpp | 95 +- client/src/SocketClient.cpp | 1 + client/src/privacy_guard_client.cpp | 157 +-- client/src/privacy_guard_client_internal_types.h | 0 common/inc/IPrivacyGuard.h | 0 common/inc/PrivacyGuardCommon.h | 0 common/inc/PrivacyGuardTypes.h | 23 +- common/inc/PrivacyIdInfo.h | 0 common/inc/SocketConnection.h | 6 +- common/inc/SocketStream.h | 0 common/inc/Utils.h | 2 +- common/src/PrivacyIdInfo.cpp | 26 +- common/src/SocketConnection.cpp | 0 common/src/SocketStream.cpp | 0 common/src/Utils.cpp | 34 +- include/CMakeLists.txt | 6 +- include/privacy_guard_client.h | 42 +- include/privacy_guard_client_types.h | 5 +- packaging/libprivacy-guard-client.manifest | 0 packaging/privacy-guard-client-devel.manifest | 0 packaging/privacy-guard-client.manifest | 0 packaging/privacy-guard-server-devel.manifest | 0 packaging/privacy-guard-server.manifest | 0 packaging/privacy-guard-server.service | 0 packaging/privacy-guard-server.socket | 0 packaging/privacy-guard.changes | 0 packaging/privacy-guard.spec | 14 +- pkgmgr_plugin/CMakeLists.txt | 0 pkgmgr_plugin/privileges.cpp | 28 +- server/CMakeLists.txt | 10 +- server/inc/CynaraService.h | 0 server/inc/ICommonDb.h | 0 server/inc/NotificationServer.h | 42 - server/inc/PrivacyGuardDaemon.h | 0 server/inc/PrivacyGuardDb.h | 28 +- server/inc/PrivacyInfoService.h | 18 +- server/inc/SocketService.h | 0 server/inc/privacy_guard_daemon.h | 0 server/src/CynaraService.cpp | 61 +- server/src/NotificationServer.cpp | 119 -- server/src/PrivacyGuardDaemon.cpp | 1 + server/src/PrivacyGuardDb.cpp | 429 ++++++- server/src/SocketService.cpp | 0 server/src/main.cpp | 0 server/src/privacy_guard_daemon.cpp | 0 server/src/service/PrivacyInfoService.cpp | 49 +- test/CMakeLists.txt | 29 + test/tc-privacy-guard.c | 1499 ++++++++++++++++++++++ 55 files changed, 2210 insertions(+), 568 deletions(-) mode change 100644 => 100755 CMakeLists.txt mode change 100644 => 100755 client/inc/PrivacyChecker.h mode change 100644 => 100755 client/inc/PrivacyGuardClient.h mode change 100644 => 100755 client/inc/SocketClient.h mode change 100644 => 100755 client/inc/privacy_guard_client_internal.h mode change 100644 => 100755 client/src/PrivacyChecker.cpp mode change 100644 => 100755 client/src/PrivacyGuardClient.cpp mode change 100644 => 100755 client/src/SocketClient.cpp mode change 100644 => 100755 client/src/privacy_guard_client.cpp mode change 100644 => 100755 client/src/privacy_guard_client_internal_types.h mode change 100644 => 100755 common/inc/IPrivacyGuard.h mode change 100644 => 100755 common/inc/PrivacyGuardCommon.h mode change 100644 => 100755 common/inc/PrivacyGuardTypes.h mode change 100644 => 100755 common/inc/PrivacyIdInfo.h mode change 100644 => 100755 common/inc/SocketConnection.h mode change 100644 => 100755 common/inc/SocketStream.h mode change 100644 => 100755 common/inc/Utils.h mode change 100644 => 100755 common/src/PrivacyIdInfo.cpp mode change 100644 => 100755 common/src/SocketConnection.cpp mode change 100644 => 100755 common/src/SocketStream.cpp mode change 100644 => 100755 common/src/Utils.cpp mode change 100644 => 100755 include/CMakeLists.txt mode change 100644 => 100755 include/privacy_guard_client.h mode change 100644 => 100755 include/privacy_guard_client_types.h mode change 100644 => 100755 packaging/libprivacy-guard-client.manifest mode change 100644 => 100755 packaging/privacy-guard-client-devel.manifest mode change 100644 => 100755 packaging/privacy-guard-client.manifest mode change 100644 => 100755 packaging/privacy-guard-server-devel.manifest mode change 100644 => 100755 packaging/privacy-guard-server.manifest mode change 100644 => 100755 packaging/privacy-guard-server.service mode change 100644 => 100755 packaging/privacy-guard-server.socket mode change 100644 => 100755 packaging/privacy-guard.changes mode change 100644 => 100755 packaging/privacy-guard.spec mode change 100644 => 100755 pkgmgr_plugin/CMakeLists.txt mode change 100644 => 100755 pkgmgr_plugin/privileges.cpp mode change 100644 => 100755 server/CMakeLists.txt mode change 100644 => 100755 server/inc/CynaraService.h mode change 100644 => 100755 server/inc/ICommonDb.h delete mode 100644 server/inc/NotificationServer.h mode change 100644 => 100755 server/inc/PrivacyGuardDaemon.h mode change 100644 => 100755 server/inc/PrivacyGuardDb.h mode change 100644 => 100755 server/inc/PrivacyInfoService.h mode change 100644 => 100755 server/inc/SocketService.h mode change 100644 => 100755 server/inc/privacy_guard_daemon.h mode change 100644 => 100755 server/src/CynaraService.cpp delete mode 100644 server/src/NotificationServer.cpp mode change 100644 => 100755 server/src/PrivacyGuardDaemon.cpp mode change 100644 => 100755 server/src/PrivacyGuardDb.cpp mode change 100644 => 100755 server/src/SocketService.cpp mode change 100644 => 100755 server/src/main.cpp mode change 100644 => 100755 server/src/privacy_guard_daemon.cpp mode change 100644 => 100755 server/src/service/PrivacyInfoService.cpp create mode 100755 test/CMakeLists.txt create mode 100755 test/tc-privacy-guard.c diff --git a/CMakeLists.txt b/CMakeLists.txt old mode 100644 new mode 100755 index 4af725f..9d20264 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,3 +17,4 @@ ADD_DEFINITIONS("-DAPI_VERSION=\"$(API_VERSION)\"") ADD_SUBDIRECTORY(server) ADD_SUBDIRECTORY(client) ADD_SUBDIRECTORY(pkgmgr_plugin) +ADD_SUBDIRECTORY(test) diff --git a/client/CMakeLists.txt b/client/CMakeLists.txt index 3453354..8c9f025 100755 --- a/client/CMakeLists.txt +++ b/client/CMakeLists.txt @@ -34,17 +34,6 @@ ADD_DEFINITIONS("-fvisibility=hidden") ADD_DEFINITIONS("-Wall -Werror") ADD_DEFINITIONS("-DDLOG_ERROR_ENABLED") ADD_DEFINITIONS("-D_PRIVACY_GUARD_DEBUG") -OPTION (FILTER_LISTED_PKG "FILTER PKG BY LIST" ON) -IF(FILTER_LISTED_PKG) - MESSAGE("FILTER PKGs BY FILTERING LIST") - ADD_DEFINITIONS("-D__FILTER_LISTED_PKG") -ENDIF(FILTER_LISTED_PKG) -## SET PRIVACY_POPUP FLAG -OPTION (PRIVACY_POPUP "PRIVACY_POPUP" ON) -IF(PRIVACY_POPUP) - MESSAGE("PRIVACY_POPUP IS ENABLED") - ADD_DEFINITIONS("-D__PRIVACY_POPUP") -ENDIF(PRIVACY_POPUP) ################################################################################################### ## for libprivacy-guard-client @@ -53,6 +42,7 @@ SET(PRIVACY_GUARD_CLIENT_SOURCES ${common_src_dir}/SocketConnection.cpp ${common_src_dir}/SocketStream.cpp ${common_src_dir}/PrivacyIdInfo.cpp + ${common_src_dir}/Utils.cpp ${client_src_dir}/SocketClient.cpp ${client_src_dir}/PrivacyChecker.cpp ${client_src_dir}/PrivacyGuardClient.cpp diff --git a/client/inc/PrivacyChecker.h b/client/inc/PrivacyChecker.h old mode 100644 new mode 100755 diff --git a/client/inc/PrivacyGuardClient.h b/client/inc/PrivacyGuardClient.h old mode 100644 new mode 100755 index 72f7392..ce175f9 --- a/client/inc/PrivacyGuardClient.h +++ b/client/inc/PrivacyGuardClient.h @@ -42,8 +42,6 @@ private: static std::mutex m_singletonMutex; - std::list > m_logInfoList; - PrivacyGuardClient(); ~PrivacyGuardClient(); @@ -54,11 +52,13 @@ public: int PgAddMonitorPolicyOffline(const int userId, const std::string packageId, const std::list < std::string > privacyList, bool monitorPolicy); - int PgAddPrivacyAccessLogBeforeTerminate(void); + int PgAddPrivacyAccessLog(const int userId, const std::string packageId, const std::string privacyId, const time_t accessTime); + + int PgAddMonitorPolicy(const int userId, const std::string packageId, const std::list < std::string >& list, int monitorPolicy); - int PgAddMonitorPolicy(const int userId, const std::string pkgId, const std::list < std::string >& list, int monitorPolicy); + int PgAddMonitorPolicy(const int userId, const std::string packageId, const std::string privacyId, int monitorPolicy); - int PgDeleteAllLogsAndMonitorPolicy(void); + int PgDeleteAllLogs(void); int PgDeleteLogsByPackageId(const std::string packageId); @@ -72,7 +72,7 @@ public: int PgForeachPrivacyCountByPackageId(const int userId, const int startDate, const int endDate, const std::string packageId, std::list < std::pair > & privacyInfoList) const; - int PgForeachPrivacyPackageId(const int userId, std::list < std::string > & packageList) const; + int PgForeachPackageIdUsingPrivacy(const int userId, std::list < std::string > & packageList) const; int PgForeachPackageInfoByPrivacyId(const int userId, const std::string privacyId, std::list < package_data_s > & packageList) const; @@ -86,11 +86,9 @@ public: int PgUpdateMonitorPolicy(const int userId, const std::string packageId, const std::string privacyId, const int monitorPolicy); - int PgGetMainMonitorPolicy(const int userId, bool &mainMonitorPolicy) const; - - int PgUpdateMainMonitorPolicy(const int userId, const bool mainMonitorPolicy); + int PgGetMainMonitorPolicy(const int userId, int &mainMonitorPolicy) const; - int PgDeleteMainMonitorPolicyByUserId(const int userId); + int PgUpdateMainMonitorPolicy(const int userId, const int mainMonitorPolicy); }; #endif // _PRIVACYGUARDCLIENT_H_ \ No newline at end of file diff --git a/client/inc/SocketClient.h b/client/inc/SocketClient.h old mode 100644 new mode 100755 diff --git a/client/inc/privacy_guard_client_internal.h b/client/inc/privacy_guard_client_internal.h old mode 100644 new mode 100755 index 59d5cca..352288b --- a/client/inc/privacy_guard_client_internal.h +++ b/client/inc/privacy_guard_client_internal.h @@ -22,7 +22,6 @@ #ifndef _PRIVACY_GUARD_CLIENT_INTERNAL_H_ #define _PRIVACY_GUARD_CLIENT_INTERNAL_H_ -#include #include "privacy_guard_client_types.h" #ifdef __cplusplus @@ -35,25 +34,26 @@ extern "C" { * @param[in] user_id user ID * @param[in] package_id package ID * @param[in] privacy_id privacy ID [e.g. http://tizen.org/privacy/contact] + * @param[in] access_time access time for the privacy * @return the result of operation (ERRORCODE : success, ....) */ -EXTERN_API int privacy_guard_client_add_privacy_access_log(const int user_id, const char *package_id, const char *privacy_id); +EXTERN_API int privacy_guard_client_add_privacy_access_log(const int user_id, const char *package_id, const char *privacy_id, const time_t access_time); /** - * @fn int privacy_guard_client_delete_all_logs_and_monitor_policy(void) - * @brief clear all data from StatisticsMonitor and MonitorPolicy DB + * @fn int privacy_guard_client_delete_all_logs(void) + * @brief clear all data from StatisticsMonitor DB */ -EXTERN_API int privacy_guard_client_delete_all_logs_and_monitor_policy(void); +EXTERN_API int privacy_guard_client_delete_all_logs(void); /** - * @fn int privacy_guard_client_add_monitor_policy(const int user_id, const char *package_id, const char **privilege_list, const int monitor_policy) + * @fn int privacy_guard_client_add_monitor_policy(const int user_id, const char *package_id, const char *privacy_id, const int monitor_policy) * @brief add monitor policy by user id and specified package to MonitorPolicy DB * @param[in] user_id The user ID * @param[in] package_id The package ID - * @param[in] privilege_list The privilege list + * @param[in] privacy_id The privacy ID * @param[in] monitor_policy The monitor policy (0 or 1) */ -EXTERN_API int privacy_guard_client_add_monitor_policy(const int user_id, const char *package_id, const char **privilege_list, const int monitor_policy); +EXTERN_API int privacy_guard_client_add_monitor_policy(const int user_id, const char *package_id, const char *privacy_id, const int monitor_policy); /** * @fn int privacy_guard_client_delete_logs_by_package_id(const char *package_id) @@ -62,13 +62,6 @@ EXTERN_API int privacy_guard_client_add_monitor_policy(const int user_id, const */ EXTERN_API int privacy_guard_client_delete_logs_by_package_id(const char *package_id); -/** - * @fn int privacy_guard_client_delete_monitor_policy_by_package_id(const char *package_id) - * @brief remove statistics info by specified package from MonitorPolicy DB - * @param[in] package_id The package ID - */ -EXTERN_API int privacy_guard_client_delete_monitor_policy_by_package_id(const char *package_id); - #ifdef __cplusplus } #endif diff --git a/client/src/PrivacyChecker.cpp b/client/src/PrivacyChecker.cpp old mode 100644 new mode 100755 diff --git a/client/src/PrivacyGuardClient.cpp b/client/src/PrivacyGuardClient.cpp old mode 100644 new mode 100755 index f3e2e2a..3899005 --- a/client/src/PrivacyGuardClient.cpp +++ b/client/src/PrivacyGuardClient.cpp @@ -21,9 +21,6 @@ #include "SocketClient.h" #include "PrivacyIdInfo.h" -//#define COUNT 10 -#define COUNT 1 - #undef __READ_DB_IPC__ std::mutex PrivacyGuardClient::m_singletonMutex; @@ -31,6 +28,9 @@ PrivacyGuardClient* PrivacyGuardClient::m_pInstance = NULL; const std::string PrivacyGuardClient::INTERFACE_NAME("PrivacyInfoService"); PrivacyGuardClient::PrivacyGuardClient(void) + : m_sqlHandler(NULL) + , m_stmt(NULL) + , m_bDBOpen(false) { std::unique_ptr pSocketClient(new SocketClient(INTERFACE_NAME)); m_pSocketClient = std::move(pSocketClient); @@ -108,30 +108,26 @@ PrivacyGuardClient::PgAddMonitorPolicyOffline(const int userId, const std::strin } int -PrivacyGuardClient::PgAddPrivacyAccessLogBeforeTerminate(void) +PrivacyGuardClient::PgAddPrivacyAccessLog(const int userId, const std::string packageId, const std::string privacyId, const time_t accessTime) { int result = PRIV_GUARD_ERROR_SUCCESS; - PG_LOGD("PgAddPrivacyAccessLogBeforeTerminate, m_logInfoList.size() : %d", m_logInfoList.size()); - if(m_logInfoList.size() > 0) { - int res = m_pSocketClient->connect(); - TryReturn(res == PRIV_GUARD_ERROR_SUCCESS, res, , "connect : %d", res); + int res = m_pSocketClient->connect(); + TryReturn(res == PRIV_GUARD_ERROR_SUCCESS, res, , "connect : %d", res); - res = m_pSocketClient->call("PgAddPrivacyAccessLog", m_logInfoList, &result); - TryReturn(res == PRIV_GUARD_ERROR_SUCCESS, res, m_pSocketClient->disconnect(), "call : %d", res); + res = m_pSocketClient->call("PgAddPrivacyAccessLog", userId, packageId, privacyId, accessTime, &result); + TryReturn(res == PRIV_GUARD_ERROR_SUCCESS, res, m_pSocketClient->disconnect(), "call : %d", res); - res = m_pSocketClient->disconnect(); - TryReturn(res == PRIV_GUARD_ERROR_SUCCESS, res, , "disconnect : %d", res); - m_logInfoList.clear(); - } + res = m_pSocketClient->disconnect(); + TryReturn(res == PRIV_GUARD_ERROR_SUCCESS, res, , "disconnect : %d", res); return result; } int -PrivacyGuardClient::PgAddMonitorPolicy(const int userId, const std::string pkgId, const std::list < std::string >& list, int monitorPolicy) +PrivacyGuardClient::PgAddMonitorPolicy(const int userId, const std::string packageId, const std::list < std::string >& list, int monitorPolicy) { - PG_LOGD("userID: [%d], packageID[%s], monitorPolicy: [%d]", userId, pkgId.c_str(), monitorPolicy); + PG_LOGD("userID: [%d], packageID[%s], monitorPolicy: [%d]", userId, packageId.c_str(), monitorPolicy); std::list < std::string > privacyList; @@ -159,7 +155,7 @@ PrivacyGuardClient::PgAddMonitorPolicy(const int userId, const std::string pkgId if (isServerOperation == true) { int result = PRIV_GUARD_ERROR_SUCCESS; - res = m_pSocketClient->call("PgAddMonitorPolicy", userId, pkgId, privacyList, monitorPolicy, &result); + res = m_pSocketClient->call("PgAddMonitorPolicy", userId, packageId, privacyList, monitorPolicy, &result); TryReturn(res == PRIV_GUARD_ERROR_SUCCESS, res, m_pSocketClient->disconnect(), "call : %d", res); res = m_pSocketClient->disconnect(); @@ -167,11 +163,32 @@ PrivacyGuardClient::PgAddMonitorPolicy(const int userId, const std::string pkgId return result; } else { - return PgAddMonitorPolicyOffline(userId, pkgId, privacyList, monitorPolicy); + return PgAddMonitorPolicyOffline(userId, packageId, privacyList, monitorPolicy); } } int +PrivacyGuardClient::PgAddMonitorPolicy(const int userId, const std::string packageId, const std::string privacyId, int monitorPolicy) +{ + PG_LOGD("userID: [%d], packageID[%s], privacyID[%s], monitorPolicy: [%d]", userId, packageId.c_str(), privacyId.c_str(), monitorPolicy); + + std::list < std::string > privacyList; + privacyList.push_back(privacyId); + + int res = m_pSocketClient->connect(); + TryReturn(res == PRIV_GUARD_ERROR_SUCCESS, res, , "connect : %d", res); + + int result = PRIV_GUARD_ERROR_SUCCESS; + res = m_pSocketClient->call("PgAddMonitorPolicy", userId, packageId, privacyList, monitorPolicy, &result); + TryReturn(res == PRIV_GUARD_ERROR_SUCCESS, res, m_pSocketClient->disconnect(), "call : %d", res); + + res = m_pSocketClient->disconnect(); + TryReturn(res == PRIV_GUARD_ERROR_SUCCESS, res, , "disconnect : %d", res); + + return result; +} + +/*int PrivacyGuardClient::PgDeleteAllLogsAndMonitorPolicy(void) { int result = PRIV_GUARD_ERROR_SUCCESS; @@ -186,6 +203,23 @@ PrivacyGuardClient::PgDeleteAllLogsAndMonitorPolicy(void) TryReturn(res == PRIV_GUARD_ERROR_SUCCESS, res, , "disconnect : %d", res); return result; +}*/ + +int +PrivacyGuardClient::PgDeleteAllLogs(void) +{ + int result = PRIV_GUARD_ERROR_SUCCESS; + + int res = m_pSocketClient->connect(); + TryReturn(res == PRIV_GUARD_ERROR_SUCCESS, res, , "connect : %d", res); + + res = m_pSocketClient->call("PgDeleteAllLogs", &result); + TryReturn(res == PRIV_GUARD_ERROR_SUCCESS, res, m_pSocketClient->disconnect(), "call : %d", res); + + res = m_pSocketClient->disconnect(); + TryReturn(res == PRIV_GUARD_ERROR_SUCCESS, res, , "disconnect : %d", res); + + return result; } int @@ -296,14 +330,14 @@ PrivacyGuardClient::PgForeachPrivacyCountByPackageId(const int userId, const int } int -PrivacyGuardClient::PgForeachPrivacyPackageId(const int userId, std::list < std::string > & packageList) const +PrivacyGuardClient::PgForeachPackageIdUsingPrivacy(const int userId, std::list < std::string > & packageList) const { int result = PRIV_GUARD_ERROR_SUCCESS; int res = m_pSocketClient->connect(); TryReturn(res == PRIV_GUARD_ERROR_SUCCESS, res, , "connect : %d", res); - res = m_pSocketClient->call("PgForeachPrivacyPackageId", userId, &result, &packageList); + res = m_pSocketClient->call("PgForeachPackageIdUsingPrivacy", userId, &result, &packageList); TryReturn(res == PRIV_GUARD_ERROR_SUCCESS, res, m_pSocketClient->disconnect(), "call : %d", res); res = m_pSocketClient->disconnect(); @@ -433,7 +467,7 @@ PrivacyGuardClient::PgUpdateMonitorPolicy(const int userId, const std::string pa } int -PrivacyGuardClient::PgGetMainMonitorPolicy(const int userId, bool &mainMonitorPolicy) const +PrivacyGuardClient::PgGetMainMonitorPolicy(const int userId, int &mainMonitorPolicy) const { int result = PRIV_GUARD_ERROR_SUCCESS; @@ -450,7 +484,7 @@ PrivacyGuardClient::PgGetMainMonitorPolicy(const int userId, bool &mainMonitorPo } int -PrivacyGuardClient::PgUpdateMainMonitorPolicy(const int userId, const bool mainMonitorPolicy) +PrivacyGuardClient::PgUpdateMainMonitorPolicy(const int userId, const int mainMonitorPolicy) { int result = PRIV_GUARD_ERROR_SUCCESS; @@ -465,20 +499,3 @@ PrivacyGuardClient::PgUpdateMainMonitorPolicy(const int userId, const bool mainM return result; } - -int -PrivacyGuardClient::PgDeleteMainMonitorPolicyByUserId(const int userId) -{ - int result = PRIV_GUARD_ERROR_SUCCESS; - - int res = m_pSocketClient->connect(); - TryReturn(res == PRIV_GUARD_ERROR_SUCCESS, res, , "connect : %d", res); - - res = m_pSocketClient->call("PgDeleteMainMonitorPolicyByUserId", userId, &result); - TryReturn(res == PRIV_GUARD_ERROR_SUCCESS, res, m_pSocketClient->disconnect(), "call : %d", res); - - res = m_pSocketClient->disconnect(); - TryReturn(res == PRIV_GUARD_ERROR_SUCCESS, res, , "disconnect : %d", res); - - return result; -} diff --git a/client/src/SocketClient.cpp b/client/src/SocketClient.cpp old mode 100644 new mode 100755 index 542e3f7..64a4948 --- a/client/src/SocketClient.cpp +++ b/client/src/SocketClient.cpp @@ -33,6 +33,7 @@ } while(0) SocketClient::SocketClient(const std::string& interfaceName) + : m_socketFd(-1) { m_interfaceName = interfaceName; m_serverAddress = SERVER_ADDRESS; diff --git a/client/src/privacy_guard_client.cpp b/client/src/privacy_guard_client.cpp old mode 100644 new mode 100755 index 639c560..980f69c --- a/client/src/privacy_guard_client.cpp +++ b/client/src/privacy_guard_client.cpp @@ -17,7 +17,6 @@ #include #include #include -#include #include "PrivacyChecker.h" #include "PrivacyGuardClient.h" #include "privacy_guard_client.h" @@ -36,7 +35,7 @@ #define TIZEN_PATH_MAX 1024 #endif -int privacy_guard_client_delete_all_logs_and_monitor_policy(void) +/*int privacy_guard_client_delete_all_logs_and_monitor_policy(void) { PrivacyGuardClient *pInst = PrivacyGuardClient::getInstance(); @@ -47,27 +46,22 @@ int privacy_guard_client_delete_all_logs_and_monitor_policy(void) } return PRIV_GUARD_ERROR_SUCCESS; -} +}*/ -int privacy_guard_client_delete_logs_by_package_id(const char *package_id) +int privacy_guard_client_delete_all_logs(void) { - if (package_id == NULL) { - PG_LOGE("Invalid parameters. (package_id)"); - return PRIV_GUARD_ERROR_INVALID_PARAMETER; - } - PrivacyGuardClient *pInst = PrivacyGuardClient::getInstance(); - int retval = pInst->PgDeleteLogsByPackageId(std::string(package_id)); + int retval = pInst->PgDeleteAllLogs(); if (retval != PRIV_GUARD_ERROR_SUCCESS) { - PG_LOGE("Failed to do PrivacyGuardClient::PgDeleteLogsByPackageId() [%d]", retval); + PG_LOGE("Failed to do PrivacyGuardClient::PgDeleteAllLogs() [%d]", retval); return retval; } return PRIV_GUARD_ERROR_SUCCESS; } -int privacy_guard_client_delete_monitor_policy_by_package_id(const char *package_id) +int privacy_guard_client_delete_logs_by_package_id(const char *package_id) { if (package_id == NULL) { PG_LOGE("Invalid parameters. (package_id)"); @@ -76,9 +70,9 @@ int privacy_guard_client_delete_monitor_policy_by_package_id(const char *package PrivacyGuardClient *pInst = PrivacyGuardClient::getInstance(); - int retval = pInst->PgDeleteMonitorPolicyByPackageId(std::string(package_id)); + int retval = pInst->PgDeleteLogsByPackageId(std::string(package_id)); if (retval != PRIV_GUARD_ERROR_SUCCESS) { - PG_LOGE("Failed to do PrivacyGuardClient::PgDeleteMonitorPolicyByPackageId() [%d]", retval); + PG_LOGE("Failed to do PrivacyGuardClient::PgDeleteLogsByPackageId() [%d]", retval); return retval; } @@ -102,9 +96,6 @@ int privacy_guard_client_foreach_total_privacy_count_of_package(const int user_i return retval; } - if (list.size() == 0) - return PRIV_GUARD_ERROR_NO_DATA; - for (std::list >::iterator iter = list.begin(); iter != list.end(); ++iter) { //PG_LOGD("result > package_id : %s, count : %d", iter->first.c_str(), iter->second); bool ret = callback(iter->first.c_str(), iter->second, user_data); @@ -132,9 +123,6 @@ int privacy_guard_client_foreach_total_privacy_count_of_privacy(const int user_i return retval; } - if (list.size() == 0) - return PRIV_GUARD_ERROR_NO_DATA; - for (std::list >::iterator iter = list.begin(); iter != list.end(); ++iter) { PG_LOGD("privacy_id: %s, count: %d", iter->first.c_str(), iter->second); bool ret = callback(iter->first.c_str(), iter->second, user_data); @@ -163,9 +151,6 @@ int privacy_guard_client_foreach_privacy_count_by_privacy_id(const int user_id, return retval; } - if (list.size() == 0) - return PRIV_GUARD_ERROR_NO_DATA; - for (std::list >::iterator iter = list.begin(); iter != list.end(); ++iter) { PG_LOGD("package_id: %s, count: %d", iter->first.c_str(), iter->second); bool ret = callback(iter->first.c_str(), iter->second, user_data); @@ -194,9 +179,6 @@ int privacy_guard_client_foreach_privacy_count_by_package_id(const int user_id, return retval; } - if (list.size() == 0) - return PRIV_GUARD_ERROR_NO_DATA; - for (std::list >::iterator iter = list.begin(); iter != list.end(); ++iter) { PG_LOGD("privacy_id: %s, count: %d", iter->first.c_str(), iter->second); bool ret = callback(iter->first.c_str(), iter->second, user_data); @@ -207,67 +189,16 @@ int privacy_guard_client_foreach_privacy_count_by_package_id(const int user_id, return retval; } -int privacy_guard_client_add_monitor_policy(const int user_id, const char *package_id, const char **privilege_list, const int monitor_policy) -{ - PG_LOGD("user_id: [%d], package_id: [%s], monitor_policy: [%d]", user_id, package_id, monitor_policy); - - if (user_id < 0 || package_id == NULL || privilege_list == NULL) { - PG_LOGE("Invalid parameters."); - return PRIV_GUARD_ERROR_INVALID_PARAMETER; - } - - PrivacyGuardClient *pInst = PrivacyGuardClient::getInstance(); - std::list < std::string > privilegeList; - - while (*privilege_list[0] != '\0') { - PG_LOGD("Privilege in the list: %s", *privilege_list); - privilegeList.push_back(std::string(*privilege_list++)); - } - - int retval = pInst->PgAddMonitorPolicy(user_id, std::string(package_id), privilegeList, monitor_policy); - if (retval != PRIV_GUARD_ERROR_SUCCESS) { - PG_LOGE("Failed to do PrivacyGuardClient::PgAddMonitorPolicy() [%d]", retval); - return retval; - } - - return PRIV_GUARD_ERROR_SUCCESS; -} - int privacy_guard_client_update_monitor_policy(const int user_id, const char *package_id, const char *privacy_id, const int monitor_policy) { - if (user_id < 0 || package_id == NULL || privacy_id == NULL) { - PG_LOGE("Invalid parameters."); + if (user_id < 0 || package_id == NULL || privacy_id == NULL || monitor_policy < 0) { + PG_LOGE("Invalid parameters. UserID[%d], PkgID[%s], PrivacyID[%s], Policy[%d]", user_id, package_id, privacy_id, monitor_policy); return PRIV_GUARD_ERROR_INVALID_PARAMETER; } PrivacyGuardClient *pInst = PrivacyGuardClient::getInstance(); - pkgmgrinfo_pkginfo_h pkg_handle; - bool is_global = false; - int fixed_user_id = -1; - - int retval = pkgmgrinfo_pkginfo_get_pkginfo(package_id, &pkg_handle); - if (retval != PMINFO_R_OK) { - PG_LOGE("Failed to do pkgmgrinfo_pkginfo_get_pkginfo [%d]", retval); - return retval; - } - - retval = pkgmgrinfo_pkginfo_is_global(pkg_handle, &is_global); - if (retval != PMINFO_R_OK) { - PG_LOGE("Failed to do pkgmgrinfo_pkginfo_is_global [%d]", retval); - return retval; - } - - pkgmgrinfo_pkginfo_destroy_pkginfo(pkg_handle); - - if (is_global == true) { - PG_LOGD("[%s] is global app.", package_id); - fixed_user_id = 0; - } else { - fixed_user_id = user_id; - } - - retval = pInst->PgUpdateMonitorPolicy(fixed_user_id, std::string(package_id), std::string(privacy_id), monitor_policy); + int retval = pInst->PgUpdateMonitorPolicy(user_id, std::string(package_id), std::string(privacy_id), monitor_policy); if (retval != PRIV_GUARD_ERROR_SUCCESS) { PG_LOGE("Failed to do PrivacyGuardClient::PgUpdateMonitorPolicy() [%d]", retval); return retval; @@ -293,9 +224,6 @@ int privacy_guard_client_foreach_monitor_policy_by_package_id(const int user_id, return retval; } - if (privacyInfoList.size() == 0) - return PRIV_GUARD_ERROR_NO_DATA; - for (std::list ::iterator iter = privacyInfoList.begin(); iter != privacyInfoList.end(); ++iter) { PG_LOGD("privacy_id: %s, monitor_policy: %d", iter->privacy_id, iter->monitor_policy); bool ret = callback(iter->privacy_id, iter->monitor_policy, user_data); @@ -326,7 +254,7 @@ int privacy_guard_client_check_privacy_package(const int user_id, const char *pa return PRIV_GUARD_ERROR_SUCCESS; } -int privacy_guard_client_foreach_privacy_package_id(const int user_id, privacy_guard_client_package_id_cb callback, void *user_data) +int privacy_guard_client_foreach_package_id_using_privacy(const int user_id, privacy_guard_client_package_id_cb callback, void *user_data) { if (user_id < 0) { PG_LOGE("Invalid parameters. (user_id: %d)", user_id); @@ -337,15 +265,12 @@ int privacy_guard_client_foreach_privacy_package_id(const int user_id, privacy_g std::list < std::string > packageList; - int retval = pInst->PgForeachPrivacyPackageId(user_id, packageList); + int retval = pInst->PgForeachPackageIdUsingPrivacy(user_id, packageList); if (retval != PRIV_GUARD_ERROR_SUCCESS) { PG_LOGE("Failed to do PrivacyGuardClient::PgForeachPrivacyPackageId(). [%d]", retval); return retval; } - if (packageList.size() == 0) - return PRIV_GUARD_ERROR_NO_DATA; - for (std::list < std::string >::iterator iter = packageList.begin(); iter != packageList.end(); ++iter) { PG_LOGD("package_id: %s", iter->c_str()); bool ret = callback(iter->c_str(), user_data); @@ -363,6 +288,12 @@ int privacy_guard_client_foreach_package_info_by_privacy_id(const int user_id, c return PRIV_GUARD_ERROR_INVALID_PARAMETER; } +// int res = Utils::PgCheckAccess(); +// if(res != PRIV_GUARD_ERROR_SUCCESS) { +// PG_LOGE("PgCheckAccess() is failed. [%d]", res); +// return res; +// } + PrivacyGuardClient* pInst = PrivacyGuardClient::getInstance(); std::list packageInfoList; @@ -373,11 +304,6 @@ int privacy_guard_client_foreach_package_info_by_privacy_id(const int user_id, c return retval; } - if (packageInfoList.size() == 0) { - PG_LOGE("The size of package info list is 0."); - return PRIV_GUARD_ERROR_NO_DATA; - } - for (std::list < package_data_s >::iterator iter = packageInfoList.begin(); iter != packageInfoList.end(); ++iter) { bool ret = callback(iter->package_id, iter->count, iter->time, iter->monitor_policy, user_data); if (ret == false) @@ -387,56 +313,41 @@ int privacy_guard_client_foreach_package_info_by_privacy_id(const int user_id, c return PRIV_GUARD_ERROR_SUCCESS; } -int privacy_guard_client_update_main_monitor_policy(const int user_id, const bool main_monitor_policy) +//////////////////////// INTERNAL APIs //////////////////////////////////// +int privacy_guard_client_add_privacy_access_log(const int user_id, const char *package_id, const char *privacy_id, const time_t access_time) { - if (user_id < 0) { - PG_LOGE("Invalid parameters. (user_id: %d)", user_id); + if (user_id < 0 || package_id == NULL || privacy_id == NULL || access_time < 0) { + PG_LOGE("Invalid parameters. [user_id: %d, package_id: %s, privacy_id: %s, time: %ld", user_id, package_id, privacy_id, access_time); return PRIV_GUARD_ERROR_INVALID_PARAMETER; } - PrivacyGuardClient *pInst = PrivacyGuardClient::getInstance(); + PrivacyGuardClient *pInst = PrivacyGuardClient::getInstance(); - int retval = pInst->PgUpdateMainMonitorPolicy(user_id, main_monitor_policy); + int retval = pInst->PgAddPrivacyAccessLog(user_id, package_id, privacy_id, access_time); if (retval != PRIV_GUARD_ERROR_SUCCESS) { - PG_LOGE("Failed to do PrivacyGuardClient::PgUpdateMainMonitorPolicy() [%d]", retval); + PG_LOGE("Failed to do PrivacyGuardClient::PgAddPrivacyAccessLog() [%d]", retval); return retval; } - return PRIV_GUARD_ERROR_SUCCESS; + return PRIV_GUARD_ERROR_SUCCESS; } -int privacy_guard_client_get_main_monitor_policy(const int user_id, bool *main_monitor_policy) +int privacy_guard_client_add_monitor_policy(const int user_id, const char *package_id, const char *privacy_id, const int monitor_policy) { - if (user_id < 0) { - PG_LOGE("Invalid parameters. (user_id: %d)", user_id); + if (user_id < 0 || package_id == NULL || privacy_id == NULL || monitor_policy < 0 || monitor_policy > 1) { + PG_LOGE("Invalid parameters."); return PRIV_GUARD_ERROR_INVALID_PARAMETER; } - PrivacyGuardClient* pInst = PrivacyGuardClient::getInstance(); + PrivacyGuardClient *pInst = PrivacyGuardClient::getInstance(); - int retval = pInst->PgGetMainMonitorPolicy(user_id, *main_monitor_policy); + int retval = pInst->PgAddMonitorPolicy(user_id, package_id, privacy_id, monitor_policy); if (retval != PRIV_GUARD_ERROR_SUCCESS) { - PG_LOGE("Failed to do PrivacyGuardClient::PgGetMainMonitorPolicy(). [%d]", retval); + PG_LOGE("Failed to do PrivacyGuardClient::PgAddMonitorPolicy() [%d]", retval); return retval; } - return PRIV_GUARD_ERROR_SUCCESS; + return PRIV_GUARD_ERROR_SUCCESS; } -int privacy_guard_client_delete_main_monitor_policy_by_user_id(const int user_id) -{ - if (user_id < 0) { - PG_LOGE("Invalid parameters. (user_id: %d)", user_id); - return PRIV_GUARD_ERROR_INVALID_PARAMETER; - } - - PrivacyGuardClient *pInst = PrivacyGuardClient::getInstance(); - int retval = pInst->PgDeleteMainMonitorPolicyByUserId(user_id); - if (retval != PRIV_GUARD_ERROR_SUCCESS) { - PG_LOGE("Failed to do PrivacyGuardClient::PgDeleteMainMonitorPolicyByUserId(). [%d]", retval); - return retval; - } - - return PRIV_GUARD_ERROR_SUCCESS; -} diff --git a/client/src/privacy_guard_client_internal_types.h b/client/src/privacy_guard_client_internal_types.h old mode 100644 new mode 100755 diff --git a/common/inc/IPrivacyGuard.h b/common/inc/IPrivacyGuard.h old mode 100644 new mode 100755 diff --git a/common/inc/PrivacyGuardCommon.h b/common/inc/PrivacyGuardCommon.h old mode 100644 new mode 100755 diff --git a/common/inc/PrivacyGuardTypes.h b/common/inc/PrivacyGuardTypes.h old mode 100644 new mode 100755 index 0483cae..209ee30 --- a/common/inc/PrivacyGuardTypes.h +++ b/common/inc/PrivacyGuardTypes.h @@ -21,27 +21,30 @@ #include #include "privacy_guard_client_types.h" -#define PRIVACY_DB_PATH tzplatform_mkpath(TZ_SYS_DB,".privacy_guard.db") - -#define CYNARA_BUFFER_SIZE 100 -#define USER_APP_PREFIX "User::App::" -#define USER_APP_PREFIX_LEN 11 -#define USER_PKG_PREFIX "User::Pkg::" -#define USER_PKG_PREFIX_LEN 11 -#define GLOBAL_USER tzplatform_getuid(TZ_SYS_GLOBALAPP_USER) +#define PRIVACY_DB_PATH tzplatform_mkpath(TZ_SYS_DB,".privacy_guard.db") +#define GLOBAL_USER tzplatform_getuid(TZ_SYS_GLOBALAPP_USER) +#define LEN_PRIVACY_ID 128 +#define LEN_PACKAGE_ID 128 +#define CYNARA_BUFFER_SIZE 100 +#define DEFAULT_MONITOR_POLICY 0 typedef struct _privacy_data_s { - char *privacy_id; + char privacy_id[LEN_PRIVACY_ID]; int monitor_policy; } privacy_data_s; typedef struct _package_data_s { - char *package_id; + char package_id[LEN_PACKAGE_ID]; int count; time_t time; int monitor_policy; } package_data_s; +typedef struct _package_privacy_data_s { + char package_id[LEN_PACKAGE_ID]; + char privacy_id[LEN_PRIVACY_ID]; +} package_privacy_data_s; + static const std::string SERVER_ADDRESS ("/tmp/privacy_guard_server"); static const std::string DBUS_PATH("/privacy_guard/dbus_notification"); static const std::string DBUS_SIGNAL_INTERFACE("org.tizen.privacy_guard.signal"); diff --git a/common/inc/PrivacyIdInfo.h b/common/inc/PrivacyIdInfo.h old mode 100644 new mode 100755 diff --git a/common/inc/SocketConnection.h b/common/inc/SocketConnection.h old mode 100644 new mode 100755 index b7aeca3..08dd212 --- a/common/inc/SocketConnection.h +++ b/common/inc/SocketConnection.h @@ -212,7 +212,8 @@ public: m_socketStream.readStream(length, pBuf); TryReturn(res == PRIV_GUARD_ERROR_SUCCESS, res, delete[] pBuf, "readStream : %d", res); pBuf[length] = 0; - out.privacy_id = strdup(pBuf); + memset(out.privacy_id, 0x0, LEN_PRIVACY_ID); + strncpy(out.privacy_id, pBuf, length + 1); delete[] pBuf; // monitor policy @@ -236,7 +237,8 @@ public: m_socketStream.readStream(length, pBuf); TryReturn(res == PRIV_GUARD_ERROR_SUCCESS, res, delete[] pBuf, "readStream : %d", res); pBuf[length] = 0; - out.package_id = strdup(pBuf); + memset(out.package_id, 0x0, LEN_PACKAGE_ID); + strncpy(out.package_id, pBuf, length + 1); delete[] pBuf; // monitor policy diff --git a/common/inc/SocketStream.h b/common/inc/SocketStream.h old mode 100644 new mode 100755 diff --git a/common/inc/Utils.h b/common/inc/Utils.h old mode 100644 new mode 100755 index c50c85c..c0d1851 --- a/common/inc/Utils.h +++ b/common/inc/Utils.h @@ -126,7 +126,7 @@ static const int SLEEP_TIME = 50000; class Utils { public: - static std::string toHash (std::string src); + static int PgCheckAccess(void); }; #endif //_UTILS_H_ diff --git a/common/src/PrivacyIdInfo.cpp b/common/src/PrivacyIdInfo.cpp old mode 100644 new mode 100755 index f38f6e3..27f2882 --- a/common/src/PrivacyIdInfo.cpp +++ b/common/src/PrivacyIdInfo.cpp @@ -43,7 +43,7 @@ PrivacyIdInfo::initialize(void) ret = privilege_info_get_privilege_list_by_privacy(privacy_id, &privilege_list); if (ret != PRVMGR_ERR_NONE) { PG_LOGE("Failed to get privilege list from security-privilege-manager [%d] using privacy[%s].", ret, privacy_id); - g_list_free(privacy_list); + g_list_free_full(privacy_list, g_free); return PRIV_GUARD_ERROR_INTERNAL_ERROR; } @@ -52,10 +52,13 @@ PrivacyIdInfo::initialize(void) //PG_LOGD("(privacy, privilege): (%s, %s)", privacy_id, privilege_id); m_privilegeToPrivacyMap.insert(std::map< std::string, std::string >::value_type(std::string(privilege_id), std::string(privacy_id))); } + + g_list_free_full(privilege_list, g_free); + privilege_list = NULL; } - g_list_free(privacy_list); - g_list_free(privilege_list); + g_list_free_full(privacy_list, g_free); + privacy_list = NULL; m_isInitialized = true; @@ -69,9 +72,10 @@ PrivacyIdInfo::getPrivacyIdFromPrivilege(const std::string privilege, std::strin initialize(); } +#if 0 // the below API "privilege_info_get_privacy_by_privilege" accesses DB on every call. This may degrade entire performance. int ret; - char *privacy_id = NULL; + ret = privilege_info_get_privacy_by_privilege(privilege.c_str(), &privacy_id); if (ret != PRVMGR_ERR_NONE) { //PG_LOGE("Failed to do privilege_info_get_privacy_by_privilege [%d]", ret); @@ -80,6 +84,15 @@ PrivacyIdInfo::getPrivacyIdFromPrivilege(const std::string privilege, std::strin privacyId = privacy_id; free(privacy_id); +#else + std::map< std::string, std::string >::iterator iter = m_privilegeToPrivacyMap.find(privilege); + if (iter == m_privilegeToPrivacyMap.end()) { + //PG_LOGD("There is no matching privacy to privilege [%s]", privilege.c_str()); + return PRIV_GUARD_ERROR_NO_DATA; + } + + privacyId = iter->second; +#endif return PRIV_GUARD_ERROR_SUCCESS; } @@ -146,9 +159,12 @@ PrivacyIdInfo::getAllPrivacyId(std::list< std::string >& privacyIdList) for (l = privacy_list; l != NULL; l = l->next) { char *privacy_id = (char*)l->data; - PG_LOGD("[kylee] privacy_id: %s", privacy_id); + PG_LOGD("privacy_id: %s", privacy_id); privacyIdList.push_back(std::string(privacy_id)); } + g_list_free_full(privacy_list, g_free); + privacy_list = NULL; + return PRIV_GUARD_ERROR_SUCCESS; } diff --git a/common/src/SocketConnection.cpp b/common/src/SocketConnection.cpp old mode 100644 new mode 100755 diff --git a/common/src/SocketStream.cpp b/common/src/SocketStream.cpp old mode 100644 new mode 100755 diff --git a/common/src/Utils.cpp b/common/src/Utils.cpp old mode 100644 new mode 100755 index af8052e..6c4922e --- a/common/src/Utils.cpp +++ b/common/src/Utils.cpp @@ -15,8 +15,38 @@ */ #include "Utils.h" +#include "privacy_guard_client_types.h" +#include -std::string Utils::toHash(std::string src) +const char pApprovedAppList[][64] = { + "org.tizen.privacy-setting", + "tc-privacy-guard" + }; + +int Utils::PgCheckAccess(void) { - return src; + pid_t pid = getpid(); + PG_LOGD("Process ID: %d", pid); + + char appId[128] = {0, }; + + aul_app_get_appid_bypid_for_uid(pid, appId, sizeof(appId), getuid()); + PG_LOGD("App ID: %s", appId); + + bool isApprovedApp = false; + int len = sizeof(pApprovedAppList) / sizeof(pApprovedAppList[0]); + + for(int i = 0; i < len; i++ ) { + if(strncmp(appId, pApprovedAppList[i], strlen(pApprovedAppList[i])) == 0) { + isApprovedApp = true; + } + } + + if(isApprovedApp == false) { + PG_LOGE("Abnormal Access"); + return PRIV_GUARD_ERROR_NOT_PERMITTED; + } + PG_LOGD("Normal Access"); + + return PRIV_GUARD_ERROR_SUCCESS; } \ No newline at end of file diff --git a/include/CMakeLists.txt b/include/CMakeLists.txt old mode 100644 new mode 100755 index 70766ea..108f858 --- a/include/CMakeLists.txt +++ b/include/CMakeLists.txt @@ -1,11 +1,11 @@ CMAKE_MINIMUM_REQUIRED(VERSION 2.6) #PROJECT(privacy-guard-client) -SET(PF_INCLUDE_PATH ${PROJECT_SOURCE_DIR}/include) +SET(PG_INCLUDE_PATH ${PROJECT_SOURCE_DIR}/include) SET(PRIVACY_GUARD_CLIENT_HEADERS - ${PF_INCLUDE_PATH}/privacy_guard_client.h - ${PF_INCLUDE_PATH}/privacy_guard_client_types.h + ${PG_INCLUDE_PATH}/privacy_guard_client.h + ${PG_INCLUDE_PATH}/privacy_guard_client_types.h ) INSTALL(FILES ${PRIVACY_GUARD_CLIENT_HEADERS} DESTINATION ${INCLUDE_INSTALL_DIR}/privacy_guard) diff --git a/include/privacy_guard_client.h b/include/privacy_guard_client.h old mode 100644 new mode 100755 index 55c6ec9..59e2276 --- a/include/privacy_guard_client.h +++ b/include/privacy_guard_client.h @@ -22,7 +22,6 @@ #ifndef _PRIVACY_GUARD_CLIENT_H_ #define _PRIVACY_GUARD_CLIENT_H_ -#include #include "privacy_guard_client_types.h" #ifdef __cplusplus @@ -60,18 +59,17 @@ typedef bool (*privacy_guard_client_package_id_cb) (const char *package_id, void * @brief Called when the reply of the package id request is delivered. * @since tizen 3.0 * - * @param[in] package_id The package ID - * @param[in] count The privacy count - * @param[in] time The latest access time - * @param[in] monitor_policy The monitor policy (0 or 1) + * @param[in] package_id The package ID + * @param[in] count The privacy count + * @param[in] latest_access_time The latest access time + * @param[in] monitor_policy The monitor policy (0 or 1) * @param[in] user_data The user data passed from the callback registration function * * @returns: true to continue with the next iteration of the loop, otherwise return false to break out of the loop * * @see privacy_guard_client_foreach_package_info_by_privacy_id() */ -typedef bool (*privacy_guard_client_package_info_cb) (const char *package_id, const int count, const time_t time, const int monitor_policy, void *user_data); - +typedef bool (*privacy_guard_client_package_info_cb) (const char *package_id, const int count, const time_t latest_access_time, const int monitor_policy, void *user_data); /** * @brief Called when the reply of the privacy count request is delivered. @@ -165,8 +163,7 @@ EXTERN_API int privacy_guard_client_update_monitor_policy(const int user_id, con * @param[in] package_id The package ID * @param[in] callback The callback function to invoke */ -EXTERN_API int privacy_guard_client_foreach_monitor_policy_by_package_id(const int user_id, const char *package_id, - privacy_guard_client_monitor_policy_cb callback, void *user_data); +EXTERN_API int privacy_guard_client_foreach_monitor_policy_by_package_id(const int user_id, const char *package_id, privacy_guard_client_monitor_policy_cb callback, void *user_data); /** * @fn int privacy_guard_client_check_privacy_package(const int user_id, const char *package_id, bool *is_privacy_package) @@ -185,10 +182,10 @@ EXTERN_API int privacy_guard_client_check_privacy_package(const int user_id, con * @param[in] callback The callback function to invoke * @param[in] user_data The user data to be passed to the callback function */ -EXTERN_API int privacy_guard_client_foreach_privacy_package_id(const int user_id, privacy_guard_client_package_id_cb callback, void *user_data); +EXTERN_API int privacy_guard_client_foreach_package_id_using_privacy(const int user_id, privacy_guard_client_package_id_cb callback, void *user_data); /** - * @fn int privacy_guard_client_foreach_package_by_privacy_id(const int user_id, const char *privacy_id, privacy_guard_client_package_id_cb callback, void *user_data) + * @fn int privacy_guard_client_foreach_package_info_by_privacy_id(const int user_id, const char *privacy_id, privacy_guard_client_package_id_cb callback, void *user_data) * @brief get package info (package_id, count, monitor_enable) using specified privacy * @param[in] user_id The user ID * @param[in] privacy_id The privacy ID @@ -197,31 +194,8 @@ EXTERN_API int privacy_guard_client_foreach_privacy_package_id(const int user_id */ EXTERN_API int privacy_guard_client_foreach_package_info_by_privacy_id(const int user_id, const char *privacy_id, privacy_guard_client_package_info_cb callback, void *user_data); -/** - * @fn int privacy_guard_client_update_main_monitor_policy(const int user_id, const bool main_monitor_policy) - * @brief update main monitor policy - * @param[in] user_id The user ID - * @param[in] main_monitor_policy The main monitor policy (false or true) to be set - */ -EXTERN_API int privacy_guard_client_update_main_monitor_policy(const int user_id, const bool main_monitor_policy); - -/** - * @fn int privacy_guard_client_update_main_monitor_policy(const int user_id, const bool main_monitor_policy) - * @brief get main monitor policy - * @param[in] user_id The user ID - * @param[out] main_monitor_policy The value of main monitor policy - */ -EXTERN_API int privacy_guard_client_get_main_monitor_policy(const int user_id, bool *main_monitor_policy); - -/** - * @fn int privacy_guard_client_update_main_monitor_policy(const int user_id, const bool main_monitor_policy) - * @brief Delete main monitor policy - * @param[in] user_id The user ID to be deleted - */ -EXTERN_API int privacy_guard_client_delete_main_monitor_policy_by_user_id(const int user_id); #ifdef __cplusplus } #endif - #endif //_PRIVACY_GUARD_CLIENT_H_ diff --git a/include/privacy_guard_client_types.h b/include/privacy_guard_client_types.h old mode 100644 new mode 100755 index afd8fcb..0a95ce7 --- a/include/privacy_guard_client_types.h +++ b/include/privacy_guard_client_types.h @@ -17,6 +17,9 @@ #ifndef _PRIVACY_GUARD_CLIENT_TYPES_H_ #define _PRIVACY_GUARD_CLIENT_TYPES_H_ +#include +#include + #ifdef __cplusplus extern "C" { #endif @@ -39,11 +42,11 @@ enum { PRIV_GUARD_ERROR_SYSTEM_ERROR = -18, PRIV_GUARD_ERROR_USER_NOT_CONSENTED = -19, PRIV_GUARD_ERROR_INTERNAL_ERROR = -20, + PRIV_GUARD_ERROR_NOT_PERMITTED = -21, PRIV_GUARD_ERROR_UNKNOWN = -(0x99), }; - #ifdef __cplusplus } #endif diff --git a/packaging/libprivacy-guard-client.manifest b/packaging/libprivacy-guard-client.manifest old mode 100644 new mode 100755 diff --git a/packaging/privacy-guard-client-devel.manifest b/packaging/privacy-guard-client-devel.manifest old mode 100644 new mode 100755 diff --git a/packaging/privacy-guard-client.manifest b/packaging/privacy-guard-client.manifest old mode 100644 new mode 100755 diff --git a/packaging/privacy-guard-server-devel.manifest b/packaging/privacy-guard-server-devel.manifest old mode 100644 new mode 100755 diff --git a/packaging/privacy-guard-server.manifest b/packaging/privacy-guard-server.manifest old mode 100644 new mode 100755 diff --git a/packaging/privacy-guard-server.service b/packaging/privacy-guard-server.service old mode 100644 new mode 100755 diff --git a/packaging/privacy-guard-server.socket b/packaging/privacy-guard-server.socket old mode 100644 new mode 100755 diff --git a/packaging/privacy-guard.changes b/packaging/privacy-guard.changes old mode 100644 new mode 100755 diff --git a/packaging/privacy-guard.spec b/packaging/privacy-guard.spec old mode 100644 new mode 100755 index 8edf16b..a24996a --- a/packaging/privacy-guard.spec +++ b/packaging/privacy-guard.spec @@ -1,5 +1,5 @@ Name: privacy-guard-server -Version: 0.0.5 +Version: 0.5.0 Release: 1 License: Apache-2.0 Summary: Privacy Management @@ -28,6 +28,7 @@ BuildRequires: pkgconfig(security-privilege-manager) BuildRequires: pkgconfig(cynara-monitor) BuildRequires: pkgconfig(pkgmgr-installer) BuildRequires: pkgconfig(security-manager) +BuildRequires: pkgconfig(aul) Requires(post): /sbin/ldconfig Requires(postun): /sbin/ldconfig @@ -43,7 +44,7 @@ Requires: privacy-guard-server = %{version} privacy-guard server devel %package -n privacy-guard-client -Summary: Privacy Guard client +Summary: Privacy Guard Client Requires: privacy-guard-server = %{version} %description -n privacy-guard-client @@ -56,6 +57,11 @@ Requires: privacy-guard-client = %{version} %description -n privacy-guard-client-devel Privacy Management(development files) +%package -n tc-privacy-guard +Summary: Test Case for Privacy Guard + +%description -n tc-privacy-guard +test cases for privacy-guard package %prep %setup -q @@ -108,7 +114,6 @@ chsmack -a System %{TZ_SYS_DB}/.privacy_guard.db-journal /usr/sbin/setcap cap_chown,cap_dac_override,cap_lease+eip /usr/bin/privacy-guard-server - %files -n privacy-guard-server %defattr(-,root,root,-) %license LICENSE.APLv2 @@ -133,3 +138,6 @@ chsmack -a System %{TZ_SYS_DB}/.privacy_guard.db-journal %manifest privacy-guard-client-devel.manifest %{_includedir}/* %{_libdir}/pkgconfig/privacy-guard-client.pc + +%files -n tc-privacy-guard +%{_bindir}/tc-privacy-guard \ No newline at end of file diff --git a/pkgmgr_plugin/CMakeLists.txt b/pkgmgr_plugin/CMakeLists.txt old mode 100644 new mode 100755 diff --git a/pkgmgr_plugin/privileges.cpp b/pkgmgr_plugin/privileges.cpp old mode 100644 new mode 100755 index fd96d65..94938fb --- a/pkgmgr_plugin/privileges.cpp +++ b/pkgmgr_plugin/privileges.cpp @@ -26,9 +26,6 @@ #include "PrivacyGuardClient.h" #include -#define DEFAULT_MONITOR_POLICY 0 - -//static const xmlChar _NODE_PRIVILEGES[] = "privileges"; static const xmlChar _NODE_PRIVILEGE[] = "privilege"; void destroy_char_list(char** ppList, int size) @@ -63,9 +60,7 @@ int PKGMGR_PARSER_PLUGIN_INSTALL(xmlDocPtr docPtr, const char* packageId) LOGD("PKGMGR_PARSER_PLUGIN_INSTALL() called with [%s].", packageId); uid_t user_id; - if (pkgmgr_installer_info_get_target_uid(&user_id) < 0) { - LOGE("Failed to get target user id"); - } + pkgmgr_installer_info_get_target_uid(&user_id); LOGD("user_id is %d.", user_id); int ret = 0; @@ -98,7 +93,7 @@ int PKGMGR_PARSER_PLUGIN_INSTALL(xmlDocPtr docPtr, const char* packageId) curPtr = curPtr->next; } - char **ppPrivilegeList = (char**) calloc(privilegeList.size() + 1, sizeof(char*)); + char **ppPrivilegeList = (char**)calloc(privilegeList.size() + 1, sizeof(char*)); char **temp = ppPrivilegeList; std::list ::iterator iter = privilegeList.begin(); for (size_t i = 0; i < privilegeList.size(); ++i) { @@ -116,19 +111,26 @@ int PKGMGR_PARSER_PLUGIN_INSTALL(xmlDocPtr docPtr, const char* packageId) memcpy(ppPrivilegeList[privilegeList.size()], "\0", 1); PrivacyGuardClient *pInst = PrivacyGuardClient::getInstance(); - std::list < std::string > privilege_List; + std::list < std::string > privilege_list; while (*ppPrivilegeList[0] != '\0') { LOGD("privilege in the List: %s", *ppPrivilegeList); - privilege_List.push_back(std::string(*ppPrivilegeList++)); + privilege_list.push_back(std::string(*ppPrivilegeList++)); } int monitor_policy = DEFAULT_MONITOR_POLICY; - ret = pInst->PgAddMonitorPolicy(user_id, std::string(packageId), privilege_List, monitor_policy); + ret = pInst->PgAddMonitorPolicy(user_id, std::string(packageId), privilege_list, monitor_policy); if (ret != PRIV_GUARD_ERROR_SUCCESS) { - LOGE("Failed to add monitor policy: [%d]", ret); -// return -EIO; -// return 0; + LOGE("Failed to add monitor policy for user [%d]. Error code: [%d]", user_id, ret); + } else { + // in case of GLOBAL USER (376), add policy for owner (5001) additionally. + if (user_id == tzplatform_getuid(TZ_SYS_GLOBALAPP_USER)) { // global app (376) + uid_t owner_user_id = tzplatform_getuid(TZ_SYS_DEFAULT_USER); // owner (5001) + ret = pInst->PgAddMonitorPolicy(owner_user_id, std::string(packageId), privilege_list, monitor_policy); + if (ret != PRIV_GUARD_ERROR_SUCCESS) { + LOGE("Failed to add monitor policy for user [%d]. Error code: [%d]", owner_user_id, ret); + } + } } if (temp) diff --git a/server/CMakeLists.txt b/server/CMakeLists.txt old mode 100644 new mode 100755 index 7ff76f0..149a828 --- a/server/CMakeLists.txt +++ b/server/CMakeLists.txt @@ -7,8 +7,7 @@ SET(EXEC_PREFIX "\${prefix}") SET(INCLUDEDIR "\${prefix}/include") INCLUDE(FindPkgConfig) -pkg_check_modules(pkgs REQUIRED dlog sqlite3 dbus-1 dbus-glib-1 db-util pkgmgr-info capi-system-info libtzplatform-config security-privilege-manager cynara-monitor security-manager) -#pkg_check_modules(pkgs REQUIRED dlog sqlite3 dbus-1 dbus-glib-1 db-util pkgmgr-info capi-system-info libtzplatform-config security-privilege-manager) +pkg_check_modules(pkgs REQUIRED dlog sqlite3 dbus-1 dbus-glib-1 db-util pkgmgr-info capi-system-info libtzplatform-config security-privilege-manager cynara-monitor security-manager aul) FOREACH(flag ${pkgs_CFLAGS}) SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}") @@ -35,11 +34,6 @@ ADD_DEFINITIONS("-fvisibility=hidden") ADD_DEFINITIONS("-Wall -Werror") ADD_DEFINITIONS("-DDLOG_ERROR_ENABLED") ADD_DEFINITIONS("-D_PRIVACY_GUARD_DEBUG") -OPTION (FILTER_LISTED_PKG "FILTER PKG BY LIST" ON) -IF(FILTER_LISTED_PKG) - MESSAGE("FILTER PKGs BY FILTERING LIST") - ADD_DEFINITIONS("-D__FILTER_LISTED_PKG") -ENDIF(FILTER_LISTED_PKG) ################################################################################################### ## for privacy-guard-server (executable) @@ -61,7 +55,6 @@ SET(PRIVACY_GUARD_SERVER_SOURCES ${server_src_dir}/CynaraService.cpp ${server_src_dir}/PrivacyGuardDaemon.cpp ${server_src_dir}/service/PrivacyInfoService.cpp - ${server_src_dir}/NotificationServer.cpp ) SET(PRIVACY_GUARD_SERVER_LDFLAGS " -module -avoid-version ") SET(PRIVACY_GUARD_SERVER_CFLAGS " ${CFLAGS} -fPIE ") @@ -84,4 +77,3 @@ CONFIGURE_FILE(../privacy-guard-server.pc.in privacy-guard-server.pc @ONLY) INSTALL(TARGETS privacy-guard-server DESTINATION /usr/bin COMPONENT RuntimeLibraries) INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/privacy-guard-server.pc DESTINATION ${LIB_INSTALL_DIR}/pkgconfig) -#INSTALL(FILES ${PRIVACY_GUARD_SERVER_HEADERS} DESTINATION ${INCLUDE_INSTALL_DIR}/privacy_guard/server) diff --git a/server/inc/CynaraService.h b/server/inc/CynaraService.h old mode 100644 new mode 100755 diff --git a/server/inc/ICommonDb.h b/server/inc/ICommonDb.h old mode 100644 new mode 100755 diff --git a/server/inc/NotificationServer.h b/server/inc/NotificationServer.h deleted file mode 100644 index b033b43..0000000 --- a/server/inc/NotificationServer.h +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright (c) 2013 Samsung Electronics Co., Ltd All Rights Reserved - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef _NOTIFICATIONSERVER_H_ -#define _NOTIFICATIONSERVER_H_ - -#include -#include -#include -#include -#include -#include - -class NotificationServer -{ -private: - bool m_initialized; - DBusConnection* m_pDBusConnection; -public: - - NotificationServer(void); - ~NotificationServer(void); - int initialize(void); - int notifySettingChanged(const std::string pkgId, const std::string privacyId); - int notifyPkgRemoved(const std::string pkgId); -}; - - -#endif // _NOTIFICATIONSERVER_H_ \ No newline at end of file diff --git a/server/inc/PrivacyGuardDaemon.h b/server/inc/PrivacyGuardDaemon.h old mode 100644 new mode 100755 diff --git a/server/inc/PrivacyGuardDb.h b/server/inc/PrivacyGuardDb.h old mode 100644 new mode 100755 index 26209b0..977b731 --- a/server/inc/PrivacyGuardDb.h +++ b/server/inc/PrivacyGuardDb.h @@ -32,11 +32,13 @@ private: static std::mutex m_singletonMutex; static PrivacyGuardDb* m_pInstance; static GList *m_privacy_list; + static GList *m_prepared_user_id_list; bool m_bInitialized; private: PrivacyGuardDb(void); void initialize(void); + int prepareGlobalPackagePolicy(const int userId); ~PrivacyGuardDb(void); public: @@ -44,37 +46,37 @@ public: virtual void openSqliteDB(void); - int PgAddPrivacyAccessLogForCynara(const int userId, const std::string packageId, const std::string privacyId, const time_t timestamp); + int PgAddPrivacyAccessLog(const int userId, const std::string packageId, const std::string privacyId, const time_t timestamp); int PgAddMonitorPolicy(const int userId, const std::string packageId, const std::list < std::string > privacyList, bool monitorPolicy); + int PgAddMonitorPolicy(const int userId, const std::string packageId, const std::string privacyId, bool monitorPolicy); + int PgCheckPrivacyPackage(const int userId, const std::string packageId, bool &isPrivacyPackage); int PgDeleteAllLogsAndMonitorPolicy(void); + int PgDeleteAllLogs(void); + int PgDeleteLogsByPackageId(const std::string packageId); int PgDeleteMonitorPolicyByPackageId(const std::string packageId); - int PgForeachTotalPrivacyCountOfPackage(const int userId, const int startDate, const int endDate, - std::list < std::pair < std::string, int > >& packageInfoList); + int PgForeachTotalPrivacyCountOfPackage(const int userId, const int startDate, const int endDate, std::list < std::pair < std::string, int > >& packageInfoList); - int PgForeachTotalPrivacyCountOfPrivacy(const int userId, const int startDate, const int endDate, - std::list < std::pair < std::string, int > >& privacyInfoList); + int PgForeachTotalPrivacyCountOfPrivacy(const int userId, const int startDate, const int endDate, std::list < std::pair < std::string, int > >& privacyInfoList); - int PgForeachPrivacyCountByPrivacyId(const int userId, const int startDate, const int endDate, - const std::string privacyId, std::list < std::pair < std::string, int > >& packageInfoList); + int PgForeachPrivacyCountByPrivacyId(const int userId, const int startDate, const int endDate, const std::string privacyId, std::list < std::pair < std::string, int > >& packageInfoList); - int PgForeachPrivacyCountByPackageId(const int userId, const int startDate, const int endDate, - const std::string packageId, std::list < std::pair < std::string, int > >& privacyInfoList); + int PgForeachPrivacyCountByPackageId(const int userId, const int startDate, const int endDate, const std::string packageId, std::list < std::pair < std::string, int > >& privacyInfoList); - int PgGetMonitorPolicy(const int userId, const std::string packageId, const std::string privacyId, int& monitorPolicy); + int PgGetMonitorPolicy(const int userId, const std::string packageId, const std::string privacyId, int &monitorPolicy); int PgGetAllMonitorPolicy(std::list < std::pair < std::string, int > >& monitorPolicyList); int PgForeachMonitorPolicyByPackageId(const int userId, const std::string packageId, std::list & privacyInfoList); - int PgForeachPrivacyPackageId(const int userId, std::list < std::string > & packageList); + int PgForeachPackageIdUsingPrivacy(const int userId, std::list < std::string > & packageList); int PgForeachPackageInfoByPrivacyId(const int userId, const std::string privacyId, std::list < package_data_s > &packageList); @@ -82,9 +84,9 @@ public: int PgAddMainMonitorPolicy(const int userId); - int PgUpdateMainMonitorPolicy(const int userId, const bool mainMonitorPolicy); + int PgUpdateMainMonitorPolicy(const int userId, const int mainMonitorPolicy); - int PgGetMainMonitorPolicy(const int userId, bool &mainMonitorPolicy); + int PgGetMainMonitorPolicy(const int userId, int &mainMonitorPolicy); int PgDeleteMainMonitorPolicyByUserId(const int userId); }; diff --git a/server/inc/PrivacyInfoService.h b/server/inc/PrivacyInfoService.h old mode 100644 new mode 100755 index 4fc33c5..ad4f7fc --- a/server/inc/PrivacyInfoService.h +++ b/server/inc/PrivacyInfoService.h @@ -30,15 +30,16 @@ private: public: static void registerCallbacks(SocketService* pSocketService) { + pSocketService->registerServiceCallback(getInterfaceName(), std::string("PgAddPrivacyAccessLog"), PgAddPrivacyAccessLog); pSocketService->registerServiceCallback(getInterfaceName(), std::string("PgAddMonitorPolicy"), PgAddMonitorPolicy); - pSocketService->registerServiceCallback(getInterfaceName(), std::string("PgDeleteAllLogsAndMonitorPolicy"), PgDeleteAllLogsAndMonitorPolicy); + //pSocketService->registerServiceCallback(getInterfaceName(), std::string("PgDeleteAllLogsAndMonitorPolicy"), PgDeleteAllLogsAndMonitorPolicy); pSocketService->registerServiceCallback(getInterfaceName(), std::string("PgDeleteLogsByPackageId"), PgDeleteLogsByPackageId); - pSocketService->registerServiceCallback(getInterfaceName(), std::string("PgDeleteMonitorPolicyByPackageId"), PgDeleteMonitorPolicyByPackageId); + //pSocketService->registerServiceCallback(getInterfaceName(), std::string("PgDeleteMonitorPolicyByPackageId"), PgDeleteMonitorPolicyByPackageId); pSocketService->registerServiceCallback(getInterfaceName(), std::string("PgForeachTotalPrivacyCountOfPackage"), PgForeachTotalPrivacyCountOfPackage); pSocketService->registerServiceCallback(getInterfaceName(), std::string("PgForeachTotalPrivacyCountOfPrivacy"), PgForeachTotalPrivacyCountOfPrivacy); pSocketService->registerServiceCallback(getInterfaceName(), std::string("PgForeachPrivacyCountByPrivacyId"), PgForeachPrivacyCountByPrivacyId); pSocketService->registerServiceCallback(getInterfaceName(), std::string("PgForeachPrivacyCountByPackageId"), PgForeachPrivacyCountByPackageId); - pSocketService->registerServiceCallback(getInterfaceName(), std::string("PgForeachPrivacyPackageId"), PgForeachPrivacyPackageId); + pSocketService->registerServiceCallback(getInterfaceName(), std::string("PgForeachPackageIdUsingPrivacy"), PgForeachPackageIdUsingPrivacy); pSocketService->registerServiceCallback(getInterfaceName(), std::string("PgForeachPackageInfoByPrivacyId"), PgForeachPackageInfoByPrivacyId); pSocketService->registerServiceCallback(getInterfaceName(), std::string("PgForeachMonitorPolicyByPackageId"), PgForeachMonitorPolicyByPackageId); pSocketService->registerServiceCallback(getInterfaceName(), std::string("PgGetMonitorPolicy"), PgGetMonitorPolicy); @@ -47,18 +48,19 @@ public: pSocketService->registerServiceCallback(getInterfaceName(), std::string("PgUpdateMonitorPolicy"), PgUpdateMonitorPolicy); pSocketService->registerServiceCallback(getInterfaceName(), std::string("PgGetMainMonitorPolicy"), PgGetMainMonitorPolicy); pSocketService->registerServiceCallback(getInterfaceName(), std::string("PgUpdateMainMonitorPolicy"), PgUpdateMainMonitorPolicy); - pSocketService->registerServiceCallback(getInterfaceName(), std::string("PgDeleteMainMonitorPolicyByUserId"), PgDeleteMainMonitorPolicyByUserId); + pSocketService->registerServiceCallback(getInterfaceName(), std::string("PgDeleteAllLogs"), PgDeleteAllLogs); } + static void PgAddPrivacyAccessLog(SocketConnection* pConnector); static void PgAddMonitorPolicy(SocketConnection* pConnector); - static void PgDeleteAllLogsAndMonitorPolicy(SocketConnection* pConnector); + //static void PgDeleteAllLogsAndMonitorPolicy(SocketConnection* pConnector); static void PgDeleteLogsByPackageId(SocketConnection* pConnector); - static void PgDeleteMonitorPolicyByPackageId(SocketConnection* pConnector); + //static void PgDeleteMonitorPolicyByPackageId(SocketConnection* pConnector); static void PgForeachTotalPrivacyCountOfPackage(SocketConnection* pConnector); static void PgForeachTotalPrivacyCountOfPrivacy(SocketConnection* pConnector); static void PgForeachPrivacyCountByPrivacyId(SocketConnection* pConnector); static void PgForeachPrivacyCountByPackageId(SocketConnection* pConnector); - static void PgForeachPrivacyPackageId(SocketConnection* pConnector); + static void PgForeachPackageIdUsingPrivacy(SocketConnection* pConnector); static void PgForeachPackageInfoByPrivacyId(SocketConnection* pConnector); static void PgForeachMonitorPolicyByPackageId(SocketConnection* pConnector); static void PgGetMonitorPolicy(SocketConnection* pConnector); @@ -67,6 +69,6 @@ public: static void PgUpdateMonitorPolicy(SocketConnection* pConnector); static void PgGetMainMonitorPolicy(SocketConnection* pConnector); static void PgUpdateMainMonitorPolicy(SocketConnection* pConnector); - static void PgDeleteMainMonitorPolicyByUserId(SocketConnection* pConnector); + static void PgDeleteAllLogs(SocketConnection* pConnector); }; #endif // _PRIVACYINFOSERVICE_H_ \ No newline at end of file diff --git a/server/inc/SocketService.h b/server/inc/SocketService.h old mode 100644 new mode 100755 diff --git a/server/inc/privacy_guard_daemon.h b/server/inc/privacy_guard_daemon.h old mode 100644 new mode 100755 diff --git a/server/src/CynaraService.cpp b/server/src/CynaraService.cpp old mode 100644 new mode 100755 index 215df6c..ac30e4d --- a/server/src/CynaraService.cpp +++ b/server/src/CynaraService.cpp @@ -26,7 +26,6 @@ #include #include #include -#include #include #include "PrivacyGuardTypes.h" @@ -35,8 +34,6 @@ #include "PrivacyGuardDb.h" #include "PrivacyIdInfo.h" -#define GLOBAL_USER tzplatform_getuid(TZ_SYS_GLOBALAPP_USER) - #define BUF_SIZE 256 #define ONE_SEC (SLEEP_TIME * 20) @@ -63,7 +60,7 @@ CynaraService::~CynaraService(void) int CynaraService::initialize(void) { - PG_LOGD("initializing CynaraService.."); + //PG_LOGD("initializing CynaraService.."); int res = cynara_monitor_configuration_create(&p_conf); if(res != CYNARA_API_SUCCESS){ @@ -83,9 +80,9 @@ CynaraService::initialize(void) return PRIV_GUARD_ERROR_SYSTEM_ERROR; } -// cynara_monitor_configuration_set_filter + // cynara_monitor_configuration_set_filter - PG_LOGI("CynaraService initialized"); + //PG_LOGI("CynaraService initialized"); return PRIV_GUARD_ERROR_SUCCESS; } @@ -93,7 +90,7 @@ CynaraService::initialize(void) int CynaraService::start(void) { - PG_LOGI("starting CynaraService.."); + //PG_LOGI("starting CynaraService.."); int res = 0; char buf[256]; @@ -109,16 +106,16 @@ CynaraService::start(void) #endif pthread_t cynaraThread; - PG_LOGD("starting new thread (getEntriesThread)"); + //PG_LOGD("starting new thread (getEntriesThread)"); res = pthread_create(&cynaraThread, NULL, &getEntriesThread, this); TryReturn( res >= 0, PRIV_GUARD_ERROR_SYSTEM_ERROR, errno = res, "pthread_create : %s", strerror_r(errno, buf, sizeof(buf))); - PG_LOGD("new thread (getEntriesThread) started"); + //PG_LOGD("new thread (getEntriesThread) started"); m_cynaraThread = cynaraThread; exit_flag = false; - PG_LOGD("CynaraService started"); + //PG_LOGD("CynaraService started"); return PRIV_GUARD_ERROR_SUCCESS; } @@ -126,7 +123,7 @@ CynaraService::start(void) void* CynaraService::getEntriesThread(void* pData) { - PG_LOGD("Running get entries thread"); + //PG_LOGD("Running get entries thread"); int res = -1; @@ -180,11 +177,9 @@ CynaraService::updateDb(cynara_monitor_entry **monitor_entries) const char *user = NULL, *client = NULL, *privilege = NULL; const timespec *timestamp = NULL; uid_t user_id; - std::string privacyId;//appId, packageId; + std::string privacyId; time_t date; int res = -1; - pkgmgrinfo_pkginfo_h pkg_handle; - bool is_global = false; char *pkg_name = NULL, *app_name = NULL; while (*entryIter != nullptr) { @@ -196,8 +191,8 @@ CynaraService::updateDb(cynara_monitor_entry **monitor_entries) res = PrivacyIdInfo::getPrivacyIdFromPrivilege(privilege, privacyId); //PG_LOGD("########## res: [%d] privilege: [%s] privacy: [%s]", res, privilege, privacyId.c_str()); if (res != PRIV_GUARD_ERROR_NO_DATA) { - PG_LOGD("#Privilege: [%s]", privilege); - PG_LOGD("#Privacy : [%s]", privacyId.c_str()); + //PG_LOGD("#Privilege: [%s]", privilege); + //PG_LOGD("#Privacy : [%s]", privacyId.c_str()); // User ID - string user = cynara_monitor_entry_get_user(*entryIter); @@ -205,7 +200,6 @@ CynaraService::updateDb(cynara_monitor_entry **monitor_entries) // App ID - string client = cynara_monitor_entry_get_client(*entryIter); - //PG_LOGD("client: [%s]", client); TryReturn(client != NULL, PRIV_GUARD_ERROR_SYSTEM_ERROR, , "App Id in the entry is NULL"); // timestamp @@ -218,38 +212,17 @@ CynaraService::updateDb(cynara_monitor_entry **monitor_entries) // get pkgname from cynara-style app ID res = security_manager_identify_app_from_cynara_client(client, &pkg_name, &app_name); if (res != SECURITY_MANAGER_SUCCESS) { - PG_LOGD("Failed to do security_manager_identify_app_from_cynara_client [%d]", res); - PG_LOGD("So use [%s] as a package name instead.", client); + //PG_LOGD("Failed to do security_manager_identify_app_from_cynara_client [%d]", res); + //PG_LOGD("So use [%s] as a package name instead.", client); pkg_name = strdup(client); - } else { - PG_LOGD("#Package : [%s]", pkg_name); - // check this package is global app - if (user_id == GLOBAL_USER) { - res = pkgmgrinfo_pkginfo_get_pkginfo(pkg_name, &pkg_handle); - } else { - res = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkg_name, user_id, &pkg_handle); - } - if (res != PMINFO_R_OK) { - PG_LOGE("Failed to do pkgmgrinfo_pkginfo_get_pkginfo or pkgmgrinfo_pkginfo_get_usr_pkginfo [%d] for the package [%s] with user [%d]", res, pkg_name, user_id); - } else { - res = pkgmgrinfo_pkginfo_is_global(pkg_handle, &is_global); - if (res != PMINFO_R_OK) { - PG_LOGE("Failed to do pkgmgrinfo_pkginfo_is_global [%d]", res); - } else { - if (is_global == true) { - user_id = GLOBAL_USER; - } - } - pkgmgrinfo_pkginfo_destroy_pkginfo(pkg_handle); - } } // datetime date = timestamp->tv_sec; // add access log - PG_LOGD("Insert [%s, %s] to DB.", pkg_name, privacyId.c_str()); - int ret = PrivacyGuardDb::getInstance()->PgAddPrivacyAccessLogForCynara(user_id, pkg_name, privacyId, date); + PG_LOGD("Try to insert [%d, %s, %s] to DB.", user_id, pkg_name, privacyId.c_str()); + int ret = PrivacyGuardDb::getInstance()->PgAddPrivacyAccessLog(user_id, pkg_name, privacyId, date); if(ret != PRIV_GUARD_ERROR_SUCCESS){ PG_LOGE("Failed to add access log to DB. UserID:[%d], PackageID:[%s], Privacy:[%s]", user_id, pkg_name, privacyId.c_str()); } @@ -257,11 +230,11 @@ CynaraService::updateDb(cynara_monitor_entry **monitor_entries) // deallocation if (pkg_name) { //PG_LOGD("freeing a pkg_name: [%s].", pkg_name); - free(pkg_name); + SAFE_FREE(pkg_name); } if (app_name) { //PG_LOGD("freeing an app_name: [%s].", app_name); - free(app_name); + SAFE_FREE(app_name); } } ++entryIter; diff --git a/server/src/NotificationServer.cpp b/server/src/NotificationServer.cpp deleted file mode 100644 index f4a0c1c..0000000 --- a/server/src/NotificationServer.cpp +++ /dev/null @@ -1,119 +0,0 @@ -/* - * Copyright (c) 2012 Samsung Electronics Co., Ltd All Rights Reserved - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include -#include -#include "NotificationServer.h" -#include "PrivacyGuardTypes.h" -#include "Utils.h" - -auto DBusConnectionDeleter = [&](DBusConnection* pPtr) { dbus_connection_close(pPtr); pPtr = NULL;}; -const int MAX_LOCAL_BUF_SIZE = 128; - -NotificationServer::NotificationServer(void) - : m_initialized(false) - , m_pDBusConnection(NULL) -{ - -} - -NotificationServer::~NotificationServer(void) -{ - if (m_pDBusConnection) - { - dbus_connection_close(m_pDBusConnection); - m_pDBusConnection = NULL; - } -} - -int -NotificationServer::initialize(void) -{ - if (m_initialized) - return PRIV_GUARD_ERROR_SUCCESS; - - DBusError error; - dbus_error_init(&error); - - m_pDBusConnection = dbus_bus_get_private(DBUS_BUS_SYSTEM, &error); - TryReturn(m_pDBusConnection != NULL, PRIV_GUARD_ERROR_SYSTEM_ERROR, dbus_error_free(&error), "dbus_bus_get_private : %s", error.message); - - dbus_connection_setup_with_g_main(m_pDBusConnection, NULL); - std::unique_ptr < char[] > pRule(new char[MAX_LOCAL_BUF_SIZE]); - - snprintf(pRule.get(), MAX_LOCAL_BUF_SIZE, "path='%s',type='signal',interface='%s'", DBUS_PATH.c_str(), DBUS_SIGNAL_INTERFACE.c_str()); - - dbus_bus_add_match(m_pDBusConnection, pRule.get(), &error); - TryReturn(!dbus_error_is_set(&error), PRIV_GUARD_ERROR_SYSTEM_ERROR, dbus_error_free(&error), "dbus_bus_add_match : %s", error.message); - - m_initialized = true; - - return PRIV_GUARD_ERROR_SUCCESS; -} - -int -NotificationServer::notifySettingChanged(const std::string pkgId, const std::string privacyId) -{ - if (!m_initialized) - return PRIV_GUARD_ERROR_INVALID_STATE; - - char* pPkgId = const_cast (pkgId.c_str()); - char* pPrivacyId = const_cast (privacyId.c_str()); - - DBusMessage* pMessage = dbus_message_new_signal(DBUS_PATH.c_str(), DBUS_SIGNAL_INTERFACE.c_str(), DBUS_SIGNAL_SETTING_CHANGED.c_str()); - TryReturn(pMessage != NULL, PRIV_GUARD_ERROR_IPC_ERROR, , "dbus_message_new_signal"); - - dbus_bool_t r; - r = dbus_message_append_args(pMessage, - DBUS_TYPE_STRING, &pPkgId, - DBUS_TYPE_STRING, &pPrivacyId, - DBUS_TYPE_INVALID); - TryReturn(r, PRIV_GUARD_ERROR_IPC_ERROR, , "dbus_message_append_args"); - - r = dbus_connection_send(m_pDBusConnection, pMessage, NULL); - TryReturn(r, PRIV_GUARD_ERROR_IPC_ERROR, dbus_message_unref(pMessage);, "dbus_connection_send"); - - dbus_connection_flush(m_pDBusConnection); - dbus_message_unref(pMessage); - - return PRIV_GUARD_ERROR_SUCCESS; -} - -int -NotificationServer::notifyPkgRemoved(const std::string pkgId) -{ - if (!m_initialized) - return PRIV_GUARD_ERROR_INVALID_STATE; - - char* pPkgId = const_cast (pkgId.c_str()); - - DBusMessage* pMessage = dbus_message_new_signal(DBUS_PATH.c_str(), DBUS_SIGNAL_INTERFACE.c_str(), DBUS_SIGNAL_PKG_REMOVED.c_str()); - TryReturn(pMessage != NULL, PRIV_GUARD_ERROR_IPC_ERROR, , "dbus_message_new_signal"); - - dbus_bool_t r; - r = dbus_message_append_args(pMessage, - DBUS_TYPE_STRING, &pPkgId, - DBUS_TYPE_INVALID); - TryReturn(r, PRIV_GUARD_ERROR_IPC_ERROR, , "dbus_message_append_args"); - - r = dbus_connection_send(m_pDBusConnection, pMessage, NULL); - TryReturn(r, PRIV_GUARD_ERROR_IPC_ERROR, dbus_message_unref(pMessage);, "dbus_connection_send"); - - dbus_connection_flush(m_pDBusConnection); - dbus_message_unref(pMessage); - - return PRIV_GUARD_ERROR_SUCCESS; -} diff --git a/server/src/PrivacyGuardDaemon.cpp b/server/src/PrivacyGuardDaemon.cpp old mode 100644 new mode 100755 index 84b56e5..922f25b --- a/server/src/PrivacyGuardDaemon.cpp +++ b/server/src/PrivacyGuardDaemon.cpp @@ -24,6 +24,7 @@ PrivacyGuardDaemon* PrivacyGuardDaemon::pInstance = NULL; PrivacyGuardDaemon::PrivacyGuardDaemon(void) : pSocketService(NULL) + , pCynaraService(NULL) { } diff --git a/server/src/PrivacyGuardDb.cpp b/server/src/PrivacyGuardDb.cpp old mode 100644 new mode 100755 index 3d07b76..b245866 --- a/server/src/PrivacyGuardDb.cpp +++ b/server/src/PrivacyGuardDb.cpp @@ -17,14 +17,12 @@ #include #include #include -#include #include #include #include "Utils.h" #include "PrivacyGuardDb.h" #include "PrivacyIdInfo.h" - #define PRIVACY_GUARD_DAYS 7 #define UNIX_TIME_ONE_DAY (24 * 60 * 60) // 86400 secs #define BUF_SIZE 256 @@ -32,6 +30,7 @@ std::mutex PrivacyGuardDb::m_singletonMutex; PrivacyGuardDb* PrivacyGuardDb::m_pInstance = NULL; GList *PrivacyGuardDb::m_privacy_list = NULL; +GList *PrivacyGuardDb::m_prepared_user_id_list = NULL; static cynara_monitor_configuration *p_conf; static cynara_monitor *p_cynara_monitor; @@ -53,17 +52,22 @@ PrivacyGuardDb::initialize(void) res = cynara_monitor_configuration_create(&p_conf); if(res != CYNARA_API_SUCCESS){ PG_LOGE("cynara_monitor_configuration_create() is failed."); - //return PRIV_GUARD_ERROR_SYSTEM_ERROR; + g_list_free_full(m_privacy_list, g_free); + m_privacy_list = NULL; return; } res = cynara_monitor_initialize(&p_cynara_monitor, p_conf); if(res != CYNARA_API_SUCCESS){ PG_LOGE("cynara_monitor_initialize() is failed."); - //return PRIV_GUARD_ERROR_SYSTEM_ERROR; + g_list_free_full(m_privacy_list, g_free); + m_privacy_list = NULL; + cynara_monitor_configuration_destroy(p_conf); return; } + cynara_monitor_configuration_destroy(p_conf); + m_bInitialized = true; } @@ -83,7 +87,7 @@ PrivacyGuardDb::openSqliteDB(void) } int -PrivacyGuardDb::PgAddPrivacyAccessLogForCynara(const int userId, const std::string packageId, const std::string privacyId, const time_t date) +PrivacyGuardDb::PgAddPrivacyAccessLog(const int userId, const std::string packageId, const std::string privacyId, const time_t date) { if(userId < 0 || date <= 0) { PG_LOGE("Invalid parameter: userId: [%d], date: [%d]", userId, date); @@ -97,9 +101,10 @@ PrivacyGuardDb::PgAddPrivacyAccessLogForCynara(const int userId, const std::stri res = PgGetMonitorPolicy(userId, packageId, privacyId, monitorPolicy); TryReturn(res == PRIV_GUARD_ERROR_SUCCESS, res, , "PgGetMonitorPolicy is failed: [%d]", res); if (monitorPolicy == 0) { - //PG_LOGD("Monitor Policy is 0. So skip it. UserId:[%d], PrivacyId:[%s], PackageId:[%s], Policy:[%d]", userId, privacyId.c_str(), packageId.c_str(), monitorPolicy); + PG_LOGD("Monitor Policy is 0. So skip it. UserId:[%d], PrivacyId:[%s], PackageId:[%s], Policy:[%d]", userId, privacyId.c_str(), packageId.c_str(), monitorPolicy); return PRIV_GUARD_ERROR_SUCCESS; } + //PG_LOGD("Monitor Policy is 1. So write it. UserId:[%d], PrivacyId:[%s], PackageId:[%s], Policy:[%d]", userId, privacyId.c_str(), packageId.c_str(), monitorPolicy); static const std::string QUERY_INSERT = std::string("INSERT INTO StatisticsMonitorInfo(USER_ID, PKG_ID, PRIVACY_ID, USE_DATE) VALUES(?, ?, ?, ?)"); @@ -137,7 +142,7 @@ PrivacyGuardDb::PgAddPrivacyAccessLogForCynara(const int userId, const std::stri res = sqlite3_step(m_stmt); TryCatchResLogReturn(res == SQLITE_DONE, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_step : %d", res); - sqlite3_reset(m_stmt); + sqlite3_finalize(m_stmt); m_dbMutex.unlock(); @@ -170,7 +175,7 @@ PrivacyGuardDb::PgAddMonitorPolicy(const int userId, const std::string packageId TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_prepare_v2 : %d", res); for (std::list ::const_iterator iter = privacyList.begin(); iter != privacyList.end(); ++iter) { - PG_LOGD("User ID: [%d], Package ID: [%s], PrivacyID: [%s], Monitor Policy: [%d]", userId, packageId.c_str(), iter->c_str(), monitorPolicy); + //PG_LOGD("User ID: [%d], Package ID: [%s], PrivacyID: [%s], Monitor Policy: [%d]", userId, packageId.c_str(), iter->c_str(), monitorPolicy); // bind res = sqlite3_bind_int(m_stmt, 1, userId); @@ -190,6 +195,57 @@ PrivacyGuardDb::PgAddMonitorPolicy(const int userId, const std::string packageId sqlite3_reset(m_stmt); } + + sqlite3_finalize(m_stmt); + + m_dbMutex.unlock(); + + return PRIV_GUARD_ERROR_SUCCESS; +} + +int +PrivacyGuardDb::PgAddMonitorPolicy(const int userId, const std::string packageId, const std::string privacyId, bool monitorPolicy) +{ + int res = -1; + + static const std::string QUERY_INSERT = std::string("INSERT INTO MonitorPolicy(USER_ID, PKG_ID, PRIVACY_ID, MONITOR_POLICY) VALUES(?, ?, ?, ?)"); + + m_dbMutex.lock(); + + // initialize + if (m_bInitialized == false) { + initialize(); + } + TryCatchResLogReturn(m_bInitialized == true, m_dbMutex.unlock(), PRIV_GUARD_ERROR_NOT_INITIALIZED, "Failed to initialize", res); + + // open db + if(m_bDBOpen == false) { + openSqliteDB(); + } + TryCatchResLogReturn(m_bDBOpen == true, m_dbMutex.unlock(), PRIV_GUARD_ERROR_IO_ERROR, "openSqliteDB : %d", res); + + // prepare + res = sqlite3_prepare_v2(m_sqlHandler, QUERY_INSERT.c_str(), -1, &m_stmt, NULL); + TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_prepare_v2 : %d", res); + + // bind + res = sqlite3_bind_int(m_stmt, 1, userId); + TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_int : %d", res); + + res = sqlite3_bind_text(m_stmt, 2, packageId.c_str(), -1, SQLITE_TRANSIENT); + TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_text : %d", res); + + res = sqlite3_bind_text(m_stmt, 3, privacyId.c_str(), -1, SQLITE_TRANSIENT); + TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_text : %d", res); + + res = sqlite3_bind_int(m_stmt, 4, monitorPolicy); + TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_int : %d", res); + + res = sqlite3_step(m_stmt); + TryCatchResLogReturn(res == SQLITE_DONE, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_step : %d", res); + + sqlite3_finalize(m_stmt); + m_dbMutex.unlock(); return PRIV_GUARD_ERROR_SUCCESS; @@ -201,9 +257,15 @@ PrivacyGuardDb::PgCheckPrivacyPackage(const int userId, const std::string packag int res = -1; char buf[BUF_SIZE]; - snprintf(buf, BUF_SIZE, "SELECT COUNT(*) FROM MonitorPolicy WHERE (USER_ID=? OR USER_ID=%d) AND PKG_ID=?", GLOBAL_USER); + snprintf(buf, BUF_SIZE, "SELECT COUNT(*) FROM MonitorPolicy WHERE USER_ID=? AND PKG_ID=?"); static const std::string query = std::string(buf); + /////////////////////////////////////////////////// + // prepare privacy monitor policy for global apps + /////////////////////////////////////////////////// + res = prepareGlobalPackagePolicy(userId); + TryCatchResLogReturn(res == PRIV_GUARD_ERROR_SUCCESS, m_dbMutex.unlock();, PRIV_GUARD_ERROR_INTERNAL_ERROR, "PgPrepareGlobalPackagePolicy: %d", res); + m_dbMutex.lock(); // initialize @@ -235,6 +297,9 @@ PrivacyGuardDb::PgCheckPrivacyPackage(const int userId, const std::string packag if ((res = sqlite3_step(m_stmt)) == SQLITE_ROW) { count = sqlite3_column_int(m_stmt, 0); } + + sqlite3_finalize(m_stmt); + m_dbMutex.unlock(); if (count > 0) { @@ -277,23 +342,62 @@ PrivacyGuardDb::PgDeleteAllLogsAndMonitorPolicy(void) res = sqlite3_step(m_stmt); TryCatchResLogReturn(res == SQLITE_DONE, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_step : %d", res); + sqlite3_reset(m_stmt); + res = sqlite3_prepare_v2(m_sqlHandler, POLICY_DELETE.c_str(), -1, &m_stmt, NULL); TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_prepare_v2 : %d", res); res = sqlite3_step(m_stmt); TryCatchResLogReturn(res == SQLITE_DONE, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_step : %d", res); + sqlite3_reset(m_stmt); + res = sqlite3_prepare_v2(m_sqlHandler, MAIN_POLICY_DELETE.c_str(), -1, &m_stmt, NULL); TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_prepare_v2 : %d", res); res = sqlite3_step(m_stmt); TryCatchResLogReturn(res == SQLITE_DONE, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_step : %d", res); + sqlite3_finalize(m_stmt); + m_dbMutex.unlock(); return PRIV_GUARD_ERROR_SUCCESS; } +int +PrivacyGuardDb::PgDeleteAllLogs(void) +{ + int res = -1; + static const std::string LOG_DELETE = std::string("DELETE FROM StatisticsMonitorInfo"); + + m_dbMutex.lock(); + + // initialize + if (m_bInitialized == false) { + initialize(); + } + TryCatchResLogReturn(m_bInitialized == true, m_dbMutex.unlock(), PRIV_GUARD_ERROR_NOT_INITIALIZED, "Failed to initialize", res); + + // open db + if(m_bDBOpen == false) { + openSqliteDB(); + } + TryCatchResLogReturn(m_bDBOpen == true, m_dbMutex.unlock(), PRIV_GUARD_ERROR_IO_ERROR, "openSqliteDB : %d", res); + + // prepare + res = sqlite3_prepare_v2(m_sqlHandler, LOG_DELETE.c_str(), -1, &m_stmt, NULL); + TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_prepare_v2 : %d", res); + + res = sqlite3_step(m_stmt); + TryCatchResLogReturn(res == SQLITE_DONE, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_step : %d", res); + + sqlite3_finalize(m_stmt); + + m_dbMutex.unlock(); + + return PRIV_GUARD_ERROR_SUCCESS; +} int PrivacyGuardDb::PgDeleteLogsByPackageId(const std::string packageId) @@ -328,6 +432,8 @@ PrivacyGuardDb::PgDeleteLogsByPackageId(const std::string packageId) res = sqlite3_step(m_stmt); TryCatchResLogReturn(res == SQLITE_DONE, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_step : %d", res); + sqlite3_finalize(m_stmt); + m_dbMutex.unlock(); return PRIV_GUARD_ERROR_SUCCESS; @@ -366,6 +472,8 @@ PrivacyGuardDb::PgDeleteMonitorPolicyByPackageId(const std::string packageId) res = sqlite3_step(m_stmt); TryCatchResLogReturn(res == SQLITE_DONE, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_step : %d", res); + sqlite3_finalize(m_stmt); + m_dbMutex.unlock(); return PRIV_GUARD_ERROR_SUCCESS; @@ -378,6 +486,12 @@ PrivacyGuardDb::PgForeachTotalPrivacyCountOfPackage(const int userId, const int char buf[BUF_SIZE]; sqlite3_stmt* infoStmt; + /////////////////////////////////////////////////// + // prepare privacy monitor policy for global apps + /////////////////////////////////////////////////// + res = prepareGlobalPackagePolicy(userId); + TryCatchResLogReturn(res == PRIV_GUARD_ERROR_SUCCESS, m_dbMutex.unlock();, PRIV_GUARD_ERROR_INTERNAL_ERROR, "PgPrepareGlobalPackagePolicy: %d", res); + // [CYNARA] Flush Entries res = cynara_monitor_entries_flush(p_cynara_monitor); if(res != CYNARA_API_SUCCESS){ @@ -388,10 +502,10 @@ PrivacyGuardDb::PgForeachTotalPrivacyCountOfPackage(const int userId, const int } } - snprintf(buf, BUF_SIZE, "SELECT DISTINCT PKG_ID FROM StatisticsMonitorInfo WHERE (USER_ID=? OR USER_ID=%d) AND USE_DATE>=? AND USE_DATE<=?", GLOBAL_USER); + snprintf(buf, BUF_SIZE, "SELECT DISTINCT PKG_ID FROM StatisticsMonitorInfo WHERE USER_ID=? AND USE_DATE>=? AND USE_DATE<=?"); static const std::string PKGID_SELECT = std::string(buf); - snprintf(buf, BUF_SIZE, "SELECT COUNT(*) FROM StatisticsMonitorInfo WHERE (USER_ID=? OR USER_ID=%d) AND PKG_ID=? AND USE_DATE>=? AND USE_DATE<=?", GLOBAL_USER); + snprintf(buf, BUF_SIZE, "SELECT COUNT(*) FROM StatisticsMonitorInfo WHERE USER_ID=? AND PKG_ID=? AND USE_DATE>=? AND USE_DATE<=?"); static const std::string PKGINFO_SELECT = std::string(buf); m_dbMutex.lock(); @@ -448,8 +562,11 @@ PrivacyGuardDb::PgForeachTotalPrivacyCountOfPackage(const int userId, const int } packageInfoList.push_back(std::pair (std::string(packageId), count)); } - sqlite3_reset(infoStmt); + sqlite3_finalize(infoStmt); } + + sqlite3_finalize(m_stmt); + m_dbMutex.unlock(); return PRIV_GUARD_ERROR_SUCCESS; @@ -461,6 +578,12 @@ PrivacyGuardDb::PgForeachTotalPrivacyCountOfPrivacy(const int userId, const int int res = -1; char buf[BUF_SIZE]; + /////////////////////////////////////////////////// + // prepare privacy monitor policy for global apps + /////////////////////////////////////////////////// + res = prepareGlobalPackagePolicy(userId); + TryCatchResLogReturn(res == PRIV_GUARD_ERROR_SUCCESS, m_dbMutex.unlock();, PRIV_GUARD_ERROR_INTERNAL_ERROR, "PgPrepareGlobalPackagePolicy: %d", res); + // [CYNARA] Flush Entries res = cynara_monitor_entries_flush(p_cynara_monitor); if(res != CYNARA_API_SUCCESS){ @@ -471,7 +594,7 @@ PrivacyGuardDb::PgForeachTotalPrivacyCountOfPrivacy(const int userId, const int } } - snprintf(buf, BUF_SIZE, "SELECT COUNT(*) FROM StatisticsMonitorInfo WHERE (USER_ID=? OR USER_ID=%d) AND PRIVACY_ID=? AND USE_DATE>=? AND USE_DATE<=?", GLOBAL_USER); + snprintf(buf, BUF_SIZE, "SELECT COUNT(*) FROM StatisticsMonitorInfo WHERE USER_ID=? AND PRIVACY_ID=? AND USE_DATE>=? AND USE_DATE<=?"); static const std::string PRIVACY_SELECT = std::string(buf); m_dbMutex.lock(); @@ -519,6 +642,8 @@ PrivacyGuardDb::PgForeachTotalPrivacyCountOfPrivacy(const int userId, const int sqlite3_reset(m_stmt); } + sqlite3_finalize(m_stmt); + m_dbMutex.unlock(); return PRIV_GUARD_ERROR_SUCCESS; @@ -532,6 +657,12 @@ PrivacyGuardDb::PgForeachPrivacyCountByPrivacyId(const int userId, const int sta sqlite3_stmt* infoStmt; char buf[BUF_SIZE]; + /////////////////////////////////////////////////// + // prepare privacy monitor policy for global apps + /////////////////////////////////////////////////// + res = prepareGlobalPackagePolicy(userId); + TryCatchResLogReturn(res == PRIV_GUARD_ERROR_SUCCESS, m_dbMutex.unlock();, PRIV_GUARD_ERROR_INTERNAL_ERROR, "PgPrepareGlobalPackagePolicy: %d", res); + // [CYNARA] Flush Entries res = cynara_monitor_entries_flush(p_cynara_monitor); if(res != CYNARA_API_SUCCESS){ @@ -542,10 +673,10 @@ PrivacyGuardDb::PgForeachPrivacyCountByPrivacyId(const int userId, const int sta } } - snprintf(buf, BUF_SIZE, "SELECT DISTINCT PKG_ID FROM StatisticsMonitorInfo WHERE (USER_ID=? OR USER_ID=%d) AND PRIVACY_ID=? AND USE_DATE>=? AND USE_DATE<=?", GLOBAL_USER); + snprintf(buf, BUF_SIZE, "SELECT DISTINCT PKG_ID FROM StatisticsMonitorInfo WHERE USER_ID=? AND PRIVACY_ID=? AND USE_DATE>=? AND USE_DATE<=?"); static const std::string PKGID_SELECT = std::string(buf); - snprintf(buf, BUF_SIZE, "SELECT COUNT(*) FROM StatisticsMonitorInfo WHERE (USER_ID=? OR USER_ID=%d) AND PKG_ID=? AND PRIVACY_ID=? AND USE_DATE>=? AND USE_DATE<=?", GLOBAL_USER); + snprintf(buf, BUF_SIZE, "SELECT COUNT(*) FROM StatisticsMonitorInfo WHERE USER_ID=? AND PKG_ID=? AND PRIVACY_ID=? AND USE_DATE>=? AND USE_DATE<=?"); static const std::string PKGINFO_SELECT = std::string(buf); m_dbMutex.lock(); @@ -609,9 +740,11 @@ PrivacyGuardDb::PgForeachPrivacyCountByPrivacyId(const int userId, const int sta continue; packageInfoList.push_back(std::pair (std::string(packageId), count)); } - sqlite3_reset(infoStmt); + sqlite3_finalize(infoStmt); } + sqlite3_finalize(m_stmt); + m_dbMutex.unlock(); return PRIV_GUARD_ERROR_SUCCESS; @@ -624,6 +757,12 @@ PrivacyGuardDb::PgForeachPrivacyCountByPackageId(const int userId, const int sta int res = -1; char buf[BUF_SIZE]; + /////////////////////////////////////////////////// + // prepare privacy monitor policy for global apps + /////////////////////////////////////////////////// + res = prepareGlobalPackagePolicy(userId); + TryCatchResLogReturn(res == PRIV_GUARD_ERROR_SUCCESS, m_dbMutex.unlock();, PRIV_GUARD_ERROR_INTERNAL_ERROR, "PgPrepareGlobalPackagePolicy: %d", res); + // [CYNARA] Flush Entries res = cynara_monitor_entries_flush(p_cynara_monitor); if(res != CYNARA_API_SUCCESS){ @@ -634,7 +773,7 @@ PrivacyGuardDb::PgForeachPrivacyCountByPackageId(const int userId, const int sta } } - snprintf(buf, BUF_SIZE, "SELECT COUNT(*) FROM StatisticsMonitorInfo WHERE (USER_ID=? OR USER_ID=%d) AND PKG_ID=? AND PRIVACY_ID=? AND USE_DATE>=? AND USE_DATE<=?", GLOBAL_USER); + snprintf(buf, BUF_SIZE, "SELECT COUNT(*) FROM StatisticsMonitorInfo WHERE USER_ID=? AND PKG_ID=? AND PRIVACY_ID=? AND USE_DATE>=? AND USE_DATE<=?"); static const std::string PRIVACY_SELECT = std::string(buf); m_dbMutex.lock(); @@ -684,6 +823,8 @@ PrivacyGuardDb::PgForeachPrivacyCountByPackageId(const int userId, const int sta sqlite3_reset(m_stmt); } + sqlite3_finalize(m_stmt); + m_dbMutex.unlock(); return PRIV_GUARD_ERROR_SUCCESS; @@ -692,11 +833,16 @@ PrivacyGuardDb::PgForeachPrivacyCountByPackageId(const int userId, const int sta int PrivacyGuardDb::PgGetMonitorPolicy(const int userId, const std::string packageId, const std::string privacyId, int& monitorPolicy) { - int res = -1; char buf[BUF_SIZE]; - snprintf(buf, BUF_SIZE, "SELECT MONITOR_POLICY FROM MonitorPolicy WHERE (USER_ID=? OR USER_ID=%d) AND PKG_ID=? AND PRIVACY_ID=?", GLOBAL_USER); + /////////////////////////////////////////////////// + // prepare privacy monitor policy for global apps + /////////////////////////////////////////////////// + res = prepareGlobalPackagePolicy(userId); + TryCatchResLogReturn(res == PRIV_GUARD_ERROR_SUCCESS, m_dbMutex.unlock();, PRIV_GUARD_ERROR_INTERNAL_ERROR, "PgPrepareGlobalPackagePolicy: %d", res); + + snprintf(buf, BUF_SIZE, "SELECT MONITOR_POLICY FROM MonitorPolicy WHERE USER_ID=? AND PKG_ID=? AND PRIVACY_ID=?"); static const std::string query = std::string(buf); m_dbMutex.lock(); @@ -732,6 +878,9 @@ PrivacyGuardDb::PgGetMonitorPolicy(const int userId, const std::string packageId if ((res = sqlite3_step(m_stmt)) == SQLITE_ROW) { monitorPolicy = sqlite3_column_int(m_stmt, 0); } + + sqlite3_finalize(m_stmt); + m_dbMutex.unlock(); return PRIV_GUARD_ERROR_SUCCESS; @@ -778,6 +927,8 @@ PrivacyGuardDb::PgGetAllMonitorPolicy(std::list < std::pair < std::string, int > monitorPolicyList.push_back(std::pair < std::string, int > (userPkgIdPrivacyId, monitorPolicy)); } + sqlite3_finalize(m_stmt); + m_dbMutex.unlock(); PG_LOGD("monitorPolicyList.size() is [%d]", monitorPolicyList.size()); if(monitorPolicyList.size() > 0) { @@ -796,7 +947,13 @@ PrivacyGuardDb::PgForeachMonitorPolicyByPackageId(const int userId, const std::s int res = -1; char buf[BUF_SIZE]; - snprintf(buf, BUF_SIZE, "SELECT DISTINCT PRIVACY_ID, MONITOR_POLICY FROM MonitorPolicy WHERE (USER_ID=? OR USER_ID=%d) AND PKG_ID=?", GLOBAL_USER); + /////////////////////////////////////////////////// + // prepare privacy monitor policy for global apps + /////////////////////////////////////////////////// + res = prepareGlobalPackagePolicy(userId); + TryCatchResLogReturn(res == PRIV_GUARD_ERROR_SUCCESS, m_dbMutex.unlock();, PRIV_GUARD_ERROR_INTERNAL_ERROR, "PgPrepareGlobalPackagePolicy: %d", res); + + snprintf(buf, BUF_SIZE, "SELECT DISTINCT PRIVACY_ID, MONITOR_POLICY FROM MonitorPolicy WHERE USER_ID=? AND PKG_ID=?"); static const std::string query = std::string(buf); m_dbMutex.lock(); @@ -826,31 +983,40 @@ PrivacyGuardDb::PgForeachMonitorPolicyByPackageId(const int userId, const std::s // step while ((res = sqlite3_step(m_stmt)) == SQLITE_ROW) { - char* tmp_data = (char*)sqlite3_column_text(m_stmt, 0); if(tmp_data == NULL) { continue; } privacy_data_s p_data; - p_data.privacy_id = strdup(tmp_data); + memset(p_data.privacy_id, 0x0, LEN_PRIVACY_ID); + strncpy(p_data.privacy_id, tmp_data, strlen(tmp_data)); p_data.monitor_policy= sqlite3_column_int(m_stmt, 1); privacyInfoList.push_back(p_data); } + + sqlite3_finalize(m_stmt); + m_dbMutex.unlock(); return PRIV_GUARD_ERROR_SUCCESS; } int -PrivacyGuardDb::PgForeachPrivacyPackageId(const int userId, std::list < std::string > &packageList) +PrivacyGuardDb::PgForeachPackageIdUsingPrivacy(const int userId, std::list < std::string > &packageList) { int res = -1; char buf[BUF_SIZE]; - snprintf(buf, BUF_SIZE, "SELECT DISTINCT PKG_ID FROM MonitorPolicy WHERE (USER_ID=? OR USER_ID=%d)", GLOBAL_USER); + snprintf(buf, BUF_SIZE, "SELECT DISTINCT PKG_ID FROM MonitorPolicy WHERE USER_ID=?"); static const std::string query = std::string(buf); + /////////////////////////////////////////////////// + // prepare privacy monitor policy for global apps + /////////////////////////////////////////////////// + res = prepareGlobalPackagePolicy(userId); + TryCatchResLogReturn(res == PRIV_GUARD_ERROR_SUCCESS, m_dbMutex.unlock();, PRIV_GUARD_ERROR_INTERNAL_ERROR, "PgPrepareGlobalPackagePolicy: %d", res); + m_dbMutex.lock(); // initialize @@ -881,11 +1047,135 @@ PrivacyGuardDb::PgForeachPrivacyPackageId(const int userId, std::list < std::str } packageList.push_back(std::string(p_data)); } + + sqlite3_finalize(m_stmt); + + m_dbMutex.unlock(); + + return PRIV_GUARD_ERROR_SUCCESS; +} + +int +PrivacyGuardDb::prepareGlobalPackagePolicy(const int userId) +{ + int res = -1; + char buf[BUF_SIZE]; + std::list dataList; + + // parameter check + if (userId < 0) { + PG_LOGE("Invalid UserID [%d]", userId); + return PRIV_GUARD_ERROR_INVALID_PARAMETER; + } + + // parameter check + if ((uid_t)userId == GLOBAL_USER) { + PG_LOGD("Not need to prepare global package policy for Global User ID [%d]", userId); + return PRIV_GUARD_ERROR_SUCCESS; + } + + /////////////////////////////////////////////////////////////////////////////////////////////// + // Building-up cache for USER IDs + /////////////////////////////////////////////////////////////////////////////////////////////// + if (m_prepared_user_id_list == NULL) { + PG_LOGD("user id cache is not exist. Start to create user id cache."); + + m_dbMutex.lock(); + + // open db + if(m_bDBOpen == false) { + openSqliteDB(); + } + TryCatchResLogReturn(m_bDBOpen == true, m_dbMutex.unlock(), PRIV_GUARD_ERROR_IO_ERROR, "openSqliteDB : %d", res); + + // query + snprintf(buf, BUF_SIZE, "SELECT DISTINCT USER_ID FROM MonitorPolicy"); + static const std::string query_user_id = std::string(buf); + + // prepare + res = sqlite3_prepare_v2(m_sqlHandler, query_user_id.c_str(), -1, &m_stmt, NULL); + TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_prepare_v2 : %d", res); + + // step + while ((res = sqlite3_step(m_stmt)) == SQLITE_ROW) { + int temp_uid = sqlite3_column_int(m_stmt, 0); + //PG_LOGD("######### USER ID: [%d]", temp_uid); + // insert the user id to the cache + m_prepared_user_id_list = g_list_append(m_prepared_user_id_list, GINT_TO_POINTER(temp_uid)); + } + + sqlite3_finalize(m_stmt); + + m_dbMutex.unlock(); + } + + // finding input user id from the cache + PG_LOGD("Finding input user id (%d) from the cache.", userId); + GList *node = g_list_find(m_prepared_user_id_list, GINT_TO_POINTER(userId)); + if (node != NULL && GPOINTER_TO_INT(node->data) == userId) { + PG_LOGD("Global Package Policy for User ID [%d] is already created.", GPOINTER_TO_INT(node->data)); + return PRIV_GUARD_ERROR_SUCCESS; + } else { + PG_LOGD("There is no such User ID [%d] in the cache. Start to create Global Package Policy for User ID [%d].", userId, userId); + } + + /////////////////////////////////////////////////////////////////////////////////////////////// + // getting the list of all packages of GLOBALUSER and insert them into DB for input user_id + /////////////////////////////////////////////////////////////////////////////////////////////// + // query for getting the list of (package, privacy) of GLOBAL USER + snprintf(buf, BUF_SIZE, "SELECT DISTINCT PKG_ID, PRIVACY_ID FROM MonitorPolicy WHERE USER_ID=%d", GLOBAL_USER); + static const std::string query = std::string(buf); + + m_dbMutex.lock(); + + // open db + if(m_bDBOpen == false) { + openSqliteDB(); + } + TryCatchResLogReturn(m_bDBOpen == true, m_dbMutex.unlock(), PRIV_GUARD_ERROR_IO_ERROR, "openSqliteDB : %d", res); + + // prepare + res = sqlite3_prepare_v2(m_sqlHandler, query.c_str(), -1, &m_stmt, NULL); + TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock();, PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_prepare_v2 : %d", res); + + // step + while ((res = sqlite3_step(m_stmt)) == SQLITE_ROW) { + char *temp1 = (char*)sqlite3_column_text(m_stmt, 0); // package_id + char *temp2 = (char*)sqlite3_column_text(m_stmt, 1); // privacy_id + if(temp1 == NULL || temp2 == NULL) { + //PG_LOGD("PackageID: [%s], PrivacyID: [%s]", temp1, temp2); + continue; + } + package_privacy_data_s p_data; + memset(p_data.package_id, 0x0, LEN_PACKAGE_ID); + memset(p_data.privacy_id, 0x0, LEN_PRIVACY_ID); + strncpy(p_data.package_id, temp1, strlen(temp1)); + strncpy(p_data.privacy_id, temp2, strlen(temp2)); + dataList.push_back(p_data); + } + + sqlite3_finalize(m_stmt); + m_dbMutex.unlock(); + // insert privacy monitor policy of global packages for input userId + for (std::list::const_iterator iter = dataList.begin(); iter != dataList.end(); ++iter) { + char *temp_package_id = (char*)iter->package_id; + char *temp_privacy_id = (char*)iter->privacy_id; + //PG_LOGD("Adding [%d | %s | %s | %d] to the DB.", userId, temp_package_id, temp_privacy_id, DEFAULT_MONITOR_POLICY); + PgAddMonitorPolicy(userId, temp_package_id, temp_privacy_id, DEFAULT_MONITOR_POLICY); + } + + dataList.clear(); + + // add this User ID to the checked list + PG_LOGD("Adding User ID [%d] to the user id cache.", userId); + m_prepared_user_id_list = g_list_append(m_prepared_user_id_list, GINT_TO_POINTER(userId)); + return PRIV_GUARD_ERROR_SUCCESS; } + int PrivacyGuardDb::PgForeachPackageInfoByPrivacyId(const int userId, const std::string privacyId, std::list < package_data_s > &packageInfoList) { @@ -895,10 +1185,17 @@ PrivacyGuardDb::PgForeachPackageInfoByPrivacyId(const int userId, const std::str struct tm date; char buf[BUF_SIZE]; - snprintf(buf, BUF_SIZE, "SELECT DISTINCT PKG_ID, MONITOR_POLICY FROM MonitorPolicy WHERE (USER_ID=? OR USER_ID=%d) AND PRIVACY_ID=?", GLOBAL_USER); + /////////////////////////////////////////////////// + // prepare privacy monitor policy for global apps + /////////////////////////////////////////////////// + res = prepareGlobalPackagePolicy(userId); + TryCatchResLogReturn(res == PRIV_GUARD_ERROR_SUCCESS, m_dbMutex.unlock();, PRIV_GUARD_ERROR_INTERNAL_ERROR, "PgPrepareGlobalPackagePolicy: %d", res); + + + snprintf(buf, BUF_SIZE, "SELECT DISTINCT PKG_ID, MONITOR_POLICY FROM MonitorPolicy WHERE USER_ID=? AND PRIVACY_ID=?"); static const std::string query = std::string(buf); - snprintf(buf, BUF_SIZE, "SELECT COUNT(*), MAX(USE_DATE) FROM StatisticsMonitorInfo WHERE (USER_ID=? OR USER_ID=%d) AND PKG_ID=? AND PRIVACY_ID=? AND USE_DATE>=? AND USE_DATE<=?", GLOBAL_USER); + snprintf(buf, BUF_SIZE, "SELECT COUNT(*), MAX(USE_DATE) FROM StatisticsMonitorInfo WHERE USER_ID=? AND PKG_ID=? AND PRIVACY_ID=? AND USE_DATE>=? AND USE_DATE<=?"); static const std::string PKGINFO_SELECT = std::string(buf); // get start~end date (for 7 days) @@ -954,30 +1251,31 @@ PrivacyGuardDb::PgForeachPackageInfoByPrivacyId(const int userId, const std::str continue; } package_data_s p_data; - p_data.package_id = strdup(tmp_data); + memset(p_data.package_id, 0x0, LEN_PACKAGE_ID); + strncpy(p_data.package_id, tmp_data, strlen(tmp_data)); p_data.monitor_policy = sqlite3_column_int(m_stmt, 1); PG_LOGD("## package_id[%s]", p_data.package_id); PG_LOGD("## monitor_policy[%d]", p_data.monitor_policy); // prepare res = sqlite3_prepare_v2(m_sqlHandler, PKGINFO_SELECT.c_str(), -1, &infoStmt, NULL); - TryCatchResLogReturn(res == SQLITE_OK, SAFE_FREE(p_data.package_id); m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_prepare_v2 : %d", res); + TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_prepare_v2 : %d", res); // bind res = sqlite3_bind_int(infoStmt, 1, userId); - TryCatchResLogReturn(res == SQLITE_OK, SAFE_FREE(p_data.package_id); m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_int : %d", res); + TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_int : %d", res); res = sqlite3_bind_text(infoStmt, 2, p_data.package_id, -1, SQLITE_TRANSIENT); - TryCatchResLogReturn(res == SQLITE_OK, SAFE_FREE(p_data.package_id); m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_text : %d", res); + TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_text : %d", res); res = sqlite3_bind_text(infoStmt, 3, privacyId.c_str(), -1, SQLITE_TRANSIENT); - TryCatchResLogReturn(res == SQLITE_OK, SAFE_FREE(p_data.package_id); m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_text : %d", res); + TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_text : %d", res); res = sqlite3_bind_int(infoStmt, 4, start_date); - TryCatchResLogReturn(res == SQLITE_OK, SAFE_FREE(p_data.package_id); m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_int : %d", res); + TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_int : %d", res); res = sqlite3_bind_int(infoStmt, 5, end_date); - TryCatchResLogReturn(res == SQLITE_OK, SAFE_FREE(p_data.package_id); m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_int : %d", res); + TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_int : %d", res); if ((res = sqlite3_step(infoStmt)) == SQLITE_ROW) { int count = sqlite3_column_int(infoStmt, 0); @@ -993,8 +1291,11 @@ PrivacyGuardDb::PgForeachPackageInfoByPrivacyId(const int userId, const std::str packageInfoList.push_back(p_data); } - sqlite3_reset(infoStmt); + sqlite3_finalize(infoStmt); } + + sqlite3_finalize(m_stmt); + m_dbMutex.unlock(); return PRIV_GUARD_ERROR_SUCCESS; @@ -1006,9 +1307,15 @@ PrivacyGuardDb::PgUpdateMonitorPolicy(const int userId, const std::string packag int res = -1; char buf[BUF_SIZE]; - snprintf(buf, BUF_SIZE, "UPDATE MonitorPolicy SET MONITOR_POLICY=? WHERE (USER_ID=? OR USER_ID=%d) AND PKG_ID=? AND PRIVACY_ID=?", GLOBAL_USER); + snprintf(buf, BUF_SIZE, "UPDATE MonitorPolicy SET MONITOR_POLICY=? WHERE USER_ID=? AND PKG_ID=? AND PRIVACY_ID=?"); static const std::string query = std::string(buf); + /////////////////////////////////////////////////// + // prepare privacy monitor policy for global apps + /////////////////////////////////////////////////// + res = prepareGlobalPackagePolicy(userId); + TryCatchResLogReturn(res == PRIV_GUARD_ERROR_SUCCESS, m_dbMutex.unlock();, PRIV_GUARD_ERROR_INTERNAL_ERROR, "PgPrepareGlobalPackagePolicy: %d", res); + m_dbMutex.lock(); // initialize @@ -1044,6 +1351,8 @@ PrivacyGuardDb::PgUpdateMonitorPolicy(const int userId, const std::string packag res = sqlite3_step(m_stmt); TryCatchResLogReturn(res == SQLITE_DONE, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_step : %d", res); + sqlite3_finalize(m_stmt); + m_dbMutex.unlock(); return PRIV_GUARD_ERROR_SUCCESS; @@ -1056,6 +1365,12 @@ PrivacyGuardDb::PgAddMainMonitorPolicy(const int userId) static const std::string QUERY_INSERT = std::string("INSERT INTO MainMonitorPolicy(USER_ID) VALUES(?)"); + /////////////////////////////////////////////////// + // prepare privacy monitor policy for global apps + /////////////////////////////////////////////////// + res = prepareGlobalPackagePolicy(userId); + TryCatchResLogReturn(res == PRIV_GUARD_ERROR_SUCCESS, m_dbMutex.unlock();, PRIV_GUARD_ERROR_INTERNAL_ERROR, "PgPrepareGlobalPackagePolicy: %d", res); + m_dbMutex.lock(); // initialize @@ -1084,20 +1399,28 @@ PrivacyGuardDb::PgAddMainMonitorPolicy(const int userId) res = sqlite3_step(m_stmt); TryCatchResLogReturn(res == SQLITE_DONE, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_step : %d", res); + sqlite3_finalize(m_stmt); + m_dbMutex.unlock(); return PRIV_GUARD_ERROR_SUCCESS; } int -PrivacyGuardDb::PgUpdateMainMonitorPolicy(const int userId, const bool mainMonitorPolicy) +PrivacyGuardDb::PgUpdateMainMonitorPolicy(const int userId, const int mainMonitorPolicy) { int res = -1; char buf[BUF_SIZE]; - snprintf(buf, BUF_SIZE, "UPDATE MainMonitorPolicy SET MAIN_MONITOR_POLICY=? WHERE (USER_ID=? OR USER_ID=%d)", GLOBAL_USER); + snprintf(buf, BUF_SIZE, "UPDATE MainMonitorPolicy SET MAIN_MONITOR_POLICY=? WHERE USER_ID=? "); static const std::string query = std::string(buf); + /////////////////////////////////////////////////// + // prepare privacy monitor policy for global apps + /////////////////////////////////////////////////// + res = prepareGlobalPackagePolicy(userId); + TryCatchResLogReturn(res == PRIV_GUARD_ERROR_SUCCESS, m_dbMutex.unlock();, PRIV_GUARD_ERROR_INTERNAL_ERROR, "PgPrepareGlobalPackagePolicy: %d", res); + m_dbMutex.lock(); // initialize @@ -1127,6 +1450,8 @@ PrivacyGuardDb::PgUpdateMainMonitorPolicy(const int userId, const bool mainMonit res = sqlite3_step(m_stmt); TryCatchResLogReturn(res == SQLITE_DONE, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_step : %d", res); + sqlite3_finalize(m_stmt); + m_dbMutex.unlock(); #if 0 @@ -1138,14 +1463,20 @@ PrivacyGuardDb::PgUpdateMainMonitorPolicy(const int userId, const bool mainMonit } int -PrivacyGuardDb::PgGetMainMonitorPolicy(const int userId, bool &mainMonitorPolicy) +PrivacyGuardDb::PgGetMainMonitorPolicy(const int userId, int &mainMonitorPolicy) { int res = -1; char buf[BUF_SIZE]; - snprintf(buf, BUF_SIZE, "SELECT MAIN_MONITOR_POLICY FROM MainMonitorPolicy WHERE (USER_ID=? OR USER_ID=%d)", GLOBAL_USER); + snprintf(buf, BUF_SIZE, "SELECT MAIN_MONITOR_POLICY FROM MainMonitorPolicy WHERE USER_ID=?"); static const std::string query = std::string(buf); + /////////////////////////////////////////////////// + // prepare privacy monitor policy for global apps + /////////////////////////////////////////////////// + res = prepareGlobalPackagePolicy(userId); + TryCatchResLogReturn(res == PRIV_GUARD_ERROR_SUCCESS, m_dbMutex.unlock();, PRIV_GUARD_ERROR_INTERNAL_ERROR, "PgPrepareGlobalPackagePolicy: %d", res); + m_dbMutex.lock(); // initialize @@ -1169,12 +1500,15 @@ PrivacyGuardDb::PgGetMainMonitorPolicy(const int userId, bool &mainMonitorPolicy TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_int : %d", res); // step - mainMonitorPolicy = false; + mainMonitorPolicy = 0; if ((res = sqlite3_step(m_stmt)) == SQLITE_ROW) { mainMonitorPolicy = sqlite3_column_int(m_stmt, 0); + + sqlite3_finalize(m_stmt); m_dbMutex.unlock(); } else { + sqlite3_finalize(m_stmt); m_dbMutex.unlock(); res = PgAddMainMonitorPolicy(userId); TryReturn(res == PRIV_GUARD_ERROR_SUCCESS, res, , "PgAddMainMonitorPolicy failed : %d", res); @@ -1190,6 +1524,12 @@ PrivacyGuardDb::PgDeleteMainMonitorPolicyByUserId(const int userId) static const std::string QUERY_DELETE = std::string("DELETE FROM MainMonitorPolicy WHERE USER_ID=?"); + /////////////////////////////////////////////////// + // prepare privacy monitor policy for global apps + /////////////////////////////////////////////////// + res = prepareGlobalPackagePolicy(userId); + TryCatchResLogReturn(res == PRIV_GUARD_ERROR_SUCCESS, m_dbMutex.unlock();, PRIV_GUARD_ERROR_INTERNAL_ERROR, "PgPrepareGlobalPackagePolicy: %d", res); + m_dbMutex.lock(); // initialize @@ -1216,12 +1556,15 @@ PrivacyGuardDb::PgDeleteMainMonitorPolicyByUserId(const int userId) res = sqlite3_step(m_stmt); TryCatchResLogReturn(res == SQLITE_DONE, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_step : %d", res); + sqlite3_finalize(m_stmt); + m_dbMutex.unlock(); return PRIV_GUARD_ERROR_SUCCESS; } PrivacyGuardDb::PrivacyGuardDb(void) + : m_bInitialized(false) { // open DB m_bDBOpen = false; @@ -1246,8 +1589,10 @@ PrivacyGuardDb::~PrivacyGuardDb(void) if (m_bInitialized == true) { m_dbMutex.lock(); - g_list_free(m_privacy_list); - cynara_monitor_configuration_destroy(p_conf); + g_list_free_full(m_privacy_list, g_free); + m_privacy_list = NULL; + g_list_free_full(m_prepared_user_id_list, g_free); + m_prepared_user_id_list = NULL; m_bInitialized = false; m_dbMutex.unlock(); } diff --git a/server/src/SocketService.cpp b/server/src/SocketService.cpp old mode 100644 new mode 100755 diff --git a/server/src/main.cpp b/server/src/main.cpp old mode 100644 new mode 100755 diff --git a/server/src/privacy_guard_daemon.cpp b/server/src/privacy_guard_daemon.cpp old mode 100644 new mode 100755 diff --git a/server/src/service/PrivacyInfoService.cpp b/server/src/service/PrivacyInfoService.cpp old mode 100644 new mode 100755 index df42777..052475f --- a/server/src/service/PrivacyInfoService.cpp +++ b/server/src/service/PrivacyInfoService.cpp @@ -19,6 +19,21 @@ #include "Utils.h" void +PrivacyInfoService::PgAddPrivacyAccessLog(SocketConnection* pConnector) +{ + int userId = 0; + std::string packageId; + std::string privacyId; + time_t accessTime = 0; + + pConnector->read(&userId, &packageId, &privacyId, &accessTime); + + int result = PrivacyGuardDb::getInstance()->PgAddPrivacyAccessLog(userId, packageId, privacyId, accessTime); + + pConnector->write(result); +} + +void PrivacyInfoService::PgAddMonitorPolicy(SocketConnection* pConnector) { int userId = 0; @@ -33,12 +48,20 @@ PrivacyInfoService::PgAddMonitorPolicy(SocketConnection* pConnector) pConnector->write(result); } -void +/*void PrivacyInfoService::PgDeleteAllLogsAndMonitorPolicy(SocketConnection* pConnector) { int result = PrivacyGuardDb::getInstance()->PgDeleteAllLogsAndMonitorPolicy(); pConnector->write(result); +}*/ + +void +PrivacyInfoService::PgDeleteAllLogs(SocketConnection* pConnector) +{ + int result = PrivacyGuardDb::getInstance()->PgDeleteAllLogs(); + + pConnector->write(result); } void @@ -53,7 +76,7 @@ PrivacyInfoService::PgDeleteLogsByPackageId(SocketConnection* pConnector) pConnector->write(result); } -void +/*void PrivacyInfoService::PgDeleteMonitorPolicyByPackageId(SocketConnection* pConnector) { std::string packageId; @@ -63,7 +86,7 @@ PrivacyInfoService::PgDeleteMonitorPolicyByPackageId(SocketConnection* pConnecto int result = PrivacyGuardDb::getInstance()->PgDeleteMonitorPolicyByPackageId(packageId); pConnector->write(result); -} +}*/ void PrivacyInfoService::PgForeachTotalPrivacyCountOfPackage(SocketConnection* pConnector) @@ -132,14 +155,14 @@ PrivacyInfoService::PgForeachPrivacyCountByPackageId(SocketConnection* pConnecto } void -PrivacyInfoService::PgForeachPrivacyPackageId(SocketConnection* pConnector) +PrivacyInfoService::PgForeachPackageIdUsingPrivacy(SocketConnection* pConnector) { int userId = 0; std::list < std::string > packageList; pConnector->read(&userId); - int result = PrivacyGuardDb::getInstance()->PgForeachPrivacyPackageId(userId, packageList); + int result = PrivacyGuardDb::getInstance()->PgForeachPackageIdUsingPrivacy(userId, packageList); pConnector->write(result); pConnector->write(packageList); @@ -236,7 +259,7 @@ void PrivacyInfoService::PgUpdateMainMonitorPolicy(SocketConnection* pConnector) { int userId = 0; - bool mainMonitorPolicy = false; + int mainMonitorPolicy = 0; pConnector->read(&userId, &mainMonitorPolicy); @@ -249,7 +272,7 @@ void PrivacyInfoService::PgGetMainMonitorPolicy(SocketConnection* pConnector) { int userId = 0; - bool mainMonitorPolicy = false; + int mainMonitorPolicy = 0; pConnector->read(&userId); @@ -258,15 +281,3 @@ PrivacyInfoService::PgGetMainMonitorPolicy(SocketConnection* pConnector) pConnector->write(result); pConnector->write(mainMonitorPolicy); } - -void -PrivacyInfoService::PgDeleteMainMonitorPolicyByUserId(SocketConnection* pConnector) -{ - int userId = 0; - - pConnector->read(&userId); - - int result = PrivacyGuardDb::getInstance()->PgDeleteMainMonitorPolicyByUserId(userId); - - pConnector->write(result); -} diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt new file mode 100755 index 0000000..457810a --- /dev/null +++ b/test/CMakeLists.txt @@ -0,0 +1,29 @@ +CMAKE_MINIMUM_REQUIRED(VERSION 2.6) +SET(PACKAGE_NAME tc-privacy-guard) +PROJECT(${PACKAGE_NAME}) + +SET(TC1_NAME tc-privacy-guard) + +SET(TC1_SRCS ${TC1_NAME}.c) + +INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/include ${CMAKE_SOURCE_DIR}/client/inc) + +SET(requires glib-2.0 dlog) +INCLUDE(FindPkgConfig) +pkg_check_modules(${PACKAGE_NAME} REQUIRED ${requires}) +FOREACH(flag ${${PACKAGE_NAME}_CFLAGS}) + SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}") +ENDFOREACH(flag) + +# Compiler flags +SET(EXTRA_CXXFLAGS "${EXTRA_CFLAGS} -fPIC -Wall -Werror") +SET(CMAKE_CXX_FLAGS_DEBUG "-O0 -g") + +SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -fPIC -Wall -fvisibility=hidden -D_WITH_SYSTEMD_") +SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS} ${PACKAGE_NAME_CFLAGS}") + +ADD_EXECUTABLE(${TC1_NAME} ${TC1_SRCS}) + +TARGET_LINK_LIBRARIES(${TC1_NAME} "privacy-guard-client") + +INSTALL(TARGETS ${TC1_NAME} DESTINATION /usr/bin) diff --git a/test/tc-privacy-guard.c b/test/tc-privacy-guard.c new file mode 100755 index 0000000..57c0936 --- /dev/null +++ b/test/tc-privacy-guard.c @@ -0,0 +1,1499 @@ +#include +#include +#include +#include +#include +#include +#include + +#define BRIGHTNESS 0 +#define RED 31 +#define GREEN 32 +#define YELLOW 33 +#define BG_BLACK 40 + +#define USER_ID_OWNER 5001 +#define INVALID_USER_ID -1 + +#define LEN_FUNCTION_NAME 128 +#define LEN_PRIVACY_ID 128 +#define LEN_PACKAGE_ID 128 +#define LEN_PARAM 128 + +#define PRIVACY_ACCOUNT "http://tizen.org/privacy/account" +#define PRIVACY_BOOKMARK "http://tizen.org/privacy/bookmark" +#define PRIVACY_CALENDAR "http://tizen.org/privacy/calendar" +#define PRIVACY_CALL "http://tizen.org/privacy/call" +#define PRIVACY_USERHISTORY "http://tizen.org/privacy/userhistory" +#define PRIVACY_CAMERA "http://tizen.org/privacy/camera" +#define PRIVACY_CONTACTS "http://tizen.org/privacy/contacts" +#define PRIVACY_SENSOR "http://tizen.org/privacy/sensor" +#define PRIVACY_LOCATION "http://tizen.org/privacy/location" +#define PRIVACY_MESSAGE "http://tizen.org/privacy/message" +#define PRIVACY_MICROPHONE "http://tizen.org/privacy/microphone" + + +typedef struct { + char function_name[LEN_FUNCTION_NAME]; + char privacy_id[LEN_PRIVACY_ID]; + char package_id[LEN_PACKAGE_ID]; +} TEST_DATA; + +static int fail_cnt = 0; +static int success_cnt = 0; + +static GList *all_package_list = NULL; + +static GList *package_list_using_location = NULL; +static GList *package_list_using_account = NULL; +static GList *package_list_using_bookmark = NULL; +static GList *package_list_using_calendar = NULL; +static GList *package_list_using_call = NULL; +static GList *package_list_using_userhistory = NULL; +static GList *package_list_using_camera = NULL; +static GList *package_list_using_contacts = NULL; +static GList *package_list_using_message = NULL; +static GList *package_list_using_microphone = NULL; +static GList *package_list_using_sensor = NULL; + +// temp utility +/*static void display_all_packages(void) +{ + int i; + + for (i = 0; i < (int)g_list_length(all_package_list); i++) { + char *package_id = (char*)g_list_nth_data(all_package_list, i); + printf("######################### found package: [%d][%s]\n", i, package_id); + } +}*/ + +/////////////////////////////////////////////////////////////////////////////// +// test utilities +/////////////////////////////////////////////////////////////////////////////// +static void __change_color_to_red(void) +{ + printf("%c[%d;%dm", 0x1B, BRIGHTNESS, RED); +} + +static void __change_color_to_green(void) +{ + printf("%c[%d;%dm", 0x1B, BRIGHTNESS, GREEN); +} + +static void __change_color_to_yellow(void) +{ + printf("%c[%d;%dm", 0x1B, BRIGHTNESS, YELLOW); +} + +static void __change_color_to_origin(void) +{ + printf("%c[%dm", 0x1B, 0); +} + +static void __start_test(const char *function_name) +{ + __change_color_to_yellow(); + printf("================================================================================\n"); + printf("\t%s\n", function_name); + printf("================================================================================\n"); + __change_color_to_origin(); +} + +static void __finish_test(void) +{ + __change_color_to_yellow(); + printf("\n\n"); + __change_color_to_origin(); +} + +/////////////////////////////////////////////////////////////////////////////// +// test verfication utility +/////////////////////////////////////////////////////////////////////////////// +static const char* __get_result_string(int ret) +{ + if (ret == PRIV_GUARD_ERROR_SUCCESS) { + return "PRIV_GUARD_ERROR_SUCCESS"; + } else if (ret == PRIV_GUARD_ERROR_NOT_INITIALIZED) { + return "PRIV_GUARD_ERROR_NOT_INITIALIZED"; + } else if (ret == PRIV_GUARD_ERROR_INVALID_PARAMETER) { + return "PRIV_GUARD_ERROR_INVALID_PARAMETER"; + } else if (ret == PRIV_GUARD_ERROR_OUT_OF_MEMORY) { + return "PRIV_GUARD_ERROR_OUT_OF_MEMORY"; + } else if (ret == PRIV_GUARD_ERROR_IO_ERROR) { + return "PRIV_GUARD_ERROR_IO_ERROR"; + } else if (ret == PRIV_GUARD_ERROR_NO_DATA) { + return "PRIV_GUARD_ERROR_NO_DATA"; + } else if (ret == PRIV_GUARD_ERROR_DB_ERROR) { + return "PRIV_GUARD_ERROR_DB_ERROR"; + } else if (ret == PRIV_GUARD_ERROR_IPC_ERROR) { + return "PRIV_GUARD_ERROR_IPC_ERROR"; + } else if (ret == PRIV_GUARD_ERROR_INVALID_STATE) { + return "PRIV_GUARD_ERROR_INVALID_STATE"; + } else if (ret == PRIV_GUARD_ERROR_SYSTEM_ERROR) { + return "PRIV_GUARD_ERROR_SYSTEM_ERROR"; + } else if (ret == PRIV_GUARD_ERROR_USER_NOT_CONSENTED) { + return "PRIV_GUARD_ERROR_USER_NOT_CONSENTED"; + } else if (ret == PRIV_GUARD_ERROR_INTERNAL_ERROR) { + return "PRIV_GUARD_ERROR_INTERNAL_ERROR"; + } else if (ret == PRIV_GUARD_ERROR_NOT_PERMITTED) { + return "PRIV_GUARD_ERROR_NOT_PERMITTED"; + } else { + return "PRIV_GUARD_ERROR_UNKNOWN"; + } + + return "FAIL"; +} + +static const char *__get_bool_string(const bool result) +{ + if (result == true) { + return "TRUE"; + } + + return "FALSE"; +} + +static void __check_result_error_code(int expected, int actual, int line) +{ + printf("expected result = %s, actual result = %s\n", __get_result_string(expected), __get_result_string(actual)); + + if(expected != actual) { + printf("not matched [line %d]\n", line); + __change_color_to_red(); + printf("test fail\n"); + fail_cnt++; + } else { + printf("matched [line %d]\n", line); + __change_color_to_green(); + printf("test success\n"); + success_cnt++; + } + __change_color_to_origin(); +} + +static void __check_result_int(int expected, int actual, int line) +{ + printf("expected result = %d, actual result = %d\n", expected, actual); + + if(expected != actual) { + printf("not matched [line %d]\n", line); + __change_color_to_red(); + printf("test fail\n"); + fail_cnt++; + } else { + printf("matched [line %d]\n", line); + __change_color_to_green(); + printf("test success\n"); + success_cnt++; + } + __change_color_to_origin(); +} + +static void __check_result_int_over_zero(int actual, int line) +{ + printf("expected result > 0, actual result = %d\n", actual); + + if (actual <= 0) { + printf("not matched [line %d]\n", line); + __change_color_to_red(); + printf("test fail\n"); + fail_cnt++; + } else { + printf("matched [line %d]\n", line); + __change_color_to_green(); + printf("test success\n"); + success_cnt++; + } + __change_color_to_origin(); +} + +static void __check_result_bool(bool expected, bool actual, int line) +{ + printf("expected result = %s, actual result = %s\n", __get_bool_string(expected), __get_bool_string(actual)); + + if(expected != actual) { + printf("not matched [line %d]\n", line); + __change_color_to_red(); + printf("test fail\n"); + fail_cnt++; + } else { + printf("matched [line %d]\n", line); + __change_color_to_green(); + printf("test success\n"); + success_cnt++; + } + __change_color_to_origin(); +} + +static void __check_pointer_not_null(const void *ptr, int line) +{ + printf("expected result = \"NOT_NULL_POINTER\", actual result = %p\n", ptr); + + if(!ptr) { + printf("null string [line %d]\n", line); + __change_color_to_red(); + printf("test fail\n"); + fail_cnt++; + } else { + printf("not null string [line %d]\n", line); + __change_color_to_green(); + printf("test success\n"); + success_cnt++; + } + __change_color_to_origin(); +} + +/////////////////////////////////////////////////////////////////////////////// +// callbacks +/////////////////////////////////////////////////////////////////////////////// + +// callback for privacy_guard_client_monitor_policy_cb +static bool __monitor_policy_cb(const char *privacy_id, const int monitor_policy, void *user_data) +{ + __change_color_to_yellow(); + printf("%s is called.\n", __FUNCTION__); + __change_color_to_origin(); + + if (!user_data) { + printf("user_data is null.\n"); + return false; + } + + TEST_DATA *data = user_data; + int on = 1, off = 0; + + if (strncmp(data->function_name, "__test_privacy_guard_client_update_monitor_policy_02", strlen(data->function_name)) == 0) { + if (strncmp(privacy_id, data->privacy_id, strlen(data->privacy_id)) == 0) { + __check_result_int(off, monitor_policy, __LINE__); + } + } else if (strncmp(data->function_name, "__test_privacy_guard_client_foreach_monitor_policy_by_package_id_02", strlen(data->function_name)) == 0) { + if (strncmp(privacy_id, data->privacy_id, strlen(data->privacy_id)) == 0) { + __check_result_int(on, monitor_policy, __LINE__); + } + } + + return true; +} + +// callback for privacy_guard_client_package_id_cb +static bool __package_id_cb(const char *package_id, void *user_data) +{ + __change_color_to_yellow(); + printf("%s is called.\n", __FUNCTION__); + __change_color_to_origin(); + + char *data = (char*)user_data; + if (strncmp(data, "__db_util_get_all_package_list_using_privacy", strlen(data)) == 0) { + all_package_list = g_list_append(all_package_list, strdup(package_id)); + } else if (strncmp(data, "__test_privacy_guard_client_foreach_privacy_package_id", strlen(data)) == 0) { + bool is_privacy_package = false; + int res = privacy_guard_client_check_privacy_package(USER_ID_OWNER, package_id, &is_privacy_package); + __check_result_error_code(PRIV_GUARD_ERROR_SUCCESS, res, __LINE__); + __check_result_bool(true, is_privacy_package, __LINE__); + } + + return true; +} + +// callback for privacy_guard_client_package_info_cb +static bool __package_info_cb(const char *package_id, const int count, const time_t time, const int monitor_policy, void *user_data) +{ + __change_color_to_yellow(); + printf("%s is called.\n", __FUNCTION__); + __change_color_to_origin(); + + if (!user_data) { + printf("user_data is null.\n"); + return false; + } + + char *privacy_id = (char*)user_data; + GList *node; + + if (strncmp(privacy_id, PRIVACY_ACCOUNT, strlen(privacy_id)) == 0) { + node = g_list_find_custom(package_list_using_account, package_id, (GCompareFunc)strcmp); + __check_pointer_not_null(node, __LINE__); + } else if (strncmp(privacy_id, PRIVACY_BOOKMARK, strlen(privacy_id)) == 0) { + node = g_list_find_custom(package_list_using_bookmark, package_id, (GCompareFunc)strcmp); + __check_pointer_not_null(node, __LINE__); + } else if (strncmp(privacy_id, PRIVACY_CALENDAR, strlen(privacy_id)) == 0) { + node = g_list_find_custom(package_list_using_calendar, package_id, (GCompareFunc)strcmp); + __check_pointer_not_null(node, __LINE__); + } else if (strncmp(privacy_id, PRIVACY_CALL, strlen(privacy_id)) == 0) { + node = g_list_find_custom(package_list_using_call, package_id, (GCompareFunc)strcmp); + __check_pointer_not_null(node, __LINE__); + } else if (strncmp(privacy_id, PRIVACY_USERHISTORY, strlen(privacy_id)) == 0) { + node = g_list_find_custom(package_list_using_userhistory, package_id, (GCompareFunc)strcmp); + __check_pointer_not_null(node, __LINE__); + } else if (strncmp(privacy_id, PRIVACY_CAMERA, strlen(privacy_id)) == 0) { + node = g_list_find_custom(package_list_using_camera, package_id, (GCompareFunc)strcmp); + __check_pointer_not_null(node, __LINE__); + } else if (strncmp(privacy_id, PRIVACY_CONTACTS, strlen(privacy_id)) == 0) { + node = g_list_find_custom(package_list_using_contacts, package_id, (GCompareFunc)strcmp); + __check_pointer_not_null(node, __LINE__); + } else if (strncmp(privacy_id, PRIVACY_SENSOR, strlen(privacy_id)) == 0) { + node = g_list_find_custom(package_list_using_sensor, package_id, (GCompareFunc)strcmp); + __check_pointer_not_null(node, __LINE__); + } else if (strncmp(privacy_id, PRIVACY_LOCATION, strlen(privacy_id)) == 0) { + node = g_list_find_custom(package_list_using_location, package_id, (GCompareFunc)strcmp); + __check_pointer_not_null(node, __LINE__); + } else if (strncmp(privacy_id, PRIVACY_MESSAGE, strlen(privacy_id)) == 0) { + node = g_list_find_custom(package_list_using_message, package_id, (GCompareFunc)strcmp); + __check_pointer_not_null(node, __LINE__); + } else if (strncmp(privacy_id, PRIVACY_MICROPHONE, strlen(privacy_id)) == 0) { + node = g_list_find_custom(package_list_using_microphone, package_id, (GCompareFunc)strcmp); + __check_pointer_not_null(node, __LINE__); + } + + return true; +} + +// callback for privacy_guard_client_privacy_count_cb +static bool __privacy_count_cb(const char *privacy_id, const int count, void *user_data) +{ + __change_color_to_yellow(); + printf("%s is called.\n", __FUNCTION__); + __change_color_to_origin(); + + if (!user_data) { + printf("user_data is null.\n"); + return false; + } + + char *data = (char*)user_data; + + if (strncmp(data, "__test_privacy_guard_client_foreach_total_privacy_count_of_privacy_02", strlen(data)) == 0) { + __check_result_int_over_zero(count, __LINE__); + } + + return true; +} + +// callback for privacy_guard_client_privacy_count_of_package_cb +static bool __privacy_count_of_package_cb(const char *package_id, const int count, void *user_data) +{ + __change_color_to_yellow(); + printf("%s is called.\n", __FUNCTION__); + __change_color_to_origin(); + + if (!user_data) { + printf("user_data is null.\n"); + return false; + } + + char *data = (char*)user_data; + + if (strncmp(data, "__test_privacy_guard_client_foreach_total_privacy_count_of_package_02", strlen(data)) == 0) { + __check_result_int_over_zero(count, __LINE__); + } else if (strncmp(data, "__test_privacy_guard_client_foreach_privacy_count_by_privacy_id_02", strlen(data)) == 0) { + __check_result_int(1, count, __LINE__); + } else if (strncmp(data, "__test_privacy_guard_client_foreach_privacy_count_by_package_id_02", strlen(data)) == 0) { + __check_result_int(1, count, __LINE__); + } else if (strncmp(data, PRIVACY_ACCOUNT, strlen(data)) == 0) { + package_list_using_account = g_list_append(package_list_using_account, strdup(package_id)); + } else if (strncmp(data, PRIVACY_BOOKMARK, strlen(data)) == 0) { + package_list_using_bookmark = g_list_append(package_list_using_bookmark, strdup(package_id)); + } else if (strncmp(data, PRIVACY_CALENDAR, strlen(data)) == 0) { + package_list_using_calendar = g_list_append(package_list_using_calendar, strdup(package_id)); + } else if (strncmp(data, PRIVACY_CALL, strlen(data)) == 0) { + package_list_using_call = g_list_append(package_list_using_call, strdup(package_id)); + } else if (strncmp(data, PRIVACY_USERHISTORY, strlen(data)) == 0) { + package_list_using_userhistory = g_list_append(package_list_using_userhistory, strdup(package_id)); + } else if (strncmp(data, PRIVACY_CAMERA, strlen(data)) == 0) { + package_list_using_camera = g_list_append(package_list_using_camera, strdup(package_id)); + } else if (strncmp(data, PRIVACY_CONTACTS, strlen(data)) == 0) { + package_list_using_contacts = g_list_append(package_list_using_contacts, strdup(package_id)); + } else if (strncmp(data, PRIVACY_SENSOR, strlen(data)) == 0) { + package_list_using_sensor = g_list_append(package_list_using_sensor, strdup(package_id)); + } else if (strncmp(data, PRIVACY_LOCATION, strlen(data)) == 0) { + package_list_using_location = g_list_append(package_list_using_location, strdup(package_id)); + } else if (strncmp(data, PRIVACY_MESSAGE, strlen(data)) == 0) { + package_list_using_message = g_list_append(package_list_using_message, strdup(package_id)); + } else if (strncmp(data, PRIVACY_MICROPHONE, strlen(data)) == 0) { + package_list_using_microphone = g_list_append(package_list_using_microphone, strdup(package_id)); + } + + return true; +} + +/////////////////////////////////////////////////////////////////////////////// +// test preparation utility +/////////////////////////////////////////////////////////////////////////////// +static int __db_util_delete_all_logs(void) +{ + int res = privacy_guard_client_delete_all_logs(); + if (res != PRIV_GUARD_ERROR_SUCCESS) { + printf("failed to operate privacy_guard_client_delete_all_logs() [%d]\n", res); + } + + return res; +} + +static int __db_util_get_all_package_list_using_privacy(void) +{ + int res = privacy_guard_client_foreach_package_id_using_privacy(USER_ID_OWNER, __package_id_cb, (char*)__FUNCTION__); + if (res != PRIV_GUARD_ERROR_SUCCESS) { + printf("failed to operate privacy_guard_client_add_privacy_access_log() [%d]\n", res); + } + + //display_all_packages(); + + return res; +} + +static int __db_util_get_package_list_using_privacy(const char *privacy_id) +{ + int res; + time_t start_date, end_date; + + end_date = time(NULL); + start_date = end_date - (24 * 60 * 60) * 7; // almost 7 days + + res = privacy_guard_client_foreach_privacy_count_by_privacy_id(USER_ID_OWNER, start_date, end_date, privacy_id, __privacy_count_of_package_cb, (char*)privacy_id); + if (res != PRIV_GUARD_ERROR_SUCCESS) { + printf("Failed to operate privacy_guard_client_foreach_privacy_count_by_privacy_id() [%d]\n", res); + } + + return res; +} + +static int __db_util_add_test_logs(void) +{ + int i; + time_t access_time; + int res = -1; + + access_time = time(NULL); + + for (i = 0; i < (int)g_list_length(all_package_list); i++) { + char *package_id = (char*)g_list_nth_data(all_package_list, i); + if (package_id) { + //printf("## [%d] package_id[%s]\n", i, package_id); + // account + res = privacy_guard_client_update_monitor_policy(USER_ID_OWNER, package_id, PRIVACY_ACCOUNT, 1); + if (res != PRIV_GUARD_ERROR_SUCCESS) printf("Failed to operate privacy_guard_client_update_monitor_policy() [%d]\n", res); + res = privacy_guard_client_add_privacy_access_log(USER_ID_OWNER, package_id, PRIVACY_ACCOUNT, access_time - i * 100); + if (res != PRIV_GUARD_ERROR_SUCCESS) printf("Failed to operate privacy_guard_client_add_privacy_access_log() [%d]\n", res); + // bookmark + res = privacy_guard_client_update_monitor_policy(USER_ID_OWNER, package_id, PRIVACY_BOOKMARK, 1); + if (res != PRIV_GUARD_ERROR_SUCCESS) printf("Failed to operate privacy_guard_client_update_monitor_policy() [%d]\n", res); + res = privacy_guard_client_add_privacy_access_log(USER_ID_OWNER, package_id, PRIVACY_BOOKMARK, access_time - i * 100); + if (res != PRIV_GUARD_ERROR_SUCCESS) printf("Failed to operate privacy_guard_client_add_privacy_access_log() [%d]\n", res); + // calendar + res = privacy_guard_client_update_monitor_policy(USER_ID_OWNER, package_id, PRIVACY_CALENDAR, 1); + if (res != PRIV_GUARD_ERROR_SUCCESS) printf("Failed to operate privacy_guard_client_update_monitor_policy() [%d]\n", res); + res = privacy_guard_client_add_privacy_access_log(USER_ID_OWNER, package_id, PRIVACY_CALENDAR, access_time - i * 100); + if (res != PRIV_GUARD_ERROR_SUCCESS) printf("Failed to operate privacy_guard_client_add_privacy_access_log() [%d]\n", res); + // call + res = privacy_guard_client_update_monitor_policy(USER_ID_OWNER, package_id, PRIVACY_CALL, 1); + if (res != PRIV_GUARD_ERROR_SUCCESS) printf("Failed to operate privacy_guard_client_update_monitor_policy() [%d]\n", res); + res = privacy_guard_client_add_privacy_access_log(USER_ID_OWNER, package_id, PRIVACY_CALL, access_time - i * 100); + if (res != PRIV_GUARD_ERROR_SUCCESS) printf("Failed to operate privacy_guard_client_add_privacy_access_log() [%d]\n", res); + // userhistory + res = privacy_guard_client_update_monitor_policy(USER_ID_OWNER, package_id, PRIVACY_USERHISTORY, 1); + if (res != PRIV_GUARD_ERROR_SUCCESS) printf("Failed to operate privacy_guard_client_update_monitor_policy() [%d]\n", res); + res = privacy_guard_client_add_privacy_access_log(USER_ID_OWNER, package_id, PRIVACY_USERHISTORY, access_time - i * 100); + if (res != PRIV_GUARD_ERROR_SUCCESS) printf("Failed to operate privacy_guard_client_add_privacy_access_log() [%d]\n", res); + // camera + res = privacy_guard_client_update_monitor_policy(USER_ID_OWNER, package_id, PRIVACY_CAMERA, 1); + if (res != PRIV_GUARD_ERROR_SUCCESS) printf("Failed to operate privacy_guard_client_update_monitor_policy() [%d]\n", res); + res = privacy_guard_client_add_privacy_access_log(USER_ID_OWNER, package_id, PRIVACY_CAMERA, access_time - i * 100); + if (res != PRIV_GUARD_ERROR_SUCCESS) printf("Failed to operate privacy_guard_client_add_privacy_access_log() [%d]\n", res); + // contacts + res = privacy_guard_client_update_monitor_policy(USER_ID_OWNER, package_id, PRIVACY_CONTACTS, 1); + if (res != PRIV_GUARD_ERROR_SUCCESS) printf("Failed to operate privacy_guard_client_update_monitor_policy() [%d]\n", res); + res = privacy_guard_client_add_privacy_access_log(USER_ID_OWNER, package_id, PRIVACY_CONTACTS, access_time - i * 100); + if (res != PRIV_GUARD_ERROR_SUCCESS) printf("Failed to operate privacy_guard_client_add_privacy_access_log() [%d]\n", res); + // sensor + res = privacy_guard_client_update_monitor_policy(USER_ID_OWNER, package_id, PRIVACY_SENSOR, 1); + if (res != PRIV_GUARD_ERROR_SUCCESS) printf("Failed to operate privacy_guard_client_update_monitor_policy() [%d]\n", res); + res = privacy_guard_client_add_privacy_access_log(USER_ID_OWNER, package_id, PRIVACY_SENSOR, access_time - i * 100); + if (res != PRIV_GUARD_ERROR_SUCCESS) printf("Failed to operate privacy_guard_client_add_privacy_access_log() [%d]\n", res); + // location + res = privacy_guard_client_update_monitor_policy(USER_ID_OWNER, package_id, PRIVACY_LOCATION, 1); + if (res != PRIV_GUARD_ERROR_SUCCESS) printf("Failed to operate privacy_guard_client_update_monitor_policy() [%d]\n", res); + res = privacy_guard_client_add_privacy_access_log(USER_ID_OWNER, package_id, PRIVACY_LOCATION, access_time - i * 100); + if (res != PRIV_GUARD_ERROR_SUCCESS) printf("Failed to operate privacy_guard_client_add_privacy_access_log() [%d]\n", res); + // message + res = privacy_guard_client_update_monitor_policy(USER_ID_OWNER, package_id, PRIVACY_MESSAGE, 1); + if (res != PRIV_GUARD_ERROR_SUCCESS) printf("Failed to operate privacy_guard_client_update_monitor_policy() [%d]\n", res); + res = privacy_guard_client_add_privacy_access_log(USER_ID_OWNER, package_id, PRIVACY_MESSAGE, access_time - i * 100); + if (res != PRIV_GUARD_ERROR_SUCCESS) printf("Failed to operate privacy_guard_client_add_privacy_access_log() [%d]\n", res); + // microphone + res = privacy_guard_client_update_monitor_policy(USER_ID_OWNER, package_id, PRIVACY_MICROPHONE, 1); + if (res != PRIV_GUARD_ERROR_SUCCESS) printf("Failed to operate privacy_guard_client_update_monitor_policy() [%d]\n", res); + res = privacy_guard_client_add_privacy_access_log(USER_ID_OWNER, package_id, PRIVACY_MICROPHONE, access_time - i * 100); + if (res != PRIV_GUARD_ERROR_SUCCESS) printf("Failed to operate privacy_guard_client_add_privacy_access_log() [%d]\n", res); + } + } + + return res; +} + +static void __test_preparation(void) +{ + fail_cnt = 0; + success_cnt = 0; + + all_package_list = NULL; + package_list_using_location = NULL; + package_list_using_account = NULL; + package_list_using_bookmark = NULL; + package_list_using_calendar = NULL; + package_list_using_call = NULL; + package_list_using_userhistory = NULL; + package_list_using_camera = NULL; + package_list_using_contacts = NULL; + package_list_using_message = NULL; + package_list_using_microphone = NULL; + package_list_using_sensor = NULL; + + __db_util_get_all_package_list_using_privacy(); + + __db_util_add_test_logs(); + + __db_util_get_package_list_using_privacy(PRIVACY_ACCOUNT); + __db_util_get_package_list_using_privacy(PRIVACY_LOCATION); + __db_util_get_package_list_using_privacy(PRIVACY_BOOKMARK); + __db_util_get_package_list_using_privacy(PRIVACY_CALENDAR); + __db_util_get_package_list_using_privacy(PRIVACY_CALL); + __db_util_get_package_list_using_privacy(PRIVACY_USERHISTORY); + __db_util_get_package_list_using_privacy(PRIVACY_CAMERA); + __db_util_get_package_list_using_privacy(PRIVACY_CONTACTS); + __db_util_get_package_list_using_privacy(PRIVACY_SENSOR); + __db_util_get_package_list_using_privacy(PRIVACY_MESSAGE); + __db_util_get_package_list_using_privacy(PRIVACY_MICROPHONE); +} + +static void __test_termination(void) +{ + fail_cnt = 0; + success_cnt = 0; + + g_list_free(all_package_list); + g_list_free(package_list_using_location); + g_list_free(package_list_using_account); + g_list_free(package_list_using_bookmark); + g_list_free(package_list_using_calendar); + g_list_free(package_list_using_call); + g_list_free(package_list_using_userhistory); + g_list_free(package_list_using_camera); + g_list_free(package_list_using_contacts); + g_list_free(package_list_using_message); + g_list_free(package_list_using_microphone); + g_list_free(package_list_using_sensor); +} + +/////////////////////////////////////////////////////////////////////////////// +// main testcases +/////////////////////////////////////////////////////////////////////////////// + +static void __test_privacy_guard_client_foreach_total_privacy_count_of_package_01(void) +{ + __start_test(__FUNCTION__); + + time_t start_date, end_date; + + end_date = time(NULL); + start_date = end_date - (24 * 60 * 60) * 7; // almost 7 days + + ////////////////////////////////////////////////////////////////////////// + // preparation - delete all logs + ////////////////////////////////////////////////////////////////////////// + int res = __db_util_delete_all_logs(); + __check_result_error_code(PRIV_GUARD_ERROR_SUCCESS, res, __LINE__); + + ////////////////////////////////////////////////////////////////////////// + // positive case + ////////////////////////////////////////////////////////////////////////// + res = privacy_guard_client_foreach_total_privacy_count_of_package(USER_ID_OWNER, start_date, end_date, __privacy_count_of_package_cb, (char*)__FUNCTION__); + __check_result_error_code(PRIV_GUARD_ERROR_SUCCESS, res, __LINE__); + + ////////////////////////////////////////////////////////////////////////// + // negative cases + ////////////////////////////////////////////////////////////////////////// + // invalid user id + res = privacy_guard_client_foreach_total_privacy_count_of_package(INVALID_USER_ID, start_date, end_date, __privacy_count_of_package_cb, (char*)__FUNCTION__); + __check_result_error_code(PRIV_GUARD_ERROR_INVALID_PARAMETER, res, __LINE__); + + // invalid start date + res = privacy_guard_client_foreach_total_privacy_count_of_package(USER_ID_OWNER, -1, end_date, __privacy_count_of_package_cb, (char*)__FUNCTION__); + __check_result_error_code(PRIV_GUARD_ERROR_INVALID_PARAMETER, res, __LINE__); + + // invalid end date + res = privacy_guard_client_foreach_total_privacy_count_of_package(USER_ID_OWNER, start_date, -1, __privacy_count_of_package_cb, (char*)__FUNCTION__); + __check_result_error_code(PRIV_GUARD_ERROR_INVALID_PARAMETER, res, __LINE__); + + __finish_test(); +} + +static void __test_privacy_guard_client_foreach_total_privacy_count_of_package_02(void) +{ + __start_test(__FUNCTION__); + + time_t start_date, end_date; + + end_date = time(NULL); + start_date = end_date - (24 * 60 * 60) * 7; // almost 7 days + + ////////////////////////////////////////////////////////////////////////// + // preparation - delete all logs + ////////////////////////////////////////////////////////////////////////// + int res = __db_util_delete_all_logs(); + __check_result_error_code(PRIV_GUARD_ERROR_SUCCESS, res, __LINE__); + + ////////////////////////////////////////////////////////////////////////// + // preparation - add logs to each package + ////////////////////////////////////////////////////////////////////////// + res = __db_util_add_test_logs(); + __check_result_error_code(PRIV_GUARD_ERROR_SUCCESS, res, __LINE__); + + ////////////////////////////////////////////////////////////////////////// + // positive case + ////////////////////////////////////////////////////////////////////////// + res = privacy_guard_client_foreach_total_privacy_count_of_package(USER_ID_OWNER, start_date, end_date, __privacy_count_of_package_cb, (char*)__FUNCTION__); + __check_result_error_code(PRIV_GUARD_ERROR_SUCCESS, res, __LINE__); + + __finish_test(); +} + +static void __test_privacy_guard_client_foreach_total_privacy_count_of_privacy_01(void) +{ + __start_test(__FUNCTION__); + + time_t start_date, end_date; + + end_date = time(NULL); + start_date = end_date - (24 * 60 * 60) * 7; // almost 7 days + + ////////////////////////////////////////////////////////////////////////// + // positive case + ////////////////////////////////////////////////////////////////////////// + int res = privacy_guard_client_foreach_total_privacy_count_of_privacy(USER_ID_OWNER, start_date, end_date, __privacy_count_cb, NULL); + __check_result_error_code(PRIV_GUARD_ERROR_SUCCESS, res, __LINE__); + + ////////////////////////////////////////////////////////////////////////// + // negative cases + ////////////////////////////////////////////////////////////////////////// + // invalid user id + res = privacy_guard_client_foreach_total_privacy_count_of_privacy(INVALID_USER_ID, start_date, end_date, __privacy_count_cb, NULL); + __check_result_error_code(PRIV_GUARD_ERROR_INVALID_PARAMETER, res, __LINE__); + + // invalid start date + res = privacy_guard_client_foreach_total_privacy_count_of_privacy(USER_ID_OWNER, -1, end_date, __privacy_count_cb, NULL); + __check_result_error_code(PRIV_GUARD_ERROR_INVALID_PARAMETER, res, __LINE__); + + // invalid end date + res = privacy_guard_client_foreach_total_privacy_count_of_privacy(USER_ID_OWNER, start_date, -1, __privacy_count_cb, NULL); + __check_result_error_code(PRIV_GUARD_ERROR_INVALID_PARAMETER, res, __LINE__); + + __finish_test(); +} + +static void __test_privacy_guard_client_foreach_total_privacy_count_of_privacy_02(void) +{ + __start_test(__FUNCTION__); + + time_t start_date, end_date; + + end_date = time(NULL); + start_date = end_date - (24 * 60 * 60) * 7; // almost 7 days + + ////////////////////////////////////////////////////////////////////////// + // preparation - delete all logs + ////////////////////////////////////////////////////////////////////////// + int res = __db_util_delete_all_logs(); + __check_result_error_code(PRIV_GUARD_ERROR_SUCCESS, res, __LINE__); + + ////////////////////////////////////////////////////////////////////////// + // preparation - add logs to each package + ////////////////////////////////////////////////////////////////////////// + res = __db_util_add_test_logs(); + __check_result_error_code(PRIV_GUARD_ERROR_SUCCESS, res, __LINE__); + + ////////////////////////////////////////////////////////////////////////// + // positive case + ////////////////////////////////////////////////////////////////////////// + res = privacy_guard_client_foreach_total_privacy_count_of_privacy(USER_ID_OWNER, start_date, end_date, __privacy_count_cb, (char*)__FUNCTION__); + __check_result_error_code(PRIV_GUARD_ERROR_SUCCESS, res, __LINE__); + + __finish_test(); +} + +static void __test_privacy_guard_client_foreach_privacy_count_by_privacy_id_01(void) +{ + __start_test(__FUNCTION__); + + time_t start_date, end_date; + char privacy_id[LEN_PRIVACY_ID] = "http://tizen.org/privacy/location"; + + end_date = time(NULL); + start_date = end_date - (24 * 60 * 60) * 7; // almost 7 days + + ////////////////////////////////////////////////////////////////////////// + // positive case + ////////////////////////////////////////////////////////////////////////// + int res = privacy_guard_client_foreach_privacy_count_by_privacy_id(USER_ID_OWNER, start_date, end_date, privacy_id, __privacy_count_of_package_cb, NULL); + __check_result_error_code(PRIV_GUARD_ERROR_SUCCESS, res, __LINE__); + + ////////////////////////////////////////////////////////////////////////// + // negative cases + ////////////////////////////////////////////////////////////////////////// + // invalid user id + res = privacy_guard_client_foreach_privacy_count_by_privacy_id(INVALID_USER_ID, start_date, end_date, privacy_id, __privacy_count_of_package_cb, NULL); + __check_result_error_code(PRIV_GUARD_ERROR_INVALID_PARAMETER, res, __LINE__); + + // invalid start date + res = privacy_guard_client_foreach_privacy_count_by_privacy_id(USER_ID_OWNER, -1, end_date, privacy_id, __privacy_count_of_package_cb, NULL); + __check_result_error_code(PRIV_GUARD_ERROR_INVALID_PARAMETER, res, __LINE__); + + // invalid end date + res = privacy_guard_client_foreach_privacy_count_by_privacy_id(USER_ID_OWNER, start_date, -1, privacy_id, __privacy_count_of_package_cb, NULL); + __check_result_error_code(PRIV_GUARD_ERROR_INVALID_PARAMETER, res, __LINE__); + + // invalid privacy id + res = privacy_guard_client_foreach_privacy_count_by_privacy_id(USER_ID_OWNER, start_date, -1, NULL, __privacy_count_of_package_cb, NULL); + __check_result_error_code(PRIV_GUARD_ERROR_INVALID_PARAMETER, res, __LINE__); + + __finish_test(); +} + +static void __test_privacy_guard_client_foreach_privacy_count_by_privacy_id_02(void) +{ + __start_test(__FUNCTION__); + + time_t start_date, end_date; + + end_date = time(NULL); + start_date = end_date - (24 * 60 * 60) * 7; // almost 7 days + + ////////////////////////////////////////////////////////////////////////// + // preparation - delete all logs + ////////////////////////////////////////////////////////////////////////// + int res = __db_util_delete_all_logs(); + __check_result_error_code(PRIV_GUARD_ERROR_SUCCESS, res, __LINE__); + + ////////////////////////////////////////////////////////////////////////// + // preparation - add logs to each package + ////////////////////////////////////////////////////////////////////////// + res = __db_util_add_test_logs(); + __check_result_error_code(PRIV_GUARD_ERROR_SUCCESS, res, __LINE__); + + ////////////////////////////////////////////////////////////////////////// + // positive case + ////////////////////////////////////////////////////////////////////////// + res = privacy_guard_client_foreach_privacy_count_by_privacy_id(USER_ID_OWNER, start_date, end_date, PRIVACY_ACCOUNT, __privacy_count_of_package_cb, (char*)__FUNCTION__); + __check_result_error_code(PRIV_GUARD_ERROR_SUCCESS, res, __LINE__); + + res = privacy_guard_client_foreach_privacy_count_by_privacy_id(USER_ID_OWNER, start_date, end_date, PRIVACY_BOOKMARK, __privacy_count_of_package_cb, (char*)__FUNCTION__); + __check_result_error_code(PRIV_GUARD_ERROR_SUCCESS, res, __LINE__); + + res = privacy_guard_client_foreach_privacy_count_by_privacy_id(USER_ID_OWNER, start_date, end_date, PRIVACY_CALENDAR, __privacy_count_of_package_cb, (char*)__FUNCTION__); + __check_result_error_code(PRIV_GUARD_ERROR_SUCCESS, res, __LINE__); + + res = privacy_guard_client_foreach_privacy_count_by_privacy_id(USER_ID_OWNER, start_date, end_date, PRIVACY_CALL, __privacy_count_of_package_cb, (char*)__FUNCTION__); + __check_result_error_code(PRIV_GUARD_ERROR_SUCCESS, res, __LINE__); + + res = privacy_guard_client_foreach_privacy_count_by_privacy_id(USER_ID_OWNER, start_date, end_date, PRIVACY_USERHISTORY, __privacy_count_of_package_cb, (char*)__FUNCTION__); + __check_result_error_code(PRIV_GUARD_ERROR_SUCCESS, res, __LINE__); + + res = privacy_guard_client_foreach_privacy_count_by_privacy_id(USER_ID_OWNER, start_date, end_date, PRIVACY_CAMERA, __privacy_count_of_package_cb, (char*)__FUNCTION__); + __check_result_error_code(PRIV_GUARD_ERROR_SUCCESS, res, __LINE__); + + res = privacy_guard_client_foreach_privacy_count_by_privacy_id(USER_ID_OWNER, start_date, end_date, PRIVACY_CONTACTS, __privacy_count_of_package_cb, (char*)__FUNCTION__); + __check_result_error_code(PRIV_GUARD_ERROR_SUCCESS, res, __LINE__); + + res = privacy_guard_client_foreach_privacy_count_by_privacy_id(USER_ID_OWNER, start_date, end_date, PRIVACY_SENSOR, __privacy_count_of_package_cb, (char*)__FUNCTION__); + __check_result_error_code(PRIV_GUARD_ERROR_SUCCESS, res, __LINE__); + + res = privacy_guard_client_foreach_privacy_count_by_privacy_id(USER_ID_OWNER, start_date, end_date, PRIVACY_LOCATION, __privacy_count_of_package_cb, (char*)__FUNCTION__); + __check_result_error_code(PRIV_GUARD_ERROR_SUCCESS, res, __LINE__); + + res = privacy_guard_client_foreach_privacy_count_by_privacy_id(USER_ID_OWNER, start_date, end_date, PRIVACY_MESSAGE, __privacy_count_of_package_cb, (char*)__FUNCTION__); + __check_result_error_code(PRIV_GUARD_ERROR_SUCCESS, res, __LINE__); + + res = privacy_guard_client_foreach_privacy_count_by_privacy_id(USER_ID_OWNER, start_date, end_date, PRIVACY_MICROPHONE, __privacy_count_of_package_cb, (char*)__FUNCTION__); + __check_result_error_code(PRIV_GUARD_ERROR_SUCCESS, res, __LINE__); + + __finish_test(); +} + +static void __test_privacy_guard_client_foreach_privacy_count_by_package_id_01(void) +{ + __start_test(__FUNCTION__); + + time_t start_date, end_date; + char *package_id; + + package_id = (char*)g_list_first(all_package_list)->data; + + end_date = time(NULL); + start_date = end_date - (24 * 60 * 60) * 7; // almost 7 days + + ////////////////////////////////////////////////////////////////////////// + // positive case + ////////////////////////////////////////////////////////////////////////// + int res = privacy_guard_client_foreach_privacy_count_by_package_id(USER_ID_OWNER, start_date, end_date, package_id, __privacy_count_of_package_cb, NULL); + __check_result_error_code(PRIV_GUARD_ERROR_SUCCESS, res, __LINE__); + + ////////////////////////////////////////////////////////////////////////// + // negative cases + ////////////////////////////////////////////////////////////////////////// + // invalid user id + res = privacy_guard_client_foreach_privacy_count_by_package_id(INVALID_USER_ID, start_date, end_date, package_id, __privacy_count_cb, NULL); + __check_result_error_code(PRIV_GUARD_ERROR_INVALID_PARAMETER, res, __LINE__); + + // invalid start date + res = privacy_guard_client_foreach_privacy_count_by_package_id(USER_ID_OWNER, -1, end_date, package_id, __privacy_count_cb, NULL); + __check_result_error_code(PRIV_GUARD_ERROR_INVALID_PARAMETER, res, __LINE__); + + // invalid end date + res = privacy_guard_client_foreach_privacy_count_by_package_id(USER_ID_OWNER, start_date, -1, package_id, __privacy_count_cb, NULL); + __check_result_error_code(PRIV_GUARD_ERROR_INVALID_PARAMETER, res, __LINE__); + + // invalid privacy id + res = privacy_guard_client_foreach_privacy_count_by_package_id(USER_ID_OWNER, start_date, end_date, NULL, __privacy_count_cb, NULL); + __check_result_error_code(PRIV_GUARD_ERROR_INVALID_PARAMETER, res, __LINE__); + + __finish_test(); +} + +static void __test_privacy_guard_client_foreach_privacy_count_by_package_id_02(void) +{ + __start_test(__FUNCTION__); + + time_t start_date, end_date; + int i; + + end_date = time(NULL); + start_date = end_date - (24 * 60 * 60) * 7; // almost 7 days + + ////////////////////////////////////////////////////////////////////////// + // preparation - delete all logs + ////////////////////////////////////////////////////////////////////////// + int res = __db_util_delete_all_logs(); + __check_result_error_code(PRIV_GUARD_ERROR_SUCCESS, res, __LINE__); + + ////////////////////////////////////////////////////////////////////////// + // preparation - add logs to each package + ////////////////////////////////////////////////////////////////////////// + res = __db_util_add_test_logs(); + __check_result_error_code(PRIV_GUARD_ERROR_SUCCESS, res, __LINE__); + + ////////////////////////////////////////////////////////////////////////// + // positive case + ////////////////////////////////////////////////////////////////////////// + for (i = 0; i < (int)g_list_length(all_package_list); i++) { + char *package_id = (char*)g_list_nth_data(all_package_list, i); + if (package_id) { + res = privacy_guard_client_foreach_privacy_count_by_package_id(USER_ID_OWNER, start_date, end_date, package_id, __privacy_count_of_package_cb, (char*)__FUNCTION__); + __check_result_error_code(PRIV_GUARD_ERROR_SUCCESS, res, __LINE__); + } + } + + __finish_test(); +} + +static void __test_privacy_guard_client_update_monitor_policy_01(void) +{ + __start_test(__FUNCTION__); + + int on = 1, off = 0; + char privacy_id[LEN_PRIVACY_ID] = "http://tizen.org/privacy/location"; + char *package_id; + + package_id = (char*)g_list_first(all_package_list)->data; + + ////////////////////////////////////////////////////////////////////////// + // positive case + ////////////////////////////////////////////////////////////////////////// + int res = privacy_guard_client_update_monitor_policy(USER_ID_OWNER, package_id, privacy_id, on); + __check_result_error_code(PRIV_GUARD_ERROR_SUCCESS, res, __LINE__); + + ////////////////////////////////////////////////////////////////////////// + // negative cases + ////////////////////////////////////////////////////////////////////////// + // invalid user id + res = privacy_guard_client_update_monitor_policy(INVALID_USER_ID, package_id, privacy_id, on); + __check_result_error_code(PRIV_GUARD_ERROR_INVALID_PARAMETER, res, __LINE__); + + // invalid package_id + res = privacy_guard_client_update_monitor_policy(USER_ID_OWNER, NULL, privacy_id, on); + __check_result_error_code(PRIV_GUARD_ERROR_INVALID_PARAMETER, res, __LINE__); + + // invalid privacy id + res = privacy_guard_client_update_monitor_policy(USER_ID_OWNER, package_id, NULL, off); + __check_result_error_code(PRIV_GUARD_ERROR_INVALID_PARAMETER, res, __LINE__); + + __finish_test(); +} + +static void __test_privacy_guard_client_update_monitor_policy_02(void) +{ + __start_test(__FUNCTION__); + + int off = 0; + int i; + TEST_DATA data; + + ////////////////////////////////////////////////////////////////////////// + // preparation - delete all logs + ////////////////////////////////////////////////////////////////////////// + int res = __db_util_delete_all_logs(); + __check_result_error_code(PRIV_GUARD_ERROR_SUCCESS, res, __LINE__); + + ////////////////////////////////////////////////////////////////////////// + // preparation - add logs to each package + ////////////////////////////////////////////////////////////////////////// + res = __db_util_add_test_logs(); + __check_result_error_code(PRIV_GUARD_ERROR_SUCCESS, res, __LINE__); + + ////////////////////////////////////////////////////////////////////////// + // positive case + ////////////////////////////////////////////////////////////////////////// + // LOCATION + for (i = 0; i < (int)g_list_length(package_list_using_location); i++) { + char *package_id = (char*)g_list_nth_data(package_list_using_location, i); + if (package_id) { + res = privacy_guard_client_update_monitor_policy(USER_ID_OWNER, package_id, PRIVACY_LOCATION, off); + __check_result_error_code(PRIV_GUARD_ERROR_SUCCESS, res, __LINE__); + memset(&data, 0x0, sizeof(TEST_DATA)); + strncpy(data.function_name, __FUNCTION__, strlen(__FUNCTION__)); + strncpy(data.privacy_id, PRIVACY_LOCATION, strlen(PRIVACY_LOCATION)); + strncpy(data.package_id, package_id, strlen(package_id)); + res = privacy_guard_client_foreach_monitor_policy_by_package_id(USER_ID_OWNER, package_id, __monitor_policy_cb, &data); + __check_result_error_code(PRIV_GUARD_ERROR_SUCCESS, res, __LINE__); + } + } + + // ACCOUNT + for (i = 0; i < (int)g_list_length(package_list_using_account); i++) { + char *package_id = (char*)g_list_nth_data(package_list_using_account, i); + if (package_id) { + res = privacy_guard_client_update_monitor_policy(USER_ID_OWNER, package_id, PRIVACY_ACCOUNT, off); + __check_result_error_code(PRIV_GUARD_ERROR_SUCCESS, res, __LINE__); + memset(&data, 0x0, sizeof(TEST_DATA)); + strncpy(data.function_name, __FUNCTION__, strlen(__FUNCTION__)); + strncpy(data.privacy_id, PRIVACY_ACCOUNT, strlen(PRIVACY_ACCOUNT)); + strncpy(data.package_id, package_id, strlen(package_id)); + res = privacy_guard_client_foreach_monitor_policy_by_package_id(USER_ID_OWNER, package_id, __monitor_policy_cb, &data); + __check_result_error_code(PRIV_GUARD_ERROR_SUCCESS, res, __LINE__); + } + } + + // BOOKMARK + for (i = 0; i < (int)g_list_length(package_list_using_bookmark); i++) { + char *package_id = (char*)g_list_nth_data(package_list_using_bookmark, i); + if (package_id) { + res = privacy_guard_client_update_monitor_policy(USER_ID_OWNER, package_id, PRIVACY_BOOKMARK, off); + __check_result_error_code(PRIV_GUARD_ERROR_SUCCESS, res, __LINE__); + memset(&data, 0x0, sizeof(TEST_DATA)); + strncpy(data.function_name, __FUNCTION__, strlen(__FUNCTION__)); + strncpy(data.privacy_id, PRIVACY_BOOKMARK, strlen(PRIVACY_BOOKMARK)); + strncpy(data.package_id, package_id, strlen(package_id)); + res = privacy_guard_client_foreach_monitor_policy_by_package_id(USER_ID_OWNER, package_id, __monitor_policy_cb, &data); + __check_result_error_code(PRIV_GUARD_ERROR_SUCCESS, res, __LINE__); + } + } + + // CALENDAR + for (i = 0; i < (int)g_list_length(package_list_using_calendar); i++) { + char *package_id = (char*)g_list_nth_data(package_list_using_calendar, i); + if (package_id) { + res = privacy_guard_client_update_monitor_policy(USER_ID_OWNER, package_id, PRIVACY_CALENDAR, off); + __check_result_error_code(PRIV_GUARD_ERROR_SUCCESS, res, __LINE__); + memset(&data, 0x0, sizeof(TEST_DATA)); + strncpy(data.function_name, __FUNCTION__, strlen(__FUNCTION__)); + strncpy(data.privacy_id, PRIVACY_CALENDAR, strlen(PRIVACY_CALENDAR)); + strncpy(data.package_id, package_id, strlen(package_id)); + res = privacy_guard_client_foreach_monitor_policy_by_package_id(USER_ID_OWNER, package_id, __monitor_policy_cb, &data); + __check_result_error_code(PRIV_GUARD_ERROR_SUCCESS, res, __LINE__); + } + } + + // CALL + for (i = 0; i < (int)g_list_length(package_list_using_call); i++) { + char *package_id = (char*)g_list_nth_data(package_list_using_call, i); + if (package_id) { + res = privacy_guard_client_update_monitor_policy(USER_ID_OWNER, package_id, PRIVACY_CALL, off); + __check_result_error_code(PRIV_GUARD_ERROR_SUCCESS, res, __LINE__); + memset(&data, 0x0, sizeof(TEST_DATA)); + strncpy(data.function_name, __FUNCTION__, strlen(__FUNCTION__)); + strncpy(data.privacy_id, PRIVACY_CALL, strlen(PRIVACY_CALL)); + strncpy(data.package_id, package_id, strlen(package_id)); + res = privacy_guard_client_foreach_monitor_policy_by_package_id(USER_ID_OWNER, package_id, __monitor_policy_cb, &data); + __check_result_error_code(PRIV_GUARD_ERROR_SUCCESS, res, __LINE__); + } + } + + // USERHISTORY + for (i = 0; i < (int)g_list_length(package_list_using_userhistory); i++) { + char *package_id = (char*)g_list_nth_data(package_list_using_userhistory, i); + if (package_id) { + res = privacy_guard_client_update_monitor_policy(USER_ID_OWNER, package_id, PRIVACY_USERHISTORY, off); + __check_result_error_code(PRIV_GUARD_ERROR_SUCCESS, res, __LINE__); + memset(&data, 0x0, sizeof(TEST_DATA)); + strncpy(data.function_name, __FUNCTION__, strlen(__FUNCTION__)); + strncpy(data.privacy_id, PRIVACY_USERHISTORY, strlen(PRIVACY_USERHISTORY)); + strncpy(data.package_id, package_id, strlen(package_id)); + res = privacy_guard_client_foreach_monitor_policy_by_package_id(USER_ID_OWNER, package_id, __monitor_policy_cb, &data); + __check_result_error_code(PRIV_GUARD_ERROR_SUCCESS, res, __LINE__); + } + } + + // CAMERA + for (i = 0; i < (int)g_list_length(package_list_using_camera); i++) { + char *package_id = (char*)g_list_nth_data(package_list_using_camera, i); + if (package_id) { + res = privacy_guard_client_update_monitor_policy(USER_ID_OWNER, package_id, PRIVACY_CAMERA, off); + __check_result_error_code(PRIV_GUARD_ERROR_SUCCESS, res, __LINE__); + memset(&data, 0x0, sizeof(TEST_DATA)); + strncpy(data.function_name, __FUNCTION__, strlen(__FUNCTION__)); + strncpy(data.privacy_id, PRIVACY_CAMERA, strlen(PRIVACY_CAMERA)); + strncpy(data.package_id, package_id, strlen(package_id)); + res = privacy_guard_client_foreach_monitor_policy_by_package_id(USER_ID_OWNER, package_id, __monitor_policy_cb, &data); + __check_result_error_code(PRIV_GUARD_ERROR_SUCCESS, res, __LINE__); + } + } + + // CONTACTS + for (i = 0; i < (int)g_list_length(package_list_using_contacts); i++) { + char *package_id = (char*)g_list_nth_data(package_list_using_contacts, i); + if (package_id) { + res = privacy_guard_client_update_monitor_policy(USER_ID_OWNER, package_id, PRIVACY_CONTACTS, off); + __check_result_error_code(PRIV_GUARD_ERROR_SUCCESS, res, __LINE__); + memset(&data, 0x0, sizeof(TEST_DATA)); + strncpy(data.function_name, __FUNCTION__, strlen(__FUNCTION__)); + strncpy(data.privacy_id, PRIVACY_CONTACTS, strlen(PRIVACY_CONTACTS)); + strncpy(data.package_id, package_id, strlen(package_id)); + res = privacy_guard_client_foreach_monitor_policy_by_package_id(USER_ID_OWNER, package_id, __monitor_policy_cb, &data); + __check_result_error_code(PRIV_GUARD_ERROR_SUCCESS, res, __LINE__); + } + } + + // MESSAGE + for (i = 0; i < (int)g_list_length(package_list_using_message); i++) { + char *package_id = (char*)g_list_nth_data(package_list_using_message, i); + if (package_id) { + res = privacy_guard_client_update_monitor_policy(USER_ID_OWNER, package_id, PRIVACY_MESSAGE, off); + __check_result_error_code(PRIV_GUARD_ERROR_SUCCESS, res, __LINE__); + memset(&data, 0x0, sizeof(TEST_DATA)); + strncpy(data.function_name, __FUNCTION__, strlen(__FUNCTION__)); + strncpy(data.privacy_id, PRIVACY_MESSAGE, strlen(PRIVACY_MESSAGE)); + strncpy(data.package_id, package_id, strlen(package_id)); + res = privacy_guard_client_foreach_monitor_policy_by_package_id(USER_ID_OWNER, package_id, __monitor_policy_cb, &data); + __check_result_error_code(PRIV_GUARD_ERROR_SUCCESS, res, __LINE__); + } + } + + // MICROPHONE + for (i = 0; i < (int)g_list_length(package_list_using_microphone); i++) { + char *package_id = (char*)g_list_nth_data(package_list_using_microphone, i); + if (package_id) { + res = privacy_guard_client_update_monitor_policy(USER_ID_OWNER, package_id, PRIVACY_MICROPHONE, off); + __check_result_error_code(PRIV_GUARD_ERROR_SUCCESS, res, __LINE__); + memset(&data, 0x0, sizeof(TEST_DATA)); + strncpy(data.function_name, __FUNCTION__, strlen(__FUNCTION__)); + strncpy(data.privacy_id, PRIVACY_MICROPHONE, strlen(PRIVACY_MICROPHONE)); + strncpy(data.package_id, package_id, strlen(package_id)); + res = privacy_guard_client_foreach_monitor_policy_by_package_id(USER_ID_OWNER, package_id, __monitor_policy_cb, &data); + __check_result_error_code(PRIV_GUARD_ERROR_SUCCESS, res, __LINE__); + } + } + + // SENSOR + for (i = 0; i < (int)g_list_length(package_list_using_sensor); i++) { + char *package_id = (char*)g_list_nth_data(package_list_using_sensor, i); + if (package_id) { + res = privacy_guard_client_update_monitor_policy(USER_ID_OWNER, package_id, PRIVACY_SENSOR, off); + __check_result_error_code(PRIV_GUARD_ERROR_SUCCESS, res, __LINE__); + memset(&data, 0x0, sizeof(TEST_DATA)); + strncpy(data.function_name, __FUNCTION__, strlen(__FUNCTION__)); + strncpy(data.privacy_id, PRIVACY_SENSOR, strlen(PRIVACY_SENSOR)); + strncpy(data.package_id, package_id, strlen(package_id)); + res = privacy_guard_client_foreach_monitor_policy_by_package_id(USER_ID_OWNER, package_id, __monitor_policy_cb, &data); + __check_result_error_code(PRIV_GUARD_ERROR_SUCCESS, res, __LINE__); + } + } + + __finish_test(); +} + +static void __test_privacy_guard_client_foreach_monitor_policy_by_package_id_01(void) +{ + __start_test(__FUNCTION__); + + char *package_id; + + package_id = (char*)g_list_first(all_package_list)->data; + + ////////////////////////////////////////////////////////////////////////// + // positive case + ////////////////////////////////////////////////////////////////////////// + int res = privacy_guard_client_foreach_monitor_policy_by_package_id(USER_ID_OWNER, package_id, __monitor_policy_cb, NULL); + __check_result_error_code(PRIV_GUARD_ERROR_SUCCESS, res, __LINE__); + + ////////////////////////////////////////////////////////////////////////// + // negative cases + ////////////////////////////////////////////////////////////////////////// + // invalid user id + res = privacy_guard_client_foreach_monitor_policy_by_package_id(INVALID_USER_ID, package_id, __monitor_policy_cb, NULL); + __check_result_error_code(PRIV_GUARD_ERROR_INVALID_PARAMETER, res, __LINE__); + + // invalid package id + res = privacy_guard_client_foreach_monitor_policy_by_package_id(USER_ID_OWNER, NULL, __monitor_policy_cb, NULL); + __check_result_error_code(PRIV_GUARD_ERROR_INVALID_PARAMETER, res, __LINE__); + + __finish_test(); +} + +static void __test_privacy_guard_client_foreach_monitor_policy_by_package_id_02(void) +{ + __start_test(__FUNCTION__); + + int i; + TEST_DATA data; + + ////////////////////////////////////////////////////////////////////////// + // preparation - delete all logs + ////////////////////////////////////////////////////////////////////////// + int res = __db_util_delete_all_logs(); + __check_result_error_code(PRIV_GUARD_ERROR_SUCCESS, res, __LINE__); + + ////////////////////////////////////////////////////////////////////////// + // preparation - add logs to each package + ////////////////////////////////////////////////////////////////////////// + res = __db_util_add_test_logs(); + __check_result_error_code(PRIV_GUARD_ERROR_SUCCESS, res, __LINE__); + + ////////////////////////////////////////////////////////////////////////// + // positive case + ////////////////////////////////////////////////////////////////////////// + // LOCATION + for (i = 0; i < (int)g_list_length(package_list_using_location); i++) { + char *package_id = (char*)g_list_nth_data(package_list_using_location, i); + if (package_id) { + memset(&data, 0x0, sizeof(TEST_DATA)); + strncpy(data.function_name, __FUNCTION__, strlen(__FUNCTION__)); + strncpy(data.privacy_id, PRIVACY_LOCATION, strlen(PRIVACY_LOCATION)); + strncpy(data.package_id, package_id, strlen(package_id)); + res = privacy_guard_client_foreach_monitor_policy_by_package_id(USER_ID_OWNER, package_id, __monitor_policy_cb, &data); + __check_result_error_code(PRIV_GUARD_ERROR_SUCCESS, res, __LINE__); + } + } + + // ACCOUNT + for (i = 0; i < (int)g_list_length(package_list_using_account); i++) { + char *package_id = (char*)g_list_nth_data(package_list_using_account, i); + if (package_id) { + memset(&data, 0x0, sizeof(TEST_DATA)); + strncpy(data.function_name, __FUNCTION__, strlen(__FUNCTION__)); + strncpy(data.privacy_id, PRIVACY_ACCOUNT, strlen(PRIVACY_ACCOUNT)); + strncpy(data.package_id, package_id, strlen(package_id)); + res = privacy_guard_client_foreach_monitor_policy_by_package_id(USER_ID_OWNER, package_id, __monitor_policy_cb, &data); + __check_result_error_code(PRIV_GUARD_ERROR_SUCCESS, res, __LINE__); + } + } + + // BOOKMARK + for (i = 0; i < (int)g_list_length(package_list_using_bookmark); i++) { + char *package_id = (char*)g_list_nth_data(package_list_using_bookmark, i); + if (package_id) { + memset(&data, 0x0, sizeof(TEST_DATA)); + strncpy(data.function_name, __FUNCTION__, strlen(__FUNCTION__)); + strncpy(data.privacy_id, PRIVACY_BOOKMARK, strlen(PRIVACY_BOOKMARK)); + strncpy(data.package_id, package_id, strlen(package_id)); + res = privacy_guard_client_foreach_monitor_policy_by_package_id(USER_ID_OWNER, package_id, __monitor_policy_cb, &data); + __check_result_error_code(PRIV_GUARD_ERROR_SUCCESS, res, __LINE__); + } + } + + // CALENDAR + for (i = 0; i < (int)g_list_length(package_list_using_calendar); i++) { + char *package_id = (char*)g_list_nth_data(package_list_using_calendar, i); + if (package_id) { + memset(&data, 0x0, sizeof(TEST_DATA)); + strncpy(data.function_name, __FUNCTION__, strlen(__FUNCTION__)); + strncpy(data.privacy_id, PRIVACY_CALENDAR, strlen(PRIVACY_CALENDAR)); + strncpy(data.package_id, package_id, strlen(package_id)); + res = privacy_guard_client_foreach_monitor_policy_by_package_id(USER_ID_OWNER, package_id, __monitor_policy_cb, &data); + __check_result_error_code(PRIV_GUARD_ERROR_SUCCESS, res, __LINE__); + } + } + + // CALL + for (i = 0; i < (int)g_list_length(package_list_using_call); i++) { + char *package_id = (char*)g_list_nth_data(package_list_using_call, i); + if (package_id) { + memset(&data, 0x0, sizeof(TEST_DATA)); + strncpy(data.function_name, __FUNCTION__, strlen(__FUNCTION__)); + strncpy(data.privacy_id, PRIVACY_CALL, strlen(PRIVACY_CALL)); + strncpy(data.package_id, package_id, strlen(package_id)); + res = privacy_guard_client_foreach_monitor_policy_by_package_id(USER_ID_OWNER, package_id, __monitor_policy_cb, &data); + __check_result_error_code(PRIV_GUARD_ERROR_SUCCESS, res, __LINE__); + } + } + + // USERHISTORY + for (i = 0; i < (int)g_list_length(package_list_using_userhistory); i++) { + char *package_id = (char*)g_list_nth_data(package_list_using_userhistory, i); + if (package_id) { + memset(&data, 0x0, sizeof(TEST_DATA)); + strncpy(data.function_name, __FUNCTION__, strlen(__FUNCTION__)); + strncpy(data.privacy_id, PRIVACY_USERHISTORY, strlen(PRIVACY_USERHISTORY)); + strncpy(data.package_id, package_id, strlen(package_id)); + res = privacy_guard_client_foreach_monitor_policy_by_package_id(USER_ID_OWNER, package_id, __monitor_policy_cb, &data); + __check_result_error_code(PRIV_GUARD_ERROR_SUCCESS, res, __LINE__); + } + } + + // CAMERA + for (i = 0; i < (int)g_list_length(package_list_using_camera); i++) { + char *package_id = (char*)g_list_nth_data(package_list_using_camera, i); + if (package_id) { + memset(&data, 0x0, sizeof(TEST_DATA)); + strncpy(data.function_name, __FUNCTION__, strlen(__FUNCTION__)); + strncpy(data.privacy_id, PRIVACY_CAMERA, strlen(PRIVACY_CAMERA)); + strncpy(data.package_id, package_id, strlen(package_id)); + res = privacy_guard_client_foreach_monitor_policy_by_package_id(USER_ID_OWNER, package_id, __monitor_policy_cb, &data); + __check_result_error_code(PRIV_GUARD_ERROR_SUCCESS, res, __LINE__); + } + } + + // CONTACTS + for (i = 0; i < (int)g_list_length(package_list_using_contacts); i++) { + char *package_id = (char*)g_list_nth_data(package_list_using_contacts, i); + if (package_id) { + memset(&data, 0x0, sizeof(TEST_DATA)); + strncpy(data.function_name, __FUNCTION__, strlen(__FUNCTION__)); + strncpy(data.privacy_id, PRIVACY_CONTACTS, strlen(PRIVACY_CONTACTS)); + strncpy(data.package_id, package_id, strlen(package_id)); + res = privacy_guard_client_foreach_monitor_policy_by_package_id(USER_ID_OWNER, package_id, __monitor_policy_cb, &data); + __check_result_error_code(PRIV_GUARD_ERROR_SUCCESS, res, __LINE__); + } + } + + // MESSAGE + for (i = 0; i < (int)g_list_length(package_list_using_message); i++) { + char *package_id = (char*)g_list_nth_data(package_list_using_message, i); + if (package_id) { + memset(&data, 0x0, sizeof(TEST_DATA)); + strncpy(data.function_name, __FUNCTION__, strlen(__FUNCTION__)); + strncpy(data.privacy_id, PRIVACY_MESSAGE, strlen(PRIVACY_MESSAGE)); + strncpy(data.package_id, package_id, strlen(package_id)); + res = privacy_guard_client_foreach_monitor_policy_by_package_id(USER_ID_OWNER, package_id, __monitor_policy_cb, &data); + __check_result_error_code(PRIV_GUARD_ERROR_SUCCESS, res, __LINE__); + } + } + + // MICROPHONE + for (i = 0; i < (int)g_list_length(package_list_using_microphone); i++) { + char *package_id = (char*)g_list_nth_data(package_list_using_microphone, i); + if (package_id) { + memset(&data, 0x0, sizeof(TEST_DATA)); + strncpy(data.function_name, __FUNCTION__, strlen(__FUNCTION__)); + strncpy(data.privacy_id, PRIVACY_MICROPHONE, strlen(PRIVACY_MICROPHONE)); + strncpy(data.package_id, package_id, strlen(package_id)); + res = privacy_guard_client_foreach_monitor_policy_by_package_id(USER_ID_OWNER, package_id, __monitor_policy_cb, &data); + __check_result_error_code(PRIV_GUARD_ERROR_SUCCESS, res, __LINE__); + } + } + + // SENSOR + for (i = 0; i < (int)g_list_length(package_list_using_sensor); i++) { + char *package_id = (char*)g_list_nth_data(package_list_using_sensor, i); + if (package_id) { + memset(&data, 0x0, sizeof(TEST_DATA)); + strncpy(data.function_name, __FUNCTION__, strlen(__FUNCTION__)); + strncpy(data.privacy_id, PRIVACY_SENSOR, strlen(PRIVACY_SENSOR)); + strncpy(data.package_id, package_id, strlen(package_id)); + res = privacy_guard_client_foreach_monitor_policy_by_package_id(USER_ID_OWNER, package_id, __monitor_policy_cb, &data); + __check_result_error_code(PRIV_GUARD_ERROR_SUCCESS, res, __LINE__); + } + } + + __finish_test(); + +} + +static void __test_privacy_guard_client_check_privacy_package_01(void) +{ + __start_test(__FUNCTION__); + + bool is_privacy_package = false; + char *package_id; + + package_id = (char*)g_list_first(all_package_list)->data; + + ////////////////////////////////////////////////////////////////////////// + // positive case + ////////////////////////////////////////////////////////////////////////// + int res = privacy_guard_client_check_privacy_package(USER_ID_OWNER, package_id, &is_privacy_package); + __check_result_error_code(PRIV_GUARD_ERROR_SUCCESS, res, __LINE__); + __check_result_bool(true, is_privacy_package, __LINE__); + + ////////////////////////////////////////////////////////////////////////// + // negative cases + ////////////////////////////////////////////////////////////////////////// + // invalid user id + res = privacy_guard_client_check_privacy_package(INVALID_USER_ID, package_id, &is_privacy_package); + __check_result_error_code(PRIV_GUARD_ERROR_INVALID_PARAMETER, res, __LINE__); + + // invalid package id + res = privacy_guard_client_check_privacy_package(USER_ID_OWNER, NULL, &is_privacy_package); + __check_result_error_code(PRIV_GUARD_ERROR_INVALID_PARAMETER, res, __LINE__); + + __finish_test(); +} + +static void __test_privacy_guard_client_check_privacy_package_02(void) +{ + __start_test(__FUNCTION__); + + int i; + bool is_privacy_package = false; + + ////////////////////////////////////////////////////////////////////////// + // preparation - delete all logs + ////////////////////////////////////////////////////////////////////////// + int res = __db_util_delete_all_logs(); + __check_result_error_code(PRIV_GUARD_ERROR_SUCCESS, res, __LINE__); + + ////////////////////////////////////////////////////////////////////////// + // preparation - add logs to each package + ////////////////////////////////////////////////////////////////////////// + res = __db_util_add_test_logs(); + __check_result_error_code(PRIV_GUARD_ERROR_SUCCESS, res, __LINE__); + + ////////////////////////////////////////////////////////////////////////// + // positive case + ////////////////////////////////////////////////////////////////////////// + // LOCATION + for (i = 0; i < (int)g_list_length(all_package_list); i++) { + char *package_id = (char*)g_list_nth_data(all_package_list, i); + if (package_id) { + res = privacy_guard_client_check_privacy_package(USER_ID_OWNER, package_id, &is_privacy_package); + __check_result_bool(true, is_privacy_package, __LINE__); + } + } + __finish_test(); +} + +static void __test_privacy_guard_client_foreach_privacy_package_id(void) +{ + __start_test(__FUNCTION__); + + ////////////////////////////////////////////////////////////////////////// + // positive case + ////////////////////////////////////////////////////////////////////////// + int res = privacy_guard_client_foreach_package_id_using_privacy(USER_ID_OWNER, __package_id_cb, (char*)__FUNCTION__); + __check_result_error_code(PRIV_GUARD_ERROR_SUCCESS, res, __LINE__); + + ////////////////////////////////////////////////////////////////////////// + // negative cases + ////////////////////////////////////////////////////////////////////////// + // invalid user id + res = privacy_guard_client_foreach_package_id_using_privacy(INVALID_USER_ID, __package_id_cb, NULL); + __check_result_error_code(PRIV_GUARD_ERROR_INVALID_PARAMETER, res, __LINE__); + + __finish_test(); +} + +static void __test_privacy_guard_client_foreach_package_info_by_privacy_id(void) +{ + __start_test(__FUNCTION__); + + ////////////////////////////////////////////////////////////////////////// + // preparation - delete all logs + ////////////////////////////////////////////////////////////////////////// + int res = __db_util_delete_all_logs(); + __check_result_error_code(PRIV_GUARD_ERROR_SUCCESS, res, __LINE__); + + ////////////////////////////////////////////////////////////////////////// + // preparation - add logs to each package + ////////////////////////////////////////////////////////////////////////// + res = __db_util_add_test_logs(); + __check_result_error_code(PRIV_GUARD_ERROR_SUCCESS, res, __LINE__); + + ////////////////////////////////////////////////////////////////////////// + // positive case + ////////////////////////////////////////////////////////////////////////// + res = privacy_guard_client_foreach_package_info_by_privacy_id(USER_ID_OWNER, PRIVACY_ACCOUNT, __package_info_cb, (char*)PRIVACY_ACCOUNT); + __check_result_error_code(PRIV_GUARD_ERROR_SUCCESS, res, __LINE__); + res = privacy_guard_client_foreach_package_info_by_privacy_id(USER_ID_OWNER, PRIVACY_BOOKMARK, __package_info_cb, (char*)PRIVACY_BOOKMARK); + __check_result_error_code(PRIV_GUARD_ERROR_SUCCESS, res, __LINE__); + res = privacy_guard_client_foreach_package_info_by_privacy_id(USER_ID_OWNER, PRIVACY_CALENDAR, __package_info_cb, (char*)PRIVACY_CALENDAR); + __check_result_error_code(PRIV_GUARD_ERROR_SUCCESS, res, __LINE__); + res = privacy_guard_client_foreach_package_info_by_privacy_id(USER_ID_OWNER, PRIVACY_CALL, __package_info_cb, (char*)PRIVACY_CALL); + __check_result_error_code(PRIV_GUARD_ERROR_SUCCESS, res, __LINE__); + res = privacy_guard_client_foreach_package_info_by_privacy_id(USER_ID_OWNER, PRIVACY_USERHISTORY, __package_info_cb, (char*)PRIVACY_USERHISTORY); + __check_result_error_code(PRIV_GUARD_ERROR_SUCCESS, res, __LINE__); + res = privacy_guard_client_foreach_package_info_by_privacy_id(USER_ID_OWNER, PRIVACY_CAMERA, __package_info_cb, (char*)PRIVACY_CAMERA); + __check_result_error_code(PRIV_GUARD_ERROR_SUCCESS, res, __LINE__); + res = privacy_guard_client_foreach_package_info_by_privacy_id(USER_ID_OWNER, PRIVACY_CONTACTS, __package_info_cb, (char*)PRIVACY_CONTACTS); + __check_result_error_code(PRIV_GUARD_ERROR_SUCCESS, res, __LINE__); + res = privacy_guard_client_foreach_package_info_by_privacy_id(USER_ID_OWNER, PRIVACY_SENSOR, __package_info_cb, (char*)PRIVACY_SENSOR); + __check_result_error_code(PRIV_GUARD_ERROR_SUCCESS, res, __LINE__); + res = privacy_guard_client_foreach_package_info_by_privacy_id(USER_ID_OWNER, PRIVACY_LOCATION, __package_info_cb, (char*)PRIVACY_LOCATION); + __check_result_error_code(PRIV_GUARD_ERROR_SUCCESS, res, __LINE__); + res = privacy_guard_client_foreach_package_info_by_privacy_id(USER_ID_OWNER, PRIVACY_MESSAGE, __package_info_cb, (char*)PRIVACY_MESSAGE); + __check_result_error_code(PRIV_GUARD_ERROR_SUCCESS, res, __LINE__); + res = privacy_guard_client_foreach_package_info_by_privacy_id(USER_ID_OWNER, PRIVACY_MICROPHONE, __package_info_cb, (char*)PRIVACY_MICROPHONE); + __check_result_error_code(PRIV_GUARD_ERROR_SUCCESS, res, __LINE__); + + + ////////////////////////////////////////////////////////////////////////// + // negative cases + ////////////////////////////////////////////////////////////////////////// + // invalid user id + res = privacy_guard_client_foreach_package_info_by_privacy_id(INVALID_USER_ID, PRIVACY_LOCATION, __package_info_cb, NULL); + __check_result_error_code(PRIV_GUARD_ERROR_INVALID_PARAMETER, res, __LINE__); + + // invalid privacy id + res = privacy_guard_client_foreach_package_info_by_privacy_id(USER_ID_OWNER, NULL, __package_info_cb, NULL); + __check_result_error_code(PRIV_GUARD_ERROR_INVALID_PARAMETER, res, __LINE__); + + __finish_test(); +} + +int main() +{ + __change_color_to_green(); + printf("Test Start\n"); + __change_color_to_origin(); + + __test_preparation(); + + ///////////////////////////////////////////////////////////////////////// + // test cases /////////////////////////////////////////////////////////// + ///////////////////////////////////////////////////////////////////////// + __test_privacy_guard_client_foreach_total_privacy_count_of_package_01(); + __test_privacy_guard_client_foreach_total_privacy_count_of_package_02(); + __test_privacy_guard_client_foreach_total_privacy_count_of_privacy_01(); + __test_privacy_guard_client_foreach_total_privacy_count_of_privacy_02(); + __test_privacy_guard_client_foreach_privacy_count_by_privacy_id_01(); + __test_privacy_guard_client_foreach_privacy_count_by_privacy_id_02(); + __test_privacy_guard_client_foreach_privacy_count_by_package_id_01(); + __test_privacy_guard_client_foreach_privacy_count_by_package_id_02(); + __test_privacy_guard_client_update_monitor_policy_01(); + __test_privacy_guard_client_update_monitor_policy_02(); + __test_privacy_guard_client_foreach_monitor_policy_by_package_id_01(); + __test_privacy_guard_client_foreach_monitor_policy_by_package_id_02(); + __test_privacy_guard_client_check_privacy_package_01(); + __test_privacy_guard_client_check_privacy_package_02(); + __test_privacy_guard_client_foreach_privacy_package_id(); + __test_privacy_guard_client_foreach_package_info_by_privacy_id(); + ////////////////////////////////////////////////////////////////////////// + + __change_color_to_green(); + printf("Test Complete\n"); + printf("success : %d, ", success_cnt); + __change_color_to_red(); + printf("fail : %d\n", fail_cnt); + __change_color_to_origin(); + + __test_termination(); + + return 0; +} + -- 2.7.4