Add API unit tests 64/238464/4
authorDaehyeon Jung <darrenh.jung@samsung.com>
Tue, 14 Jul 2020 06:54:06 +0000 (15:54 +0900)
committerDaehyeon Jung <darrenh.jung@samsung.com>
Wed, 15 Jul 2020 02:27:47 +0000 (11:27 +0900)
Change-Id: I0f20462e3d380260c996b9dab6fc45bd8b74a203
Signed-off-by: Daehyeon Jung <darrenh.jung@samsung.com>
packaging/app2sd.spec
unit-tests/CMakeLists.txt
unit-tests/test_lib.cc [new file with mode: 0644]
unit-tests/test_main.cc

index 9285709..6b8701f 100644 (file)
@@ -68,7 +68,7 @@ rm -rf %{buildroot}
 
 %check
 cd unit-tests
-ctest -V
+LD_LIBRARY_PATH=../:../plugin/app2sd/ ctest -V
 
 %post -p /sbin/ldconfig
 
index cfb6cce..0d80311 100644 (file)
@@ -1,5 +1,7 @@
 AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR}/ TEST_SRCS)
 
+INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/../)
+
 ENABLE_TESTING()
 
 SET(TARGET_TEST "app2sd-test")
@@ -7,9 +9,9 @@ SET(TARGET_TEST "app2sd-test")
 ADD_EXECUTABLE(${TARGET_TEST} ${TEST_SRCS})
 
 APPLY_PKG_CONFIG(${TARGET_TEST} PUBLIC
+  app2ext_libpkgs
   GMOCK_DEPS
 )
 
-TARGET_LINK_LIBRARIES(${TARGET_TEST} PUBLIC ${TARGET_LIB_COMMON} pthread)
-
 ADD_TEST(${TARGET_TEST} ${TARGET_TEST})
