From 7f6ab54bc32bd4f3372bbbb9f2391dca12876621 Mon Sep 17 00:00:00 2001 From: Ji-hoon Lee Date: Mon, 6 Sep 2021 21:04:50 +0900 Subject: [PATCH] Add tests for wakeup-policy-default These new tests shows that the existing WakeupPolicyDefault store the string values using shallow copy method and thus accessing the string value on timer expired event might cause unexpected behavior. [ RUN ] DefaultFixture.WakeupEventInfoPreservedForStringLiterals [ OK ] DefaultFixture.WakeupEventInfoPreservedForStringLiterals (1 ms) [ RUN ] DefaultFixture.WakeupEventInfoPreservedForDynamicallyAllocatedStrings /home/abuild/ttpo/tests/test_main.cpp:128: Failure Expected equality of these values: mWakeupAppID.compare(appid) Which is: -5 0 [ FAILED ] DefaultFixture.WakeupEventInfoPreservedForDynamicallyAllocatedStrings (1 ms) Change-Id: I3fe82b3269cff96e74de1bbf86e0c2a9ffba2fc2 --- tests/utc/CMakeLists.txt | 1 + tests/utc/wakeup-policy-default/CMakeLists.txt | 78 ++++++++++++++ tests/utc/wakeup-policy-default/test_main.cpp | 136 +++++++++++++++++++++++++ 3 files changed, 215 insertions(+) create mode 100644 tests/utc/wakeup-policy-default/CMakeLists.txt create mode 100644 tests/utc/wakeup-policy-default/test_main.cpp diff --git a/tests/utc/CMakeLists.txt b/tests/utc/CMakeLists.txt index e35099e..33c0a69 100644 --- a/tests/utc/CMakeLists.txt +++ b/tests/utc/CMakeLists.txt @@ -6,3 +6,4 @@ ADD_SUBDIRECTORY(audio-manager) ADD_SUBDIRECTORY(service-main) ADD_SUBDIRECTORY(preference-manager-vconf) ADD_SUBDIRECTORY(package-update-monitor) +ADD_SUBDIRECTORY(wakeup-policy-default) diff --git a/tests/utc/wakeup-policy-default/CMakeLists.txt b/tests/utc/wakeup-policy-default/CMakeLists.txt new file mode 100644 index 0000000..e05d076 --- /dev/null +++ b/tests/utc/wakeup-policy-default/CMakeLists.txt @@ -0,0 +1,78 @@ +# THIS FILE WAS GENERATED BY THE TIZEN TDD-PROJECT OUTLINER. +# IT IS NOT RECOMMENDED TO MODIFY THIS FILE SINCE THE +# MODIFICATION CAN BE OVERWRITTEN BY THE GENERATION TOOL. + +CMAKE_MINIMUM_REQUIRED(VERSION 2.6) +PROJECT(ttpo-project CXX C) + +LINK_DIRECTORIES(${CMAKE_BINARY_DIR}) +INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}) + +SET(TTPO_SRCS + test_main.cpp + ../../../plugins/wakeup-manager/src/wakeup_policy.cpp + ../../../plugins/wakeup-manager/src/wakeup_policy_default.cpp +) + +SET(TTPO_PKGS + ecore + dlog +) + +SET(TTPO_INCLUDES + ../../../plugins/wakeup-manager/inc/ +) + +SET(TTPO_CFLAGS +) + +SET(TTPO_WRAPPERS + "-Wl,\ +--wrap=ecore_main_loop_thread_safe_call_async" +) + +SET(TTPO_LDFLAGS +) + +SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -std=c++14") +SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -g") +SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -O0") +SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${EXTRA_CFLAGS}") +SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TTPO_CFLAGS}") +SET(EXTRA_LDFLAGS "${EXTRA_LDFLAGS} ${TTPO_LDFLAGS}") + +# Find Packages +INCLUDE(FindPkgConfig) +pkg_check_modules(pkgs REQUIRED + ${TTPO_PKGS} +) + +INCLUDE_DIRECTORIES(${TTPO_INCLUDES}) + +FOREACH(flag ${pkgs_CFLAGS}) + SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${flag}") +ENDFOREACH(flag) + +FIND_PACKAGE(GTest REQUIRED) +INCLUDE_DIRECTORIES(${GTEST_INCLUDE_DIRS}) +LINK_DIRECTORIES(${GTEST_LIBRARY_DIRS}) + +SET(TTPO_EXECUTABLE test-wakeup-policy-default) +ADD_EXECUTABLE( + ${TTPO_EXECUTABLE} + ${TTPO_SRCS} +) + +TARGET_LINK_LIBRARIES(${TTPO_EXECUTABLE} ${pkgs_LDFLAGS}) +if(NOT "${EXTRA_LDFLAGS}" STREQUAL " ") + TARGET_LINK_LIBRARIES(${TTPO_EXECUTABLE} ${EXTRA_LDFLAGS}) +endif() +TARGET_LINK_LIBRARIES(${TTPO_EXECUTABLE} ${GTEST_LIBRARIES}) + +SET_TARGET_PROPERTIES(${TTPO_EXECUTABLE} PROPERTIES + COMPILE_FLAGS "-fPIE" + LINK_FLAGS "${TTPO_WRAPPERS}") +ADD_TEST(NAME ${TTPO_EXECUTABLE} COMMAND ${TTPO_EXECUTABLE}) + +# REPLACE THE TARGET INSTALL DESTINATION AS NEEDED (default : bin) +INSTALL(TARGETS ${TTPO_EXECUTABLE} DESTINATION bin) diff --git a/tests/utc/wakeup-policy-default/test_main.cpp b/tests/utc/wakeup-policy-default/test_main.cpp new file mode 100644 index 0000000..c8892b5 --- /dev/null +++ b/tests/utc/wakeup-policy-default/test_main.cpp @@ -0,0 +1,136 @@ +#include +#include + +#include + +#include "wakeup_policy.h" +#include "wakeup_policy_default.h" + +extern "C" { +/* Add wrapper functions here with __wrap_ and __real prefix +Example : + +int __real_vconf_get_int(const char *in_key, int *intval); +int __wrap_vconf_get_int(const char *in_key, int *intval) +{ + if (strcmp(in_key, "TestKey") == 0) + return 0; + + return __real_vconf_get_int(in_key, intval); +} +*/ + +/* The wakeup_candidate() function of CWakeupPolicyDefault tries to call + * ecore_main_loop_thread_safe_call_async() function, which will cause the + * test to hang since no ecore main loop is available. For this reason, + * we provide a mock function that does not do anything but returning. */ +void __real_ecore_main_loop_thread_safe_call_async(Ecore_Cb callback, void *data); +void __wrap_ecore_main_loop_thread_safe_call_async(Ecore_Cb callback, void *data) +{ +} + +} + +using namespace multiassistant::wakeup; + +class PolicyEventObserver : public IPolicyEventObserver +{ + void on_wakeup(mas_wakeup_event_info wakeup_info); +}; + +class DefaultFixture : public testing::Test +{ +public: + DefaultFixture() { + } + virtual ~DefaultFixture() { + } + void SetUp() override { + eina_init(); + mWakeupAppID.clear(); + } + void TearDown() override { + eina_shutdown(); + } + + PolicyEventObserver mObserver; + CWakeupPolicyDefault policy{&mObserver}; + std::string mWakeupAppID; +}; + +void PolicyEventObserver::on_wakeup(mas_wakeup_event_info wakeup_info) { + if (wakeup_info.extra_data) { + DefaultFixture* fixture = (DefaultFixture*)(wakeup_info.extra_data); + fixture->mWakeupAppID = wakeup_info.wakeup_appid; + LOGD("wakeup_appid of wakeup_info : %p, %s", wakeup_info.wakeup_appid, wakeup_info.wakeup_appid); + } +} + +void reset_wakeup_event_info(mas_wakeup_event_info *info) +{ + if (info == nullptr) return; + + info->wakeup_appid = nullptr; + info->wakeup_word = nullptr; + info->wakeup_language = nullptr; + info->wakeup_voice_id = nullptr; + info->wakeup_engine = nullptr; + info->wakeup_confidence_score = 0.0f; + + info->wakeup_start_time = 0; + info->wakeup_end_time = 0; + info->wakeup_time_valid = false; + + info->extra_data = nullptr; + info->extra_data_length = 0; + info->extra_data_description = nullptr; +} + +TEST_F(DefaultFixture, WakeupEventInfoPreservedForStringLiterals) { + const char *appid = "APPID"; + + mas_wakeup_event_info info; + reset_wakeup_event_info(&info); + info.wakeup_appid = appid; + + info.extra_data = this; + + LOGD("wakeup_appid of info : %p, %s", info.wakeup_appid, info.wakeup_appid); + + policy.wakeup_candidate(info); + reset_wakeup_event_info(&info); + + LOGD("wakeup_appid of info : %p, %s", info.wakeup_appid, info.wakeup_appid); + + policy.timer_expired(); + + ASSERT_EQ(mWakeupAppID.compare(appid), 0); +} + +TEST_F(DefaultFixture, WakeupEventInfoPreservedForDynamicallyAllocatedStrings) { + const char *appid = "APPID"; + + mas_wakeup_event_info info; + reset_wakeup_event_info(&info); + info.wakeup_appid = strdup(appid); + info.extra_data = this; + + LOGD("wakeup_appid of info : %p, %s", info.wakeup_appid, info.wakeup_appid); + + policy.wakeup_candidate(info); + + free((void*)info.wakeup_appid); + info.wakeup_appid = nullptr; + LOGD("wakeup_appid of info : %p, %s", info.wakeup_appid, info.wakeup_appid); + + policy.timer_expired(); + + ASSERT_EQ(mWakeupAppID.compare(appid), 0); +} + +int main(int argc, char** argv) { + std::cout << "Starting tests" << std::endl; + testing::InitGoogleTest(&argc, argv); + int ret = RUN_ALL_TESTS(); + return ret; +} -- 2.7.4