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 gsignond
6 * Copyright (C) 2012 Intel Corporation.
8 * Contact: Alexander Kanavin <alex.kanavin@gmail.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,
16 * but 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
30 #include "daemon/gsignond-types.h"
31 #include "gsignond-plugin-remote-private.h"
32 #include "gsignond-plugin-remote.h"
33 #include "gplugind/gsignond-plugin-daemon.h"
34 #include <gsignond/gsignond-plugin-interface.h>
35 #include <gsignond/gsignond-error.h>
36 #include <gsignond/gsignond-config.h>
37 #include <gsignond/gsignond-log.h>
39 static GMainLoop *main_loop = NULL;
40 guint child_watch_id = 0;
42 struct _GSignondAuthSession
47 struct _GSignondAuthSessionClass
49 GObjectClass parent_class;
52 G_DEFINE_TYPE (GSignondAuthSession, gsignond_auth_session, G_TYPE_OBJECT);
58 g_main_loop_quit (main_loop);
65 g_main_loop_run (main_loop);
71 #if !GLIB_CHECK_VERSION (2, 36, 0)
75 if (main_loop == NULL) {
76 main_loop = g_main_loop_new (NULL, FALSE);
85 g_main_loop_unref (main_loop);
91 _timeout_quit_loop (gpointer data)
98 _run_loop_with_timeout (guint timeout)
100 g_timeout_add_seconds (timeout, _timeout_quit_loop, NULL);
105 gsignond_auth_session_init (
106 GSignondAuthSession *self)
111 gsignond_auth_session_class_init (
112 GSignondAuthSessionClass *klass)
117 gsignond_auth_session_notify_process_result (
118 GSignondAuthSession *iface,
119 GSignondSessionData *result,
125 gsignond_auth_session_notify_process_error (
126 GSignondAuthSession *iface,
133 gsignond_auth_session_notify_store (
134 GSignondAuthSession *self,
135 GSignondSessionData *session_data)
140 gsignond_auth_session_notify_user_action_required (
141 GSignondAuthSession *self,
142 GSignondSignonuiData *session_data)
147 gsignond_auth_session_notify_refreshed (
148 GSignondAuthSession *self,
149 GSignondSignonuiData *session_data)
154 gsignond_auth_session_notify_state_changed (
155 GSignondAuthSession *self,
157 const gchar *message,
164 GSignondPlugin* plugin)
169 fail_if(plugin == NULL);
171 g_object_get(plugin, "type", &type, "mechanisms", &mechanisms, NULL);
173 fail_unless(g_strcmp0(type, "password") == 0);
174 fail_unless(g_strcmp0(mechanisms[0], "password") == 0);
175 fail_unless(mechanisms[1] == NULL);
178 g_strfreev(mechanisms);
183 GSignondPlugin* plugin,
184 GSignondSessionData* result,
188 GSignondSessionData** user_data_p = user_data;
189 *user_data_p = gsignond_dictionary_copy(result);
194 user_action_required_callback(
195 GSignondPlugin* plugin,
196 GSignondSignonuiData* ui_request,
200 GSignondSignonuiData** user_data_p = user_data;
201 *user_data_p = gsignond_dictionary_copy(ui_request);
207 GSignondPlugin* plugin,
212 GError** user_data_p = user_data;
213 *user_data_p = g_error_copy(error);
217 START_TEST (test_pluginremote_create)
220 GSignondPlugin *plugin = NULL;
221 const gchar *plugin_type = "password";
223 GSignondConfig* config = gsignond_config_new ();
224 fail_if (config == NULL);
226 gchar* loader_path = g_build_filename (g_getenv("SSO_BIN_DIR"), "gsignond-plugind", NULL);
227 plugin = GSIGNOND_PLUGIN (gsignond_plugin_remote_new(loader_path, plugin_type));
229 g_object_unref (config);
231 check_plugin (plugin);
232 g_object_unref (plugin);
236 START_TEST (test_pluginremote_plugind_create)
239 GSignondPlugin *plugin = NULL;
240 const gchar *plugin_type = "password";
241 GSignondPluginRemotePrivate* priv = NULL;
243 GSignondConfig* config = gsignond_config_new ();
244 fail_if (config == NULL);
246 gchar* loader_path = g_build_filename (g_getenv("SSO_BIN_DIR"), "gsignond-plugind", NULL);
247 plugin = GSIGNOND_PLUGIN (gsignond_plugin_remote_new(loader_path, plugin_type));
249 g_object_unref (config);
251 fail_if (plugin == NULL);
252 priv = (GSignondPluginRemotePrivate *)GSIGNOND_PLUGIN_REMOTE (plugin)->priv;
254 fail_unless (priv->cpid > 0);
255 fail_unless (kill (priv->cpid, 0) == 0);
257 check_plugin (plugin);
259 g_object_unref (plugin);
263 START_TEST (test_pluginremote_plugind_unref)
266 GSignondPlugin *plugin = NULL;
267 const gchar *plugin_type = "password";
268 GSignondPluginRemotePrivate* priv = NULL;
271 GSignondConfig* config = gsignond_config_new ();
272 fail_if (config == NULL);
274 gchar* loader_path = g_build_filename (g_getenv("SSO_BIN_DIR"), "gsignond-plugind", NULL);
275 plugin = GSIGNOND_PLUGIN (gsignond_plugin_remote_new(loader_path, plugin_type));
277 fail_if (plugin == NULL);
278 priv = (GSignondPluginRemotePrivate *)GSIGNOND_PLUGIN_REMOTE (plugin)->priv;
280 fail_unless (priv->cpid > 0);
281 fail_unless (kill (priv->cpid, 0) == 0);
284 g_object_unref (plugin);
285 g_object_unref (config);
287 fail_unless (kill (cpid, 0) != 0);
291 START_TEST (test_pluginremote_plugind_kill)
294 GSignondPlugin *plugin = NULL;
295 const gchar *plugin_type = "password";
296 GSignondPluginRemotePrivate* priv = NULL;
299 GSignondConfig* config = gsignond_config_new ();
300 fail_if (config == NULL);
302 gchar* loader_path = g_build_filename (g_getenv("SSO_BIN_DIR"), "gsignond-plugind", NULL);
303 plugin = GSIGNOND_PLUGIN (gsignond_plugin_remote_new(loader_path, plugin_type));
305 fail_if (plugin == NULL);
306 priv = (GSignondPluginRemotePrivate *)GSIGNOND_PLUGIN_REMOTE (plugin)->priv;
308 fail_unless (priv->cpid > 0);
309 fail_unless (kill (priv->cpid, 0) == 0);
311 check_plugin (plugin);
313 kill (priv->cpid, SIGTERM);
315 _run_loop_with_timeout (1);
317 fail_unless (kill (cpid, 0) != 0);
319 g_object_unref (plugin);
320 g_object_unref (config);
324 START_TEST (test_pluginremote_request)
327 GSignondPlugin *plugin = NULL;
328 const gchar *plugin_type = "password";
330 GSignondConfig* config = gsignond_config_new ();
331 fail_if(config == NULL);
333 gchar* loader_path = g_build_filename (g_getenv("SSO_BIN_DIR"), "gsignond-plugind", NULL);
334 plugin = GSIGNOND_PLUGIN (gsignond_plugin_remote_new(loader_path, plugin_type));
336 fail_if(plugin == NULL);
338 GSignondSessionData* result = NULL;
339 GSignondSignonuiData* ui_action = NULL;
340 GError* error = NULL;
343 g_signal_connect(plugin, "response-final", G_CALLBACK(response_callback),
345 g_signal_connect(plugin, "user-action-required",
346 G_CALLBACK(user_action_required_callback), &ui_action);
347 g_signal_connect(plugin, "error", G_CALLBACK(error_callback), &error);
349 GSignondSessionData* data = gsignond_dictionary_new ();
351 // username empty, password not empty
352 gsignond_session_data_set_secret(data, "megapassword");
353 gsignond_plugin_request_initial(plugin, data, NULL, "password");
356 fail_if(result == NULL);
357 fail_if(ui_action != NULL);
358 fail_if(error != NULL);
359 fail_if(gsignond_session_data_get_username(result) != NULL);
361 gsignond_session_data_get_secret(result), "megapassword") != 0);
362 gsignond_dictionary_unref(result);
365 // username and password not empty
366 gsignond_session_data_set_username(data, "megauser");
367 gsignond_plugin_request_initial(plugin, data, NULL, "password");
370 fail_if(result == NULL);
371 fail_if(ui_action != NULL);
372 fail_if(error != NULL);
374 gsignond_session_data_get_username(result), "megauser") != 0);
376 gsignond_session_data_get_secret(result), "megapassword") != 0);
377 gsignond_dictionary_unref(result);
380 //username and password empty
381 gsignond_dictionary_unref(data);
382 data = gsignond_dictionary_new();
383 gsignond_plugin_request_initial(plugin, data, NULL, "password");
386 fail_if(result != NULL);
387 fail_if(ui_action == NULL);
388 fail_if(error != NULL);
389 fail_if(gsignond_signonui_data_get_query_username(ui_action, &bool_res)
391 fail_if(bool_res == FALSE);
392 fail_if(gsignond_signonui_data_get_query_password(ui_action, &bool_res)
394 fail_if(bool_res == FALSE);
395 gsignond_dictionary_unref(ui_action);
398 //username not empty, password empty
399 gsignond_session_data_set_username(data, "megauser");
400 gsignond_plugin_request_initial(plugin, data, NULL, "password");
403 fail_if(result != NULL);
404 fail_if(ui_action == NULL);
405 fail_if(error != NULL);
406 fail_if(gsignond_signonui_data_get_query_username(ui_action, &bool_res)
408 fail_if(bool_res == TRUE);
409 fail_if(gsignond_signonui_data_get_query_password(ui_action, &bool_res)
411 fail_if(bool_res == FALSE);
412 gsignond_dictionary_unref(ui_action);
415 gsignond_dictionary_unref(data);
417 g_object_unref(config);
418 g_object_unref(plugin);
422 START_TEST (test_pluginremote_user_action_finished)
425 GSignondPlugin *plugin = NULL;
426 const gchar *plugin_type = "password";
428 GSignondConfig* config = gsignond_config_new ();
429 fail_if(config == NULL);
431 gchar* loader_path = g_build_filename (g_getenv("SSO_BIN_DIR"), "gsignond-plugind", NULL);
432 plugin = GSIGNOND_PLUGIN (gsignond_plugin_remote_new(loader_path, plugin_type));
434 fail_if(plugin == NULL);
436 GSignondSessionData* result = NULL;
437 GSignondSignonuiData* ui_action = NULL;
438 GError* error = NULL;
440 g_signal_connect(plugin, "response-final", G_CALLBACK(response_callback),
442 g_signal_connect(plugin, "user-action-required",
443 G_CALLBACK(user_action_required_callback), &ui_action);
444 g_signal_connect(plugin, "error", G_CALLBACK(error_callback), &error);
446 GSignondSignonuiData* data = gsignond_dictionary_new();
449 gsignond_plugin_user_action_finished(plugin, data);
452 fail_if(result != NULL);
453 fail_if(ui_action != NULL);
454 fail_if(error == NULL);
455 fail_unless (error->code == GSIGNOND_ERROR_USER_INTERACTION);
460 gsignond_signonui_data_set_username(data, "megauser");
461 gsignond_signonui_data_set_password(data, "megapassword");
462 gsignond_signonui_data_set_query_error(data, SIGNONUI_ERROR_NONE);
463 gsignond_plugin_user_action_finished(plugin, data);
466 fail_if(result == NULL);
467 fail_if(ui_action != NULL);
468 fail_if(error != NULL);
470 gsignond_session_data_get_username(result), "megauser") != 0);
472 gsignond_session_data_get_secret(result), "megapassword") != 0);
473 gsignond_dictionary_unref(result);
477 gsignond_signonui_data_set_query_error(data, SIGNONUI_ERROR_CANCELED);
478 gsignond_plugin_user_action_finished(plugin, data);
481 fail_if(result != NULL);
482 fail_if(ui_action != NULL);
483 fail_if(error == NULL);
484 fail_unless (error->code == GSIGNOND_ERROR_SESSION_CANCELED);
488 // error in ui request
489 gsignond_signonui_data_set_query_error(data, SIGNONUI_ERROR_GENERAL);
490 gsignond_plugin_user_action_finished(plugin, data);
493 fail_if(result != NULL);
494 fail_if(ui_action != NULL);
495 fail_if(error == NULL);
496 fail_unless (error->code == GSIGNOND_ERROR_USER_INTERACTION);
500 gsignond_dictionary_unref(data);
502 g_object_unref(config);
503 g_object_unref(plugin);
507 START_TEST (test_pluginremote_refresh)
510 GSignondPlugin *plugin = NULL;
511 const gchar *plugin_type = "password";
513 GSignondConfig* config = gsignond_config_new ();
514 fail_if(config == NULL);
516 gchar* loader_path = g_build_filename (g_getenv("SSO_BIN_DIR"), "gsignond-plugind", NULL);
517 plugin = GSIGNOND_PLUGIN (gsignond_plugin_remote_new(loader_path, plugin_type));
519 fail_if(plugin == NULL);
521 GSignondSessionData* result = NULL;
522 GError* error = NULL;
524 g_signal_connect(plugin, "refreshed", G_CALLBACK(response_callback),
526 g_signal_connect(plugin, "error", G_CALLBACK(error_callback), &error);
528 GSignondSessionData* data = gsignond_dictionary_new();
529 gsignond_plugin_refresh(plugin, data);
532 fail_if(result == NULL);
533 fail_if(error != NULL);
534 gsignond_dictionary_unref(result);
537 gsignond_dictionary_unref(data);
539 g_object_unref(config);
540 g_object_unref(plugin);
544 START_TEST (test_plugind_daemon)
547 GSignondPluginDaemon *daemon = NULL;
548 const gchar *plugin_type = "password";
550 const gchar *plugin_dir = g_getenv("SSO_GPLUGINS_DIR");
551 fail_if (plugin_dir == NULL);
553 gchar *plugin_path = g_module_build_path (plugin_dir, "nonexisting");
554 fail_if (plugin_path == NULL);
555 daemon = gsignond_plugin_daemon_new (plugin_path, "nonexisting", 0, 1);
556 g_free (plugin_path);
557 fail_if (daemon != NULL);
559 plugin_path = g_module_build_path (plugin_dir, plugin_type);
560 fail_if (plugin_path == NULL);
561 daemon = gsignond_plugin_daemon_new (plugin_path, plugin_type, 0, 1);
562 g_free (plugin_path);
563 fail_if (daemon == NULL);
564 g_object_unref (daemon);
569 Suite* pluginremote_suite (void)
571 Suite *s = suite_create ("Plugin remote");
574 TCase *tc_core = tcase_create ("RemoteTests");
575 tcase_add_checked_fixture (tc_core, _setup, _teardown);
576 tcase_add_test (tc_core, test_pluginremote_create);
577 tcase_add_test (tc_core, test_pluginremote_plugind_create);
578 tcase_add_test (tc_core, test_pluginremote_plugind_unref);
579 tcase_add_test (tc_core, test_pluginremote_plugind_kill);
580 tcase_add_test (tc_core, test_pluginremote_request);
581 tcase_add_test (tc_core, test_pluginremote_user_action_finished);
582 tcase_add_test (tc_core, test_pluginremote_refresh);
583 tcase_add_test (tc_core, test_plugind_daemon);
585 suite_add_tcase (s, tc_core);
593 Suite *s = pluginremote_suite();
594 SRunner *sr = srunner_create(s);
595 srunner_run_all(sr, CK_NORMAL);
596 number_failed = srunner_ntests_failed(sr);
598 return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;