--- /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_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()