8b52c19bd4c2cc4635130d97b9094e304ceab981
[platform/core/system/deviced.git] / src / auto-test / udev.c
1 /*
2  * test
3  *
4  * Copyright (c) 2020 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 static const struct device_change_type {
21         char *name;
22         char *status;
23 } device_change_types[] = {
24         {"udev",                "start"},
25         {"udev",                "stop"},
26 };
27
28 static int udev(int index)
29 {
30         GVariant *msg;
31         int ret = 0, val = 0, err;
32
33         err = dbus_handle_method_sync_with_reply_var(DEVICED_BUS_NAME,
34                         DEVICED_PATH_SYSNOTI,
35                         DEVICED_INTERFACE_SYSNOTI,
36                         "udev",
37                         g_variant_new("(sis)", "udev", 1, device_change_types[index].status),
38                         &msg);
39         if (err < 0) {
40                 _E("fail : %s %s %s %s",
41                         DEVICED_BUS_NAME, DEVICED_PATH_SYSNOTI, DEVICED_INTERFACE_SYSNOTI,
42                         "udev");
43                 return -EBADMSG;
44         }
45
46         if (!g_variant_get_safe(msg, "(i)", &val))
47                 _E("fail : no message");
48         else {
49                 if ((val == -ENOTSUP) || (val == -ENOSYS)) {
50                         _I("Not supported feature! : %d", val);
51                         ret = TRUE;
52                 } else if (val < 0) {
53                         _E("fail : returned fail (%d)", val);
54                 } else {
55                         _I("success : %d", val);
56                         ret = TRUE;
57                 }
58         }
59
60         _I("%s", device_change_types[index].status);
61         if (val < 0)
62                 _R("[NG] ---- %s             : %s",
63                 __func__, device_change_types[index].status);
64         else
65                 _R("[OK] ---- %s             : %s",
66                 __func__, device_change_types[index].status);
67
68         g_variant_unref(msg);
69         sleep(TEST_WAIT_TIME_INTERVAL);
70         return ret;
71 }
72
73 static void unit(char *unit, char *status)
74 {
75         int index;
76         int unit_len;
77         int status_len;
78
79         if (!unit || !status)
80                 return;
81
82         unit_len = strlen(unit);
83         status_len = strlen(status);
84
85         for (index = 0; index < ARRAY_SIZE(device_change_types); index++) {
86                 if (unit_len != strlen(device_change_types[index].name) ||
87                     status_len != strlen(device_change_types[index].status))
88                         continue;
89                 if (strncmp(unit, device_change_types[index].name, unit_len) != 0 ||
90                     strncmp(status, device_change_types[index].status, status_len) != 0)
91                         continue;
92                 udev(index);
93         }
94 }
95
96 static void udev_init(void *data)
97 {
98         int index;
99
100         _I("start test");
101         for (index = 0; index < ARRAY_SIZE(device_change_types); index++)
102                 udev(index);
103 }
104
105 static void udev_exit(void *data)
106 {
107         _I("end test");
108 }
109
110 static int udev_unit(int argc, char **argv)
111 {
112         if (argc != 4)
113                 return -EINVAL;
114
115         unit(argv[2], argv[3]);
116         return 0;
117 }
118
119 static const struct test_ops udev_test_ops = {
120         .priority = TEST_PRIORITY_HIGH,
121         .name     = "udev",
122         .init     = udev_init,
123         .exit    = udev_exit,
124         .unit    = udev_unit,
125 };
126
127 TEST_OPS_REGISTER(&udev_test_ops)