Add smack-rules positive tests 68/234168/17
authorTomasz Swierczek <t.swierczek@samsung.com>
Fri, 22 May 2020 07:51:23 +0000 (09:51 +0200)
committerTomasz Swierczek <t.swierczek@samsung.com>
Wed, 24 Jun 2020 12:19:20 +0000 (14:19 +0200)
The goal of this commit is to increase code coverage of unit-tests.

Change-Id: I800695c7c31d192a46371b1c9138da9159f7f773

test/test_smack-rules.cpp

index d4b9fcadf95109d92be86228e71a31d4f6d4f20d..642a8a27729d3a7836b7c0550c032ce29c48ac6e 100644 (file)
  * @version    1.0
  */
 
+#include <algorithm>
 #include <fstream>
+#include <string>
+#include <vector>
 
 #include <smack-accesses.h>
 #include <smack-rules.h>
 #include <smack-labels.h>
-
+#include <privilege_db.h>
 #include <testmacros.h>
 
 using namespace SecurityManager;
 using namespace SecurityManager::SmackLabels;
 
-struct RulesFixture
+struct DbFixture
 {
-    RulesFixture()
-    {
-        if (std::ifstream(templateRulesFilePath))
-            BOOST_REQUIRE_MESSAGE(unlink(templateRulesFilePath) == 0,
-                                  "Error while unlink the file: " << templateRulesFilePath);
+    DbFixture()
+        : db(PrivilegeDb::Offline::no) {
+            db.GetAllPackages(packages);
+        }
+
+    virtual ~DbFixture() {}
+
+    // this has to return pkgId string that
+    // * is NOT in the DB
+    // * each time the API is called, it should return different pkgID
+    // this way tests that need to have two or more pkgIds of not-installed apps
+    // can use this function
+    std::string getNextPkgId() const {
+        static int i = 0;
+        std::string pkgId;
+        do {
+            pkgId = "pkg" + std::to_string(i++);
+        } while (std::find(packages.begin(), packages.end(), pkgId) != packages.end());
+        return pkgId;
     }
 
-    ~RulesFixture()
-    {
-        if (std::ifstream(templateRulesFilePath))
-            BOOST_WARN_MESSAGE(unlink(templateRulesFilePath) == 0,
-                               "Error while unlink the file: " << templateRulesFilePath);
+    int getNextAuthorId() {
+        static int i = 0;
+        do {
+            ++i;
+        } while (db.AuthorIdExists(i));
+        return i;
     }
 
-    const static char* templateRulesFilePath;
+    PrivilegeDb db;
+    std::vector<std::string> packages;
 };
 
-const char* RulesFixture::templateRulesFilePath = "/tmp/SecurityManagerUTTemplateRules.rules";
 
 BOOST_AUTO_TEST_SUITE(SMACK_RULES_TEST)
 
@@ -226,4 +244,131 @@ NEGATIVE_ASCII_OBJECT(31)
 #undef NEGATIVE_ASCII_SUBJECT
 #undef NEGATIVE_ASCII_OBJECT
 
+POSITIVE_FIXTURE_TEST_CASE(T1300_smack_rules_class_install_app_rules, DbFixture)
+{
+    SmackRules rules;
+    std::string pkg = getNextPkgId();
+    std::string label1 = generateProcessLabel("app1", pkg, true);
+    std::string label2 = generateProcessLabel("app2", pkg, true);
+    int author = getNextAuthorId();
+    BOOST_REQUIRE_NO_THROW(rules.installApplicationRules(
+            label1, pkg, author,
+            {label1, label2}));
+    BOOST_REQUIRE_NO_THROW(rules.uninstallApplicationRules(
+        label1, pkg, author));
+}
+
+POSITIVE_TEST_CASE(T1301_smack_rules_class_check_is_mapping_enabled)
+{
+    SmackRules rules;
+    BOOST_REQUIRE_NO_THROW((void)rules.isPrivilegeMappingEnabled());
+}
+
+POSITIVE_FIXTURE_TEST_CASE(T1302_smack_rules_class_enable_disable_privileges, DbFixture)
+{
+    SmackRules rules;
+    std::string pkg = getNextPkgId();
+    std::string label = generateProcessLabel("app", pkg, false);
+    int author = getNextAuthorId();
+    BOOST_REQUIRE_NO_THROW(rules.enablePrivilegeRules(label, pkg, author, {}));
+    BOOST_REQUIRE_NO_THROW(rules.enablePrivilegeRules(label, pkg, author, {"http://tizen.org/privilege/dummy"}));
+    BOOST_REQUIRE_NO_THROW(rules.disableAllPrivilegeRules(label, pkg, author));
+}
+
+POSITIVE_FIXTURE_TEST_CASE(T1304_smack_rules_class_disable_specific_privilege_rules, DbFixture)
+{
+    SmackRules rules;
+    std::string pkg = getNextPkgId();
+    std::string label = generateProcessLabel("app", pkg, false);
+    int author = getNextAuthorId();
+    BOOST_REQUIRE_NO_THROW(rules.disablePrivilegeRules(
+        label, pkg, author, {"http://tizen.org/privilege/dummy"}));
+}
+
+POSITIVE_FIXTURE_TEST_CASE(T1305_smack_rules_class_uninstall_pkg_rules, DbFixture)
+{
+    SmackRules rules;
+    std::string pkg = getNextPkgId();
+    std::string label = generateProcessLabel("app", pkg, false);
+    BOOST_REQUIRE_NO_THROW(rules.uninstallPackageRules(pkg, {label}));
+}
+
+POSITIVE_FIXTURE_TEST_CASE(T1306_smack_rules_class_uninstall_app_rules, DbFixture)
+{
+    SmackRules rules;
+    std::string pkg = getNextPkgId();
+    std::string label = generateProcessLabel("app", pkg, false);
+    BOOST_REQUIRE_NO_THROW(rules.uninstallApplicationRules(label, pkg, 1));
+}
+
+POSITIVE_FIXTURE_TEST_CASE(T1307_smack_rules_class_update_pkg_rules, DbFixture)
+{
+    SmackRules rules;
+    std::string pkg = getNextPkgId();
+    std::string label1 = generateProcessLabel("app1", pkg, true);
+    std::string label2 = generateProcessLabel("app2", pkg, true);
+    BOOST_REQUIRE_NO_THROW(rules.updatePackageRules(pkg, {label1, label2}));
+    BOOST_REQUIRE_NO_THROW(rules.uninstallPackageRules(pkg, {label1, label2}));
+}
+
+POSITIVE_FIXTURE_TEST_CASE(T1308_smack_rules_class_uninstall_author_rules, DbFixture)
+{
+    SmackRules rules;
+    BOOST_REQUIRE_NO_THROW(rules.uninstallAuthorRules(getNextAuthorId()));
+}
+
+POSITIVE_FIXTURE_TEST_CASE(T1309_smack_rules_class_private_sharing, DbFixture)
+{
+    SmackRules rules;
+    std::string pkg1 = getNextPkgId();
+    std::string pkg2 = getNextPkgId();
+    std::string label1 = generateProcessLabel("app", pkg1, false);
+    std::string label2 = generateProcessLabel("app", pkg2, false);
+    BOOST_REQUIRE_NO_THROW(rules.applyPrivateSharingRules(
+        pkg1,
+        {label1},
+        label2,
+        "samplePathLabel",
+        false,
+        false));
+    BOOST_REQUIRE_NO_THROW(rules.dropPrivateSharingRules(
+        pkg1,
+        {label1},
+        label2,
+        "samplePathLabel",
+        true,
+        true));
+}
+
+POSITIVE_FIXTURE_TEST_CASE(T1310_smack_rules_class_templates, DbFixture)
+{
+    SmackRules rules;
+    SmackAccesses accesses;
+
+    std::string pkg = getNextPkgId();
+    std::string label = generateProcessLabel("app", pkg, false);
+    int author = getNextAuthorId();
+
+    BOOST_REQUIRE_NO_THROW(rules.addFromTemplate(
+        accesses,
+        TemplateManager::Type::APP_RULES_TEMPLATE,
+        label,
+        pkg,
+        author));
+
+    BOOST_REQUIRE_NO_THROW(rules.addFromPrivTemplate(
+        accesses,
+        TemplateManager::Type::PRIV_RULES_TEMPLATE,
+        "http://tizen.org/privilege/dummy",
+        label,
+        "aPrivilegeLabelDummy",
+        pkg,
+        author));
+
+    BOOST_REQUIRE_NO_THROW(rules.useTemplate(
+        TemplateManager::Type::APP_RULES_TEMPLATE,
+        label, pkg, 1));
+    BOOST_REQUIRE_NO_THROW(rules.disableAllPrivilegeRules(label, pkg, 1));
+}
+
 BOOST_AUTO_TEST_SUITE_END()