From 8364d27ef96780e19886961d9e73cbe0df50e9a2 Mon Sep 17 00:00:00 2001 From: hyunho Date: Thu, 3 Sep 2020 17:17:59 +0900 Subject: [PATCH] Add gtest Change-Id: I17dc5ab6b9535d3bf00b098f042a18663dfd7280 Signed-off-by: hyunho Signed-off-by: Jusung Son --- CMakeLists.txt | 89 +++++++------------- cmake/Modules/ApplyPkgConfig.cmake | 35 ++++++++ mock/app_common_mock.cc | 24 ++++++ mock/app_common_mock.h | 32 +++++++ mock/aul_mock.cc | 24 ++++++ mock/aul_mock.h | 32 +++++++ mock/gio_mock.cc | 103 +++++++++++++++++++++++ mock/gio_mock.h | 69 +++++++++++++++ mock/mock_hook.h | 42 +++++++++ mock/module_mock.h | 25 ++++++ mock/test_fixture.cc | 21 +++++ mock/test_fixture.h | 53 ++++++++++++ packaging/message-port.spec | 19 ++++- src/CMakeLists.txt | 41 +++++++++ test/CMakeLists.txt | 1 + test/unit_tests/CMakeLists.txt | 27 ++++++ test/unit_tests/main.cc | 25 ++++++ test/unit_tests/test_message_port.cc | 159 +++++++++++++++++++++++++++++++++++ 18 files changed, 763 insertions(+), 58 deletions(-) create mode 100644 cmake/Modules/ApplyPkgConfig.cmake create mode 100644 mock/app_common_mock.cc create mode 100644 mock/app_common_mock.h create mode 100644 mock/aul_mock.cc create mode 100644 mock/aul_mock.h create mode 100644 mock/gio_mock.cc create mode 100644 mock/gio_mock.h create mode 100644 mock/mock_hook.h create mode 100644 mock/module_mock.h create mode 100644 mock/test_fixture.cc create mode 100644 mock/test_fixture.h create mode 100644 src/CMakeLists.txt create mode 100644 test/CMakeLists.txt create mode 100644 test/unit_tests/CMakeLists.txt create mode 100644 test/unit_tests/main.cc create mode 100644 test/unit_tests/test_message_port.cc diff --git a/CMakeLists.txt b/CMakeLists.txt index fe6a1bf..f0fe7b5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,65 +1,40 @@ -PROJECT(message-port C) -SET(this_target message-port) +CMAKE_MINIMUM_REQUIRED(VERSION 2.8.12) -INCLUDE_DIRECTORIES ( - ${CMAKE_SOURCE_DIR}/include - ${CMAKE_SOURCE_DIR}/src/inc - ) +PROJECT(capi-message-port) -SET(${this_target}_requires "dlog bundle glib-2.0 gio-2.0 aul openssl1.1 capi-base-common pkgmgr-info gio-unix-2.0") +SET(PREFIX ${CMAKE_INSTALL_PREFIX}) +SET(BINDIR "${CMAKE_INSTALL_PREFIX}/bin") +SET(LIBDIR ${LIB_INSTALL_DIR}) +SET(INCLUDEDIR "\${prefix}/include") -INCLUDE(FindPkgConfig) -pkg_check_modules(${this_target} REQUIRED ${${this_target}_requires}) - -FOREACH(flag ${${this_target}_CFLAGS}) - SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}") -ENDFOREACH(flag) - -SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -Wall" ) - -## SET C COMPILER FLAGS -SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS} -fPIC -Wall -Werror -fvisibility=hidden") -SET(CMAKE_C_FLAGS_DEBUG "-O0 -g") - -## SET CPP COMPILER FLAGS -SET(CMAKE_CXX_FLAGS "${OSP_DEBUG_FLAGS} ${CMAKE_CXX_FLAGS} ${EXTRA_CFLAGS} ${OSP_COMPILER_FLAGS} -fvisibility=hidden") +## Compiler flags +SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fvisibility=hidden -g -Wall -Werror") -## Create Library -AUX_SOURCE_DIRECTORY(src SOURCES) -ADD_LIBRARY(${this_target} SHARED ${SOURCES}) +## Linker flags +SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--as-needed,--gc-sections -pie") -## SET LINKER FLAGS -SET(CMAKE_SHARED_LINKER_FLAGS "-Wl,--as-needed -Wl,--no-undefined -Wl,--rpath=${LIB_INSTALL_DIR}") +SET(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules/") -TARGET_LINK_LIBRARIES(${this_target} ${${this_target}_LDFLAGS}) +## Targets +SET(TARGET_MESSAGE_PORT "message-port") -SET_TARGET_PROPERTIES(${this_target} - PROPERTIES - VERSION ${FULLVER} - SOVERSION ${MAJORVER} - CLEAN_DIRECT_OUTPUT 1 - ) +ENABLE_TESTING() +SET(TARGET_MESSAGE_PORT_UNIT_TEST "message-port-unit-test") -# pkgconfig file -SET(PC_NAME ${this_target}) -SET(PACKAGE_DESCRIPTION "Message Port internal library") -SET(VERSION ${FULLVER}) -SET(PC_REQUIRED ${${this_target}_requires}) - -## OUTPUT PATHS -SET(LIBRARY_OUTPUT_PATH ${CMAKE_SOURCE_DIR}/cmake_build_tmp/output) -ADD_CUSTOM_COMMAND( - TARGET ${this_target} POST_BUILD - COMMAND ln -s ./libmessage-port.so.${MAJORVER} ${LIBRARY_OUTPUT_PATH}/libcapi-message-port.so.${MAJORVER}) - -CONFIGURE_FILE(${CMAKE_SOURCE_DIR}/${this_target}.pc.in ${CMAKE_SOURCE_DIR}/${this_target}.pc @ONLY) -INSTALL(FILES ${CMAKE_SOURCE_DIR}/${this_target}.pc DESTINATION ${LIB_INSTALL_DIR}/pkgconfig) -CONFIGURE_FILE(${CMAKE_SOURCE_DIR}/capi-${this_target}.pc.in ${CMAKE_SOURCE_DIR}/capi-${this_target}.pc @ONLY) -INSTALL(FILES ${CMAKE_SOURCE_DIR}/capi-${this_target}.pc DESTINATION ${LIB_INSTALL_DIR}/pkgconfig) -CONFIGURE_FILE(${PROJECT_NAME}.conf.in ${PROJECT_NAME}.conf @ONLY) -INSTALL(FILES ${CMAKE_SOURCE_DIR}/${PROJECT_NAME}.conf DESTINATION ${SYSCONF_INSTALL_DIR}/dbus-1/session.d/) - -INSTALL(TARGETS ${this_target} DESTINATION ${LIB_INSTALL_DIR}) - -INSTALL(DIRECTORY ${CMAKE_SOURCE_DIR}/include/ DESTINATION include/appfw FILES_MATCHING PATTERN "*.h") -INSTALL(DIRECTORY ${LIBRARY_OUTPUT_PATH}/ DESTINATION ${LIB_INSTALL_DIR} FILES_MATCHING PATTERN "*.so*") +INCLUDE(FindPkgConfig) +INCLUDE(ApplyPkgConfig) + +## Find all needed packages once +PKG_CHECK_MODULES(BUNDLE_DEPS REQUIRED bundle) +PKG_CHECK_MODULES(DLOG_DEPS REQUIRED dlog) +PKG_CHECK_MODULES(GLIB_DEPS REQUIRED glib-2.0) +PKG_CHECK_MODULES(GIO_DEPS REQUIRED gio-2.0) +PKG_CHECK_MODULES(AUL_DEPS REQUIRED aul) +PKG_CHECK_MODULES(OPENSSL_DEPS REQUIRED openssl1.1) +PKG_CHECK_MODULES(BASE_COMMON_DEPS REQUIRED capi-base-common) +PKG_CHECK_MODULES(PKGMGR_INFO_DEPS REQUIRED pkgmgr-info) +PKG_CHECK_MODULES(GIO_UNIX_DEPS REQUIRED gio-unix-2.0) +PKG_CHECK_MODULES(GMOCK_DEPS REQUIRED gmock) + +ADD_SUBDIRECTORY(src) +ADD_SUBDIRECTORY(test) diff --git a/cmake/Modules/ApplyPkgConfig.cmake b/cmake/Modules/ApplyPkgConfig.cmake new file mode 100644 index 0000000..97679d7 --- /dev/null +++ b/cmake/Modules/ApplyPkgConfig.cmake @@ -0,0 +1,35 @@ +# Copyright (c) 2014 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. + +# +# This function applies external (out of source tree) dependencies +# to given target. Arguments are: +# TARGET - valid cmake target +# PRIVACY - dependency can be inherited by dependent targets or not: +# PUBLIC - this should be used by default, cause compile/link flags passing +# PRIVATE - do not passes any settings to dependent targets, +# may be usefull for static libraries from the inside of the project +# Argument ARGV2 and following are supposed to be names of checked pkg config +# packages. This function will use variables created by check_pkg_modules(). +# - ${DEP_NAME}_LIBRARIES +# - ${DEP_NAME}_INCLUDE_DIRS +# - ${DEP_NAME}_CFLAGS +# +FUNCTION(APPLY_PKG_CONFIG TARGET PRIVACY) + MATH(EXPR DEST_INDEX "${ARGC}-1") + FOREACH(I RANGE 2 ${DEST_INDEX}) + IF(NOT ${ARGV${I}}_FOUND) + MESSAGE(FATAL_ERROR "Not found dependency - ${ARGV${I}}_FOUND") + ENDIF(NOT ${ARGV${I}}_FOUND) + TARGET_LINK_LIBRARIES(${TARGET} ${PRIVACY} "${${ARGV${I}}_LIBRARIES}") + TARGET_INCLUDE_DIRECTORIES(${TARGET} ${PRIVACY} SYSTEM "${${ARGV${I}}_INCLUDE_DIRS}") + STRING(REPLACE ";" " " CFLAGS_STR "${${ARGV${I}}_CFLAGS}") + SET(CFLAGS_LIST ${CFLAGS_STR}) + SEPARATE_ARGUMENTS(CFLAGS_LIST) + FOREACH(OPTION ${CFLAGS_LIST}) + TARGET_COMPILE_OPTIONS(${TARGET} ${PRIVACY} ${OPTION}) + ENDFOREACH(OPTION) + SET_TARGET_PROPERTIES(${TARGET} PROPERTIES SKIP_BUILD_RPATH true) + ENDFOREACH(I RANGE 2 ${DEST_INDEX}) +ENDFUNCTION(APPLY_PKG_CONFIG TARGET PRIVACY) diff --git a/mock/app_common_mock.cc b/mock/app_common_mock.cc new file mode 100644 index 0000000..801c681 --- /dev/null +++ b/mock/app_common_mock.cc @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2020 Samsung Electronics Co., Ltd. + * + * 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. + */ + +#include "app_common_mock.h" + +#include "mock_hook.h" +#include "test_fixture.h" + +extern "C" int app_get_id(char **id) { + return MOCK_HOOK_P1(AppCommonMock, app_get_id, id); +} diff --git a/mock/app_common_mock.h b/mock/app_common_mock.h new file mode 100644 index 0000000..835062d --- /dev/null +++ b/mock/app_common_mock.h @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2020 Samsung Electronics Co., Ltd. + * + * 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. + */ + +#ifndef UNIT_TESTS_MOCK_APP_COMMON_MOCK_H_ +#define UNIT_TESTS_MOCK_APP_COMMON_MOCK_H_ + +#include + +#include "module_mock.h" + +class AppCommonMock : public virtual ModuleMock { + public: + virtual ~AppCommonMock() {} + + MOCK_METHOD1(app_get_id, int (char **id)); +}; + +#endif // UNIT_TESTS_MOCK_APP_COMMON_MOCK_H_ + diff --git a/mock/aul_mock.cc b/mock/aul_mock.cc new file mode 100644 index 0000000..96d66ca --- /dev/null +++ b/mock/aul_mock.cc @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2020 Samsung Electronics Co., Ltd. + * + * 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. + */ + +#include "aul_mock.h" + +#include "mock_hook.h" +#include "test_fixture.h" + +extern "C" int aul_app_get_pkgid_bypid(int pid, char *pkgid, int len) { + return MOCK_HOOK_P3(AulMock, aul_app_get_pkgid_bypid, pid, pkgid, len); +} diff --git a/mock/aul_mock.h b/mock/aul_mock.h new file mode 100644 index 0000000..92b7ad4 --- /dev/null +++ b/mock/aul_mock.h @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2020 Samsung Electronics Co., Ltd. + * + * 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. + */ + +#ifndef UNIT_TESTS_MOCK_AUL_MOCK_H_ +#define UNIT_TESTS_MOCK_AUL_MOCK_H_ + +#include + +#include "module_mock.h" + +class AulMock : public virtual ModuleMock { + public: + virtual ~AulMock() {} + + MOCK_METHOD3(aul_app_get_pkgid_bypid, int (int , char *, int)); +}; + +#endif // UNIT_TESTS_MOCK_AUL_MOCK_H_ + diff --git a/mock/gio_mock.cc b/mock/gio_mock.cc new file mode 100644 index 0000000..4f727e5 --- /dev/null +++ b/mock/gio_mock.cc @@ -0,0 +1,103 @@ +/* + * Copyright (c) 2020 Samsung Electronics Co., Ltd. + * + * 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. + */ + +#include "gio_mock.h" + +#include "mock_hook.h" +#include "test_fixture.h" + +extern "C" GDBusConnection* g_bus_get_sync(GBusType type, + GCancellable* cancellable, GError** error) { + return MOCK_HOOK_P3(GioMock, g_bus_get_sync, type, cancellable, error); +} + +extern "C" GDBusMessage* g_dbus_connection_send_message_with_reply_sync( + GDBusConnection* conn, GDBusMessage* msg, GDBusSendMessageFlags flags, + gint timeout, volatile guint32* out_serial, GCancellable* cancellable, + GError** error) { + return MOCK_HOOK_P7(GioMock, g_dbus_connection_send_message_with_reply_sync, + conn, msg, flags, timeout, out_serial, cancellable, error); +} + +extern "C" GDBusMessage* g_dbus_message_new_method_call(const gchar* arg0, + const gchar* arg1, const gchar* arg2, const gchar* arg3) { + return MOCK_HOOK_P4(GioMock, g_dbus_message_new_method_call, arg0, arg1, arg2, + arg3); +} + +extern "C" void g_dbus_message_set_body(GDBusMessage* arg0, GVariant* arg1) { + return MOCK_HOOK_P2(GioMock, g_dbus_message_set_body, arg0, arg1); +} + +extern "C" GVariant* g_dbus_message_get_body(GDBusMessage* arg0) { + return MOCK_HOOK_P1(GioMock, g_dbus_message_get_body, arg0); +} + +extern "C" gboolean g_dbus_connection_emit_signal(GDBusConnection* arg0, + const gchar* arg1, const gchar* arg2, const gchar* arg3, const gchar* arg4, + GVariant* arg5, GError** arg6) { + return MOCK_HOOK_P7(GioMock, g_dbus_connection_emit_signal, + arg0, arg1, arg2, arg3, arg4, arg5, arg6); +} + +extern "C" GDBusNodeInfo* g_dbus_node_info_new_for_xml( + const gchar* arg0, GError** arg1) { + return MOCK_HOOK_P2(GioMock, g_dbus_node_info_new_for_xml, arg0, arg1); +} + +extern "C" guint g_dbus_connection_register_object(GDBusConnection* arg0, + const gchar* arg1, GDBusInterfaceInfo* arg2, + const GDBusInterfaceVTable* arg3, gpointer arg4, + GDestroyNotify arg5, GError** arg6) { + return MOCK_HOOK_P7(GioMock, g_dbus_connection_register_object, + arg0, arg1, arg2, arg3, arg4, arg5, arg6); +} + +extern "C" gboolean g_dbus_connection_unregister_object (GDBusConnection *connection, + guint registration_id) { + return MOCK_HOOK_P2(GioMock, g_dbus_connection_unregister_object, + connection, registration_id); +} + +extern "C" guint g_bus_own_name_on_connection(GDBusConnection* arg0, + const gchar* arg1, GBusNameOwnerFlags arg2, + GBusNameAcquiredCallback arg3, GBusNameLostCallback arg4, + gpointer arg5, GDestroyNotify arg6) { + return MOCK_HOOK_P7(GioMock, g_bus_own_name_on_connection, + arg0, arg1, arg2, arg3, arg4, arg5, arg6); +} + +extern "C" guint g_dbus_connection_signal_subscribe(GDBusConnection* arg0, + const gchar* arg1, const gchar* arg2, const gchar* arg3, const gchar* arg4, + const gchar* arg5, GDBusSignalFlags arg6, GDBusSignalCallback arg7, + gpointer arg8, GDestroyNotify arg9) { + return MOCK_HOOK_P10(GioMock, g_dbus_connection_signal_subscribe, + arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9); +} + +extern "C" void g_dbus_connection_send_message_with_reply(GDBusConnection* arg0, + GDBusMessage* arg1, GDBusSendMessageFlags arg2, gint arg3, + volatile guint32* arg4, GCancellable* arg5, GAsyncReadyCallback arg6, + gpointer arg7) { + return MOCK_HOOK_P8(GioMock, g_dbus_connection_send_message_with_reply, + arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7); +} + +extern "C" GDBusMessage* g_dbus_connection_send_message_with_reply_finish( + GDBusConnection* arg0, GAsyncResult* arg1, GError** arg2) { + return MOCK_HOOK_P3(GioMock, g_dbus_connection_send_message_with_reply_finish, + arg0, arg1, arg2); +} diff --git a/mock/gio_mock.h b/mock/gio_mock.h new file mode 100644 index 0000000..0ff6e33 --- /dev/null +++ b/mock/gio_mock.h @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2020 Samsung Electronics Co., Ltd. + * + * 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. + */ + +#ifndef UNIT_TESTS_MOCK_GIO_MOCK_H_ +#define UNIT_TESTS_MOCK_GIO_MOCK_H_ + +#include +#include + +#include "module_mock.h" + +class GioMock : public virtual ModuleMock { + public: + virtual ~GioMock() {} + + MOCK_METHOD3(g_bus_get_sync, + GDBusConnection*(GBusType, GCancellable*, GError**)); + MOCK_METHOD7(g_bus_own_name_on_connection, + guint(GDBusConnection*, const gchar*, GBusNameOwnerFlags, + GBusNameAcquiredCallback, GBusNameLostCallback, + gpointer, GDestroyNotify)); + + MOCK_METHOD4(g_dbus_message_new_method_call, + GDBusMessage*(const gchar*, const gchar*, const gchar*, const gchar*)); + MOCK_METHOD2(g_dbus_message_set_body, void(GDBusMessage*, GVariant*)); + MOCK_METHOD2(g_dbus_connection_unregister_object, + gboolean(GDBusConnection*, guint)); + MOCK_METHOD1(g_dbus_message_get_body, GVariant*(GDBusMessage*)); + + MOCK_METHOD7(g_dbus_connection_send_message_with_reply_sync, + GDBusMessage*(GDBusConnection*, GDBusMessage*, GDBusSendMessageFlags, + gint, volatile guint32*, GCancellable*, GError**)); + MOCK_METHOD7(g_dbus_connection_emit_signal, + gboolean(GDBusConnection*, const gchar*, const gchar*, const gchar*, + const gchar*, GVariant*, GError**)); + MOCK_METHOD7(g_dbus_connection_register_object, + guint(GDBusConnection*, const gchar*, GDBusInterfaceInfo*, + const GDBusInterfaceVTable*, gpointer, GDestroyNotify, GError**)); + MOCK_METHOD10(g_dbus_connection_signal_subscribe, + guint(GDBusConnection*, const gchar*, const gchar*, const gchar*, + const gchar*, const gchar*, GDBusSignalFlags, GDBusSignalCallback, + gpointer, GDestroyNotify)); + MOCK_METHOD8(g_dbus_connection_send_message_with_reply, + void(GDBusConnection*, GDBusMessage*, GDBusSendMessageFlags, gint, + volatile guint32*, GCancellable*, GAsyncReadyCallback, gpointer)); + MOCK_METHOD3(g_dbus_connection_send_message_with_reply_finish, + GDBusMessage*(GDBusConnection*, GAsyncResult*, GError**)); + + MOCK_METHOD2(g_dbus_node_info_new_for_xml, + GDBusNodeInfo*(const gchar*, GError**)); + + MOCK_METHOD2(g_dbus_method_invocation_return_value, + GDBusNodeInfo*(GDBusMethodInvocation*, GVariant*)); +}; + +#endif // UNIT_TESTS_MOCK_GIO_MOCK_H_ diff --git a/mock/mock_hook.h b/mock/mock_hook.h new file mode 100644 index 0000000..af27bba --- /dev/null +++ b/mock/mock_hook.h @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2020 Samsung Electronics Co., Ltd. + * + * 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. + */ + +#ifndef UNIT_TESTS_MOCK_MOCK_HOOK_H_ +#define UNIT_TESTS_MOCK_MOCK_HOOK_H_ + +#define MOCK_HOOK_P0(MOCK_CLASS, f) \ + TestFixture::GetMock().f() +#define MOCK_HOOK_P1(MOCK_CLASS, f, p1) \ + TestFixture::GetMock().f(p1) +#define MOCK_HOOK_P2(MOCK_CLASS, f, p1, p2) \ + TestFixture::GetMock().f(p1, p2) +#define MOCK_HOOK_P3(MOCK_CLASS, f, p1, p2, p3) \ + TestFixture::GetMock().f(p1, p2, p3) +#define MOCK_HOOK_P4(MOCK_CLASS, f, p1, p2, p3, p4) \ + TestFixture::GetMock().f(p1, p2, p3, p4) +#define MOCK_HOOK_P5(MOCK_CLASS, f, p1, p2, p3, p4, p5) \ + TestFixture::GetMock().f(p1, p2, p3, p4, p5) +#define MOCK_HOOK_P6(MOCK_CLASS, f, p1, p2, p3, p4, p5, p6) \ + TestFixture::GetMock().f(p1, p2, p3, p4, p5, p6) +#define MOCK_HOOK_P7(MOCK_CLASS, f, p1, p2, p3, p4, p5, p6, p7) \ + TestFixture::GetMock().f(p1, p2, p3, p4, p5, p6, p7) +#define MOCK_HOOK_P8(MOCK_CLASS, f, p1, p2, p3, p4, p5, p6, p7, p8) \ + TestFixture::GetMock().f(p1, p2, p3, p4, p5, p6, p7, p8) +#define MOCK_HOOK_P10(MOCK_CLASS, f, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10) \ + TestFixture::GetMock().f( \ + p1, p2, p3, p4, p5, p6, p7, p8, p9, p10) + +#endif // UNIT_TESTS_MOCK_MOCK_HOOK_H_ diff --git a/mock/module_mock.h b/mock/module_mock.h new file mode 100644 index 0000000..9b19d89 --- /dev/null +++ b/mock/module_mock.h @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2020 Samsung Electronics Co., Ltd. + * + * 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. + */ + +#ifndef UNIT_TESTS_MOCK_MODULE_MOCK_H_ +#define UNIT_TESTS_MOCK_MODULE_MOCK_H_ + +class ModuleMock { + public: + virtual ~ModuleMock() {} +}; + +#endif // UNIT_TESTS_MOCK_MODULE_MOCK_H_ diff --git a/mock/test_fixture.cc b/mock/test_fixture.cc new file mode 100644 index 0000000..27f5666 --- /dev/null +++ b/mock/test_fixture.cc @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2020 Samsung Electronics Co., Ltd. + * + * 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. + */ + +#include "test_fixture.h" + +#include + +std::unique_ptr TestFixture::mock_; diff --git a/mock/test_fixture.h b/mock/test_fixture.h new file mode 100644 index 0000000..1ea3b8f --- /dev/null +++ b/mock/test_fixture.h @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2020 Samsung Electronics Co., Ltd. + * + * 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. + */ + +#ifndef UNIT_TESTS_MOCK_TEST_FIXTURE_H_ +#define UNIT_TESTS_MOCK_TEST_FIXTURE_H_ + +#include + +#include +#include +#include +#include + +#include "module_mock.h" + +class TestFixture : public ::testing::Test { + public: + explicit TestFixture(std::unique_ptr&& mock) { + mock_ = std::move(mock); + } + virtual ~TestFixture() { + mock_.reset(); + } + + virtual void SetUp() {} + virtual void TearDown() {} + + template + static T& GetMock() { + auto ptr = dynamic_cast(mock_.get()); + if (!ptr) + throw std::invalid_argument("The test does not provide mock of \"" + + std::string(typeid(T).name()) + "\""); + return *ptr; + } + + static std::unique_ptr mock_; +}; + +#endif // UNIT_TESTS_MOCK_TEST_FIXTURE_H_ diff --git a/packaging/message-port.spec b/packaging/message-port.spec index 11f3ad7..a4ff44f 100644 --- a/packaging/message-port.spec +++ b/packaging/message-port.spec @@ -14,6 +14,12 @@ BuildRequires: pkgconfig(aul) BuildRequires: pkgconfig(pkgmgr) BuildRequires: pkgconfig(pkgmgr-info) BuildRequires: pkgconfig(openssl1.1) +BuildRequires: pkgconfig(gmock) + +%if 0%{?gcov:1} +BuildRequires: lcov +BuildRequires: zip +%endif Requires(post): /sbin/ldconfig Requires(post): coreutils @@ -74,6 +80,17 @@ mkdir -p %{buildroot}%{_datadir}/gcov/obj install -m 0644 gcov-obj/* %{buildroot}%{_datadir}/gcov/obj %endif +%check +export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:%{buildroot}/usr/lib/ +ctest -V + +%if 0%{?gcov:1} +lcov -c --ignore-errors graph --no-external -q -d . -o message-port.info +genhtml message-port.info -o message-port.out +zip -r message-port.zip message-port.out +install -m 0644 message-port.zip %{buildroot}%{_datadir}/gcov/ +%endif + %post -p /sbin/ldconfig %postun -p /sbin/ldconfig @@ -92,6 +109,6 @@ install -m 0644 gcov-obj/* %{buildroot}%{_datadir}/gcov/obj %if 0%{?gcov:1} %files gcov -%{_datadir}/gcov/obj/* +%{_datadir}/gcov/* %endif diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 0000000..a143372 --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1,41 @@ +AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} MESSAGE_PORT_SRCS) + +ADD_LIBRARY(${TARGET_MESSAGE_PORT} SHARED ${MESSAGE_PORT_SRCS}) +SET_TARGET_PROPERTIES(${TARGET_MESSAGE_PORT} + PROPERTIES + VERSION ${FULLVER} + SOVERSION ${MAJORVER} + CLEAN_DIRECT_OUTPUT 1 + ) + +TARGET_INCLUDE_DIRECTORIES(${TARGET_MESSAGE_PORT} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/inc/) +TARGET_INCLUDE_DIRECTORIES(${TARGET_MESSAGE_PORT} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../include/) + +APPLY_PKG_CONFIG(${TARGET_MESSAGE_PORT} PUBLIC + BUNDLE_DEPS + DLOG_DEPS + GLIB_DEPS + AUL_DEPS + OPENSSL_DEPS + BASE_COMMON_DEPS + PKGMGR_INFO_DEPS + GIO_UNIX_DEPS +) + +## OUTPUT PATHS +SET(LIBRARY_OUTPUT_PATH ${CMAKE_SOURCE_DIR}/cmake_build_tmp/output) +ADD_CUSTOM_COMMAND( + TARGET ${TARGET_MESSAGE_PORT} POST_BUILD + COMMAND ln -s ./libmessage-port.so.${MAJORVER} ${LIBRARY_OUTPUT_PATH}/libcapi-message-port.so.${MAJORVER}) + +CONFIGURE_FILE(${CMAKE_SOURCE_DIR}/${TARGET_MESSAGE_PORT}.pc.in ${CMAKE_SOURCE_DIR}/${TARGET_MESSAGE_PORT}.pc @ONLY) +INSTALL(FILES ${CMAKE_SOURCE_DIR}/${TARGET_MESSAGE_PORT}.pc DESTINATION ${LIB_INSTALL_DIR}/pkgconfig) +CONFIGURE_FILE(${CMAKE_SOURCE_DIR}/capi-${TARGET_MESSAGE_PORT}.pc.in ${CMAKE_SOURCE_DIR}/capi-${TARGET_MESSAGE_PORT}.pc @ONLY) +INSTALL(FILES ${CMAKE_SOURCE_DIR}/capi-${TARGET_MESSAGE_PORT}.pc DESTINATION ${LIB_INSTALL_DIR}/pkgconfig) +CONFIGURE_FILE(${CMAKE_SOURCE_DIR}/${TARGET_MESSAGE_PORT}.conf.in ${CMAKE_SOURCE_DIR}/${TARGET_MESSAGE_PORT}.conf @ONLY) +INSTALL(FILES ${CMAKE_SOURCE_DIR}/${TARGET_MESSAGE_PORT}.conf DESTINATION ${SYSCONF_INSTALL_DIR}/dbus-1/session.d/) + +INSTALL(TARGETS ${TARGET_MESSAGE_PORT} DESTINATION ${LIB_INSTALL_DIR}) + +INSTALL(DIRECTORY ${CMAKE_SOURCE_DIR}/include/ DESTINATION include/appfw FILES_MATCHING PATTERN "*.h") +INSTALL(DIRECTORY ${LIBRARY_OUTPUT_PATH}/ DESTINATION ${LIB_INSTALL_DIR} FILES_MATCHING PATTERN "*.so*") diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt new file mode 100644 index 0000000..c90fac8 --- /dev/null +++ b/test/CMakeLists.txt @@ -0,0 +1 @@ +ADD_SUBDIRECTORY(unit_tests) diff --git a/test/unit_tests/CMakeLists.txt b/test/unit_tests/CMakeLists.txt new file mode 100644 index 0000000..9d7a190 --- /dev/null +++ b/test/unit_tests/CMakeLists.txt @@ -0,0 +1,27 @@ +AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR}/ UNIT_TESTS_SRCS) +AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR}/../../mock/ UNIT_TESTS_SRCS) +ADD_EXECUTABLE(${TARGET_MESSAGE_PORT_UNIT_TEST} + ${UNIT_TESTS_SRCS} +) + +TARGET_INCLUDE_DIRECTORIES(${TARGET_MESSAGE_PORT_UNIT_TEST} PUBLIC + "${CMAKE_CURRENT_SOURCE_DIR}/../" + "${CMAKE_CURRENT_SOURCE_DIR}/../../include" + "${CMAKE_CURRENT_SOURCE_DIR}/../../src" + "${CMAKE_CURRENT_SOURCE_DIR}/../../mock" +) + +APPLY_PKG_CONFIG(${TARGET_MESSAGE_PORT_UNIT_TEST} PUBLIC + GLIB_DEPS + GMOCK_DEPS + BUNDLE_DEPS +) + +TARGET_LINK_LIBRARIES(${TARGET_MESSAGE_PORT_UNIT_TEST} PUBLIC ${TARGET_MESSAGE_PORT}) +SET_TARGET_PROPERTIES(${TARGET_MESSAGE_PORT_UNIT_TEST} PROPERTIES COMPILE_FLAGS "-fPIE") +SET_TARGET_PROPERTIES(${TARGET_MESSAGE_PORT_UNIT_TEST} PROPERTIES LINK_FLAGS "-pie") + +ADD_TEST( + NAME ${TARGET_MESSAGE_PORT_UNIT_TEST} + COMMAND ${TARGET_MESSAGE_PORT_UNIT_TEST} +) diff --git a/test/unit_tests/main.cc b/test/unit_tests/main.cc new file mode 100644 index 0000000..5f340cd --- /dev/null +++ b/test/unit_tests/main.cc @@ -0,0 +1,25 @@ +// Copyright (c) 2020 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 + +int main(int argc, char** argv) { + int ret = -1; + try { + testing::InitGoogleTest(&argc, argv); + } catch(...) { + std::cout << "Exception occurred" << std::endl; + } + + try { + ret = RUN_ALL_TESTS(); + } catch (const ::testing::internal::GoogleTestFailureException& e) { + ret = -1; + std::cout << "GoogleTestFailureException was thrown:" << e.what() + << std::endl; + } + + return ret; +} diff --git a/test/unit_tests/test_message_port.cc b/test/unit_tests/test_message_port.cc new file mode 100644 index 0000000..b3281e0 --- /dev/null +++ b/test/unit_tests/test_message_port.cc @@ -0,0 +1,159 @@ +/* + * 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. + */ + + +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include "message_port.h" +#include "app_common_mock.h" +#include "gio_mock.h" +#include "aul_mock.h" +#include "test_fixture.h" + +using ::testing::_; +using ::testing::DoAll; +using ::testing::Return; +using ::testing::SetArgPointee; +using ::testing::Invoke; + +typedef enum { + LOG_ID_INVALID = -1, + LOG_ID_MAIN, + LOG_ID_RADIO, + LOG_ID_SYSTEM, + LOG_ID_APPS, + LOG_ID_KMSG, + LOG_ID_SYSLOG, + LOG_ID_MAX +} log_id_t; + +extern "C" int __dlog_print( + log_id_t log_id, int prio, const char* tag, const char* fmt, ...) { + va_list ap; + va_start(ap, fmt); + vprintf(fmt, ap); + va_end(ap); + printf("\n"); + + return 0; +} + +extern "C" int aul_app_get_appid_bypid(int pid, char* appid, int len) { + char test[5] = "test"; + snprintf(appid, sizeof(test), "%s", test); + return 0; +} + +extern "C" GVariant* g_dbus_connection_call_sync( + GDBusConnection* connection, const gchar* bus_name, + const gchar* object_path, const gchar* interface_name, + const gchar* method_name, GVariant* parameters, + const GVariantType* reply_type, GDBusCallFlags flags, + gint timeout_msec, GCancellable* cancellable, GError** error) { + return g_variant_new("(u)", 1); +} + +class Mocks : public ::testing::NiceMock, + public ::testing::NiceMock, + public ::testing::NiceMock {}; + +class MessagePortTest : public TestFixture { + public: + MessagePortTest() : TestFixture(std::make_unique()) {} + virtual ~MessagePortTest() {} + + virtual void SetUp() { + } + + virtual void TearDown() { + } +}; + +static void __message_cb(int local_port_id, const char* remote_app_id, + const char* remote_port, bool trusted_remote_port, + bundle* message, void* user_data) { +} + +static void __trusted_message_cb(int trusted_local_port_id, + const char* remote_app_id, const char* remote_port, + bool trusted_remote_port, bundle* message, void* user_data) { +} + +typedef struct _GDBusConnection GDBusConnection; +struct _GDBusConnection { + int test; +}; + +GDBusConnection __gdbus_conn; +TEST_F(MessagePortTest, message_port_register_local_port) { + EXPECT_CALL(GetMock(), g_bus_get_sync(_, _, _)). + WillOnce(Return(&__gdbus_conn)); + GDBusNodeInfo* info = reinterpret_cast( + malloc(sizeof(GDBusNodeInfo))); + info->ref_count = 10; + info->path = NULL; + info->interfaces = reinterpret_cast( + malloc(sizeof(GDBusInterfaceInfo*))); + info->nodes = NULL; + info->annotations = NULL; + EXPECT_CALL(GetMock(), + g_dbus_node_info_new_for_xml(_, _)).WillOnce(Return(info)); + EXPECT_CALL(GetMock(), + g_dbus_connection_register_object(_, _, _, _, _, _, _)). + WillOnce(Return(1)); + + int port = message_port_register_local_port("PORT", __message_cb, nullptr); + EXPECT_EQ(port, 1); +} + +TEST_F(MessagePortTest, message_port_register_trusted_local_port) { + GDBusNodeInfo* info = reinterpret_cast( + malloc(sizeof(GDBusNodeInfo))); + info->ref_count = 10; + info->path = NULL; + info->interfaces = reinterpret_cast( + malloc(sizeof(GDBusInterfaceInfo*))); + info->nodes = NULL; + info->annotations = NULL; + EXPECT_CALL(GetMock(), + g_dbus_node_info_new_for_xml(_, _)).WillOnce(Return(info)); + EXPECT_CALL(GetMock(), + g_dbus_connection_register_object(_, _, _, _, _, _, _)). + WillOnce(Return(2)); + + int port = message_port_register_trusted_local_port("PORT", + __trusted_message_cb, nullptr); + EXPECT_EQ(port, 2); +} + +TEST_F(MessagePortTest, message_port_unregister_local_port) { + int ret = message_port_unregister_local_port(1); + EXPECT_EQ(ret, MESSAGE_PORT_ERROR_NONE); +} + +TEST_F(MessagePortTest, message_port_unregister_trusted_local_port) { + int ret = message_port_unregister_trusted_local_port(2); + EXPECT_EQ(ret, MESSAGE_PORT_ERROR_NONE); +} -- 2.7.4