1 /* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of libsignon-glib
5 * Copyright (C) 2009-2011 Nokia Corporation.
6 * Copyright (C) 2011-2012 Canonical Ltd.
7 * Copyright (C) 2012 Intel Corporation.
9 * Contact: Alberto Mardegan <alberto.mardegan@canonical.com>
10 * Contact: Jussi Laako <jussi.laako@linux.intel.com>
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public License
14 * version 2.1 as published by the Free Software Foundation.
16 * This library is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
28 * @example check_signon.c
29 * Shows how to initialize the framework.
32 #define SIGNON_DISABLE_DEPRECATION_WARNINGS
34 #include "libsignon-glib/signon-internals.h"
35 #include "libsignon-glib/signon-auth-service.h"
36 #include "libsignon-glib/signon-auth-session.h"
37 #include "libsignon-glib/signon-identity.h"
38 #include "libsignon-glib/signon-errors.h"
47 static const gchar *ssotest_mechanisms[] =
48 { "mech1", "mech2", "mech3", "BLOB", NULL };
49 static GMainLoop *main_loop = NULL;
50 static SignonIdentity *identity = NULL;
51 static SignonAuthService *auth_service = NULL;
53 #define SIGNOND_IDLE_TIMEOUT (5 + 2)
60 g_object_unref (auth_service);
66 g_object_unref (identity);
72 g_main_loop_quit (main_loop);
73 g_main_loop_unref (main_loop);
82 g_debug("%s", G_STRFUNC);
83 auth_service = signon_auth_service_new ();
84 main_loop = g_main_loop_new (NULL, FALSE);
86 fail_unless (SIGNON_IS_AUTH_SERVICE (auth_service),
87 "Failed to initialize the AuthService.");
93 signon_query_methods_cb (SignonAuthService *auth_service, gchar **methods,
94 GError *error, gpointer user_data)
98 g_warning ("%s: %s", G_STRFUNC, error->message);
99 g_main_loop_quit (main_loop);
103 gboolean has_ssotest = FALSE;
105 fail_unless (g_strcmp0 (user_data, "Hello") == 0, "Got wrong string");
106 fail_unless (methods != NULL, "The methods does not exist");
110 if (g_strcmp0 (*methods, "ssotest") == 0)
117 fail_unless (has_ssotest, "ssotest method does not exist");
119 g_main_loop_quit (main_loop);
122 START_TEST(test_query_methods)
126 g_debug("%s", G_STRFUNC);
128 main_loop = g_main_loop_new (NULL, FALSE);
130 auth_service = signon_auth_service_new ();
132 fail_unless (SIGNON_IS_AUTH_SERVICE (auth_service),
133 "Failed to initialize the AuthService.");
135 signon_auth_service_query_methods (auth_service, (SignonQueryMethodsCb)signon_query_methods_cb, "Hello");
136 g_main_loop_run (main_loop);
142 signon_query_mechanisms_cb (SignonAuthService *auth_service, gchar *method,
143 gchar **mechanisms, GError *error, gpointer user_data)
147 g_warning ("%s: %s", G_STRFUNC, error->message);
148 g_main_loop_quit (main_loop);
152 gboolean has_mech1 = FALSE;
153 gboolean has_mech2 = FALSE;
154 gboolean has_mech3 = FALSE;
156 fail_unless (strcmp (user_data, "Hello") == 0, "Got wrong string");
157 fail_unless (mechanisms != NULL, "The mechanisms does not exist");
161 if (g_strcmp0 (*mechanisms, "mech1") == 0)
164 if (g_strcmp0 (*mechanisms, "mech2") == 0)
167 if (g_strcmp0 (*mechanisms, "mech3") == 0)
173 fail_unless (has_mech1, "mech1 mechanism does not exist");
174 fail_unless (has_mech2, "mech2 mechanism does not exist");
175 fail_unless (has_mech3, "mech3 mechanism does not exist");
177 g_main_loop_quit (main_loop);
181 signon_query_mechanisms_cb_fail (SignonAuthService *auth_service,
184 GError *error, gpointer user_data)
186 fail_unless (error != NULL);
187 fail_unless (mechanisms == NULL);
188 fail_unless (error->domain == SIGNON_ERROR);
189 fail_unless (error->code == SIGNON_ERROR_METHOD_NOT_KNOWN);
190 g_main_loop_quit (main_loop);
193 START_TEST(test_query_mechanisms)
197 g_debug("%s", G_STRFUNC);
198 auth_service = signon_auth_service_new ();
200 fail_unless (SIGNON_IS_AUTH_SERVICE (auth_service),
201 "Failed to initialize the AuthService.");
203 signon_auth_service_query_mechanisms (auth_service,
205 (SignonQueryMechanismCb)signon_query_mechanisms_cb,
208 main_loop = g_main_loop_new (NULL, FALSE);
210 g_main_loop_run (main_loop);
212 /* Test a non existing method */
213 signon_auth_service_query_mechanisms (auth_service,
215 (SignonQueryMechanismCb)signon_query_mechanisms_cb_fail,
217 g_main_loop_run (main_loop);
224 test_quit_main_loop_cb (gpointer data)
226 g_main_loop_quit (main_loop);
231 test_auth_session_query_mechanisms_cb (SignonAuthSession *self,
238 g_warning ("%s: %s", G_STRFUNC, error->message);
239 g_main_loop_quit (main_loop);
243 fail_unless (mechanisms != NULL, "The mechanisms does not exist");
245 gchar** patterns = (gchar**)user_data;
247 int i = g_strv_length(mechanisms);
248 int x = g_strv_length(patterns);
249 fail_unless( i == x, "The number of obtained methods is wrong: %d vs %d", i, x);
253 gchar* pattern = patterns[--i];
254 fail_unless(g_strcmp0(pattern, mechanisms[i]) == 0, "The obtained mechanism differs from predefined pattern: %s vs %s", mechanisms[i], pattern);
257 g_strfreev(mechanisms);
258 g_main_loop_quit (main_loop);
261 START_TEST(test_auth_session_query_mechanisms)
267 g_debug("%s", G_STRFUNC);
268 SignonIdentity *idty = signon_identity_new(NULL);
269 fail_unless (idty != NULL, "Cannot create Iddentity object");
271 SignonAuthSession *auth_session = signon_identity_create_session(idty,
274 fail_unless (auth_session != NULL, "Cannot create AuthSession object");
279 patterns[0] = g_strdup("mech1");
280 patterns[1] = g_strdup("mech2");
281 patterns[2] = g_strdup("mech3");
284 signon_auth_session_query_available_mechanisms(auth_session,
285 (const gchar**)patterns,
286 test_auth_session_query_mechanisms_cb,
289 main_loop = g_main_loop_new (NULL, FALSE);
291 g_main_loop_run (main_loop);
296 signon_auth_session_query_available_mechanisms(auth_session,
297 (const gchar**)patterns,
298 test_auth_session_query_mechanisms_cb,
301 g_main_loop_run (main_loop);
306 signon_auth_session_query_available_mechanisms(auth_session,
307 (const gchar**)patterns,
308 test_auth_session_query_mechanisms_cb,
311 g_main_loop_run (main_loop);
314 g_object_unref(idty);
321 test_auth_session_query_mechanisms_nonexisting_cb (SignonAuthSession *self,
328 g_main_loop_quit (main_loop);
333 g_warning ("%s: %s", G_STRFUNC, error->message);
334 g_main_loop_quit (main_loop);
337 START_TEST(test_auth_session_query_mechanisms_nonexisting)
342 g_debug("%s", G_STRFUNC);
343 SignonIdentity *idty = signon_identity_new(NULL);
344 fail_unless (idty != NULL, "Cannot create Iddentity object");
346 SignonAuthSession *auth_session = signon_identity_create_session(idty,
349 fail_unless (auth_session != NULL, "Cannot create AuthSession object");
354 patterns[0] = g_strdup("mech1");
355 patterns[1] = g_strdup("mech2");
356 patterns[2] = g_strdup("mech3");
359 signon_auth_session_query_available_mechanisms(auth_session,
360 (const gchar**)patterns,
361 test_auth_session_query_mechanisms_nonexisting_cb,
364 main_loop = g_main_loop_new (NULL, FALSE);
366 g_main_loop_run (main_loop);
371 g_object_unref(idty);
378 test_auth_session_states_cb (SignonAuthSession *self,
383 gint *state_counter = (gint *)user_data;
388 test_auth_session_process_cb (SignonAuthSession *self,
389 GHashTable *sessionData,
395 g_warning ("%s: %s", G_STRFUNC, error->message);
396 g_main_loop_quit (main_loop);
400 fail_unless (sessionData != NULL, "The result is empty");
402 gchar* usernameKey = g_strdup(SIGNON_SESSION_DATA_USERNAME);
403 GValue* usernameVa = (GValue*)g_hash_table_lookup(sessionData, usernameKey);
405 gchar* realmKey = g_strdup(SIGNON_SESSION_DATA_REALM);
406 GValue* realmVa = (GValue*)g_hash_table_lookup(sessionData, realmKey);
408 fail_unless(g_strcmp0(g_value_get_string(usernameVa), "test_username") == 0, "Wrong value of username");
409 fail_unless(g_strcmp0(g_value_get_string(realmVa), "testRealm_after_test") == 0, "Wrong value of realm");
411 g_hash_table_destroy(sessionData);
416 g_main_loop_quit (main_loop);
419 START_TEST(test_auth_session_creation)
424 g_debug("%s", G_STRFUNC);
425 SignonIdentity *idty = signon_identity_new(NULL);
426 fail_unless (idty != NULL, "Cannot create Identity object");
428 SignonAuthSession *auth_session = signon_identity_create_session(idty,
432 fail_unless (auth_session != NULL, "Cannot create AuthSession object");
434 g_object_unref (idty);
435 fail_unless (SIGNON_IS_IDENTITY(idty), "Identity must stay untill all its session are not destroyed");
436 g_object_unref (auth_session);
438 fail_if (SIGNON_IS_AUTH_SESSION(auth_session), "AuthSession is not synchronized with parent Identity");
439 fail_if (SIGNON_IS_IDENTITY(idty), "Identity is not synchronized with its AuthSession");
445 START_TEST(test_auth_session_process)
448 gint state_counter = 0;
451 g_debug("%s", G_STRFUNC);
452 SignonIdentity *idty = signon_identity_new(NULL);
453 fail_unless (idty != NULL, "Cannot create Iddentity object");
455 SignonAuthSession *auth_session = signon_identity_create_session(idty,
459 fail_unless (auth_session != NULL, "Cannot create AuthSession object");
463 g_signal_connect(auth_session, "state-changed",
464 G_CALLBACK(test_auth_session_states_cb), &state_counter);
466 GHashTable* sessionData = g_hash_table_new(g_str_hash,
468 GValue* usernameVa = g_new0(GValue, 1);
469 gchar* usernameKey = g_strdup(SIGNON_SESSION_DATA_USERNAME);
470 g_value_init (usernameVa, G_TYPE_STRING);
471 g_value_set_static_string(usernameVa, "test_username");
473 g_hash_table_insert (sessionData,
477 GValue* passwordVa = g_new0(GValue, 1);
478 gchar* passwordKey = g_strdup(SIGNON_SESSION_DATA_SECRET);
480 g_value_init (passwordVa, G_TYPE_STRING);
481 g_value_set_static_string(passwordVa, "test_username");
483 g_hash_table_insert (sessionData,
487 signon_auth_session_process(auth_session,
490 test_auth_session_process_cb,
493 main_loop = g_main_loop_new (NULL, FALSE);
496 g_main_loop_run (main_loop);
497 fail_unless (state_counter == 12, "Wrong numer of state change signals: %d", state_counter);
500 signon_auth_session_process(auth_session,
503 test_auth_session_process_cb,
506 g_main_loop_run (main_loop);
507 fail_unless (state_counter == 12, "Wrong numer of state change signals: %d", state_counter);
510 signon_auth_session_process(auth_session,
513 test_auth_session_process_cb,
516 g_main_loop_run (main_loop);
517 fail_unless (state_counter == 12, "Wrong numer of state change signals: %d", state_counter);
520 g_object_unref (auth_session);
521 g_object_unref (idty);
523 g_value_unset(usernameVa);
527 g_value_unset(passwordVa);
536 test_auth_session_process_failure_cb (GObject *source_object,
540 SignonAuthSession *auth_session = SIGNON_AUTH_SESSION (source_object);
542 GError **error = user_data;
544 fail_unless (SIGNON_IS_AUTH_SESSION (source_object));
546 v_reply = signon_auth_session_process_finish (auth_session, res, error);
547 fail_unless (v_reply == NULL);
549 g_main_loop_quit (main_loop);
552 START_TEST(test_auth_session_process_failure)
554 SignonIdentity *identity;
555 SignonAuthSession *auth_session;
556 GVariantBuilder builder;
557 GVariant *session_data;
558 GError *error = NULL;
560 g_debug("%s", G_STRFUNC);
564 identity = signon_identity_new_from_db (1, NULL);
565 fail_unless (identity != NULL, "Cannot create Identity object");
566 auth_session = signon_auth_session_new (identity,
569 fail_unless (auth_session != NULL, "Cannot create AuthSession object");
570 fail_unless (error == NULL);
572 g_variant_builder_init (&builder, G_VARIANT_TYPE_VARDICT);
573 g_variant_builder_add (&builder, "{sv}",
574 "key", g_variant_new_string ("value"));
576 session_data = g_variant_builder_end (&builder);
578 signon_auth_session_process_async (auth_session,
582 test_auth_session_process_failure_cb,
585 main_loop = g_main_loop_new (NULL, FALSE);
586 g_main_loop_run (main_loop);
587 fail_unless (error != NULL);
588 fail_unless (error->domain == SIGNON_ERROR);
589 fail_unless (error->code == SIGNON_ERROR_MECHANISM_NOT_AVAILABLE);
591 g_object_unref (auth_session);
592 g_object_unref (identity);
599 test_auth_session_process_after_store_cb (SignonAuthSession *self,
608 fail("Got error: %s", error->message);
609 g_main_loop_quit (main_loop);
613 fail_unless (reply != NULL, "The result is empty");
615 v_username = g_hash_table_lookup(reply,
616 SIGNON_SESSION_DATA_USERNAME);
618 fail_unless (g_strcmp0 (g_value_get_string (v_username), "Nice user") == 0,
619 "Wrong value of username");
621 g_hash_table_unref (reply);
622 g_object_unref (self);
624 g_main_loop_quit (main_loop);
628 test_auth_session_process_after_store_start_session(SignonIdentity *self,
637 g_warning ("%s %d: %s", G_STRFUNC, __LINE__, error->message);
639 g_main_loop_quit (main_loop);
643 fail_unless (id > 0);
645 SignonAuthSession *auth_session =
646 signon_identity_create_session (self,
650 fail_unless (auth_session != NULL, "Cannot create AuthSession object");
653 fail ("Got error: %s", err->message);
654 g_clear_error (&err);
657 GHashTable *session_data = g_hash_table_new (g_str_hash,
660 signon_auth_session_process (auth_session,
663 test_auth_session_process_after_store_cb,
667 START_TEST(test_auth_session_process_after_store)
669 SignonIdentityInfo *info;
670 SignonIdentity *identity;
672 g_debug("%s", G_STRFUNC);
675 main_loop = g_main_loop_new (NULL, FALSE);
677 identity = signon_identity_new (NULL);
678 fail_unless (SIGNON_IS_IDENTITY (identity),
679 "Failed to initialize the Identity.");
681 info = signon_identity_info_new ();
682 signon_identity_info_set_method (info, "ssotest", ssotest_mechanisms);
683 signon_identity_info_set_owner_from_values (info, "", "");
684 signon_identity_info_access_control_list_append (info,
685 signon_security_context_new_from_values ("*", "*"));
686 signon_identity_info_set_username (info, "Nice user");
688 signon_identity_store_credentials_with_info (identity,
690 test_auth_session_process_after_store_start_session,
692 g_main_loop_run (main_loop);
694 g_object_unref (identity);
700 static GHashTable *create_methods_hashtable()
702 gchar *mechanisms[] = {
709 GHashTable *methods = g_hash_table_new_full (g_str_hash, g_str_equal, g_free,
710 (GDestroyNotify)g_strfreev);
712 g_hash_table_insert (methods, g_strdup("method1"), g_strdupv(mechanisms));
713 g_hash_table_insert (methods, g_strdup("method2"), g_strdupv(mechanisms));
714 g_hash_table_insert (methods, g_strdup("method3"), g_strdupv(mechanisms));
719 static void new_identity_store_credentials_cb(SignonIdentity *self,
724 gint *new_id = user_data;
728 g_warning ("%s %d: %s", G_STRFUNC, __LINE__, error->message);
732 fail_unless (id > 0);
736 g_main_loop_quit (main_loop);
742 SignonIdentity *identity;
746 if (main_loop == NULL)
747 main_loop = g_main_loop_new (NULL, FALSE);
749 identity = signon_identity_new (NULL);
750 fail_unless (SIGNON_IS_IDENTITY (identity));
751 methods = g_hash_table_new (g_str_hash, g_str_equal);
752 g_hash_table_insert (methods, "ssotest", ssotest_mechanisms);
753 signon_identity_store_credentials_with_args (identity,
763 new_identity_store_credentials_cb,
765 g_hash_table_destroy (methods);
768 g_main_loop_run (main_loop);
775 identity_registered_cb (gpointer data)
777 g_main_loop_quit (main_loop);
781 START_TEST(test_get_existing_identity)
785 g_debug("%s", G_STRFUNC);
786 guint id = new_identity();
788 fail_unless (id != 0);
790 identity = signon_identity_new_from_db (id, NULL);
792 fail_unless (identity != NULL);
793 fail_unless (SIGNON_IS_IDENTITY (identity),
794 "Failed to initialize the Identity.");
796 g_timeout_add (1000, identity_registered_cb, identity);
797 main_loop = g_main_loop_new (NULL, FALSE);
798 g_main_loop_run (main_loop);
804 START_TEST(test_get_nonexisting_identity)
808 g_debug("%s", G_STRFUNC);
809 identity = signon_identity_new_from_db (G_MAXINT, NULL);
811 fail_unless (identity != NULL);
812 fail_unless (SIGNON_IS_IDENTITY (identity),
813 "Failed to initialize the Identity.");
815 g_timeout_add (1000, identity_registered_cb, identity);
816 main_loop = g_main_loop_new (NULL, FALSE);
817 g_main_loop_run (main_loop);
819 const GError *error = NULL;
820 error = signon_identity_get_last_error(identity);
821 fail_unless (error != NULL);
823 fail_unless (error->domain == SIGNON_ERROR);
824 fail_unless (error->code == SIGNON_ERROR_IDENTITY_NOT_FOUND);
830 static void store_credentials_identity_cb(SignonIdentity *self,
837 g_warning ("%s %d: %s", G_STRFUNC, __LINE__, error->message);
841 fail_unless (id > 0);
843 if (user_data != NULL)
845 gint *last_id = (gint *)user_data;
846 g_warning ("%s (prev_id vs new_id): %d vs %d", G_STRFUNC, *last_id, id);
848 fail_unless (id == (*last_id) + 1);
852 /* Wait some time to ensure that the info-updated signals are
855 g_timeout_add_seconds (2, test_quit_main_loop_cb, main_loop);
858 START_TEST(test_store_credentials_identity)
861 g_debug("%s", G_STRFUNC);
862 SignonIdentity *idty = signon_identity_new(NULL);
863 fail_unless (idty != NULL);
864 fail_unless (SIGNON_IS_IDENTITY (idty),
865 "Failed to initialize the Identity.");
867 gint last_id = new_identity();
869 GHashTable *methods = create_methods_hashtable();
871 signon_identity_store_credentials_with_args (idty,
881 store_credentials_identity_cb,
883 g_hash_table_destroy (methods);
885 g_timeout_add (1000, test_quit_main_loop_cb, idty);
886 main_loop = g_main_loop_new (NULL, FALSE);
887 g_main_loop_run (main_loop);
889 g_object_unref(idty);
894 static void identity_verify_username_cb(SignonIdentity *self,
899 fail_unless (error != NULL, "The callback returned NULL error for unimplemented function");
900 g_warning ("Error: %s ", error->message);
902 g_main_loop_quit((GMainLoop *)user_data);
905 static void identity_remove_cb(SignonIdentity *self, const GError *error, gpointer user_data)
908 g_warning (" %s ", __func__);
911 g_warning ("Error: %s ", error->message);
912 fail_if (user_data == NULL, "There should be no error in callback");
916 g_warning ("No error");
917 fail_if (user_data != NULL, "The callback must return an error");
920 g_main_loop_quit(main_loop);
923 START_TEST(test_remove_identity)
926 g_debug("%s", G_STRFUNC);
927 SignonIdentity *idty = signon_identity_new (NULL);
928 fail_unless (idty != NULL);
929 fail_unless (SIGNON_IS_IDENTITY (idty),
930 "Failed to initialize the Identity.");
932 main_loop = g_main_loop_new (NULL, FALSE);
934 * Try to remove non-stored identity
936 signon_identity_remove(idty, identity_remove_cb, NULL);
937 g_main_loop_run (main_loop);
939 GHashTable *methods = create_methods_hashtable();
942 * Try to remove existing identy
945 gint id = new_identity();
946 SignonIdentity *idty2 = signon_identity_new_from_db (id, NULL);
948 signon_identity_remove(idty2, identity_remove_cb, NULL);
949 g_main_loop_run (main_loop);
952 * Try to remove already removed
955 signon_identity_remove(idty2, identity_remove_cb, GINT_TO_POINTER(TRUE));
957 g_object_unref (idty);
958 g_object_unref (idty2);
963 static gboolean _contains(gchar **mechs, gchar *mech)
965 gboolean present = FALSE;
967 while (mechs[i] != NULL)
969 if (g_strcmp0 (mech, mechs[i]) == 0) present = TRUE;
975 static void identity_info_cb(SignonIdentity *self, const SignonIdentityInfo *info, const GError *error, gpointer user_data)
979 g_warning ("%s: Error: %s ", __func__, error->message);
980 fail_if (info != NULL, "Error: %s ", error->message);
981 g_main_loop_quit(main_loop);
985 g_warning ("No error");
987 SignonIdentityInfo **pattern_ptr = (SignonIdentityInfo **)user_data;
988 SignonIdentityInfo *pattern = NULL;
991 pattern = (*pattern_ptr);
994 fail_unless (info == NULL, "The info must be NULL");
997 fail_unless (info != NULL, "The info must be non-null");
998 fail_unless (g_strcmp0 (signon_identity_info_get_username(info),
999 signon_identity_info_get_username(pattern)) == 0, "The info has wrong username");
1000 fail_unless (g_strcmp0 (signon_identity_info_get_caption(info),
1001 signon_identity_info_get_caption(pattern)) == 0, "The info has wrong caption");
1003 GHashTable *methods = (GHashTable *)signon_identity_info_get_methods (info);
1004 gchar **mechs1 = g_hash_table_lookup (methods, "method1");
1005 gchar **mechs2 = g_hash_table_lookup (methods, "method2");
1006 gchar **mechs3 = g_hash_table_lookup (methods, "method3");
1008 fail_unless (g_strv_length (mechs1) == 3);
1009 fail_unless (g_strv_length (mechs2) == 3);
1010 fail_unless (g_strv_length (mechs3) == 3);
1012 fail_unless (_contains(mechs1, "mechanism1"));
1013 fail_unless (_contains(mechs1, "mechanism2"));
1014 fail_unless (_contains(mechs1, "mechanism3"));
1016 fail_unless (_contains(mechs2, "mechanism1"));
1017 fail_unless (_contains(mechs2, "mechanism2"));
1018 fail_unless (_contains(mechs2, "mechanism3"));
1020 fail_unless (_contains(mechs3, "mechanism1"));
1021 fail_unless (_contains(mechs3, "mechanism2"));
1022 fail_unless (_contains(mechs3, "mechanism3"));
1027 signon_identity_info_free (pattern);
1028 *pattern_ptr = signon_identity_info_copy (info);
1031 g_main_loop_quit(main_loop);
1034 static SignonIdentityInfo *create_standard_info()
1036 GHashTable *methods;
1038 g_debug("%s", G_STRFUNC);
1040 SignonIdentityInfo *info = signon_identity_info_new ();
1042 methods = g_hash_table_new (g_str_hash, g_str_equal);
1043 g_hash_table_insert (methods, "ssotest", ssotest_mechanisms);
1044 signon_identity_info_set_methods (info, methods);
1045 g_hash_table_destroy (methods);
1047 signon_identity_info_set_owner_from_values (info, "", "");
1048 signon_identity_info_access_control_list_append (info,
1049 signon_security_context_new_from_values ("*", "*"));
1050 signon_identity_info_set_username (info, "James Bond");
1051 signon_identity_info_set_secret (info, "007", TRUE);
1052 signon_identity_info_set_caption (info, "MI-6");
1054 gchar *mechanisms[] = {
1061 signon_identity_info_set_method (info, "method1", (const gchar **)mechanisms);
1062 signon_identity_info_set_method (info, "method2", (const gchar **)mechanisms);
1063 signon_identity_info_set_method (info, "method3", (const gchar **)mechanisms);
1068 START_TEST(test_info_identity)
1070 g_debug("%s", G_STRFUNC);
1072 SignonIdentity *idty = signon_identity_new (NULL);
1073 fail_unless (idty != NULL);
1074 fail_unless (SIGNON_IS_IDENTITY (idty),
1075 "Failed to initialize the Identity.");
1077 SignonIdentityInfo *info = NULL;
1079 main_loop = g_main_loop_new (NULL, FALSE);
1081 * Try to get_info for non-stored idetnity
1083 signon_identity_query_info (idty, identity_info_cb, &info);
1084 g_main_loop_run (main_loop);
1086 GHashTable *methods = create_methods_hashtable();
1087 signon_identity_store_credentials_with_args (idty,
1097 store_credentials_identity_cb,
1099 g_hash_table_destroy (methods);
1100 g_main_loop_run (main_loop);
1102 info = signon_identity_info_new ();
1103 signon_identity_info_set_username (info, "James Bond");
1104 signon_identity_info_set_secret (info, "007", TRUE);
1105 signon_identity_info_set_caption (info, "MI-6");
1107 gchar *mechanisms[] = {
1114 signon_identity_info_set_method (info, "method1", (const gchar **)mechanisms);
1115 signon_identity_info_set_method (info, "method2", (const gchar **)mechanisms);
1116 signon_identity_info_set_method (info, "method3", (const gchar **)mechanisms);
1118 signon_identity_query_info (idty, identity_info_cb, &info);
1119 g_main_loop_run (main_loop);
1121 gint id = signon_identity_info_get_id (info);
1122 fail_unless (id != 0);
1123 SignonIdentity *idty2 = signon_identity_new_from_db (id, NULL);
1125 signon_identity_query_info (idty2, identity_info_cb, &info);
1126 g_main_loop_run (main_loop);
1129 * Try to update one identity and
1130 * have a look what will happen
1132 signon_identity_info_set_username (info, "James Bond_2nd version");
1133 signon_identity_info_set_caption (info, "caption_2nd version");
1135 signon_identity_store_credentials_with_info (idty2,
1137 store_credentials_identity_cb,
1139 g_main_loop_run (main_loop);
1141 signon_identity_query_info (idty, identity_info_cb, &info);
1142 g_main_loop_run (main_loop);
1144 * Try to remove existing identity and
1145 * have a look what will happen
1147 signon_identity_remove(idty2, identity_remove_cb, NULL);
1148 g_main_loop_run (main_loop);
1151 * no main_loops required as
1152 * the callback is executed immediately
1154 signon_identity_query_info (idty2, identity_info_cb, NULL);
1155 signon_identity_query_info (idty, identity_info_cb, NULL);
1157 signon_identity_info_free (info);
1158 g_object_unref (idty);
1159 g_object_unref (idty2);
1164 static void identity_signout_cb (SignonIdentity *self,
1165 const GError *error,
1169 g_warning ("%s: %s", G_STRFUNC, error->message);
1171 g_warning ("%s: No error", G_STRFUNC);
1173 fail_unless (error == NULL, "There should be no error in callback");
1174 g_main_loop_quit (main_loop);
1177 static void identity_signout_signal_cb (gpointer instance, gpointer user_data)
1179 gint *incr = (gint *)user_data;
1180 (*incr) = (*incr) + 1;
1181 g_warning ("%s: %d", G_STRFUNC, *incr);
1184 START_TEST(test_signout_identity)
1186 g_debug("%s", G_STRFUNC);
1188 SignonIdentity *idty = signon_identity_new (NULL);
1189 fail_unless (idty != NULL);
1190 fail_unless (SIGNON_IS_IDENTITY (idty),
1191 "Failed to initialize the Identity.");
1193 SignonIdentityInfo *info = create_standard_info();
1194 main_loop = g_main_loop_new (NULL, FALSE);
1196 signon_identity_store_credentials_with_info (idty,
1198 store_credentials_identity_cb,
1200 g_main_loop_run (main_loop);
1201 signon_identity_query_info (idty, identity_info_cb, &info);
1202 g_main_loop_run (main_loop);
1204 gint id = signon_identity_info_get_id (info);
1205 SignonIdentity *idty2 = signon_identity_new_from_db (id, NULL);
1207 /* wait some more time to ensure that the object gets registered */
1208 g_timeout_add_seconds (2, test_quit_main_loop_cb, main_loop);
1209 g_main_loop_run (main_loop);
1211 signon_identity_info_free (info);
1215 SignonAuthSession *as1 = signon_identity_create_session (idty,
1218 fail_unless (as1 != NULL, "cannot create AuthSession");
1220 SignonAuthSession *as2 = signon_identity_create_session (idty2,
1223 fail_unless (as2 != NULL, "cannot create AuthSession");
1227 g_signal_connect (idty, "signout",
1228 G_CALLBACK(identity_signout_signal_cb), &counter);
1229 g_signal_connect (idty2, "signout",
1230 G_CALLBACK(identity_signout_signal_cb), &counter);
1232 signon_identity_signout (idty, identity_signout_cb, NULL);
1233 g_main_loop_run (main_loop);
1235 fail_unless (counter == 2, "Lost some of SIGNOUT signals");
1236 fail_if (SIGNON_IS_AUTH_SESSION (as1), "Authsession1 was not destroyed after signout");
1237 fail_if (SIGNON_IS_AUTH_SESSION (as2), "Authsession2 was not destroyed after signout");
1239 g_object_unref (idty);
1240 g_object_unref (idty2);
1244 START_TEST(test_unregistered_identity)
1247 g_debug("%s", G_STRFUNC);
1248 SignonIdentity *idty = signon_identity_new (NULL);
1249 fail_unless (idty != NULL);
1250 fail_unless (SIGNON_IS_IDENTITY (idty),
1251 "Failed to initialize the Identity.");
1253 SignonIdentityInfo *info = create_standard_info();
1254 main_loop = g_main_loop_new (NULL, FALSE);
1256 signon_identity_store_credentials_with_info (idty,
1258 store_credentials_identity_cb,
1260 g_main_loop_run (main_loop);
1263 * give the time for identity to became idle
1265 sleep(SIGNOND_IDLE_TIMEOUT);
1266 SignonIdentity *idty2 = signon_identity_new (NULL);
1269 * give time to handle unregistered signal
1271 g_timeout_add_seconds (5, test_quit_main_loop_cb, main_loop);
1273 signon_identity_query_info (idty, identity_info_cb, &info);
1274 g_main_loop_run (main_loop);
1276 g_object_unref (idty);
1277 g_object_unref (idty2);
1281 START_TEST(test_unregistered_auth_session)
1283 g_debug("%s", G_STRFUNC);
1285 SignonIdentity *idty = signon_identity_new (NULL);
1286 fail_unless (idty != NULL);
1287 fail_unless (SIGNON_IS_IDENTITY (idty),
1288 "Failed to initialize the Identity.");
1290 main_loop = g_main_loop_new (NULL, FALSE);
1293 SignonAuthSession *as = signon_identity_create_session(idty,
1296 /* give time to register the objects */
1297 g_timeout_add_seconds (2, test_quit_main_loop_cb, main_loop);
1298 g_main_loop_run (main_loop);
1301 * give the time for identity to became idle
1303 sleep(SIGNOND_IDLE_TIMEOUT);
1304 SignonIdentity *idty2 = signon_identity_new (NULL);
1307 * give time to handle unregistered signal
1309 g_timeout_add_seconds (5, test_quit_main_loop_cb, main_loop);
1310 g_main_loop_run (main_loop);
1314 patterns[0] = g_strdup("mech1");
1315 patterns[1] = g_strdup("mech2");
1316 patterns[2] = g_strdup("mech3");
1319 signon_auth_session_query_available_mechanisms(as,
1320 (const gchar**)patterns,
1321 test_auth_session_query_mechanisms_cb,
1322 (gpointer)patterns);
1323 g_main_loop_run (main_loop);
1325 g_object_unref (as);
1326 g_object_unref (idty);
1327 g_object_unref (idty2);
1329 g_free (patterns[0]);
1330 g_free (patterns[1]);
1331 g_free (patterns[2]);
1332 g_free (patterns[3]);
1337 test_regression_unref_process_cb (SignonAuthSession *self,
1339 const GError *error,
1346 g_warning ("%s: %s", G_STRFUNC, error->message);
1347 g_main_loop_quit (main_loop);
1351 fail_unless (reply != NULL, "The result is empty");
1353 fail_unless (g_strcmp0 (user_data, "Hi there!") == 0,
1354 "Didn't get expected user_data");
1356 v_string = g_hash_table_lookup(reply, "James");
1357 fail_unless (v_string != 0);
1358 fail_unless (g_strcmp0 (g_value_get_string (v_string), "Bond") == 0,
1359 "Wrong reply data");
1361 /* The next line is actually the regression we want to test */
1362 g_object_unref (self);
1364 g_main_loop_quit (main_loop);
1367 START_TEST(test_regression_unref)
1369 SignonIdentity *identity;
1370 SignonAuthSession *auth_session;
1371 GHashTable *session_data;
1372 GError *error = NULL;
1373 GValue v_string = G_VALUE_INIT;
1375 g_debug ("%s", G_STRFUNC);
1378 main_loop = g_main_loop_new (NULL, FALSE);
1380 identity = signon_identity_new_from_db (1, NULL);
1381 fail_unless (identity != NULL);
1382 auth_session = signon_auth_session_new (identity, "ssotest", &error);
1383 fail_unless (auth_session != NULL);
1385 session_data = g_hash_table_new (g_str_hash, g_str_equal);
1386 g_value_init (&v_string, G_TYPE_STRING);
1387 g_value_set_static_string (&v_string, "Bond");
1388 g_hash_table_insert (session_data, "James", &v_string);
1390 signon_auth_session_process (auth_session,
1393 test_regression_unref_process_cb,
1394 g_strdup ("Hi there!"));
1395 g_main_loop_run (main_loop);
1397 g_object_unref (auth_session);
1398 g_object_unref (identity);
1407 Suite *s = suite_create ("signon-glib");
1409 /* Core test case */
1410 TCase * tc_core = tcase_create("Core");
1413 * 18 minutes timeout
1415 tcase_set_timeout(tc_core, 1080);
1416 tcase_add_test (tc_core, test_init);
1417 tcase_add_test (tc_core, test_query_methods);
1418 tcase_add_test (tc_core, test_query_mechanisms);
1419 tcase_add_test (tc_core, test_get_existing_identity);
1420 tcase_add_test (tc_core, test_get_nonexisting_identity);
1422 tcase_add_test (tc_core, test_auth_session_creation);
1423 tcase_add_test (tc_core, test_auth_session_query_mechanisms);
1424 tcase_add_test (tc_core, test_auth_session_query_mechanisms_nonexisting);
1425 tcase_add_test (tc_core, test_auth_session_process);
1426 tcase_add_test (tc_core, test_auth_session_process_failure);
1427 tcase_add_test (tc_core, test_auth_session_process_after_store);
1428 tcase_add_test (tc_core, test_store_credentials_identity);
1429 tcase_add_test (tc_core, test_remove_identity);
1430 tcase_add_test (tc_core, test_info_identity);
1432 tcase_add_test (tc_core, test_signout_identity);
1433 tcase_add_test (tc_core, test_unregistered_identity);
1434 tcase_add_test (tc_core, test_unregistered_auth_session);
1436 tcase_add_test (tc_core, test_regression_unref);
1438 suite_add_tcase (s, tc_core);
1446 Suite * s = signon_suite();
1447 SRunner * sr = srunner_create(s);
1449 srunner_set_xml(sr, "/tmp/result.xml");
1450 srunner_run_all(sr, CK_NORMAL);
1451 number_failed = srunner_ntests_failed(sr);
1454 return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
1457 /* vim: set ai et tw=75 ts=4 sw=4: */