From 71273c750fb752eed42ee0cb9085255573034751 Mon Sep 17 00:00:00 2001 From: Tomasz Iwanek Date: Mon, 23 Mar 2015 11:05:19 +0100 Subject: [PATCH] [Unittest] service_handler_unittests This commit removes checks on GetManifestData() as it ws used for multithreading purpose in crosswalk. Change-Id: I2c3a19f834bb2af02f9319008ed813d427b2460c --- CMakeLists.txt | 1 + src/parser/application_data.cc | 4 - src/parser/application_data.h | 8 +- src/parser/manifest_util.cc | 4 +- src/unit_tests/CMakeLists.txt | 8 +- src/unit_tests/common/CMakeLists.txt | 15 ++ .../scoped_testing_manifest_handler_registry.cc | 25 +++ .../scoped_testing_manifest_handler_registry.h | 30 +++ src/unit_tests/service_handler_unittest.cc | 236 +++++++++++++++++++++ ...et_manifest_parser_manifest_handler_unittest.cc | 18 +- 10 files changed, 321 insertions(+), 28 deletions(-) create mode 100644 src/unit_tests/common/CMakeLists.txt create mode 100644 src/unit_tests/common/scoped_testing_manifest_handler_registry.cc create mode 100644 src/unit_tests/common/scoped_testing_manifest_handler_registry.h create mode 100644 src/unit_tests/service_handler_unittest.cc diff --git a/CMakeLists.txt b/CMakeLists.txt index 256b275..3de066d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -30,6 +30,7 @@ SET(TARGET_WGT_BACKEND "wgt-backend") SET(TARGET_XPK_BACKEND "xpk-backend") SET(TARGET_NATIVE_BACKEND "native-backend") SET(TARGET_TPK_BACKEND "tpk-backend") +SET(TARGET_UNITTEST_COMMON "common-unittests") ADD_DEFINITIONS("-Wall") ADD_DEFINITIONS("-Wextra") diff --git a/src/parser/application_data.cc b/src/parser/application_data.cc index 75e9924..ec568d1 100644 --- a/src/parser/application_data.cc +++ b/src/parser/application_data.cc @@ -82,7 +82,6 @@ std::string ApplicationData::GetBaseURLFromApplicationId( ApplicationData::ManifestData* ApplicationData::GetManifestData( const std::string& key) const { - assert(finished_parsing_manifest_); ManifestDataMap::const_iterator iter = manifest_data_.find(key); if (iter != manifest_data_.end()) return iter->second.get(); @@ -91,7 +90,6 @@ ApplicationData::ManifestData* ApplicationData::GetManifestData( void ApplicationData::SetManifestData(const std::string& key, std::shared_ptr data) { - assert(!finished_parsing_manifest_); manifest_data_[key] = data; } @@ -121,7 +119,6 @@ ApplicationData::ApplicationData(const bf::path& path, : manifest_version_(0), path_(path), manifest_(std::move(manifest)), - finished_parsing_manifest_(false), source_type_(source_type) { assert(path_.empty() || path_.is_absolute()); } @@ -138,7 +135,6 @@ bool ApplicationData::Init(const std::string& explicit_id, return false; application_url_ = ApplicationData::GetBaseURLFromApplicationId(ID()); - finished_parsing_manifest_ = true; return true; } diff --git a/src/parser/application_data.h b/src/parser/application_data.h index 929a899..e01e6d1 100644 --- a/src/parser/application_data.h +++ b/src/parser/application_data.h @@ -160,13 +160,13 @@ class ApplicationData : public std::enable_shared_from_this { // Stored parsed manifest data. ManifestDataMap manifest_data_; - // Set to true at the end of InitValue when initialization is finished. - bool finished_parsing_manifest_; - // The source the application was loaded from. SourceType source_type_; -// DISALLOW_COPY_AND_ASSIGN(ApplicationData); + friend std::shared_ptr CreateApplicationDataForTest( + std::unique_ptr manifest); + + DISALLOW_COPY_AND_ASSIGN(ApplicationData); }; } // namespace parser diff --git a/src/parser/manifest_util.cc b/src/parser/manifest_util.cc index beec06d..626b88a 100644 --- a/src/parser/manifest_util.cc +++ b/src/parser/manifest_util.cc @@ -57,8 +57,8 @@ const char* kSingletonElements[] = { "content" }; -const char kIdPattern[] = "\\A[0-9a-zA-Z]{10}[.][0-9a-zA-Z]{1,52}\\z"; -const char kPackagePattern[] = "\\A[0-9a-zA-Z]{10}\\z"; +const char kIdPattern[] = "^[0-9a-zA-Z]{10}[.][0-9a-zA-Z]{1,52}$"; +const char kPackagePattern[] = "^[0-9a-zA-Z]{10}$"; } // namespace diff --git a/src/unit_tests/CMakeLists.txt b/src/unit_tests/CMakeLists.txt index 9b46d00..bc88825 100644 --- a/src/unit_tests/CMakeLists.txt +++ b/src/unit_tests/CMakeLists.txt @@ -1,5 +1,7 @@ SET(PRJ_PATH ..) +ADD_SUBDIRECTORY("common") + SET(TESTS values_unittest manifest_unittest @@ -8,6 +10,7 @@ SET(TESTS string_util_unittest widget_manifest_parser_unittest xml_parser_unittest + service_handler_unittest ) INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/../) @@ -26,6 +29,8 @@ ADD_EXECUTABLE(widget_manifest_parser_unittest widget_manifest_parser_unittest.cc) ADD_EXECUTABLE(xml_parser_unittest xml_parser_unittest.cc ../xml_parser/xml_parser.cc) +ADD_EXECUTABLE(service_handler_unittest + service_handler_unittest.cc) INSTALL(DIRECTORY test_samples/ DESTINATION ${SHAREDIR}/app-installers-ut/test_samples) @@ -45,11 +50,12 @@ ENDFOREACH(test) # GTEST_MAIN_LIBRARIES is needed. target_link_libraries(values_unittest PUBLIC ${TARGET_LIBNAME_UTILS} ${GTEST_MAIN_LIBRARIES}) target_link_libraries(manifest_unittest PUBLIC ${TARGET_LIBNAME_WIDGET_MANIFEST_PARSER} ${GTEST_MAIN_LIBRARIES}) -target_link_libraries(manifest_handler_unittest PUBLIC ${TARGET_LIBNAME_WIDGET_MANIFEST_PARSER} ${GTEST_MAIN_LIBRARIES}) +target_link_libraries(manifest_handler_unittest PUBLIC ${GTEST_MAIN_LIBRARIES} ${TARGET_UNITTEST_COMMON}) target_link_libraries(manifest_util_unittest PUBLIC ${TARGET_LIBNAME_WIDGET_MANIFEST_PARSER} ${GTEST_MAIN_LIBRARIES}) target_link_libraries(string_util_unittest PUBLIC ${TARGET_LIBNAME_UTILS} ${GTEST_MAIN_LIBRARIES}) target_link_libraries(widget_manifest_parser_unittest PUBLIC ${TARGET_LIBNAME_WIDGET_MANIFEST_PARSER} ${GTEST_MAIN_LIBRARIES}) target_link_libraries(xml_parser_unittest PUBLIC ${TARGET_LIBNAME_XML_PARSER} ${GTEST_MAIN_LIBRARIES}) +target_link_libraries(service_handler_unittest PUBLIC ${GTEST_MAIN_LIBRARIES} ${TARGET_UNITTEST_COMMON}) FOREACH(test ${TESTS}) INSTALL(TARGETS ${test} DESTINATION ${BINDIR}/app-installers-ut) diff --git a/src/unit_tests/common/CMakeLists.txt b/src/unit_tests/common/CMakeLists.txt new file mode 100644 index 0000000..49a3fc9 --- /dev/null +++ b/src/unit_tests/common/CMakeLists.txt @@ -0,0 +1,15 @@ +# Target - sources +SET(SRCS + scoped_testing_manifest_handler_registry.cc +) +# Target - definition +ADD_LIBRARY(${TARGET_UNITTEST_COMMON} STATIC ${SRCS}) +# Target - includes +TARGET_INCLUDE_DIRECTORIES(${TARGET_UNITTEST_COMMON} PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/../../") + +# Target in-package deps +TARGET_LINK_LIBRARIES(${TARGET_UNITTEST_COMMON} PUBLIC ${TARGET_LIBNAME_WIDGET_MANIFEST_PARSER}) + +# Extra +SET_TARGET_PROPERTIES(${TARGET_UNITTEST_COMMON} PROPERTIES VERSION ${VERSION}) +SET_TARGET_PROPERTIES(${TARGET_UNITTEST_COMMON} PROPERTIES SOVERSION ${VERSION_MAJOR}) diff --git a/src/unit_tests/common/scoped_testing_manifest_handler_registry.cc b/src/unit_tests/common/scoped_testing_manifest_handler_registry.cc new file mode 100644 index 0000000..3467b65 --- /dev/null +++ b/src/unit_tests/common/scoped_testing_manifest_handler_registry.cc @@ -0,0 +1,25 @@ +// Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + + +#include "unit_tests/common/scoped_testing_manifest_handler_registry.h" + +namespace common_installer { + +namespace parser { + +ScopedTestingManifestHandlerRegistry::ScopedTestingManifestHandlerRegistry( + const std::vector& handlers) + : registry_(new ManifestHandlerRegistry(handlers)), + prev_registry_(ManifestHandlerRegistry::GetInstance()) { + ManifestHandlerRegistry::SetInstanceForTesting(registry_); +} + +ScopedTestingManifestHandlerRegistry::~ScopedTestingManifestHandlerRegistry() { + ManifestHandlerRegistry::SetInstanceForTesting(prev_registry_); +} + +} // namespace parser + +} // namespace common_installer diff --git a/src/unit_tests/common/scoped_testing_manifest_handler_registry.h b/src/unit_tests/common/scoped_testing_manifest_handler_registry.h new file mode 100644 index 0000000..4bea82f --- /dev/null +++ b/src/unit_tests/common/scoped_testing_manifest_handler_registry.h @@ -0,0 +1,30 @@ +// Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef UNIT_TESTS_COMMON_SCOPED_TESTING_MANIFEST_HANDLER_REGISTRY_H_ +#define UNIT_TESTS_COMMON_SCOPED_TESTING_MANIFEST_HANDLER_REGISTRY_H_ + +#include + +#include "parser/manifest_handler.h" + +namespace common_installer { + +namespace parser { + +class ScopedTestingManifestHandlerRegistry { + public: + ScopedTestingManifestHandlerRegistry( + const std::vector& handlers); + ~ScopedTestingManifestHandlerRegistry(); + + ManifestHandlerRegistry* registry_; + ManifestHandlerRegistry* prev_registry_; +}; + +} // namespace parser + +} // namespace common_installer + +#endif // UNIT_TESTS_COMMON_SCOPED_TESTING_MANIFEST_HANDLER_REGISTRY_H_ diff --git a/src/unit_tests/service_handler_unittest.cc b/src/unit_tests/service_handler_unittest.cc new file mode 100644 index 0000000..f453a91 --- /dev/null +++ b/src/unit_tests/service_handler_unittest.cc @@ -0,0 +1,236 @@ +// Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved +// Use of this source code is governed by a apache-2.0 license that can be +// found in the LICENSE file. + +#include +#include + +#include + +#include "parser/application_manifest_constants.h" +#include "parser/manifest.h" +#include "parser/manifest_handler.h" +#include "parser/manifest_handlers/service_handler.h" +#include "unit_tests/common/scoped_testing_manifest_handler_registry.h" + +namespace bf = boost::filesystem; + +namespace { + +const char kServiceKey[] = "service"; + +} // namespace + +namespace common_installer { + +namespace parser { + +std::shared_ptr CreateApplicationDataForTest( + std::unique_ptr manifest) { + return std::shared_ptr( + new ApplicationData(bf::path(), ApplicationData::INTERNAL, + std::move(manifest))); +} + +namespace keys = application_widget_keys; + +class ServiceHandlerTest : public testing::Test { + public: + void SetUp() override { + std::vector handlers; + handlers.push_back(new ServiceHandler()); + test_registry_.reset(new ScopedTestingManifestHandlerRegistry(handlers)); + } + protected: + std::unique_ptr test_registry_; +}; + +TEST_F(ServiceHandlerTest, NoServiceEntry) { + // Set test values + std::unique_ptr value(new utils::DictionaryValue()); + std::unique_ptr manifest(new Manifest(std::move(value))); + // Create application data + std::string error; + std::shared_ptr app_data = + CreateApplicationDataForTest(std::move(manifest)); + ManifestHandlerRegistry* registry = ManifestHandlerRegistry::GetInstance(); + // Check correctness + ASSERT_TRUE(!!app_data); + ASSERT_TRUE(registry); + ASSERT_TRUE(registry->ParseAppManifest(app_data, &error)); + ASSERT_TRUE(registry->ValidateAppManifest(app_data, &error)); +} + +TEST_F(ServiceHandlerTest, SingleServiceEntryDefault) { + // Set test values + std::unique_ptr value(new utils::DictionaryValue()); + std::unique_ptr widget(new utils::DictionaryValue()); + std::unique_ptr service(new utils::DictionaryValue()); + service->SetString(keys::kTizenServiceIdKey, "correct001.appId"); + widget->Set(kServiceKey, service.release()); + value->Set(keys::kWidgetKey, widget.release()); + std::unique_ptr manifest(new Manifest(std::move(value))); + // Create application data + std::string error; + std::shared_ptr app_data = + CreateApplicationDataForTest(std::move(manifest)); + ManifestHandlerRegistry* registry = ManifestHandlerRegistry::GetInstance(); + // Check correctness + ASSERT_TRUE(!!app_data); + ASSERT_TRUE(registry); + ASSERT_TRUE(registry->ParseAppManifest(app_data, &error)); + ASSERT_TRUE(registry->ValidateAppManifest(app_data, &error)); + ASSERT_TRUE(app_data->GetManifestData(keys::kTizenServiceKey)); + ServiceList* service_list = + dynamic_cast( + app_data->GetManifestData(keys::kTizenServiceKey)); + ASSERT_TRUE(service_list); + ASSERT_EQ(service_list->services.size(), 1); + ASSERT_EQ(service_list->services[0].id(), "correct001.appId"); + ASSERT_EQ(service_list->services[0].auto_restart(), false); + ASSERT_EQ(service_list->services[0].on_boot(), false); +} + +TEST_F(ServiceHandlerTest, SingleServiceEntryOnBootOn) { + // Set test values + std::unique_ptr value(new utils::DictionaryValue()); + std::unique_ptr widget(new utils::DictionaryValue()); + std::unique_ptr service(new utils::DictionaryValue()); + service->SetString(keys::kTizenServiceIdKey, "correct002.appId"); + service->SetString(keys::kTizenServiceOnBootKey, "true"); + widget->Set(kServiceKey, service.release()); + value->Set(keys::kWidgetKey, widget.release()); + std::unique_ptr manifest(new Manifest(std::move(value))); + // Create application data + std::string error; + std::shared_ptr app_data = + CreateApplicationDataForTest(std::move(manifest)); + ManifestHandlerRegistry* registry = ManifestHandlerRegistry::GetInstance(); + // Check correctness + ASSERT_TRUE(!!app_data); + ASSERT_TRUE(registry); + ASSERT_TRUE(registry->ParseAppManifest(app_data, &error)); + ASSERT_TRUE(registry->ValidateAppManifest(app_data, &error)); + ASSERT_TRUE(app_data->GetManifestData(keys::kTizenServiceKey)); + ServiceList* service_list = + dynamic_cast( + app_data->GetManifestData(keys::kTizenServiceKey)); + ASSERT_TRUE(service_list); + ASSERT_EQ(service_list->services.size(), 1); + ASSERT_EQ(service_list->services[0].id(), "correct002.appId"); + ASSERT_EQ(service_list->services[0].auto_restart(), false); + ASSERT_EQ(service_list->services[0].on_boot(), true); +} + +TEST_F(ServiceHandlerTest, SingleServiceEntryAutoRestartOn) { + // Set test values + std::unique_ptr value(new utils::DictionaryValue()); + std::unique_ptr widget(new utils::DictionaryValue()); + std::unique_ptr service(new utils::DictionaryValue()); + service->SetString(keys::kTizenServiceIdKey, "correct003.appId"); + service->SetString(keys::kTizenServiceOnBootKey, "false"); + service->SetString(keys::kTizenServiceAutoRestartKey, "true"); + widget->Set(kServiceKey, service.release()); + value->Set(keys::kWidgetKey, widget.release()); + std::unique_ptr manifest(new Manifest(std::move(value))); + // Create application data + std::string error; + std::shared_ptr app_data = + CreateApplicationDataForTest(std::move(manifest)); + ManifestHandlerRegistry* registry = ManifestHandlerRegistry::GetInstance(); + // Check correctness + ASSERT_TRUE(!!app_data); + ASSERT_TRUE(registry); + ASSERT_TRUE(registry->ParseAppManifest(app_data, &error)); + ASSERT_TRUE(registry->ValidateAppManifest(app_data, &error)); + ASSERT_TRUE(app_data->GetManifestData(keys::kTizenServiceKey)); + ServiceList* service_list = + dynamic_cast( + app_data->GetManifestData(keys::kTizenServiceKey)); + ASSERT_TRUE(service_list); + ASSERT_EQ(service_list->services.size(), 1); + ASSERT_EQ(service_list->services[0].id(), "correct003.appId"); + ASSERT_EQ(service_list->services[0].auto_restart(), true); + ASSERT_EQ(service_list->services[0].on_boot(), false); +} + +TEST_F(ServiceHandlerTest, SingleServiceEntryWrongId) { + // Set test values + std::unique_ptr value(new utils::DictionaryValue()); + std::unique_ptr widget(new utils::DictionaryValue()); + std::unique_ptr service(new utils::DictionaryValue()); + service->SetString(keys::kTizenServiceIdKey, "wrongid.appId"); + widget->Set(kServiceKey, service.release()); + value->Set(keys::kWidgetKey, widget.release()); + std::unique_ptr manifest(new Manifest(std::move(value))); + // Create application data + std::string error; + std::shared_ptr app_data = + CreateApplicationDataForTest(std::move(manifest)); + ManifestHandlerRegistry* registry = ManifestHandlerRegistry::GetInstance(); + // Check correctness + ASSERT_TRUE(!!app_data); + ASSERT_TRUE(registry); + ASSERT_TRUE(registry->ParseAppManifest(app_data, &error)); + ASSERT_FALSE(registry->ValidateAppManifest(app_data, &error)); +} + +TEST_F(ServiceHandlerTest, SingleServiceEntryIdTypeMismatch) { + // Set test values + std::unique_ptr value(new utils::DictionaryValue()); + std::unique_ptr widget(new utils::DictionaryValue()); + std::unique_ptr service(new utils::DictionaryValue()); + service->SetInteger(keys::kTizenServiceIdKey, 1410); + widget->Set(kServiceKey, service.release()); + value->Set(keys::kWidgetKey, widget.release()); + std::unique_ptr manifest(new Manifest(std::move(value))); + // Create application data + std::string error; + std::shared_ptr app_data = + CreateApplicationDataForTest(std::move(manifest)); + ManifestHandlerRegistry* registry = ManifestHandlerRegistry::GetInstance(); + // Check correctness + ASSERT_TRUE(!!app_data); + ASSERT_TRUE(registry); + ASSERT_FALSE(registry->ParseAppManifest(app_data, &error)); +} + +TEST_F(ServiceHandlerTest, MultipleServiceEntry) { + // Set test values + std::unique_ptr value(new utils::DictionaryValue()); + std::unique_ptr widget(new utils::DictionaryValue()); + std::unique_ptr list(new utils::ListValue()); + std::unique_ptr service1( + new utils::DictionaryValue()); + std::unique_ptr service2( + new utils::DictionaryValue()); + service1->SetString(keys::kTizenServiceIdKey, "correct004.appId"); + service2->SetString(keys::kTizenServiceIdKey, "correct005.appId"); + list->Append(service1.release()); + list->Append(service2.release()); + widget->Set(kServiceKey, list.release()); + value->Set(keys::kWidgetKey, widget.release()); + std::unique_ptr manifest(new Manifest(std::move(value))); + // Create application data + std::string error; + std::shared_ptr app_data = + CreateApplicationDataForTest(std::move(manifest)); + ManifestHandlerRegistry* registry = ManifestHandlerRegistry::GetInstance(); + // Check correctness + ASSERT_TRUE(!!app_data); + ASSERT_TRUE(registry); + ASSERT_TRUE(registry->ParseAppManifest(app_data, &error)); + ASSERT_TRUE(registry->ValidateAppManifest(app_data, &error)); + ASSERT_TRUE(app_data->GetManifestData(keys::kTizenServiceKey)); + ServiceList* service_list = + dynamic_cast( + app_data->GetManifestData(keys::kTizenServiceKey)); + ASSERT_TRUE(service_list); + ASSERT_EQ(service_list->services.size(), 2); + ASSERT_EQ(service_list->services[0].id(), "correct004.appId"); + ASSERT_EQ(service_list->services[1].id(), "correct005.appId"); +} + +} // namespace parser +} // namespace common_installer + diff --git a/src/unit_tests/widget_manifest_parser_manifest_handler_unittest.cc b/src/unit_tests/widget_manifest_parser_manifest_handler_unittest.cc index 7923c9a..5b1beff 100644 --- a/src/unit_tests/widget_manifest_parser_manifest_handler_unittest.cc +++ b/src/unit_tests/widget_manifest_parser_manifest_handler_unittest.cc @@ -12,6 +12,7 @@ #include "utils/values.h" #include "parser/application_data.h" #include "parser/manifest_handler.h" +#include "unit_tests/common/scoped_testing_manifest_handler_registry.h" namespace bf = boost::filesystem; @@ -26,23 +27,6 @@ std::vector SingleKey(const std::string& key) { } // namespace -class ScopedTestingManifestHandlerRegistry { - public: - ScopedTestingManifestHandlerRegistry( - const std::vector& handlers) - : registry_(new ManifestHandlerRegistry(handlers)), - prev_registry_(ManifestHandlerRegistry::GetInstance()) { - ManifestHandlerRegistry::SetInstanceForTesting(registry_); - } - - ~ScopedTestingManifestHandlerRegistry() { - ManifestHandlerRegistry::SetInstanceForTesting(prev_registry_); - } - - ManifestHandlerRegistry* registry_; - ManifestHandlerRegistry* prev_registry_; -}; - class ManifestHandlerTest : public testing::Test { public: class ParsingWatcher { -- 2.7.4