From: Youngjae Shin Date: Tue, 2 Jun 2020 06:56:42 +0000 (+0900) Subject: add gtest with mocks X-Git-Tag: submit/tizen/20200806.035941~3 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=refs%2Fchanges%2F07%2F235107%2F7;p=platform%2Fcore%2Fapi%2Ftelephony.git add gtest with mocks Change-Id: Iee838704351ca74719995b269065e16ae430bb0d --- diff --git a/CMakeLists.txt b/CMakeLists.txt index 61eb124..81ec8ce 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ CMAKE_MINIMUM_REQUIRED(VERSION 3.9) -PROJECT(capi-telephony C) +PROJECT(capi-telephony) INCLUDE(FindPkgConfig) INCLUDE_DIRECTORIES(common) @@ -15,7 +15,7 @@ IF(STDOUT_LOG) ENDIF(STDOUT_LOG) PKG_CHECK_MODULES(pkgs REQUIRED dlog tapi glib-2.0 capi-base-common openssl1.1 - capi-system-info cynara-client cynara-session cynara-creds-self) + capi-system-info cynara-client cynara-session cynara-creds-self gio-2.0) INCLUDE_DIRECTORIES(${pkgs_INCLUDE_DIRS}) LINK_DIRECTORIES(${pkgs_LIBRARY_DIRS}) diff --git a/packaging/capi-telephony.spec b/packaging/capi-telephony.spec index e9a9bbe..6e96be9 100644 --- a/packaging/capi-telephony.spec +++ b/packaging/capi-telephony.spec @@ -18,6 +18,7 @@ BuildRequires: pkgconfig(capi-system-info) BuildRequires: pkgconfig(cynara-client) BuildRequires: pkgconfig(cynara-session) BuildRequires: pkgconfig(cynara-creds-self) +BuildRequires: pkgconfig(gmock) Requires(post): /sbin/ldconfig Requires(postun): /sbin/ldconfig %if 0%{?gcov:1} @@ -85,6 +86,13 @@ install -d -m 755 %{buildroot}%{_datadir}/gcov/obj tar xf %{name}-gcov.tar -C %{buildroot}%{_datadir}/gcov/obj %endif +%check +tests/tel-gtest +%if 0%{?gcov:1} +lcov -c --ignore-errors graph --no-external -b . -d . -o %{name}.info +genhtml %{name}.info -o out --legend --show-details +%endif + %post -p /sbin/ldconfig %postun -p /sbin/ldconfig @@ -101,6 +109,7 @@ tar xf %{name}-gcov.tar -C %{buildroot}%{_datadir}/gcov/obj %files unittests %manifest %{name}.manifest %{_bindir}/telephony-test +%{_bindir}/%{name}/*gtest* %license LICENSE.APLv2 %if 0%{?gcov:1} diff --git a/src/telephony_common.c b/src/telephony_common.c index 07d5d84..1b7c06b 100644 --- a/src/telephony_common.c +++ b/src/telephony_common.c @@ -416,7 +416,6 @@ API int telephony_init(telephony_handle_list_s *list) for (i = 0; i < cp_count; i++) { telephony_h tmp = malloc(sizeof(struct telephony_data)); if (tmp == NULL) { - /* LCOV_EXCL_START */ int j; ERR("Out of memory"); /* Need to be freed if allocated data is existing */ @@ -429,13 +428,11 @@ API int telephony_init(telephony_handle_list_s *list) free(list->handle); g_strfreev(cp_list); return TELEPHONY_ERROR_OUT_OF_MEMORY; - /* LCOV_EXCL_STOP */ } tmp->evt_list = NULL; tmp->tapi_h = tel_init(cp_list[i]); if (tmp->tapi_h == NULL) { - /* LCOV_EXCL_START */ int j; ERR("tel_init() is failed"); /* Need to be freed if allocated data is existing */ @@ -449,7 +446,6 @@ API int telephony_init(telephony_handle_list_s *list) free(list->handle); g_strfreev(cp_list); return TELEPHONY_ERROR_OPERATION_FAILED; - /* LCOV_EXCL_STOP */ } list->handle[i] = tmp; } diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 8c567af..659ebd1 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,9 +1,28 @@ LINK_DIRECTORIES(${CMAKE_BINARY_DIR}) +ADD_DEFINITIONS("-DTEL_TEST") -SET(TELE_TEST "telephony-test") - +SET(TEL_TEST "telephony-test") FILE(GLOB TEST_SRCS *.c) -ADD_EXECUTABLE(${TELE_TEST} ${TEST_SRCS}) -TARGET_LINK_LIBRARIES(${TELE_TEST} ${PROJECT_NAME}) -SET_TARGET_PROPERTIES(${TELE_TEST} PROPERTIES COMPILE_FLAGS "-fPIE") -INSTALL(TARGETS ${TELE_TEST} DESTINATION ${BIN_INSTALL_DIR}) +ADD_EXECUTABLE(${TEL_TEST} ${TEST_SRCS}) +TARGET_LINK_LIBRARIES(${TEL_TEST} ${PROJECT_NAME}) +SET_TARGET_PROPERTIES(${TEL_TEST} PROPERTIES COMPILE_FLAGS "-fPIE") +INSTALL(TARGETS ${TEL_TEST} DESTINATION ${BIN_INSTALL_DIR}) + +pkg_check_modules(gtest_pkgs REQUIRED gmock) +INCLUDE_DIRECTORIES(${gtest_pkgs_INCLUDE_DIRS}) +LINK_DIRECTORIES(${gtest_pkgs_LIBRARY_DIRS}) + +SET(TEL_GTEST "tel-gtest") +FILE(GLOB GTEST_SRCS *.cpp mocks/*.c ${CMAKE_SOURCE_DIR}/src/*.c) +ADD_EXECUTABLE(${TEL_GTEST} ${GTEST_SRCS}) +TARGET_LINK_LIBRARIES(${TEL_GTEST} ${gtest_pkgs_LIBRARIES} ${pkgs_LIBRARIES}) +SET_TARGET_PROPERTIES(${TEL_GTEST} PROPERTIES + COMPILE_FLAGS "-fPIE" + #Never add any space for LINKFLAGS + LINK_FLAGS "-Wl,\ +--wrap=malloc,\ +--wrap=tel_get_cp_name_list,\ +--wrap=tel_init,\ +--wrap=tel_deinit,\ +--wrap=system_info_get_platform_bool") +INSTALL(TARGETS ${TEL_GTEST} DESTINATION ${TEST_INSTALL_DIR}) diff --git a/tests/gtest_common.cpp b/tests/gtest_common.cpp new file mode 100644 index 0000000..ee5e539 --- /dev/null +++ b/tests/gtest_common.cpp @@ -0,0 +1,90 @@ +/* + * Copyright (c) 2019-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 "telephony_common.h" + +extern "C" { +#include "telephony_private.h" +#include "mocks/telephony_malloc.h" +#include "mocks/telephony_sysinfo.h" +#include "mocks/telephony_tapi.h" +} + +class TelCommonTest : public ::testing::Test { +protected: + void SetUp() override + { + //Setting up Normal Codition + telephony_mock_sysinfo_set_up(true); + telephony_mock_tapi_cp_setup(1); + telephony_mock_tapi_init_setup(1); + } + + void TearDown() override + { + telephony_mock_sysinfo_set_up(false); + telephony_mock_tapi_cp_setup(0); + telephony_mock_tapi_init_setup(0); + } +}; + +TEST_F(TelCommonTest, initP) +{ + int ret; + telephony_handle_list_s list; + + ret = telephony_init(&list); + EXPECT_EQ(ret, TELEPHONY_ERROR_NONE); + + ret = telephony_deinit(&list); + EXPECT_EQ(ret, TELEPHONY_ERROR_NONE); +} + +TEST_F(TelCommonTest, initN) +{ + int ret; + telephony_handle_list_s list; + + telephony_mock_tapi_init_setup(0); + ret = telephony_init(&list); + EXPECT_EQ(ret, TELEPHONY_ERROR_OPERATION_FAILED); + + telephony_mock_malloc_set_up(sizeof(struct telephony_data), 0); + ret = telephony_init(&list); + EXPECT_EQ(ret, TELEPHONY_ERROR_OUT_OF_MEMORY); + + telephony_mock_malloc_set_up(1 * sizeof(telephony_h), 0); + ret = telephony_init(&list); + EXPECT_EQ(ret, TELEPHONY_ERROR_OUT_OF_MEMORY); + + telephony_mock_tapi_cp_setup(0); + ret = telephony_init(&list); + EXPECT_EQ(ret, TELEPHONY_ERROR_OPERATION_FAILED); + + ret = telephony_init(NULL); + EXPECT_EQ(ret, TELEPHONY_ERROR_INVALID_PARAMETER); + + telephony_mock_sysinfo_set_up(false); + ret = telephony_init(&list); + EXPECT_EQ(ret, TELEPHONY_ERROR_NOT_SUPPORTED); +} + +int main(int argc, char **argv) +{ + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} diff --git a/tests/mocks/telephony_malloc.c b/tests/mocks/telephony_malloc.c new file mode 100644 index 0000000..713b01e --- /dev/null +++ b/tests/mocks/telephony_malloc.c @@ -0,0 +1,41 @@ +/* + * 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 "telephony_malloc.h" + +#include +#include +#include "telephony_private.h" + +static int telephony_mock_malloc_size; +static int telephony_mock_malloc_result; +extern void* __real_malloc(size_t size); + +void telephony_mock_malloc_set_up(size_t size, int result) +{ + telephony_mock_malloc_size = size; + telephony_mock_malloc_result = result; +} + +void* __wrap_malloc(size_t size) +{ + if (size == telephony_mock_malloc_size) { + if (0 == telephony_mock_malloc_result) + return NULL; + } + + return __real_malloc(size); +} + diff --git a/tests/mocks/telephony_malloc.h b/tests/mocks/telephony_malloc.h new file mode 100644 index 0000000..5801be9 --- /dev/null +++ b/tests/mocks/telephony_malloc.h @@ -0,0 +1,20 @@ +/* + * 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. + */ +#pragma once + +#include + +void telephony_mock_malloc_set_up(size_t size, int result); diff --git a/tests/mocks/telephony_sysinfo.c b/tests/mocks/telephony_sysinfo.c new file mode 100644 index 0000000..5d9f51b --- /dev/null +++ b/tests/mocks/telephony_sysinfo.c @@ -0,0 +1,40 @@ +/* + * 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 "telephony_sysinfo.h" + +#include +#include +#include +#include +#include "telephony_private.h" + +static bool telephony_mock_sysinfo_result; +extern int __real_system_info_get_platform_bool(const char *key, bool *value); + +void telephony_mock_sysinfo_set_up(bool value) +{ + telephony_mock_sysinfo_result = value; +} + +API int __wrap_system_info_get_platform_bool(const char *key, bool *value) +{ + if (0 == strcmp(key, TELEPHONY_FEATURE)) { + *value = telephony_mock_sysinfo_result; + return SYSTEM_INFO_ERROR_NONE; + } + + return __real_system_info_get_platform_bool(key, value); +} \ No newline at end of file diff --git a/tests/mocks/telephony_sysinfo.h b/tests/mocks/telephony_sysinfo.h new file mode 100644 index 0000000..c768dde --- /dev/null +++ b/tests/mocks/telephony_sysinfo.h @@ -0,0 +1,20 @@ +/* + * 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. + */ +#pragma once + +#include + +void telephony_mock_sysinfo_set_up(bool value); diff --git a/tests/mocks/telephony_tapi.c b/tests/mocks/telephony_tapi.c new file mode 100644 index 0000000..f4471fe --- /dev/null +++ b/tests/mocks/telephony_tapi.c @@ -0,0 +1,65 @@ +/* + * 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 "telephony_tapi.h" + +#include +#include +#include +#include +#include "telephony_private.h" + +static int telephony_mock_tapi_cp_num; +static int telephony_mock_tapi_init_result; + +void telephony_mock_tapi_cp_setup(int cp_num) +{ + telephony_mock_tapi_cp_num = cp_num; +} + +char** __wrap_tel_get_cp_name_list() +{ + if (telephony_mock_tapi_cp_num) { + char **result = calloc(telephony_mock_tapi_cp_num + 1, sizeof(char*)); + int i; + for (i = 0; i < telephony_mock_tapi_cp_num; i++) { + char buf[20]; + snprintf(buf, sizeof(buf), "sprdmodem%d", i); + result[i] = strdup(buf); + } + return result; + } else { + return NULL; + } +} + +void telephony_mock_tapi_init_setup(int result) +{ + telephony_mock_tapi_init_result = result; +} + +TapiHandle* __wrap_tel_init(const char *cp_name) +{ + if (0 == telephony_mock_tapi_init_result) + return NULL; + + return calloc(1, sizeof(TapiHandle)); +} + +int __wrap_tel_deinit(TapiHandle *handle) +{ + free(handle); + return TAPI_API_SUCCESS; +} \ No newline at end of file diff --git a/tests/mocks/telephony_tapi.h b/tests/mocks/telephony_tapi.h new file mode 100644 index 0000000..ca1fc20 --- /dev/null +++ b/tests/mocks/telephony_tapi.h @@ -0,0 +1,19 @@ +/* + * 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. + */ +#pragma once + +void telephony_mock_tapi_cp_setup(int cp_num); +void telephony_mock_tapi_init_setup(int result); \ No newline at end of file