Add tests for service_impl_utils.cpp functions 81/229081/29
authorTomasz Swierczek <t.swierczek@samsung.com>
Fri, 27 Mar 2020 14:35:47 +0000 (15:35 +0100)
committerTomasz Swierczek <t.swierczek@samsung.com>
Fri, 19 Jun 2020 10:46:29 +0000 (10:46 +0000)
This patch is aimed at increasing unit test code coverage.

Change-Id: I1392355c4933659b0f0ede136ae600ca0356936c

test/CMakeLists.txt
test/test_service_impl_utils.cpp [new file with mode: 0644]

index 77bbc919e1b98215af78e9c3d4e7b68dda973918..77fdca5096ece1a2195269cc9c6b71f746cb1667 100644 (file)
@@ -63,6 +63,7 @@ SET(SM_TESTS_SOURCES
     ${SM_TEST_SRC}/test_privilege_db_sharing.cpp
     ${SM_TEST_SRC}/test_privilege_db_app_defined_privileges.cpp
     ${SM_TEST_SRC}/test_privilege_db_migration.cpp
+    ${SM_TEST_SRC}/test_service_impl_utils.cpp
     ${SM_TEST_SRC}/test_smack-labels.cpp
     ${SM_TEST_SRC}/test_smack-rules.cpp
     ${SM_TEST_SRC}/test_check_proper_drop.cpp
diff --git a/test/test_service_impl_utils.cpp b/test/test_service_impl_utils.cpp
new file mode 100644 (file)
index 0000000..8eee490
--- /dev/null
@@ -0,0 +1,180 @@
+/*
+ *  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()