Add gtest for coverage and auto test
[platform/core/api/uwb.git] / tests / capi-network-uwb-test.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <string.h>
5 #include <errno.h>
6 #include <time.h>
7 #include <assert.h>
8
9 #include <glib.h>
10 #include <glib-object.h>
11
12 #include <uwb.h>
13
14 #define RESET_COLOR "\e[m"
15 #define MAKE_RED "\e[31m"
16 #define MAKE_GREEN "\e[32m"
17
18 #define __FUNC_ENTER__ printf("%s() entering...\n", __func__)
19 #define __FUNC_EXIT__ printf("%s() leaving...\n", __func__)
20
21 #define RET_IF_LOOP_IS_NULL()\
22         do {\
23                 if (!g_main_loop_p) {\
24                         printf("Loop was not initialized\n");\
25                         return;\
26                 } \
27         } while (0)
28
29
30 enum {
31         CMD_QUIT,
32         CMD_FULL_MENU,
33         CMD_INITIALIZE,
34         CMD_DEINITIALIZE,
35         CMD_RESET,
36         CMD_FACTORY_RESET,
37         CMD_GET_OWN_NODE,
38         CMD_GET_NETWORK,
39         CMD_SET_POSITION,
40         CMD_SEND_MESSAGE,
41         CMD_SEND_MESSAGE_TO,
42         CMD_GET_CONFIGURATION,
43         CMD_SET_CONFIGURATION,
44
45         CMD_INVALID,
46 };
47
48 char *g_menu_str[] = {
49                 [CMD_QUIT]
50                  = "QUIT",
51                 [CMD_FULL_MENU]
52                  = "FULL_MENU",
53                 [CMD_INITIALIZE]
54                  = "INITIALIZE",
55                 [CMD_DEINITIALIZE]
56                  = "DEINITIALIZE",
57                 [CMD_RESET]
58                  = "RESET",
59                 [CMD_FACTORY_RESET]
60                  = "FACTORY_RESET",
61                 [CMD_GET_OWN_NODE]
62                  = "GET_OWN_NODE",
63                 [CMD_GET_NETWORK]
64                  = "GET_NETWORK",
65
66                 [CMD_SET_POSITION]
67                  = "SET_POSITION",
68                 [CMD_SEND_MESSAGE]
69                  = "SEND_MESSAGE",
70                 [CMD_SEND_MESSAGE_TO]
71                  = "SEND_MESSAGE_TO",
72                 [CMD_GET_CONFIGURATION]
73                  = "GET_CONFIGURATION",
74                 [CMD_SET_CONFIGURATION]
75                  = "SET_CONFIGURATION",
76
77                 [CMD_INVALID]
78                  = NULL, };
79
80 static GMainLoop* g_main_loop_p;
81
82 static const char *__print_error(uwb_error_e err_type)
83 {
84         switch (err_type) {
85         case UWB_ERROR_NONE:
86                 return "NONE";
87         case UWB_ERROR_IO_ERROR:
88                 return "IO_ERROR";
89         case UWB_ERROR_OUT_OF_MEMORY:
90                 return "OUT_OF_MEMORY";
91         case UWB_ERROR_INVALID_PARAMETER:
92                 return "INVALID_PARAMETER";
93         case UWB_ERROR_PERMISSION_DENIED:
94                 return "PERMISSION_DENIED";
95         case UWB_ERROR_NOT_SUPPORTED:
96                 return "NOT_SUPPORTED";
97         case UWB_ERROR_OPERATION_FAILED:
98                 return "OPERATION_FAILED";
99         case UWB_ERROR_ALREADY_INITIALIZED:
100                 return "ALREADY_INITIALIZED";
101         case UWB_ERROR_NOT_INITIALIZED:
102                 return "NOT_INITIALIZED";
103         default:
104                 break;
105         }
106         return "UNKNOWN";
107 }
108
109 static inline void __print_result(int ret, gchar *function_name)
110 {
111         if (ret == UWB_ERROR_NONE) {
112                 printf(MAKE_GREEN"%s"RESET_COLOR"\n", function_name);
113         } else {
114                 printf(MAKE_RED"%s : %s ", function_name, __print_error(ret));
115                 printf(RESET_COLOR"\n");
116         }
117
118         printf("%s() result=[%d]\n", function_name, ret);
119 }
120
121 static char *__cmd_transform(char *str)
122 {
123         int i, j;
124         int len;
125         static char static_buffer[255];
126
127         if (str == NULL)
128                 return "";
129
130         len = strlen(str);
131         if (len == 0)
132                 return "";
133
134         /* lower char */
135         /* replance "_" to space */
136         for (i = 0, j = 0; i < len; i++, j++) {
137
138                 if (str[j] >= 'A' && str[j] <= 'Z')
139                         static_buffer[i] = str[j] + 'a' - 'A';
140                 else if (str[j] == '_')
141                         static_buffer[i] = ' ';
142                 else
143                         static_buffer[i] = str[j];
144         }
145         static_buffer[j] = '\0';
146
147         return static_buffer;
148 }
149
150 static inline void __usage_full()
151 {
152         int i;
153         printf("Call Test Program\n");
154
155         for (i = CMD_QUIT; i < CMD_INVALID; i++) {
156                 if (i%3 == 0)
157                         printf("\n");
158                 printf(" %02d: %-32s ", i,
159                                 __cmd_transform(g_menu_str[i]));
160         }
161         printf("\n");
162 }
163
164 static int __is_digit(const char* str)
165 {
166         int len;
167         int i;
168
169         if (str == NULL)
170                 return -1;
171
172         if (strlen(str) == 0)
173                 return -1;
174
175         len = strlen(str);
176         for (i = 0; i < len; i++) {
177                 if (str[i] < '0' || str[i] > '9')
178                         return -2;
179         }
180
181         return 0;
182 }
183
184 void test_full_menu(void)
185 {
186         __usage_full();
187
188         return;
189 }
190
191 void test_quit(void)
192 {
193         RET_IF_LOOP_IS_NULL();
194
195         printf("Bye\n");
196         g_main_loop_quit(g_main_loop_p);
197
198         return;
199 }
200
201 void test_init(void)
202 {
203         int ret = 0;
204
205         RET_IF_LOOP_IS_NULL();
206
207         ret = uwb_initialize();
208         __print_result(ret, "uwb_initialize");
209
210         return;
211 }
212
213 void test_deinit(void)
214 {
215         int ret = 0;
216
217         RET_IF_LOOP_IS_NULL();
218
219         ret = uwb_deinitialize();
220         __print_result(ret, "uwb_deinitialize");
221
222         return;
223 }
224
225 void test_reset(void)
226 {
227         int ret = 0;
228
229         RET_IF_LOOP_IS_NULL();
230
231         ret = uwb_reset();
232         __print_result(ret, "uwb_reset");
233
234         return;
235 }
236
237 void test_factory_reset(void)
238 {
239         int ret = 0;
240
241         RET_IF_LOOP_IS_NULL();
242
243         ret = uwb_factory_reset();
244         __print_result(ret, "uwb_factory_reset");
245
246         return;
247 }
248
249 bool __print_node_info(uwb_node_h node, void *user_data)
250 {
251         uint64_t distance = 0;
252         uint64_t node_id = 0;
253         uint64_t pan_id = 0;
254         bool is_remote = 0;
255         int x = 0, y = 0, z = 0;
256         int ret = 0;
257
258         if (node == NULL)
259                 return true;
260
261         ret = uwb_node_get_distance(node, &distance);
262         __print_result(ret, "uwb_node_get_distance");
263         ret = uwb_node_get_node_id(node, &node_id);
264         __print_result(ret, "uwb_node_get_node_id");
265         ret = uwb_node_get_pan_id(node, &pan_id);
266         __print_result(ret, "uwb_node_get_pan_id");
267         ret = uwb_node_get_is_remote(node, &is_remote);
268         __print_result(ret, "uwb_node_get_is_remote");
269         ret = uwb_node_get_position(node, &x, &y, &z);
270         __print_result(ret, "uwb_node_get_position");
271
272         printf("Distance :%llu Node ID: %llu Pan ID: %llu\n", distance, node_id, pan_id);
273         printf("Position X: %d Y: %d Z: %d\n", x, y, z);
274         return true;
275 }
276
277 void test_get_own_node(void)
278 {
279         uwb_node_h own_node = NULL;
280         int ret = 0;
281
282         RET_IF_LOOP_IS_NULL();
283
284         ret = uwb_get_own_node(&own_node);
285         __print_result(ret, "uwb_get_own_node");
286
287         if (own_node == NULL)
288                 return;
289
290         uwb_node_h cloned_node = NULL;
291         ret = uwb_node_clone(own_node, &cloned_node);
292         __print_result(ret, "uwb_node_clone");
293
294         ret = uwb_node_destroy(own_node);
295         __print_result(ret, "uwb_node_destroy");
296
297         if (cloned_node == NULL)
298                 return;
299
300         __print_node_info(cloned_node, NULL);
301         ret = uwb_node_destroy(cloned_node);
302         __print_result(ret, "cloned_node");
303
304         return;
305 }
306
307 void __print_network_info(uwb_network_h uwb_network)
308 {
309         uint64_t pan_id = 0;
310         int remote_node_count = 0;
311         int ret = 0;
312
313         ret = uwb_network_get_pan_id(uwb_network, &pan_id);
314         __print_result(ret, "uwb_network_get_pan_id");
315         ret = uwb_network_get_remote_node_count(uwb_network, &remote_node_count);
316         __print_result(ret, "uwb_network_get_remote_node_count");
317
318         printf("Pan ID: %llu remote node count: %d\n", pan_id, remote_node_count);
319         uwb_network_foreach_remote_node(uwb_network, __print_node_info, NULL);
320 }
321
322 void __network_finished_cb(int result, uwb_network_h uwb_network, void *user_data)
323 {
324         int ret = 0;
325         if (result != UWB_ERROR_NONE)
326                 return;
327
328         if (uwb_network == NULL)
329                 return;
330
331         uwb_network_h cloned_network = NULL;
332         ret = uwb_network_clone(uwb_network, &cloned_network);
333         __print_result(ret, "uwb_network_clone");
334
335         ret = uwb_network_destroy(uwb_network);
336         __print_result(ret, "uwb_network_destroy");
337
338         if (cloned_network == NULL)
339                 return;
340
341         __print_network_info(cloned_network);
342         ret = uwb_network_destroy(cloned_network);
343         __print_result(ret, "uwb_network_destroy");
344
345 }
346
347 void test_get_network(void)
348 {
349         int ret = 0;
350
351         RET_IF_LOOP_IS_NULL();
352
353         ret = uwb_get_network(__network_finished_cb, NULL);
354         __print_result(ret, "uwb_get_network");
355
356         return;
357 }
358
359 void test_set_position(void)
360 {
361         uwb_node_h own_node = NULL;
362         int ret = 0;
363         int test_x = 1000;
364         int test_y = 1000;
365         int test_z = 1000;
366
367         RET_IF_LOOP_IS_NULL();
368         ret = uwb_get_own_node(&own_node);
369         __print_result(ret, "uwb_get_own_node");
370
371
372         ret = uwb_node_set_position(own_node, test_x, test_y, test_z);
373         __print_result(ret, "uwb_node_set_position");
374
375         ret = uwb_node_destroy(own_node);
376         __print_result(ret, "uwb_node_destroy");
377 }
378
379 void test_send_message(void)
380 {
381         uwb_node_h own_node = NULL;
382         int ret = 0;
383         const char test_string[] = {"test string"};
384
385         ret = uwb_get_own_node(&own_node);
386         __print_result(ret, "uwb_get_own_node");
387
388         ret = uwb_node_send_message((const unsigned char *)test_string, strlen(test_string));
389         __print_result(ret, "uwb_node_send_message");
390
391         ret = uwb_node_destroy(own_node);
392         __print_result(ret, "uwb_node_destroy");
393 }
394
395 void test_send_message_to(void)
396 {
397         uwb_node_h own_node = NULL;
398         int ret = 0;
399         const char test_string[] = {"test string"};
400
401         ret = uwb_get_own_node(&own_node);
402         __print_result(ret, "uwb_get_own_node");
403
404         ret = uwb_node_send_message_to(own_node, (const unsigned char *)test_string, strlen(test_string));
405         __print_result(ret, "uwb_node_send_message");
406
407         ret = uwb_node_destroy(own_node);
408         __print_result(ret, "uwb_node_destroy");
409 }
410
411 void test_get_configuration(void)
412 {
413         uwb_node_h own_node = NULL;
414         int data_type = 0;
415         char *key;
416         int32_t int32_data;
417         int64_t int64_data;
418         char *string_data = NULL;
419         int ret = 0;
420
421         RET_IF_LOOP_IS_NULL();
422
423         printf("Select Data type :\n");
424         printf("0.  int32_t\n");
425         printf("1.  int64_t\n");
426         printf("2.  const char *\n");
427         if (scanf("%d", &data_type) < 1)
428                 return;
429
430         printf("Input key :\n");
431         if (scanf(" %255ms", &key) < 1)
432                 return;
433
434         ret = uwb_get_own_node(&own_node);
435         __print_result(ret, "uwb_get_own_node");
436         if (!own_node)
437                 goto out;
438
439         if (data_type == 0) {
440                 ret = uwb_node_get_configuration_int32(own_node, key, &int32_data);
441                 __print_result(ret, "uwb_node_get_configuration_int32");
442                 printf("Value [%d]\n", int32_data);
443         } else if (data_type == 1) {
444                 ret = uwb_node_get_configuration_int64(own_node, key, &int64_data);
445                 __print_result(ret, "uwb_node_get_configuration_int64");
446                 printf("Value [%lld]\n", int64_data);
447         } else if (data_type == 2) {
448                 ret = uwb_node_get_configuration_string(own_node, key, &string_data);
449                 __print_result(ret, "uwb_node_get_configuration_string");
450                 if (string_data)
451                         printf("Value [%s]\n", string_data);
452         }
453
454 out:
455         if (key)
456                 free(key);
457         uwb_node_destroy(own_node);
458         if (string_data)
459                 free(string_data);
460 }
461
462 void test_set_configuration(void)
463 {
464         uwb_node_h own_node = NULL;
465         int data_type = 0;
466         char *key;
467         int32_t int32_data = 2020;
468         int64_t int64_data = 2020;
469         char *string_data = NULL;
470         int ret = 0;
471
472         RET_IF_LOOP_IS_NULL();
473
474         printf("Select Data type :\n");
475         printf("0.  int32_t\n");
476         printf("1.  int64_t\n");
477         printf("2.  const char *\n");
478         if (scanf("%d", &data_type) < 1)
479                 return;
480
481         printf("Input key :\n");
482         if (scanf(" %255ms", &key) < 1)
483                 return;
484
485         ret = uwb_get_own_node(&own_node);
486         __print_result(ret, "uwb_get_own_node");
487         if (!own_node)
488                 goto out;
489
490         if (data_type == 0) {
491                 ret = uwb_node_set_configuration_int32(own_node, key, int32_data);
492                 __print_result(ret, "uwb_node_set_configuration_int32");
493         } else if (data_type == 1) {
494                 ret = uwb_node_set_configuration_int64(own_node, key, int64_data);
495                 __print_result(ret, "uwb_node_set_configuration_int64");
496         } else if (data_type == 2) {
497                 printf("Input string :\n");
498                 if (scanf(" %255ms", &string_data) < 1) {
499                         ret = uwb_node_destroy(own_node);
500                         __print_result(ret, "uwb_node_destroy");
501
502                         return;
503                 }
504                 ret = uwb_node_set_configuration_string(own_node, key, string_data);
505                 __print_result(ret, "uwb_node_set_configuration_string");
506         }
507
508 out:
509         if (key)
510                 free(key);
511         uwb_node_destroy(own_node);
512         if (string_data)
513                 free(string_data);
514 }
515
516
517 typedef void (*test_func)(void);
518 test_func g_menu_func[] = {
519                 [CMD_QUIT]
520                  = test_quit,
521                 [CMD_FULL_MENU]
522                  = test_full_menu,
523
524                 [CMD_INITIALIZE]
525                  = test_init,
526                 [CMD_DEINITIALIZE]
527                  = test_deinit,
528
529                 [CMD_RESET]
530                  = test_reset,
531                 [CMD_FACTORY_RESET]
532                  = test_factory_reset,
533
534                 [CMD_GET_OWN_NODE]
535                  = test_get_own_node,
536                 [CMD_GET_NETWORK]
537                  = test_get_network,
538
539                 [CMD_SET_POSITION]
540                  = test_set_position,
541                 [CMD_SEND_MESSAGE]
542                  = test_send_message,
543                 [CMD_SEND_MESSAGE_TO]
544                  = test_send_message_to,
545                 [CMD_GET_CONFIGURATION]
546                  = test_get_configuration,
547                 [CMD_SET_CONFIGURATION]
548                  = test_set_configuration,
549                 [CMD_INVALID]
550                  = NULL, };
551
552 static void __process_input(const char *input, gpointer user_data)
553 {
554         int cmd = strtol(input, NULL, 0);
555
556         if (__is_digit(input) < 0 || strlen(input) == 0 || errno == ERANGE || errno
557                         == EINVAL)
558                 cmd = CMD_INVALID;
559
560         printf("cmd=[%d]\n", cmd);
561         if (cmd >= CMD_INVALID || cmd < CMD_QUIT) {
562                 printf("Invalid CMD\n");
563                 return;
564         }
565         g_menu_func[cmd]();
566 }
567
568 static gboolean __test_terminal_read_std_input(GIOChannel * source,
569                 GIOCondition condition, gpointer user_data)
570 {
571         int fd = 0;
572
573         static char buf[1024];
574         int n;
575
576         errno = 0;
577         n = read(fd, buf, 1024);
578         if (n == 0) {
579                 printf("Error: read() from stdin returns 0.\n");
580         } else if (n < 0) {
581                 char error_buf[100] = {0, };
582                 strerror_r(errno, error_buf, sizeof(error_buf));
583                 printf("input: read, err=%s\n", error_buf);
584         } else {
585                 buf[n - 1] = '\0'; /* remove new line... */
586                 printf("\n\n");
587                 /* printf("Read [%d]bytes data: [%s]\n", n, buf); */
588                 /* printf("Processing it ---------------------\n", n, buf); */
589                 __process_input(buf, user_data);
590         }
591
592         return TRUE;
593 }
594
595 static void __glib_log(
596                 const gchar *log_domain,
597                 GLogLevelFlags log_level,
598                 const gchar *msg,
599                 gpointer user_data)
600 {
601         printf("[GLib Err] %s-0x%2.2X: %s\n",
602                         log_domain, log_level, msg);
603 }
604
605 int main(int argc, char **argv)
606 {
607
608 #if !GLIB_CHECK_VERSION(2, 36, 0)
609         g_type_init();
610 #endif
611
612         g_log_set_default_handler(__glib_log, NULL);
613         g_main_loop_p = g_main_loop_new(NULL, FALSE);
614
615         int std_input_fd = 0;
616         GIOChannel *gio2 = g_io_channel_unix_new(std_input_fd);
617         g_io_add_watch(gio2, G_IO_IN, (GIOFunc) __test_terminal_read_std_input, NULL);
618         g_io_channel_unref(gio2);
619
620         __usage_full();
621
622         g_main_loop_run(g_main_loop_p);
623
624         return 0;
625 }