1 /* vi: set et sw=4 ts=4 cino=t0,(0: */
2 /* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
4 * Copyright (C) 2013 Intel Corporation.
6 * Contact: Amarnath Valluri <amarnath.valluri@linux.intel.com>
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.
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.
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
28 #include <message-port.h>
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";
42 } *__test_data = NULL;
44 #define TEST_CASE(case) \
46 if (case() != TRUE) { \
47 g_printerr ("%s: FAIL\n", #case); \
50 else g_print ("%s: SUCCESS\n", #case); \
54 #define test_assert(expr, msg...) \
56 if ((expr) == FALSE) {\
57 g_print ("%s +%d: assert(%s):%s\n", __FUNCTION__, __LINE__, #expr, ##msg); \
63 static void _dump_data (const char *key, const int type, const bundle_keyval_t *kv, void *user_data)
67 bundle_keyval_get_basic_val ((bundle_keyval_t*)kv, (void**)&val, &size);
68 g_debug (" %s - %s", key, val);
71 void (_on_child_got_message)(int port_id, const char* remote_app_id, const char* remote_port, gboolean trusted_message, bundle* data)
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");
80 bundle_foreach (data, _dump_data, NULL);
82 /* Write acknoledgement */
83 if (write (__pipe[1], "OK", strlen("OK") + 1) < 3) {
84 g_warning ("WRITE failed");
88 __test_data->result = TRUE;
89 g_main_loop_quit (__test_data->m_loop);
93 void (_on_parent_got_message)(int port_id, const char* remote_app_id, const char* remote_port, gboolean trusted_message, bundle* data)
96 gboolean found = FALSE;
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");
105 bundle_foreach (data, _dump_data, NULL);
107 /* Write acknoledgement */
108 if ( write (__pipe[1], "OK", strlen("OK") + 1) < 3) {
109 g_warning ("WRITE failed");
112 /* check message is coming from remote port to send back to message,
114 if (!remote_app_id || !remote_port) {
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);
121 g_warning ("PARENT: Could not found remote port (%d)", res);
125 g_debug ("PARENT: Found remote prot");
127 bundle *reply = bundle_create ();
129 bundle_add (reply, "Results", "GOT_IT");
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);
135 if (res != MESSAGEPORT_ERROR_NONE)
137 g_warning ("PARENT: Faile to send message to server : %d", res);
139 else g_debug ("PARENT: Data sent successfully");
142 int _register_test_port (const gchar *port_name, gboolean is_trusted, messageport_message_cb cb)
144 int port_id = is_trusted ? messageport_register_trusted_local_port (port_name, cb)
145 : messageport_register_local_port (port_name, cb);
151 test_register_local_port ()
153 int port_id = _register_test_port (PARENT_TEST_PORT, FALSE, _on_parent_got_message);
155 test_assert (port_id >= 0, "Failed to register port '%s', error : %d", PARENT_TEST_PORT, port_id);
161 test_register_trusted_local_port()
163 int port_id = _register_test_port (PARENT_TEST_TRUSTED_PORT, TRUE, _on_parent_got_message);
165 test_assert (port_id >= 0, "Failed to register port '%s', error : %d", PARENT_TEST_TRUSTED_PORT, port_id);
171 test_check_remote_port()
173 const gchar remote_app_id[128];
174 gboolean found = FALSE;
175 messageport_error_e res;
177 g_sprintf (remote_app_id, "%d", getppid());
179 res = messageport_check_remote_port (remote_app_id, PARENT_TEST_PORT, &found);
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);
187 test_check_trusted_remote_port()
189 const gchar remote_app_id[128];
190 gboolean found = FALSE;
191 messageport_error_e res;
193 g_sprintf (remote_app_id, "%d", getppid());
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);
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");
210 g_sprintf (remote_app_id, "%d", getppid());
211 res = messageport_send_message (remote_app_id, PARENT_TEST_PORT, 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);
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");
224 test_send_trusted_message()
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");
232 g_sprintf (remote_app_id, "%d", getppid());
233 res = messageport_send_trusted_message (remote_app_id, PARENT_TEST_TRUSTED_PORT, 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);
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");
246 _update_test_result (gpointer data)
249 __test_data->result = FALSE;
250 g_main_loop_quit (__test_data->m_loop);
257 test_send_bidirectional_message()
259 messageport_error_e res;
260 int local_port_id = 0;
261 const gchar remote_app_id[128];
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");
268 child_got_message = FALSE;
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");
274 g_sprintf (remote_app_id, "%d", getppid());
275 res = messageport_send_bidirectional_message (local_port_id, remote_app_id, PARENT_TEST_PORT, 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);
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");
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);
288 g_main_loop_run (__test_data->m_loop);
289 child_got_message = __test_data->result;
291 g_main_loop_unref (__test_data->m_loop);
292 g_free (__test_data);
295 test_assert (child_got_message == TRUE, "Child did not recieved reply");
301 test_send_bidirectional_trusted_message()
303 messageport_error_e res;
304 int local_port_id = 0;
305 const gchar remote_app_id[128];
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");
312 child_got_message = FALSE;
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");
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);
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);
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");
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);
332 g_main_loop_run (__test_data->m_loop);
333 child_got_message = __test_data->result;
335 g_main_loop_unref (__test_data->m_loop);
336 g_free (__test_data);
339 test_assert (child_got_message == TRUE, "Child did not recieved reply");
345 test_get_local_port_name()
347 int local_port_id = 0;
348 messageport_error_e res;
349 gchar *port_name = NULL;
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);
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);
357 test_assert (g_strcmp0 (port_name, CHILD_TEST_TRUSTED_PORT) == 0, "Got wrong port name");
363 test_check_trusted_local_port ()
365 int local_port_id = 0;
366 messageport_error_e res;
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);
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");
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);
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");
388 _on_term (gpointer userdata)
390 g_main_loop_quit ((GMainLoop *)userdata);
395 int main (int argc, char *argv[])
400 g_warning ("Failed to open pipe");
407 g_error ("Failed to fork ");
409 else if (child_pid > 0) {
411 GMainLoop *m_loop = g_main_loop_new (NULL, FALSE);
413 TEST_CASE(test_register_local_port);
414 TEST_CASE(test_register_trusted_local_port);
416 g_unix_signal_add (SIGTERM, _on_term, m_loop);
418 g_main_loop_run (m_loop);
419 g_main_loop_unref (m_loop);
423 /* sleep sometime till server ports are ready */
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);
437 kill(getppid(), SIGTERM);