+TARGET_LINK_LIBRARIES(${TARGET_TEST} app2ext "${${TARGET_TEST}_LDFLAGS}" dl gobject-2.0)
\ No newline at end of file
diff --git a/unit-tests/test_lib.cc b/unit-tests/test_lib.cc
new file mode 100644 (file)
index 0000000..d9f4104
--- /dev/null
@@ -0,0 +1,151 @@
+/*
+ * app2sd-unittest
+ *
+ * Copyright (c) 2020 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.
+ *
+ */
+
+#include <gio/gio.h>
+#include <gmock/gmock.h>
+#include "inc/app2ext_interface.h"
+
+static GVariant* __last_parameters;
+static gchar* __last_method_name;
+
+extern "C" GDBusConnection* g_bus_get_sync(GBusType type,
+    GCancellable* cancellable, GError** error) {
+  gpointer dummy = g_object_new(G_TYPE_OBJECT, nullptr);
+  return (GDBusConnection*)dummy;
+}
+
+extern "C" GDBusProxy* g_dbus_proxy_new_sync(GDBusConnection* connection,
+    GDBusProxyFlags flags, GDBusInterfaceInfo* info, const gchar* name,
+    const gchar* object_path, const gchar* interface_name,
+    GCancellable* cancellable, GError** error) {
+  gpointer dummy = g_object_new(G_TYPE_OBJECT, nullptr);
+  return (GDBusProxy*)dummy;
+}
+
+extern "C" GVariant* g_dbus_proxy_call_sync(GDBusProxy* proxy,
+    const gchar* method_name, GVariant* parameters, GDBusCallFlags flags,
+    gint timeout_msec, GCancellable* cancellable, GError** error) {
+  GVariant* gv = g_variant_new("(i)", 0);
+
+  if (parameters != nullptr) {
+    if (__last_parameters)
+        g_variant_unref(__last_parameters);
+    __last_parameters = g_variant_ref(parameters);
+  }
+
+  if (__last_method_name)
+    g_free(__last_method_name);
+
+  __last_method_name = g_strdup(method_name);
+  return gv;
+}
+
+namespace {
+class App2extInterfaceTest : public ::testing::Test {
+ protected:
+  void SetUp() override {}
+
+  void TearDown() override {
+    if (__last_parameters) {
+      g_variant_unref(__last_parameters);
+      __last_parameters = nullptr;
+    }
+    if (__last_method_name) {
+      g_free(__last_method_name);
+      __last_method_name = nullptr;
+    }
+  }
+};
+
+TEST_F(App2extInterfaceTest, Init) {
+  app2ext_handle* handle = app2ext_init(APP2EXT_INTERNAL_MEM);
+  EXPECT_THAT(handle, testing::Eq(nullptr));
+  handle = app2ext_init(APP2EXT_SD_CARD);
+  ASSERT_TRUE(handle != nullptr);
+  EXPECT_THAT(handle->type, testing::Eq(APP2EXT_SD_CARD));
+  app2ext_deinit(handle);
+}
+
+TEST_F(App2extInterfaceTest, EnableDisableExternalPkgs) {
+  // wrong parameters
+  int ret = app2ext_enable_external_pkg(nullptr);
+  EXPECT_THAT(ret, testing::Eq(-1));
+  ret = app2ext_disable_external_pkg(nullptr);
+  EXPECT_THAT(ret, testing::Eq(-1));
+
+  // normal
+  ret = app2ext_enable_external_pkg("not.exists.package");
+  EXPECT_THAT(ret, testing::Eq(0));
+  EXPECT_THAT(__last_method_name, testing::StrEq("OndemandSetupInit"));
+  gchar* pkgid;
+  gint uid;
+  g_variant_get(__last_parameters, "(si)", &pkgid, &uid);
+  EXPECT_THAT(pkgid, testing::StrEq("not.exists.package"));
+  g_free(pkgid);
+
+  ret = app2ext_disable_external_pkg("not.exists.package");
+  EXPECT_THAT(ret, testing::Eq(0));
+  EXPECT_THAT(__last_method_name, testing::StrEq("OndemandSetupExit"));
+  g_variant_get(__last_parameters, "(si)", &pkgid, &uid);
+  EXPECT_THAT(pkgid, testing::StrEq("not.exists.package"));
+  g_free(pkgid);
+
+  ret = app2ext_enable_all_external_pkgs();
+  EXPECT_THAT(ret, testing::Eq(0));
+  EXPECT_THAT(__last_method_name, testing::StrEq("EnableFullPkg"));
+
+  ret = app2ext_disable_all_external_pkgs();
+  EXPECT_THAT(ret, testing::Eq(0));
+  EXPECT_THAT(__last_method_name, testing::StrEq("DisableFullPkg"));
+}
+
+TEST_F(App2extInterfaceTest, Cleanup) {
+  // wrong parameters
+  int ret = app2ext_force_clean_pkg(nullptr);
+  EXPECT_THAT(ret, testing::Eq(-1));
+
+  // normal
+  ret = app2ext_force_clean_pkg("not.exists.package");
+  EXPECT_THAT(ret, testing::Eq(0));
+  EXPECT_THAT(__last_method_name, testing::StrEq("ForceClean"));
+  gchar* pkgid;
+  gint uid;
+  g_variant_get(__last_parameters, "(si)", &pkgid, &uid);
+  EXPECT_THAT(pkgid, testing::StrEq("not.exists.package"));
+  g_free(pkgid);
+}
+
+TEST_F(App2extInterfaceTest, Migrate) {
+  int ret = app2ext_migrate_legacy_all();
+  EXPECT_THAT(ret, testing::Eq(0));
+  EXPECT_THAT(__last_method_name, testing::StrEq("MigrateLegacyAll"));
+}
+
+TEST_F(App2extInterfaceTest, Get) {
+  // wrong parameter
+  char* ret = app2ext_usr_getname_image(nullptr, 0);
+  EXPECT_THAT(ret, testing::Eq(nullptr));
+  ret = app2ext_usr_get_image_path(nullptr, 0);
+  EXPECT_THAT(ret, testing::Eq(nullptr));
+
+  ret = app2ext_usr_getname_image("not.exists.package", 0);
+  EXPECT_TRUE(ret != nullptr);
+}
+
+}
\ No newline at end of file
index c6f2820..8be8620 100644 (file)
@@ -16,7 +16,7 @@
  * limitations under the License.
  *
  */
-
+#include <dlfcn.h>
 #include <gmock/gmock.h>
 
 namespace {
@@ -25,21 +25,29 @@ const ::testing::Environment* env = nullptr;
 
 namespace app2sd {
 
+extern "C" void* dlopen(const char* path, int flag) {
+  return dlmopen(LM_ID_BASE, "libapp2sd.so", flag);
+}
+
 class App2sdEnvironment : public ::testing::Environment {
  public:
   void SetUp() override {
-
+    dl_handle_ = dlopen("/usr/bin/libapp2sd.so", RTLD_LAZY|RTLD_GLOBAL);
+    if (dl_handle_ == nullptr)
+      abort();
   }
 
   void TearDown() override {
-
+    dlclose(dl_handle_);
   }
+
+  void* dl_handle_;
 };
 
 } // namespace app2sd
 
 int main(int argc, char* argv[]) {
-    ::testing::InitGoogleTest(&argc, argv);
-    ::env = testing::AddGlobalTestEnvironment(new app2sd::App2sdEnvironment);
-    return RUN_ALL_TESTS();
+  ::testing::InitGoogleTest(&argc, argv);
+  ::env = testing::AddGlobalTestEnvironment(new app2sd::App2sdEnvironment);
+  return RUN_ALL_TESTS();
 }