tizen 2.3 release
[framework/system/deviced.git] / src / auto-test / storage.c
1 /*
2  * test
3  *
4  * Copyright (c) 2013 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 required 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_GET_STORAGE      "getstorage"
21 #define METHOD_MEM_TRIM "MemTrim"
22
23 static void mem_trim(void *data, DBusMessage *msg, DBusError *tmp)
24 {
25         DBusError err;
26         int ret, result;
27
28         if (!msg)
29                 return;
30
31         dbus_error_init(&err);
32         ret = dbus_message_get_args(msg, &err, DBUS_TYPE_INT32, &result, DBUS_TYPE_INVALID);
33         if (!ret) {
34                 _E("no message [%s:%s]", err.name, err.message);
35                 dbus_error_free(&err);
36                 return;
37         }
38         _D("succeed mem trim : %d", result);
39 }
40
41 static int request_mem_trim(void)
42 {
43         int ret;
44
45         ret = dbus_method_async_with_reply(DEVICED_BUS_NAME,
46                 DEVICED_PATH_STORAGE, DEVICED_INTERFACE_STORAGE,
47                 METHOD_MEM_TRIM, NULL, NULL, mem_trim, -1, NULL);
48         if (ret == 0)
49                 _D("request mem trim");
50         return ret;
51 }
52
53 static int storage(void)
54 {
55         DBusError err;
56         DBusMessage *msg;
57         int ret;
58         double dAvail;
59         double dTotal;
60
61         msg = dbus_method_sync_with_reply(DEVICED_BUS_NAME,
62                         DEVICED_PATH_STORAGE,
63                         DEVICED_INTERFACE_STORAGE,
64                         METHOD_GET_STORAGE, NULL, NULL);
65         if (!msg) {
66                 _E("fail : %s %s %s %s",
67                         DEVICED_BUS_NAME, DEVICED_PATH_SYSNOTI, DEVICED_INTERFACE_SYSNOTI,
68                         METHOD_GET_STORAGE);
69                 return -EBADMSG;
70         }
71
72         dbus_error_init(&err);
73
74         ret = dbus_message_get_args(msg, &err, DBUS_TYPE_INT64, &dTotal, DBUS_TYPE_INT64, &dAvail, DBUS_TYPE_INVALID);;
75         if (!ret) {
76                 _E("no message : [%s:%s]", err.name, err.message);
77         }
78         _I("total : %4.4lf avail %4.4lf", dTotal, dAvail);
79         if (ret < 0)
80                 _R("[NG] ---- %s", __func__);
81         else
82                 _R("[OK] ---- %s          : T(%4.4lf) A(%4.4lf)",
83                 __func__, dTotal, dAvail);
84         dbus_message_unref(msg);
85         dbus_error_free(&err);
86         sleep(TEST_WAIT_TIME_INTERVAL);
87         return ret;
88 }
89
90 static void storage_init(void *data)
91 {
92         _I("start test");
93         storage();
94 }
95
96 static void storage_exit(void *data)
97 {
98         _I("end test");
99 }
100
101 static int storage_unit(int argc, char **argv)
102 {
103         if (argv[1] == NULL)
104                 return -EINVAL;
105         if (strcmp(argv[2], "value") == 0)
106                 storage();
107         else if (strcmp(argv[2], "signal") == 0) {
108                 request_mem_trim();
109                 ecore_main_loop_begin();
110         }
111         return 0;
112 }
113
114 static const struct test_ops storage_test_ops = {
115         .priority = TEST_PRIORITY_NORMAL,
116         .name     = "storage",
117         .init     = storage_init,
118         .exit    = storage_exit,
119         .unit    = storage_unit,
120 };
121
122 TEST_OPS_REGISTER(&storage_test_ops)