Add unit test Boost-based binary 91/232291/15
authorPawel Kowalski <p.kowalski2@partner.samsung.com>
Wed, 29 Apr 2020 17:56:08 +0000 (19:56 +0200)
committerMateusz Cegielka <m.cegielka@samsung.com>
Mon, 20 Jul 2020 11:42:54 +0000 (13:42 +0200)
Change-Id: Ie8f5b988f04aa1976fa697878f921fc22906ce0f

CMakeLists.txt
packaging/askuser-notification.spec
test/unit-tests/CMakeLists.txt [new file with mode: 0644]
test/unit-tests/colour_log_formatter.cpp [new file with mode: 0644]
test/unit-tests/colour_log_formatter.h [new file with mode: 0644]
test/unit-tests/main.cpp [new file with mode: 0644]

index 7bca093..ef9d99d 100644 (file)
@@ -78,6 +78,7 @@ SET(TARGET_ASKUSER_NOTIFICATION_LIB_TEST "askuser-notification-test")
 SET(TARGET_ASKUSER_NOTIFICATION_CLIENT_LIB "askuser-notification-client")
 SET(TARGET_PRIVACY_PRIVILEGE_MANAGER_CAPI_LIB "capi-privacy-privilege-manager")
 SET(TARGET_PRIVACY_PRIVILEGE_MANAGER_TEST "capi-privacy-privilege-manager-test")
+SET(TARGET_ASKUSER_UNIT_TESTS "askuser-unit-tests")
 
 ADD_SUBDIRECTORY(src/plugin)
 ADD_SUBDIRECTORY(src/notification-daemon)
@@ -87,6 +88,7 @@ ADD_SUBDIRECTORY(src/client)
 ADD_SUBDIRECTORY(src/capi)
 
 ADD_SUBDIRECTORY(pkgconfig)
+ADD_SUBDIRECTORY(test/unit-tests)
 
 IF (BUILD_WITH_SYSTEMD_DAEMON)
 ADD_SUBDIRECTORY(systemd)
index 913ecae..419a990 100644 (file)
@@ -28,6 +28,7 @@ BuildRequires: pkgconfig(capi-appfw-component-manager)
 BuildRequires: pkgconfig(capi-base-common)
 BuildRequires: pkgconfig(libsmack)
 BuildRequires: pkgconfig(aul)
+BuildRequires: boost-devel
 
 %if !%{defined build_type}
 %define build_type RELEASE
@@ -59,6 +60,13 @@ Summary: Tools for testing askuser-notification client and C API
 Provides tests for checking the API provided by capi-privacy-privilege-manager
 and askuser-notification-client
 
+%package -n askuser-unit-tests
+Summary:    Askuser unit test binaries
+Group:      Security/Development
+
+%description -n askuser-unit-tests
+Internal test for Askuser implementation.
+
 %package client
 Summary: Askuser notification client library
 Requires(post):   /sbin/ldconfig
@@ -172,6 +180,11 @@ systemctl restart cynara.service
 %attr(755,root,root) %{_bindir}/askuser-notification-test
 %attr(755,root,root) %{_bindir}/capi-privacy-privilege-manager-test
 
+%files -n askuser-unit-tests
+%manifest %{_datadir}/default.manifest
+%license LICENSE
+%attr(755,root,root) %{_bindir}/askuser-unit-tests
+
 %files client
 %manifest %{_datadir}/default.manifest
 %license LICENSE
