Add unit tests and mocks for it 32/239532/5
authorJunghyun Yeon <jungh.yeon@samsung.com>
Mon, 27 Jul 2020 02:20:51 +0000 (11:20 +0900)
committerJunghyun Yeon <jungh.yeon@samsung.com>
Wed, 5 Aug 2020 10:52:48 +0000 (10:52 +0000)
Change-Id: I79018783e33ca40beadda6af43ac1d613c36482a
Signed-off-by: Junghyun Yeon <jungh.yeon@samsung.com>
16 files changed:
CMakeLists.txt
packaging/unified-backend.spec
test/unit_tests/CMakeLists.txt
test/unit_tests/mock/gum_mock.cc [new file with mode: 0644]
test/unit_tests/mock/gum_mock.h [new file with mode: 0644]
test/unit_tests/mock/mock_hook.h [new file with mode: 0644]
test/unit_tests/mock/module_mock.h [new file with mode: 0644]
test/unit_tests/mock/pkgmgr_info_mock.cc [new file with mode: 0644]
test/unit_tests/mock/pkgmgr_info_mock.h [new file with mode: 0644]
test/unit_tests/mock/test_fixture.cc [new file with mode: 0644]
test/unit_tests/mock/test_fixture.h [new file with mode: 0644]
test/unit_tests/test_samples/test-tpk-pkg.tpk [new file with mode: 0644]
test/unit_tests/test_samples/test-wgt-pkg.wgt [new file with mode: 0644]
test/unit_tests/test_samples/tpk-recovery-123456 [new file with mode: 0644]
test/unit_tests/test_samples/wgt-recovery-123456 [new file with mode: 0644]
test/unit_tests/test_unified_installer_factory.cc

index ddad75b926eff5806fcca28db3565dbfd656e88a..5f3d9560208473e8d70369bcdcb43e55d0e0e8f1 100644 (file)
@@ -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)
 
