From 44cc36d78aaaf70efd2f313ceb3f84d8b86cbce8 Mon Sep 17 00:00:00 2001 From: Mateusz Cegielka Date: Tue, 7 Jul 2020 15:11:19 +0200 Subject: [PATCH] Test src/common/policy/ module Change-Id: Ib5879a39fadb52eaf02172e8af3e732368c6d265 --- test/unit-tests/CMakeLists.txt | 1 + test/unit-tests/test_common_policy.cpp | 244 +++++++++++++++++++++++++++++++++ 2 files changed, 245 insertions(+) create mode 100644 test/unit-tests/test_common_policy.cpp diff --git a/test/unit-tests/CMakeLists.txt b/test/unit-tests/CMakeLists.txt index 946c547..9c011dd 100644 --- a/test/unit-tests/CMakeLists.txt +++ b/test/unit-tests/CMakeLists.txt @@ -29,6 +29,7 @@ SET(TESTS_SOURCES ${ASKUSER_SRC_PATH}/common/policy/PrivilegePolicy.cpp ${ASKUSER_TESTS_PATH}/colour_log_formatter.cpp ${ASKUSER_TESTS_PATH}/main.cpp + ${ASKUSER_TESTS_PATH}/test_common_policy.cpp ) ADD_EXECUTABLE(${TARGET_ASKUSER_UNIT_TESTS} ${TESTS_SOURCES}) diff --git a/test/unit-tests/test_common_policy.cpp b/test/unit-tests/test_common_policy.cpp new file mode 100644 index 0000000..6975950 --- /dev/null +++ b/test/unit-tests/test_common_policy.cpp @@ -0,0 +1,244 @@ +/* + * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ + +#include +#include +#include +#include + +#include + +#include + +#include +#include +#include +#include +#include +#include + +#include "macros.h" + +namespace { + +const std::string APP = "org.tizen.camera-app"; +const std::string APP_TYPE = "capp"; +const std::string APP_API_VERSION = "4.0"; +const std::pair APP_RESPONSES[] = { + {"http://tizen.org/privilege/camera", "Allow"}, + {"http://tizen.org/privilege/mapservice", "Deny"}, +}; +const std::string PKG = "org.tizen.music-player"; +const std::string PKG_MAIN_APP = "org.tizen.music-player"; +const std::string PKG_LABEL = "Music"; +const std::string PKG_APPS[] = { + "org.tizen.music-player.widget", + "org.tizen.sound-player", + "org.tizen.music-chooser", + "org.tizen.music-player", +}; +const std::string PRIVACY = "http://tizen.org/privacy/location"; +const std::string PRIVACY_DISPLAY_NAME = "IDS_TPLATFORM_OPT_LOCATION_T_LBS"; +const std::string PRIVACY_PRIVILEGES[] = { + "http://tizen.org/privilege/location", + "http://tizen.org/privilege/location.coarse", +}; +const std::string PRIVILEGE = "http://tizen.org/privilege/externalstorage"; +const std::string PRIVILEGE_SAME_PRIVACY_PRIVILEGES[] = { + "http://tizen.org/privilege/externalstorage", + "http://tizen.org/privilege/mediastorage", +}; +const std::string NONEXISTENT_APP = "org.tizen.does-not-exist"; +const std::string NONEXISTENT_PRIVACY = "http://tizen.org/privacy/does-not-exist"; +const std::string NONEXISTENT_PRIVILEGE = "http://tizen.org/privilege/does-not-exist"; +const uid_t USER = 0; + +}; + +BOOST_AUTO_TEST_SUITE(TESTS_ASKUSER_COMMON_POLICY) + +POSITIVE_TEST_CASE(askuser_common_policy_entry_default) +{ + AskUser::PolicyEntry policy; + BOOST_REQUIRE(policy.getAppId() == SECURITY_MANAGER_ANY); + BOOST_REQUIRE(policy.getLevel().empty()); + BOOST_REQUIRE(policy.getPrivilege() == SECURITY_MANAGER_ANY); + BOOST_REQUIRE(policy.getUser() == std::to_string(geteuid())); +} + +POSITIVE_TEST_CASE(askuser_common_policy_entry_setters) +{ + const std::string LEVEL = "some-level"; + const std::string PRIVILEGE = "some-privilege"; + const std::string USER = "42"; + AskUser::PolicyEntry policy; + policy.setApp(APP); + policy.setLevel(LEVEL); + policy.setPrivilege(PRIVILEGE); + policy.setUser(USER); + BOOST_REQUIRE(policy.getAppId() == APP); + BOOST_REQUIRE(policy.getLevel() == LEVEL); + BOOST_REQUIRE(policy.getPrivilege() == PRIVILEGE); + BOOST_REQUIRE(policy.getUser() == USER); + AskUser::PolicyEntry policy2 = std::move(policy); + BOOST_REQUIRE(policy2.getAppId() == APP); + BOOST_REQUIRE(policy2.getLevel() == LEVEL); + BOOST_REQUIRE(policy2.getPrivilege() == PRIVILEGE); + BOOST_REQUIRE(policy2.getUser() == USER); + AskUser::PolicyEntryCopy copy{policy2.get()}; + BOOST_REQUIRE(copy.getAppId() == APP); + BOOST_REQUIRE(copy.getLevel() == LEVEL); + BOOST_REQUIRE(copy.getPrivilege() == PRIVILEGE); + BOOST_REQUIRE(copy.getUser() == USER); + AskUser::PolicyEntryCopy copy2 = std::move(copy); + BOOST_REQUIRE(copy2.getAppId() == APP); + BOOST_REQUIRE(copy2.getLevel() == LEVEL); + BOOST_REQUIRE(copy2.getPrivilege() == PRIVILEGE); + BOOST_REQUIRE(copy2.getUser() == USER); +} + +POSITIVE_TEST_CASE(askuser_common_appinfo) +{ + AskUser::PkgMgrAppInfo info; + BOOST_REQUIRE(info.type(APP, USER) == APP_TYPE); + BOOST_REQUIRE(info.apiVersion(APP, USER) == APP_API_VERSION); +} + +NEGATIVE_TEST_CASE(askuser_common_appinfo_app_does_not_exist) +{ + AskUser::PkgMgrAppInfo info; + BOOST_REQUIRE(info.type(NONEXISTENT_APP, USER).empty()); + BOOST_REQUIRE(info.apiVersion(NONEXISTENT_APP, USER).empty()); +} + +POSITIVE_TEST_CASE(askuser_common_pkginfo) +{ + AskUser::PkgMgrPkgInfo info; + BOOST_REQUIRE(info.mainAppId(PKG, USER) == PKG_MAIN_APP); + BOOST_REQUIRE(info.pkgLabel(PKG, USER) == PKG_LABEL); + std::vector appIds = AskUser::PkgMgrPkgInfo::listOfAppIds(PKG, USER); + BOOST_REQUIRE(std::is_permutation(appIds.begin(), appIds.end(), + std::begin(PKG_APPS), std::end(PKG_APPS))); +} + +NEGATIVE_TEST_CASE(askuser_common_pkginfo_pkg_does_not_exist) +{ + const std::string NONEXISTENT_PKG = "org.tizen.does-not-exist"; + AskUser::PkgMgrPkgInfo info; + BOOST_REQUIRE(info.mainAppId(NONEXISTENT_PKG, USER).empty()); + BOOST_REQUIRE(info.pkgLabel(NONEXISTENT_PKG, USER).empty()); + BOOST_REQUIRE(AskUser::PkgMgrPkgInfo::listOfAppIds(NONEXISTENT_PKG, USER).empty()); +} + +POSITIVE_TEST_CASE(askuser_common_privilege_policy) +{ + AskUser::PkgMgrAppInfo info; + for (auto& privilegePolicy : APP_RESPONSES) { + AskUser::PrivilegePolicy request(APP, privilegePolicy.first); + BOOST_REQUIRE(request.calculatePolicy(info) == privilegePolicy.second); + BOOST_REQUIRE(request.calculateAskablePrivacies(info).empty()); + } +} + +NEGATIVE_TEST_CASE(askuser_common_privilege_policy_privilege_does_not_exist) +{ + AskUser::PkgMgrAppInfo info; + AskUser::PrivilegePolicy req(APP, NONEXISTENT_PRIVILEGE); + BOOST_REQUIRE(req.calculatePolicy(info) == "Deny"); + BOOST_REQUIRE(req.calculateAskablePrivacies(info).empty()); +} + +NEGATIVE_TEST_CASE(askuser_common_privilege_policy_app_does_not_exist) +{ + AskUser::PkgMgrAppInfo info; + AskUser::PrivilegePolicy req(NONEXISTENT_APP, PRIVILEGE); + BOOST_REQUIRE(req.calculatePolicy(info) == "Deny"); + BOOST_REQUIRE(req.calculateAskablePrivacies(info).empty()); +} + +POSITIVE_TEST_CASE(askuser_common_privilege_same_privacy) +{ + std::vector others = + AskUser::PrivilegeInfo::getSamePrivacyPrivilegeMapping(PRIVILEGE); + BOOST_REQUIRE(std::is_permutation(others.begin(), others.end(), + std::begin(PRIVILEGE_SAME_PRIVACY_PRIVILEGES), + std::end(PRIVILEGE_SAME_PRIVACY_PRIVILEGES))); +} + +POSITIVE_TEST_CASE(askuser_common_privacy_display_name) +{ + BOOST_REQUIRE(AskUser::PrivilegeInfo::getPrivacyDisplayName(PRIVACY) == PRIVACY_DISPLAY_NAME); +} + +POSITIVE_TEST_CASE(askuser_common_privacy_privileges) +{ + std::vector privileges = AskUser::PrivilegeInfo::getPrivacyPrivileges(PRIVACY); + BOOST_REQUIRE(std::is_permutation(privileges.begin(), privileges.end(), + std::begin(PRIVACY_PRIVILEGES), std::end(PRIVACY_PRIVILEGES))); +} + +NEGATIVE_TEST_CASE(askuser_common_privilege_same_privacy_does_not_exist) +{ + BOOST_REQUIRE(AskUser::PrivilegeInfo::getSamePrivacyPrivilegeMapping(NONEXISTENT_PRIVILEGE) + .empty()); +} + +NEGATIVE_TEST_CASE(askuser_common_privacy_display_name_does_not_exist) +{ + BOOST_REQUIRE(AskUser::PrivilegeInfo::getPrivacyDisplayName(NONEXISTENT_PRIVACY) + == NONEXISTENT_PRIVACY); +} + +NEGATIVE_TEST_CASE(askuser_common_privacy_privileges_does_not_exist) +{ + std::vector ret = + AskUser::PrivilegeInfo::getPrivacyPrivileges(NONEXISTENT_PRIVACY); + BOOST_REQUIRE(ret.size() == 1); + BOOST_REQUIRE(ret[0] == NONEXISTENT_PRIVACY); +} + +NEGATIVE_TEST_CASE(askuser_common_get_own_app_id_no_app_context) +{ + AskUser::PkgMgrPkgInfo info; + BOOST_REQUIRE_THROW(AskUser::getOwnAppId(info), AskUser::Exception); +} + +NEGATIVE_TEST_CASE(askuser_common_policy_app_privs_does_not_exist) +{ + BOOST_REQUIRE(AskUser::getManifestPrivs(NONEXISTENT_APP).empty()); +} + +NEGATIVE_TEST_CASE(askuser_common_policy_app_identify_does_not_exist) +{ + const std::string NONEXISTENT_CYNARA_CLIENT = "does-not-exist"; + std::string buf1; + std::string buf2; + bool buf3; + AskUser::PkgMgrPkgInfo info; + BOOST_REQUIRE_THROW(AskUser::identifyApp(info, NONEXISTENT_CYNARA_CLIENT, buf1, buf2, buf3), + AskUser::Exception); +} + +NEGATIVE_TEST_CASE(askuser_common_policy_request_bad_change) +{ + AskUser::PolicyEntry entry; + AskUser::PolicyRequest req; + entry.setApp(APP); + req.addEntry(std::move(entry)); + BOOST_REQUIRE_THROW(req.updatePolicy(), AskUser::Exception); +} + +BOOST_AUTO_TEST_SUITE_END() -- 2.7.4