From: Tomasz Swierczek Date: Fri, 22 May 2020 07:51:23 +0000 (+0200) Subject: Add smack-rules positive tests X-Git-Tag: submit/tizen/20200710.130420~3 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=03456f6edced7dd5fc74ab73020a7f7add9fbf00;p=platform%2Fcore%2Fsecurity%2Fsecurity-manager.git Add smack-rules positive tests The goal of this commit is to increase code coverage of unit-tests. Change-Id: I800695c7c31d192a46371b1c9138da9159f7f773 --- diff --git a/test/test_smack-rules.cpp b/test/test_smack-rules.cpp index d4b9fcad..642a8a27 100644 --- a/test/test_smack-rules.cpp +++ b/test/test_smack-rules.cpp @@ -20,37 +20,55 @@ * @version 1.0 */ +#include #include +#include +#include #include #include #include - +#include #include 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 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()