Extend privilegeDb api 62/129062/2
authorBartlomiej Grzelewski <b.grzelewski@samsung.com>
Fri, 12 May 2017 15:46:34 +0000 (17:46 +0200)
committerBartlomiej Grzelewski <b.grzelewski@samsung.com>
Fri, 12 May 2017 17:01:32 +0000 (19:01 +0200)
The function will not directly inform caller if row was found in
database. In previous implmentation functions may return empty
string if row was not found in database. It could be translated as row
contained empty string or no row was found.

Change-Id: Id44a5337e2ceb53b35be914962e442e4b5aeec0f

src/common/include/privilege_db.h
src/common/privilege_db.cpp
src/common/service_impl.cpp
test/test_privilege_db_app_defined_privileges.cpp

index 6d305fa86e285ec082dae7482932f37023c47b10..d4f4210d7527191fe42e454b4a45fff92e02e190 100644 (file)
@@ -631,9 +631,12 @@ public:
      *
      * @exception PrivilegeDb::Exception::InternalError on internal error
      * @exception PrivilegeDb::Exception::ConstraintError on constraint violation
+     * @return true if data were found in the database
      */
-    void GetAppAndLicenseForAppDefinedPrivilege(uid_t uid, const std::string &privilege,
-                                                std::string &appName, std::string &license);
+    bool GetAppAndLicenseForAppDefinedPrivilege(uid_t uid,
+                                                const std::string &privilege,
+                                                std::string &appName,
+                                                std::string &license);
 
     /**
      * Retrieve license of client application
@@ -645,8 +648,11 @@ public:
      *
      * @exception PrivilegeDb::Exception::InternalError on internal error
      * @exception PrivilegeDb::Exception::ConstraintError on constraint violation
+     * @return true if data were found in the database
      */