index 4dfe133029ae3b3efa1c4b310c7daefef3870410..b21e44dcae0590128df75f7d2a5914be7f6c056c 100644 (file)
@@ -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
index 8d7e80ba15627a017ab7d87372ae1700ea62994e..200c070de90c8f5a06245ab15a4b7daf50afc24f 100644 (file)
@@ -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 (file)
index 0000000..491d9ff
--- /dev/null
@@ -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 <gum/gum-user.h>
+#include <gum/gum-user-service.h>
+#include <gum/common/gum-user-types.h>
+
+#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 (file)
index 0000000..6581f27
--- /dev/null
@@ -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 <gum/gum-user.h>
+#include <gum/gum-user-service.h>
+#include <gum/common/gum-user-types.h>
+
+#include <gmock/gmock.h>
+
+#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 (file)
index 0000000..b57f369
--- /dev/null
@@ -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<MOCK_CLASS>().f()
+#define MOCK_HOOK_P1(MOCK_CLASS, f, p1)                                        \
+    TestFixture::GetMock<MOCK_CLASS>().f(p1)
+#define MOCK_HOOK_P2(MOCK_CLASS, f, p1, p2)                                    \
+    TestFixture::GetMock<MOCK_CLASS>().f(p1, p2)
+#define MOCK_HOOK_P3(MOCK_CLASS, f, p1, p2, p3)                                \
+    TestFixture::GetMock<MOCK_CLASS>().f(p1, p2, p3)
+#define MOCK_HOOK_P4(MOCK_CLASS, f, p1, p2, p3, p4)                            \
+    TestFixture::GetMock<MOCK_CLASS>().f(p1, p2, p3, p4)
+#define MOCK_HOOK_P5(MOCK_CLASS, f, p1, p2, p3, p4, p5)                        \
+    TestFixture::GetMock<MOCK_CLASS>().f(p1, p2, p3, p4, p5)
+#define MOCK_HOOK_P6(MOCK_CLASS, f, p1, p2, p3, p4, p5, p6)                    \
+    TestFixture::GetMock<MOCK_CLASS>().f(p1, p2, p3, p4, p5, p6)
+#define MOCK_HOOK_P7(MOCK_CLASS, f, p1, p2, p3, p4, p5, p6, p7)                \
+    TestFixture::GetMock<MOCK_CLASS>().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<MOCK_CLASS>().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<MOCK_CLASS>().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 (file)
index 0000000..b8163e9
--- /dev/null
@@ -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 (file)
index 0000000..28afa21
--- /dev/null
@@ -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 <pkgmgr-info.h>
+
+#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 (file)
index 0000000..147bdf1
--- /dev/null
@@ -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 <gmock/gmock.h>
+#include <pkgmgr-info.h>
+
+#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 (file)
index 0000000..5d7035c
--- /dev/null
@@ -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 <memory>
+
+std::unique_ptr<ModuleMock> TestFixture::mock_;
diff --git a/test/unit_tests/mock/test_fixture.h b/test/unit_tests/mock/test_fixture.h
new file mode 100644 (file)
index 0000000..6472a18
--- /dev/null
@@ -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 <gtest/gtest.h>
+
+#include <memory>
+#include <stdexcept>
+#include <string>
+#include <utility>
+
+#include "unit_tests/mock/module_mock.h"
+
+class TestFixture : public ::testing::Test {
+ public:
+  explicit TestFixture(std::unique_ptr<ModuleMock>&& mock) {
+    mock_ = std::move(mock);
+  }
+  virtual ~TestFixture() {
+    mock_.reset();
+  }
+
+  virtual void SetUp() {}
+  virtual void TearDown() {}
+
+  template <typename T>
+  static T& GetMock() {
+    auto ptr = dynamic_cast<T*>(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<ModuleMock> 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 (file)
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 (file)
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 (file)
index 0000000..9b58d3c
--- /dev/null
@@ -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 (file)
index 0000000..8286f65
--- /dev/null
@@ -0,0 +1,4 @@
+NEW
+/opt/usr/share/package-unpacked/unpack-265cd0
+vHhZK6lhQg
+false
index ad6b8393ead14a025b1747cbf0f4e2d844db0e94..a130f2893533f0c27474850f4ec19dc4ec16c20e 100644 (file)
  * limitations under the License.
  */
 
+#include <gio/gio.h>
+#include <glib-object.h>
 #include <stdlib.h>
 
-#include <gtest/gtest.h>
+#include <pkgmgrinfo_basic.h>
 
+#include <gtest/gtest.h>
 #include <iostream>
 #include <memory>
 
+#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<GumMock>,
+              public ::testing::NiceMock<PkgmgrInfoMock>{};
+
+class UnifiedInstallerFactoryTest : public TestFixture {
  public:
+  UnifiedInstallerFactoryTest() : TestFixture(std::make_unique<Mocks>()) {}
   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<std::string> arguments =
+      {"unified-backend", "-i", "tpk_pkgid", "wgt_pkgid"};
+  std::vector<char*> argv;
+  for (const auto& arg : arguments)
+    argv.push_back(const_cast<char*>(
+            reinterpret_cast<const char*>(arg.data())));
+  argv.push_back(nullptr);
+
+  TestPkgmgrInstaller pkgmgr_installer;
+  EXPECT_CALL(GetMock<GumMock>(),
+      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_ptr<common_installer::UnifiedInstallerFactory>factory(
+      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<PkgmgrInfoMock>(),
+      pkgmgrinfo_pkginfo_get_usr_all_pkginfo(StrEq("tpk_pkgid"), _, _)).
+          WillOnce(DoAll(
+                  SetArgPointee<2>(
+                      reinterpret_cast<pkgmgrinfo_pkginfo_h>(handle1)),
+                  Return(PMINFO_R_OK)));
+  EXPECT_CALL(GetMock<PkgmgrInfoMock>(),
+      pkgmgrinfo_pkginfo_get_usr_all_pkginfo(StrEq("wgt_pkgid"), _, _)).
+          WillOnce(DoAll(
+                  SetArgPointee<2>(
+                      reinterpret_cast<pkgmgrinfo_pkginfo_h>(handle2)),
+                  Return(PMINFO_R_OK)));
+  char wgt[] = "wgt";
+  char tpk[] = "tpk";
+
+  EXPECT_CALL(GetMock<PkgmgrInfoMock>(),
+      pkgmgrinfo_pkginfo_get_type(
+          reinterpret_cast<pkgmgrinfo_pkginfo_h>(handle1), _)).
+          WillOnce(DoAll(
+                  SetArgPointee<1>(const_cast<char*>(tpk)),
+                  Return(PMINFO_R_OK)));
+  EXPECT_CALL(GetMock<PkgmgrInfoMock>(),
+      pkgmgrinfo_pkginfo_get_type(
+          reinterpret_cast<pkgmgrinfo_pkginfo_h>(handle2), _)).
+          WillOnce(DoAll(
+                  SetArgPointee<1>(const_cast<char*>(wgt)),
+                  Return(PMINFO_R_OK)));
+
+  EXPECT_EQ(pkgmgr->GetRequestInfoCount(), 2);
+
+  for (int idx = 0; idx < 2; idx++) {
+    std::unique_ptr<common_installer::AppInstaller> 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<std::string> arguments =
+      {"unified-backend", "-b", "test_samples/tpk-recovery-123456",
+      "test_samples/wgt-recovery-123456"};
+  std::vector<char*> argv;
+  for (const auto& arg : arguments)
+    argv.push_back(const_cast<char*>(
+            reinterpret_cast<const char*>(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<PkgmgrInfoMock>(),
+      pkgmgrinfo_pkginfo_get_usr_all_pkginfo(
+          StrEq("test_samples/tpk-recovery-123456"), _, _)).
+          WillOnce(DoAll(
+                  SetArgPointee<2>(
+                      reinterpret_cast<pkgmgrinfo_pkginfo_h>(handle1)),
+                  Return(PMINFO_R_OK)));
+  EXPECT_CALL(GetMock<PkgmgrInfoMock>(),
+      pkgmgrinfo_pkginfo_get_usr_all_pkginfo(
+          StrEq("test_samples/wgt-recovery-123456"), _, _)).
+          WillOnce(DoAll(
+                  SetArgPointee<2>(
+                      reinterpret_cast<pkgmgrinfo_pkginfo_h>(handle2)),
+                  Return(PMINFO_R_OK)));
+
+  TestPkgmgrInstaller pkgmgr_installer;
+  EXPECT_CALL(GetMock<GumMock>(),
+      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_ptr<common_installer::UnifiedInstallerFactory>factory(
+      new common_installer::UnifiedInstallerFactory());
+
+  for (int idx = 0; idx < 2; idx++) {
+    std::unique_ptr<common_installer::AppInstaller> 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<std::string> arguments =
+      {"unified-backend", "-i", "test_samples/test-tpk-pkg.tpk",
+      "test_samples/test-wgt-pkg.wgt"};
+  std::vector<char*> argv;
+  for (const auto& arg : arguments)
+    argv.push_back(const_cast<char*>(
+            reinterpret_cast<const char*>(arg.data())));
+  argv.push_back(nullptr);
+
+  TestPkgmgrInstaller pkgmgr_installer;
+  EXPECT_CALL(GetMock<GumMock>(),
+      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<PkgmgrInfoMock>(),
+      pkgmgrinfo_pkginfo_get_usr_all_pkginfo(
+          StrEq("test_samples/test-tpk-pkg.tpk"), _, _)).
+          WillOnce(DoAll(
+                  SetArgPointee<2>(
+                      reinterpret_cast<pkgmgrinfo_pkginfo_h>(handle1)),
+                  Return(PMINFO_R_OK)));
+  EXPECT_CALL(GetMock<PkgmgrInfoMock>(),
+      pkgmgrinfo_pkginfo_get_usr_all_pkginfo(
+          StrEq("test_samples/test-wgt-pkg.wgt"), _, _)).
+          WillOnce(DoAll(
+                  SetArgPointee<2>(
+                      reinterpret_cast<pkgmgrinfo_pkginfo_h>(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_ptr<common_installer::UnifiedInstallerFactory>factory(
+      new common_installer::UnifiedInstallerFactory());
 
-  ASSERT_TRUE(true);
+  for (int idx = 0; idx < 2; idx++) {
+    std::unique_ptr<common_installer::AppInstaller> installer =
+        factory->CreateInstaller(pkgmgr, idx);
+    // TODO(jungh.yeon): Need to add ways about validating instance.
+    EXPECT_NE(installer, nullptr);
+  }
 }