Add unit test
authorSangyoon Jang <jeremy.jang@samsung.com>
Wed, 24 Oct 2018 08:49:47 +0000 (17:49 +0900)
committer장상윤/Tizen Platform Lab(SR)/Engineer/삼성전자 <jeremy.jang@samsung.com>
Thu, 15 Nov 2018 02:48:56 +0000 (11:48 +0900)
Run /usr/bin/capi-appfw-capmgr-ut/unit-test after discovering at least
one device.

Change-Id: I261dc6b699e9b99f1b45940ae83e0b31a8cd75dc
Signed-off-by: Sangyoon Jang <jeremy.jang@samsung.com>
CMakeLists.txt
packaging/capi-appfw-capmgr.spec
src/CMakeLists.txt
src/unit_tests/CMakeLists.txt [new file with mode: 0644]
src/unit_tests/unit_test.cc [new file with mode: 0644]

index 77e1752..25bcfdc 100644 (file)
@@ -33,6 +33,7 @@ PKG_CHECK_MODULES(GLIB_DEPS REQUIRED glib-2.0)
 PKG_CHECK_MODULES(SQLITE_DEPS REQUIRED sqlite3)
 
 FIND_PACKAGE(Boost REQUIRED COMPONENTS filesystem program_options system)
+FIND_PACKAGE(GTest REQUIRED)
 INSTALL(DIRECTORY include/ DESTINATION ${INCLUDEDIR} FILES_MATCHING PATTERN "*.h")
 
 ADD_SUBDIRECTORY(src)
index ae05be9..5d90a34 100644 (file)
@@ -8,6 +8,7 @@ Source0:    %{name}-%{version}.tar.gz
 Source1001: %{name}.manifest
 BuildRequires:  boost-devel
 BuildRequires:  cmake
+BuildRequires:  gtest-devel
 BuildRequires:  pkgconfig(aul)
 BuildRequires:  pkgconfig(bundle)
 BuildRequires:  pkgconfig(capi-base-common)
@@ -27,6 +28,13 @@ Requires:  %{name} = %{version}
 %description devel
 This package contains header files of capability manager native API
 
+%package tests
+Summary:   Unit-tests for capmgr native API
+Requires:  %{name} = %{version}
+
+%description tests
+Unit-tests for capmgr native API
+
 %prep
 %setup -q
 cp %{SOURCE1001} .
@@ -57,3 +65,6 @@ MAJORVER=`echo %{version} | awk 'BEGIN {FS="."}{print $1}'`
 %{_includedir}/capability_manager_internal.h
 %{_libdir}/pkgconfig/%{name}.pc
 %{_libdir}/lib%{name}.so
