diff options
author | Janusz Kozerski <j.kozerski@samsung.com> | 2015-05-28 10:41:03 +0200 |
---|---|---|
committer | Janusz Kozerski <j.kozerski@samsung.com> | 2015-06-26 12:36:50 +0200 |
commit | a282a131cd809c3f425a44f42e30dfb902bca76e (patch) | |
tree | 5542ae81ed7f55803577beed89c91004216c267a /tests | |
parent | 0cad75f144c2ef178d2bc7cced6cf4c4b0e57409 (diff) | |
download | cert-checker-a282a131cd809c3f425a44f42e30dfb902bca76e.tar.gz cert-checker-a282a131cd809c3f425a44f42e30dfb902bca76e.tar.bz2 cert-checker-a282a131cd809c3f425a44f42e30dfb902bca76e.zip |
Add cert-checker database (+tests). Change include files path.
* Add m_buffer in Logic class.
* Load database into buffer in Logic::setup()
* Move include to separate folder - this solves problem with app.h file
from different package.
[Verification] Run cert-checker-tests. All should pass.
Change-Id: I809951a17fd44d5258ad91e480b6914e92d1058b
Diffstat (limited to 'tests')
-rw-r--r-- | tests/CMakeLists.txt | 64 | ||||
-rw-r--r-- | tests/colour_log_formatter.cpp | 238 | ||||
-rw-r--r-- | tests/colour_log_formatter.h | 49 | ||||
-rw-r--r-- | tests/dbfixture.cpp | 61 | ||||
-rw-r--r-- | tests/dbfixture.h | 38 | ||||
-rw-r--r-- | tests/main.cpp | 39 | ||||
-rw-r--r-- | tests/test_db.cpp | 146 |
7 files changed, 635 insertions, 0 deletions
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt new file mode 100644 index 0000000..4e2add7 --- /dev/null +++ b/tests/CMakeLists.txt @@ -0,0 +1,64 @@ +PKG_CHECK_MODULES(CERT_CHECKER_TESTS_DEP + REQUIRED + dbus-1 + dbus-glib-1 + db-util + glib-2.0 + gio-2.0 + icu-i18n + capi-appfw-package-manager + notification + libsystemd-journal + libtzplatform-config + sqlite3 + ) + +FIND_PACKAGE(Threads REQUIRED) +ADD_DEFINITIONS( "-DBOOST_TEST_DYN_LINK" ) + +SET(CERT_CHECKER_SRC_PATH ${PROJECT_SOURCE_DIR}/src) +SET(CERT_CHECKER_TESTS_SRC_PATH ${PROJECT_SOURCE_DIR}/tests) + +SET(CERT_CHECKER_TESTS_SOURCES + # tests + ${CERT_CHECKER_TESTS_SRC_PATH}/main.cpp + ${CERT_CHECKER_TESTS_SRC_PATH}/dbfixture.cpp + ${CERT_CHECKER_TESTS_SRC_PATH}/colour_log_formatter.cpp + ${CERT_CHECKER_TESTS_SRC_PATH}/test_db.cpp + # cert-checker + ${CERT_CHECKER_SRC_PATH}/app.cpp + # logs + ${CERT_CHECKER_SRC_PATH}/log/log.cpp + # dpl + ${CERT_CHECKER_SRC_PATH}/dpl/core/src/assert.cpp + ${CERT_CHECKER_SRC_PATH}/dpl/core/src/char_traits.cpp + ${CERT_CHECKER_SRC_PATH}/dpl/core/src/colors.cpp + ${CERT_CHECKER_SRC_PATH}/dpl/core/src/errno_string.cpp + ${CERT_CHECKER_SRC_PATH}/dpl/core/src/exception.cpp + ${CERT_CHECKER_SRC_PATH}/dpl/core/src/noncopyable.cpp + ${CERT_CHECKER_SRC_PATH}/dpl/core/src/string.cpp + # dpl DB + ${CERT_CHECKER_SRC_PATH}/dpl/db/src/sql_connection.cpp + ${CERT_CHECKER_SRC_PATH}/dpl/db/src/naive_synchronization_object.cpp + # DB + ${CERT_CHECKER_SRC_PATH}/db/sql_query.cpp + ) + +INCLUDE_DIRECTORIES(SYSTEM + ${CERT_CHECKER_DEP_INCLUDE_DIRS} + ${CERT_CHECKER_SRC_PATH}/include/ + ${CERT_CHECKER_SRC_PATH}/dpl/core/include/ + ${CERT_CHECKER_SRC_PATH}/dpl/db/include/ + ${CERT_CHECKER_TESTS_SRC_PATH}/ + ) + +ADD_EXECUTABLE(${TARGET_CERT_CHECKER_TESTS} ${CERT_CHECKER_TESTS_SOURCES}) + +TARGET_LINK_LIBRARIES(${TARGET_CERT_CHECKER_TESTS} + ${CERT_CHECKER_TESTS_DEP_LIBRARIES} + ${CMAKE_THREAD_LIBS_INIT} + boost_unit_test_framework + -ldl + ) + +INSTALL(TARGETS ${TARGET_CERT_CHECKER_TESTS} DESTINATION ${BINDIR}) diff --git a/tests/colour_log_formatter.cpp b/tests/colour_log_formatter.cpp new file mode 100644 index 0000000..8b71d99 --- /dev/null +++ b/tests/colour_log_formatter.cpp @@ -0,0 +1,238 @@ +// 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 CCHECKER { + +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 = CCHECKER::Colors::Text::GREEN_BEGIN; + std::string status = "OK"; + if (m_isTestCaseFailed) { + color = CCHECKER::Colors::Text::RED_BEGIN; + status = "FAIL"; + } + output << "\t" << "[ " << color << status << CCHECKER::Colors::Text::COLOR_END << " ]"; + + + output << ", " << CCHECKER::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 << CCHECKER::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' << CCHECKER::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 << CCHECKER::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' << CCHECKER::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' << CCHECKER::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 << CCHECKER::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 CCHECKER + +//____________________________________________________________________________// + diff --git a/tests/colour_log_formatter.h b/tests/colour_log_formatter.h new file mode 100644 index 0000000..dd6028b --- /dev/null +++ b/tests/colour_log_formatter.h @@ -0,0 +1,49 @@ +#ifndef COLOUR_LOG_FORMATTER_H_ +#define COLOUR_LOG_FORMATTER_H_ + +#include <boost/test/unit_test_log_formatter.hpp> + +namespace CCHECKER { +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 CCHECKER + +#endif /* COLOUR_LOG_FORMATTER_H_ */ diff --git a/tests/dbfixture.cpp b/tests/dbfixture.cpp new file mode 100644 index 0000000..2fb73b5 --- /dev/null +++ b/tests/dbfixture.cpp @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2011 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 dbfixture.cpp + * @author Janusz Kozerski (j.kozerski@samsung.com) + * @version 1.0 + * @brief Implementation of DB test class + */ +#include <tzplatform_config.h> + +#include <dbfixture.h> + +using namespace CCHECKER; + +namespace { +const char *TEST_DB_PATH = tzplatform_mkpath(TZ_SYS_DB, ".cert-checker-test.db"); + +const char *DB_CMD_CLEAR_URL = + "DELETE FROM ocsp_urls;"; + +const char *DB_CMD_CLEAR_TO_CHECK = + "DELETE FROM to_check;"; + +const char *DB_CMD_CLEAR_CERTS_TO_CHECK = + "DELETE FROM certs_to_check;"; +} // anonymus namespace + +DBFixture::DBFixture() : + DB::SqlQuery(TEST_DB_PATH) +{}; + +DBFixture::~DBFixture() +{}; + +void DBFixture::clear_database () +{ + // TODO: Restore DB from copy instead of removing all data from it. + + DB::SqlConnection::DataCommandAutoPtr getUrlCommand = + m_connection->PrepareDataCommand(DB_CMD_CLEAR_URL); + getUrlCommand->Step(); + + getUrlCommand = m_connection->PrepareDataCommand(DB_CMD_CLEAR_CERTS_TO_CHECK); + getUrlCommand->Step(); + + getUrlCommand = m_connection->PrepareDataCommand(DB_CMD_CLEAR_TO_CHECK); + getUrlCommand->Step(); +} diff --git a/tests/dbfixture.h b/tests/dbfixture.h new file mode 100644 index 0000000..2b64164 --- /dev/null +++ b/tests/dbfixture.h @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2011 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 dbfixture.h + * @author Janusz Kozerski (j.kozerski@samsung.com) + * @version 1.0 + * @brief Implementation of DB test class + */ + +#include <cchecker/sql_query.h> + +#ifndef CCHECKER_DBFIXTURE_H +#define CCHECKER_DBFIXTURE_H + +using namespace CCHECKER; + +class DBFixture : public DB::SqlQuery { + public: + DBFixture(); + virtual ~DBFixture(); + + void clear_database (); +}; + +#endif //CCHECKER_DBFIXTURE_H diff --git a/tests/main.cpp b/tests/main.cpp new file mode 100644 index 0000000..5ac4ab5 --- /dev/null +++ b/tests/main.cpp @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2000 - 2015 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 Janusz Kozerski (j.kozerski@samsung.com) + * @version 1.0 + */ + +#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 <cchecker/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 CCHECKER::colour_log_formatter); + } +}; + +BOOST_GLOBAL_FIXTURE(TestConfig) + diff --git a/tests/test_db.cpp b/tests/test_db.cpp new file mode 100644 index 0000000..dd5a962 --- /dev/null +++ b/tests/test_db.cpp @@ -0,0 +1,146 @@ +/* + * Copyright (c) 2015 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_db.cpp + * @author Janusz Kozerski (j.kozerski@samsung.com) + * @version 1.0 + * @brief Database tests + */ + +#define BOOST_TEST_MODULE CERT_CHECKER_TESTS +#include <boost/test/unit_test.hpp> +#include <string> + +#include <cchecker/app.h> +#include <cchecker/log.h> +#include <dbfixture.h> + +BOOST_FIXTURE_TEST_SUITE(DB_TEST, DBFixture) + +BOOST_AUTO_TEST_CASE(DB_url) { + std::string url; + std::string url_org = "url://url"; + std::string url_update = "http://issuer"; + std::string url_org2 = "address"; + std::string url_update2 = "random_text"; + std::string url_org3 = "########"; + std::string url_update3 = "@@@"; + + clear_database(); + + // No url in database + BOOST_REQUIRE(get_url("Issuer_test1", url)==false); + BOOST_REQUIRE(get_url("Issuer_test2", url)==false); + BOOST_REQUIRE(get_url("Issuer_test3", url)==false); + + // Url should be added + set_url("Issuer_test1", url_org, 500); + BOOST_REQUIRE(get_url("Issuer_test1", url)==true); + BOOST_REQUIRE(url==url_org); + + // Url for issuer 2 and 3 should remain empty + BOOST_REQUIRE(get_url("Issuer_test2", url)==false); + BOOST_REQUIRE(get_url("Issuer_test3", url)==false); + + // Should NOT be updated. Should get original url. + set_url("Issuer_test1", url_update, 400); + BOOST_REQUIRE(get_url("Issuer_test1", url)==true); + BOOST_REQUIRE(url==url_org); + + // Should be updated. Should get updated url. + set_url("Issuer_test1", url_update, 600); + BOOST_REQUIRE(get_url("Issuer_test1", url)==true); + BOOST_REQUIRE(url==url_update); + + // Add url for new issuer + set_url("Issuer_test2", url_org2, 200); + BOOST_REQUIRE(get_url("Issuer_test2", url)==true); + BOOST_REQUIRE(url==url_org2); + + // Url for issuer 3 should remain empty + BOOST_REQUIRE(get_url("Issuer_test3", url)==false); + + // Add url for issuer 3 + set_url("Issuer_test3", url_org3, 1000); + BOOST_REQUIRE(get_url("Issuer_test3", url)==true); + BOOST_REQUIRE(url==url_org3); + + // Urls for issuer 1 and 2 should remain as they were + BOOST_REQUIRE(get_url("Issuer_test1", url)==true); + BOOST_REQUIRE(url==url_update); + BOOST_REQUIRE(get_url("Issuer_test2", url)==true); + BOOST_REQUIRE(url==url_org2); + + // Update url for issuer 3 + set_url("Issuer_test3", url_update3, 1001); + BOOST_REQUIRE(get_url("Issuer_test3", url)==true); + BOOST_REQUIRE(url==url_update3); + + // Urls for issuer 1 and 2 should remain as they were + BOOST_REQUIRE(get_url("Issuer_test1", url)==true); + BOOST_REQUIRE(url==url_update); + BOOST_REQUIRE(get_url("Issuer_test2", url)==true); + BOOST_REQUIRE(url==url_org2); +} + +BOOST_AUTO_TEST_CASE(DB_app) { + clear_database(); + + std::list<app_t> buffer; + app_t app1("app_1", "pkg_1", 5001, {}); + app_t app2("app_2", "pkg 2", 5002, {"cert_2"}); + app_t app2r("app_2_remove", "pkg 2", 5002, {"cert_2"}); + app_t app3("app 3", "pkg 3", 5003, {"cert_3.1", "cert 3.2"}); + app_t app4("app 4", "pkg 4", 5004, {"cert_4.1", "cert 4.2", "cert 4.3"}); + + BOOST_REQUIRE(add_app_to_check_list(app1)==true); + BOOST_REQUIRE(add_app_to_check_list(app2)==true); + BOOST_REQUIRE(add_app_to_check_list(app2r)==true); + BOOST_REQUIRE(add_app_to_check_list(app3)==true); + BOOST_REQUIRE(add_app_to_check_list(app4)==true); + + mark_as_verified(app2, app_t::verified_t::NO); + mark_as_verified(app3, app_t::verified_t::YES); + remove_app_from_check_list(app2r); + + app2.verified = app_t::verified_t::NO; + app3.verified = app_t::verified_t::YES; + std::list<app_t> buffer_ok = {app1, app2, app3, app4}; + + get_app_list(buffer); + + std::list<app_t>::iterator iter = buffer.begin(); + std::list<app_t>::iterator iter_ok = buffer_ok.begin(); + for (; iter!=buffer.end(); iter++) { + bool is_ok = false; + for (iter_ok = buffer_ok.begin(); iter_ok!=buffer_ok.end(); iter_ok++) { + if (iter->app_id == iter_ok->app_id && + iter->pkg_id == iter_ok->pkg_id && + iter->uid == iter_ok->uid && + iter->certificates == iter_ok->certificates && + iter->verified == iter_ok->verified) { + // check_id field is created by database and can be ignored + LogDebug(iter->str() << " has been found"); + is_ok = true; + buffer_ok.erase(iter_ok); + break; + } + } + BOOST_REQUIRE(is_ok == true); + } +} + +BOOST_AUTO_TEST_SUITE_END() |