From: Junghyun Yeon Date: Mon, 27 Jul 2020 02:20:51 +0000 (+0900) Subject: Add unit tests and mocks for it X-Git-Tag: submit/tizen/20200814.081404~4 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=c2eece25900763b71b4864c0ba2ac25a8292c739;p=platform%2Fcore%2Fappfw%2Funified-backend.git Add unit tests and mocks for it Change-Id: I79018783e33ca40beadda6af43ac1d613c36482a Signed-off-by: Junghyun Yeon --- diff --git a/CMakeLists.txt b/CMakeLists.txt index ddad75b..5f3d956 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -44,12 +44,15 @@ PKG_CHECK_MODULES(APP_INSTALLERS_DEPS REQUIRED app-installers) PKG_CHECK_MODULES(GUM_DEPS REQUIRED libgum) PKG_CHECK_MODULES(MANIFEST_PARSER_DEPS REQUIRED manifest-parser) PKG_CHECK_MODULES(MINIZIP_DEPS REQUIRED minizip) +PKG_CHECK_MODULES(PKGMGR_INFO_DEPS REQUIRED pkgmgr-info) PKG_CHECK_MODULES(PKGMGR_INSTALLER_DEPS REQUIRED pkgmgr-installer) PKG_CHECK_MODULES(PKGMGR_TYPES_DEPS REQUIRED pkgmgr-types) PKG_CHECK_MODULES(TPK_INSTALLER_DEPS REQUIRED tpk-installer) PKG_CHECK_MODULES(VCONF_DEPS REQUIRED vconf) PKG_CHECK_MODULES(WGT_INSTALLER_DEPS REQUIRED wgt-installer) PKG_CHECK_MODULES(GMOCK_DEPS REQUIRED gmock) +PKG_CHECK_MODULES(GIO_DEPS REQUIRED gio-2.0) +PKG_CHECK_MODULES(GLIB_DEPS REQUIRED glib-2.0) FIND_PACKAGE(Boost REQUIRED COMPONENTS system filesystem regex program_options) diff --git a/packaging/unified-backend.spec b/packaging/unified-backend.spec index 4dfe133..b21e44d 100644 --- a/packaging/unified-backend.spec +++ b/packaging/unified-backend.spec @@ -19,12 +19,15 @@ BuildRequires: pkgconfig(app-installers) BuildRequires: pkgconfig(libgum) BuildRequires: pkgconfig(manifest-parser) BuildRequires: pkgconfig(minizip) +BuildRequires: pkgconfig(pkgmgr-info) BuildRequires: pkgconfig(pkgmgr-installer) BuildRequires: pkgconfig(pkgmgr-types) BuildRequires: pkgconfig(tpk-installer) BuildRequires: pkgconfig(vconf) BuildRequires: pkgconfig(wgt-installer) BuildRequires: pkgconfig(gmock) +BuildRequires: pkgconfig(gio-2.0) +BuildRequires: pkgconfig(glib-2.0) %if 0%{?gcov:1} BuildRequires: lcov @@ -79,6 +82,7 @@ find . -name '*.gcno' ! -path './gcov-obj/*' -exec cp '{}' gcov-obj ';' %endif %check +export LD_LIBRARY_PATH=../../src/unified ctest -V %if 0%{?gcov:1} lcov -c --ignore-errors graph --no-external -q -d . -o unified-backend.info diff --git a/test/unit_tests/CMakeLists.txt b/test/unit_tests/CMakeLists.txt index 8d7e80b..200c070 100644 --- a/test/unit_tests/CMakeLists.txt +++ b/test/unit_tests/CMakeLists.txt @@ -1,19 +1,25 @@ AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR}/ UNIT_TESTS_SRCS) +AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR}/mock/ UNIT_TESTS_SRCS) ADD_EXECUTABLE(${TARGET_UNIFIED_INSTALLER_UNIT_TEST} ${UNIT_TESTS_SRCS} ) -#TARGET_INCLUDE_DIRECTORIES(${TARGET_UNIFIED_INSTALLER_UNIT_TEST} PUBLIC -# "${CMAKE_CURRENT_SOURCE_DIR}/../" -# "${CMAKE_CURRENT_SOURCE_DIR}/../../src" -#) +TARGET_INCLUDE_DIRECTORIES(${TARGET_UNIFIED_INSTALLER_UNIT_TEST} PUBLIC + "${CMAKE_CURRENT_SOURCE_DIR}/../" + "${CMAKE_CURRENT_SOURCE_DIR}/../../src" +) APPLY_PKG_CONFIG(${TARGET_UNIFIED_INSTALLER_UNIT_TEST} PUBLIC GMOCK_DEPS + APP_INSTALLERS_DEPS + GIO_DEPS + GLIB_DEPS + GUM_DEPS + PKGMGR_INFO_DEPS ) -TARGET_LINK_LIBRARIES(${TARGET_UNIFIED_INSTALLER_UNIT_TEST} PUBLIC ${TARGET_LIBNAME_UNIFIED} ${TARGET_TPK_SMOKE_UTILS} ${TARGET_WGT_SMOKE_UTILS}) +TARGET_LINK_LIBRARIES(${TARGET_UNIFIED_INSTALLER_UNIT_TEST} PRIVATE ${TARGET_LIBNAME_UNIFIED}) SET_TARGET_PROPERTIES(${TARGET_UNIFIED_INSTALLER_UNIT_TEST} PROPERTIES COMPILE_FLAGS "-fPIE") SET_TARGET_PROPERTIES(${TARGET_UNIFIED_INSTALLER_UNIT_TEST} PROPERTIES LINK_FLAGS "-pie") diff --git a/test/unit_tests/mock/gum_mock.cc b/test/unit_tests/mock/gum_mock.cc new file mode 100644 index 0000000..491d9ff --- /dev/null +++ b/test/unit_tests/mock/gum_mock.cc @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2020 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 "unit_tests/mock/gum_mock.h" + +#include +#include +#include + +#include "unit_tests/mock/mock_hook.h" +#include "unit_tests/mock/test_fixture.h" + +extern "C" GumUser* gum_user_get_sync(uid_t uid, gboolean offline) { + return MOCK_HOOK_P2(GumMock, gum_user_get_sync, uid, offline); +} diff --git a/test/unit_tests/mock/gum_mock.h b/test/unit_tests/mock/gum_mock.h new file mode 100644 index 0000000..6581f27 --- /dev/null +++ b/test/unit_tests/mock/gum_mock.h @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2020 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 TEST_UNIT_TESTS_MOCK_GUM_MOCK_H_ +#define TEST_UNIT_TESTS_MOCK_GUM_MOCK_H_ + +#include +#include +#include + +#include + +#include "unit_tests/mock/module_mock.h" + +class GumMock : public virtual ModuleMock { + public: + virtual ~GumMock() {} +#if 1 + MOCK_METHOD2(gum_user_get_sync, GumUser*(uid_t, gboolean)); +#else + MOCK_METHOD3(g_bus_get_sync, + GDBusConnection*(GBusType, GCancellable*, GError**)); + MOCK_METHOD7(g_bus_own_name_on_connection, + guint(GDBusConnection*, const gchar*, GBusNameOwnerFlags, + GBusNameAcquiredCallback, GBusNameLostCallback, + gpointer, GDestroyNotify)); + + MOCK_METHOD4(g_dbus_message_new_method_call, + GDBusMessage*(const gchar*, const gchar*, const gchar*, const gchar*)); + MOCK_METHOD2(g_dbus_message_set_body, void(GDBusMessage*, GVariant*)); + MOCK_METHOD1(g_dbus_message_get_body, GVariant*(GDBusMessage*)); + + MOCK_METHOD7(g_dbus_connection_send_message_with_reply_sync, + GDBusMessage*(GDBusConnection*, GDBusMessage*, GDBusSendMessageFlags, + gint, volatile guint32*, GCancellable*, GError**)); + MOCK_METHOD7(g_dbus_connection_emit_signal, + gboolean(GDBusConnection*, const gchar*, const gchar*, const gchar*, + const gchar*, GVariant*, GError**)); + MOCK_METHOD7(g_dbus_connection_register_object, + guint(GDBusConnection*, const gchar*, GDBusInterfaceInfo*, + const GDBusInterfaceVTable*, gpointer, GDestroyNotify, GError**)); + MOCK_METHOD10(g_dbus_connection_signal_subscribe, + guint(GDBusConnection*, const gchar*, const gchar*, const gchar*, + const gchar*, const gchar*, GDBusSignalFlags, GDBusSignalCallback, + gpointer, GDestroyNotify)); + MOCK_METHOD8(g_dbus_connection_send_message_with_reply, + void(GDBusConnection*, GDBusMessage*, GDBusSendMessageFlags, gint, + volatile guint32*, GCancellable*, GAsyncReadyCallback, gpointer)); + MOCK_METHOD3(g_dbus_connection_send_message_with_reply_finish, + GDBusMessage*(GDBusConnection*, GAsyncResult*, GError**)); + + MOCK_METHOD2(g_dbus_node_info_new_for_xml, + GDBusNodeInfo*(const gchar*, GError**)); + + MOCK_METHOD2(g_dbus_method_invocation_return_value, + GDBusNodeInfo*(GDBusMethodInvocation*, GVariant*)); +#endif +}; + +#endif // TEST_UNIT_TESTS_MOCK_GUM_MOCK_H_ diff --git a/test/unit_tests/mock/mock_hook.h b/test/unit_tests/mock/mock_hook.h new file mode 100644 index 0000000..b57f369 --- /dev/null +++ b/test/unit_tests/mock/mock_hook.h @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2020 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 TEST_UNIT_TESTS_MOCK_MOCK_HOOK_H_ +#define TEST_UNIT_TESTS_MOCK_MOCK_HOOK_H_ + +#define MOCK_HOOK_P0(MOCK_CLASS, f) \ + TestFixture::GetMock().f() +#define MOCK_HOOK_P1(MOCK_CLASS, f, p1) \ + TestFixture::GetMock().f(p1) +#define MOCK_HOOK_P2(MOCK_CLASS, f, p1, p2) \ + TestFixture::GetMock().f(p1, p2) +#define MOCK_HOOK_P3(MOCK_CLASS, f, p1, p2, p3) \ + TestFixture::GetMock().f(p1, p2, p3) +#define MOCK_HOOK_P4(MOCK_CLASS, f, p1, p2, p3, p4) \ + TestFixture::GetMock().f(p1, p2, p3, p4) +#define MOCK_HOOK_P5(MOCK_CLASS, f, p1, p2, p3, p4, p5) \ + TestFixture::GetMock().f(p1, p2, p3, p4, p5) +#define MOCK_HOOK_P6(MOCK_CLASS, f, p1, p2, p3, p4, p5, p6) \ + TestFixture::GetMock().f(p1, p2, p3, p4, p5, p6) +#define MOCK_HOOK_P7(MOCK_CLASS, f, p1, p2, p3, p4, p5, p6, p7) \ + TestFixture::GetMock().f(p1, p2, p3, p4, p5, p6, p7) +#define MOCK_HOOK_P8(MOCK_CLASS, f, p1, p2, p3, p4, p5, p6, p7, p8) \ + TestFixture::GetMock().f(p1, p2, p3, p4, p5, p6, p7, p8) +#define MOCK_HOOK_P10(MOCK_CLASS, f, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10) \ + TestFixture::GetMock().f( \ + p1, p2, p3, p4, p5, p6, p7, p8, p9, p10) + +#endif // TEST_UNIT_TESTS_MOCK_MOCK_HOOK_H_ diff --git a/test/unit_tests/mock/module_mock.h b/test/unit_tests/mock/module_mock.h new file mode 100644 index 0000000..b8163e9 --- /dev/null +++ b/test/unit_tests/mock/module_mock.h @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2020 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 TEST_UNIT_TESTS_MOCK_MODULE_MOCK_H_ +#define TEST_UNIT_TESTS_MOCK_MODULE_MOCK_H_ + +class ModuleMock { + public: + virtual ~ModuleMock() {} +}; + +#endif // TEST_UNIT_TESTS_MOCK_MODULE_MOCK_H_ diff --git a/test/unit_tests/mock/pkgmgr_info_mock.cc b/test/unit_tests/mock/pkgmgr_info_mock.cc new file mode 100644 index 0000000..28afa21 --- /dev/null +++ b/test/unit_tests/mock/pkgmgr_info_mock.cc @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2020 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 "unit_tests/mock/pkgmgr_info_mock.h" + +#include + +#include "unit_tests/mock/mock_hook.h" +#include "unit_tests/mock/test_fixture.h" + +extern "C" int pkgmgrinfo_pkginfo_get_usr_all_pkginfo( + const char* pkgid, uid_t uid, pkgmgrinfo_pkginfo_h* handle) { + return MOCK_HOOK_P3(PkgmgrInfoMock, + pkgmgrinfo_pkginfo_get_usr_all_pkginfo, pkgid, uid, handle); +} + +extern "C" int pkgmgrinfo_pkginfo_get_type(pkgmgrinfo_pkginfo_h handle, + char** type) { + return MOCK_HOOK_P2(PkgmgrInfoMock, pkgmgrinfo_pkginfo_get_type, handle, + type); +} diff --git a/test/unit_tests/mock/pkgmgr_info_mock.h b/test/unit_tests/mock/pkgmgr_info_mock.h new file mode 100644 index 0000000..147bdf1 --- /dev/null +++ b/test/unit_tests/mock/pkgmgr_info_mock.h @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2020 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 TEST_UNIT_TESTS_MOCK_PKGMGR_INFO_MOCK_H_ +#define TEST_UNIT_TESTS_MOCK_PKGMGR_INFO_MOCK_H_ + +#include +#include + +#include "unit_tests/mock/module_mock.h" + +class PkgmgrInfoMock : public virtual ModuleMock { + public: + virtual ~PkgmgrInfoMock() {} + + MOCK_METHOD3(pkgmgrinfo_pkginfo_get_usr_all_pkginfo, + int(const char*, uid_t, pkgmgrinfo_pkginfo_h*)); + MOCK_METHOD2(pkgmgrinfo_pkginfo_get_type, + int(pkgmgrinfo_pkginfo_h, char**)); +}; + +#endif // TEST_UNIT_TESTS_MOCK_PKGMGR_INFO_MOCK_H_ diff --git a/test/unit_tests/mock/test_fixture.cc b/test/unit_tests/mock/test_fixture.cc new file mode 100644 index 0000000..5d7035c --- /dev/null +++ b/test/unit_tests/mock/test_fixture.cc @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2020 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 "unit_tests/mock/test_fixture.h" +#include + +std::unique_ptr TestFixture::mock_; diff --git a/test/unit_tests/mock/test_fixture.h b/test/unit_tests/mock/test_fixture.h new file mode 100644 index 0000000..6472a18 --- /dev/null +++ b/test/unit_tests/mock/test_fixture.h @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2020 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 TEST_UNIT_TESTS_MOCK_TEST_FIXTURE_H_ +#define TEST_UNIT_TESTS_MOCK_TEST_FIXTURE_H_ + +#include + +#include +#include +#include +#include + +#include "unit_tests/mock/module_mock.h" + +class TestFixture : public ::testing::Test { + public: + explicit TestFixture(std::unique_ptr&& mock) { + mock_ = std::move(mock); + } + virtual ~TestFixture() { + mock_.reset(); + } + + virtual void SetUp() {} + virtual void TearDown() {} + + template + static T& GetMock() { + auto ptr = dynamic_cast(mock_.get()); + if (!ptr) + throw std::invalid_argument("The test does not provide mock of \"" + + std::string(typeid(T).name()) + "\""); + return *ptr; + } + + static std::unique_ptr mock_; +}; + +#endif // TEST_UNIT_TESTS_MOCK_TEST_FIXTURE_H_ diff --git a/test/unit_tests/test_samples/test-tpk-pkg.tpk b/test/unit_tests/test_samples/test-tpk-pkg.tpk new file mode 100644 index 0000000..c6f7890 Binary files /dev/null and b/test/unit_tests/test_samples/test-tpk-pkg.tpk differ diff --git a/test/unit_tests/test_samples/test-wgt-pkg.wgt b/test/unit_tests/test_samples/test-wgt-pkg.wgt new file mode 100644 index 0000000..888ec34 Binary files /dev/null and b/test/unit_tests/test_samples/test-wgt-pkg.wgt differ diff --git a/test/unit_tests/test_samples/tpk-recovery-123456 b/test/unit_tests/test_samples/tpk-recovery-123456 new file mode 100644 index 0000000..9b58d3c --- /dev/null +++ b/test/unit_tests/test_samples/tpk-recovery-123456 @@ -0,0 +1,4 @@ +NEW +/opt/usr/share/package-unpacked/unpack-265cd0 +org.example.tpk40sample2 +false diff --git a/test/unit_tests/test_samples/wgt-recovery-123456 b/test/unit_tests/test_samples/wgt-recovery-123456 new file mode 100644 index 0000000..8286f65 --- /dev/null +++ b/test/unit_tests/test_samples/wgt-recovery-123456 @@ -0,0 +1,4 @@ +NEW +/opt/usr/share/package-unpacked/unpack-265cd0 +vHhZK6lhQg +false diff --git a/test/unit_tests/test_unified_installer_factory.cc b/test/unit_tests/test_unified_installer_factory.cc index ad6b839..a130f28 100644 --- a/test/unit_tests/test_unified_installer_factory.cc +++ b/test/unit_tests/test_unified_installer_factory.cc @@ -14,17 +14,45 @@ * limitations under the License. */ +#include +#include #include -#include +#include +#include #include #include +#include "common/installer/app_installer.h" #include "unified/unified_installer_factory.h" -class UnifiedInstallerFactoryTest : public testing::Test { +#include "unit_tests/mock/gum_mock.h" +#include "unit_tests/mock/pkgmgr_info_mock.h" +#include "unit_tests/mock/test_fixture.h" + +#include "common/pkgmgr_interface.h" + + + +using ::testing::_; +using ::testing::DoAll; +using ::testing::Invoke; +using ::testing::StrEq; +using ::testing::SetArgPointee; +using ::testing::Return; + +static GumUser* __gum_user_get_sync_fake( + uid_t, gboolean) { + return nullptr; +} + +class Mocks : public ::testing::NiceMock, + public ::testing::NiceMock{}; + +class UnifiedInstallerFactoryTest : public TestFixture { public: + UnifiedInstallerFactoryTest() : TestFixture(std::make_unique()) {} virtual ~UnifiedInstallerFactoryTest() {} virtual void SetUp() {} @@ -32,8 +60,195 @@ class UnifiedInstallerFactoryTest : public testing::Test { virtual void TestDown() {} }; -TEST_F(UnifiedInstallerFactoryTest, CreateInstaller_P) { - // template testcase. +class TestPkgmgrInstaller : public common_installer::PkgmgrInstallerInterface { + public: + bool CreatePkgMgrInstaller(pkgmgr_installer** installer, + common_installer::InstallationMode* mode) { + *installer = pkgmgr_installer_offline_new(); + if (!*installer) + return false; + *mode = common_installer::InstallationMode::ONLINE; + return true; + } + + bool ShouldCreateSignal() const { + return false; + } +}; + +TEST_F(UnifiedInstallerFactoryTest, CreateInstaller_GetPkgTypeFromPkgID) { + std::vector arguments = + {"unified-backend", "-i", "tpk_pkgid", "wgt_pkgid"}; + std::vector argv; + for (const auto& arg : arguments) + argv.push_back(const_cast( + reinterpret_cast(arg.data()))); + argv.push_back(nullptr); + + TestPkgmgrInstaller pkgmgr_installer; + EXPECT_CALL(GetMock(), + gum_user_get_sync(_, _)). + WillRepeatedly(Invoke(__gum_user_get_sync_fake)); + + auto pkgmgr = common_installer::PkgMgrInterface::Create( + argv.size() - 1, + argv.data(), + &pkgmgr_installer, + nullptr); + if (!pkgmgr) { + ASSERT_TRUE(false); + } + + std::unique_ptrfactory( + new common_installer::UnifiedInstallerFactory()); + + pkgmgrinfo_pkginfo_h handle1 = calloc(1, sizeof(manifest_x)); + pkgmgrinfo_pkginfo_h handle2 = calloc(1, sizeof(manifest_x)); + + EXPECT_CALL(GetMock(), + pkgmgrinfo_pkginfo_get_usr_all_pkginfo(StrEq("tpk_pkgid"), _, _)). + WillOnce(DoAll( + SetArgPointee<2>( + reinterpret_cast(handle1)), + Return(PMINFO_R_OK))); + EXPECT_CALL(GetMock(), + pkgmgrinfo_pkginfo_get_usr_all_pkginfo(StrEq("wgt_pkgid"), _, _)). + WillOnce(DoAll( + SetArgPointee<2>( + reinterpret_cast(handle2)), + Return(PMINFO_R_OK))); + char wgt[] = "wgt"; + char tpk[] = "tpk"; + + EXPECT_CALL(GetMock(), + pkgmgrinfo_pkginfo_get_type( + reinterpret_cast(handle1), _)). + WillOnce(DoAll( + SetArgPointee<1>(const_cast(tpk)), + Return(PMINFO_R_OK))); + EXPECT_CALL(GetMock(), + pkgmgrinfo_pkginfo_get_type( + reinterpret_cast(handle2), _)). + WillOnce(DoAll( + SetArgPointee<1>(const_cast(wgt)), + Return(PMINFO_R_OK))); + + EXPECT_EQ(pkgmgr->GetRequestInfoCount(), 2); + + for (int idx = 0; idx < 2; idx++) { + std::unique_ptr installer = + factory->CreateInstaller(pkgmgr, idx); + // TODO(jungh.yeon): Need to add ways about validating instance. + EXPECT_NE(installer, nullptr); + } +} + +TEST_F(UnifiedInstallerFactoryTest, CreateInstaller_GetPkgTypeFromFilename) { + std::vector arguments = + {"unified-backend", "-b", "test_samples/tpk-recovery-123456", + "test_samples/wgt-recovery-123456"}; + std::vector argv; + for (const auto& arg : arguments) + argv.push_back(const_cast( + reinterpret_cast(arg.data()))); + argv.push_back(nullptr); + + pkgmgrinfo_pkginfo_h handle1 = calloc(1, sizeof(manifest_x)); + pkgmgrinfo_pkginfo_h handle2 = calloc(1, sizeof(manifest_x)); + + EXPECT_CALL(GetMock(), + pkgmgrinfo_pkginfo_get_usr_all_pkginfo( + StrEq("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"), _, _)). + WillOnce(DoAll( + SetArgPointee<2>( + reinterpret_cast(handle2)), + Return(PMINFO_R_OK))); + + TestPkgmgrInstaller pkgmgr_installer; + EXPECT_CALL(GetMock(), + gum_user_get_sync(_, _)). + WillRepeatedly(Invoke(__gum_user_get_sync_fake)); + + auto pkgmgr = common_installer::PkgMgrInterface::Create( + argv.size() - 1, + argv.data(), + &pkgmgr_installer, + nullptr); + if (!pkgmgr) { + ASSERT_TRUE(false); + } + + EXPECT_EQ(pkgmgr->GetRequestInfoCount(), 2); + + std::unique_ptrfactory( + new common_installer::UnifiedInstallerFactory()); + + for (int idx = 0; idx < 2; idx++) { + std::unique_ptr installer = + factory->CreateInstaller(pkgmgr, idx); + // TODO(jungh.yeon): Need to add ways about validating instance. + EXPECT_NE(installer, nullptr); + } +} + +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 argv; + for (const auto& arg : arguments) + argv.push_back(const_cast( + reinterpret_cast(arg.data()))); + argv.push_back(nullptr); + + TestPkgmgrInstaller pkgmgr_installer; + EXPECT_CALL(GetMock(), + gum_user_get_sync(_, _)). + WillRepeatedly(Invoke(__gum_user_get_sync_fake)); + + pkgmgrinfo_pkginfo_h handle1 = calloc(1, sizeof(manifest_x)); + pkgmgrinfo_pkginfo_h handle2 = calloc(1, sizeof(manifest_x)); + + EXPECT_CALL(GetMock(), + pkgmgrinfo_pkginfo_get_usr_all_pkginfo( + StrEq("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"), _, _)). + WillOnce(DoAll( + SetArgPointee<2>( + reinterpret_cast(handle2)), + Return(PMINFO_R_OK))); + + auto pkgmgr = common_installer::PkgMgrInterface::Create( + argv.size() - 1, + argv.data(), + &pkgmgr_installer, + nullptr); + if (!pkgmgr) { + ASSERT_TRUE(false); + } + + EXPECT_EQ(pkgmgr->GetRequestInfoCount(), 2); + + std::unique_ptrfactory( + new common_installer::UnifiedInstallerFactory()); - ASSERT_TRUE(true); + for (int idx = 0; idx < 2; idx++) { + std::unique_ptr installer = + factory->CreateInstaller(pkgmgr, idx); + // TODO(jungh.yeon): Need to add ways about validating instance. + EXPECT_NE(installer, nullptr); + } }