Enable gcov 12/307912/3
authorYonggoo Kang <ygace.kang@samsung.com>
Thu, 14 Mar 2024 07:03:40 +0000 (16:03 +0900)
committerYonggoo Kang <ygace.kang@samsung.com>
Fri, 15 Mar 2024 02:31:02 +0000 (02:31 +0000)
 - Add settings
 - Change filename tests/main.cpp to unittests.cpp to distinguish it from server/main.cpp

Change-Id: I60a32e2eb1a347987bfde1e2a5aacd151dac3a7e

CMakeLists.txt
packaging/webauthn.spec
srcs/server/main.cpp
tests/CMakeLists.txt
tests/main.cpp [deleted file]
tests/unittests.cpp [new file with mode: 0644]

index bf910d526a783caa26727418e610a4c25cb6fee7..36eca2aaf526b908f90f6abc3ad5d7073c148b43 100644 (file)
@@ -35,6 +35,14 @@ SET(CMAKE_CXX_FLAGS_RELEASE    "-g -std=c++17 -O2")
 SET(CMAKE_C_FLAGS_CCOV         "-g -std=c99 -O2 --coverage")
 SET(CMAKE_CXX_FLAGS_CCOV       "-g -std=c++17 -O2 --coverage")
 
+# If gcov enabled, add build options and set the variable used in source codes
+IF(GCOV_BUILD)
+SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fprofile-arcs -ftest-coverage")
+SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage")
+SET(LDFLAGS "${LDFLAGS} -lgcov")
+ADD_DEFINITIONS("-DGCOV_BUILD=\"${GCOV_BUILD}\"")
+ENDIF(GCOV_BUILD)
+
 # If supported for the target machine, emit position-independent code,suitable
 # for dynamic linking and avoiding any limit on the size of the global offset
 # table. This option makes a difference on the m68k, PowerPC and SPARC.
index 9f763261c7a6ee6ec0fff8b91171ccb6434b9baf..9b60617efc7b64b31fcf2407905d685d2f8c82fb 100644 (file)
@@ -97,7 +97,8 @@ Web Authentication Service (unit tests)
 %setup -q
 
 %build
-%cmake . -DPREFIX=%{_prefix} \
+%cmake . -DGCOV_BUILD=%{?gcov:1}%{!?gcov:0} \
+         -DPREFIX=%{_prefix} \
          -DEXEC_PREFIX=%{_exec_prefix} \
          -DINCLUDEDIR=%{_includedir} \
          -DLIB_DIR=%{lib_dir} \
index a283f884d26b9f55a9b15ae9b5779c1c7dd7d5fd..8223a0b2b17d9a25961a0b6069677bebacf3dce7 100644 (file)
 #include <service.h>
 #include <service-file-locker.h>
 
+#ifdef GCOV_BUILD
+extern "C" void __gcov_flush(void);
+#endif
+
 #define WEBAUTHN_LOG_TAG "WEBAUTHN"
 
 int webauthn_manager()
@@ -48,6 +52,10 @@ int webauthn_manager()
 
 int main(void)
 {
+#ifdef GCOV_BUILD
+    setenv("GCOV_PREFIX", "/tmp", 1);
+#endif
+    int ret = EXIT_FAILURE;
     UNHANDLED_EXCEPTION_HANDLER_BEGIN
     {
         WA::Singleton<WA::WebAuthnLog>::Instance().SetTag(WEBAUTHN_LOG_TAG);
@@ -62,11 +70,15 @@ int main(void)
             LogError("Error in pthread_sigmask");
             return EXIT_FAILURE;
         }
-        return webauthn_manager();
+        ret = webauthn_manager();
     } catch (const std::runtime_error &e) {
                LogError(e.what());
        }
     UNHANDLED_EXCEPTION_HANDLER_END
-
-    return EXIT_SUCCESS;
+#ifdef GCOV_BUILD
+    LogDebug("Flushing gcov");
+    __gcov_flush();
+#endif
+    LogDebug("Stopping server..");
+    return ret;
 }
\ No newline at end of file
index bf33e8c5d0c762a409ef20b7121396b71d591fd9..bb14e0d1a929733c69aef7c704cf72eb0ca435b1 100644 (file)
@@ -5,7 +5,7 @@ PKG_CHECK_MODULES(UNIT_TESTS_DEPS
     )
 
 SET(UNIT_TESTS_SOURCES
-    ${CMAKE_CURRENT_SOURCE_DIR}/main.cpp
+    ${CMAKE_CURRENT_SOURCE_DIR}/unittests.cpp
     ${CMAKE_CURRENT_SOURCE_DIR}/webauthn-client-test.cpp
     ${CMAKE_CURRENT_SOURCE_DIR}/serialization-test.cpp
 )
diff --git a/tests/main.cpp b/tests/main.cpp
deleted file mode 100644 (file)
index 343f8ad..0000000
+++ /dev/null
@@ -1,33 +0,0 @@
-/*
- *  Copyright (c) 2023 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
- *
- *
- * @file        main.cpp
- * @version     1.0
- * @brief       unit tests for webauthn
- */
-
-
-#include <gtest/gtest.h>
-
-int main(int argc, char *argv[])
-{
-       try {
-               ::testing::InitGoogleTest(&argc, argv);
-               return RUN_ALL_TESTS();
-       } catch (...) {
-               return 1;
-       }
-}
diff --git a/tests/unittests.cpp b/tests/unittests.cpp
new file mode 100644 (file)
index 0000000..84965df
--- /dev/null
@@ -0,0 +1,57 @@
+/*
+ *  Copyright (c) 2023 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
+ *
+ *
+ * @file        main.cpp
+ * @version     1.0
+ * @brief       unit tests for webauthn
+ */
+
+
+#include <gtest/gtest.h>
+#include <iostream>
+#include <webauthn-log.h>
+
+#define WEBAUTHN_TEST_LOG_TAG "WEBAUTHN-TEST"
+
+#ifdef GCOV_BUILD
+extern "C" void __gcov_flush(void);
+#endif
+
+int main(int argc, char *argv[])
+{
+
+    WA::Singleton<WA::WebAuthnLog>::Instance().SetTag(WEBAUTHN_TEST_LOG_TAG);
+
+    bool ret = false;
+    LogDebug("START");
+#ifdef GCOV_BUILD
+    LogDebug("SET GCOV_PREFIX");
+    setenv("GCOV_PREFIX", "/tmp", 1);
+#endif
+    try {
+        ::testing::InitGoogleTest(&argc, argv);
+        ret = RUN_ALL_TESTS();
+    } catch (...) {
+        ret = true;
+    }
+#ifdef GCOV_BUILD
+    LogDebug("Waiting 2 mins for terminating webauthn-server process to flush gcov");
+    std::cout << "Waiting 2 mins for terminating webauthn-server process to flush gcov"
+              << std::endl;
+    sleep(120);
+#endif
+    return ret;
+}