1 /* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of libgsignon-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 "libgsignon-glib/signon-internals.h"
35 #include "libgsignon-glib/signon-auth-service.h"
36 #include "libgsignon-glib/signon-auth-session.h"
37 #include "libgsignon-glib/signon-identity.h"
38 #include "libgsignon-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;
52 static gboolean id_destroyed = FALSE;
54 #define SIGNOND_IDLE_TIMEOUT (5 + 2)
60 g_main_loop_quit (main_loop);
68 g_main_loop_run (main_loop);
76 if (main_loop == NULL) {
77 main_loop = g_main_loop_new (NULL, FALSE);
86 g_object_unref (auth_service);
92 g_object_unref (identity);
98 g_main_loop_unref (main_loop);
104 new_identity_store_credentials_cb(
105 SignonIdentity *self,
110 gint *new_id = user_data;
114 g_warning ("%s %d: %s", G_STRFUNC, __LINE__, error->message);
118 fail_unless (id > 0);
128 SignonIdentity *idty;
132 idty = signon_identity_new ();
133 fail_unless (SIGNON_IS_IDENTITY (idty));
134 methods = g_hash_table_new (g_str_hash, g_str_equal);
135 g_hash_table_insert (methods, "ssotest", ssotest_mechanisms);
136 signon_identity_store_credentials_with_args (idty,
146 new_identity_store_credentials_cb,
148 g_hash_table_destroy (methods);
153 g_object_unref (idty);
159 START_TEST(test_init)
161 g_debug("%s", G_STRFUNC);
162 auth_service = signon_auth_service_new ();
164 fail_unless (SIGNON_IS_AUTH_SERVICE (auth_service),
165 "Failed to initialize the AuthService.");
170 signon_query_methods_cb (SignonAuthService *auth_service, gchar **methods,
171 GError *error, gpointer user_data)
175 g_warning ("%s: %s", G_STRFUNC, error->message);
177 if (methods) g_strfreev (methods);
181 gboolean has_ssotest = FALSE;
182 gchar **pmethods = methods;
184 fail_unless (g_strcmp0 (user_data, "Hello") == 0, "Got wrong string");
185 fail_unless (methods != NULL, "The methods does not exist");
189 if (g_strcmp0 (*pmethods, "ssotest") == 0)
196 g_strfreev (methods);
197 fail_unless (has_ssotest, "ssotest method does not exist");
202 START_TEST(test_query_methods)
204 g_debug("%s", G_STRFUNC);
206 auth_service = signon_auth_service_new ();
208 fail_unless (SIGNON_IS_AUTH_SERVICE (auth_service),
209 "Failed to initialize the AuthService.");
211 signon_auth_service_query_methods (auth_service, (SignonQueryMethodsCb)signon_query_methods_cb, "Hello");
217 signon_query_mechanisms_cb (SignonAuthService *auth_service, gchar *method,
218 gchar **mechanisms, GError *error, gpointer user_data)
222 g_warning ("%s: %s", G_STRFUNC, error->message);
223 if (mechanisms) g_strfreev (mechanisms);
228 gboolean has_mech1 = FALSE;
229 gboolean has_mech2 = FALSE;
230 gboolean has_mech3 = FALSE;
231 gchar **pmechanisms = mechanisms;
233 fail_unless (strcmp (user_data, "Hello") == 0, "Got wrong string");
234 fail_unless (mechanisms != NULL, "The mechanisms does not exist");
238 if (g_strcmp0 (*pmechanisms, "mech1") == 0)
241 if (g_strcmp0 (*pmechanisms, "mech2") == 0)
244 if (g_strcmp0 (*pmechanisms, "mech3") == 0)
249 g_strfreev (mechanisms);
250 fail_unless (has_mech1, "mech1 mechanism does not exist");
251 fail_unless (has_mech2, "mech2 mechanism does not exist");
252 fail_unless (has_mech3, "mech3 mechanism does not exist");
258 signon_query_mechanisms_cb_fail (SignonAuthService *auth_service,
261 GError *error, gpointer user_data)
263 fail_unless (error != NULL);
264 fail_unless (mechanisms == NULL);
265 fail_unless (error->domain == SIGNON_ERROR);
266 fail_unless (error->code == SIGNON_ERROR_METHOD_NOT_KNOWN);
267 if (mechanisms) g_strfreev (mechanisms);
271 START_TEST(test_query_mechanisms)
273 g_debug("%s", G_STRFUNC);
274 auth_service = signon_auth_service_new ();
276 fail_unless (SIGNON_IS_AUTH_SERVICE (auth_service),
277 "Failed to initialize the AuthService.");
279 signon_auth_service_query_mechanisms (auth_service,
281 (SignonQueryMechanismCb)signon_query_mechanisms_cb,
286 /* Test a non existing method */
287 signon_auth_service_query_mechanisms (auth_service,
289 (SignonQueryMechanismCb)signon_query_mechanisms_cb_fail,
297 test_quit_main_loop_cb (gpointer data)
304 test_auth_session_query_mechanisms_cb (SignonAuthSession *self,
311 g_warning ("%s: %s", G_STRFUNC, error->message);
316 fail_unless (mechanisms != NULL, "The mechanisms does not exist");
318 gchar** patterns = (gchar**)user_data;
320 int i = g_strv_length(mechanisms);
321 int x = g_strv_length(patterns);
322 fail_unless( i == x, "The number of obtained methods is wrong: %d vs %d", i, x);
326 gchar* pattern = patterns[--i];
327 fail_unless(g_strcmp0(pattern, mechanisms[i]) == 0, "The obtained mechanism differs from predefined pattern: %s vs %s", mechanisms[i], pattern);
330 g_strfreev(mechanisms);
334 START_TEST(test_auth_session_query_mechanisms)
338 g_debug("%s", G_STRFUNC);
339 SignonIdentity *idty = signon_identity_new();
340 fail_unless (idty != NULL, "Cannot create Iddentity object");
342 SignonAuthSession *auth_session = signon_identity_create_session(idty,
345 fail_unless (auth_session != NULL, "Cannot create AuthSession object");
350 patterns[0] = g_strdup("mech1");
351 patterns[1] = g_strdup("mech2");
352 patterns[2] = g_strdup("mech3");
355 signon_auth_session_query_available_mechanisms(auth_session,
356 (const gchar**)patterns,
357 test_auth_session_query_mechanisms_cb,
365 signon_auth_session_query_available_mechanisms(auth_session,
366 (const gchar**)patterns,
367 test_auth_session_query_mechanisms_cb,
375 signon_auth_session_query_available_mechanisms(auth_session,
376 (const gchar**)patterns,
377 test_auth_session_query_mechanisms_cb,
383 g_object_unref (auth_session);
384 g_object_unref (idty);
390 test_auth_session_query_mechanisms_nonexisting_cb (SignonAuthSession *self,
402 g_warning ("%s: %s", G_STRFUNC, error->message);
406 START_TEST(test_auth_session_query_mechanisms_nonexisting)
410 g_debug("%s", G_STRFUNC);
411 SignonIdentity *idty = signon_identity_new();
412 fail_unless (idty != NULL, "Cannot create Iddentity object");
414 SignonAuthSession *auth_session = signon_identity_create_session(idty,
417 fail_unless (auth_session != NULL, "Cannot create AuthSession object");
422 patterns[0] = g_strdup("mech1");
423 patterns[1] = g_strdup("mech2");
424 patterns[2] = g_strdup("mech3");
427 signon_auth_session_query_available_mechanisms(auth_session,
428 (const gchar**)patterns,
429 test_auth_session_query_mechanisms_nonexisting_cb,
437 g_object_unref (auth_session);
438 g_object_unref (idty);
444 test_auth_session_states_cb (SignonAuthSession *self,
449 gint *state_counter = (gint *)user_data;
454 test_auth_session_process_cb (SignonAuthSession *self,
455 GHashTable *sessionData,
461 g_warning ("%s: %s", G_STRFUNC, error->message);
466 fail_unless (sessionData != NULL, "The result is empty");
468 gchar* usernameKey = g_strdup(SIGNON_SESSION_DATA_USERNAME);
469 GValue* usernameVa = (GValue*)g_hash_table_lookup(sessionData, usernameKey);
471 gchar* realmKey = g_strdup(SIGNON_SESSION_DATA_REALM);
472 GValue* realmVa = (GValue*)g_hash_table_lookup(sessionData, realmKey);
474 fail_unless(g_strcmp0(g_value_get_string(usernameVa), "test_username") == 0, "Wrong value of username");
475 fail_unless(g_strcmp0(g_value_get_string(realmVa), "testRealm_after_test") == 0, "Wrong value of realm");
477 g_hash_table_destroy(sessionData);
486 _on_identity_destroyed (gpointer data, GObject *obj)
492 _on_auth_session_destroyed (gpointer data, GObject *obj)
494 gboolean *is_destroyed = (gboolean *)data;
495 *is_destroyed = TRUE;
498 START_TEST(test_auth_session_creation)
501 gboolean auth_sess_destroyed = FALSE;
503 g_debug("%s", G_STRFUNC);
504 SignonIdentity *idty = signon_identity_new();
505 fail_unless (idty != NULL, "Cannot create Identity object");
507 SignonAuthSession *auth_session = signon_identity_create_session(idty,
511 fail_unless (auth_session != NULL, "Cannot create AuthSession object");
513 id_destroyed = FALSE;
514 g_object_weak_ref (G_OBJECT (idty), _on_identity_destroyed, NULL);
515 g_object_weak_ref (G_OBJECT (auth_session), _on_auth_session_destroyed,
516 &auth_sess_destroyed);
518 g_object_unref (idty);
519 fail_unless (id_destroyed == FALSE, "Identity must stay untill all its session are not destroyed");
521 g_object_unref (auth_session);
523 fail_if (auth_sess_destroyed == FALSE, "AuthSession is not synchronized with parent Identity");
524 fail_if (id_destroyed == FALSE, "Identity is not synchronized with its AuthSession");
531 START_TEST(test_auth_session_process)
533 gint state_counter = 0;
536 g_debug("%s", G_STRFUNC);
537 SignonIdentity *idty = signon_identity_new();
538 fail_unless (idty != NULL, "Cannot create Iddentity object");
540 SignonAuthSession *auth_session = signon_identity_create_session(idty,
544 fail_unless (auth_session != NULL, "Cannot create AuthSession object");
548 g_signal_connect(auth_session, "state-changed",
549 G_CALLBACK(test_auth_session_states_cb), &state_counter);
551 GHashTable* sessionData = g_hash_table_new(g_str_hash,
553 GValue* usernameVa = g_new0(GValue, 1);
554 gchar* usernameKey = g_strdup(SIGNON_SESSION_DATA_USERNAME);
555 g_value_init (usernameVa, G_TYPE_STRING);
556 g_value_set_static_string(usernameVa, "test_username");
558 g_hash_table_insert (sessionData,
562 GValue* passwordVa = g_new0(GValue, 1);
563 gchar* passwordKey = g_strdup(SIGNON_SESSION_DATA_SECRET);
565 g_value_init (passwordVa, G_TYPE_STRING);
566 g_value_set_static_string(passwordVa, "test_username");
568 g_hash_table_insert (sessionData,
572 signon_auth_session_process(auth_session,
575 test_auth_session_process_cb,
580 fail_unless (state_counter == 12, "Wrong numer of state change signals: %d", state_counter);
583 signon_auth_session_process(auth_session,
586 test_auth_session_process_cb,
590 fail_unless (state_counter == 12, "Wrong numer of state change signals: %d", state_counter);
593 signon_auth_session_process(auth_session,
596 test_auth_session_process_cb,
600 fail_unless (state_counter == 12, "Wrong numer of state change signals: %d", state_counter);
603 g_object_unref (auth_session);
604 g_object_unref (idty);
606 g_value_unset(usernameVa);
610 g_value_unset(passwordVa);
614 g_hash_table_unref (sessionData);
619 test_auth_session_process_failure_cb (GObject *source_object,
623 g_debug("%s", G_STRFUNC);
624 SignonAuthSession *auth_session = SIGNON_AUTH_SESSION (source_object);
626 GError **error = user_data;
628 fail_unless (SIGNON_IS_AUTH_SESSION (source_object));
630 v_reply = signon_auth_session_process_finish (auth_session, res, error);
631 fail_unless (v_reply == NULL);
636 START_TEST(test_auth_session_process_failure)
638 SignonIdentity *idty;
639 SignonAuthSession *auth_session;
640 GVariantBuilder builder;
641 GVariant *session_data;
642 GError *error = NULL;
644 g_debug("%s", G_STRFUNC);
646 guint id = new_identity();
648 fail_unless (id != 0);
650 idty = signon_identity_new_from_db (id);
652 fail_unless (idty != NULL, "Cannot create Identity object");
653 auth_session = signon_auth_session_new_for_identity (idty,
656 fail_unless (auth_session != NULL, "Cannot create AuthSession object");
657 fail_unless (error == NULL);
659 g_variant_builder_init (&builder, G_VARIANT_TYPE_VARDICT);
660 g_variant_builder_add (&builder, "{sv}",
661 "key", g_variant_new_string ("value"));
663 session_data = g_variant_builder_end (&builder);
665 signon_auth_session_process_async (auth_session,
669 test_auth_session_process_failure_cb,
672 fail_unless (error != NULL);
673 fail_unless (error->domain == SIGNON_ERROR);
674 fail_unless (error->code == SIGNON_ERROR_MECHANISM_NOT_AVAILABLE);
675 g_error_free (error);
677 g_object_unref (auth_session);
678 g_object_unref (idty);
683 test_auth_session_process_after_store_cb (SignonAuthSession *self,
692 fail("Got error: %s", error->message);
697 fail_unless (reply != NULL, "The result is empty");
699 v_username = g_hash_table_lookup(reply,
700 SIGNON_SESSION_DATA_USERNAME);
702 fail_unless (g_strcmp0 (g_value_get_string (v_username), "Nice user") == 0,
703 "Wrong value of username");
705 g_hash_table_unref (reply);
706 g_object_unref (self);
712 test_auth_session_process_after_store_start_session(SignonIdentity *self,
721 g_warning ("%s %d: %s", G_STRFUNC, __LINE__, error->message);
727 fail_unless (id > 0);
729 SignonAuthSession *auth_session =
730 signon_identity_create_session (self,
734 fail_unless (auth_session != NULL, "Cannot create AuthSession object");
737 fail ("Got error: %s", err->message);
738 g_clear_error (&err);
741 GHashTable *session_data = g_hash_table_new (g_str_hash,
744 signon_auth_session_process (auth_session,
747 test_auth_session_process_after_store_cb,
749 g_hash_table_unref (session_data);
752 START_TEST(test_auth_session_process_after_store)
754 SignonIdentityInfo *info;
755 SignonIdentity *idty;
757 g_debug("%s", G_STRFUNC);
759 idty = signon_identity_new ();
760 fail_unless (SIGNON_IS_IDENTITY (idty),
761 "Failed to initialize the Identity.");
763 info = signon_identity_info_new ();
764 signon_identity_info_set_method (info, "ssotest", ssotest_mechanisms);
765 signon_identity_info_set_owner_from_values (info, "someone", "else");
766 signon_identity_info_access_control_list_append (info,
767 signon_security_context_new_from_values ("*", "*"));
768 signon_identity_info_set_username (info, "Nice user");
770 signon_identity_store_credentials_with_info (idty,
772 test_auth_session_process_after_store_start_session,
776 g_object_unref (idty);
777 signon_identity_info_free (info);
781 static GHashTable *create_methods_hashtable()
783 gchar *mechanisms[] = {
790 GHashTable *methods = g_hash_table_new_full (g_str_hash, g_str_equal, g_free,
791 (GDestroyNotify)g_strfreev);
793 g_hash_table_insert (methods, g_strdup("method1"), g_strdupv(mechanisms));
794 g_hash_table_insert (methods, g_strdup("method2"), g_strdupv(mechanisms));
795 g_hash_table_insert (methods, g_strdup("method3"), g_strdupv(mechanisms));
801 identity_registeration_timeout_cb (gpointer data)
803 g_debug("%s", G_STRFUNC);
808 START_TEST(test_get_existing_identity)
810 g_debug("%s", G_STRFUNC);
811 guint id = new_identity();
813 fail_unless (id != 0);
815 identity = signon_identity_new_from_db (id);
817 fail_unless (identity != NULL);
818 fail_unless (SIGNON_IS_IDENTITY (identity),
819 "Failed to initialize the Identity.");
821 g_timeout_add (1000, identity_registeration_timeout_cb, identity);
824 const GError *error = NULL;
825 error = signon_identity_get_last_error(identity);
826 fail_unless (error == NULL);
830 START_TEST(test_get_nonexisting_identity)
832 g_debug("%s", G_STRFUNC);
833 identity = signon_identity_new_from_db (G_MAXINT);
835 fail_unless (identity != NULL);
836 fail_unless (SIGNON_IS_IDENTITY (identity),
837 "Failed to initialize the Identity.");
839 g_timeout_add (1000, identity_registeration_timeout_cb, identity);
842 const GError *error = NULL;
843 error = signon_identity_get_last_error(identity);
844 fail_unless (error != NULL);
846 fail_unless (error->domain == SIGNON_ERROR);
847 fail_unless (error->code == SIGNON_ERROR_IDENTITY_NOT_FOUND);
851 static void store_credentials_identity_cb(SignonIdentity *self,
858 g_warning ("%s %d: %s", G_STRFUNC, __LINE__, error->message);
862 fail_unless (id > 0);
864 if (user_data != NULL)
866 gint *last_id = (gint *)user_data;
867 g_warning ("%s (prev_id vs new_id): %d vs %d", G_STRFUNC, *last_id, id);
869 fail_unless (id == (*last_id) + 1);
873 /* Wait some time to ensure that the info-updated signals are
876 g_timeout_add_seconds (2, test_quit_main_loop_cb, main_loop);
879 START_TEST(test_store_credentials_identity)
881 g_debug("%s", G_STRFUNC);
882 SignonIdentity *idty = signon_identity_new();
883 fail_unless (idty != NULL);
884 fail_unless (SIGNON_IS_IDENTITY (idty),
885 "Failed to initialize the Identity.");
887 gint last_id = new_identity();
889 GHashTable *methods = create_methods_hashtable();
891 signon_identity_store_credentials_with_args (idty,
901 store_credentials_identity_cb,
903 g_hash_table_destroy (methods);
905 g_timeout_add (1000, test_quit_main_loop_cb, idty);
908 g_object_unref(idty);
912 static void identity_remove_cb(SignonIdentity *self, const GError *error, gpointer user_data)
915 g_warning (" %s ", __func__);
918 g_warning ("Error: %s ", error->message);
919 fail_if (user_data == NULL, "There should be no error in callback");
923 g_warning ("No error");
924 fail_if (user_data != NULL, "The callback must return an error");
930 START_TEST(test_remove_identity)
932 g_debug("%s", G_STRFUNC);
933 SignonIdentity *idty = signon_identity_new ();
934 fail_unless (idty != NULL);
935 fail_unless (SIGNON_IS_IDENTITY (idty),
936 "Failed to initialize the Identity.");
939 * Try to remove non-stored identity
941 signon_identity_remove(idty, identity_remove_cb, NULL);
945 * Try to remove existing identy
948 gint id = new_identity();
949 SignonIdentity *idty2 = signon_identity_new_from_db (id);
951 signon_identity_remove(idty2, identity_remove_cb, NULL);
955 * Try to remove already removed
958 signon_identity_remove(idty2, identity_remove_cb, GINT_TO_POINTER(TRUE));
960 g_object_unref (idty);
961 g_object_unref (idty2);
965 static gboolean _contains(gchar **mechs, gchar *mech)
967 gboolean present = FALSE;
969 while (mechs[i] != NULL)
971 if (g_strcmp0 (mech, mechs[i]) == 0) present = TRUE;
977 static void identity_info_cb(SignonIdentity *self, const SignonIdentityInfo *info, const GError *error, gpointer user_data)
981 g_warning ("%s: Error: %s ", __func__, error->message);
982 fail_if (info != NULL, "Error: %s ", error->message);
987 g_warning ("No error");
989 SignonIdentityInfo **pattern_ptr = (SignonIdentityInfo **)user_data;
990 SignonIdentityInfo *pattern = NULL;
993 pattern = (*pattern_ptr);
996 fail_unless (info == NULL, "The info must be NULL");
999 fail_unless (info != NULL, "The info must be non-null");
1000 fail_unless (g_strcmp0 (signon_identity_info_get_username(info),
1001 signon_identity_info_get_username(pattern)) == 0, "The info has wrong username");
1002 fail_unless (g_strcmp0 (signon_identity_info_get_caption(info),
1003 signon_identity_info_get_caption(pattern)) == 0, "The info has wrong caption");
1005 GHashTable *methods = (GHashTable *)signon_identity_info_get_methods (info);
1006 gchar **mechs1 = g_hash_table_lookup (methods, "method1");
1007 gchar **mechs2 = g_hash_table_lookup (methods, "method2");
1008 gchar **mechs3 = g_hash_table_lookup (methods, "method3");
1010 fail_unless (g_strv_length (mechs1) == 3);
1011 fail_unless (g_strv_length (mechs2) == 3);
1012 fail_unless (g_strv_length (mechs3) == 3);
1014 fail_unless (_contains(mechs1, "mechanism1"));
1015 fail_unless (_contains(mechs1, "mechanism2"));
1016 fail_unless (_contains(mechs1, "mechanism3"));
1018 fail_unless (_contains(mechs2, "mechanism1"));
1019 fail_unless (_contains(mechs2, "mechanism2"));
1020 fail_unless (_contains(mechs2, "mechanism3"));
1022 fail_unless (_contains(mechs3, "mechanism1"));
1023 fail_unless (_contains(mechs3, "mechanism2"));
1024 fail_unless (_contains(mechs3, "mechanism3"));
1029 signon_identity_info_free (pattern);
1030 *pattern_ptr = signon_identity_info_copy (info);
1036 static SignonIdentityInfo *create_standard_info()
1038 GHashTable *methods;
1040 g_debug("%s", G_STRFUNC);
1042 SignonIdentityInfo *info = signon_identity_info_new ();
1044 methods = g_hash_table_new (g_str_hash, g_str_equal);
1045 g_hash_table_insert (methods, "ssotest", ssotest_mechanisms);
1046 signon_identity_info_set_methods (info, methods);
1047 g_hash_table_destroy (methods);
1049 signon_identity_info_set_owner_from_values (info, "", "");
1050 signon_identity_info_access_control_list_append (info,
1051 signon_security_context_new_from_values ("*", "*"));
1052 signon_identity_info_set_username (info, "James Bond");
1053 signon_identity_info_set_secret (info, "007", TRUE);
1054 signon_identity_info_set_caption (info, "MI-6");
1056 gchar *mechanisms[] = {
1063 signon_identity_info_set_method (info, "method1", (const gchar **)mechanisms);
1064 signon_identity_info_set_method (info, "method2", (const gchar **)mechanisms);
1065 signon_identity_info_set_method (info, "method3", (const gchar **)mechanisms);
1070 START_TEST(test_info_identity)
1072 g_debug("%s", G_STRFUNC);
1073 SignonIdentity *idty = signon_identity_new ();
1074 fail_unless (idty != NULL);
1075 fail_unless (SIGNON_IS_IDENTITY (idty),
1076 "Failed to initialize the Identity.");
1078 SignonIdentityInfo *info = NULL;
1081 * Try to get_info for non-stored idetnity
1083 signon_identity_query_info (idty, identity_info_cb, &info);
1086 GHashTable *methods = create_methods_hashtable();
1087 signon_identity_store_credentials_with_args (idty,
1097 store_credentials_identity_cb,
1100 g_hash_table_destroy (methods);
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);
1121 gint id = signon_identity_info_get_id (info);
1122 fail_unless (id != 0);
1123 SignonIdentity *idty2 = signon_identity_new_from_db (id);
1125 signon_identity_query_info (idty2, identity_info_cb, &info);
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,
1141 signon_identity_query_info (idty, identity_info_cb, &info);
1145 * Try to remove existing identity and
1146 * have a look what will happen
1148 signon_identity_remove(idty2, identity_remove_cb, NULL);
1152 * no main_loops required as
1153 * the callback is executed immediately
1155 signon_identity_query_info (idty2, identity_info_cb, NULL);
1156 signon_identity_query_info (idty, identity_info_cb, NULL);
1158 signon_identity_info_free (info);
1159 g_object_unref (idty);
1160 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");
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 gboolean as1_destroyed = FALSE, as2_destroyed = FALSE;
1187 g_debug("%s", G_STRFUNC);
1188 SignonIdentity *idty = signon_identity_new ();
1189 fail_unless (idty != NULL);
1190 fail_unless (SIGNON_IS_IDENTITY (idty),
1191 "Failed to initialize the Identity.");
1193 SignonIdentityInfo *info = create_standard_info();
1195 signon_identity_store_credentials_with_info (idty,
1197 store_credentials_identity_cb,
1200 signon_identity_query_info (idty, identity_info_cb, &info);
1203 gint id = signon_identity_info_get_id (info);
1204 SignonIdentity *idty2 = signon_identity_new_from_db (id);
1206 /* wait some more time to ensure that the object gets registered */
1207 g_timeout_add_seconds (2, test_quit_main_loop_cb, main_loop);
1210 signon_identity_info_free (info);
1214 SignonAuthSession *as1 = signon_identity_create_session (idty,
1217 fail_unless (as1 != NULL, "cannot create AuthSession");
1219 SignonAuthSession *as2 = signon_identity_create_session (idty2,
1222 fail_unless (as2 != NULL, "cannot create AuthSession");
1226 g_signal_connect (idty, "signout",
1227 G_CALLBACK(identity_signout_signal_cb), &counter);
1228 g_signal_connect (idty2, "signout",
1229 G_CALLBACK(identity_signout_signal_cb), &counter);
1231 g_object_weak_ref (G_OBJECT (as1), _on_auth_session_destroyed,
1233 g_object_weak_ref (G_OBJECT (as2), _on_auth_session_destroyed,
1235 signon_identity_signout (idty, identity_signout_cb, NULL);
1238 fail_unless (counter == 2, "Lost some of SIGNOUT signals");
1239 fail_if (as1_destroyed == FALSE, "Authsession1 was not destroyed after signout");
1240 fail_if (as2_destroyed == FALSE, "Authsession2 was not destroyed after signout");
1242 g_object_unref (idty);
1243 g_object_unref (idty2);
1247 START_TEST(test_unregistered_identity)
1249 g_debug("%s", G_STRFUNC);
1250 SignonIdentity *idty = signon_identity_new ();
1251 fail_unless (idty != NULL);
1252 fail_unless (SIGNON_IS_IDENTITY (idty),
1253 "Failed to initialize the Identity.");
1255 SignonIdentityInfo *info = create_standard_info();
1257 signon_identity_store_credentials_with_info (idty,
1259 store_credentials_identity_cb,
1264 * give the time for identity to became idle
1266 sleep(SIGNOND_IDLE_TIMEOUT);
1267 SignonIdentity *idty2 = signon_identity_new ();
1270 * give time to handle unregistered signal
1272 g_timeout_add_seconds (5, test_quit_main_loop_cb, main_loop);
1274 signon_identity_query_info (idty, identity_info_cb, &info);
1277 signon_identity_info_free (info);
1278 g_object_unref (idty);
1279 g_object_unref (idty2);
1283 START_TEST(test_unregistered_auth_session)
1285 g_debug("%s", G_STRFUNC);
1286 SignonIdentity *idty = signon_identity_new ();
1287 fail_unless (idty != NULL);
1288 fail_unless (SIGNON_IS_IDENTITY (idty),
1289 "Failed to initialize the Identity.");
1292 SignonAuthSession *as = signon_identity_create_session(idty,
1295 /* give time to register the objects */
1296 g_timeout_add_seconds (2, test_quit_main_loop_cb, main_loop);
1300 * give the time for identity to became idle
1302 sleep(SIGNOND_IDLE_TIMEOUT);
1303 SignonIdentity *idty2 = signon_identity_new ();
1306 * give time to handle unregistered signal
1308 g_timeout_add_seconds (5, test_quit_main_loop_cb, main_loop);
1313 patterns[0] = g_strdup("mech1");
1314 patterns[1] = g_strdup("mech2");
1315 patterns[2] = g_strdup("mech3");
1318 signon_auth_session_query_available_mechanisms(as,
1319 (const gchar**)patterns,
1320 test_auth_session_query_mechanisms_cb,
1321 (gpointer)patterns);
1324 g_object_unref (as);
1325 g_object_unref (idty);
1326 g_object_unref (idty2);
1328 g_free (patterns[0]);
1329 g_free (patterns[1]);
1330 g_free (patterns[2]);
1331 g_free (patterns[3]);
1335 void free_identity_info_cb (gpointer data)
1337 SignonIdentityInfo *info;
1339 signon_identity_info_free (info);
1342 void query_identities_cb (SignonAuthService *auth_service,
1343 SignonIdentityList *identity_list, const GError *error, gpointer user_data)
1345 SignonIdentityList *iter = identity_list;
1347 while (iter && !error)
1349 SignonIdentityInfo *info = (SignonIdentityInfo *) iter->data;
1350 const gchar *caption = signon_identity_info_get_caption (info);
1352 g_print ("\tid=%d username='%s' caption='%s'\n",
1353 signon_identity_info_get_id (info),
1354 signon_identity_info_get_username (info),
1357 fail_unless (g_strcmp0 (caption, "MI-6") == 0,
1358 "Wrong caption in identity");
1360 iter = g_list_next (iter);
1362 g_list_free_full (identity_list, free_identity_info_cb);
1364 fail_unless (error == NULL, "There should be no error in callback");
1368 START_TEST(test_query_identities)
1370 g_debug("%s", G_STRFUNC);
1372 SignonAuthService *asrv = signon_auth_service_new ();
1374 signon_auth_service_query_identities (asrv, NULL, NULL, query_identities_cb, NULL);
1376 g_timeout_add_seconds (5, test_quit_main_loop_cb, main_loop);
1379 g_object_unref (asrv);
1384 test_regression_unref_process_cb (SignonAuthSession *self,
1386 const GError *error,
1393 g_warning ("%s: %s", G_STRFUNC, error->message);
1398 fail_unless (reply != NULL, "The result is empty");
1400 fail_unless (g_strcmp0 (user_data, "Hi there!") == 0,
1401 "Didn't get expected user_data");
1403 v_string = g_hash_table_lookup(reply, "James");
1404 fail_unless (v_string != 0);
1405 fail_unless (g_strcmp0 (g_value_get_string (v_string), "Bond") == 0,
1406 "Wrong reply data");
1408 g_hash_table_destroy (reply);
1410 /* The next line is actually the regression we want to test */
1411 g_object_unref (self);
1416 START_TEST(test_regression_unref)
1418 SignonIdentity *idty;
1419 SignonAuthSession *auth_session;
1420 GHashTable *session_data;
1421 GError *error = NULL;
1422 GValue v_string = G_VALUE_INIT;
1423 gchar *test_msg = g_strdup ("Hi there!");
1425 g_debug ("%s", G_STRFUNC);
1427 guint id = new_identity();
1428 fail_unless (id != 0);
1429 idty = signon_identity_new_from_db (id);
1431 fail_unless (idty != NULL);
1432 auth_session = signon_auth_session_new_for_identity (idty, "ssotest",
1434 fail_unless (auth_session != NULL);
1436 session_data = g_hash_table_new (g_str_hash, g_str_equal);
1437 g_value_init (&v_string, G_TYPE_STRING);
1438 g_value_set_static_string (&v_string, "Bond");
1439 g_hash_table_insert (session_data, "James", &v_string);
1442 signon_auth_session_process (auth_session,
1445 test_regression_unref_process_cb,
1450 g_object_unref (idty);
1451 g_hash_table_unref (session_data);
1459 Suite *s = suite_create ("signon-glib");
1461 /* Core test case */
1462 TCase * tc_core = tcase_create("Core");
1463 tcase_add_checked_fixture (tc_core, _setup, _teardown);
1466 * 18 minutes timeout
1468 tcase_set_timeout(tc_core, 1080);
1469 tcase_add_test (tc_core, test_init);
1470 tcase_add_test (tc_core, test_query_methods);
1472 tcase_add_test (tc_core, test_query_mechanisms);
1473 tcase_add_test (tc_core, test_get_existing_identity);
1474 tcase_add_test (tc_core, test_get_nonexisting_identity);
1476 tcase_add_test (tc_core, test_auth_session_creation);
1477 tcase_add_test (tc_core, test_auth_session_query_mechanisms);
1478 tcase_add_test (tc_core, test_auth_session_query_mechanisms_nonexisting);
1479 tcase_add_test (tc_core, test_auth_session_process);
1480 tcase_add_test (tc_core, test_auth_session_process_failure);
1481 tcase_add_test (tc_core, test_auth_session_process_after_store);
1482 tcase_add_test (tc_core, test_store_credentials_identity);
1483 tcase_add_test (tc_core, test_remove_identity);
1484 tcase_add_test (tc_core, test_info_identity);
1486 tcase_add_test (tc_core, test_query_identities);
1488 tcase_add_test (tc_core, test_signout_identity);
1489 tcase_add_test (tc_core, test_unregistered_identity);
1490 tcase_add_test (tc_core, test_unregistered_auth_session);
1492 tcase_add_test (tc_core, test_regression_unref);
1493 suite_add_tcase (s, tc_core);
1501 Suite * s = signon_suite();
1502 SRunner * sr = srunner_create(s);
1504 srunner_set_xml(sr, "/tmp/result.xml");
1505 srunner_run_all(sr, CK_NORMAL);
1506 number_failed = srunner_ntests_failed(sr);
1509 return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
1512 /* vim: set ai et tw=75 ts=4 sw=4: */