-# Copyright (c) 2011 - 2014 Samsung Electronics Co., Ltd All Rights Reserved
+# Copyright (c) 2011 - 2016 Samsung Electronics Co., Ltd All Rights Reserved
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
ADD_DEFINITIONS("-DBUILD_TYPE_DEBUG")
ENDIF (CMAKE_BUILD_TYPE MATCHES "DEBUG")
+SET(INCLUDE_PATH ${PROJECT_SOURCE_DIR}/src/include)
+SET(COMMON_PATH ${PROJECT_SOURCE_DIR}/src/common)
+SET(CLIENT_PATH ${PROJECT_SOURCE_DIR}/src/client)
+SET(SERVER_PATH ${PROJECT_SOURCE_DIR}/src/server)
+SET(DPL_PATH ${PROJECT_SOURCE_DIR}/src/dpl)
+SET(CMD_PATH ${PROJECT_SOURCE_DIR}/src/cmd)
+SET(NSS_PATH ${PROJECT_SOURCE_DIR}/src/nss)
+
+SET(TARGET_SERVER "security-manager")
+SET(TARGET_CLIENT "security-manager-client")
+SET(TARGET_COMMON "security-manager-commons")
+SET(TARGET_CMD "security-manager-cmd")
+SET(TARGET_CLEANUP "security-manager-cleanup")
+SET(TARGET_NSS "security-manager-nss")
+
ADD_SUBDIRECTORY(src)
ADD_SUBDIRECTORY(pc)
ADD_SUBDIRECTORY(systemd)
ADD_SUBDIRECTORY(db)
ADD_SUBDIRECTORY(policy)
ADD_SUBDIRECTORY(migration)
+ADD_SUBDIRECTORY(test)
--- /dev/null
+<manifest>
+ <request>
+ <domain name="_"/>
+ </request>
+</manifest>
Source1: security-manager.manifest
Source3: libsecurity-manager-client.manifest
Source4: libnss-security-manager.manifest
+Source5: security-manager-tests.manifest
Requires: security-manager-policy
Requires: nether
Requires(post): sqlite3
%description policy
Set of security rules that constitute security policy in the system
+%package -n security-manager-tests
+Summary: Security manager unit test binaries
+Group: Security/Development
+Requires: boost-test
+
+%description -n security-manager-tests
+Internal test for security manager implementation.
+
%prep
%setup -q
cp %{SOURCE1} .
cp %{SOURCE3} .
cp %{SOURCE4} .
+cp %{SOURCE5} .
%build
%if 0%{?sec_build_binary_debug_enable}
%{_datadir}/security-manager/policy
%attr(755,root,root) %{_bindir}/security-manager-policy-reload
%attr(755,root,root) %{_sysconfdir}/opt/upgrade/241.security-manager.policy-update.sh
+
+%files -n security-manager-tests
+%manifest %{name}.manifest
+%attr(755,root,root) %{_bindir}/security-manager-unit-tests
+
-SET(INCLUDE_PATH ${PROJECT_SOURCE_DIR}/src/include)
-SET(COMMON_PATH ${PROJECT_SOURCE_DIR}/src/common)
-SET(CLIENT_PATH ${PROJECT_SOURCE_DIR}/src/client)
-SET(SERVER_PATH ${PROJECT_SOURCE_DIR}/src/server)
-SET(DPL_PATH ${PROJECT_SOURCE_DIR}/src/dpl)
-SET(CMD_PATH ${PROJECT_SOURCE_DIR}/src/cmd)
-SET(NSS_PATH ${PROJECT_SOURCE_DIR}/src/nss)
-
-SET(TARGET_SERVER "security-manager")
-SET(TARGET_CLIENT "security-manager-client")
-SET(TARGET_COMMON "security-manager-commons")
-SET(TARGET_CMD "security-manager-cmd")
-SET(TARGET_CLEANUP "security-manager-cleanup")
-SET(TARGET_NSS "security-manager-nss")
-
ADD_SUBDIRECTORY(include)
ADD_SUBDIRECTORY(common)
ADD_SUBDIRECTORY(client)
extern const char* BOLD_GOLD_END;
extern const char* BOLD_WHITE_BEGIN;
extern const char* BOLD_WHITE_END;
+extern const char* COLOR_END;
} //namespace Text
namespace Html {
const char* BOLD_GOLD_END = "\033[m";
const char* BOLD_WHITE_BEGIN = "\033[1;37m";
const char* BOLD_WHITE_END = "\033[m";
+const char* COLOR_END = "\033[m";
} //namespace Text
namespace Html {
--- /dev/null
+# Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
+#
+# 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 CMakeLists.txt
+# @author Radoslaw Bartosiak <r.bartosiak@samsung.com>
+# @brief Cmake for internal security manager tests
+#
+
+PKG_CHECK_MODULES(COMMON_DEP
+ REQUIRED
+ libtzplatform-config
+ )
+
+IF(DPL_WITH_DLOG)
+ PKG_CHECK_MODULES(DLOG_DEP REQUIRED dlog)
+ENDIF(DPL_WITH_DLOG)
+
+ADD_DEFINITIONS( "-DBOOST_TEST_DYN_LINK" )
+
+################################################################################
+
+SET(DPL_PATH ${PROJECT_SOURCE_DIR}/src/dpl)
+SET(SM_TEST_SRC ${PROJECT_SOURCE_DIR}/test)
+
+################################################################################
+
+SET(TARGET_SM_TESTS "security-manager-unit-tests")
+
+SET(SM_TESTS_SOURCES
+ ${SM_TEST_SRC}/colour_log_formatter.cpp
+ ${SM_TEST_SRC}/security-manager-tests.cpp
+ ${SM_TEST_SRC}/test_file-lock.cpp
+ ${DPL_PATH}/core/src/assert.cpp
+ ${DPL_PATH}/core/src/colors.cpp
+ ${DPL_PATH}/core/src/errno_string.cpp
+ ${DPL_PATH}/core/src/exception.cpp
+ ${DPL_PATH}/core/src/noncopyable.cpp
+ ${DPL_PATH}/log/src/abstract_log_provider.cpp
+ ${DPL_PATH}/log/src/log.cpp
+ ${DPL_PATH}/log/src/old_style_log_provider.cpp
+ ${PROJECT_SOURCE_DIR}/src/common/file-lock.cpp
+ ${PROJECT_SOURCE_DIR}/src/common/tzplatform-config.cpp
+)
+
+IF(DPL_WITH_DLOG)
+ SET(SM_TESTS_SOURCES
+ ${SM_TESTS_SOURCES}
+ ${DPL_PATH}/log/src/dlog_log_provider.cpp)
+ENDIF(DPL_WITH_DLOG)
+
+IF(DPL_WITH_SYSTEMD_JOURNAL)
+ SET(SM_TESTS_SOURCES
+ ${SM_TESTS_SOURCES}
+ ${DPL_PATH}/log/src/sd_journal_provider.cpp)
+ENDIF(DPL_WITH_SYSTEMD_JOURNAL)
+
+INCLUDE_DIRECTORIES(
+ ${COMMON_DEP_INCLUDE_DIRS}
+ ${DLOG_DEP_INCLUDE_DIRS}
+ ${SM_TEST_SRC}
+ ${PROJECT_SOURCE_DIR}/src/include
+ ${PROJECT_SOURCE_DIR}/src/client/include
+ ${PROJECT_SOURCE_DIR}/src/common/include
+ ${PROJECT_SOURCE_DIR}/src/server/main/include
+ ${PROJECT_SOURCE_DIR}/src/dpl/core/include
+ ${PROJECT_SOURCE_DIR}/src/dpl/log/include
+ ${PROJECT_SOURCE_DIR}/src/dpl/log/include/dpl/log
+ ${PROJECT_SOURCE_DIR}/src/dpl/log
+)
+
+ADD_EXECUTABLE(${TARGET_SM_TESTS} ${SM_TESTS_SOURCES})
+
+TARGET_LINK_LIBRARIES(${TARGET_SM_TESTS}
+ ${COMMON_DEP_LIBRARIES}
+ ${DLOG_DEP_LIBRARIES}
+ boost_unit_test_framework
+ -ldl
+)
+
+INSTALL(TARGETS ${TARGET_SM_TESTS} DESTINATION ${BIN_INSTALL_DIR})
\ No newline at end of file
--- /dev/null
+/*
+ * (C) Copyright Gennadiy Rozental 2005-2008.
+ * Distributed under the Boost Software License, Version 1.0.
+ * (See accompanying file LICENSE_1_0.txt or copy at
+ * http://www.boost.org/LICENSE_1_0.txt)
+ *
+ * See http://www.boost.org/libs/test for the library home page.
+ */
+/*
+ * @file colour_log_formatter.cpp
+ * @author Zofia Abramowska (z.abramowska@samsung.com)
+ * @version
+ * @brief
+ */
+// Boost.Test
+#include <colour_log_formatter.h>
+#include <boost/test/unit_test_suite_impl.hpp>
+#include <boost/test/framework.hpp>
+#include <boost/test/utils/basic_cstring/io.hpp>
+#include <boost/test/utils/lazy_ostream.hpp>
+
+// Boost
+#include <boost/version.hpp>
+
+// STL
+#include <iostream>
+#include <string>
+
+#include <dpl/colors.h>
+
+// ************************************************************************** //
+// ************** colour_log_formatter ************** //
+// ************************************************************************** //
+
+using namespace boost::unit_test;
+namespace SecurityManager {
+
+namespace {
+
+const_string
+test_phase_identifier()
+{
+ return framework::is_initialized()
+ ? const_string(framework::current_test_case().p_name.get())
+ : BOOST_TEST_L("Test setup");
+}
+
+const_string
+get_basename(const const_string &file_name)
+{
+ return basename(file_name.begin());
+}
+
+std::string
+get_basename(const std::string &file_name)
+{
+ return basename(file_name.c_str());
+}
+
+} // local namespace
+
+//____________________________________________________________________________//
+
+void
+colour_log_formatter::log_start(
+ std::ostream &output,
+ counter_t test_cases_amount)
+{
+ if (test_cases_amount > 0)
+ output << "Running " << test_cases_amount << " test "
+ << (test_cases_amount > 1 ? "cases" : "case") << "...\n";
+}
+
+//____________________________________________________________________________//
+
+void
+colour_log_formatter::log_finish(std::ostream &ostr)
+{
+ ostr.flush();
+}
+
+//____________________________________________________________________________//
+
+void
+colour_log_formatter::log_build_info(std::ostream &output)
+{
+ output << "Platform: " << BOOST_PLATFORM << '\n'
+ << "Compiler: " << BOOST_COMPILER << '\n'
+ << "STL : " << BOOST_STDLIB << '\n'
+ << "Boost : " << BOOST_VERSION / 100000 << "."
+ << BOOST_VERSION / 100 % 1000 << "."
+ << BOOST_VERSION % 100 << std::endl;
+}
+
+//____________________________________________________________________________//
+
+void
+colour_log_formatter::test_unit_start(
+ std::ostream &output,
+ test_unit const &tu)
+{
+ if (tu.p_type_name->find(const_string("suite")) == 0) {
+ output << "Starting test " << tu.p_type_name << " \"" << tu.p_name << "\"" <<
+ std::endl;
+ } else {
+ output << "Running test " << tu.p_type_name << " \"" << tu.p_name << "\"" <<
+ std::endl;
+ }
+}
+
+//____________________________________________________________________________//
+
+void
+colour_log_formatter::test_unit_finish(
+ std::ostream &output,
+ test_unit const &tu,
+ unsigned long elapsed)
+{
+ if (tu.p_type_name->find(const_string("suite")) == 0) {
+ output << "Finished test " << tu.p_type_name << " \"" << tu.p_name << "\"" <<
+ std::endl;
+ return;
+ }
+
+ std::string color = SecurityManager::Colors::Text::GREEN_BEGIN;
+ std::string status = "OK";
+
+ if (m_isTestCaseFailed) {
+ color = SecurityManager::Colors::Text::RED_BEGIN;
+ status = "FAIL";
+ }
+
+ output << "\t" << "[ " << color << status << SecurityManager::Colors::Text::COLOR_END <<
+ " ]";
+
+
+ output << ", " << SecurityManager::Colors::Text::CYAN_BEGIN << "time: ";
+
+ if (elapsed > 0) {
+ if (elapsed % 1000 == 0)
+ output << elapsed / 1000 << "ms";
+ else
+ output << elapsed << "mks";
+ } else {
+ output << "N/A";
+ }
+
+ output << SecurityManager::Colors::Text::COLOR_END << std::endl;
+ m_isTestCaseFailed = false;
+}
+
+//____________________________________________________________________________//
+
+void
+colour_log_formatter::test_unit_skipped(
+ std::ostream &output,
+ test_unit const &tu)
+{
+ output << "Test " << tu.p_type_name << " \"" << tu.p_name << "\"" <<
+ "is skipped" << std::endl;
+}
+
+//____________________________________________________________________________//
+
+void
+colour_log_formatter::log_exception(
+ std::ostream &output,
+ log_checkpoint_data const &checkpoint_data,
+ boost::execution_exception const &ex)
+{
+ boost::execution_exception::location const &loc = ex.where();
+ output << '\t' << SecurityManager::Colors::Text::BOLD_YELLOW_BEGIN << get_basename(
+ loc.m_file_name)
+ << '(' << loc.m_line_num << "), ";
+
+ output << "fatal error in \""
+ << (loc.m_function.is_empty() ? test_phase_identifier() : loc.m_function) <<
+ "\": ";
+
+ output << SecurityManager::Colors::Text::COLOR_END << ex.what();
+
+ if (!checkpoint_data.m_file_name.is_empty()) {
+ output << '\n';
+ output << "\tlast checkpoint : " << get_basename(checkpoint_data.m_file_name)
+ << '(' << checkpoint_data.m_line_num << ")";
+
+ if (!checkpoint_data.m_message.empty())
+ output << ": " << checkpoint_data.m_message;
+ }
+
+ output << std::endl;
+ m_isTestCaseFailed = true;
+}
+
+//____________________________________________________________________________//
+
+void
+colour_log_formatter::log_entry_start(
+ std::ostream &output,
+ log_entry_data const &entry_data,
+ log_entry_types let)
+{
+ switch (let) {
+ case BOOST_UTL_ET_INFO:
+ output << '\t' << entry_data.m_file_name << '(' << entry_data.m_line_num <<
+ "), ";
+ output << "info: ";
+ break;
+
+ case BOOST_UTL_ET_MESSAGE:
+ break;
+
+ case BOOST_UTL_ET_WARNING:
+ output << '\t' << get_basename(entry_data.m_file_name) << '(' <<
+ entry_data.m_line_num << "), ";
+ output << "warning in \"" << test_phase_identifier() << "\": ";
+ break;
+
+ case BOOST_UTL_ET_ERROR:
+ output << '\t' << SecurityManager::Colors::Text::BOLD_YELLOW_BEGIN << get_basename(
+ entry_data.m_file_name)
+ << '(' << entry_data.m_line_num << "), ";
+ output << "error in \"" << test_phase_identifier() << "\": ";
+ m_isTestCaseFailed = true;
+ break;
+
+ case BOOST_UTL_ET_FATAL_ERROR:
+ output << '\t' << SecurityManager::Colors::Text::BOLD_YELLOW_BEGIN << get_basename(
+ entry_data.m_file_name)
+ << '(' << entry_data.m_line_num << "), ";
+ output << " fatal error in \"" << test_phase_identifier() << "\": ";
+ m_isTestCaseFailed = true;
+ break;
+ }
+
+ output << SecurityManager::Colors::Text::COLOR_END;
+}
+
+//____________________________________________________________________________//
+
+void
+colour_log_formatter::log_entry_value(
+ std::ostream &output,
+ const_string value)
+{
+ output << value;
+}
+
+//____________________________________________________________________________//
+
+void
+colour_log_formatter::log_entry_value(
+ std::ostream &output,
+ lazy_ostream const &value)
+{
+ output << value;
+}
+
+//____________________________________________________________________________//
+
+void
+colour_log_formatter::log_entry_finish(
+ std::ostream &output)
+{
+ output << std::endl;
+}
+
+//____________________________________________________________________________//
+
+//____________________________________________________________________________//
+
+} // namespace SecurityManager
+
+//____________________________________________________________________________//
+
--- /dev/null
+/*
+ * (C) Copyright Gennadiy Rozental 2005-2008.
+ * Distributed under the Boost Software License, Version 1.0.
+ * (See accompanying file LICENSE_1_0.txt or copy at
+ * http://www.boost.org/LICENSE_1_0.txt)
+ *
+ * See http://www.boost.org/libs/test for the library home page.
+ */
+/*
+ * @file colour_log_formatter.h
+ * @author Zofia Abramowska (z.abramowska@samsung.com)
+ * @version
+ * @brief
+ */
+#pragma once
+#include <boost/test/unit_test_log_formatter.hpp>
+
+namespace SecurityManager {
+class colour_log_formatter : public boost::unit_test::unit_test_log_formatter {
+public:
+ // Formatter interface
+ colour_log_formatter() : m_isTestCaseFailed(false) {}
+ void log_start(
+ std::ostream &,
+ boost::unit_test::counter_t test_cases_amount);
+ void log_finish(std::ostream &);
+ void log_build_info(std::ostream &);
+
+ void test_unit_start(
+ std::ostream &,
+ boost::unit_test::test_unit const &tu);
+ void test_unit_finish(
+ std::ostream &,
+ boost::unit_test::test_unit const &tu,
+ unsigned long elapsed);
+ void test_unit_skipped(
+ std::ostream &,
+ boost::unit_test::test_unit const &tu);
+
+ void log_exception(
+ std::ostream &,
+ boost::unit_test::log_checkpoint_data const &,
+ boost::execution_exception const &ex);
+
+ void log_entry_start(
+ std::ostream &,
+ boost::unit_test::log_entry_data const &,
+ log_entry_types let);
+ void log_entry_value(
+ std::ostream &,
+ boost::unit_test::const_string value);
+ void log_entry_value(
+ std::ostream &,
+ boost::unit_test::lazy_ostream const &value);
+ void log_entry_finish(std::ostream &);
+
+private:
+ bool m_isTestCaseFailed;
+};
+} // namespace SecurityManager
--- /dev/null
+/*
+ * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * 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/security-manager-tests.cpp
+ * @author Radoslaw Bartosiak <r.bartosiak@samsung.com>
+ * @version 1.0
+ * @brief Security manager tests
+ */
+
+#include <iostream>
+#include <boost/test/unit_test.hpp>
+#include <boost/test/unit_test_log.hpp>
+#include <boost/test/results_reporter.hpp>
+#include <colour_log_formatter.h>
+#include <dpl/log/log.h>
+
+struct TestConfig {
+ TestConfig()
+ {
+ boost::unit_test::unit_test_log.set_threshold_level(
+ boost::unit_test::log_test_units);
+ boost::unit_test::results_reporter::set_level(boost::unit_test::SHORT_REPORT);
+ boost::unit_test::unit_test_log.set_formatter(new SecurityManager::colour_log_formatter);
+ }
+ ~TestConfig()
+ {
+ }
+};
+
+
+struct LogSetup {
+ LogSetup()
+ {
+ SecurityManager::Singleton<SecurityManager::Log::LogSystem>::Instance().SetTag("SECURITY_MANAGER_TESTS");
+ }
+ ~LogSetup() {}
+};
+
+BOOST_GLOBAL_FIXTURE(TestConfig)
+BOOST_GLOBAL_FIXTURE(LogSetup)
--- /dev/null
+/*
+ * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * 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_file-lock.cpp
+ * @author Radoslaw Bartosiak (r.bartosiak@samsung.com)
+ * @version 1.0
+ */
+#define BOOST_TEST_MODULE SecurityManagerTest
+#include <boost/test/unit_test.hpp>
+#include <boost/test/results_reporter.hpp>
+
+#include <cstdio>
+#include <string>
+
+#include <dpl/errno_string.h>
+#include <dpl/log/log.h>
+#include <file-lock.h>
+
+using namespace SecurityManager;
+
+struct FileLockFixture
+{
+ FileLockFixture()
+ {
+ FILE *f;
+ f= fopen(existingLockFile.c_str(), "w");
+ if (f != NULL)
+ fclose(f);
+ else
+ LogError("Failed to open file " << existingLockFile << " : " << GetErrnoString());
+ remove(noExistingLockFile.c_str());
+
+ }
+
+ ~FileLockFixture()
+ {
+ if (remove(existingLockFile.c_str()))
+ LogWarning("Failed to remove file " << existingLockFile << " : " << GetErrnoString());
+ remove(noExistingLockFile.c_str());
+ }
+
+ const static std::string existingLockFile;
+ const static std::string noExistingLockFile;
+};
+
+const std::string FileLockFixture::existingLockFile = "/tmp/SecurityManagerUTFileLockExisting";
+const std::string FileLockFixture::noExistingLockFile = "/tmp/SecurityManagerUTFileLockNoExisting";
+
+
+BOOST_AUTO_TEST_SUITE(FILE_LOCK_TEST)
+
+BOOST_FIXTURE_TEST_CASE(T010_empty_file_name, FileLockFixture)
+{
+ BOOST_REQUIRE_THROW(FileLocker fl(""), FileLocker::Exception::LockFailed);
+}
+
+BOOST_FIXTURE_TEST_CASE(T020_existing_file_name, FileLockFixture)
+{
+ BOOST_REQUIRE_NO_THROW(FileLocker fl(FileLockFixture::existingLockFile));
+}
+
+BOOST_FIXTURE_TEST_CASE(T030_no_existing_file_name, FileLockFixture)
+{
+ BOOST_REQUIRE_NO_THROW(FileLocker fl(FileLockFixture::noExistingLockFile));
+}
+
+BOOST_FIXTURE_TEST_CASE(T040_new_lock_file_is_locked, FileLockFixture)
+{
+ FileLocker fl(std::string(FileLockFixture::existingLockFile), false);
+ BOOST_REQUIRE_MESSAGE(fl.Locked(), "New lock file is not locked");
+}
+
+BOOST_FIXTURE_TEST_CASE(T050_after_unlock_file_is_unlocked, FileLockFixture)
+{
+ FileLocker fl(FileLockFixture::noExistingLockFile, false);
+ fl.Unlock();
+ BOOST_REQUIRE_MESSAGE(not(fl.Locked()), "File is locked after Unlock()");
+}
+
+BOOST_FIXTURE_TEST_CASE(T060_after_lock_file_is_locked_again, FileLockFixture)
+{
+ FileLocker fl(FileLockFixture::existingLockFile, false);
+ fl.Unlock();
+ fl.Lock();
+ BOOST_REQUIRE_MESSAGE(fl.Locked(), "File is locked after Unlock()");
+}
+
+BOOST_AUTO_TEST_SUITE_END()