From 2af94878d80eba45e88f2a23dc67df1954b845a1 Mon Sep 17 00:00:00 2001 From: ilho kim Date: Wed, 26 Jul 2023 15:51:57 +0900 Subject: [PATCH] Change the path of the file used in the unit test Depending on the path through which the unit test is executed the path of the unintended file can be used so the relative path of the directory where the unit test executable is located is obtained and used Change-Id: I24d351b2dac7e14a456ee1684bf64bb1716ca207 Signed-off-by: ilho kim --- CMakeLists.txt | 8 +-- packaging/unified-backend.spec | 1 + test/unit_tests/CMakeLists.txt | 1 + test/unit_tests/test_unified_installer_factory.cc | 67 ++++++++++++++++------- 4 files changed, 53 insertions(+), 24 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 91e800d..d75d385 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,12 +13,12 @@ ENDIF(NOT CMAKE_BUILD_TYPE) # Compiler flags SET(EXTRA_FLAGS "-Wall -Wextra -Werror=shadow") SET(CMAKE_C_FLAGS_PROFILING "-O2 ${EXTRA_FLAGS}") -SET(CMAKE_CXX_FLAGS_PROFILING "-O2 -std=c++14 ${EXTRA_FLAGS}") +SET(CMAKE_CXX_FLAGS_PROFILING "-O2 -std=c++17 ${EXTRA_FLAGS}") SET(CMAKE_C_FLAGS_DEBUG "-O0 -g ${EXTRA_FLAGS}") -SET(CMAKE_CXX_FLAGS_DEBUG "-O0 -std=c++14 -g ${EXTRA_FLAGS}") +SET(CMAKE_CXX_FLAGS_DEBUG "-O0 -std=c++17 -g ${EXTRA_FLAGS}") SET(CMAKE_C_FLAGS_RELEASE "-O2 -g ${EXTRA_FLAGS}") -SET(CMAKE_CXX_FLAGS_RELEASE "-O2 -std=c++14 -g ${EXTRA_FLAGS}") -SET(CMAKE_CXX_FLAGS_CCOV "-O0 -std=c++14 -g --coverage ${EXTRA_FLAGS}") +SET(CMAKE_CXX_FLAGS_RELEASE "-O2 -std=c++17 -g ${EXTRA_FLAGS}") +SET(CMAKE_CXX_FLAGS_CCOV "-O0 -std=c++17 -g --coverage ${EXTRA_FLAGS}") # Linker flags SET(EXTRA_LINKER_FLAGS "-Wl,--as-needed") diff --git a/packaging/unified-backend.spec b/packaging/unified-backend.spec index 35f6031..2361283 100644 --- a/packaging/unified-backend.spec +++ b/packaging/unified-backend.spec @@ -173,4 +173,5 @@ Simple string key-val dictionary ADT gcov objects %files unittests %{_bindir}/unified-installer-ut/unified-installer-unit-test +%{_bindir}/unified-installer-ut/test_samples/* %{_bindir}/tizen-unittests/%{name}/run-unittest.sh diff --git a/test/unit_tests/CMakeLists.txt b/test/unit_tests/CMakeLists.txt index 0c34181..3839508 100644 --- a/test/unit_tests/CMakeLists.txt +++ b/test/unit_tests/CMakeLists.txt @@ -25,6 +25,7 @@ TARGET_LINK_LIBRARIES(${TARGET_UNIFIED_INSTALLER_UNIT_TEST} PRIVATE ${TARGET_LIB SET_TARGET_PROPERTIES(${TARGET_UNIFIED_INSTALLER_UNIT_TEST} PROPERTIES COMPILE_FLAGS "-fPIE") SET_TARGET_PROPERTIES(${TARGET_UNIFIED_INSTALLER_UNIT_TEST} PROPERTIES LINK_FLAGS "-pie") INSTALL(TARGETS ${TARGET_UNIFIED_INSTALLER_UNIT_TEST} DESTINATION ${BINDIR}/${DESTINATION_DIR}) +INSTALL(DIRECTORY test_samples/ DESTINATION ${BINDIR}/${DESTINATION_DIR}/test_samples) ADD_TEST( NAME ${TARGET_UNIFIED_INSTALLER_UNIT_TEST} diff --git a/test/unit_tests/test_unified_installer_factory.cc b/test/unit_tests/test_unified_installer_factory.cc index 61b3411..110fe55 100644 --- a/test/unit_tests/test_unified_installer_factory.cc +++ b/test/unit_tests/test_unified_installer_factory.cc @@ -20,6 +20,7 @@ #include +#include #include #include #include @@ -57,12 +58,30 @@ class Mocks : public ::testing::NiceMock, class UnifiedInstallerFactoryTest : public TestFixture { public: - UnifiedInstallerFactoryTest() : TestFixture(std::make_unique()) {} + UnifiedInstallerFactoryTest() : TestFixture(std::make_unique()) { + char buffer[PATH_MAX]; + ssize_t len = readlink("/proc/self/exe", buffer, sizeof(buffer) - 1); + EXPECT_NE(len, -1); + if (len != -1) { + buffer[len] = '\0'; + std::filesystem::path p(buffer); + executable_dir_ = p.parent_path().string(); + } else { + executable_dir_ = ""; + } + } virtual ~UnifiedInstallerFactoryTest() {} virtual void SetUp() {} virtual void TestDown() {} + + const std::string& ExecutableDir() { + return executable_dir_; + } + + private: + std::string executable_dir_; }; class TestPkgmgrInstaller : public common_installer::PkgmgrInstallerInterface { @@ -149,9 +168,11 @@ TEST_F(UnifiedInstallerFactoryTest, CreateInstaller_GetPkgTypeFromPkgID) { } TEST_F(UnifiedInstallerFactoryTest, CreateInstaller_GetPkgTypeFromFilename) { - std::vector arguments = - {"unified-backend", "-b", "test_samples/tpk-recovery-123456", - "test_samples/wgt-recovery-123456"}; + std::vector arguments = { + "unified-backend", "-b", + ExecutableDir() + "/test_samples/tpk-recovery-123456", + ExecutableDir() + "/test_samples/wgt-recovery-123456" + }; std::vector argv; for (const auto& arg : arguments) argv.push_back(const_cast( @@ -166,14 +187,14 @@ TEST_F(UnifiedInstallerFactoryTest, CreateInstaller_GetPkgTypeFromFilename) { EXPECT_CALL(GetMock(), pkgmgrinfo_pkginfo_get_usr_all_pkginfo( - StrEq("test_samples/tpk-recovery-123456"), _, _)). + StrEq(ExecutableDir() + "/test_samples/tpk-recovery-123456"), _, _)). WillOnce(DoAll( SetArgPointee<2>( reinterpret_cast(handle1)), Return(PMINFO_R_OK))); EXPECT_CALL(GetMock(), pkgmgrinfo_pkginfo_get_usr_all_pkginfo( - StrEq("test_samples/wgt-recovery-123456"), _, _)). + StrEq(ExecutableDir() + "/test_samples/wgt-recovery-123456"), _, _)). WillOnce(DoAll( SetArgPointee<2>( reinterpret_cast(handle2)), @@ -206,8 +227,10 @@ TEST_F(UnifiedInstallerFactoryTest, CreateInstaller_GetPkgTypeFromFilename) { } TEST_F(UnifiedInstallerFactoryTest, CreateInstaller_GetPkgTypeFromXml) { - std::vector arguments = - {"unified-backend", "-y", "test_samples/test-resource-pkg"}; + std::vector arguments = { + "unified-backend", "-y", + ExecutableDir() + "/test_samples/test-resource-pkg" + }; std::vector argv; for (const auto& arg : arguments) argv.push_back(const_cast( @@ -222,7 +245,7 @@ TEST_F(UnifiedInstallerFactoryTest, CreateInstaller_GetPkgTypeFromXml) { EXPECT_CALL(GetMock(), pkgmgrinfo_pkginfo_get_usr_all_pkginfo( - StrEq("test_samples/test-resource-pkg"), _, _)). + StrEq(ExecutableDir() + "/test_samples/test-resource-pkg"), _, _)). WillOnce(DoAll( SetArgPointee<2>( reinterpret_cast(handle1)), @@ -230,7 +253,7 @@ TEST_F(UnifiedInstallerFactoryTest, CreateInstaller_GetPkgTypeFromXml) { EXPECT_CALL(GetMock(), getUserManifestPath(_, _)). - WillRepeatedly(DoAll(Return("."))); + WillRepeatedly(DoAll(Return(""))); TestPkgmgrInstaller installer; EXPECT_CALL(GetMock(), @@ -259,9 +282,11 @@ TEST_F(UnifiedInstallerFactoryTest, CreateInstaller_GetPkgTypeFromXml) { } TEST_F(UnifiedInstallerFactoryTest, CreateInstaller_WrongRecoveryFile) { - std::vector arguments = - {"unified-backend", "-b", "test_samples/kkk-recovery-123456", - "test_samples/tgt-recovery-123456"}; + std::vector arguments = { + "unified-backend", "-b", + ExecutableDir() + "/test_samples/kkk-recovery-123456", + ExecutableDir() + "/test_samples/tgt-recovery-123456" + }; std::vector argv; for (const auto& arg : arguments) argv.push_back(const_cast( @@ -276,14 +301,14 @@ TEST_F(UnifiedInstallerFactoryTest, CreateInstaller_WrongRecoveryFile) { EXPECT_CALL(GetMock(), pkgmgrinfo_pkginfo_get_usr_all_pkginfo( - StrEq("test_samples/kkk-recovery-123456"), _, _)). + StrEq(ExecutableDir() + "/test_samples/kkk-recovery-123456"), _, _)). WillOnce(DoAll( SetArgPointee<2>( reinterpret_cast(handle1)), Return(PMINFO_R_OK))); EXPECT_CALL(GetMock(), pkgmgrinfo_pkginfo_get_usr_all_pkginfo( - StrEq("test_samples/tgt-recovery-123456"), _, _)). + StrEq(ExecutableDir() + "/test_samples/tgt-recovery-123456"), _, _)). WillOnce(DoAll( SetArgPointee<2>( reinterpret_cast(handle2)), @@ -320,9 +345,11 @@ TEST_F(UnifiedInstallerFactoryTest, CreateInstaller_WrongRecoveryFile) { } TEST_F(UnifiedInstallerFactoryTest, CreateInstaller_GetPkgTypeFromPkgFile) { - std::vector arguments = - {"unified-backend", "-i", "test_samples/test-tpk-pkg.tpk", - "test_samples/test-wgt-pkg.wgt"}; + std::vector arguments = { + "unified-backend", "-i", + ExecutableDir() + "/test_samples/test-tpk-pkg.tpk", + ExecutableDir() + "/test_samples/test-wgt-pkg.wgt" + }; std::vector argv; for (const auto& arg : arguments) argv.push_back(const_cast( @@ -339,14 +366,14 @@ TEST_F(UnifiedInstallerFactoryTest, CreateInstaller_GetPkgTypeFromPkgFile) { EXPECT_CALL(GetMock(), pkgmgrinfo_pkginfo_get_usr_all_pkginfo( - StrEq("test_samples/test-tpk-pkg.tpk"), _, _)). + StrEq(ExecutableDir() + "/test_samples/test-tpk-pkg.tpk"), _, _)). WillOnce(DoAll( SetArgPointee<2>( reinterpret_cast(handle1)), Return(PMINFO_R_OK))); EXPECT_CALL(GetMock(), pkgmgrinfo_pkginfo_get_usr_all_pkginfo( - StrEq("test_samples/test-wgt-pkg.wgt"), _, _)). + StrEq(ExecutableDir() + "/test_samples/test-wgt-pkg.wgt"), _, _)). WillOnce(DoAll( SetArgPointee<2>( reinterpret_cast(handle2)), -- 2.7.4