more easy properties for network, fix typo with "Strength".
[framework/uifw/edbus.git] / src / bin / e_dbus_connman_test_api.c
1 #include "E_Connman.h"
2 #include <stdio.h>
3 #include <string.h>
4
5 #define DBG(...) EINA_LOG_DBG(__VA_ARGS__)
6 #define INF(...) EINA_LOG_INFO(__VA_ARGS__)
7 #define WRN(...) EINA_LOG_WARN(__VA_ARGS__)
8 #define ERR(...) EINA_LOG_ERR(__VA_ARGS__)
9
10 static int success = 0;
11 static int failure = 0;
12 static Ecore_Timer *exiter = NULL;
13
14 static bool
15 _test_string_get(E_Connman_Element *element, const char *name, bool (*func)(const E_Connman_Element *element, const char **value))
16 {
17    const char *value;
18    bool ret;
19
20    INF("BEGIN: testing string get %s of element %s...\n", name, element->path);
21    ret = func(element, &value);
22    if (ret)
23      INF("SUCCESS: testing string get %s of element %s: %s\n",
24          name, element->path, value);
25    else
26      WRN("FAILURE: testing string get %s of element %s\n",
27          name, element->path);
28
29    return ret;
30 }
31
32 static bool
33 _test_bool_get(E_Connman_Element *element, const char *name, bool (*func)(const E_Connman_Element *element, bool *value))
34 {
35    bool value, ret;
36
37    INF("BEGIN: testing bool get %s of element %s...\n", name, element->path);
38    ret = func(element, &value);
39    if (ret)
40      INF("SUCCESS: testing bool get %s of element %s: %hhu\n",
41          name, element->path, value);
42    else
43      WRN("FAILURE: testing bool get %s of element %s\n",
44          name, element->path);
45
46    return ret;
47 }
48
49 static bool
50 _test_uchar_get(E_Connman_Element *element, const char *name, bool (*func)(const E_Connman_Element *element, unsigned char *value))
51 {
52    unsigned char value;
53    bool ret;
54
55    INF("BEGIN: testing uchar get %s of element %s...\n", name, element->path);
56    ret = func(element, &value);
57    if (ret)
58      INF("SUCCESS: testing uchar get %s of element %s: %hhu\n",
59          name, element->path, value);
60    else
61      WRN("FAILURE: testing uchar get %s of element %s\n",
62          name, element->path);
63
64    return ret;
65 }
66
67 static bool
68 _test_ushort_get(E_Connman_Element *element, const char *name, bool (*func)(const E_Connman_Element *element, unsigned short *value))
69 {
70    unsigned short value;
71    bool ret;
72
73    INF("BEGIN: testing ushort get %s of element %s...\n", name, element->path);
74    ret = func(element, &value);
75    if (ret)
76      INF("SUCCESS: testing ushort get %s of element %s: %hu\n",
77          name, element->path, value);
78    else
79      WRN("FAILURE: testing ushort get %s of element %s\n",
80          name, element->path);
81
82    return ret;
83 }
84
85 static bool
86 _test_uchar_array_get(E_Connman_Element *element, const char *name, bool (*func)(const E_Connman_Element *element, unsigned int *count, unsigned char **value))
87 {
88    unsigned char *value;
89    unsigned int count;
90    bool ret;
91
92    INF("BEGIN: testing ushort get %s of element %s...\n", name, element->path);
93    ret = func(element, &count, &value);
94    if (ret)
95      {
96         INF("SUCCESS: testing ushort get %s of element %s: %p\n",
97             name, element->path, value);
98         free(value);
99      }
100    else
101      WRN("FAILURE: testing ushort get %s of element %s\n",
102          name, element->path);
103
104    return ret;
105 }
106
107 static bool
108 _test_element_get(E_Connman_Element *element, const char *name, bool (*func)(const E_Connman_Element *element, E_Connman_Element **value))
109 {
110    E_Connman_Element *value;
111    bool ret;
112
113    INF("BEGIN: testing element get %s of element %s...\n", name, element->path);
114    ret = func(element, &value);
115    if (ret)
116      INF("SUCCESS: testing element get %s of element %s: %p\n",
117          name, element->path, value);
118    else
119      WRN("FAILURE: testing element get %s of element %s\n",
120          name, element->path);
121
122    return ret;
123 }
124
125 static bool
126 _test_elements_get(E_Connman_Element *element, const char *name, bool (*func)(const E_Connman_Element *element, unsigned int *count, E_Connman_Element ***elements))
127 {
128    E_Connman_Element **value;
129    unsigned int count;
130    bool ret;
131
132    INF("BEGIN: testing elements get %s of element %s...\n",
133        name, element->path);
134    ret = func(element, &count, &value);
135    if (ret)
136      {
137         INF("SUCCESS: testing elements get %s of element %s: %p\n",
138             name, element->path, value);
139         free(value);
140      }
141    else
142      WRN("FAILURE: testing elements get %s of element %s\n",
143          name, element->path);
144
145    return ret;
146 }
147
148 static bool
149 _test_elements_get_global(const char *name, bool (*func)(unsigned int *count, E_Connman_Element ***elements))
150 {
151    E_Connman_Element **value;
152    unsigned int count;
153    bool ret;
154
155    INF("BEGIN: testing elements get %s\n", name);
156    ret = func(&count, &value);
157    if (ret)
158      {
159         INF("SUCCESS: testing elements get %s: %p\n", name, value);
160         free(value);
161      }
162    else
163      WRN("FAILURE: testing elements get %s\n", name);
164
165    return ret;
166 }
167
168 static bool
169 _test_string_get_global(const char *name, bool (*func)(const char **value))
170 {
171    const char *value;
172    bool ret;
173
174    INF("BEGIN: testing string get %s...\n", name);
175    ret = func(&value);
176    if (ret)
177      INF("SUCCESS: testing string get %s: %s\n", name, value);
178    else
179      WRN("FAILURE: testing string get %s\n", name);
180
181    return ret;
182 }
183
184 static bool
185 _test_bool_get_global(const char *name, bool (*func)(bool *value))
186 {
187    bool value, ret;
188
189    INF("BEGIN: testing bool get %s...\n", name);
190    ret = func(&value);
191    if (ret)
192      INF("SUCCESS: testing bool get %s: %hhu\n", name, value);
193    else
194      WRN("FAILURE: testing bool get %s\n", name);
195
196    return ret;
197 }
198
199 struct test_desc
200 {
201    const char *name;
202    enum {
203      TEST_DESC_TYPE_STRING_GET,
204      TEST_DESC_TYPE_BOOL_GET,
205      TEST_DESC_TYPE_UCHAR_GET,
206      TEST_DESC_TYPE_USHORT_GET,
207      TEST_DESC_TYPE_UCHAR_ARRAY_GET,
208      TEST_DESC_TYPE_ELEMENT_GET,
209      TEST_DESC_TYPE_ELEMENTS_GET,
210      TEST_DESC_TYPE_ELEMENTS_GET_GLOBAL,
211      TEST_DESC_TYPE_STRING_GET_GLOBAL,
212      TEST_DESC_TYPE_BOOL_GET_GLOBAL,
213      TEST_DESC_TYPE_LAST
214    } type;
215    union {
216       bool (*string_get)(const E_Connman_Element *element, const char **value);
217       bool (*bool_get)(const E_Connman_Element *element, bool *value);
218       bool (*uchar_get)(const E_Connman_Element *element, unsigned char *value);
219       bool (*ushort_get)(const E_Connman_Element *element, unsigned short*value);
220       bool (*uchar_array_get)(const E_Connman_Element *element, unsigned int *count, unsigned char **value);
221       bool (*element_get)(const E_Connman_Element *element, E_Connman_Element **value);
222       bool (*elements_get)(const E_Connman_Element *element, unsigned int *count, E_Connman_Element ***elements);
223       bool (*elements_get_global)(unsigned int *count, E_Connman_Element ***elements);
224       bool (*string_get_global)(const char **value);
225       bool (*bool_get_global)(bool *value);
226       void *dummy;
227    } func;
228    bool may_fail;
229 };
230
231 #define TEST_DESC_STRING_GET(_func, may_fail)                           \
232   {#_func, TEST_DESC_TYPE_STRING_GET, .func.string_get=_func, may_fail}
233 #define TEST_DESC_BOOL_GET(_func, may_fail)                             \
234   {#_func, TEST_DESC_TYPE_BOOL_GET, .func.bool_get=_func, may_fail}
235 #define TEST_DESC_UCHAR_GET(_func, may_fail)                            \
236   {#_func, TEST_DESC_TYPE_UCHAR_GET, .func.uchar_get=_func, may_fail}
237 #define TEST_DESC_USHORT_GET(_func, may_fail)                           \
238   {#_func, TEST_DESC_TYPE_USHORT_GET, .func.ushort_get=_func, may_fail}
239 #define TEST_DESC_UCHAR_ARRAY_GET(_func, may_fail)                      \
240   {#_func, TEST_DESC_TYPE_UCHAR_ARRAY_GET, .func.uchar_array_get=_func, may_fail}
241 #define TEST_DESC_ELEMENT_GET(_func, may_fail)                          \
242   {#_func, TEST_DESC_TYPE_ELEMENT_GET, .func.element_get=_func, may_fail}
243 #define TEST_DESC_ELEMENTS_GET(_func, may_fail)                         \
244   {#_func, TEST_DESC_TYPE_ELEMENTS_GET, .func.elements_get=_func, may_fail}
245 #define TEST_DESC_ELEMENTS_GET_GLOBAL(_func, may_fail)                  \
246   {#_func, TEST_DESC_TYPE_ELEMENTS_GET_GLOBAL, .func.elements_get_global=_func, may_fail}
247 #define TEST_DESC_STRING_GET_GLOBAL(_func, may_fail)                    \
248   {#_func, TEST_DESC_TYPE_STRING_GET_GLOBAL, .func.string_get_global=_func, may_fail}
249 #define TEST_DESC_BOOL_GET_GLOBAL(_func, may_fail)                      \
250   {#_func, TEST_DESC_TYPE_BOOL_GET_GLOBAL, .func.bool_get_global=_func, may_fail}
251 #define TEST_DESC_SENTINEL {NULL, TEST_DESC_TYPE_LAST, .func.dummy=NULL}
252
253 static bool
254 _test_element(E_Connman_Element *element, const struct test_desc *test_descs)
255 {
256    const struct test_desc *itr;
257    int total, ok = 0, fail = 0;
258    bool ret = 1;
259
260    for (itr = test_descs; itr->type != TEST_DESC_TYPE_LAST; itr++)
261      {
262         bool r;
263
264         switch (itr->type)
265           {
266            case TEST_DESC_TYPE_STRING_GET:
267               r = _test_string_get(element, itr->name, itr->func.string_get);
268               break;
269            case TEST_DESC_TYPE_BOOL_GET:
270               r = _test_bool_get(element, itr->name, itr->func.bool_get);
271               break;
272            case TEST_DESC_TYPE_UCHAR_GET:
273               r = _test_uchar_get(element, itr->name, itr->func.uchar_get);
274               break;
275            case TEST_DESC_TYPE_USHORT_GET:
276               r = _test_ushort_get(element, itr->name, itr->func.ushort_get);
277               break;
278            case TEST_DESC_TYPE_UCHAR_ARRAY_GET:
279               r = _test_uchar_array_get
280                 (element, itr->name, itr->func.uchar_array_get);
281               break;
282            case TEST_DESC_TYPE_ELEMENT_GET:
283               r = _test_element_get
284                 (element, itr->name, itr->func.element_get);
285               break;
286            case TEST_DESC_TYPE_ELEMENTS_GET:
287               r = _test_elements_get
288                 (element, itr->name, itr->func.elements_get);
289               break;
290            case TEST_DESC_TYPE_ELEMENTS_GET_GLOBAL:
291               r = _test_elements_get_global
292                 (itr->name, itr->func.elements_get_global);
293               break;
294            case TEST_DESC_TYPE_STRING_GET_GLOBAL:
295               r = _test_string_get_global
296                 (itr->name, itr->func.string_get_global);
297               break;
298            case TEST_DESC_TYPE_BOOL_GET_GLOBAL:
299               r = _test_bool_get_global
300                 (itr->name, itr->func.bool_get_global);
301               break;
302            default:
303               ERR("unknown test type %d (%s)\n", itr->type, itr->name);
304               r = 0;
305               break;
306           }
307
308         if (r || itr->may_fail)
309           ok++;
310         else
311           {
312              ERR("test failed %s, element %s [%s]\n",
313                  itr->name, element->path, element->interface);
314              fail++;
315              ret = 0;
316           }
317      }
318
319    total = ok + failure;
320    success += ok;
321    failure += fail;
322    if (total == 0)
323      {
324         INF("no tests for %s [%s]\n", element->path, element->interface);
325         return 1;
326      }
327
328    INF("testing %s success: %d, failure: %d: %d%% [%s]\n",
329        element->path, ok, fail, (ok * 100) / total,
330        element->interface);
331
332    return ret;
333 }
334
335 static const struct test_desc test_desc_manager[] = {
336   TEST_DESC_STRING_GET_GLOBAL(e_connman_manager_state_get, 0),
337   TEST_DESC_BOOL_GET_GLOBAL(e_connman_manager_offline_mode_get, 0),
338   //TEST_DESC_BOOL_SET_GLOBAL(e_connman_manager_offline_mode_set, 0),
339   TEST_DESC_ELEMENTS_GET_GLOBAL(e_connman_manager_profiles_get, 0),
340   TEST_DESC_ELEMENTS_GET_GLOBAL(e_connman_manager_devices_get, 0),
341   TEST_DESC_ELEMENTS_GET_GLOBAL(e_connman_manager_services_get, 1),
342   TEST_DESC_SENTINEL
343 };
344
345 static const struct test_desc test_desc_device[] = {
346   TEST_DESC_STRING_GET(e_connman_device_address_get, 0),
347   TEST_DESC_STRING_GET(e_connman_device_name_get, 0),
348   TEST_DESC_STRING_GET(e_connman_device_type_get, 0),
349   TEST_DESC_STRING_GET(e_connman_device_interface_get, 0),
350   TEST_DESC_BOOL_GET(e_connman_device_powered_get, 0),
351   //TEST_DESC_BOOL_SET(e_connman_device_powered_set, 0),
352   TEST_DESC_USHORT_GET(e_connman_device_scan_interval_get, 1),
353   //TEST_DESC_USHORT_SET(e_connman_device_scan_interval_set, 1),
354   TEST_DESC_BOOL_GET(e_connman_device_scanning_get, 1),
355   TEST_DESC_ELEMENTS_GET(e_connman_device_networks_get, 1),
356   TEST_DESC_SENTINEL
357 };
358
359 static const struct test_desc test_desc_profile[] = {
360   TEST_DESC_STRING_GET(e_connman_profile_name_get, 1),
361   TEST_DESC_BOOL_GET(e_connman_profile_offline_mode_get, 0),
362   //TEST_DESC_BOOL_SET(e_connman_profile_offline_mode_set, 0),
363   TEST_DESC_ELEMENTS_GET(e_connman_profile_services_get, 1),
364   TEST_DESC_SENTINEL
365 };
366
367 static const struct test_desc test_desc_network[] = {
368   TEST_DESC_STRING_GET(e_connman_network_address_get, 0),
369   TEST_DESC_STRING_GET(e_connman_network_name_get, 0),
370   TEST_DESC_BOOL_GET(e_connman_network_connected_get, 0),
371   TEST_DESC_UCHAR_GET(e_connman_network_strength_get, 1),
372   TEST_DESC_USHORT_GET(e_connman_network_frequency_get, 1),
373   TEST_DESC_ELEMENT_GET(e_connman_network_device_get, 0),
374   TEST_DESC_UCHAR_ARRAY_GET(e_connman_network_wifi_ssid_get, 1),
375   TEST_DESC_STRING_GET(e_connman_network_wifi_mode_get, 1),
376   TEST_DESC_STRING_GET(e_connman_network_wifi_security_get, 1),
377   TEST_DESC_STRING_GET(e_connman_network_wifi_passphrase_get, 1),
378   TEST_DESC_USHORT_GET(e_connman_network_wifi_channel_get, 1),
379   TEST_DESC_STRING_GET(e_connman_network_wifi_eap_get, 1),
380   TEST_DESC_SENTINEL
381 };
382
383 static const struct test_desc test_desc_service[] = {
384   /* TODO: need to check exactly what properties may fail */
385   TEST_DESC_STRING_GET(e_connman_service_state_get, 1),
386   TEST_DESC_STRING_GET(e_connman_service_error_get, 1),
387   TEST_DESC_STRING_GET(e_connman_service_name_get, 0),
388   TEST_DESC_STRING_GET(e_connman_service_type_get, 0),
389   TEST_DESC_STRING_GET(e_connman_service_mode_get, 1),
390   TEST_DESC_STRING_GET(e_connman_service_security_get, 1),
391   TEST_DESC_STRING_GET(e_connman_service_passphrase_get, 1),
392   //TEST_DESC_STRING_SET(e_connman_service_passphrase_set, 1),
393   TEST_DESC_BOOL_GET(e_connman_service_passphrase_required_get, 1),
394   TEST_DESC_UCHAR_GET(e_connman_service_strength_get, 1),
395   TEST_DESC_BOOL_GET(e_connman_service_favorite_get, 0),
396   TEST_DESC_BOOL_GET(e_connman_service_immutable_get, 0),
397   TEST_DESC_BOOL_GET(e_connman_service_auto_connect_get, 0),
398   //TEST_DESC_BOOL_SET(e_connman_service_auto_connect_set, 1),
399   TEST_DESC_BOOL_GET(e_connman_service_setup_required_get, 1),
400   TEST_DESC_STRING_GET(e_connman_service_apn_get, 1),
401   //TEST_DESC_STRING_SET(e_connman_service_apn_set, 1),
402   TEST_DESC_STRING_GET(e_connman_service_mcc_get, 1),
403   TEST_DESC_STRING_GET(e_connman_service_mnc_get, 1),
404   TEST_DESC_BOOL_GET(e_connman_service_roaming_get, 1),
405   TEST_DESC_STRING_GET(e_connman_service_ipv4_method_get, 1),
406   TEST_DESC_STRING_GET(e_connman_service_ipv4_address_get, 1),
407   TEST_DESC_SENTINEL
408 };
409
410 static int
411 _quit(void *data)
412 {
413    ecore_main_loop_quit();
414    return 0;
415 }
416
417 static int
418 _on_exiter(void *data)
419 {
420    e_connman_system_shutdown();
421    ecore_idle_enterer_add(_quit, NULL);
422    exiter = NULL;
423    return 0;
424 }
425
426 static void
427 _exiter_reschedule(void)
428 {
429    if (exiter)
430      ecore_timer_del(exiter);
431    exiter = ecore_timer_add(10, _on_exiter, NULL);
432 }
433
434 struct test_element_timer_data
435 {
436    E_Connman_Element *element;
437    Ecore_Timer *timer;
438 };
439
440 static int
441 _test_element_timer(void *data)
442 {
443    struct test_element_timer_data *d = data;
444    E_Connman_Element *element = d->element;
445
446    if (e_connman_element_is_device(element))
447      _test_element(element, test_desc_device);
448    else if (e_connman_element_is_profile(element))
449      _test_element(element, test_desc_profile);
450    else if (e_connman_element_is_network(element))
451      _test_element(element, test_desc_network);
452    else if (e_connman_element_is_manager(element))
453      _test_element(element, test_desc_manager);
454    else if (e_connman_element_is_service(element))
455      _test_element(element, test_desc_service);
456    else
457      ERR("!!! don't know how to test %s [%s]\n",
458          element->path, element->interface);
459
460    _exiter_reschedule();
461
462    d->timer = NULL;
463    return 0;
464 }
465
466 static void
467 _element_listener(void *data, const E_Connman_Element *element)
468 {
469    struct test_element_timer_data *d = data;
470    if (d->timer)
471      ecore_timer_del(d->timer);
472    d->timer = ecore_timer_add(1.0, _test_element_timer, d);
473    _exiter_reschedule();
474 }
475
476 static void
477 _element_listener_free(void *data)
478 {
479    struct test_element_timer_data *d = data;
480    if (d->timer)
481      ecore_timer_del(d->timer);
482    free(d);
483 }
484
485 static int
486 _on_element_add(void *data, int type, void *info)
487 {
488    E_Connman_Element *element = info;
489    struct test_element_timer_data *d;
490
491    d = malloc(sizeof(*d));
492    if (!d)
493      return 1;
494
495    d->element = element;
496    d->timer = ecore_timer_add(1.0, _test_element_timer, d);
497    e_connman_element_listener_add
498      (element, _element_listener, d, _element_listener_free);
499
500    return 1;
501 }
502
503 static int
504 _on_element_del(void *data, int type, void *info)
505 {
506    return 1;
507 }
508
509 static int
510 _on_element_updated(void *data, int type, void *info)
511 {
512    return 1;
513 }
514
515 int
516 main(int argc, char *argv[])
517 {
518    E_DBus_Connection *c;
519    int total;
520
521    ecore_init();
522    e_dbus_init();
523    eina_init();
524
525    c = e_dbus_bus_get(DBUS_BUS_SYSTEM);
526    if (!c) {
527       printf("ERROR: can't connect to system session\n");
528       return -1;
529    }
530
531    e_connman_system_init(c);
532    ecore_event_handler_add(E_CONNMAN_EVENT_ELEMENT_ADD, _on_element_add, NULL);
533    ecore_event_handler_add(E_CONNMAN_EVENT_ELEMENT_DEL, _on_element_del, NULL);
534    ecore_event_handler_add(E_CONNMAN_EVENT_ELEMENT_UPDATED,
535                            _on_element_updated, NULL);
536
537    _exiter_reschedule();
538
539    ecore_main_loop_begin();
540
541    e_dbus_connection_close(c);
542    eina_stringshare_dump();
543    eina_shutdown();
544    e_dbus_shutdown();
545    ecore_shutdown();
546
547    total = success + failure;
548    if (total == 0)
549      fputs("DBG: clean exit, no tests executed.\n", stderr);
550    else
551      fprintf(stderr, "DBG: clean exit, success: %d, failure: %d, %d%%\n",
552              success, failure, (success * 100) / total);
553
554    return 0;
555 }