libgdbus: Move common gdbus interfaces to libsystem package
[platform/core/system/storaged.git] / src / auto-test / block.c
1 /*
2  * storaged auto-test
3  *
4  * Copyright (c) 2018 Samsung Electronics Co., Ltd.
5  *
6  * Licensed under the Apache License, Version 2.0 (the License);
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless requblocked by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 #include "test.h"
19
20 #define METHOD_BLOCK_SHOWDEVICELIST             "ShowDeviceList"
21 #define METHOD_BLOCK_GETDEVICELIST              "GetDeviceList"
22 #define METHOD_BLOCK_GETDEVICELIST2             "GetDeviceList2"
23 #define METHOD_BLOCK_MOUNT                      "Mount"
24 #define METHOD_BLOCK_UNMOUNT                    "Unmount"
25 #define METHOD_BLOCK_FORMAT                     "Format"
26 #define METHOD_BLOCK_FORMATWITHTYPE             "FormatwithType"
27 #define METHOD_BLOCK_GETDEVICEINFO              "GetDeviceInfo"
28 #define METHOD_BLOCK_GETMMCPRIMARY              "GetMmcPrimary"
29 #define METHOD_BLOCK_PRIVATEMOUNT               "PrivateMount"
30 #define METHOD_BLOCK_PRIVATEUNMOUNT             "PrivateUnmount"
31 #define METHOD_BLOCK_CHECKSPEED                 "CheckSpeed"
32 #define METHOD_BLOCK_CONTROL                    "Control"
33 #define METHOD_BLOCK_GETCONTROL                 "GetControl"
34
35 #define DBUS_DEFAULT_TIMEOUT                    (-1)
36
37 static bool request_block_method(const char *method, GVariant *param, int timeout)
38 {
39         GVariant *msg;
40         int val;
41         bool ret = FALSE;
42
43         msg = dbus_handle_method_sync_with_reply_var_timeout(STORAGED_BUS_NAME,
44                 STORAGED_PATH_BLOCK_MANAGER,
45                 STORAGED_INTERFACE_BLOCK_MANAGER,
46                 method, param, timeout);
47
48         if (!msg) {
49                 _E("fail (%s): no reply", method);
50                 return ret;
51         }
52
53         if (!dh_get_param_from_var(msg, "(i)", &val))
54                 _E("fail (%s): no message", method);
55         else {
56                 if ((val == -ENOTSUP) || (val == -ENOSYS)) {
57                         _I("Not supported feature! (%s): %d", method, val);
58                         ret = TRUE;
59                 } else if (val == -ENODEV) {
60                         _E("fail (%s): device open fail (%d)", method, val);
61                 } else if (val < 0) {
62                         _E("fail (%s): returned fail (%d)", method, val);
63                 } else {
64                         _I("success (%s): %d", method, val);
65                         ret = TRUE;
66                 }
67         }
68
69         g_variant_unref(msg);
70         return ret;
71 }
72
73 static int get_id()
74 {
75         GVariant *msg;
76         int val;
77         int ret = -1;
78
79         msg = dbus_handle_method_sync_with_reply_var(STORAGED_BUS_NAME,
80                 STORAGED_PATH_BLOCK_MANAGER,
81                 STORAGED_INTERFACE_BLOCK_MANAGER,
82                 METHOD_BLOCK_GETMMCPRIMARY,
83                 NULL);
84
85         if (!msg) {
86                 _E("fail (%s): no reply", METHOD_BLOCK_GETMMCPRIMARY);
87                 return ret;
88         }
89
90         if (!dh_get_param_from_var(msg, "(issssssisibii)",
91                         NULL, NULL, NULL, NULL, NULL, NULL,
92                         NULL, NULL, NULL, NULL, NULL, NULL, &val))
93                 _E("fail (%s): no message", METHOD_BLOCK_GETMMCPRIMARY);
94         else
95                 ret = val;
96
97         _D("id: %d", ret);
98         g_variant_unref(msg);
99         return ret;
100 }
101
102 static bool block_showdevicelist()
103 {
104         GVariant *msg;
105         int ret = FALSE;
106
107         _D("----------------------------------------------------------------------------------");
108
109         msg = dbus_handle_method_sync_with_reply_var(STORAGED_BUS_NAME,
110                 STORAGED_PATH_BLOCK_MANAGER,
111                 STORAGED_INTERFACE_BLOCK_MANAGER,
112                 METHOD_BLOCK_SHOWDEVICELIST,
113                 NULL);
114
115         if (!msg) {
116                 _E("fail (%s): no reply", METHOD_BLOCK_SHOWDEVICELIST);
117                 return ret;
118         }
119
120         _I("success (%s)", METHOD_BLOCK_SHOWDEVICELIST);
121         g_variant_unref(msg);
122         return TRUE;
123 }
124
125 static bool block_getdevicelist(char *type)
126 {
127         GVariant *msg;
128         GVariantIter *iter;
129         int val;
130         int ret = FALSE;
131
132         _D("----------------------------------------------------------------------------------");
133         msg = dbus_handle_method_sync_with_reply_var(STORAGED_BUS_NAME,
134                 STORAGED_PATH_BLOCK_MANAGER,
135                 STORAGED_INTERFACE_BLOCK_MANAGER,
136                 METHOD_BLOCK_GETDEVICELIST,
137                 g_variant_new("(s)", type));
138
139         if (!msg) {
140                 _E("fail (%s): no reply", METHOD_BLOCK_GETMMCPRIMARY);
141                 return ret;
142         }
143
144         g_variant_get(msg, "(a(issssssisibii))", &iter);
145         while (g_variant_iter_loop(iter, "(issssssisibii)",
146                         NULL, NULL, NULL, NULL, NULL, NULL,
147                         NULL, NULL, NULL, NULL, NULL, NULL, &val)) {
148                 _D("id: %d", val);
149                 ret = TRUE;
150         }
151
152         _I("success (%s): %d", METHOD_BLOCK_GETDEVICELIST, ret);
153         return ret;
154 }
155
156 static bool block_getdevicelist2(char *type)
157 {
158         GVariant *msg;
159         GVariantIter *iter;
160         char *val;
161         int ret = FALSE;
162
163         _D("----------------------------------------------------------------------------------");
164         msg = dbus_handle_method_sync_with_reply_var(STORAGED_BUS_NAME,
165                 STORAGED_PATH_BLOCK_MANAGER,
166                 STORAGED_INTERFACE_BLOCK_MANAGER,
167                 METHOD_BLOCK_GETDEVICELIST2,
168                 g_variant_new("(s)", type));
169
170         if (!msg) {
171                 _E("fail (%s): no reply", METHOD_BLOCK_GETMMCPRIMARY);
172                 return ret;
173         }
174
175         g_variant_get(msg, "(a(issssssisibi))", &iter);
176         while (g_variant_iter_loop(iter, "(issssssisibi)",
177                         NULL, &val, NULL, NULL, NULL, NULL,
178                         NULL, NULL, NULL, NULL, NULL, NULL)) {
179                 _D("devnode: %s", val);
180                 ret = TRUE;
181         }
182
183         _I("success (%s): %d", METHOD_BLOCK_GETDEVICELIST2, ret);
184         return ret;
185 }
186
187 static bool block_mount(int id, char *mount_point)
188 {
189         _D("----------------------------------------------------------------------------------");
190         return request_block_method(METHOD_BLOCK_MOUNT, g_variant_new("(is)", id, mount_point), DBUS_DEFAULT_TIMEOUT);
191 }
192
193 static bool block_unmount(int id, int option)
194 {
195         _D("----------------------------------------------------------------------------------");
196         return request_block_method(METHOD_BLOCK_UNMOUNT, g_variant_new("(ii)", id, option), DBUS_DEFAULT_TIMEOUT);
197 }
198
199 static bool block_format(int id, int option)
200 {
201         _D("----------------------------------------------------------------------------------");
202         return request_block_method(METHOD_BLOCK_FORMAT, g_variant_new("(ii)", id, option), G_MAXINT);
203 }
204
205 static bool block_formatwithtype(int id, int option, char *type)
206 {
207         _D("----------------------------------------------------------------------------------");
208         return request_block_method(METHOD_BLOCK_FORMATWITHTYPE, g_variant_new("(iis)", id, option, type), G_MAXINT);
209 }
210
211 static bool block_getdeviceinfo(int id)
212 {
213         GVariant *msg;
214         int val = -1;
215         int ret = FALSE;
216
217         _D("----------------------------------------------------------------------------------");
218         msg = dbus_handle_method_sync_with_reply_var(STORAGED_BUS_NAME,
219                 STORAGED_PATH_BLOCK_MANAGER,
220                 STORAGED_INTERFACE_BLOCK_MANAGER,
221                 METHOD_BLOCK_GETDEVICEINFO,
222                 g_variant_new("(i)", id));
223
224         if (!msg) {
225                 _E("fail (%s): no reply", METHOD_BLOCK_GETMMCPRIMARY);
226                 return ret;
227         }
228
229         if (!dh_get_param_from_var(msg, "(issssssisibii)",
230                         NULL, NULL, NULL, NULL, NULL, NULL,
231                         NULL, NULL, NULL, NULL, NULL, NULL, &val))
232                 _E("fail (%s): no message", METHOD_BLOCK_GETMMCPRIMARY);
233         else
234                 ret = TRUE;
235
236         if (val != id) {
237                 ret = FALSE;
238                 _E("fail (%s)", METHOD_BLOCK_GETMMCPRIMARY);
239         }
240
241         _I("success (%s): %d", METHOD_BLOCK_GETDEVICEINFO, ret);
242         return ret;
243 }
244
245 static bool block_getmmcprimary()
246 {
247         GVariant *msg;
248         int val = -1;
249         int ret = FALSE;
250
251         _D("----------------------------------------------------------------------------------");
252         msg = dbus_handle_method_sync_with_reply_var(STORAGED_BUS_NAME,
253                 STORAGED_PATH_BLOCK_MANAGER,
254                 STORAGED_INTERFACE_BLOCK_MANAGER,
255                 METHOD_BLOCK_GETMMCPRIMARY,
256                 NULL);
257
258         if (!msg) {
259                 _E("fail (%s): no reply", METHOD_BLOCK_GETMMCPRIMARY);
260                 return ret;
261         }
262
263         if (!dh_get_param_from_var(msg, "(issssssisibii)",
264                         NULL, NULL, NULL, NULL, NULL, NULL,
265                         NULL, NULL, NULL, NULL, NULL, NULL, &val))
266                 _E("fail (%s): no message", METHOD_BLOCK_GETMMCPRIMARY);
267         else
268                 ret = TRUE;
269
270         _D("id: %d", val);
271         _I("success (%s): %d", METHOD_BLOCK_GETMMCPRIMARY, ret);
272         return ret;
273 }
274
275 static bool block_privatemount(int id, char *mount_point)
276 {
277         _D("----------------------------------------------------------------------------------");
278         return request_block_method(METHOD_BLOCK_PRIVATEMOUNT, g_variant_new("(is)", id, mount_point), DBUS_DEFAULT_TIMEOUT);
279 }
280
281 static bool block_privateunmount(int id, int option)
282 {
283         _D("----------------------------------------------------------------------------------");
284         return request_block_method(METHOD_BLOCK_PRIVATEUNMOUNT, g_variant_new("(ii)", id, option), DBUS_DEFAULT_TIMEOUT);
285 }
286
287 static bool block_checkspeed(int id)
288 {
289         _D("----------------------------------------------------------------------------------");
290         return request_block_method(METHOD_BLOCK_CHECKSPEED, g_variant_new("(i)", id), G_MAXINT);
291 }
292
293 static bool block_control(int enable)
294 {
295         _D("----------------------------------------------------------------------------------");
296         return request_block_method(METHOD_BLOCK_CONTROL, g_variant_new("(i)", enable), DBUS_DEFAULT_TIMEOUT);
297 }
298
299 static bool block_getcontrol()
300 {
301         _D("----------------------------------------------------------------------------------");
302         return request_block_method(METHOD_BLOCK_GETCONTROL, NULL, DBUS_DEFAULT_TIMEOUT);
303 }
304
305 void block_test_all(int *success, int *fail)
306 {
307         int s = 0;
308         int f = 0;
309         int id = 0;
310
311         id = get_id();
312
313         _D("------------------- Precondition start --------------------");
314         block_unmount(id, 1);
315         _D("------------------- Precondition end ----------------------");
316         (block_checkspeed(id))                  ? s++ : f++;
317         (block_format(id, 1))                   ? s++ : f++;
318         (block_formatwithtype(id, 1, "vfat"))   ? s++ : f++;
319         (block_showdevicelist())                ? s++ : f++;
320         (block_getdevicelist("all"))            ? s++ : f++;
321         (block_getdevicelist2("all"))           ? s++ : f++;
322         (block_mount(id, ""))                   ? s++ : f++;
323         (block_unmount(id, 1))                  ? s++ : f++;
324         (block_getdeviceinfo(id))               ? s++ : f++;
325         (block_getmmcprimary())                 ? s++ : f++;
326         (block_privatemount(id, ""))            ? s++ : f++;
327         (block_privateunmount(id, 1))           ? s++ : f++;
328         block_mount(id, "");
329         (block_control(0))                      ? s++ : f++;
330         request_block_method(METHOD_BLOCK_CONTROL, g_variant_new("(i)", 1), DBUS_DEFAULT_TIMEOUT);
331         (block_getcontrol())                    ? s++ : f++;
332         _D("----------------------------------------------------------------------------------");
333
334         if (NULL != success)    *success = s;
335         if (NULL != fail)       *fail = f;
336 }
337
338 static void block_init(void *data)
339 {
340         int success = 0;
341         int fail = 0;
342
343         _I("start test");
344
345         block_test_all(&success, &fail);
346
347         _I("Total: %d, Success: %d, Fail: %d", success+fail, success, fail);
348 }
349
350 static void block_exit(void *data)
351 {
352         _I("end test");
353 }
354
355 static int block_unit(int argc, char **argv)
356 {
357         int id = 0;
358
359         id = get_id();
360
361         if (argc < 3) {
362                 int success = 0;
363                 int fail = 0;
364
365                 _I("start test");
366                 block_test_all(&success, &fail);
367                 _I("Total: %d, Success: %d, Fail: %d", success+fail, success, fail);
368         } else if (0 == strcasecmp(argv[2], METHOD_BLOCK_SHOWDEVICELIST)) {
369                 block_showdevicelist();
370         } else if (0 == strcasecmp(argv[2], METHOD_BLOCK_GETDEVICELIST)) {
371                 block_getdevicelist(argv[3]);
372         } else if (0 == strcasecmp(argv[2], METHOD_BLOCK_GETDEVICELIST2)) {
373                 block_getdevicelist2(argv[3]);
374         } else if (0 == strcasecmp(argv[2], METHOD_BLOCK_MOUNT)) {
375                 block_mount(id, argv[3]);
376         } else if (0 == strcasecmp(argv[2], METHOD_BLOCK_UNMOUNT)) {
377                 block_unmount(id, atoi(argv[3]));
378         } else if (0 == strcasecmp(argv[2], METHOD_BLOCK_FORMAT)) {
379                 block_format(id, atoi(argv[3]));
380         } else if (0 == strcasecmp(argv[2], METHOD_BLOCK_FORMATWITHTYPE)) {
381                 block_formatwithtype(id, atoi(argv[3]), argv[4]);
382         } else if (0 == strcasecmp(argv[2], METHOD_BLOCK_GETDEVICEINFO)) {
383                 block_getdeviceinfo(id);
384         } else if (0 == strcasecmp(argv[2], METHOD_BLOCK_GETMMCPRIMARY)) {
385                 block_getmmcprimary();
386         } else if (0 == strcasecmp(argv[2], METHOD_BLOCK_PRIVATEMOUNT)) {
387                 _D("------------------- Private operation test precondition start --------------------");
388                 block_unmount(id, 1);
389                 _D("------------------- Private operation test precondition end--------------------");
390                 block_privatemount(id, argv[3]);
391         } else if (0 == strcasecmp(argv[2], METHOD_BLOCK_PRIVATEUNMOUNT)) {
392                 _D("------------------- Private operation test precondition start --------------------");
393                 block_unmount(id, 1);
394                 block_privatemount(id, "");
395                 _D("------------------- Private operation test precondition end--------------------");
396                 block_privateunmount(id, atoi(argv[3]));
397         } else if (0 == strcasecmp(argv[2], METHOD_BLOCK_CHECKSPEED)) {
398                 block_checkspeed(id);
399         } else if (0 == strcasecmp(argv[2], METHOD_BLOCK_CONTROL)) {
400                 block_control(atoi(argv[3]));
401                 request_block_method(METHOD_BLOCK_CONTROL, g_variant_new("(i)", 1), DBUS_DEFAULT_TIMEOUT);
402         } else if (0 == strcasecmp(argv[2], METHOD_BLOCK_GETCONTROL)) {
403                 block_getcontrol();
404         } else {
405                 _E("Unknown test case!!!");
406         }
407         _D("----------------------------------------------------------------------------------");
408
409         return 0;
410 }
411
412 static const struct test_ops block_test_ops = {
413         .priority = TEST_PRIORITY_NORMAL,
414         .name     = "block",
415         .init     = block_init,
416         .exit    = block_exit,
417         .unit    = block_unit,
418 };
419
420 TEST_OPS_REGISTER(&block_test_ops)