Fix app defined privileges issues 06/324906/4
authorKrzysztof Jackiewicz <k.jackiewicz@samsung.com>
Wed, 28 May 2025 11:02:48 +0000 (13:02 +0200)
committerKrzysztof Jackiewicz <k.jackiewicz@samsung.com>
Wed, 28 May 2025 12:41:19 +0000 (14:41 +0200)
* Add unique constraint to prevent adding the privilege twice
* Remove view operations from migration script. View may be absent when
  migrating from older db version. It will be created by executing
  sql.db after the migration
* Fix app_private_sharing_view_insert_trigger which was using a
  non-exsitent query parameter (NEW.pkg_name)
* Update unit tests to cover few more cases, including insert trigger
  abort

Change-Id: Ieca89a6dfc75436dab37583a30bf72e3d5494fcf

db/db.sql
db/updates/update-db-to-v17.sql
test/test_privilege_db_app_defined_privileges.cpp

index 2a5c47c1abfce9e325d8c9ed05834650294574c5..9b601f2a035c6f51eacaf0f1f1654efa6f60c390 100644 (file)
--- a/db/db.sql
+++ b/db/db.sql
@@ -68,6 +68,7 @@ uid INTEGER NOT NULL,
 privilege VARCHAR NOT NULL,
 type INTEGER NOT NULL CHECK (type >= 0 AND type <= 1),
 license VARCHAR,
+UNIQUE (uid, privilege, app_id),
 FOREIGN KEY (app_id, uid) REFERENCES user_app (app_id, uid) ON UPDATE CASCADE ON DELETE CASCADE
 );
 
@@ -242,7 +243,9 @@ INSTEAD OF INSERT ON app_defined_privilege_view
 BEGIN
     SELECT RAISE(ABORT, 'App defined privilege already defined by different package')
     WHERE EXISTS (SELECT 1 FROM app_defined_privilege_view
-                  WHERE privilege=NEW.privilege AND pkg_name!=NEW.pkg_name AND uid=NEW.uid);
+                  WHERE privilege=NEW.privilege
+                  AND pkg_name!=(SELECT pkg_name FROM user_app_pkg_view WHERE app_name=NEW.app_name)
+                  AND uid=NEW.uid);
 
     INSERT INTO app_defined_privilege (app_id, uid, privilege, type, license)
     VALUES ((SELECT app_id FROM app WHERE name=NEW.app_name), NEW.uid, NEW.privilege, NEW.type, NEW.license);
index fdb1d1a54f6d487701e277311df83471411271ff..94367f7f7344c5dabb617c2c09af03aaa08bd083 100644 (file)
@@ -10,6 +10,7 @@ uid INTEGER NOT NULL,
 privilege VARCHAR NOT NULL,
 type INTEGER NOT NULL CHECK (type >= 0 AND type <= 1),
 license VARCHAR,
+UNIQUE (uid, privilege, app_id),
 FOREIGN KEY (app_id, uid) REFERENCES user_app (app_id, uid) ON UPDATE CASCADE ON DELETE CASCADE
 );
 
@@ -20,18 +21,6 @@ FROM app_defined_privilege;
 DROP TABLE app_defined_privilege;
 ALTER TABLE app_defined_privilege_new RENAME TO app_defined_privilege;
 
-DROP TRIGGER IF EXISTS app_defined_privilege_view_insert_trigger;
-CREATE TRIGGER app_defined_privilege_view_insert_trigger
-INSTEAD OF INSERT ON app_defined_privilege_view
-BEGIN
-    SELECT RAISE(ABORT, 'App defined privilege already defined by different package')
-    WHERE EXISTS (SELECT 1 FROM app_defined_privilege_view
-                  WHERE privilege=NEW.privilege AND pkg_name!=NEW.pkg_name AND uid=NEW.uid);
-
-    INSERT INTO app_defined_privilege (app_id, uid, privilege, type, license)
-    VALUES ((SELECT app_id FROM app WHERE name=NEW.app_name), NEW.uid, NEW.privilege, NEW.type, NEW.license);
-END;
-
 PRAGMA foreign_key_check;
 
 COMMIT TRANSACTION;
index 94e2b1e6d2993bb1b605ef55a42ee2ae15d5bbbb..d243f7e2015f1dd597ec5534ec5a9d45a86c677e 100644 (file)
@@ -135,9 +135,24 @@ NEGATIVE_TEST_CASE(T1300_app_defined_privileges)
     addAppSuccess(app(2), pkg(2), uid(2), tizenVer(1), author(2), Hybrid);
 
     // privilege already defined by first application
-    BOOST_REQUIRE_THROW(testPrivDb->AddAppDefinedPrivilege(app(2), uid(2), privileges[0]),
+    BOOST_REQUIRE_NO_THROW(testPrivDb->AddAppDefinedPrivilege(app(2), uid(2), privileges[0]));
+
+    // add third application in the same package as 1st
+    addAppSuccess(app(3), pkg(1), uid(1), tizenVer(1), author(1), Hybrid);
+
+    // privilege already defined by another application in the same package and uid
+    BOOST_REQUIRE_NO_THROW(testPrivDb->AddAppDefinedPrivilege(app(3), uid(1), privileges[0]));
+
+    // add fourth application in a different package than 1st but same uid
+    addAppSuccess(app(4), pkg(2), uid(1), tizenVer(1), "", Hybrid);
+
+    // privilege already defined by an app from another package, same uid
+    BOOST_REQUIRE_THROW(testPrivDb->AddAppDefinedPrivilege(app(4), uid(1), privileges[0]),
                         PrivilegeDb::Exception::ConstraintError);
 
+    // uninstall fourth application
+    removeAppSuccess(app(4), uid(1));
+
     // remove first application privileges
     BOOST_REQUIRE_NO_THROW(testPrivDb->RemoveAppDefinedPrivileges(app(1), uid(1)));
     checkAppDefinedPrivileges(app(1), uid(1), {});
@@ -146,8 +161,11 @@ NEGATIVE_TEST_CASE(T1300_app_defined_privileges)
     removeAppSuccess(app(1), uid(1));
     checkAppDefinedPrivileges(app(1), uid(1), {});
 
+    // uninstall third application
+    removeAppSuccess(app(3), uid(1));
+
     // second application defines privileges
-    BOOST_REQUIRE_NO_THROW(testPrivDb->AddAppDefinedPrivileges(app(2), uid(2), privileges));
+    BOOST_REQUIRE_NO_THROW(testPrivDb->AddAppDefinedPrivilege(app(2), uid(2), privileges[1]));
     checkAppDefinedPrivileges(app(2), uid(2), privileges);
 
     // install second application for different user and add privileges