1 // Copyright (c) 2018 Samsung Electronics Co., Ltd All Rights Reserved
2 // Use of this source code is governed by a apache 2.0 license that can be
3 // found in the LICENSE file.
5 #include <gtest/gtest.h>
9 #include "include/capability_manager.h"
10 #include "src/utils/logging.h"
14 int DeviceForeachCbForSetup(const capmgr_device_h device, void* user_data) {
15 capmgr_device_h* clone = reinterpret_cast<capmgr_device_h*>(user_data);
16 int ret = capmgr_device_clone(device, clone);
17 if (ret != CAPMGR_ERROR_NONE)
19 // use the first device
24 int ApplicationForeachCbForSetup(
25 const capmgr_application_info_h remote_app_info, void* user_data) {
26 capmgr_application_info_h* clone =
27 reinterpret_cast<capmgr_application_info_h*>(user_data);
28 int ret = capmgr_application_info_clone(remote_app_info, clone);
29 if (ret != CAPMGR_ERROR_NONE)
36 int DeviceForeachCb(const capmgr_device_h device, void* user_data) {
37 bool* is_hit = reinterpret_cast<bool*>(user_data);
42 int ApplicationForeachCb(const capmgr_application_info_h remote_app_info,
44 bool* is_hit = reinterpret_cast<bool*>(user_data);
48 capmgr_application_info_get_appid(remote_app_info, &appid);
54 const char kExtraDataKeyStr[] = "extra_key";
55 const char kExtraDataValueStr[] = "extra_value";
57 class TestEnvironment : public ::testing::Environment {
59 TestEnvironment() : device_(nullptr), app_control_(nullptr),
61 void SetUp() override {
62 ASSERT_EQ(capmgr_device_foreach_devices(DeviceForeachCbForSetup, &device_),
63 CAPMGR_ERROR_NONE) << "Failed to init test environment!";
64 ASSERT_NE(device_, nullptr);
66 capmgr_device_get_device_id(device_, &device_id);
67 LOG(INFO) << "Selected device id is: " << device_id;
69 ASSERT_EQ(capmgr_app_control_create(&app_control_), CAPMGR_ERROR_NONE);
70 ASSERT_NE(app_control_, nullptr);
71 ASSERT_EQ(capmgr_app_control_set_device(app_control_, device_),
73 ASSERT_EQ(capmgr_app_control_set_operation(app_control_, "operation"),
75 ASSERT_EQ(capmgr_app_control_set_uri(app_control_, "uri"),
77 ASSERT_EQ(capmgr_app_control_set_mime(app_control_, "mime"),
79 ASSERT_EQ(capmgr_app_control_set_appid(app_control_, "appid"),
81 ASSERT_EQ(capmgr_app_control_add_extra_data(app_control_, kExtraDataKeyStr,
82 kExtraDataValueStr), CAPMGR_ERROR_NONE);
83 ASSERT_EQ(capmgr_application_info_foreach_applications(device_,
84 ApplicationForeachCbForSetup, &app_), CAPMGR_ERROR_NONE);
86 void TearDown() override {
87 capmgr_application_info_destroy(app_);
88 capmgr_app_control_destroy(app_control_);
89 capmgr_device_destroy(device_);
92 capmgr_device_h device_;
93 capmgr_app_control_h app_control_;
94 capmgr_application_info_h app_;
97 TestEnvironment* env = nullptr;
103 class UnitTest : public ::testing::Test {
105 virtual void SetUp() {}
106 virtual void TearDown() {}
109 TEST_F(UnitTest, DeviceForeachDevicesPositiveTest) {
111 ASSERT_EQ(capmgr_device_foreach_devices(DeviceForeachCb, &is_hit),
116 TEST_F(UnitTest, DeviceForeachDevicesNegativeTest) {
117 ASSERT_EQ(capmgr_device_foreach_devices(nullptr, nullptr),
118 CAPMGR_ERROR_INVALID_PARAMETER);
121 TEST_F(UnitTest, DeviceClonePositiveTest) {
122 // need to get device
123 capmgr_device_h clone;
124 ASSERT_EQ(capmgr_device_clone(env->device_, &clone), CAPMGR_ERROR_NONE);
127 TEST_F(UnitTest, DeviceCloneNegativeTest) {
128 capmgr_device_h clone;
129 ASSERT_EQ(capmgr_device_clone(nullptr, &clone),
130 CAPMGR_ERROR_INVALID_PARAMETER);
131 ASSERT_EQ(capmgr_device_clone(env->device_, nullptr),
132 CAPMGR_ERROR_INVALID_PARAMETER);
135 TEST_F(UnitTest, DeviceDestroyPositiveTest) {
136 capmgr_device_h clone;
137 ASSERT_EQ(capmgr_device_clone(env->device_, &clone),
139 ASSERT_EQ(capmgr_device_destroy(clone), CAPMGR_ERROR_NONE);
142 TEST_F(UnitTest, DeviceDestroyNegativeTest) {
143 ASSERT_EQ(capmgr_device_destroy(nullptr), CAPMGR_ERROR_INVALID_PARAMETER);
146 #define DEVICE_GET_TEST_BLOCK(POSITIVE_TESTNAME, NEGATIVE_TESTNAME, ATTR) \
147 TEST_F(UnitTest, POSITIVE_TESTNAME) { \
148 char* ATTR = nullptr; \
149 ASSERT_EQ(capmgr_device_get_##ATTR(env->device_, &ATTR), CAPMGR_ERROR_NONE); \
150 ASSERT_NE(ATTR, nullptr); \
154 TEST_F(UnitTest, NEGATIVE_TESTNAME) { \
156 ASSERT_EQ(capmgr_device_get_##ATTR(nullptr, &ATTR), \
157 CAPMGR_ERROR_INVALID_PARAMETER); \
158 ASSERT_EQ(capmgr_device_get_##ATTR(env->device_, nullptr), \
159 CAPMGR_ERROR_INVALID_PARAMETER); \
162 DEVICE_GET_TEST_BLOCK(DeviceGetDeviceIdPositiveTest,
163 DeviceGetDeviceIdNegativeTest, device_id)
165 DEVICE_GET_TEST_BLOCK(DeviceGetModelNamePositiveTest,
166 DeviceGetModelNameNegativeTest, model_name)
168 DEVICE_GET_TEST_BLOCK(DeviceGetDeviceNamePositiveTest,
169 DeviceGetDeviceNameNegativeTest, device_name)
171 DEVICE_GET_TEST_BLOCK(DeviceGetPlatformVerPositiveTest,
172 DeviceGetPlatformVerNegativeTest, platform_ver)
174 DEVICE_GET_TEST_BLOCK(DeviceGetProfilePositiveTest,
175 DeviceGetProfileNegativeTest, profile)
177 DEVICE_GET_TEST_BLOCK(DeviceGetSWVerPositiveTest,
178 DeviceGetSWVerNegativeTest, sw_ver)
180 TEST_F(UnitTest, AppControlCreatePositiveTest) {
181 capmgr_app_control_h app_control;
182 ASSERT_EQ(capmgr_app_control_create(&app_control), CAPMGR_ERROR_NONE);
183 capmgr_app_control_destroy(app_control);
186 TEST_F(UnitTest, AppControlCreateNegativeTest) {
187 ASSERT_EQ(capmgr_app_control_create(nullptr), CAPMGR_ERROR_INVALID_PARAMETER);
190 TEST_F(UnitTest, AppControlDestroyPositiveTest) {
191 capmgr_app_control_h app_control;
192 ASSERT_EQ(capmgr_app_control_create(&app_control), CAPMGR_ERROR_NONE);
193 ASSERT_EQ(capmgr_app_control_destroy(app_control), CAPMGR_ERROR_NONE);
196 TEST_F(UnitTest, AppControlDestroyNegativeTest) {
197 ASSERT_EQ(capmgr_app_control_destroy(nullptr),
198 CAPMGR_ERROR_INVALID_PARAMETER);
201 TEST_F(UnitTest, AppControlGetDevicePositiveTest) {
202 capmgr_device_h device;
203 ASSERT_EQ(capmgr_app_control_get_device(env->app_control_, &device),
205 capmgr_device_destroy(device);
208 TEST_F(UnitTest, AppControlGetDeviceNegativeTest) {
209 capmgr_device_h device;
210 ASSERT_EQ(capmgr_app_control_get_device(nullptr, &device),
211 CAPMGR_ERROR_INVALID_PARAMETER);
212 ASSERT_EQ(capmgr_app_control_get_device(env->app_control_, nullptr),
213 CAPMGR_ERROR_INVALID_PARAMETER);
216 #define APP_CONTROL_GET_TEST_BLOCK(POSITIVE_TESTNAME, NEGATIVE_TESTNAME, ATTR) \
217 TEST_F(UnitTest, POSITIVE_TESTNAME) { \
218 char* ATTR = nullptr; \
219 ASSERT_EQ(capmgr_app_control_get_##ATTR(env->app_control_, &ATTR), \
220 CAPMGR_ERROR_NONE); \
221 ASSERT_NE(ATTR, nullptr); \
225 TEST_F(UnitTest, NEGATIVE_TESTNAME) { \
227 ASSERT_EQ(capmgr_app_control_get_##ATTR(nullptr, &ATTR), \
228 CAPMGR_ERROR_INVALID_PARAMETER); \
229 ASSERT_EQ(capmgr_app_control_get_##ATTR(env->app_control_, nullptr), \
230 CAPMGR_ERROR_INVALID_PARAMETER); \
233 APP_CONTROL_GET_TEST_BLOCK(AppControlGetOperationPositiveTest,
234 AppControlGetOperationNegativeTest, operation)
236 APP_CONTROL_GET_TEST_BLOCK(AppControlGetUriPositiveTest,
237 AppControlGetUriNegativeTest, uri)
239 APP_CONTROL_GET_TEST_BLOCK(AppControlGetMimePositiveTest,
240 AppControlGetMimeNegativeTest, mime)
242 APP_CONTROL_GET_TEST_BLOCK(AppControlGetAppidPositiveTest,
243 AppControlGetAppidNegativeTest, appid)
245 TEST_F(UnitTest, AppControlGetExtraDataPositiveTest) {
246 char* value = nullptr;
247 ASSERT_EQ(capmgr_app_control_get_extra_data(env->app_control_,
248 kExtraDataKeyStr, &value), CAPMGR_ERROR_NONE);
249 ASSERT_NE(value, nullptr);
253 TEST_F(UnitTest, AppControlGetExtraDataNegativeTest) {
255 ASSERT_EQ(capmgr_app_control_get_extra_data(nullptr,
256 kExtraDataKeyStr, &value), CAPMGR_ERROR_INVALID_PARAMETER);
257 ASSERT_EQ(capmgr_app_control_get_extra_data(env->app_control_,
258 nullptr, &value), CAPMGR_ERROR_INVALID_PARAMETER);
259 ASSERT_EQ(capmgr_app_control_get_extra_data(env->app_control_,
260 kExtraDataKeyStr, nullptr), CAPMGR_ERROR_INVALID_PARAMETER);
263 TEST_F(UnitTest, AppControlSetDevicePositiveTest) {
264 ASSERT_EQ(capmgr_app_control_set_device(env->app_control_, env->device_),
268 TEST_F(UnitTest, AppControlSetDeviceNegativeTest) {
269 ASSERT_EQ(capmgr_app_control_set_device(nullptr, env->device_),
270 CAPMGR_ERROR_INVALID_PARAMETER);
271 ASSERT_EQ(capmgr_app_control_set_device(env->app_control_, nullptr),
272 CAPMGR_ERROR_INVALID_PARAMETER);
275 #define APP_CONTROL_SET_TEST_BLOCK(POSITIVE_TESTNAME, NEGATIVE_TESTNAME, ATTR) \
276 TEST_F(UnitTest, POSITIVE_TESTNAME) { \
277 ASSERT_EQ(capmgr_app_control_set_##ATTR(env->app_control_, "value"), \
278 CAPMGR_ERROR_NONE); \
281 TEST_F(UnitTest, NEGATIVE_TESTNAME) { \
282 ASSERT_EQ(capmgr_app_control_set_##ATTR(nullptr, "value"), \
283 CAPMGR_ERROR_INVALID_PARAMETER); \
284 ASSERT_EQ(capmgr_app_control_set_##ATTR(env->app_control_, nullptr), \
285 CAPMGR_ERROR_INVALID_PARAMETER); \
288 APP_CONTROL_SET_TEST_BLOCK(AppControlSetOperationPositiveTest,
289 AppControlSetOperationNegativeTest, operation)
291 APP_CONTROL_SET_TEST_BLOCK(AppControlSetUriPositiveTest,
292 AppControlSetUriNegativeTest, uri)
294 APP_CONTROL_SET_TEST_BLOCK(AppControlSetMimePositiveTest,
295 AppControlSetMimeNegativeTest, mime)
297 APP_CONTROL_SET_TEST_BLOCK(AppControlSetAppidPositiveTest,
298 AppControlSetAppidNegativeTest, appid)
300 TEST_F(UnitTest, AppControlAddExtraDataPositiveTest) {
302 capmgr_app_control_add_extra_data(env->app_control_, "key", "value"),
306 TEST_F(UnitTest, AppControlAddExtraDataNegativeTest) {
308 capmgr_app_control_add_extra_data(nullptr, "key", "value"),
309 CAPMGR_ERROR_INVALID_PARAMETER);
311 capmgr_app_control_add_extra_data(env->app_control_, nullptr, "value"),
312 CAPMGR_ERROR_INVALID_PARAMETER);
314 capmgr_app_control_add_extra_data(env->app_control_, "key", nullptr),
315 CAPMGR_ERROR_INVALID_PARAMETER);
318 TEST_F(UnitTest, AppControlRemoveExtraDataPositiveTest) {
320 capmgr_app_control_add_extra_data(env->app_control_, "key2", "value2"),
323 capmgr_app_control_remove_extra_data(env->app_control_, "key2"),
325 // should return such as CAPMGR_ERROR_NO_SUCH_KEY?
327 ASSERT_EQ(capmgr_app_control_get_extra_data(env->app_control_,
328 "key2", &value), CAPMGR_ERROR_INVALID_PARAMETER);
331 TEST_F(UnitTest, AppControlRemoveExtraDataNegativeTest) {
333 ASSERT_EQ(capmgr_app_control_get_extra_data(nullptr,
334 kExtraDataKeyStr, &value), CAPMGR_ERROR_INVALID_PARAMETER);
335 ASSERT_EQ(capmgr_app_control_get_extra_data(env->app_control_,
336 nullptr, &value), CAPMGR_ERROR_INVALID_PARAMETER);
337 ASSERT_EQ(capmgr_app_control_get_extra_data(env->app_control_,
338 kExtraDataKeyStr, nullptr), CAPMGR_ERROR_INVALID_PARAMETER);
343 TEST_F(UnitTest, AppControlSendPositiveTest) {
346 TEST_F(UnitTest, AppControlSendNegativeTest) {
350 TEST_F(UnitTest, ApplicationInfoForeachPackagesPositiveTest) {
352 ASSERT_EQ(capmgr_application_info_foreach_applications(env->device_,
353 ApplicationForeachCb, &is_hit), CAPMGR_ERROR_NONE);
357 TEST_F(UnitTest, ApplicationInfoForeachPackagesNegativeTest) {
358 ASSERT_EQ(capmgr_application_info_foreach_applications(nullptr,
359 ApplicationForeachCb, nullptr), CAPMGR_ERROR_INVALID_PARAMETER);
360 ASSERT_EQ(capmgr_application_info_foreach_applications(env->device_,
361 nullptr, nullptr), CAPMGR_ERROR_INVALID_PARAMETER);
364 #define APPLICATION_INFO_GET_TEST_BLOCK( \
365 POSITIVE_TESTNAME, NEGATIVE_TESTNAME, ATTR) \
366 TEST_F(UnitTest, POSITIVE_TESTNAME) { \
367 char* ATTR = nullptr; \
368 ASSERT_EQ(capmgr_application_info_get_##ATTR(env->app_, &ATTR), \
369 CAPMGR_ERROR_NONE); \
370 ASSERT_NE(ATTR, nullptr); \
374 TEST_F(UnitTest, NEGATIVE_TESTNAME) { \
376 ASSERT_EQ(capmgr_application_info_get_##ATTR(nullptr, &ATTR), \
377 CAPMGR_ERROR_INVALID_PARAMETER); \
378 ASSERT_EQ(capmgr_application_info_get_##ATTR(env->app_, nullptr), \
379 CAPMGR_ERROR_INVALID_PARAMETER); \
382 APPLICATION_INFO_GET_TEST_BLOCK(ApplicationInfoGetAppidPositiveTest,
383 ApplicationInfoGetAppidNegativeTest, appid)
385 APPLICATION_INFO_GET_TEST_BLOCK(ApplicationInfoGetPkgidPositiveTest,
386 ApplicationInfoGetPkgidNegativeTest, pkgid)
388 APPLICATION_INFO_GET_TEST_BLOCK(ApplicationInfoGetLabelPositiveTest,
389 ApplicationInfoGetLabelNegativeTest, label)
391 APPLICATION_INFO_GET_TEST_BLOCK(ApplicationInfoGetVersionPositiveTest,
392 ApplicationInfoGetVersionNegativeTest, version)
394 } // namespace capmgr
396 int main(int argc, char* argv[]) {
397 ::testing::InitGoogleTest(&argc, argv);
398 ::env = static_cast<TestEnvironment*>(
399 ::testing::AddGlobalTestEnvironment(new TestEnvironment));
400 return RUN_ALL_TESTS();