+
+%files tests
+%{_bindir}/capi-appfw-capmgr-ut/*
index f4280e2..c5e8f33 100644 (file)
@@ -22,3 +22,5 @@ SET_TARGET_PROPERTIES(${TARGET_LIB_CAPMGR} PROPERTIES SOVERSION ${MAJORVER})
 INSTALL(TARGETS ${TARGET_LIB_CAPMGR} DESTINATION ${LIB_INSTALL_DIR})
 CONFIGURE_FILE(capi-appfw-capmgr.pc.in capi-appfw-capmgr.pc @ONLY)
 INSTALL(FILES capi-appfw-capmgr.pc DESTINATION ${LIB_INSTALL_DIR}/pkgconfig)
+
+ADD_SUBDIRECTORY(unit_tests)
diff --git a/src/unit_tests/CMakeLists.txt b/src/unit_tests/CMakeLists.txt
new file mode 100644 (file)
index 0000000..bd06ad2
--- /dev/null
@@ -0,0 +1,16 @@
+AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR}/ UNIT_TEST_SRCS)
+
+INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/../)
+
+SET(TARGET_UNIT_TEST "unit-test")
+
+ADD_EXECUTABLE(${TARGET_UNIT_TEST} ${UNIT_TEST_SRCS})
+
+APPLY_PKG_CONFIG(${TARGET_UNIT_TEST} PUBLIC
+  DLOG_DEPS
+  GTEST
+)
+
+TARGET_LINK_LIBRARIES(${TARGET_UNIT_TEST} PUBLIC ${TARGET_LIB_CAPMGR})
+
+INSTALL(TARGETS ${TARGET_UNIT_TEST} DESTINATION bin/capi-appfw-capmgr-ut/)
diff --git a/src/unit_tests/unit_test.cc b/src/unit_tests/unit_test.cc
new file mode 100644 (file)
index 0000000..ae68e56
--- /dev/null
@@ -0,0 +1,401 @@
+// Copyright (c) 2018 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 <gtest/gtest.h>
+
+#include <string>
+
+#include "include/capability_manager.h"
+#include "src/utils/logging.h"
+
+namespace {
+
+int DeviceForeachCbForSetup(const capmgr_device_h device, void* user_data) {
+  capmgr_device_h* clone = reinterpret_cast<capmgr_device_h*>(user_data);
+  int ret = capmgr_device_clone(device, clone);
+  if (ret != CAPMGR_ERROR_NONE)
+    return 0;
+  // use the first device
+  else
+    return 1;
+}
+
+int ApplicationForeachCbForSetup(
+    const capmgr_application_info_h remote_app_info, void* user_data) {
+  capmgr_application_info_h* clone =
+      reinterpret_cast<capmgr_application_info_h*>(user_data);
+  int ret = capmgr_application_info_clone(remote_app_info, clone);
+  if (ret != CAPMGR_ERROR_NONE)
+    return 0;
+  // use the first app
+  else
+    return 1;
+}
+
+int DeviceForeachCb(const capmgr_device_h device, void* user_data) {
+  bool* is_hit = reinterpret_cast<bool*>(user_data);
+  *is_hit = true;
+  return 1;
+}
+
+int ApplicationForeachCb(const capmgr_application_info_h remote_app_info,
+    void* user_data) {
+  bool* is_hit = reinterpret_cast<bool*>(user_data);
+  *is_hit = true;
+
+  char* appid;
+  capmgr_application_info_get_appid(remote_app_info, &appid);
+  free(appid);
+  LOG(INFO) << appid;
+  return 0;
+}
+
+const char kExtraDataKeyStr[] = "extra_key";
+const char kExtraDataValueStr[] = "extra_value";
+
+class TestEnvironment : public ::testing::Environment {
+ public:
+  TestEnvironment() : device_(nullptr), app_control_(nullptr),
+      app_(nullptr) {}
+  void SetUp() override {
+    ASSERT_EQ(capmgr_device_foreach_devices(DeviceForeachCbForSetup, &device_),
+        CAPMGR_ERROR_NONE) << "Failed to init test environment!";
+    ASSERT_NE(device_, nullptr);
+    char* device_id;
+    capmgr_device_get_device_id(device_, &device_id);
+    LOG(INFO) << "Selected device id is: " << device_id;
+    free(device_id);
+    ASSERT_EQ(capmgr_app_control_create(&app_control_), CAPMGR_ERROR_NONE);
+    ASSERT_NE(app_control_, nullptr);
+    ASSERT_EQ(capmgr_app_control_set_device(app_control_, device_),
+        CAPMGR_ERROR_NONE);
+    ASSERT_EQ(capmgr_app_control_set_operation(app_control_, "operation"),
+        CAPMGR_ERROR_NONE);
+    ASSERT_EQ(capmgr_app_control_set_uri(app_control_, "uri"),
+        CAPMGR_ERROR_NONE);
+    ASSERT_EQ(capmgr_app_control_set_mime(app_control_, "mime"),
+        CAPMGR_ERROR_NONE);
+    ASSERT_EQ(capmgr_app_control_set_appid(app_control_, "appid"),
+        CAPMGR_ERROR_NONE);
+    ASSERT_EQ(capmgr_app_control_add_extra_data(app_control_, kExtraDataKeyStr,
+            kExtraDataValueStr), CAPMGR_ERROR_NONE);
+    ASSERT_EQ(capmgr_application_info_foreach_applications(device_,
+            ApplicationForeachCbForSetup, &app_), CAPMGR_ERROR_NONE);
+  }
+  void TearDown() override {
+    capmgr_application_info_destroy(app_);
+    capmgr_app_control_destroy(app_control_);
+    capmgr_device_destroy(device_);
+  }
+
+  capmgr_device_h device_;
+  capmgr_app_control_h app_control_;
+  capmgr_application_info_h app_;
+};
+
+TestEnvironment* env = nullptr;
+
+}  // namespace
+
+namespace capmgr {
+
+class UnitTest : public ::testing::Test {
+ protected:
+  virtual void SetUp() {}
+  virtual void TearDown() {}
+};
+
+TEST_F(UnitTest, DeviceForeachDevicesPositiveTest) {
+  bool is_hit = false;
+  ASSERT_EQ(capmgr_device_foreach_devices(DeviceForeachCb, &is_hit),
+      CAPMGR_ERROR_NONE);
+  ASSERT_TRUE(is_hit);
+}
+
+TEST_F(UnitTest, DeviceForeachDevicesNegativeTest) {
+  ASSERT_EQ(capmgr_device_foreach_devices(nullptr, nullptr),
+      CAPMGR_ERROR_INVALID_PARAMETER);
+}
+
+TEST_F(UnitTest, DeviceClonePositiveTest) {
+  // need to get device
+  capmgr_device_h clone;
+  ASSERT_EQ(capmgr_device_clone(env->device_, &clone), CAPMGR_ERROR_NONE);
+}
+
+TEST_F(UnitTest, DeviceCloneNegativeTest) {
+  capmgr_device_h clone;
+  ASSERT_EQ(capmgr_device_clone(nullptr, &clone),
+      CAPMGR_ERROR_INVALID_PARAMETER);
+  ASSERT_EQ(capmgr_device_clone(env->device_, nullptr),
+      CAPMGR_ERROR_INVALID_PARAMETER);
+}
+
+TEST_F(UnitTest, DeviceDestroyPositiveTest) {
+  capmgr_device_h clone;
+  ASSERT_EQ(capmgr_device_clone(env->device_, &clone),
+      CAPMGR_ERROR_NONE);
+  ASSERT_EQ(capmgr_device_destroy(clone), CAPMGR_ERROR_NONE);
+}
+
+TEST_F(UnitTest, DeviceDestroyNegativeTest) {
+  ASSERT_EQ(capmgr_device_destroy(nullptr), CAPMGR_ERROR_INVALID_PARAMETER);
+}
+
+#define DEVICE_GET_TEST_BLOCK(POSITIVE_TESTNAME, NEGATIVE_TESTNAME, ATTR)      \
+TEST_F(UnitTest, POSITIVE_TESTNAME) {                                          \
+  char* ATTR = nullptr;                                                        \
+  ASSERT_EQ(capmgr_device_get_##ATTR(env->device_, &ATTR), CAPMGR_ERROR_NONE); \
+  ASSERT_NE(ATTR, nullptr);                                                    \
+  free(ATTR);                                                                  \
+}                                                                              \
+                                                                               \
+TEST_F(UnitTest, NEGATIVE_TESTNAME) {                                          \
+  char* ATTR;                                                                  \
+  ASSERT_EQ(capmgr_device_get_##ATTR(nullptr, &ATTR),                          \
+      CAPMGR_ERROR_INVALID_PARAMETER);                                         \
+  ASSERT_EQ(capmgr_device_get_##ATTR(env->device_, nullptr),                   \
+      CAPMGR_ERROR_INVALID_PARAMETER);                                         \
+}                                                                              \
+
+DEVICE_GET_TEST_BLOCK(DeviceGetDeviceIdPositiveTest,
+    DeviceGetDeviceIdNegativeTest, device_id)
+
+DEVICE_GET_TEST_BLOCK(DeviceGetModelNamePositiveTest,
+    DeviceGetModelNameNegativeTest, model_name)
+
+DEVICE_GET_TEST_BLOCK(DeviceGetDeviceNamePositiveTest,
+    DeviceGetDeviceNameNegativeTest, device_name)
+
+DEVICE_GET_TEST_BLOCK(DeviceGetPlatformVerPositiveTest,
+    DeviceGetPlatformVerNegativeTest, platform_ver)
+
+DEVICE_GET_TEST_BLOCK(DeviceGetProfilePositiveTest,
+    DeviceGetProfileNegativeTest, profile)
+
+DEVICE_GET_TEST_BLOCK(DeviceGetSWVerPositiveTest,
+    DeviceGetSWVerNegativeTest, sw_ver)
+
+TEST_F(UnitTest, AppControlCreatePositiveTest) {
+  capmgr_app_control_h app_control;
+  ASSERT_EQ(capmgr_app_control_create(&app_control), CAPMGR_ERROR_NONE);
+  capmgr_app_control_destroy(app_control);
+}
+
+TEST_F(UnitTest, AppControlCreateNegativeTest) {
+  ASSERT_EQ(capmgr_app_control_create(nullptr), CAPMGR_ERROR_INVALID_PARAMETER);
+}
+
+TEST_F(UnitTest, AppControlDestroyPositiveTest) {
+  capmgr_app_control_h app_control;
+  ASSERT_EQ(capmgr_app_control_create(&app_control), CAPMGR_ERROR_NONE);
+  ASSERT_EQ(capmgr_app_control_destroy(app_control), CAPMGR_ERROR_NONE);
+}
+
+TEST_F(UnitTest, AppControlDestroyNegativeTest) {
+  ASSERT_EQ(capmgr_app_control_destroy(nullptr),
+      CAPMGR_ERROR_INVALID_PARAMETER);
+}
+
+TEST_F(UnitTest, AppControlGetDevicePositiveTest) {
+  capmgr_device_h device;
+  ASSERT_EQ(capmgr_app_control_get_device(env->app_control_, &device),
+      CAPMGR_ERROR_NONE);
+  capmgr_device_destroy(device);
+}
+
+TEST_F(UnitTest, AppControlGetDeviceNegativeTest) {
+  capmgr_device_h device;
+  ASSERT_EQ(capmgr_app_control_get_device(nullptr, &device),
+      CAPMGR_ERROR_INVALID_PARAMETER);
+  ASSERT_EQ(capmgr_app_control_get_device(env->app_control_, nullptr),
+      CAPMGR_ERROR_INVALID_PARAMETER);
+}
+
+#define APP_CONTROL_GET_TEST_BLOCK(POSITIVE_TESTNAME, NEGATIVE_TESTNAME, ATTR) \
+TEST_F(UnitTest, POSITIVE_TESTNAME) {                                          \
+  char* ATTR = nullptr;                                                        \
+  ASSERT_EQ(capmgr_app_control_get_##ATTR(env->app_control_, &ATTR),           \
+      CAPMGR_ERROR_NONE);                                                      \
+  ASSERT_NE(ATTR, nullptr);                                                    \
+  free(ATTR);                                                                  \
+}                                                                              \
+                                                                               \
+TEST_F(UnitTest, NEGATIVE_TESTNAME) {                                          \
+  char* ATTR;                                                                  \
+  ASSERT_EQ(capmgr_app_control_get_##ATTR(nullptr, &ATTR),                     \
+      CAPMGR_ERROR_INVALID_PARAMETER);                                         \
+  ASSERT_EQ(capmgr_app_control_get_##ATTR(env->app_control_, nullptr),         \
+      CAPMGR_ERROR_INVALID_PARAMETER);                                         \
+}                                                                              \
+
+APP_CONTROL_GET_TEST_BLOCK(AppControlGetOperationPositiveTest,
+    AppControlGetOperationNegativeTest, operation)
+
+APP_CONTROL_GET_TEST_BLOCK(AppControlGetUriPositiveTest,
+    AppControlGetUriNegativeTest, uri)
+
+APP_CONTROL_GET_TEST_BLOCK(AppControlGetMimePositiveTest,
+    AppControlGetMimeNegativeTest, mime)
+
+APP_CONTROL_GET_TEST_BLOCK(AppControlGetAppidPositiveTest,
+    AppControlGetAppidNegativeTest, appid)
+
+TEST_F(UnitTest, AppControlGetExtraDataPositiveTest) {
+  char* value = nullptr;
+  ASSERT_EQ(capmgr_app_control_get_extra_data(env->app_control_,
+      kExtraDataKeyStr, &value), CAPMGR_ERROR_NONE);
+  ASSERT_NE(value, nullptr);
+  free(value);
+}
+
+TEST_F(UnitTest, AppControlGetExtraDataNegativeTest) {
+  char* value;
+  ASSERT_EQ(capmgr_app_control_get_extra_data(nullptr,
+      kExtraDataKeyStr, &value), CAPMGR_ERROR_INVALID_PARAMETER);
+  ASSERT_EQ(capmgr_app_control_get_extra_data(env->app_control_,
+      nullptr, &value), CAPMGR_ERROR_INVALID_PARAMETER);
+  ASSERT_EQ(capmgr_app_control_get_extra_data(env->app_control_,
+      kExtraDataKeyStr, nullptr), CAPMGR_ERROR_INVALID_PARAMETER);
+}
+
+TEST_F(UnitTest, AppControlSetDevicePositiveTest) {
+  ASSERT_EQ(capmgr_app_control_set_device(env->app_control_, env->device_),
+      CAPMGR_ERROR_NONE);
+}
+
+TEST_F(UnitTest, AppControlSetDeviceNegativeTest) {
+  ASSERT_EQ(capmgr_app_control_set_device(nullptr, env->device_),
+      CAPMGR_ERROR_INVALID_PARAMETER);
+  ASSERT_EQ(capmgr_app_control_set_device(env->app_control_, nullptr),
+      CAPMGR_ERROR_INVALID_PARAMETER);
+}
+
+#define APP_CONTROL_SET_TEST_BLOCK(POSITIVE_TESTNAME, NEGATIVE_TESTNAME, ATTR) \
+TEST_F(UnitTest, POSITIVE_TESTNAME) {                                          \
+  ASSERT_EQ(capmgr_app_control_set_##ATTR(env->app_control_, "value"),         \
+      CAPMGR_ERROR_NONE);                                                      \
+}                                                                              \
+                                                                               \
+TEST_F(UnitTest, NEGATIVE_TESTNAME) {                                          \
+  ASSERT_EQ(capmgr_app_control_set_##ATTR(nullptr, "value"),                   \
+      CAPMGR_ERROR_INVALID_PARAMETER);                                         \
+  ASSERT_EQ(capmgr_app_control_set_##ATTR(env->app_control_, nullptr),         \
+      CAPMGR_ERROR_INVALID_PARAMETER);                                         \
+}                                                                              \
+
+APP_CONTROL_SET_TEST_BLOCK(AppControlSetOperationPositiveTest,
+    AppControlSetOperationNegativeTest, operation)
+
+APP_CONTROL_SET_TEST_BLOCK(AppControlSetUriPositiveTest,
+    AppControlSetUriNegativeTest, uri)
+
+APP_CONTROL_SET_TEST_BLOCK(AppControlSetMimePositiveTest,
+    AppControlSetMimeNegativeTest, mime)
+
+APP_CONTROL_SET_TEST_BLOCK(AppControlSetAppidPositiveTest,
+    AppControlSetAppidNegativeTest, appid)
+
+TEST_F(UnitTest, AppControlAddExtraDataPositiveTest) {
+  ASSERT_EQ(
+      capmgr_app_control_add_extra_data(env->app_control_, "key", "value"),
+      CAPMGR_ERROR_NONE);
+}
+
+TEST_F(UnitTest, AppControlAddExtraDataNegativeTest) {
+  ASSERT_EQ(
+      capmgr_app_control_add_extra_data(nullptr, "key", "value"),
+      CAPMGR_ERROR_INVALID_PARAMETER);
+  ASSERT_EQ(
+      capmgr_app_control_add_extra_data(env->app_control_, nullptr, "value"),
+      CAPMGR_ERROR_INVALID_PARAMETER);
+  ASSERT_EQ(
+      capmgr_app_control_add_extra_data(env->app_control_, "key", nullptr),
+      CAPMGR_ERROR_INVALID_PARAMETER);
+}
+
+TEST_F(UnitTest, AppControlRemoveExtraDataPositiveTest) {
+  ASSERT_EQ(
+      capmgr_app_control_add_extra_data(env->app_control_, "key2", "value2"),
+      CAPMGR_ERROR_NONE);
+  ASSERT_EQ(
+      capmgr_app_control_remove_extra_data(env->app_control_, "key2"),
+      CAPMGR_ERROR_NONE);
+  // should return such as CAPMGR_ERROR_NO_SUCH_KEY?
+  char* value;
+  ASSERT_EQ(capmgr_app_control_get_extra_data(env->app_control_,
+      "key2", &value), CAPMGR_ERROR_INVALID_PARAMETER);
+}
+
+TEST_F(UnitTest, AppControlRemoveExtraDataNegativeTest) {
+  char* value;
+  ASSERT_EQ(capmgr_app_control_get_extra_data(nullptr,
+      kExtraDataKeyStr, &value), CAPMGR_ERROR_INVALID_PARAMETER);
+  ASSERT_EQ(capmgr_app_control_get_extra_data(env->app_control_,
+      nullptr, &value), CAPMGR_ERROR_INVALID_PARAMETER);
+  ASSERT_EQ(capmgr_app_control_get_extra_data(env->app_control_,
+      kExtraDataKeyStr, nullptr), CAPMGR_ERROR_INVALID_PARAMETER);
+}
+
+// TODO(jeremy.jang)
+/*
+TEST_F(UnitTest, AppControlSendPositiveTest) {
+}
+
+TEST_F(UnitTest, AppControlSendNegativeTest) {
+}
+*/
+
+TEST_F(UnitTest, ApplicationInfoForeachPackagesPositiveTest) {
+  bool is_hit = false;
+  ASSERT_EQ(capmgr_application_info_foreach_applications(env->device_,
+          ApplicationForeachCb, &is_hit), CAPMGR_ERROR_NONE);
+  ASSERT_TRUE(is_hit);
+}
+
+TEST_F(UnitTest, ApplicationInfoForeachPackagesNegativeTest) {
+  ASSERT_EQ(capmgr_application_info_foreach_applications(nullptr,
+          ApplicationForeachCb, nullptr), CAPMGR_ERROR_INVALID_PARAMETER);
+  ASSERT_EQ(capmgr_application_info_foreach_applications(env->device_,
+          nullptr, nullptr), CAPMGR_ERROR_INVALID_PARAMETER);
+}
+
+#define APPLICATION_INFO_GET_TEST_BLOCK(                                       \
+    POSITIVE_TESTNAME, NEGATIVE_TESTNAME, ATTR)                                \
+TEST_F(UnitTest, POSITIVE_TESTNAME) {                                          \
+  char* ATTR = nullptr;                                                        \
+  ASSERT_EQ(capmgr_application_info_get_##ATTR(env->app_, &ATTR),              \
+      CAPMGR_ERROR_NONE);                                                      \
+  ASSERT_NE(ATTR, nullptr);                                                    \
+  free(ATTR);                                                                  \
+}                                                                              \
+                                                                               \
+TEST_F(UnitTest, NEGATIVE_TESTNAME) {                                          \
+  char* ATTR;                                                                  \
+  ASSERT_EQ(capmgr_application_info_get_##ATTR(nullptr, &ATTR),                \
+      CAPMGR_ERROR_INVALID_PARAMETER);                                         \
+  ASSERT_EQ(capmgr_application_info_get_##ATTR(env->app_, nullptr),            \
+      CAPMGR_ERROR_INVALID_PARAMETER);                                         \
+}                                                                              \
+
+APPLICATION_INFO_GET_TEST_BLOCK(ApplicationInfoGetAppidPositiveTest,
+    ApplicationInfoGetAppidNegativeTest, appid)
+
+APPLICATION_INFO_GET_TEST_BLOCK(ApplicationInfoGetPkgidPositiveTest,
+    ApplicationInfoGetPkgidNegativeTest, pkgid)
+
+APPLICATION_INFO_GET_TEST_BLOCK(ApplicationInfoGetLabelPositiveTest,
+    ApplicationInfoGetLabelNegativeTest, label)
+
+APPLICATION_INFO_GET_TEST_BLOCK(ApplicationInfoGetVersionPositiveTest,
+    ApplicationInfoGetVersionNegativeTest, version)
+
+}  // namespace capmgr
+
+int main(int argc, char* argv[]) {
+  ::testing::InitGoogleTest(&argc, argv);
+  ::env = static_cast<TestEnvironment*>(
+      ::testing::AddGlobalTestEnvironment(new TestEnvironment));
+  return RUN_ALL_TESTS();
+}