Improve code coverage 47/266547/3
authorSeungha Son <seungha.son@samsung.com>
Mon, 15 Nov 2021 07:50:40 +0000 (16:50 +0900)
committerSeungha Son <seungha.son@samsung.com>
Mon, 15 Nov 2021 10:34:23 +0000 (19:34 +0900)
 In the QEMU environment, it was difficult to measure coverage
 for file-related functions during gtest operation, so bypass
 was performed. File-related functions were wrapped and processed
 as if they were normally processed in a QEMU environment to improve
 coverage.

 Note: description about comment for LCOV in below
 C++ code, an individual line can be excluded by adding LCOV_EXCL_LINE
 somewhere on that line, or a block can be excluded by surrounding it
 with lines containing LCOV_EXCL_START and LCOV_EXCL_STOP.

Change-Id: Iea4413fd5cc128ed1f417efcc26ad1e797321981
Signed-off-by: Seungha Son <seungha.son@samsung.com>
CMakeLists.txt
packaging/ttrace.spec
src/ttrace.c
test/CMakeLists.txt
test/src/file_mock.c [new file with mode: 0644]
test/src/file_mock.h [new file with mode: 0644]
test/src/test_ttrace.cc

index 4233798ed0699ea8d8ed2679ef300f5ebdb37da0..c5cee4b54947f6e9821c43c30a952747779628c8 100755 (executable)
@@ -31,10 +31,6 @@ ELSE()
        SET(TTRACE_TIZEN_VERSION_MAJOR "2")
 ENDIF()
 
-IF("${COVERAGE}" STREQUAL "on")
-       ADD_DEFINITIONS("-DTTRACE_COVERAGE")
-ENDIF()
-
 CONFIGURE_FILE(${TTRACE_H_IN} "include/ttrace.h")
 
 SET(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules/")
index 6900adc5f8d434518885132d1106143d16517fe4..776c4829df236d48e1afbba9581c5ac9b0e35029 100644 (file)
@@ -19,9 +19,6 @@ BuildRequires: cmake
 
 %if 0%{?gcov:1}
 BuildRequires: lcov
-%define TTRACE_COVERAGE on
-%else
-%define TTRACE_COVERAGE off
 %endif
 
 %define keepstatic 1
@@ -85,8 +82,7 @@ MAJORVER=`echo %{version} | awk 'BEGIN {FS="."}{print $1}'`
 %cmake . -DCMAKE_INSTALL_PREFIX=%{_prefix} -DLIBDIR=%{_libdir} -DINCLUDEDIR=%{_includedir} \
       -DTTRACE_PROFILE=%{TTRACE_PROFILE} -DTTRACE_TIZEN_VERSION_MAJOR=%{TTRACE_TIZEN_VERSION_MAJOR} \
       -DMAJORVER=${MAJORVER} -DFULLVER=%{version} \
-      -DATRACE_VERSION=%{ATRACE_VERSION} -DATRACE_HELPER_VERSION=%{ATRACE_HELPER_VERSION} \
-      -DCOVERAGE=%{TTRACE_COVERAGE}
+      -DATRACE_VERSION=%{ATRACE_VERSION} -DATRACE_HELPER_VERSION=%{ATRACE_HELPER_VERSION}
 make %{?jobs:-j%jobs}
 
 %if 0%{?gcov:1}
index 3331e0bebf50490a297c9ae41a96b0394ff904af..361962de0aab23a790518cd1f09975117f1f3798 100755 (executable)
@@ -87,9 +87,11 @@ static uint64_t traceInit()
                }
                sm_for_enabled_tag = mmap(NULL, sizeof(uint64_t), PROT_READ, MAP_SHARED, g_enabled_tag_fd, 0);
                if (sm_for_enabled_tag == MAP_FAILED) {
+/* LCOV_EXCL_START */
                        TTRACE_LOG("error: mmap() failed(%s)\n", strerror(errno));
                        close(g_enabled_tag_fd);
                        return 0;
+/* LCOV_EXCL_STOP */
                }
                cur_enabled_tag = sm_for_enabled_tag;
        }
@@ -98,6 +100,7 @@ static uint64_t traceInit()
        if(g_trace_handle_fd == FD_INITIAL_VALUE) {
                g_trace_handle_fd = open(TRACE_FILE, O_WRONLY);
                if (g_trace_handle_fd < 0) {
+/* LCOV_EXCL_START */
                        TTRACE_LOG("Fail to open trace file: %s(%d)", strerror(errno), errno);
                        /*
                         * If ftrace debugfs is not mounted, ttrace does not call traceInit() anymore.
@@ -108,6 +111,7 @@ static uint64_t traceInit()
 
                        set_last_result(TRACE_ERROR_IO_ERROR);
                        return 0;
+/* LCOV_EXCL_STOP */
                }
        }
        TTRACE_LOG("traceInit:: cur_enabled_tag >> %u", *cur_enabled_tag);
