libgdbus: Move common gdbus interfaces to libsystem package
[platform/core/system/storaged.git] / src / auto-test / storage.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 requstorageed 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_STORAGE_GETSTORAGE               "getstorage"
21 #define METHOD_STORAGE_GETSTATUS                "GetStatus"
22
23 static bool request_storage_method(const char *method, GVariant *param)
24 {
25         GVariant *msg;
26         unsigned long long val1, val2;
27         bool ret = FALSE;
28
29         msg = dbus_handle_method_sync_with_reply_var(STORAGED_BUS_NAME,
30                 STORAGED_PATH_STORAGE,
31                 STORAGED_INTERFACE_STORAGE,
32                 method, param);
33
34         if (!msg) {
35                 _E("fail (%s): no reply", method);
36                 return ret;
37         }
38
39         if (!dh_get_param_from_var(msg, "(tt)", &val1, &val2))
40                 _E("fail (%s): no message", method);
41         else {
42                 if (val1 <= 0) {
43                         _E("fail (%s): returned total storage (%d)", method, val1);
44                 } else {
45                         _I("success (%s): Total: %4.0llu Avail: %4.0llu", method, val1, val2);
46                         ret = TRUE;
47                 }
48         }
49
50         g_variant_unref(msg);
51         return ret;
52 }
53
54 static bool storage_getstorage()
55 {
56         _D("----------------------------------------------------------------------------------");
57         return request_storage_method(METHOD_STORAGE_GETSTORAGE, NULL);
58 }
59
60 static bool storage_getstatus(char *path)
61 {
62         _D("----------------------------------------------------------------------------------");
63         return request_storage_method(METHOD_STORAGE_GETSTATUS, g_variant_new("(s)", path));
64 }
65
66 void storage_test_all(int *success, int *fail)
67 {
68         int s = 0;
69         int f = 0;
70
71         (storage_getstorage())                  ? s++ : f++;
72         (storage_getstatus("/opt/usr/home"))    ? s++ : f++;
73         _D("----------------------------------------------------------------------------------");
74
75         if (NULL != success)    *success = s;
76         if (NULL != fail)       *fail = f;
77 }
78
79 static void storage_init(void *data)
80 {
81         int success = 0;
82         int fail = 0;
83
84         _I("start test");
85
86         storage_test_all(&success, &fail);
87
88         _I("Total: %d, Success: %d, Fail: %d", success+fail, success, fail);
89 }
90
91 static void storage_exit(void *data)
92 {
93         _I("end test");
94 }
95
96 static int storage_unit(int argc, char **argv)
97 {
98         if (argc < 3) {
99                 int success = 0;
100                 int fail = 0;
101
102                 _I("start test");
103                 storage_test_all(&success, &fail);
104                 _I("Total: %d, Success: %d, Fail: %d", success+fail, success, fail);
105         } else if (0 == strcasecmp(argv[2], METHOD_STORAGE_GETSTORAGE)) {
106                 storage_getstorage();
107         } else if (0 == strcasecmp(argv[2], METHOD_STORAGE_GETSTATUS)) {
108                 storage_getstatus(argv[3]);
109         } else {
110                 _E("Unknown test case!!!");
111         }
112         _D("----------------------------------------------------------------------------------");
113
114         return 0;
115 }
116
117 static const struct test_ops storage_test_ops = {
118         .priority = TEST_PRIORITY_NORMAL,
119         .name     = "storage",
120         .init     = storage_init,
121         .exit    = storage_exit,
122         .unit    = storage_unit,
123 };
124
125 TEST_OPS_REGISTER(&storage_test_ops)