add gtest with mocks 07/235107/7
authorYoungjae Shin <yj99.shin@samsung.com>
Tue, 2 Jun 2020 06:56:42 +0000 (15:56 +0900)
committerYoungjae Shin <yj99.shin@samsung.com>
Wed, 3 Jun 2020 05:49:50 +0000 (14:49 +0900)
Change-Id: Iee838704351ca74719995b269065e16ae430bb0d

CMakeLists.txt
packaging/capi-telephony.spec
src/telephony_common.c
tests/CMakeLists.txt
tests/gtest_common.cpp [new file with mode: 0644]
tests/mocks/telephony_malloc.c [new file with mode: 0644]
tests/mocks/telephony_malloc.h [new file with mode: 0644]
tests/mocks/telephony_sysinfo.c [new file with mode: 0644]
tests/mocks/telephony_sysinfo.h [new file with mode: 0644]
tests/mocks/telephony_tapi.c [new file with mode: 0644]
tests/mocks/telephony_tapi.h [new file with mode: 0644]

index 61eb124682cb29ce6a0e36ce6cf8fca5deda92c6..81ec8ce2d1927518ae26296b2ac4a09565862773 100644 (file)
@@ -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})
 
index e9a9bbe04b5f8834d527b86c31afb59690bce735..6e96be933e4764de00873310490849d3d5a7b61f 100644 (file)
@@ -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}
index 07d5d84bcfa3da63d4b41382c59395f67279d129..1b7c06ba35b36a3b3c86c3b9751f66d77124ae4c 100644 (file)
@@ -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;
        }
index 8c567afa89d9db03950ec5ec3c8287ad931a27d2..659ebd1bd777a79bb92fbd09141592206eda19ba 100644 (file)
@@ -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 (file)
index 0000000..ee5e539
--- /dev/null
@@ -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 <gtest/gtest.h>
+
+#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 (file)
index 0000000..713b01e
--- /dev/null
@@ -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 <stdio.h>
+#include <stdlib.h>
+#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 (file)
index 0000000..5801be9
--- /dev/null
@@ -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 <stdlib.h>
+
+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 (file)
index 0000000..5d9f51b
--- /dev/null
@@ -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 <stdio.h>
+#include <string.h>
+#include <stdbool.h>
+#include <system_info.h>
+#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 (file)
index 0000000..c768dde
--- /dev/null
@@ -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 <stdbool.h>
+
+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 (file)
index 0000000..f4471fe
--- /dev/null
@@ -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 <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <tapi_common.h>
+#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 (file)
index 0000000..ca1fc20
--- /dev/null
@@ -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