--- /dev/null
+/*
+ * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Contact: Tomasz Swierczek <t.swierczek@samsung.com>
+ *
+ * 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
+ *
+ *
+ * @file test_service_impl_utils.cpp
+ * @author Tomasz Swierczek (t.swierczek@samsung.com)
+ */
+
+#include <string>
+#include <sys/types.h>
+#include <pwd.h>
+#include <unistd.h>
+#include <utility>
+#include <vector>
+
+#include <dpl/errno_string.h>
+#include <dpl/log/log.h>
+#include <security-manager-types.h>
+#include <service_impl_utils.h>
+#include <protocols.h>
+#include <tzplatform-config.h>
+#include <testmacros.h>
+
+using namespace SecurityManager;
+
+struct ServiceImplUtilFixture
+{
+
+ ServiceImplUtilFixture()
+ {
+ FILE *f;
+ f = fopen(existingFile.c_str(), "w");
+ int er = errno;
+ if (f != NULL)
+ fclose(f);
+ else
+ LogError("Failed to open file " << existingFile << " : " << GetErrnoString(er));
+ uid_t i = 0;
+ while(getpwuid(++i));
+ nonexistingUid = i;
+ }
+
+ ~ServiceImplUtilFixture()
+ {
+ (void)remove(existingFile.c_str());
+ }
+ static inline uid_t nonexistingUid = 0;
+ const static inline std::string existingFile = "/tmp/file";
+ const static inline std::string enoentPath = "/just/a/strange/path";
+ const static inline std::string aPath = "/usr/bin/../.././tmp/../tmp/./file";
+ const static inline std::string allowedPath1 = "/opt/usr";
+ const static inline std::string allowedPath2 = "/home";
+ const static inline std::string requestedPath1 = "/opt/usr/apps";
+ const static inline std::string requestedPath2 = "/home/owner/../owner";
+ const static inline std::string invalidRequestedPath = "/tmp/aPkgId";
+};
+
+BOOST_AUTO_TEST_SUITE(SERVICE_IMPL_UTILS_TEST)
+
+POSITIVE_TEST_CASE(T260_isSubDir)
+{
+ pkg_paths paths1 = { {"/usr/local/bin", 0} };
+ BOOST_REQUIRE(containSubDir("/usr/local", paths1));
+ pkg_paths paths2 = { {"/usr/local", 0} };
+ BOOST_REQUIRE(!containSubDir("/usr/local/bin/", paths2));
+}
+
+POSITIVE_FIXTURE_TEST_CASE(T261_realpath, ServiceImplUtilFixture)
+{
+ BOOST_REQUIRE(realPath(ServiceImplUtilFixture::existingFile) == ServiceImplUtilFixture::existingFile);
+ BOOST_REQUIRE(realPath(ServiceImplUtilFixture::aPath) == ServiceImplUtilFixture::existingFile);
+}
+
+NEGATIVE_FIXTURE_TEST_CASE(T262_realpath, ServiceImplUtilFixture)
+{
+ BOOST_REQUIRE(realPath(ServiceImplUtilFixture::enoentPath).empty());
+}
+
+NEGATIVE_FIXTURE_TEST_CASE(T263_realpath, ServiceImplUtilFixture)
+{
+ BOOST_REQUIRE(realPath("").empty());
+}
+
+POSITIVE_TEST_CASE(T264_getSkelPkgDir)
+{
+ std::string path;
+ BOOST_REQUIRE(getSkelPkgDir("app/bin", path));
+ BOOST_REQUIRE(!path.empty());
+}
+
+NEGATIVE_FIXTURE_TEST_CASE(T265_getLegalPkgBaseDirs, ServiceImplUtilFixture)
+{
+ std::vector<std::string> legalPkgBaseDirs;
+ std::string home;
+ BOOST_REQUIRE_THROW(getLegalPkgBaseDirs(ServiceImplUtilFixture::nonexistingUid,
+ "pkg_name", SM_APP_INSTALL_LOCAL, home,
+ legalPkgBaseDirs, true),
+ TizenPlatformConfig::Exception::ContextError);
+}
+
+NEGATIVE_FIXTURE_TEST_CASE(T266_getLegalPkgBaseDirs, ServiceImplUtilFixture)
+{
+ std::vector<std::string> legalPkgBaseDirs;
+ std::string home;
+ BOOST_REQUIRE(getLegalPkgBaseDirs(0,
+ "pkg_name", SM_APP_INSTALL_NONE, home,
+ legalPkgBaseDirs, true) == SECURITY_MANAGER_ERROR_INPUT_PARAM);
+}
+
+NEGATIVE_FIXTURE_TEST_CASE(T267_getLegalPkgBaseDirs, ServiceImplUtilFixture)
+{
+ std::vector<std::string> legalPkgBaseDirs;
+ std::string home;
+ BOOST_REQUIRE(getLegalPkgBaseDirs(0,
+ "pkg_name", SM_APP_INSTALL_END, home,
+ legalPkgBaseDirs, true) == SECURITY_MANAGER_ERROR_INPUT_PARAM);
+}
+
+POSITIVE_FIXTURE_TEST_CASE(T268_getLegalPkgBaseDirs, ServiceImplUtilFixture)
+{
+ std::vector<std::string> legalPkgBaseDirs;
+ std::string home;
+ BOOST_REQUIRE(getLegalPkgBaseDirs(0,
+ "pkg_name", SM_APP_INSTALL_GLOBAL, home,
+ legalPkgBaseDirs, true) == SECURITY_MANAGER_SUCCESS);
+}
+
+POSITIVE_FIXTURE_TEST_CASE(T269_pathsCheck, ServiceImplUtilFixture)
+{
+ pkg_paths requested = {{ServiceImplUtilFixture::requestedPath1, SECURITY_MANAGER_PATH_OWNER_RW_OTHER_RO}};
+ BOOST_REQUIRE(pathsCheck(requested, {ServiceImplUtilFixture::allowedPath1}));
+}
+
+NEGATIVE_FIXTURE_TEST_CASE(T269_pathsCheck, ServiceImplUtilFixture)
+{
+ pkg_paths requested = {{ServiceImplUtilFixture::requestedPath1, SECURITY_MANAGER_PATH_OWNER_RW_OTHER_RO},
+ {ServiceImplUtilFixture::invalidRequestedPath, SECURITY_MANAGER_PATH_OWNER_RW_OTHER_RO}};
+ BOOST_REQUIRE(!pathsCheck(requested, {ServiceImplUtilFixture::allowedPath1, ServiceImplUtilFixture::allowedPath2}));
+}
+
+POSITIVE_FIXTURE_TEST_CASE(T270_isSharedRO, ServiceImplUtilFixture)
+{
+ pkg_paths requested = {{ServiceImplUtilFixture::requestedPath2, SECURITY_MANAGER_PATH_RW}};
+ BOOST_REQUIRE(!isSharedRO(requested));
+ requested.push_back({ServiceImplUtilFixture::requestedPath1, SECURITY_MANAGER_PATH_OWNER_RW_OTHER_RO});
+ BOOST_REQUIRE(isSharedRO(requested));
+}
+
+POSITIVE_TEST_CASE(T271_getGlobalUserId)
+{
+ BOOST_REQUIRE_NO_THROW(getGlobalUserId());
+}
+
+POSITIVE_TEST_CASE(T272_setRequestDefaultValues)
+{
+ uid_t uid = 0, globalUid = getGlobalUserId();
+ int it = SM_APP_INSTALL_NONE;
+ BOOST_REQUIRE_NO_THROW(setRequestDefaultValues(uid, it));
+ BOOST_REQUIRE(it == SM_APP_INSTALL_GLOBAL && uid == globalUid);
+ uid = 1;
+ it = SM_APP_INSTALL_GLOBAL;
+ BOOST_REQUIRE_NO_THROW(setRequestDefaultValues(uid, it));
+ BOOST_REQUIRE(it == SM_APP_INSTALL_GLOBAL && uid == globalUid);
+}
+
+BOOST_AUTO_TEST_SUITE_END()