Remove redundant author name from db 82/244982/3
authorKrzysztof Jackiewicz <k.jackiewicz@samsung.com>
Mon, 28 Sep 2020 12:01:51 +0000 (14:01 +0200)
committerDariusz Michaluk <d.michaluk@samsung.com>
Tue, 29 Sep 2020 11:15:22 +0000 (11:15 +0000)
Remove author's name from db as it's no longer needed. Make few minor changes
related to author.

Change-Id: I03f195298f6aa69d970f5d384b2ab441220f82e4

db/db.sql
db/updates/update-db-to-v15.sql [new file with mode: 0644]
src/common/include/privilege_db.h
src/common/privilege_db.cpp
src/common/service_impl.cpp
test/privilege_db_fixture.cpp
test/test_privilege_db_add_app.cpp
test/test_privilege_db_app_pkg_getters.cpp

index 2c4795ebe7341548b7e754f9d2c7a3860a3ab6f8..539b5394ea61cf153f204f24d87342d91f6775df 100644 (file)
--- a/db/db.sql
+++ b/db/db.sql
@@ -4,7 +4,7 @@ PRAGMA auto_vacuum = NONE;
 
 BEGIN EXCLUSIVE TRANSACTION;
 
-PRAGMA user_version = 14;
+PRAGMA user_version = 15;
 
 CREATE TABLE IF NOT EXISTS pkg (
 pkg_id INTEGER PRIMARY KEY,
@@ -58,7 +58,6 @@ PRIMARY KEY (privilege_name, group_name)
 
 CREATE TABLE IF NOT EXISTS author (
        author_id INTEGER PRIMARY KEY,
-       name VARCHAR NOT NULL,
        hash VARCHAR NOT NULL,
        UNIQUE (hash)
 );
@@ -96,7 +95,6 @@ SELECT
     app.version as version,
     pkg.author_id,
     pkg.name as pkg_name,
-    author.name as author_name,
     author.hash as author_hash,
     pkg.is_hybrid
 FROM user_app
@@ -116,19 +114,19 @@ BEGIN
     SELECT RAISE(ABORT, 'Another application from this package is already installed with different author')
         WHERE EXISTS (SELECT 1 FROM user_app_pkg_view
                       WHERE pkg_name=NEW.pkg_name
-                      AND author_name IS NOT NULL
-                      AND NEW.author_name IS NOT NULL
-                      AND author_name!=NEW.author_name);
+                      AND author_hash IS NOT NULL
+                      AND NEW.author_hash IS NOT NULL
+                      AND author_hash!=NEW.author_hash);
 
-    INSERT OR IGNORE INTO author(name, hash) VALUES (NEW.author_name, NEW.author_hash);
+    INSERT OR IGNORE INTO author(hash) VALUES (NEW.author_hash);
 
     INSERT OR IGNORE INTO pkg(name, author_id, is_hybrid) VALUES (
         NEW.pkg_name,
-        (SELECT author_id FROM author WHERE name=NEW.author_name),
+        (SELECT author_id FROM author WHERE hash=NEW.author_hash),
         NEW.is_hybrid);
 
     -- If pkg have already existed with empty author do update it
-    UPDATE pkg SET author_id=(SELECT author_id FROM author WHERE name=NEW.author_name)
+    UPDATE pkg SET author_id=(SELECT author_id FROM author WHERE hash=NEW.author_hash)
         WHERE name=NEW.pkg_name AND author_id IS NULL;
 
     -- If pkg have already existed with different hybrid flag do update it
diff --git a/db/updates/update-db-to-v15.sql b/db/updates/update-db-to-v15.sql
new file mode 100644 (file)
index 0000000..ef2bafa
--- /dev/null
@@ -0,0 +1,24 @@
+PRAGMA foreign_keys=OFF;
+
+BEGIN EXCLUSIVE TRANSACTION;
+
+PRAGMA user_version = 15;
+
+CREATE TABLE author_new (
+       author_id INTEGER PRIMARY KEY,
+       hash VARCHAR NOT NULL,
+       UNIQUE (hash)
+);
+
+INSERT INTO author_new
+SELECT author_id, hash
+FROM author;
+
+DROP TABLE author;
+ALTER TABLE author_new RENAME TO author;
+
+PRAGMA foreign_key_check;
+
+COMMIT TRANSACTION;
+
+PRAGMA foreign_keys=ON;
index 812bacfc57f7c9c603fbd0c730d2fc5134405de6..9478b869e17c01d7abfda5d4af803000b899e1e7 100644 (file)
@@ -460,7 +460,7 @@ public:
      */
     void GetAllPackages(std::vector<std::string> &packages);
 