@@ -117,10 +121,6 @@ static uint64_t traceInit()
 
 static inline uint64_t isTagEnabled(uint64_t cur_tag)
 {
-#ifdef TTRACE_COVERAGE
-       return 1;
-#endif
-
        if (g_trace_handle_fd == TRACE_FILE_NOT_EXIST)
                return 0;
        /* if no tag is enabled, trace all tags. */
@@ -135,9 +135,11 @@ static inline uint64_t isTagEnabled(uint64_t cur_tag)
 int check_params(int fd, char* buf, int len, const char* func)
 {
        if (fd <= 0 || buf == NULL || len < 0) {
+/* LCOV_EXCL_START */
                TTRACE_LOG("Currupted arguments, fd: %d, buf: %p, len: %d at %s.\n",
                                fd, buf, len, func);
                return -1;
+/* LCOV_EXCL_STOP */
        }
        return 0;
 }
@@ -168,8 +170,10 @@ void traceBegin(uint64_t tag, const char *name, ...)
 
                ret = write(g_trace_handle_fd, buf, len);
                if (ret < 0)
+/* LCOV_EXCL_START */
                        TTRACE_LOG("error writing, len: %d, ret: %d, errno: %d at traceBegin.\n",
                                        len, ret, errno);
+/* LCOV_EXCL_STOP */
        }
        else
                TTRACE_LOG("traceBegin:: disabled tag >> tag: %u tag_bit: %u", tag, *cur_enabled_tag);
index a3dd22af1028b2347cd9871aabcee6890a0fa1e5..727c4c4af4f512c63b3f70912b5993d11e5e0fc4 100644 (file)
@@ -1,20 +1,26 @@
 ENABLE_TESTING()
 
 AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR}/src UNIT_TESTS_SRCS)
+AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR}/../src TTRACE_SRCS)
 
-ADD_EXECUTABLE(${TARGET_TTRACE_UNIT_TEST} ${UNIT_TESTS_SRCS})
+ADD_EXECUTABLE(${TARGET_TTRACE_UNIT_TEST} ${TTRACE_SRCS} ${UNIT_TESTS_SRCS})
 
 TARGET_INCLUDE_DIRECTORIES(${TARGET_TTRACE_UNIT_TEST} PUBLIC
   "${CMAKE_CURRENT_SOURCE_DIR}/../include"
+  "${CMAKE_CURRENT_SOURCE_DIR}/src"
 )
 
 APPLY_PKG_CONFIG(${TARGET_TTRACE_UNIT_TEST} PUBLIC
   GMOCK_DEPS
+  DLOG_DEPS
+  CAPI_BASE_COMMON_DEPS
 )
 
