From: Tomasz Swierczek Date: Thu, 2 Apr 2020 11:47:45 +0000 (+0200) Subject: Add unit tests for functions in utils.cpp and other files X-Git-Tag: submit/tizen/20200710.130420~9 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=e75fd193fe22bc55a2bcfcc05ccd40d97afb5cf8;p=platform%2Fcore%2Fsecurity%2Fsecurity-manager.git Add unit tests for functions in utils.cpp and other files This commit is aimed at increasing UT code coverage as well as to increase negative test case to positive ratio. Change-Id: I7f1576d1c6f1234359a1f5a0df6610e26450dd08 --- diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 77fdca50..b316d462 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -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 index 00000000..ec9d55e2 --- /dev/null +++ b/test/test_misc.cpp @@ -0,0 +1,178 @@ +/* + * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved + * + * Contact: Tomasz Swierczek + * + * 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 +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace SecurityManager; + +BOOST_AUTO_TEST_SUITE(UTILS_TEST) + +namespace { + +int noThrowFunc() +{ + ScopedTimeStamper __stamper("a location", Credentials::getCredentialsFromSelf()); + return SECURITY_MANAGER_SUCCESS; +} + +template 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) == SECURITY_MANAGER_ERROR_MEMORY, "Invalid return value from try_catch()"); +} + +NEGATIVE_TEST_CASE(T284_try_catch) +{ + BOOST_REQUIRE_MESSAGE(try_catch(throwFunc) == SECURITY_MANAGER_ERROR_UNKNOWN, "Invalid return value from try_catch()"); +} + +NEGATIVE_TEST_CASE(T285_try_catch) +{ + BOOST_REQUIRE_MESSAGE(try_catch(throwFunc) == SECURITY_MANAGER_ERROR_UNKNOWN, "Invalid return value from try_catch()"); +} + +NEGATIVE_TEST_CASE(T286_try_catch) +{ + BOOST_REQUIRE_MESSAGE(try_catch(throwFunc) == SECURITY_MANAGER_ERROR_UNKNOWN, "Invalid return value from try_catch()"); +} + +POSITIVE_TEST_CASE(T287_groups_loading) +{ + std::vector vgroups; + std::vector privileges; + gid_t *groups = static_cast(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 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()