-    /* Retrive hash of author_name from database
+    /* Retrieve hash of author_name from database
      *
      * @param pkgName[in] package identifier
      * @param authorHash[out] hash of author_name associated with the package, or empty string if no
@@ -468,7 +468,7 @@ public:
      * @exception PrivilegeDb::Exception::InternalError on internal error
      * @exception PrivilegeDb::Exception::ConstraintError on constraint violation
      */
-    void GetPkgAuthor(const std::string &pkgName, std::string &authorHash);
+    void GetPkgAuthorHash(const std::string &pkgName, std::string &authorHash);
 
     /**
      * Retrieve vector of pairs with group_name (1st value) and privilege_name (2nd value)
index 3fe93588a088ea8ab5eae90d0e953299952f2211..33ffbc0023593d59214d27cdf730d909be433952 100644 (file)
@@ -48,8 +48,8 @@ namespace SecurityManager {
 namespace {
 
 constexpr const char *g_queries[StmtTypeCount] = {
-    [underlying(StmtType::EAddApplication)] = "INSERT INTO user_app_pkg_view (app_name, pkg_name, uid, version, author_name, author_hash, is_hybrid)"
-                                              " VALUES (?, ?, ?, ?, ?, ?, ?)",
+    [underlying(StmtType::EAddApplication)] = "INSERT INTO user_app_pkg_view (app_name, pkg_name, uid, version, author_hash, is_hybrid)"
+                                              " VALUES (?, ?, ?, ?, ?, ?)",
     [underlying(StmtType::ERemoveApplication)] = "DELETE FROM user_app_pkg_view WHERE app_name=? AND uid=?",
     [underlying(StmtType::EPkgNameExists)] = "SELECT count(*) FROM pkg WHERE name=?",
     [underlying(StmtType::EAppNameExists)] = "SELECT count(*) FROM app WHERE name=?",
@@ -319,9 +319,8 @@ void PrivilegeDb::AddApplication(
         command->BindString(2, pkgName);
         command->BindInteger(3, static_cast<unsigned int>(uid));
         command->BindString(4, targetTizenVer);
-        authorName.empty() ? command->BindNull(5) : command->BindString(5, authorName);
-        authorName.empty() ? command->BindNull(6) : command->BindString(6, getAuthorHash(authorName));
-        command->BindInteger(7, isHybrid ? 1 : 0);
+        authorName.empty() ? command->BindNull(5) : command->BindString(5, getAuthorHash(authorName));
+        command->BindInteger(6, isHybrid ? 1 : 0);
 
         if (command->Step()) {
             LogDebug("Unexpected SQLITE_ROW answer to query: " <<
@@ -347,7 +346,7 @@ void PrivilegeDb::RemoveApplication(
         GetAppPkgName(appName, pkgName);
 
         std::string authorHash;
-        GetPkgAuthor(pkgName, authorHash);
+        GetPkgAuthorHash(pkgName, authorHash);
 
         auto command = getStatement(StmtType::ERemoveApplication);
         command->BindString(1, appName);
@@ -569,7 +568,7 @@ void PrivilegeDb::GetPkgApps(const std::string &pkgName,
     });
 }
 
-void PrivilegeDb::GetPkgAuthor(const std::string &pkgName, std::string &authorHash)
+void PrivilegeDb::GetPkgAuthorHash(const std::string &pkgName, std::string &authorHash)
 {
     try_catch<void>([&] {
         auto command = getStatement(StmtType::EGetPkgAuthor);
index 70bf9f9a7336741cde6deb2c10ed131ebc98e375..43de45fa82794ba50c229548655693d3afbe2692 100644 (file)
@@ -348,7 +348,7 @@ int ServiceImpl::labelPaths(const pkg_paths &paths,
         }
 
         std::string authorHash;
-        m_privilegeDb.GetPkgAuthor(pkgName, authorHash);
+        m_privilegeDb.GetPkgAuthorHash(pkgName, authorHash);
 
         std::string homePath;
         std::vector<std::string> pkgLegalBaseDirs;
@@ -530,7 +530,7 @@ int ServiceImpl::appInstallSmackRules(app_inst_req &req, InstallHelper &ih)
     Smack::Labels pkgLabels;
 
     try {
-        m_privilegeDb.GetPkgAuthor(req.pkgName, authorHash);
+        m_privilegeDb.GetPkgAuthorHash(req.pkgName, authorHash);
 
         // Check if hybridity is changed if the package is installed
         if (ih.isUserPkgInstalled && ih.isOldPkgHybrid != req.isHybrid) {
@@ -942,7 +942,7 @@ int ServiceImpl::appUninstall(const Credentials &creds, app_inst_req &req)
         // that this app belongs to, this will allow us to remove all rules within the
         // package that the app appears in
         UninstallHelper uh;
-        m_privilegeDb.GetPkgAuthor(req.pkgName, uh.authorHash);
+        m_privilegeDb.GetPkgAuthorHash(req.pkgName, uh.authorHash);
         getPkgLabels(req.pkgName, uh.pkgLabels);
         uh.isPkgHybrid = m_privilegeDb.IsPackageHybrid(req.pkgName);
 
@@ -1139,7 +1139,7 @@ void ServiceImpl::updateRunningAppSmackPolicy(
     SmackLabels::generateAppPkgNameFromLabel(appContext.appProcessLabel, appName, pkgName);
 
     std::string authorHash;
-    m_privilegeDb.GetPkgAuthor(pkgName, authorHash);
+    m_privilegeDb.GetPkgAuthorHash(pkgName, authorHash);
 
     m_smackRules.disablePrivilegeRules(appContext.appProcessLabel, pkgName, authorHash, denied);
     m_smackRules.enablePrivilegeRules(appContext.appProcessLabel, pkgName, authorHash, allowed);
@@ -2270,7 +2270,7 @@ int ServiceImpl::prepareApp(const Credentials &creds, const std::string &appName
     }
 
     std::string authorHash;
-    m_privilegeDb.GetPkgAuthor(pkgName, authorHash);
+    m_privilegeDb.GetPkgAuthorHash(pkgName, authorHash);
 
     std::vector<std::string> pkgLabels;
     getPkgLabels(pkgName, pkgLabels);
index 2f447fa07bed7231fcfe1b4c2b8e2f679fac8455..62557b736413f8f3e4376fce9c46e96d355e7cc5 100644 (file)
@@ -145,7 +145,7 @@ void PrivilegeDBFixture::addAppSuccess(const std::string &appName,
         "PkgNameExists wrongly not reported " << pkgName << " as existing package name");
 
     if (authorName.length() > 0) {
-        BOOST_REQUIRE_NO_THROW(testPrivDb->GetPkgAuthor(pkgName, authorHash));
+        BOOST_REQUIRE_NO_THROW(testPrivDb->GetPkgAuthorHash(pkgName, authorHash));
         BOOST_REQUIRE_MESSAGE(testPrivDb->AuthorExists(authorHash),
             "AuthorExists wrongly not reported " << uid << " as existing author");
     }
@@ -161,7 +161,7 @@ void PrivilegeDBFixture::addAppFail(const std::string &appName,
     std::string authorHash;
 
     if (authorName.length() > 0) {
-        BOOST_REQUIRE_NO_THROW(testPrivDb->GetPkgAuthor(pkgName, authorHash));
+        BOOST_REQUIRE_NO_THROW(testPrivDb->GetPkgAuthorHash(pkgName, authorHash));
         BOOST_REQUIRE_NO_THROW(authorNameExists = testPrivDb->AuthorExists(authorHash));
     }
 
@@ -177,7 +177,7 @@ void PrivilegeDBFixture::addAppFail(const std::string &appName,
         "PkgNameExists wrongly changed value after unsuccessful  installation.");
 
     if (authorName.length() > 0) {
-        BOOST_REQUIRE_NO_THROW(testPrivDb->GetPkgAuthor(pkgName, authorHash));
+        BOOST_REQUIRE_NO_THROW(testPrivDb->GetPkgAuthorHash(pkgName, authorHash));
         BOOST_REQUIRE_MESSAGE(authorNameExists == testPrivDb->AuthorExists(authorHash),
         "AuthorExists wrongly changed value after unsuccessful  installation.");
     }
index cd4965f0dd4e9733c1ac1d0b6ab7302745a18e95..63a6bdce54edaaa1c3f89c095ad6c3dc41e938e4 100644 (file)
@@ -167,16 +167,16 @@ POSITIVE_TEST_CASE(T590_add_applications_with_empty_noempty_author)
     std::string authorHash;
 
     addAppSuccess(app(1), pkg(1), uid(1), tizenVer(1), "", NotHybrid);
-    BOOST_REQUIRE_NO_THROW(getPrivDb()->GetPkgAuthor(pkg(1), authorHash));
+    BOOST_REQUIRE_NO_THROW(getPrivDb()->GetPkgAuthorHash(pkg(1), authorHash));
     BOOST_REQUIRE_MESSAGE(authorHash.empty(), "Wrong author returned: " << authorHash
         << " expected: empty");
 
     addAppSuccess(app(2), pkg(1), uid(1), tizenVer(1), author(1), NotHybrid);
-    BOOST_REQUIRE_NO_THROW(getPrivDb()->GetPkgAuthor(pkg(1), authorHash));
+    BOOST_REQUIRE_NO_THROW(getPrivDb()->GetPkgAuthorHash(pkg(1), authorHash));
     BOOST_REQUIRE_MESSAGE(!authorHash.empty(), "Wrong author returned: empty");
 
     addAppSuccess(app(3), pkg(1), uid(1), tizenVer(1), "", NotHybrid);
-    BOOST_REQUIRE_NO_THROW(getPrivDb()->GetPkgAuthor(pkg(2), authorHash));
+    BOOST_REQUIRE_NO_THROW(getPrivDb()->GetPkgAuthorHash(pkg(2), authorHash));
     BOOST_REQUIRE_MESSAGE(authorHash.empty(), "Wrong author returned: " << authorHash
         << " expected: empty");
 }
index 8a7c9596ef00685485f9357f5069b7934aa34a98..6e74735842f44b57b92f08accec8f747b9ad1aee 100644 (file)
@@ -90,7 +90,7 @@ void PrivilegeDBGettersFixture::checkGetPkgAuthor(const std::string &pkgName,
         const std::string &expectedAuthorHash)
 {
     std::string authorHash;
-    BOOST_REQUIRE_NO_THROW(getPrivDb()->GetPkgAuthor(pkgName, authorHash));
+    BOOST_REQUIRE_NO_THROW(getPrivDb()->GetPkgAuthorHash(pkgName, authorHash));
     BOOST_CHECK_MESSAGE(expectedAuthorHash == authorHash, "GetPkgAuthor for package: "
         << pkgName << " returned author: " << authorHash << " expected: " << expectedAuthorHash);
 };
@@ -184,7 +184,7 @@ POSITIVE_TEST_CASE(T325_app_name_pkg_author_exists)
         "AppNameExists wrongly not reported " << app(1) << " as existing application name");
     BOOST_REQUIRE_MESSAGE(getPrivDb()->PkgNameExists(pkg(1)),
         "PkgNameExists wrongly not reported " << pkg(1) << " as existing package name");
-    BOOST_REQUIRE_NO_THROW(getPrivDb()->GetPkgAuthor(pkg(1), authorHash));
+    BOOST_REQUIRE_NO_THROW(getPrivDb()->GetPkgAuthorHash(pkg(1), authorHash));
     BOOST_REQUIRE_MESSAGE(getPrivDb()->AuthorExists(authorHash),
         "AuthorExists wrongly not found " << author(1) << " as existing author");
 }