Tests for client license in db 44/128544/3
authorDariusz Michaluk <d.michaluk@samsung.com>
Wed, 10 May 2017 09:42:46 +0000 (11:42 +0200)
committerDariusz Michaluk <d.michaluk@samsung.com>
Fri, 12 May 2017 13:22:20 +0000 (15:22 +0200)
Change-Id: I8b19fa8d40fc7e34820ee6b758e46a546a964ebc

test/test_privilege_db_app_defined_privileges.cpp

index e111fa6..b1c4a5a 100644 (file)
@@ -34,6 +34,9 @@ namespace {
 struct AppDefinedPrivilegeFixture : public PrivilegeDBFixture {
     void checkAppDefinedPrivileges(const std::string &app, uid_t uid,
                                    const AppDefinedPrivilegesVector &expected);
+    void checkClientLicense(const std::string &app, uid_t uid,
+                            const std::vector<std::string> &privileges,
+                            const std::vector<std::string> &expected);
 };
 
 void AppDefinedPrivilegeFixture::checkAppDefinedPrivileges(const std::string &app, uid_t uid,
@@ -50,6 +53,19 @@ 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)
+{
+    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]);
+    }
+}
+
 } // anonymous namespace
 
 BOOST_FIXTURE_TEST_SUITE(PRIVILEGE_DB_TEST_APP_DEFINED_PRIVILEGES, AppDefinedPrivilegeFixture)
@@ -138,4 +154,87 @@ BOOST_AUTO_TEST_CASE(T1300_app_defined_privileges)
     checkAppDefinedPrivileges(app(2), uid(3), {});
 }
 
+BOOST_AUTO_TEST_CASE(T1400_client_license)
+{
+    // add some privileges/licenses
+    std::vector<std::pair<std::string, std::string>> privilegesA, privilegesB;
+    privilegesA.push_back(std::make_pair("org.tizen.first_app.gps",
+                                         "/opt/data/client_appA/res/first_app_client_license"));
+    privilegesA.push_back(std::make_pair("org.tizen.second_app.sso",
+                                         "/opt/data/client_appA/res/second_app_client_license"));
+    privilegesB.push_back(std::make_pair("org.tizen.first_app.gps",
+                                         "/opt/data/client_appB/res/first_app_client_license"));
+    privilegesB.push_back(std::make_pair("org.tizen.second_app.sso",
+                                         "/opt/data/client_appB/res/second_app_client_license"));
+
+    // non-existing application
+    checkClientLicense(app(1), uid(1), {privilegesA[0].first}, {""});
+
+    // 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}, {""});
+
+    // add privilege/license to non-existing application
+    BOOST_REQUIRE_THROW(testPrivDb->AddClientPrivilege(app(2), uid(1), privilegesA[0].first, privilegesA[0].second),
+                        PrivilegeDb::Exception::ConstraintError);
+
+    // first application use first privilege/license
+    BOOST_REQUIRE_NO_THROW(testPrivDb->AddClientPrivilege(app(1), uid(1), privilegesA[0].first, privilegesA[0].second));
+
+    // check non-existing privilege
+    std::string license;
+    BOOST_REQUIRE_NO_THROW(testPrivDb->GetLicenseForClientPrivilege(app(1), uid(1), privilegesA[1].first, license));
+    BOOST_REQUIRE(license.empty());
+
+    // first application use second privilege/license
+    BOOST_REQUIRE_NO_THROW(testPrivDb->AddClientPrivilege(app(1), uid(1), privilegesA[1].first, privilegesA[1].second));
+
+    // check existing privilege license
+    checkClientLicense(app(1), uid(1), {privilegesA[0].first, privilegesA[1].first},
+                       {privilegesA[0].second, 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}, {""});
+
+    // second application use first privilege/license
+    BOOST_REQUIRE_NO_THROW(testPrivDb->AddClientPrivilege(app(2), uid(2), privilegesB[0].first, privilegesB[0].second));
+
+    // check non-existing privilege
+    BOOST_REQUIRE_NO_THROW(testPrivDb->GetLicenseForClientPrivilege(app(2), uid(2), privilegesB[1].first, license));
+    BOOST_REQUIRE(license.empty());
+
+    // second application use second privilege/license
+    BOOST_REQUIRE_NO_THROW(testPrivDb->AddClientPrivilege(app(2), uid(2), privilegesB[1].first, privilegesB[1].second));
+
+    // check existing privilege/license
+    checkClientLicense(app(2), uid(2), {privilegesB[0].first, privilegesB[1].first},
+                       {privilegesB[0].second, 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},
+                       {"", ""});
+
+    // 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});
+
+    // uninstall second application and check privileges/licenses
+    removeAppSuccess(app(2), uid(2));
+    checkClientLicense(app(2), uid(2), {privilegesB[0].first, privilegesB[1].first},
+                       {"", ""});
+
+    removeAppSuccess(app(2), uid(3));
+    checkClientLicense(app(2), uid(3), {privilegesB[0].first, privilegesB[1].first},
+                       {"", ""});
+}
+
 BOOST_AUTO_TEST_SUITE_END()