From e99141e8368c76cf90426768c2f457b275d053e1 Mon Sep 17 00:00:00 2001 From: "anuj.bhumiya" Date: Tue, 28 Jul 2020 10:57:09 +0530 Subject: [PATCH] Add support for auto-coverage in Bluetooth-Agent Change-Id: I53ed2243221404347d9b045860e1929b87f9b5e1 Signed-off-by: anuj.bhumiya --- CMakeLists.txt | 4 +- ag-agent/bluetooth-ag-agent.c | 6 +- packaging/bluetooth-agent.spec | 9 +- unittest/CMakeLists.txt | 16 +++- unittest/bluetooth-agent_test.cpp | 12 +-- unittest/mock/bluetooth-agent-mock.c | 160 +++++++++++++++++++++++++++++++++++ unittest/run_coverage.sh | 37 ++++++++ 7 files changed, 225 insertions(+), 19 deletions(-) create mode 100644 unittest/mock/bluetooth-agent-mock.c create mode 100755 unittest/run_coverage.sh diff --git a/CMakeLists.txt b/CMakeLists.txt index b7c18fa..6f8fc2f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,6 +9,6 @@ ADD_SUBDIRECTORY(pb-agent) ADD_SUBDIRECTORY(ag-agent) ADD_SUBDIRECTORY(ipsp-agent) ADD_SUBDIRECTORY(hid-agent) -IF (BUILD_GTESTS) +IF (BUILD_GCOV) ADD_SUBDIRECTORY(unittest) -ENDIF (BUILD_GTESTS) +ENDIF (BUILD_GCOV) \ No newline at end of file diff --git a/ag-agent/bluetooth-ag-agent.c b/ag-agent/bluetooth-ag-agent.c index a3ef15d..70a7c4a 100644 --- a/ag-agent/bluetooth-ag-agent.c +++ b/ag-agent/bluetooth-ag-agent.c @@ -1314,7 +1314,7 @@ fail: void _bt_ag_agent_get_manufacturer_name(void *device) { FN_START; - char *manufacturer_name; + char *manufacturer_name = NULL; int ret; ret = system_info_get_platform_string("http://tizen.org/system/manufacturer", @@ -1426,7 +1426,7 @@ void _bt_ag_agent_get_creg_status(void *device) void _bt_ag_agent_get_model_name(void *device) { FN_START; - char *model_name; + char *model_name = NULL; int ret; ret = system_info_get_platform_string("http://tizen.org/system/model_name", &model_name); @@ -1451,7 +1451,7 @@ void _bt_ag_agent_get_model_name(void *device) void _bt_ag_agent_get_revision_information(void *device) { FN_START; - char *revision_info; + char *revision_info = NULL; int ret; ret = system_info_get_platform_string("http://tizen.org/system/build.string", diff --git a/packaging/bluetooth-agent.spec b/packaging/bluetooth-agent.spec index 953be0e..15e27aa 100644 --- a/packaging/bluetooth-agent.spec +++ b/packaging/bluetooth-agent.spec @@ -46,7 +46,7 @@ BuildRequires: pkgconfig(gio-2.0) BuildRequires: cmake Requires: security-config %if 0%{?gcov:1} -BuildRequires: pkgconfig(gmock) +BuildRequires: gtest-devel BuildRequires: lcov %endif @@ -163,6 +163,13 @@ mkdir -p %{buildroot}%{_datadir}/gcov/obj install -m 0644 gcov-obj/* %{buildroot}%{_datadir}/gcov/obj %endif +%check +%if 0%{?gcov:1} +pushd unittest +./run_coverage.sh +popd +%endif + # This usage of profile macro does NOT conflict 4.0 configurability. #%if "%{?profile}" != "mobile" && "%{?profile}" != "tv" # Original: wearable, ivi. Added: common, "undefined" diff --git a/unittest/CMakeLists.txt b/unittest/CMakeLists.txt index 951996a..91497fb 100644 --- a/unittest/CMakeLists.txt +++ b/unittest/CMakeLists.txt @@ -16,6 +16,8 @@ # @author # @brief Cmake for tests # +SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--as-needed") + SET(TARGET_GTEST_TESTS "gtest-bluetooth-agent") INCLUDE_DIRECTORIES(/usr/include) @@ -36,7 +38,7 @@ SET(EXEC_PREFIX "\${prefix}") SET(INCLUDEDIR "/usr/include") INCLUDE(FindPkgConfig) -pkg_check_modules(testpkgs REQUIRED dlog glib-2.0 gmock) +pkg_check_modules(testpkgs REQUIRED dlog msg-service email-service glib-2.0) INCLUDE_DIRECTORIES(${testpkgs_INCLUDE_DIRS}) @@ -44,6 +46,16 @@ SET(GTEST_TESTS_SOURCES bluetooth-agent_test.cpp ) +SET(BLUETOOTH_AGENT_MOCK "bluetooth-agent-mock") +SET(BLUETOOTH_AGENT_MOCK_SRCS + mock/bluetooth-agent-mock.c) +SET(BLUETOOTH_AGENT_MOCK_CFLAGS "${CMAKE_C_FLAGS}" ) +ADD_LIBRARY(${BLUETOOTH_AGENT_MOCK} SHARED ${BLUETOOTH_AGENT_MOCK_SRCS}) +TARGET_LINK_LIBRARIES(${BLUETOOTH_AGENT_MOCK} -ldl) +SET_TARGET_PROPERTIES(${BLUETOOTH_AGENT_MOCK} PROPERTIES + COMPILE_FLAGS ${BLUETOOTH_AGENT_MOCK_CFLAGS} +) + ADD_EXECUTABLE(${TARGET_GTEST_TESTS} ${GTEST_TESTS_SOURCES} ) @@ -51,7 +63,7 @@ ADD_EXECUTABLE(${TARGET_GTEST_TESTS} TARGET_LINK_LIBRARIES(${TARGET_GTEST_TESTS} ${testpkgs_LDFLAGS} ${testpkgs_LIBRARIES} - gmock + gtest -L${CMAKE_SOURCE_DIR}/ag-agent -L${CMAKE_SOURCE_DIR}/hf-agent -L${CMAKE_SOURCE_DIR}/hid-agent diff --git a/unittest/bluetooth-agent_test.cpp b/unittest/bluetooth-agent_test.cpp index a8e48c2..c75d20f 100644 --- a/unittest/bluetooth-agent_test.cpp +++ b/unittest/bluetooth-agent_test.cpp @@ -20,7 +20,6 @@ * @brief Unit-tests setup */ -#include #include #include #include @@ -45,15 +44,6 @@ /* pb-agent header files */ #include "bluetooth_pb_vcard.h" -using ::testing::EmptyTestEventListener; -using ::testing::InitGoogleTest; -using ::testing::Test; -using ::testing::TestCase; -using ::testing::TestEventListeners; -using ::testing::TestInfo; -using ::testing::TestPartResult; -using ::testing::UnitTest; - static GMainLoop *mainloop = NULL; @@ -1274,7 +1264,7 @@ TEST(BluetoothPBAgent_test, _bt_is_sim_addressbook) { } int main(int argc, char **argv) { - InitGoogleTest(&argc, argv); + testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); } diff --git a/unittest/mock/bluetooth-agent-mock.c b/unittest/mock/bluetooth-agent-mock.c new file mode 100644 index 0000000..df90f02 --- /dev/null +++ b/unittest/mock/bluetooth-agent-mock.c @@ -0,0 +1,160 @@ +/* + * 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. + */ + +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include + + +/*Messaging Header Files*/ +#include "msg.h" +#include "msg_transport_types.h" +#include "msg_types.h" +#include "email-types.h" + +#ifndef API +#define API __attribute__ ((visibility("default"))) +#endif + + +API int vconf_get_int(const char *in_key, const int intval) +{ + return 0; +} + +API char *vconf_get_str(const char *in_key) +{ + return strdup("memory/telephony/nw_name"); +} + +API int email_service_end() +{ + return EMAIL_ERROR_NONE; +} + +API int msg_open_msg_handle(msg_handle_t *handle) +{ + return MSG_SUCCESS; +} + +API int msg_reg_sent_status_callback(msg_handle_t handle, msg_sent_status_cb cb, void *user_param) +{ + return MSG_SUCCESS; +} + +API int msg_reg_sms_message_callback(msg_handle_t handle, msg_sms_incoming_cb cb, unsigned short port, void *user_param) +{ + return MSG_SUCCESS; +} + +API GDBusConnection *g_bus_get_sync (GBusType bus_type, + GCancellable *cancellable, + GError **error) +{ + return (void *)1234; +} + +API guint g_dbus_connection_register_object (GDBusConnection *connection, + const gchar *object_path, + GDBusInterfaceInfo *interface_info, + const GDBusInterfaceVTable *vtable, + gpointer user_data, + GDestroyNotify user_data_free_func, + GError **error) +{ + return 1234; +} + +API guint g_dbus_connection_signal_subscribe (GDBusConnection *connection, + const gchar *sender, + const gchar *interface_name, + const gchar *member, + const gchar *object_path, + const gchar *arg0, + GDBusSignalFlags flags, + GDBusSignalCallback callback, + gpointer user_data, + GDestroyNotify user_data_free_func) +{ + return 0; +} +API void g_dbus_connection_signal_unsubscribe(GDBusConnection *connection, + guint subscription_id) +{ +} + +API gboolean g_dbus_connection_unregister_object(GDBusConnection *connection, + guint registration_id) +{ + return TRUE; +} + +API void g_object_unref(gpointer object) +{ +} + +API GVariant *g_dbus_proxy_call_sync(GDBusProxy *proxy, + const gchar *method_name, + GVariant *parameters, + GDBusCallFlags flags, + gint timeout_msec, + GCancellable *cancellable, + GError **error) +{ + return NULL; +} + +API GDBusProxy *g_dbus_proxy_new_sync(GDBusConnection *connection, + GDBusProxyFlags flags, + GDBusInterfaceInfo *info, + const gchar *name, + const gchar *object_path, + const gchar *interface_name, + GCancellable *cancellable, + GError **error) +{ + return (void *)1234; +} + +API GVariant *g_variant_get_child_value(GVariant *value, + gsize index_) +{ + return (void *)1234; +} + +API gsize g_variant_n_children(GVariant *value) +{ + return 1234; +} + +API void g_variant_unref(GVariant *value) +{ +} + +API void g_variant_get (GVariant *value, + const gchar *format_string, + ...) +{ +} + +API gint g_io_channel_unix_get_fd(GIOChannel *channel) +{ + return 0; +} diff --git a/unittest/run_coverage.sh b/unittest/run_coverage.sh new file mode 100755 index 0000000..dec9df1 --- /dev/null +++ b/unittest/run_coverage.sh @@ -0,0 +1,37 @@ +#!/bin/bash + +export LD_LIBRARY_PATH=..:.:../ag-agent:../hf-agent:../hid-agent:../include:../ipsp-agent:../map-agent:../pb-agent:$LD_LIBRARY_PATH +pushd ../ +RootDir=$PWD +popd + +unitTestFile=gtest-bluetooth-agent +if [ ! -x "./$unitTestFile" ] +then + echo "$unitTestFile file does not exist!" + exit -1 +fi +#./$unitTestFile + +LD_PRELOAD=./libbluetooth-agent-mock.so ./$unitTestFile + +CMakeDir=${RootDir} +CoverageDir=${RootDir}/coverage + +pushd $CMakeDir + + for obj in `ls *.o` + do + gcov -b -c $obj + done + + if [ -f /usr/bin/lcov ] + then + lcov -c --ignore-errors graph --no-external -b . -d . -o cov.info + genhtml cov.info -o ${CoverageDir} + echo "Coverage test result created! [${CoverageDir}]" + else + echo "lcov does not exist!" + fi +popd + -- 2.7.4