--- /dev/null
+// 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();
+}