From 841070b640adafb42bf4496eaec6841ebfe7fd4c Mon Sep 17 00:00:00 2001 From: =?utf8?q?=EA=B9=80=EC=9D=BC=ED=98=B8/Tizen=20Platform=20Lab=28SR=29/?= =?utf8?q?Engineer/=EC=82=BC=EC=84=B1=EC=A0=84=EC=9E=90?= Date: Fri, 5 Feb 2021 14:53:45 +0900 Subject: [PATCH] Add unittest (#31) * add unittest Signed-off-by: Ilho Kim --- CMakeLists.txt | 4 ++++ packaging/pkgmgr-info.spec | 51 ++++++++++++++++++++++++++++++++++++++++ test/CMakeLists.txt | 1 + test/unit_tests/CMakeLists.txt | 30 +++++++++++++++++++++++ test/unit_tests/main.cc | 25 ++++++++++++++++++++ test/unit_tests/test_database.cc | 44 ++++++++++++++++++++++++++++++++++ test/unit_tests/test_parcel.cc | 45 +++++++++++++++++++++++++++++++++++ test/unit_tests/test_socket.cc | 40 +++++++++++++++++++++++++++++++ 8 files changed, 240 insertions(+) create mode 100644 test/CMakeLists.txt create mode 100644 test/unit_tests/CMakeLists.txt create mode 100644 test/unit_tests/main.cc create mode 100644 test/unit_tests/test_database.cc create mode 100644 test/unit_tests/test_parcel.cc create mode 100644 test/unit_tests/test_socket.cc diff --git a/CMakeLists.txt b/CMakeLists.txt index 73f2980..c34fb15 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -34,11 +34,15 @@ SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -fvisibility=hidden") SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -g -Wall") SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS}") +ENABLE_TESTING() +SET(TARGET_PKGMGR_INFO_UNIT_TEST "pkgmgr-info-unit-test") + ################## ## build comm libraries ADD_SUBDIRECTORY(parser) ADD_SUBDIRECTORY(tool) ADD_SUBDIRECTORY(plugin_list) ADD_SUBDIRECTORY(src) +ADD_SUBDIRECTORY(test) CONFIGURE_FILE(${CMAKE_SOURCE_DIR}/pkgmgr-info.pc.in ${CMAKE_BINARY_DIR}/pkgmgr-info.pc @ONLY) CONFIGURE_FILE(pkg_db_version.txt.in pkg_db_version.txt @ONLY) diff --git a/packaging/pkgmgr-info.spec b/packaging/pkgmgr-info.spec index c7c166c..adeef48 100644 --- a/packaging/pkgmgr-info.spec +++ b/packaging/pkgmgr-info.spec @@ -19,6 +19,12 @@ BuildRequires: pkgconfig(bundle) BuildRequires: pkgconfig(capi-system-info) BuildRequires: pkgconfig(minizip) BuildRequires: pkgconfig(libsystemd) +BuildRequires: pkgconfig(gmock) + +%if 0%{?gcov:1} +BuildRequires: lcov +BuildRequires: zip +%endif %description Packager Manager infomation api for packaging @@ -64,17 +70,44 @@ Dev package for libpkgmgr_common cp %{SOURCE1001} . %build +%if 0%{?gcov:1} +export CFLAGS+=" -fprofile-arcs -ftest-coverage" +export CXXFLAGS+=" -fprofile-arcs -ftest-coverage" +export FFLAGS+=" -fprofile-arcs -ftest-coverage" +export LDFLAGS+=" -lgcov" +%endif + MAJORVER=`echo %{version} | awk 'BEGIN {FS="."}{print $1}'` %cmake . -DFULLVER=%{version} -DMAJORVER=${MAJORVER} -DUNITDIR=%{_unitdir} %__make %{?jobs:-j%jobs} +%if 0%{?gcov:1} +mkdir -p gcov-obj +find . -name '*.gcno' ! -path './gcov-obj/*' -exec cp '{}' gcov-obj ';' +%endif + +%check +export LD_LIBRARY_PATH=../../src/:../../src/client:../../src/common:../../src/server +ctest -V +%if 0%{?gcov:1} +lcov -c --ignore-errors graph --no-external -q -d . -o tizen-theme-manager.info +genhtml tizen-theme-manager.info -o tizen-theme-manager.out +zip -r tizen-theme-manager.zip tizen-theme-manager.out +install -m 0644 tizen-theme-manager.zip %{buildroot}%{_datadir}/gcov/ +%endif + %install %make_install mkdir -p %{buildroot}%{_unitdir}/basic.target.wants ln -sf ../pkg-db-recovery.service %{buildroot}%{_unitdir}/basic.target.wants/pkg-db-recovery.service +%if 0%{?gcov:1} +mkdir -p %{buildroot}%{_datadir}/gcov/obj +install -m 0644 gcov-obj/* %{buildroot}%{_datadir}/gcov/obj +%endif + %post /sbin/ldconfig @@ -135,3 +168,21 @@ ln -sf ../pkg-db-recovery.service %{buildroot}%{_unitdir}/basic.target.wants/pkg %{_libdir}/libpkgmgr_common.so %{_includedir}/pkgmgr-common/*.hh %{_includedir}/pkgmgr-common/*.h + +################################################# +# pkgmgr-info-gcov +################################################# +%if 0%{?gcov:1} +%package gcov +Summary: Packager Manager information api (gcov) +Group: Application Framework/Testing + +%description gcov +Packager Manager information api gcov objects +%endif + +%if 0%{?gcov:1} +%files gcov +%defattr(-,root,root,-) +%{_datadir}/gcov/* +%endif diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt new file mode 100644 index 0000000..c90fac8 --- /dev/null +++ b/test/CMakeLists.txt @@ -0,0 +1 @@ +ADD_SUBDIRECTORY(unit_tests) diff --git a/test/unit_tests/CMakeLists.txt b/test/unit_tests/CMakeLists.txt new file mode 100644 index 0000000..d0d7697 --- /dev/null +++ b/test/unit_tests/CMakeLists.txt @@ -0,0 +1,30 @@ +AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR}/ UNIT_TESTS_SRCS) + +INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/../../src) + +ADD_EXECUTABLE(${TARGET_PKGMGR_INFO_UNIT_TEST} + ${UNIT_TESTS_SRCS} +) + +include(FindPkgConfig) +pkg_check_modules(unit_test_pkgs REQUIRED dlog glib-2.0 gio-2.0 sqlite3 gmock) + +FOREACH(flag ${unit_test_pkgs_CFLAGS}) + SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}") +ENDFOREACH(flag) + +## Compile flags +SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${EXTRA_CFLAGS} -fpic -std=c++14") + +TARGET_LINK_LIBRARIES(${TARGET_PKGMGR_INFO_UNIT_TEST} ${libpkgs_LDFLAGS}) +TARGET_LINK_LIBRARIES(${TARGET_PKGMGR_INFO_UNIT_TEST} ${libpkgmgr-parser_LDFLAGS}) +TARGET_LINK_LIBRARIES(${TARGET_PKGMGR_INFO_UNIT_TEST} PRIVATE pkgmgr_common) +SET_TARGET_PROPERTIES(${TARGET_PKGMGR_INFO_UNIT_TEST} PROPERTIES COMPILE_FLAGS ${CFLAGS} "-fPIE -fpic") +SET_TARGET_PROPERTIES(${TARGET_PKGMGR_INFO_UNIT_TEST} PROPERTIES LINK_FLAGS "-pie") +set_target_properties(${TARGET_PKGMGR_INFO_UNIT_TEST} PROPERTIES COMPILE_FLAGS "${unit_test_pkgs_CFLAGS_str}") +target_link_libraries(${TARGET_PKGMGR_INFO_UNIT_TEST} ${unit_test_pkgs_LDFLAGS}) + +ADD_TEST( + NAME ${TARGET_PKGMGR_INFO_UNIT_TEST} + COMMAND ${TARGET_PKGMGR_INFO_UNIT_TEST} +) diff --git a/test/unit_tests/main.cc b/test/unit_tests/main.cc new file mode 100644 index 0000000..0029d65 --- /dev/null +++ b/test/unit_tests/main.cc @@ -0,0 +1,25 @@ +// Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved +// Use of this source code is governed by a apache 2.0 license that can be +// found in the LICENSE file. + +#include +#include + +int main(int argc, char** argv) { + int ret = -1; + try { + testing::InitGoogleTest(&argc, argv); + } catch(...) { + std::cout << "Exception occurred" << std::endl; + } + + try { + ret = RUN_ALL_TESTS(); + } catch (const ::testing::internal::GoogleTestFailureException& e) { + ret = -1; + std::cout << "GoogleTestFailureException was thrown:" << e.what() + << std::endl; + } + + return ret; +} diff --git a/test/unit_tests/test_database.cc b/test/unit_tests/test_database.cc new file mode 100644 index 0000000..29e0518 --- /dev/null +++ b/test/unit_tests/test_database.cc @@ -0,0 +1,44 @@ +/* + * 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. + */ + +#include + +#include + +#include "common/database/abstract_db_handler.hh" +#include "common/database/appinfo_db_handler.hh" +#include "common/database/cache_db_handler.hh" +#include "common/database/cert_get_db_handler.hh" +#include "common/database/cert_set_db_handler.hh" +#include "common/database/pkg_get_db_handler.hh" +#include "common/database/pkg_set_db_handler.hh" +#include "common/database/query_handler.hh" + +namespace pd = pkgmgr_common::database; + +class DatabaseTest : public ::testing::Test { + public: + virtual void SetUp() { + } + + virtual void TearDown() { + } +}; + +TEST_F(DatabaseTest, AppInfoDBHandler) { + pd::AppInfoDBHandler db_handler(0); + EXPECT_TRUE(db_handler.Execute()); +} diff --git a/test/unit_tests/test_parcel.cc b/test/unit_tests/test_parcel.cc new file mode 100644 index 0000000..7e1412d --- /dev/null +++ b/test/unit_tests/test_parcel.cc @@ -0,0 +1,45 @@ +/* + * 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. + */ + +#include + +#include + +#include "common/parcel/abstract_parcel.hh" +#include "common/parcel/appctrlprivinfo_parcel.hh" +#include "common/parcel/appinfo_parcel.hh" +#include "common/parcel/certinfo_parcel.hh" +#include "common/parcel/datactrlinfo_parcel.hh" +#include "common/parcel/filter_parcel.hh" +#include "common/parcel/pkginfo_parcel.hh" +#include "common/parcel/string_parcel.hh" + +namespace pp = pkgmgr_common::parcel; + +class ParcelTest : public ::testing::Test { + public: + virtual void SetUp() { + } + + virtual void TearDown() { + } +}; + +TEST_F(ParcelTest, PkgInfoParcel) { + pp::PkgInfoParcel parcel; + parcel.SetUid(10); + EXPECT_EQ(parcel.GetUid(), 10); +} diff --git a/test/unit_tests/test_socket.cc b/test/unit_tests/test_socket.cc new file mode 100644 index 0000000..aa3575a --- /dev/null +++ b/test/unit_tests/test_socket.cc @@ -0,0 +1,40 @@ +/* + * 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. + */ + +#include + +#include + +#include "common/socket/abstract_socket.hh" +#include "common/socket/client_socket.hh" +#include "common/socket/data_socket.hh" +#include "common/socket/server_socket.hh" + +namespace ps = pkgmgr_common::socket; + +class SocketTest : public ::testing::Test { + public: + virtual void SetUp() { + } + + virtual void TearDown() { + } +}; + +TEST_F(SocketTest, ClientSocket) { + ps::ClientSocket socket("test_path"); + EXPECT_EQ(socket.Connect(), 0); +} -- 2.7.4