-    void GetLicenseForClientPrivilege(const std::string &appName, uid_t uid, const std::string &privilege,
+    bool GetLicenseForClientPrivilege(const std::string &appName,
+                                      uid_t uid,
+                                      const std::string &privilege,
                                       std::string &license);
 
     /**
index ad6f90d2e61a6b06cfea71a6f16e1d956faa3ebd..85972290361ddfbd6b82ac77571a34894e67d1ea 100644 (file)
@@ -687,10 +687,13 @@ void PrivilegeDb::GetAppDefinedPrivileges(const std::string &appName, uid_t uid,
     });
 }
 
-void PrivilegeDb::GetAppAndLicenseForAppDefinedPrivilege(uid_t uid, const std::string &privilege,
-                                                         std::string &appName, std::string &license)
+bool PrivilegeDb::GetAppAndLicenseForAppDefinedPrivilege(
+        uid_t uid,
+        const std::string &privilege,
+        std::string &appName,
+        std::string &license)
 {
-    try_catch<void>([&] {
+    return try_catch<bool>([&] {
         appName.clear();
         license.clear();
 
@@ -701,19 +704,22 @@ void PrivilegeDb::GetAppAndLicenseForAppDefinedPrivilege(uid_t uid, const std::s
         if (command->Step()) {
             appName = command->GetColumnString(0);
             license = command->GetColumnString(1);
+            LogDebug("Privilege: " << privilege << " defined by " << appName);
+            return true;
         }
 
-        if (!appName.empty())
-            LogDebug("Privilege: " << privilege << " defined by " << appName);
-        else
-            LogDebug("Privilege: " << privilege << " not exist");
+        LogDebug("Privilege: " << privilege << " not exist");
+        return false;
     });
 }
 
-void PrivilegeDb::GetLicenseForClientPrivilege(const std::string &appName, uid_t uid,
-                                               const std::string &privilege, std::string &license)
+bool PrivilegeDb::GetLicenseForClientPrivilege(
+        const std::string &appName,
+        uid_t uid,
+        const std::string &privilege,
+        std::string &license)
 {
-    try_catch<void>([&] {
+    return try_catch<bool>([&] {
         license.clear();
 
         auto command = getStatement(StmtType::EGetLicenseForClientPrivilege);
@@ -721,13 +727,16 @@ void PrivilegeDb::GetLicenseForClientPrivilege(const std::string &appName, uid_t
         command->BindInteger(2, uid);
         command->BindString(3, privilege);
 
-        if (command->Step())
+        if (command->Step()) {
             license = command->GetColumnString(0);
+            LogDebug("License found for app: " << appName << " privilege: " <<
+                privilege << " uid: " << uid << " License: " << license);
+            return true;
+        }
 
-        if (license.empty())
-            LogDebug("License not found for app: " << appName << " privilege: " << privilege << " uid: " << uid);
-        else
-            LogDebug("License found for app: " << appName << " privilege: " << privilege << " uid: " << uid << " License: " << license);
+        LogDebug("License not found for app: " << appName << " privilege: " <<
+            privilege << " uid: " << uid);
+        return false;
     });
 }
 
index a87ece403a34aafea5c048fee4029d6e218c48c3..ad21e9ad48643d1b74fcafc7ee6fe9695b5544cf 100644 (file)
@@ -1719,19 +1719,23 @@ int ServiceImpl::getAppDefinedPrivilegeProvider(uid_t uid, const std::string &pr
 {
     std::string appNameString, pkgNameString, licenseString;
     try {
-        m_privilegeDb.GetAppAndLicenseForAppDefinedPrivilege(uid, privilege, appNameString, licenseString);
-
-        // check if privilege is provided by globally installed application
-        if (appNameString.empty())
-            m_privilegeDb.GetAppAndLicenseForAppDefinedPrivilege(getGlobalUserId(), privilege, appNameString, licenseString);
+        // Get appName and License
+        if (!m_privilegeDb.GetAppAndLicenseForAppDefinedPrivilege(uid, privilege, appNameString, licenseString) &&
+            !m_privilegeDb.GetAppAndLicenseForAppDefinedPrivilege(getGlobalUserId(), privilege, appNameString, licenseString))
+        {
+            LogDebug("Privilege " << privilege << " not found in database");
+            return SECURITY_MANAGER_ERROR_NO_SUCH_OBJECT;
+        }
 
+        // Convert appName to pkgName
         m_privilegeDb.GetAppPkgName(appNameString, pkgNameString);
+
         if (appNameString.empty() || pkgNameString.empty()) {
-            LogWarning("Privilege " << privilege << " not found in database");
+            LogWarning("Could not translate appName to pkgName. appName: " << appName);
             return SECURITY_MANAGER_ERROR_NO_SUCH_OBJECT;
-        } else {
-            LogDebug("Privilege: " << privilege << " provided by app: " << appNameString << ", pkg: " << pkgNameString);
         }
+
+        LogDebug("Privilege: " << privilege << " provided by app: " << appNameString << " pkg: " << pkgNameString);
     } catch (const PrivilegeDb::Exception::Base &e) {
         LogError("Error while getting appName or pkgName from database: " << e.DumpToString());
         return SECURITY_MANAGER_ERROR_SERVER_ERROR;
@@ -1747,14 +1751,17 @@ int ServiceImpl::getAppDefinedPrivilegeLicense(uid_t uid, const std::string &pri
 {
     std::string appNameString, licenseString;
     try {
-        m_privilegeDb.GetAppAndLicenseForAppDefinedPrivilege(uid, privilege, appNameString, licenseString);
-
-        // check if privilege is provided by globally installed application
-        if (appNameString.empty())
-            m_privilegeDb.GetAppAndLicenseForAppDefinedPrivilege(getGlobalUserId(), privilege, appNameString, licenseString);
+        if (!m_privilegeDb.GetAppAndLicenseForAppDefinedPrivilege(uid, privilege, appNameString, licenseString) &&
+            !m_privilegeDb.GetAppAndLicenseForAppDefinedPrivilege(getGlobalUserId(), privilege, appNameString, licenseString))
+        {
+            LogDebug("Privilege " << privilege << " is not found in database");
+            return SECURITY_MANAGER_ERROR_NO_SUCH_OBJECT;
+        }
 
-        if (licenseString.empty())
+        if (licenseString.empty()) {
+            LogWarning("Empty license was found in database for privlege: " << privilege);
             return SECURITY_MANAGER_ERROR_NO_SUCH_OBJECT;
+        }
     } catch (const PrivilegeDb::Exception::Base &e) {
         LogError("Error while getting license from database: " << e.DumpToString());
         return SECURITY_MANAGER_ERROR_SERVER_ERROR;
@@ -1771,10 +1778,9 @@ int ServiceImpl::getClientPrivilegeLicense(const std::string &appName, uid_t uid
     try {
         uid_t requestUid = m_privilegeDb.IsUserAppInstalled(appName, uid) ? uid : getGlobalUserId();
 
-        m_privilegeDb.GetLicenseForClientPrivilege(appName, requestUid, privilege, licenseString);
-
-        if (licenseString.empty())
+        if (!m_privilegeDb.GetLicenseForClientPrivilege(appName, requestUid, privilege, licenseString)) {
             return SECURITY_MANAGER_ERROR_NO_SUCH_OBJECT;
+        }
     } catch (const PrivilegeDb::Exception::Base &e) {
         LogError("Error while getting license for app: " << e.DumpToString());
         return SECURITY_MANAGER_ERROR_SERVER_ERROR;
index 2f3853a5748fa520d01a3c61cc6dbe309e1256a4..04162f3dc55afe7d4370c6719878927ee2c96af1 100644 (file)
@@ -36,7 +36,7 @@ struct AppDefinedPrivilegeFixture : public PrivilegeDBFixture {
                                    const AppDefinedPrivilegesVector &expected);
     void checkClientLicense(const std::string &app, uid_t uid,
                             const std::vector<std::string> &privileges,
-                            const std::vector<std::string> &expected);
+                            const std::vector<std::pair<bool,std::string>> &expected);
 };
 
 void AppDefinedPrivilegeFixture::checkAppDefinedPrivileges(const std::string &app, uid_t uid,
@@ -55,14 +55,14 @@ void AppDefinedPrivilegeFixture::checkAppDefinedPrivileges(const std::string &ap
 
 void AppDefinedPrivilegeFixture::checkClientLicense(const std::string &app, uid_t uid,
                                                     const std::vector<std::string> &privileges,
-                                                    const std::vector<std::string> &expected)
+                                                    const std::vector<std::pair<bool,std::string>> &expected)
 {
     BOOST_REQUIRE_MESSAGE(privileges.size() == expected.size(), "Vector sizes differ");
 
     for (unsigned int i = 0; i < privileges.size(); ++i) {
         std::string license;
-        testPrivDb->GetLicenseForClientPrivilege(app, uid, privileges[i], license);
-        BOOST_REQUIRE(license == expected[i]);
+        BOOST_REQUIRE(expected[i].first == testPrivDb->GetLicenseForClientPrivilege(app, uid, privileges[i], license));
+        BOOST_REQUIRE(license == expected[i].second);
     }
 }
 
@@ -172,13 +172,13 @@ BOOST_AUTO_TEST_CASE(T1400_client_license)
                                          "/opt/data/client_appB/res/second_app_client_license"));
 
     // non-existing application
-    checkClientLicense(app(1), uid(1), {privilegesA[0].first}, {""});
+    checkClientLicense(app(1), uid(1), {privilegesA[0].first}, {{false,""}});
 
     // add application
     addAppSuccess(app(1), pkg(1), uid(1), tizenVer(1), author(1), Hybrid);
 
     // privileges/licenses not used
-    checkClientLicense(app(1), uid(1), {privilegesA[0].first}, {""});
+    checkClientLicense(app(1), uid(1), {privilegesA[0].first}, {{false,""}});
 
     // add privilege/license to non-existing application
     BOOST_REQUIRE_THROW(testPrivDb->AddClientPrivilege(app(2), uid(1), privilegesA[0].first, privilegesA[0].second),
@@ -201,13 +201,13 @@ BOOST_AUTO_TEST_CASE(T1400_client_license)
 
     // check existing privilege license
     checkClientLicense(app(1), uid(1), {privilegesA[0].first, privilegesA[1].first},
-                       {privilegesA[0].second, privilegesA[1].second});
+                       {{true, privilegesA[0].second}, {true, privilegesA[1].second}});
 
     // add second application
     addAppSuccess(app(2), pkg(2), uid(2), tizenVer(1), author(2), Hybrid);
 
     // privileges/licenses not used
-    checkClientLicense(app(2), uid(2), {privilegesA[0].first}, {""});
+    checkClientLicense(app(2), uid(2), {privilegesA[0].first}, {{false,""}});
 
     // second application use first privilege/license
     BOOST_REQUIRE_NO_THROW(testPrivDb->AddClientPrivilege(app(2), uid(2), privilegesB[0].first, privilegesB[0].second));
@@ -221,28 +221,28 @@ BOOST_AUTO_TEST_CASE(T1400_client_license)
 
     // check existing privilege/license
     checkClientLicense(app(2), uid(2), {privilegesB[0].first, privilegesB[1].first},
-                       {privilegesB[0].second, privilegesB[1].second});
+                       {{true, privilegesB[0].second}, {true, privilegesB[1].second}});
 
     // remove first application privileges/licenses
     BOOST_REQUIRE_NO_THROW(testPrivDb->RemoveClientPrivileges(app(1), uid(1)));
     checkClientLicense(app(1), uid(1), {privilegesA[0].first, privilegesA[1].first},
-                       {"", ""});
+                       {{false, ""},{false, ""}});
 
     // install second application for different user and add privileges
     addAppSuccess(app(2), pkg(2), uid(3), tizenVer(1), author(2), Hybrid);
     BOOST_REQUIRE_NO_THROW(testPrivDb->AddClientPrivilege(app(2), uid(3), privilegesB[0].first, privilegesB[0].second));
     BOOST_REQUIRE_NO_THROW(testPrivDb->AddClientPrivilege(app(2), uid(3), privilegesB[1].first, privilegesB[1].second));
     checkClientLicense(app(2), uid(3), {privilegesB[0].first, privilegesB[1].first},
-                       {privilegesB[0].second, privilegesB[1].second});
+                       {{true,privilegesB[0].second},{true, privilegesB[1].second}});
 
     // uninstall second application and check privileges/licenses
     removeAppSuccess(app(2), uid(2));
     checkClientLicense(app(2), uid(2), {privilegesB[0].first, privilegesB[1].first},
-                       {"", ""});
+                       {{false,""},{false, ""}});
 
     removeAppSuccess(app(2), uid(3));
     checkClientLicense(app(2), uid(3), {privilegesB[0].first, privilegesB[1].first},
-                       {"", ""});
+                       {{false,""},{false, ""}});
 }
 
 BOOST_AUTO_TEST_SUITE_END()