diff --git a/test/unit-tests/CMakeLists.txt b/test/unit-tests/CMakeLists.txt
new file mode 100644 (file)
index 0000000..946c547
--- /dev/null
@@ -0,0 +1,41 @@
+FIND_PACKAGE(Boost REQUIRED COMPONENTS unit_test_framework)
+PKG_CHECK_MODULES(ASKUSER_UNIT_TESTS_DEP
+    REQUIRED
+    aul
+    glib-2.0
+    libsystemd
+    pkgmgr-info
+    security-manager
+    security-privilege-manager
+    )
+
+SET(ASKUSER_SRC_PATH ${PROJECT_SOURCE_DIR}/src)
+SET(ASKUSER_TESTS_PATH ${PROJECT_SOURCE_DIR}/test/unit-tests)
+
+ADD_DEFINITIONS("-DBOOST_TEST_DYN_LINK")
+
+INCLUDE_DIRECTORIES(SYSTEM ${ASKUSER_UNIT_TESTS_DEP_INCLUDE_DIRS})
+INCLUDE_DIRECTORIES(
+    ${ASKUSER_SRC_PATH}/common
+    ${ASKUSER_SRC_PATH}/test/unit-tests
+    )
+
+SET(TESTS_SOURCES
+    ${ASKUSER_SRC_PATH}/common/log/alog.cpp
+    ${ASKUSER_SRC_PATH}/common/policy/AppInfo.cpp
+    ${ASKUSER_SRC_PATH}/common/policy/PkgInfo.cpp
+    ${ASKUSER_SRC_PATH}/common/policy/Policy.cpp
+    ${ASKUSER_SRC_PATH}/common/policy/PrivilegeInfo.cpp
+    ${ASKUSER_SRC_PATH}/common/policy/PrivilegePolicy.cpp
+    ${ASKUSER_TESTS_PATH}/colour_log_formatter.cpp
+    ${ASKUSER_TESTS_PATH}/main.cpp
+    )
+
+ADD_EXECUTABLE(${TARGET_ASKUSER_UNIT_TESTS} ${TESTS_SOURCES})
+
+TARGET_LINK_LIBRARIES(${TARGET_ASKUSER_UNIT_TESTS}
+    ${ASKUSER_UNIT_TESTS_DEP_LIBRARIES}
+    ${Boost_LIBRARIES}
+)
+
+INSTALL(TARGETS ${TARGET_ASKUSER_UNIT_TESTS} DESTINATION bin)
diff --git a/test/unit-tests/colour_log_formatter.cpp b/test/unit-tests/colour_log_formatter.cpp
new file mode 100644 (file)
index 0000000..d56df20
--- /dev/null
@@ -0,0 +1,342 @@
+/*
+ *  (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/impl/execution_monitor.ipp>
+#if BOOST_VERSION >= 105900
+#include <boost/test/tree/test_unit.hpp>
+#else
+#include <boost/test/unit_test_suite_impl.hpp>
+#endif
+#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>
+
+// copied form DPL
+const char* GREEN_BEGIN = "\033[0;32m";
+const char* RED_BEGIN = "\033[0;31m";
+const char* CYAN_BEGIN = "\033[0;36m";
+const char* BOLD_YELLOW_BEGIN = "\033[1;33m";
+const char* COLOR_END = "\033[m";
+
+// ************************************************************************** //
+// **************            colour_log_formatter            ************** //
+// ************************************************************************** //
+
+using namespace boost::unit_test;
+namespace Askuser {
+
+namespace {
+
+const_string
+test_unit_type_name(const test_unit &tu)
+{
+#if BOOST_VERSION >= 105900
+    return const_string(tu.p_type_name);
+#else
+    return tu.p_type_name.get();
+#endif
+}
+
+const_string
+test_unit_name(const test_unit &tu)
+{
+#if BOOST_VERSION >= 105900
+    return const_string(tu.p_name);
+#else
+    return tu.p_name.get();
+#endif
+}
+
+const_string
+test_phase_identifier()
+{
+    return test_unit_name(framework::current_test_case());
+}
+
+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());
+}
+
+bool
+test_unit_type_name_contains(const test_unit &tu, const std::string &substr)
+{
+    return test_unit_type_name(tu).find(const_string(substr)) == 0;
+}
+
+} // 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, bool log_build_info)
+{
+    if (log_build_info)
+        output  << "Platform: " << BOOST_PLATFORM            << '\n'
+                << "Compiler: " << BOOST_COMPILER            << '\n'
+                << "STL     : " << BOOST_STDLIB              << '\n';
+    output  << "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 (test_unit_type_name_contains(tu, "suite")) {
+        output << "Starting test ";
+    } else {
+        output << "Running test ";
+    }
+    output << test_unit_type_name(tu) << " \"" << test_unit_name(tu)
+           << "\"" << std::endl;
+
+}
+
+//____________________________________________________________________________//
+
+void
+colour_log_formatter::test_unit_finish(
+    std::ostream &output,
+    test_unit const &tu,
+    unsigned long elapsed)
+{
+    if (test_unit_type_name_contains(tu, "suite")) {
+        output << "Finished test " << test_unit_type_name(tu) << " \"" << test_unit_name(tu) << "\"" <<
+               std::endl;
+        return;
+    }
+
+    std::string color = GREEN_BEGIN;
+    std::string status = "OK";
+
+    if (m_isTestCaseFailed) {
+        color = RED_BEGIN;
+        status = "FAIL";
+    }
+
+    output << "\t" << "[   " << color << status << COLOR_END <<
+           "   ]";
+
+
+    output << ", " << CYAN_BEGIN << "time: ";
+
+    if (elapsed > 0) {
+        if (elapsed % 1000 == 0)
+            output << elapsed / 1000 << "ms";
+        else
+            output << elapsed << "mks";
+    } else {
+        output << "N/A";
+    }
+
+    output << COLOR_END << std::endl;
+    m_isTestCaseFailed = false;
+}
+
+//____________________________________________________________________________//
+
+void
+colour_log_formatter::test_unit_skipped(
+    std::ostream &output,
+    test_unit const &tu)
+{
+    output  << "Test " << test_unit_type_name(tu) << " \"" << test_unit_name(tu) << "\"" <<
+            "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' << 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 << 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' << 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' << 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 << 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;
+}
+
+//____________________________________________________________________________//
+
+#if BOOST_VERSION >= 106501
+void colour_log_formatter::log_exception_start(std::ostream& os, boost::unit_test::log_checkpoint_data const& lcd, boost::execution_exception const& ex)
+{
+    log_exception(os, lcd, ex);
+}
+
+void colour_log_formatter::log_exception_finish(std::ostream& os)
+{
+    (void)os;
+}
+
+void colour_log_formatter::entry_context_start(std::ostream& os, boost::unit_test::log_level l)
+{
+    (void)os;
+    (void)l;
+}
+
+void colour_log_formatter::log_entry_context(std::ostream& os, boost::unit_test::log_level l, boost::unit_test::const_string value)
+{
+    (void)os;
+    (void)l;
+    (void)value;
+}
+
+void colour_log_formatter::entry_context_finish(std::ostream& os, boost::unit_test::log_level l)
+{
+    (void)os;
+    (void)l;
+}
+#endif
+
+//____________________________________________________________________________//
+
+} // namespace Askuser
+
+//____________________________________________________________________________//
+
diff --git a/test/unit-tests/colour_log_formatter.h b/test/unit-tests/colour_log_formatter.h
new file mode 100644 (file)
index 0000000..f89ff98
--- /dev/null
@@ -0,0 +1,68 @@
+/*
+ *  (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 Askuser {
+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 &output, bool log_build_info = true);
+
+    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 &);
+
+#if BOOST_VERSION >= 106501
+    void log_exception_start(std::ostream& os, boost::unit_test::log_checkpoint_data const& lcd, boost::execution_exception const& ex);
+    void log_exception_finish(std::ostream& os);
+    void entry_context_start(std::ostream& os, boost::unit_test::log_level l);
+    void log_entry_context(std::ostream& os, boost::unit_test::log_level l, boost::unit_test::const_string value);
+    void entry_context_finish(std::ostream& os, boost::unit_test::log_level l);
+#endif
+
+private:
+    bool m_isTestCaseFailed;
+};
+} // namespace Askuser
diff --git a/test/unit-tests/main.cpp b/test/unit-tests/main.cpp
new file mode 100644 (file)
index 0000000..f17b390
--- /dev/null
@@ -0,0 +1,41 @@
+/*
+ *  Copyright (c) 2020 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       main.cpp
+ * @author     Pawel Kowalski (p.kowalski2@partner.samsung.com)
+ */
+
+#define BOOST_TEST_MODULE AskuserUnitTests
+
+#include <boost/test/unit_test.hpp>
+#include <boost/test/unit_test_log.hpp>
+#include <boost/test/results_reporter.hpp>
+#include "colour_log_formatter.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 Askuser::colour_log_formatter);
+    }
+    ~TestConfig()
+    {
+    }
+};
+
+BOOST_GLOBAL_FIXTURE(TestConfig);