example: Fix testcases
[profile/ivi/message-port.git] / examples / test-app.c
1 /* vi: set et sw=4 ts=4 cino=t0,(0: */
2 /* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 /*
4  * Copyright (C) 2013 Intel Corporation.
5  *
6  * Contact: Amarnath Valluri <amarnath.valluri@linux.intel.com>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21  * 02110-1301 USA
22  */
23
24 #include <glib.h>
25 #include <unistd.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <message-port.h>
29 #include <bundle.h>
30 #include <unistd.h>
31
32 int __pipe[2]; /* pipe between two process */
33 const gchar *PARENT_TEST_PORT = "parent_test_port";
34 const gchar *PARENT_TEST_TRUSTED_PORT = "parent_test_trusted_port";
35 const gchar *CHILD_TEST_PORT = "child_test_port";
36 const gchar *CHILD_TEST_TRUSTED_PORT = "child_test_trusted_port";
37
38 struct AsyncTestData
39 {
40     GMainLoop *m_loop;
41     gboolean   result;
42 } *__test_data  = NULL;
43
44 #define TEST_CASE(case) \
45 do { \
46     if (case() != TRUE) { \
47         g_printerr ("%s: FAIL\n", #case); \
48         return -1; \
49     } \
50     else g_print ("%s: SUCCESS\n", #case); \
51 }while (0)
52
53
54 #define test_assert(expr, msg...) \
55 do { \
56     if ((expr) == FALSE) {\
57         g_print ("%s +%d: assert(%s):%s\n", __FUNCTION__, __LINE__, #expr, ##msg); \
58         return FALSE; \
59     } \
60 } while(0);
61
62
63 static void _dump_data (const char *key, const int type, const bundle_keyval_t *kv, void *user_data)
64 {
65     gchar *val = NULL;
66     size_t size;
67     bundle_keyval_get_basic_val ((bundle_keyval_t*)kv, (void**)&val, &size);
68     g_debug ("       %s - %s", key, val);
69 }
70
71 void (_on_child_got_message)(int port_id, const char* remote_app_id, const char* remote_port, gboolean trusted_message, bundle* data)
72 {
73     gchar *name = NULL;
74     messageport_get_local_port_name (port_id, &name),
75     g_debug ("CHILD: GOT MESSAGE at prot '%s' FROM :'%s' - '%s", name,
76         remote_app_id ? remote_app_id : "unknwon app", remote_port ? remote_port : "unknwon");
77     g_free (name);
78     g_assert (data);
79
80     bundle_foreach (data, _dump_data, NULL);
81
82     /* Write acknoledgement */
83     if (write (__pipe[1], "OK", strlen("OK") + 1) < 3) {
84         g_warning ("WRITE failed");
85     }
86
87     if (__test_data) {
88         __test_data->result = TRUE;
89         g_main_loop_quit (__test_data->m_loop);
90     }
91 }
92
93 void (_on_parent_got_message)(int port_id, const char* remote_app_id, const char* remote_port, gboolean trusted_message, bundle* data)
94 {
95     gchar *name = NULL;
96     gboolean found = FALSE;
97     bundle *b = NULL;
98     messageport_get_local_port_name (port_id, &name),
99     g_debug ("PARENT: GOT MESSAGE at prot %s FROM :'%s' app - '%s' port", name,
100         remote_app_id ? remote_app_id : "unknwon", remote_port ? remote_port : "unknwon");
101     g_free (name);
102
103     g_assert (data);
104
105     bundle_foreach (data, _dump_data, NULL);
106
107     /* Write acknoledgement */
108     if ( write (__pipe[1], "OK", strlen("OK") + 1) < 3) {
109         g_warning ("WRITE failed");
110     }
111
112     /* check message is coming from remote port to send back to message,
113      * if not ignore */
114     if (!remote_app_id || !remote_port) {
115         return;
116     }
117
118     messageport_error_e res = trusted_message ? messageport_check_trusted_remote_port (remote_app_id, remote_port, &found)
119                                               : messageport_check_remote_port (remote_app_id, remote_port, &found);
120     if (!found) {
121         g_warning ("PARENT: Could not found remote port (%d)", res);
122         return ;
123     }
124
125     g_debug ("PARENT: Found remote prot");
126
127     bundle *reply = bundle_create ();
128
129     bundle_add (reply, "Results", "GOT_IT");
130
131     g_debug ("PARENT: Sending reply ....");
132     res = trusted_message ? messageport_send_trusted_message (remote_app_id, remote_port, reply)
133                           : messageport_send_message (remote_app_id, remote_port, reply);
134     bundle_free (reply);
135     if (res != MESSAGEPORT_ERROR_NONE)
136     {
137         g_warning ("PARENT: Faile to send message to server : %d", res);
138     }
139     else g_debug ("PARENT: Data sent successfully");
140 }
141
142 int _register_test_port (const gchar *port_name, gboolean is_trusted, messageport_message_cb cb)
143 {
144     int port_id = is_trusted ? messageport_register_trusted_local_port (port_name, cb)
145                              : messageport_register_local_port (port_name, cb);
146
147     return port_id;
148 }
149
150 static gboolean
151 test_register_local_port ()
152 {
153     int port_id =  _register_test_port (PARENT_TEST_PORT, FALSE, _on_parent_got_message);
154
155     test_assert (port_id >= 0, "Failed to register port '%s', error : %d", PARENT_TEST_PORT, port_id);
156
157     return TRUE;
158 }
159
160 static gboolean
161 test_register_trusted_local_port()
162 {
163     int port_id =  _register_test_port (PARENT_TEST_TRUSTED_PORT, TRUE, _on_parent_got_message);
164
165     test_assert (port_id >= 0, "Failed to register port '%s', error : %d", PARENT_TEST_TRUSTED_PORT, port_id);
166
167     return TRUE;
168 }
169
170 static gboolean
171 test_check_remote_port()
172 {
173     const gchar remote_app_id[128];
174     gboolean found = FALSE;
175     messageport_error_e res;
176
177     g_sprintf (remote_app_id, "%d", getppid());
178
179     res = messageport_check_remote_port (remote_app_id, PARENT_TEST_PORT, &found);
180
181     test_assert (res == MESSAGEPORT_ERROR_NONE, "Fail to find remote port '%s' at app_id '%s', error: %d", PARENT_TEST_PORT, remote_app_id, res);
182
183     return TRUE;
184 }
185
186 static gboolean
187 test_check_trusted_remote_port()
188 {
189     const gchar remote_app_id[128];
190     gboolean found = FALSE;
191     messageport_error_e res;
192
193     g_sprintf (remote_app_id, "%d", getppid());
194
195     res = messageport_check_trusted_remote_port (remote_app_id, PARENT_TEST_TRUSTED_PORT, &found);
196     test_assert (res == MESSAGEPORT_ERROR_NONE, "Fail to find trusted remote port '%s' at app_id '%s', error: %d", PARENT_TEST_TRUSTED_PORT, remote_app_id, res);
197
198     return TRUE;
199 }
200
201 static gboolean
202 test_send_message()
203 {
204     messageport_error_e res;
205     const gchar remote_app_id[128];
206     bundle *b = bundle_create ();
207     bundle_add (b, "Name", "Amarnath");
208     bundle_add (b, "Email", "amarnath.valluri@intel.com");
209
210     g_sprintf (remote_app_id, "%d", getppid());
211     res = messageport_send_message (remote_app_id, PARENT_TEST_PORT, b);
212     bundle_free (b);
213     test_assert (res == MESSAGEPORT_ERROR_NONE, "Fail to send message to port '%s' at app_id : '%s', error : %d", PARENT_TEST_PORT, remote_app_id, res);
214
215     gchar result[32];
216
217     test_assert ((read (__pipe[0], &result, sizeof(result)) > 0), "Parent did not received the message");
218     test_assert ((g_strcmp0 (result, "OK") == 0), "Parent did not received the message");
219
220     return TRUE;
221 }
222
223 static gboolean
224 test_send_trusted_message()
225 {
226     messageport_error_e res;
227     const gchar remote_app_id[128];
228     bundle *b = bundle_create ();
229     bundle_add (b, "Name", "Amarnath");
230     bundle_add (b, "Email", "amarnath.valluri@intel.com");
231
232     g_sprintf (remote_app_id, "%d", getppid());
233     res = messageport_send_trusted_message (remote_app_id, PARENT_TEST_TRUSTED_PORT, b);
234     bundle_free (b);
235     test_assert (res == MESSAGEPORT_ERROR_NONE, "Fail to send message to port '%s' at app_id : '%s', error : %d", PARENT_TEST_PORT, remote_app_id, res);
236
237     gchar result[32];
238
239     test_assert( (read (__pipe[0], &result, sizeof(result)) > 0), "Parent did not received the message");
240     test_assert( (g_strcmp0 (result, "OK") == 0), "Parent did not received the message");
241
242     return TRUE;
243 }
244
245 static gboolean
246 _update_test_result (gpointer data)
247 {
248     if (__test_data) {
249         __test_data->result = FALSE;
250         g_main_loop_quit (__test_data->m_loop);
251     }
252
253     return FALSE;
254 }
255
256 static gboolean
257 test_send_bidirectional_message()
258 {
259     messageport_error_e res;
260     int local_port_id = 0;
261     const gchar remote_app_id[128];
262     gchar result[32];
263     gboolean child_got_message = FALSE;
264     bundle *b = bundle_create ();
265     bundle_add (b, "Name", "Amarnath");
266     bundle_add (b, "Email", "amarnath.valluri@intel.com");
267
268     child_got_message = FALSE;
269
270     /* register local message port for return message */
271     test_assert ((local_port_id = _register_test_port (CHILD_TEST_PORT, FALSE, _on_child_got_message)) > 0,
272         "Fail to register message port");
273
274     g_sprintf (remote_app_id, "%d", getppid());
275     res = messageport_send_bidirectional_message (local_port_id, remote_app_id, PARENT_TEST_PORT, b);
276     bundle_free (b);
277     test_assert (res == MESSAGEPORT_ERROR_NONE,
278         "Fail to send message to port '%s' at app_id : '%s', error : %d", PARENT_TEST_PORT, remote_app_id, res);
279
280
281     test_assert( (read (__pipe[0], &result, sizeof(result)) > 0), "Parent did not received the message");
282     test_assert( (g_strcmp0 (result, "OK") == 0), "Parent did not received the message");
283
284     __test_data = g_new0 (struct AsyncTestData, 1);
285     __test_data->m_loop = g_main_loop_new (NULL, FALSE);
286     g_timeout_add_seconds (5, _update_test_result, NULL);
287
288     g_main_loop_run (__test_data->m_loop);
289     child_got_message = __test_data->result;
290
291     g_main_loop_unref (__test_data->m_loop);
292     g_free (__test_data);
293     __test_data = NULL;
294
295     test_assert (child_got_message == TRUE, "Child did not recieved reply");
296
297     return TRUE;
298 }
299
300 static gboolean
301 test_send_bidirectional_trusted_message()
302 {
303     messageport_error_e res;
304     int local_port_id = 0;
305     const gchar remote_app_id[128];
306     gchar result[32];
307     gboolean child_got_message = FALSE;
308     bundle *b = bundle_create ();
309     bundle_add (b, "Name", "Amarnath");
310     bundle_add (b, "Email", "amarnath.valluri@intel.com");
311
312     child_got_message = FALSE;
313
314     /* register local message port for return message */
315     test_assert ((local_port_id = _register_test_port (CHILD_TEST_TRUSTED_PORT, FALSE, _on_child_got_message)) > 0,
316         "Fail to register message port");
317
318     g_sprintf (remote_app_id, "%d", getppid());
319     res = messageport_send_bidirectional_trusted_message (local_port_id, remote_app_id, PARENT_TEST_TRUSTED_PORT, b);
320     bundle_free (b);
321     test_assert (res == MESSAGEPORT_ERROR_NONE,
322         "Fail to send message to port '%s' at app_id : '%s', error : %d", PARENT_TEST_TRUSTED_PORT, remote_app_id, res);
323
324
325     test_assert( (read (__pipe[0], &result, sizeof(result)) > 0), "Parent did not received the message");
326     test_assert( (g_strcmp0 (result, "OK") == 0), "Parent did not received the message");
327
328     __test_data = g_new0 (struct AsyncTestData, 1);
329     __test_data->m_loop = g_main_loop_new (NULL, FALSE);
330     g_timeout_add_seconds (5, _update_test_result, NULL);
331
332     g_main_loop_run (__test_data->m_loop);
333     child_got_message = __test_data->result;
334
335     g_main_loop_unref (__test_data->m_loop);
336     g_free (__test_data);
337     __test_data = NULL;
338
339     test_assert (child_got_message == TRUE, "Child did not recieved reply");
340
341     return TRUE;
342 }
343
344 static gboolean
345 test_get_local_port_name()
346 {
347     int local_port_id = 0;
348     messageport_error_e res;
349     gchar *port_name = NULL;
350
351     test_assert ((local_port_id = _register_test_port (CHILD_TEST_TRUSTED_PORT, FALSE, _on_child_got_message)) > 0,
352         "Fail to register test port : error : %d", local_port_id);
353     
354     res = messageport_get_local_port_name (local_port_id, &port_name);
355     test_assert (res == MESSAGEPORT_ERROR_NONE, "Failed to get message port name, error: %d", res);
356
357     test_assert (g_strcmp0 (port_name, CHILD_TEST_TRUSTED_PORT) == 0, "Got wrong port name");
358
359     return TRUE;
360 }
361
362 static gboolean
363 test_check_trusted_local_port ()
364 {
365     int local_port_id = 0;
366     messageport_error_e res;
367     gboolean is_trusted;
368
369     test_assert ((local_port_id = _register_test_port (CHILD_TEST_PORT, FALSE, _on_child_got_message)) > 0,
370         "Fail to register test port : error : %d", local_port_id);
371     
372     res = messageport_check_trusted_local_port (local_port_id, &is_trusted);
373     test_assert (res == MESSAGEPORT_ERROR_NONE, "Failed to get message port name, error: %d", res);
374     test_assert (is_trusted == FALSE, "Go FLSE for trusted port");
375
376     test_assert ((local_port_id = _register_test_port (CHILD_TEST_TRUSTED_PORT, TRUE, _on_child_got_message)) > 0,
377         "Fail to register test port : error : %d", local_port_id);
378     
379     res = messageport_check_trusted_local_port (local_port_id, &is_trusted);
380     test_assert (res == MESSAGEPORT_ERROR_NONE, "Failed to get message port name, error: %d", res);
381     test_assert (is_trusted == TRUE, "Go TRUE for untrusted port");
382
383     return TRUE;
384 }
385
386
387 static gboolean
388 _on_term (gpointer userdata)
389 {
390     g_main_loop_quit ((GMainLoop *)userdata);
391
392     return FALSE;
393 }
394
395 int main (int argc, char *argv[])
396 {
397     pid_t child_pid;
398
399     if (pipe (__pipe)) {
400         g_warning ("Failed to open pipe");
401         return -1;
402     }
403
404     child_pid = fork ();
405     
406     if (child_pid < 0) {
407         g_error ("Failed to fork ");
408     }
409     else if (child_pid > 0)  {
410         /* parent process */
411         GMainLoop *m_loop = g_main_loop_new (NULL, FALSE);
412         /* server ports */
413         TEST_CASE(test_register_local_port);
414         TEST_CASE(test_register_trusted_local_port);
415
416         g_unix_signal_add (SIGTERM, _on_term, m_loop);
417
418         g_main_loop_run (m_loop);
419         g_main_loop_unref (m_loop);
420     }
421     else {
422         /* child process */
423         /* sleep sometime till server ports are ready */
424         sleep (3);
425
426         TEST_CASE(test_register_trusted_local_port);
427         TEST_CASE(test_check_remote_port);
428         TEST_CASE(test_check_trusted_remote_port);
429         TEST_CASE(test_send_message);
430         TEST_CASE(test_send_trusted_message);
431         TEST_CASE(test_send_bidirectional_message);
432         TEST_CASE(test_send_bidirectional_trusted_message);
433         TEST_CASE(test_get_local_port_name);
434         TEST_CASE(test_check_trusted_local_port);
435
436         /* end of tests */
437         kill(getppid(), SIGTERM);
438
439     }
440
441     return 0;
442 }
443