1 /* vi: set et sw=4 ts=4 cino=t0,(0: */
2 /* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
4 * This file is part of message-port.
6 * Copyright (C) 2013 Intel Corporation.
8 * Contact: Amarnath Valluri <amarnath.valluri@linux.intel.com>
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
31 #include <message-port.h>
35 int __pipe[2]; /* pipe between two process */
36 pid_t __daemon_pid; /* dbus daemon pid */
38 const gchar *PARENT_TEST_PORT = "parent_test_port";
39 const gchar *PARENT_TEST_TRUSTED_PORT = "parent_test_trusted_port";
40 const gchar *CHILD_TEST_PORT = "child_test_port";
41 const gchar *CHILD_TEST_TRUSTED_PORT = "child_test_trusted_port";
47 } *__test_data = NULL;
49 #define TEST_CASE(case) \
51 if (case() != TRUE) { \
52 g_printerr ("%s: FAIL\n", #case); \
55 else g_print ("%s: SUCCESS\n", #case); \
59 #define test_assert(expr, msg...) \
61 if ((expr) == FALSE) {\
62 g_print ("%s +%d: assert(%s):%s\n", __FUNCTION__, __LINE__, #expr, ##msg); \
68 static void _dump_data (const char *key, const int type, const bundle_keyval_t *kv, void *user_data)
72 bundle_keyval_get_basic_val ((bundle_keyval_t*)kv, (void**)&val, &size);
73 g_debug (" %s - %s", key, val);
76 void (_on_child_got_message)(int port_id, const char* remote_app_id, const char* remote_port, gboolean trusted_message, bundle* data)
79 messageport_get_local_port_name (port_id, &name),
80 g_debug ("CHILD: GOT MESSAGE at prot '%s' FROM :'%s' - '%s", name,
81 remote_app_id ? remote_app_id : "unknwon app", remote_port ? remote_port : "unknwon");
85 bundle_foreach (data, _dump_data, NULL);
87 /* Write acknoledgement */
88 if (write (__pipe[1], "OK", strlen("OK") + 1) < 3) {
89 g_warning ("WRITE failed");
93 __test_data->result = TRUE;
94 g_main_loop_quit (__test_data->m_loop);
98 void (_on_parent_got_message)(int port_id, const char* remote_app_id, const char* remote_port, gboolean trusted_message, bundle* data)
101 gboolean found = FALSE;
103 messageport_get_local_port_name (port_id, &name),
104 g_debug ("PARENT: GOT MESSAGE at prot %s FROM :'%s' app - '%s' port", name,
105 remote_app_id ? remote_app_id : "unknwon", remote_port ? remote_port : "unknwon");
110 bundle_foreach (data, _dump_data, NULL);
112 /* Write acknoledgement */
113 if ( write (__pipe[1], "OK", strlen("OK") + 1) < 3) {
114 g_warning ("WRITE failed");
117 /* check message is coming from remote port to send back to message,
119 if (!remote_app_id || !remote_port) {
123 messageport_error_e res = trusted_message ? messageport_check_trusted_remote_port (remote_app_id, remote_port, &found)
124 : messageport_check_remote_port (remote_app_id, remote_port, &found);
126 g_warning ("PARENT: Could not found remote port (%d)", res);
130 g_debug ("PARENT: Found remote prot");
132 bundle *reply = bundle_create ();
134 bundle_add (reply, "Results", "GOT_IT");
136 g_debug ("PARENT: Sending reply ....");
137 res = trusted_message ? messageport_send_trusted_message (remote_app_id, remote_port, reply)
138 : messageport_send_message (remote_app_id, remote_port, reply);
140 if (res != MESSAGEPORT_ERROR_NONE)
142 g_warning ("PARENT: Faile to send message to server : %d", res);
144 else g_debug ("PARENT: Data sent successfully");
147 int _register_test_port (const gchar *port_name, gboolean is_trusted, messageport_message_cb cb)
149 int port_id = is_trusted ? messageport_register_trusted_local_port (port_name, cb)
150 : messageport_register_local_port (port_name, cb);
156 test_register_local_port ()
158 int port_id = _register_test_port (PARENT_TEST_PORT, FALSE, _on_parent_got_message);
160 test_assert (port_id >= 0, "Failed to register port '%s', error : %d", PARENT_TEST_PORT, port_id);
166 test_register_trusted_local_port()
168 int port_id = _register_test_port (PARENT_TEST_TRUSTED_PORT, TRUE, _on_parent_got_message);
170 test_assert (port_id >= 0, "Failed to register port '%s', error : %d", PARENT_TEST_TRUSTED_PORT, port_id);
176 test_check_remote_port()
178 const gchar remote_app_id[128];
179 gboolean found = FALSE;
180 messageport_error_e res;
182 g_sprintf (remote_app_id, "%d", getppid());
184 res = messageport_check_remote_port (remote_app_id, PARENT_TEST_PORT, &found);
186 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);
192 test_check_trusted_remote_port()
194 const gchar remote_app_id[128];
195 gboolean found = FALSE;
196 messageport_error_e res;
198 g_sprintf (remote_app_id, "%d", getppid());
200 res = messageport_check_trusted_remote_port (remote_app_id, PARENT_TEST_TRUSTED_PORT, &found);
201 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);
209 messageport_error_e res;
210 const gchar remote_app_id[128];
211 bundle *b = bundle_create ();
212 bundle_add (b, "Name", "Amarnath");
213 bundle_add (b, "Email", "amarnath.valluri@intel.com");
215 g_sprintf (remote_app_id, "%d", getppid());
216 res = messageport_send_message (remote_app_id, PARENT_TEST_PORT, b);
218 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);
222 test_assert ((read (__pipe[0], &result, sizeof(result)) > 0), "Parent did not received the message");
223 test_assert ((g_strcmp0 (result, "OK") == 0), "Parent did not received the message");
229 test_send_trusted_message()
231 messageport_error_e res;
232 const gchar remote_app_id[128];
233 bundle *b = bundle_create ();
234 bundle_add (b, "Name", "Amarnath");
235 bundle_add (b, "Email", "amarnath.valluri@intel.com");
237 g_sprintf (remote_app_id, "%d", getppid());
238 res = messageport_send_trusted_message (remote_app_id, PARENT_TEST_TRUSTED_PORT, b);
240 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);
244 test_assert( (read (__pipe[0], &result, sizeof(result)) > 0), "Parent did not received the message");
245 test_assert( (g_strcmp0 (result, "OK") == 0), "Parent did not received the message");
251 _update_test_result (gpointer data)
254 __test_data->result = FALSE;
255 g_main_loop_quit (__test_data->m_loop);
262 test_send_bidirectional_message()
264 messageport_error_e res;
265 int local_port_id = 0;
266 const gchar remote_app_id[128];
268 gboolean child_got_message = FALSE;
269 bundle *b = bundle_create ();
270 bundle_add (b, "Name", "Amarnath");
271 bundle_add (b, "Email", "amarnath.valluri@intel.com");
273 child_got_message = FALSE;
275 /* register local message port for return message */
276 test_assert ((local_port_id = _register_test_port (CHILD_TEST_PORT, FALSE, _on_child_got_message)) > 0,
277 "Fail to register message port");
279 g_sprintf (remote_app_id, "%d", getppid());
280 res = messageport_send_bidirectional_message (local_port_id, remote_app_id, PARENT_TEST_PORT, b);
282 test_assert (res == MESSAGEPORT_ERROR_NONE,
283 "Fail to send message to port '%s' at app_id : '%s', error : %d", PARENT_TEST_PORT, remote_app_id, res);
286 test_assert( (read (__pipe[0], &result, sizeof(result)) > 0), "Parent did not received the message");
287 test_assert( (g_strcmp0 (result, "OK") == 0), "Parent did not received the message");
289 __test_data = g_new0 (struct AsyncTestData, 1);
290 __test_data->m_loop = g_main_loop_new (NULL, FALSE);
291 g_timeout_add_seconds (5, _update_test_result, NULL);
293 g_main_loop_run (__test_data->m_loop);
294 child_got_message = __test_data->result;
296 g_main_loop_unref (__test_data->m_loop);
297 g_free (__test_data);
300 test_assert (child_got_message == TRUE, "Child did not recieved reply");
306 test_send_bidirectional_trusted_message()
308 messageport_error_e res;
309 int local_port_id = 0;
310 const gchar remote_app_id[128];
312 gboolean child_got_message = FALSE;
313 bundle *b = bundle_create ();
314 bundle_add (b, "Name", "Amarnath");
315 bundle_add (b, "Email", "amarnath.valluri@intel.com");
317 child_got_message = FALSE;
319 /* register local message port for return message */
320 test_assert ((local_port_id = _register_test_port (CHILD_TEST_TRUSTED_PORT, FALSE, _on_child_got_message)) > 0,
321 "Fail to register message port");
323 g_sprintf (remote_app_id, "%d", getppid());
324 res = messageport_send_bidirectional_trusted_message (local_port_id, remote_app_id, PARENT_TEST_TRUSTED_PORT, b);
326 test_assert (res == MESSAGEPORT_ERROR_NONE,
327 "Fail to send message to port '%s' at app_id : '%s', error : %d", PARENT_TEST_TRUSTED_PORT, remote_app_id, res);
330 test_assert( (read (__pipe[0], &result, sizeof(result)) > 0), "Parent did not received the message");
331 test_assert( (g_strcmp0 (result, "OK") == 0), "Parent did not received the message");
333 __test_data = g_new0 (struct AsyncTestData, 1);
334 __test_data->m_loop = g_main_loop_new (NULL, FALSE);
335 g_timeout_add_seconds (5, _update_test_result, NULL);
337 g_main_loop_run (__test_data->m_loop);
338 child_got_message = __test_data->result;
340 g_main_loop_unref (__test_data->m_loop);
341 g_free (__test_data);
344 test_assert (child_got_message == TRUE, "Child did not recieved reply");
350 test_get_local_port_name()
352 int local_port_id = 0;
353 messageport_error_e res;
354 gchar *port_name = NULL;
356 test_assert ((local_port_id = _register_test_port (CHILD_TEST_TRUSTED_PORT, FALSE, _on_child_got_message)) > 0,
357 "Fail to register test port : error : %d", local_port_id);
359 res = messageport_get_local_port_name (local_port_id, &port_name);
360 test_assert (res == MESSAGEPORT_ERROR_NONE, "Failed to get message port name, error: %d", res);
362 test_assert (g_strcmp0 (port_name, CHILD_TEST_TRUSTED_PORT) == 0, "Got wrong port name");
368 test_check_trusted_local_port ()
370 int local_port_id = 0;
371 messageport_error_e res;
374 test_assert ((local_port_id = _register_test_port (CHILD_TEST_PORT, FALSE, _on_child_got_message)) > 0,
375 "Fail to register test port : error : %d", local_port_id);
377 res = messageport_check_trusted_local_port (local_port_id, &is_trusted);
378 test_assert (res == MESSAGEPORT_ERROR_NONE, "Failed to get message port name, error: %d", res);
379 test_assert (is_trusted == FALSE, "Go FLSE for trusted port");
381 test_assert ((local_port_id = _register_test_port (CHILD_TEST_TRUSTED_PORT, TRUE, _on_child_got_message)) > 0,
382 "Fail to register test port : error : %d", local_port_id);
384 res = messageport_check_trusted_local_port (local_port_id, &is_trusted);
385 test_assert (res == MESSAGEPORT_ERROR_NONE, "Failed to get message port name, error: %d", res);
386 test_assert (is_trusted == TRUE, "Go TRUE for untrusted port");
393 _on_term (gpointer userdata)
395 g_main_loop_quit ((GMainLoop *)userdata);
403 #ifdef USE_SESSION_BUS
404 GIOChannel *channel = NULL;
405 gchar *bus_address = NULL;
410 const gchar *dbus_monitor = NULL;
411 GError *error = NULL;
413 argv[0] = "dbus-daemon";
414 argv[1] = "--print-address=<<fd>>";
415 argv[2] = "--config-file="TEST_DBUS_DAEMON_CONF_FILE;
418 if (pipe(pipe_fd)== -1) {
419 GSpawnFlags flags = G_SPAWN_SEARCH_PATH;
420 argv[1] = g_strdup_printf ("--print-address=1");
421 g_spawn_async_with_pipes (NULL, argv, NULL, flags, NULL, NULL, &__daemon_pid, NULL, NULL, &tmp_fd, &error);
423 GSpawnFlags flags = (GSpawnFlags)(G_SPAWN_SEARCH_PATH | G_SPAWN_LEAVE_DESCRIPTORS_OPEN);
425 argv[1] = g_strdup_printf ("--print-address=%d", pipe_fd[1]);
426 g_spawn_async (NULL, argv, NULL, flags, NULL, NULL, &__daemon_pid, &error);
429 test_assert (error == NULL, "Failed to span daemon : %s", error->message);
430 test_assert (__daemon_pid != 0, "Failed to get daemon pid");
431 sleep (5); /* 5 seconds */
433 channel = g_io_channel_unix_new (tmp_fd);
434 g_io_channel_read_line (channel, &bus_address, NULL, &len, &error);
435 test_assert (error == NULL, "Failed to daemon address : %s", error->message);
436 g_io_channel_unref (channel);
438 if (pipe_fd[0]) close (pipe_fd[0]);
439 if (pipe_fd[1]) close (pipe_fd[1]);
441 if (bus_address) bus_address[len] = '\0';
442 test_assert (bus_address != NULL && bus_address[0] != 0, "Failed to get dbus-daemon address");
444 setenv("DBUS_SESSION_BUS_ADDRESS", bus_address, TRUE);
446 g_print ("Dbus daemon start at : %s\n", bus_address);
448 g_free (bus_address);
449 #endif /* USE_SESSION_BUS */
457 #ifdef USE_SESSION_BUS
458 if (__daemon_pid) kill (__daemon_pid, SIGTERM);
459 #endif /* USE_SESSION_BUS */
463 int main (int argc, char *argv[])
468 g_error ("Could not start session bus!!! \n");
473 g_warning ("Failed to open pipe");
480 g_error ("Failed to fork ");
482 else if (child_pid > 0) {
484 GMainLoop *m_loop = g_main_loop_new (NULL, FALSE);
486 TEST_CASE(test_register_local_port);
487 TEST_CASE(test_register_trusted_local_port);
488 TEST_CASE(test_get_local_port_name);
489 TEST_CASE(test_check_trusted_local_port);
491 g_unix_signal_add (SIGTERM, _on_term, m_loop);
493 g_main_loop_run (m_loop);
494 g_main_loop_unref (m_loop);
498 /* sleep sometime till server ports are ready */
501 TEST_CASE(test_check_remote_port);
502 TEST_CASE(test_check_trusted_remote_port);
503 TEST_CASE(test_send_message);
504 TEST_CASE(test_send_bidirectional_message);
505 TEST_CASE(test_send_trusted_message);
506 TEST_CASE(test_send_bidirectional_trusted_message);
509 kill(getppid(), SIGTERM);