-TARGET_LINK_LIBRARIES(${TARGET_TTRACE_UNIT_TEST} PUBLIC ${TARGET_TTRACE})
-SET_TARGET_PROPERTIES(${TARGET_TTRACE_UNIT_TEST} PROPERTIES COMPILE_FLAGS "-fPIE -fvisibility=default")
-SET_TARGET_PROPERTIES(${TARGET_TTRACE_UNIT_TEST} PROPERTIES LINK_FLAGS "-pie")
+SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${EXTRA_CFLAGS} -fpic -fPIE -std=c++11")
+
+#TARGET_LINK_LIBRARIES(${TARGET_TTRACE_UNIT_TEST} PUBLIC ${TARGET_TTRACE})
+SET_TARGET_PROPERTIES(${TARGET_TTRACE_UNIT_TEST} PROPERTIES COMPILE_FLAGS ${CFLAGS} "-fPIE -fvisibility=default")
+SET_TARGET_PROPERTIES(${TARGET_TTRACE_UNIT_TEST} PROPERTIES LINK_FLAGS "-pie -Wl,--wrap=open -Wl,--wrap=open64 -Wl,--wrap=write -Wl,--wrap=mmap -Wl,--wrap=mmap64 -Wl,--wrap=close")
 
 ADD_TEST(
   NAME ${TARGET_TTRACE_UNIT_TEST}
diff --git a/test/src/file_mock.c b/test/src/file_mock.c
new file mode 100644 (file)
index 0000000..cdc409c
--- /dev/null
@@ -0,0 +1,68 @@
+#include <stdlib.h>
+#include <stdbool.h>
+#include <stdint.h>
+
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+#include "file_mock.h"
+
+int __real_open(const char *pathname, int flags, mode_t mode);
+int __real_open64(const char *pathname, int flags, mode_t mode);
+ssize_t __real_write(int fd, const void* buf, size_t count);
+void *__real_mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset);
+void *__real_mmap64(void *addr, size_t length, int prot, int flags, int fd, off_t offset);
+int __real_close(int fd);
+
+static uint64_t enabled_tag_ = 1 << 20; // TTRACE_TAG_APP
+static bool mock_enabled_flag_ = false;
+
+void file_mock_setup(bool flag) {
+  mock_enabled_flag_ = flag;
+}
+
+int __wrap_open(const char *pathname, int flags, mode_t mode) {
+  if (!mock_enabled_flag_)
+    return __real_open(pathname, flags, mode);
+
+  return 2;
+}
+
+int __wrap_open64(const char *pathname, int flags, mode_t mode) {
+  if (!mock_enabled_flag_)
+    return __real_open64(pathname, flags, mode);
+
+  return 2;
+}
+
+ssize_t __wrap_write(int fd, const void* buf, size_t count) {
+  if (!mock_enabled_flag_)
+    return __real_write(fd, buf, count);
+
+  return count;
+}
+
+void *__wrap_mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset)
+{
+  if (!mock_enabled_flag_)
+    return __real_mmap(addr, length, prot, flags, fd, offset);
+
+  return &enabled_tag_;
+}
+
+void *__wrap_mmap64(void *addr, size_t length, int prot, int flags, int fd, off_t offset)
+{
+  if (!mock_enabled_flag_)
+    return __real_mmap64(addr, length, prot, flags, fd, offset);
+
+  return &enabled_tag_;
+}
+
+int __wrap_close(int fd) {
+  if (!mock_enabled_flag_)
+    return __real_close(fd);
+
+  return 0;
+}
diff --git a/test/src/file_mock.h b/test/src/file_mock.h
new file mode 100644 (file)
index 0000000..bd60881
--- /dev/null
@@ -0,0 +1,32 @@
+/*
+ * Copyright (c) 2021 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 __TTRACE_FILE_MOCK_H__
+#define __TTRACE_FILE_MOCK_H__
+
+#include <stdbool.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+void file_mock_setup(bool flag);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __TTRACE_FILE_MOCK_H__ */
index 4df01099aaf25b9a1df7fed2c6fd4a47d7c1d9b7..6b32aba428586e22db8a46c6a9e52c9bbde262ae 100644 (file)
 
 #include <gtest/gtest.h>
 
+#include <stdio.h>
+
 #include <trace.h>
 
 #include <string>
 #include <vector>
 #include <memory>
 
+#include "file_mock.h"
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+
 class TtraceTest : public testing::Test {
  public:
   TtraceTest() {}
 
   virtual ~TtraceTest() {}
 
-  virtual void SetUp() {}
+  virtual void SetUp() { file_mock_setup(true); }
 
-  virtual void TearDown() {}
+  virtual void TearDown() { file_mock_setup(false); }
 };
 
-TEST(TtraceTest, trace_begin) {
+TEST_F(TtraceTest, trace_begin) {
   int ret = -1;
 
   trace_begin("fmt");
@@ -43,7 +51,7 @@ TEST(TtraceTest, trace_begin) {
   EXPECT_EQ(ret, 0);
 }
 
-TEST(TtraceTest, trace_end) {
+TEST_F(TtraceTest, trace_end) {
   int ret = -1;
 
   trace_end();
@@ -53,7 +61,7 @@ TEST(TtraceTest, trace_end) {
   EXPECT_EQ(ret, 0);
 }
 
-TEST(TtraceTest, trace_async_begin) {
+TEST_F(TtraceTest, trace_async_begin) {
   int ret = -1;
 
   trace_async_begin(1, "fmt");
@@ -63,7 +71,7 @@ TEST(TtraceTest, trace_async_begin) {
   EXPECT_EQ(ret, 0);
 }
 
-TEST(TtraceTest, trace_async_end) {
+TEST_F(TtraceTest, trace_async_end) {
   int ret = -1;
 
   trace_async_end(1, "fmt");
@@ -73,7 +81,7 @@ TEST(TtraceTest, trace_async_end) {
   EXPECT_EQ(ret, 0);
 }
 
-TEST(TtraceTest, trace_update_counter) {
+TEST_F(TtraceTest, trace_update_counter) {
   int ret = -1;
 
   trace_update_counter(1, "fmt");