Add unit tests for functions in utils.cpp and other files 58/229658/28
authorTomasz Swierczek <t.swierczek@samsung.com>
Thu, 2 Apr 2020 11:47:45 +0000 (13:47 +0200)
committerTomasz Swierczek <t.swierczek@samsung.com>
Wed, 24 Jun 2020 11:03:35 +0000 (13:03 +0200)
This commit is aimed at increasing UT code coverage as well
as to increase negative test case to positive ratio.

Change-Id: I7f1576d1c6f1234359a1f5a0df6610e26450dd08

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

index 77fdca5096ece1a2195269cc9c6b71f746cb1667..b316d462331959bd0ca432fda3812f4a258811ce 100644 (file)
@@ -67,6 +67,7 @@ SET(SM_TESTS_SOURCES
     ${SM_TEST_SRC}/test_smack-labels.cpp
     ${SM_TEST_SRC}/test_smack-rules.cpp
     ${SM_TEST_SRC}/test_check_proper_drop.cpp
+    ${SM_TEST_SRC}/test_misc.cpp
     ${DPL_PATH}/core/src/assert.cpp
     ${DPL_PATH}/core/src/colors.cpp
     ${DPL_PATH}/core/src/errno_string.cpp
@@ -78,6 +79,7 @@ SET(SM_TESTS_SOURCES
     ${DPL_PATH}/log/src/log.cpp
     ${DPL_PATH}/log/src/old_style_log_provider.cpp
     ${PROJECT_SOURCE_DIR}/src/common/config-file.cpp
+    ${PROJECT_SOURCE_DIR}/src/common/credentials.cpp
     ${PROJECT_SOURCE_DIR}/src/common/filesystem.cpp
     ${PROJECT_SOURCE_DIR}/src/common/file-lock.cpp
     ${PROJECT_SOURCE_DIR}/src/common/privilege_db.cpp
diff --git a/test/test_misc.cpp b/test/test_misc.cpp
new file mode 100644 (file)
index 0000000..ec9d55e
--- /dev/null
@@ -0,0 +1,178 @@
+/*
+ *  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_misc.cpp
+ * @author      Tomasz Ćšwierczek (t.swierczek@samsung.com)
+ * @version     1.0
+ * @brief       Tests of functions & classes from utils.cpp file and other smaller files
+ */
+
+#include <algorithm>
+#include <credentials.h>
+#include <string>
+#include <stdexcept>
+#include <dpl/exception.h>
+#include <security-manager-types.h>
+#include <sys/types.h>
+#include <utils.h>
+#include <testmacros.h>
+#include <tzplatform-config.h>
+
+using namespace SecurityManager;
+
+BOOST_AUTO_TEST_SUITE(UTILS_TEST)
+
+namespace {
+
+int noThrowFunc()
+{
+    ScopedTimeStamper __stamper("a location", Credentials::getCredentialsFromSelf());
+    return SECURITY_MANAGER_SUCCESS;
+}
+
+template<class E> int throwFunc()
+{
+    throw E();
+    return SECURITY_MANAGER_SUCCESS;
+}
+
+int throwExFunc()
+{
+    throw Exception("","",0,"");
+    return SECURITY_MANAGER_SUCCESS;
+}
+
+} // namespace anonymous
+
+POSITIVE_TEST_CASE(T280_ScopedTimeStamper_try_catch)
+{
+    BOOST_REQUIRE_MESSAGE(try_catch(noThrowFunc) == SECURITY_MANAGER_SUCCESS, "Invalid return from try_catch()");
+}
+
+NEGATIVE_TEST_CASE(T282_try_catch)
+{
+    BOOST_REQUIRE_MESSAGE(try_catch(throwExFunc) == SECURITY_MANAGER_ERROR_UNKNOWN, "Invalid return value from try_catch()");
+}
+
+NEGATIVE_TEST_CASE(T283_try_catch)
+{
+    BOOST_REQUIRE_MESSAGE(try_catch(throwFunc<std::bad_alloc>) == SECURITY_MANAGER_ERROR_MEMORY, "Invalid return value from try_catch()");
+}
+
+NEGATIVE_TEST_CASE(T284_try_catch)
+{
+    BOOST_REQUIRE_MESSAGE(try_catch(throwFunc<std::system_error>) == SECURITY_MANAGER_ERROR_UNKNOWN, "Invalid return value from try_catch()");
+}
+
+NEGATIVE_TEST_CASE(T285_try_catch)
+{
+    BOOST_REQUIRE_MESSAGE(try_catch(throwFunc<std::exception>) == SECURITY_MANAGER_ERROR_UNKNOWN, "Invalid return value from try_catch()");
+}
+
+NEGATIVE_TEST_CASE(T286_try_catch)
+{
+    BOOST_REQUIRE_MESSAGE(try_catch(throwFunc<std::string>) == SECURITY_MANAGER_ERROR_UNKNOWN, "Invalid return value from try_catch()");
+}
+
+POSITIVE_TEST_CASE(T287_groups_loading)
+{
+    std::vector<gid_t> vgroups;
+    std::vector<std::string> privileges;
+    gid_t *groups = static_cast<gid_t*>(malloc(sizeof(gid_t)));
+    BOOST_REQUIRE_MESSAGE(groups, "Memory allocation problem");
+    auto gPtr = makeUnique(groups, free);
+    size_t groups_count = 1;
+    BOOST_REQUIRE_NO_THROW(group_vector_to_array(vgroups, &groups, &groups_count));
+    BOOST_REQUIRE_MESSAGE(!groups && !groups_count, "Invalid output from group_vector_to_array");
+    BOOST_REQUIRE_NO_THROW(loadGroups(vgroups, &privileges));
+    // current configuration has some groups and some privileges
+    BOOST_REQUIRE_MESSAGE(vgroups.size() > 0 && privileges.size() == vgroups.size(),
+                          "Invalid output from loadGroups");
+    BOOST_REQUIRE_NO_THROW(group_vector_to_array(vgroups, &groups, &groups_count));
+    auto gPtr2 = makeUnique(groups, free);
+    BOOST_REQUIRE_MESSAGE(groups, "NULL returned from group_vector_to_array");
+    BOOST_REQUIRE_MESSAGE(groups_count ==  vgroups.size(), "Invalid number of groups");
+    BOOST_REQUIRE_MESSAGE(std::is_permutation(vgroups.begin(), vgroups.end(),
+                          groups, groups + groups_count),
+                          "Invalid return from group_vector_to_array");
+}
+
+POSITIVE_TEST_CASE(T288_monotonic_clock)
+{
+    time_t t;
+    BOOST_REQUIRE_NO_THROW(t = monotonicNow());
+    BOOST_REQUIRE_MESSAGE(t > 0, "Invalid monotonic time value");
+}
+
+POSITIVE_TEST_CASE(T289_loadSystemdManagedPrivileges)
+{
+    std::vector<std::string> privs;
+    BOOST_REQUIRE_NO_THROW(loadSystemdManagedPrivileges(privs));
+}
+
+NEGATIVE_TEST_CASE(T290_creds)
+{
+    BOOST_REQUIRE_THROW(Credentials::getCredentialsFromSocket(-1), Credentials::Exception::SocketError);
+}
+
+NEGATIVE_TEST_CASE(T291_creds)
+{
+    BOOST_REQUIRE_THROW(Credentials::getCredentialsFromFd(-1), Credentials::Exception::FdError);
+}
+
+POSITIVE_TEST_CASE(T292_tzplatform_config)
+{
+    TizenPlatformConfig cfg(0);
+    BOOST_REQUIRE_NO_THROW(cfg.ctxMakePath(
+                    TizenPlatformConfig::getId("TZ_USER_HOME"),
+                    "/just a string 1/"));
+    BOOST_REQUIRE_NO_THROW(cfg.ctxMakePath(
+                    TizenPlatformConfig::getId("TZ_USER_HOME"),
+                    "/just a string 1/", "/just a string 2/"));
+    BOOST_REQUIRE_NO_THROW(cfg.ctxMakePath(
+                    TizenPlatformConfig::getId("TZ_USER_HOME"),
+                    "/just a string 1/", "/just a string 2/", "/just a string 3/"));
+
+    BOOST_REQUIRE_NO_THROW(TizenPlatformConfig::getId("TZ_USER_HOME"));
+    BOOST_REQUIRE_NO_THROW(TizenPlatformConfig::makePath(
+                                TizenPlatformConfig::getId("TZ_USER_HOME"),
+                                "/just a string 1/"));
+    BOOST_REQUIRE_NO_THROW(TizenPlatformConfig::makePath(
+                                TizenPlatformConfig::getId("TZ_USER_HOME"),
+                                "/just a string 1/", "/just a string 2/"));
+
+    BOOST_REQUIRE_NO_THROW(TizenPlatformConfig::makePath(
+                                TizenPlatformConfig::getId("TZ_USER_HOME"),
+                                "/just a string 1/", "/just a string 2/", "/just a string 3/"));
+}
+
+POSITIVE_TEST_CASE(T293_exception_class)
+{
+        Exception e("path", "function", 1, "message");
+        std::string s = Exception::KnownExceptionToString(e);
+        BOOST_REQUIRE_MESSAGE(!s.empty(), "Empty message passed from KnownExceptionToString");
+
+        std::string s1 = Exception::UnknownExceptionToString();
+        BOOST_REQUIRE_MESSAGE(!s1.empty(), "Empty message passed from UnknownExceptionToString");
+
+        Exception e2(e);
+        BOOST_REQUIRE_NO_THROW(Exception::DisplayKnownException(e2));
+        BOOST_REQUIRE_NO_THROW(Exception::DisplayUnknownException());
+}
+
+BOOST_AUTO_TEST_SUITE_END()