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);
75 #if !GLIB_CHECK_VERSION (2, 36, 0)
78 if (main_loop == NULL) {
79 main_loop = g_main_loop_new (NULL, FALSE);
88 g_object_unref (auth_service);
94 g_object_unref (identity);
100 g_main_loop_unref (main_loop);
106 new_identity_store_credentials_cb(
107 SignonIdentity *self,
112 gint *new_id = user_data;
116 g_warning ("%s %d: %s", G_STRFUNC, __LINE__, error->message);
120 fail_unless (id > 0);
130 SignonIdentity *idty;
134 idty = signon_identity_new ();
135 fail_unless (SIGNON_IS_IDENTITY (idty));
136 methods = g_hash_table_new (g_str_hash, g_str_equal);
137 g_hash_table_insert (methods, "ssotest", ssotest_mechanisms);
138 signon_identity_store_credentials_with_args (idty,
148 new_identity_store_credentials_cb,
150 g_hash_table_destroy (methods);
155 g_object_unref (idty);
161 START_TEST(test_init)
163 g_debug("%s", G_STRFUNC);
164 auth_service = signon_auth_service_new ();
166 fail_unless (SIGNON_IS_AUTH_SERVICE (auth_service),
167 "Failed to initialize the AuthService.");
172 signon_query_methods_cb (SignonAuthService *auth_service, gchar **methods,
173 GError *error, gpointer user_data)
177 g_warning ("%s: %s", G_STRFUNC, error->message);
179 if (methods) g_strfreev (methods);
183 gboolean has_ssotest = FALSE;
184 gchar **pmethods = methods;
186 fail_unless (g_strcmp0 (user_data, "Hello") == 0, "Got wrong string");
187 fail_unless (methods != NULL, "The methods does not exist");
191 if (g_strcmp0 (*pmethods, "ssotest") == 0)
198 g_strfreev (methods);
199 fail_unless (has_ssotest, "ssotest method does not exist");
204 START_TEST(test_query_methods)
206 g_debug("%s", G_STRFUNC);
208 auth_service = signon_auth_service_new ();
210 fail_unless (SIGNON_IS_AUTH_SERVICE (auth_service),
211 "Failed to initialize the AuthService.");
213 signon_auth_service_query_methods (auth_service, (SignonQueryMethodsCb)signon_query_methods_cb, "Hello");
219 signon_query_mechanisms_cb (SignonAuthService *auth_service, gchar *method,
220 gchar **mechanisms, GError *error, gpointer user_data)
224 g_warning ("%s: %s", G_STRFUNC, error->message);
225 if (mechanisms) g_strfreev (mechanisms);
230 gboolean has_mech1 = FALSE;
231 gboolean has_mech2 = FALSE;
232 gboolean has_mech3 = FALSE;
233 gchar **pmechanisms = mechanisms;
235 fail_unless (strcmp (user_data, "Hello") == 0, "Got wrong string");
236 fail_unless (mechanisms != NULL, "The mechanisms does not exist");
240 if (g_strcmp0 (*pmechanisms, "mech1") == 0)
243 if (g_strcmp0 (*pmechanisms, "mech2") == 0)
246 if (g_strcmp0 (*pmechanisms, "mech3") == 0)
251 g_strfreev (mechanisms);
252 fail_unless (has_mech1, "mech1 mechanism does not exist");
253 fail_unless (has_mech2, "mech2 mechanism does not exist");
254 fail_unless (has_mech3, "mech3 mechanism does not exist");
260 signon_query_mechanisms_cb_fail (SignonAuthService *auth_service,
263 GError *error, gpointer user_data)
265 fail_unless (error != NULL);
266 fail_unless (mechanisms == NULL);
267 fail_unless (error->domain == SIGNON_ERROR);
268 fail_unless (error->code == SIGNON_ERROR_METHOD_NOT_KNOWN);
269 if (mechanisms) g_strfreev (mechanisms);
273 START_TEST(test_query_mechanisms)
275 g_debug("%s", G_STRFUNC);
276 auth_service = signon_auth_service_new ();
278 fail_unless (SIGNON_IS_AUTH_SERVICE (auth_service),
279 "Failed to initialize the AuthService.");
281 signon_auth_service_query_mechanisms (auth_service,
283 (SignonQueryMechanismCb)signon_query_mechanisms_cb,
288 /* Test a non existing method */
289 signon_auth_service_query_mechanisms (auth_service,
291 (SignonQueryMechanismCb)signon_query_mechanisms_cb_fail,
299 test_quit_main_loop_cb (gpointer data)
306 test_auth_session_query_mechanisms_cb (SignonAuthSession *self,
313 g_warning ("%s: %s", G_STRFUNC, error->message);
318 fail_unless (mechanisms != NULL, "The mechanisms does not exist");
320 gchar** patterns = (gchar**)user_data;
322 int i = g_strv_length(mechanisms);
323 int x = g_strv_length(patterns);
324 fail_unless( i == x, "The number of obtained methods is wrong: %d vs %d", i, x);
328 gchar* pattern = patterns[--i];
329 fail_unless(g_strcmp0(pattern, mechanisms[i]) == 0, "The obtained mechanism differs from predefined pattern: %s vs %s", mechanisms[i], pattern);
332 g_strfreev(mechanisms);
336 START_TEST(test_auth_session_query_mechanisms)
340 g_debug("%s", G_STRFUNC);
341 SignonIdentity *idty = signon_identity_new();
342 fail_unless (idty != NULL, "Cannot create Iddentity object");
344 SignonAuthSession *auth_session = signon_identity_create_session(idty,
347 fail_unless (auth_session != NULL, "Cannot create AuthSession object");
352 patterns[0] = g_strdup("mech1");
353 patterns[1] = g_strdup("mech2");
354 patterns[2] = g_strdup("mech3");
357 signon_auth_session_query_available_mechanisms(auth_session,
358 (const gchar**)patterns,
359 test_auth_session_query_mechanisms_cb,
367 signon_auth_session_query_available_mechanisms(auth_session,
368 (const gchar**)patterns,
369 test_auth_session_query_mechanisms_cb,
377 signon_auth_session_query_available_mechanisms(auth_session,
378 (const gchar**)patterns,
379 test_auth_session_query_mechanisms_cb,
385 g_object_unref (auth_session);
386 g_object_unref (idty);
392 test_auth_session_query_mechanisms_nonexisting_cb (SignonAuthSession *self,
404 g_warning ("%s: %s", G_STRFUNC, error->message);
408 START_TEST(test_auth_session_query_mechanisms_nonexisting)
412 g_debug("%s", G_STRFUNC);
413 SignonIdentity *idty = signon_identity_new();
414 fail_unless (idty != NULL, "Cannot create Iddentity object");
416 SignonAuthSession *auth_session = signon_identity_create_session(idty,
419 fail_unless (auth_session != NULL, "Cannot create AuthSession object");
424 patterns[0] = g_strdup("mech1");
425 patterns[1] = g_strdup("mech2");
426 patterns[2] = g_strdup("mech3");
429 signon_auth_session_query_available_mechanisms(auth_session,
430 (const gchar**)patterns,
431 test_auth_session_query_mechanisms_nonexisting_cb,
439 g_object_unref (auth_session);
440 g_object_unref (idty);
446 test_auth_session_states_cb (SignonAuthSession *self,
451 gint *state_counter = (gint *)user_data;
456 test_auth_session_process_cb (SignonAuthSession *self,
457 GHashTable *sessionData,
463 g_warning ("%s: %s", G_STRFUNC, error->message);
468 fail_unless (sessionData != NULL, "The result is empty");
470 gchar* usernameKey = g_strdup(SIGNON_SESSION_DATA_USERNAME);
471 GValue* usernameVa = (GValue*)g_hash_table_lookup(sessionData, usernameKey);
473 gchar* realmKey = g_strdup(SIGNON_SESSION_DATA_REALM);
474 GValue* realmVa = (GValue*)g_hash_table_lookup(sessionData, realmKey);
476 fail_unless(g_strcmp0(g_value_get_string(usernameVa), "test_username") == 0, "Wrong value of username");
477 fail_unless(g_strcmp0(g_value_get_string(realmVa), "testRealm_after_test") == 0, "Wrong value of realm");
479 g_hash_table_destroy(sessionData);
488 _on_identity_destroyed (gpointer data, GObject *obj)
494 _on_auth_session_destroyed (gpointer data, GObject *obj)
496 gboolean *is_destroyed = (gboolean *)data;
497 *is_destroyed = TRUE;
500 START_TEST(test_auth_session_creation)
503 gboolean auth_sess_destroyed = FALSE;
505 g_debug("%s", G_STRFUNC);
506 SignonIdentity *idty = signon_identity_new();
507 fail_unless (idty != NULL, "Cannot create Identity object");
509 SignonAuthSession *auth_session = signon_identity_create_session(idty,
513 fail_unless (auth_session != NULL, "Cannot create AuthSession object");
515 id_destroyed = FALSE;
516 g_object_weak_ref (G_OBJECT (idty), _on_identity_destroyed, NULL);
517 g_object_weak_ref (G_OBJECT (auth_session), _on_auth_session_destroyed,
518 &auth_sess_destroyed);
520 g_object_unref (idty);
521 fail_unless (id_destroyed == FALSE, "Identity must stay untill all its session are not destroyed");
523 g_object_unref (auth_session);
525 fail_if (auth_sess_destroyed == FALSE, "AuthSession is not synchronized with parent Identity");
526 fail_if (id_destroyed == FALSE, "Identity is not synchronized with its AuthSession");
533 START_TEST(test_auth_session_process)
535 gint state_counter = 0;
538 g_debug("%s", G_STRFUNC);
539 SignonIdentity *idty = signon_identity_new();
540 fail_unless (idty != NULL, "Cannot create Iddentity object");
542 SignonAuthSession *auth_session = signon_identity_create_session(idty,
546 fail_unless (auth_session != NULL, "Cannot create AuthSession object");
550 g_signal_connect(auth_session, "state-changed",
551 G_CALLBACK(test_auth_session_states_cb), &state_counter);
553 GHashTable* sessionData = g_hash_table_new(g_str_hash,
555 GValue* usernameVa = g_new0(GValue, 1);
556 gchar* usernameKey = g_strdup(SIGNON_SESSION_DATA_USERNAME);
557 g_value_init (usernameVa, G_TYPE_STRING);
558 g_value_set_static_string(usernameVa, "test_username");
560 g_hash_table_insert (sessionData,
564 GValue* passwordVa = g_new0(GValue, 1);
565 gchar* passwordKey = g_strdup(SIGNON_SESSION_DATA_SECRET);
567 g_value_init (passwordVa, G_TYPE_STRING);
568 g_value_set_static_string(passwordVa, "test_username");
570 g_hash_table_insert (sessionData,
574 signon_auth_session_process(auth_session,
577 test_auth_session_process_cb,
582 fail_unless (state_counter == 12, "Wrong numer of state change signals: %d", state_counter);
585 signon_auth_session_process(auth_session,
588 test_auth_session_process_cb,
592 fail_unless (state_counter == 12, "Wrong numer of state change signals: %d", state_counter);
595 signon_auth_session_process(auth_session,
598 test_auth_session_process_cb,
602 fail_unless (state_counter == 12, "Wrong numer of state change signals: %d", state_counter);
605 g_object_unref (auth_session);
606 g_object_unref (idty);
608 g_value_unset(usernameVa);
612 g_value_unset(passwordVa);
616 g_hash_table_unref (sessionData);
621 test_auth_session_process_failure_cb (GObject *source_object,
625 g_debug("%s", G_STRFUNC);
626 SignonAuthSession *auth_session = SIGNON_AUTH_SESSION (source_object);
628 GError **error = user_data;
630 fail_unless (SIGNON_IS_AUTH_SESSION (source_object));
632 v_reply = signon_auth_session_process_finish (auth_session, res, error);
633 fail_unless (v_reply == NULL);
638 START_TEST(test_auth_session_process_failure)
640 SignonIdentity *idty;
641 SignonAuthSession *auth_session;
642 GVariantBuilder builder;
643 GVariant *session_data;
644 GError *error = NULL;
646 g_debug("%s", G_STRFUNC);
648 guint id = new_identity();
650 fail_unless (id != 0);
652 idty = signon_identity_new_from_db (id);
654 fail_unless (idty != NULL, "Cannot create Identity object");
655 auth_session = signon_auth_session_new_for_identity (idty,
658 fail_unless (auth_session != NULL, "Cannot create AuthSession object");
659 fail_unless (error == NULL);
661 g_variant_builder_init (&builder, G_VARIANT_TYPE_VARDICT);
662 g_variant_builder_add (&builder, "{sv}",
663 "key", g_variant_new_string ("value"));
665 session_data = g_variant_builder_end (&builder);
667 signon_auth_session_process_async (auth_session,
671 test_auth_session_process_failure_cb,
674 fail_unless (error != NULL);
675 fail_unless (error->domain == SIGNON_ERROR);
676 fail_unless (error->code == SIGNON_ERROR_MECHANISM_NOT_AVAILABLE);
677 g_error_free (error);
679 g_object_unref (auth_session);
680 g_object_unref (idty);
685 test_auth_session_process_after_store_cb (SignonAuthSession *self,
694 fail("Got error: %s", error->message);
699 fail_unless (reply != NULL, "The result is empty");
701 v_username = g_hash_table_lookup(reply,
702 SIGNON_SESSION_DATA_USERNAME);
704 fail_unless (g_strcmp0 (g_value_get_string (v_username), "Nice user") == 0,
705 "Wrong value of username");
707 g_hash_table_unref (reply);
708 g_object_unref (self);
714 test_auth_session_process_after_store_start_session(SignonIdentity *self,
723 g_warning ("%s %d: %s", G_STRFUNC, __LINE__, error->message);
729 fail_unless (id > 0);
731 SignonAuthSession *auth_session =
732 signon_identity_create_session (self,
736 fail_unless (auth_session != NULL, "Cannot create AuthSession object");
739 fail ("Got error: %s", err->message);
740 g_clear_error (&err);
743 GHashTable *session_data = g_hash_table_new (g_str_hash,
746 signon_auth_session_process (auth_session,
749 test_auth_session_process_after_store_cb,
751 g_hash_table_unref (session_data);
754 START_TEST(test_auth_session_process_after_store)
756 SignonIdentityInfo *info;
757 SignonIdentity *idty;
759 g_debug("%s", G_STRFUNC);
761 idty = signon_identity_new ();
762 fail_unless (SIGNON_IS_IDENTITY (idty),
763 "Failed to initialize the Identity.");
765 info = signon_identity_info_new ();
766 signon_identity_info_set_method (info, "ssotest", ssotest_mechanisms);
767 signon_identity_info_set_owner_from_values (info, "someone", "else");
768 signon_identity_info_access_control_list_append (info,
769 signon_security_context_new_from_values ("*", "*"));
770 signon_identity_info_set_username (info, "Nice user");
772 signon_identity_store_credentials_with_info (idty,
774 test_auth_session_process_after_store_start_session,
778 g_object_unref (idty);
779 signon_identity_info_free (info);
783 static GHashTable *create_methods_hashtable()
785 gchar *mechanisms[] = {
792 GHashTable *methods = g_hash_table_new_full (g_str_hash, g_str_equal, g_free,
793 (GDestroyNotify)g_strfreev);
795 g_hash_table_insert (methods, g_strdup("method1"), g_strdupv(mechanisms));
796 g_hash_table_insert (methods, g_strdup("method2"), g_strdupv(mechanisms));
797 g_hash_table_insert (methods, g_strdup("method3"), g_strdupv(mechanisms));
803 identity_registeration_timeout_cb (gpointer data)
805 g_debug("%s", G_STRFUNC);
810 START_TEST(test_get_existing_identity)
812 g_debug("%s", G_STRFUNC);
813 guint id = new_identity();
815 fail_unless (id != 0);
817 identity = signon_identity_new_from_db (id);
819 fail_unless (identity != NULL);
820 fail_unless (SIGNON_IS_IDENTITY (identity),
821 "Failed to initialize the Identity.");
823 g_timeout_add (1000, identity_registeration_timeout_cb, identity);
826 const GError *error = NULL;
827 error = signon_identity_get_last_error(identity);
828 fail_unless (error == NULL);
832 START_TEST(test_get_nonexisting_identity)
834 g_debug("%s", G_STRFUNC);
835 identity = signon_identity_new_from_db (G_MAXINT);
837 fail_unless (identity != NULL);
838 fail_unless (SIGNON_IS_IDENTITY (identity),
839 "Failed to initialize the Identity.");
841 g_timeout_add (1000, identity_registeration_timeout_cb, identity);
844 const GError *error = NULL;
845 error = signon_identity_get_last_error(identity);
846 fail_unless (error != NULL);
848 fail_unless (error->domain == SIGNON_ERROR);
849 fail_unless (error->code == SIGNON_ERROR_IDENTITY_NOT_FOUND);
853 static void store_credentials_identity_cb(SignonIdentity *self,
860 g_warning ("%s %d: %s", G_STRFUNC, __LINE__, error->message);
864 fail_unless (id > 0);
866 if (user_data != NULL)
868 gint *last_id = (gint *)user_data;
869 g_warning ("%s (prev_id vs new_id): %d vs %d", G_STRFUNC, *last_id, id);
871 fail_unless (id == (*last_id) + 1);
875 /* Wait some time to ensure that the info-updated signals are
878 g_timeout_add_seconds (2, test_quit_main_loop_cb, main_loop);
881 START_TEST(test_store_credentials_identity)
883 g_debug("%s", G_STRFUNC);
884 SignonIdentity *idty = signon_identity_new();
885 fail_unless (idty != NULL);
886 fail_unless (SIGNON_IS_IDENTITY (idty),
887 "Failed to initialize the Identity.");
889 gint last_id = new_identity();
891 GHashTable *methods = create_methods_hashtable();
893 signon_identity_store_credentials_with_args (idty,
903 store_credentials_identity_cb,
905 g_hash_table_destroy (methods);
907 g_timeout_add (1000, test_quit_main_loop_cb, idty);
910 g_object_unref(idty);
914 static void identity_remove_cb(SignonIdentity *self, const GError *error, gpointer user_data)
917 g_warning (" %s ", __func__);
920 g_warning ("Error: %s ", error->message);
921 fail_if (user_data == NULL, "There should be no error in callback");
925 g_warning ("No error");
926 fail_if (user_data != NULL, "The callback must return an error");
932 START_TEST(test_remove_identity)
934 g_debug("%s", G_STRFUNC);
935 SignonIdentity *idty = signon_identity_new ();
936 fail_unless (idty != NULL);
937 fail_unless (SIGNON_IS_IDENTITY (idty),
938 "Failed to initialize the Identity.");
941 * Try to remove non-stored identity
943 signon_identity_remove(idty, identity_remove_cb, NULL);
947 * Try to remove existing identy
950 gint id = new_identity();
951 SignonIdentity *idty2 = signon_identity_new_from_db (id);
953 signon_identity_remove(idty2, identity_remove_cb, NULL);
957 * Try to remove already removed
960 signon_identity_remove(idty2, identity_remove_cb, GINT_TO_POINTER(TRUE));
962 g_object_unref (idty);
963 g_object_unref (idty2);
967 static gboolean _contains(gchar **mechs, gchar *mech)
969 gboolean present = FALSE;
971 while (mechs[i] != NULL)
973 if (g_strcmp0 (mech, mechs[i]) == 0) present = TRUE;
979 static void identity_info_cb(SignonIdentity *self, SignonIdentityInfo *info, const GError *error, gpointer user_data)
983 g_warning ("%s: Error: %s ", __func__, error->message);
984 fail_if (info != NULL, "Error: %s ", error->message);
989 g_warning ("No error");
991 SignonIdentityInfo **pattern_ptr = (SignonIdentityInfo **)user_data;
992 SignonIdentityInfo *pattern = NULL;
995 pattern = (*pattern_ptr);
998 fail_unless (info == NULL, "The info must be NULL");
1001 fail_unless (info != NULL, "The info must be non-null");
1002 fail_unless (g_strcmp0 (signon_identity_info_get_username(info),
1003 signon_identity_info_get_username(pattern)) == 0, "The info has wrong username");
1004 fail_unless (g_strcmp0 (signon_identity_info_get_caption(info),
1005 signon_identity_info_get_caption(pattern)) == 0, "The info has wrong caption");
1007 GHashTable *methods = (GHashTable *)signon_identity_info_get_methods (info);
1008 gchar **mechs1 = g_hash_table_lookup (methods, "method1");
1009 gchar **mechs2 = g_hash_table_lookup (methods, "method2");
1010 gchar **mechs3 = g_hash_table_lookup (methods, "method3");
1012 fail_unless (g_strv_length (mechs1) == 3);
1013 fail_unless (g_strv_length (mechs2) == 3);
1014 fail_unless (g_strv_length (mechs3) == 3);
1016 fail_unless (_contains(mechs1, "mechanism1"));
1017 fail_unless (_contains(mechs1, "mechanism2"));
1018 fail_unless (_contains(mechs1, "mechanism3"));
1020 fail_unless (_contains(mechs2, "mechanism1"));
1021 fail_unless (_contains(mechs2, "mechanism2"));
1022 fail_unless (_contains(mechs2, "mechanism3"));
1024 fail_unless (_contains(mechs3, "mechanism1"));
1025 fail_unless (_contains(mechs3, "mechanism2"));
1026 fail_unless (_contains(mechs3, "mechanism3"));
1031 signon_identity_info_free (pattern);
1032 *pattern_ptr = signon_identity_info_copy (info);
1038 static SignonIdentityInfo *create_standard_info()
1040 GHashTable *methods;
1042 g_debug("%s", G_STRFUNC);
1044 SignonIdentityInfo *info = signon_identity_info_new ();
1046 methods = g_hash_table_new (g_str_hash, g_str_equal);
1047 g_hash_table_insert (methods, "ssotest", ssotest_mechanisms);
1048 signon_identity_info_set_methods (info, methods);
1049 g_hash_table_destroy (methods);
1051 signon_identity_info_set_owner_from_values (info, "", "");
1052 signon_identity_info_access_control_list_append (info,
1053 signon_security_context_new_from_values ("*", "*"));
1054 signon_identity_info_set_username (info, "James Bond");
1055 signon_identity_info_set_secret (info, "007", TRUE);
1056 signon_identity_info_set_caption (info, "MI-6");
1058 gchar *mechanisms[] = {
1065 signon_identity_info_set_method (info, "method1", (const gchar **)mechanisms);
1066 signon_identity_info_set_method (info, "method2", (const gchar **)mechanisms);
1067 signon_identity_info_set_method (info, "method3", (const gchar **)mechanisms);
1072 START_TEST(test_info_identity)
1074 g_debug("%s", G_STRFUNC);
1075 SignonIdentity *idty = signon_identity_new ();
1076 fail_unless (idty != NULL);
1077 fail_unless (SIGNON_IS_IDENTITY (idty),
1078 "Failed to initialize the Identity.");
1080 SignonIdentityInfo *info = NULL;
1083 * Try to get_info for non-stored idetnity
1085 signon_identity_query_info (idty, identity_info_cb, &info);
1088 GHashTable *methods = create_methods_hashtable();
1089 signon_identity_store_credentials_with_args (idty,
1099 store_credentials_identity_cb,
1102 g_hash_table_destroy (methods);
1104 info = signon_identity_info_new ();
1105 signon_identity_info_set_username (info, "James Bond");
1106 signon_identity_info_set_secret (info, "007", TRUE);
1107 signon_identity_info_set_caption (info, "MI-6");
1109 gchar *mechanisms[] = {
1116 signon_identity_info_set_method (info, "method1", (const gchar **)mechanisms);
1117 signon_identity_info_set_method (info, "method2", (const gchar **)mechanisms);
1118 signon_identity_info_set_method (info, "method3", (const gchar **)mechanisms);
1120 signon_identity_query_info (idty, identity_info_cb, &info);
1123 gint id = signon_identity_info_get_id (info);
1124 fail_unless (id != 0);
1125 SignonIdentity *idty2 = signon_identity_new_from_db (id);
1127 signon_identity_query_info (idty2, identity_info_cb, &info);
1131 * Try to update one identity and
1132 * have a look what will happen
1134 signon_identity_info_set_username (info, "James Bond_2nd version");
1135 signon_identity_info_set_caption (info, "caption_2nd version");
1137 signon_identity_store_credentials_with_info (idty2,
1139 store_credentials_identity_cb,
1143 signon_identity_query_info (idty, identity_info_cb, &info);
1147 * Try to remove existing identity and
1148 * have a look what will happen
1150 signon_identity_remove(idty2, identity_remove_cb, NULL);
1154 * no main_loops required as
1155 * the callback is executed immediately
1157 signon_identity_query_info (idty2, identity_info_cb, NULL);
1158 signon_identity_query_info (idty, identity_info_cb, NULL);
1160 signon_identity_info_free (info);
1161 g_object_unref (idty);
1162 g_object_unref (idty2);
1166 static void identity_signout_cb (SignonIdentity *self,
1167 const GError *error,
1171 g_warning ("%s: %s", G_STRFUNC, error->message);
1173 g_warning ("%s: No error", G_STRFUNC);
1175 fail_unless (error == NULL, "There should be no error in callback");
1179 static void identity_signout_signal_cb (gpointer instance, gpointer user_data)
1181 gint *incr = (gint *)user_data;
1182 (*incr) = (*incr) + 1;
1183 g_warning ("%s: %d", G_STRFUNC, *incr);
1186 START_TEST(test_signout_identity)
1188 gboolean as1_destroyed = FALSE, as2_destroyed = FALSE;
1189 g_debug("%s", G_STRFUNC);
1190 SignonIdentity *idty = signon_identity_new ();
1191 fail_unless (idty != NULL);
1192 fail_unless (SIGNON_IS_IDENTITY (idty),
1193 "Failed to initialize the Identity.");
1195 SignonIdentityInfo *info = create_standard_info();
1197 signon_identity_store_credentials_with_info (idty,
1199 store_credentials_identity_cb,
1202 signon_identity_query_info (idty, identity_info_cb, &info);
1205 gint id = signon_identity_info_get_id (info);
1206 SignonIdentity *idty2 = signon_identity_new_from_db (id);
1208 /* wait some more time to ensure that the object gets registered */
1209 g_timeout_add_seconds (2, test_quit_main_loop_cb, main_loop);
1212 signon_identity_info_free (info);
1216 SignonAuthSession *as1 = signon_identity_create_session (idty,
1219 fail_unless (as1 != NULL, "cannot create AuthSession");
1221 SignonAuthSession *as2 = signon_identity_create_session (idty2,
1224 fail_unless (as2 != NULL, "cannot create AuthSession");
1228 g_signal_connect (idty, "signout",
1229 G_CALLBACK(identity_signout_signal_cb), &counter);
1230 g_signal_connect (idty2, "signout",
1231 G_CALLBACK(identity_signout_signal_cb), &counter);
1233 g_object_weak_ref (G_OBJECT (as1), _on_auth_session_destroyed,
1235 g_object_weak_ref (G_OBJECT (as2), _on_auth_session_destroyed,
1237 signon_identity_signout (idty, identity_signout_cb, NULL);
1240 fail_unless (counter == 2, "Lost some of SIGNOUT signals");
1241 fail_if (as1_destroyed == FALSE, "Authsession1 was not destroyed after signout");
1242 fail_if (as2_destroyed == FALSE, "Authsession2 was not destroyed after signout");
1244 g_object_unref (idty);
1245 g_object_unref (idty2);
1249 START_TEST(test_unregistered_identity)
1251 g_debug("%s", G_STRFUNC);
1252 SignonIdentity *idty = signon_identity_new ();
1253 fail_unless (idty != NULL);
1254 fail_unless (SIGNON_IS_IDENTITY (idty),
1255 "Failed to initialize the Identity.");
1257 SignonIdentityInfo *info = create_standard_info();
1259 signon_identity_store_credentials_with_info (idty,
1261 store_credentials_identity_cb,
1266 * give the time for identity to became idle
1268 sleep(SIGNOND_IDLE_TIMEOUT);
1269 SignonIdentity *idty2 = signon_identity_new ();
1272 * give time to handle unregistered signal
1274 g_timeout_add_seconds (5, test_quit_main_loop_cb, main_loop);
1276 signon_identity_query_info (idty, identity_info_cb, &info);
1279 signon_identity_info_free (info);
1280 g_object_unref (idty);
1281 g_object_unref (idty2);
1285 START_TEST(test_unregistered_auth_session)
1287 g_debug("%s", G_STRFUNC);
1288 SignonIdentity *idty = signon_identity_new ();
1289 fail_unless (idty != NULL);
1290 fail_unless (SIGNON_IS_IDENTITY (idty),
1291 "Failed to initialize the Identity.");
1294 SignonAuthSession *as = signon_identity_create_session(idty,
1297 /* give time to register the objects */
1298 g_timeout_add_seconds (2, test_quit_main_loop_cb, main_loop);
1302 * give the time for identity to became idle
1304 sleep(SIGNOND_IDLE_TIMEOUT);
1305 SignonIdentity *idty2 = signon_identity_new ();
1308 * give time to handle unregistered signal
1310 g_timeout_add_seconds (5, test_quit_main_loop_cb, main_loop);
1315 patterns[0] = g_strdup("mech1");
1316 patterns[1] = g_strdup("mech2");
1317 patterns[2] = g_strdup("mech3");
1320 signon_auth_session_query_available_mechanisms(as,
1321 (const gchar**)patterns,
1322 test_auth_session_query_mechanisms_cb,
1323 (gpointer)patterns);
1326 g_object_unref (as);
1327 g_object_unref (idty);
1328 g_object_unref (idty2);
1330 g_free (patterns[0]);
1331 g_free (patterns[1]);
1332 g_free (patterns[2]);
1333 g_free (patterns[3]);
1337 void free_identity_info_cb (gpointer data)
1339 SignonIdentityInfo *info;
1341 signon_identity_info_free (info);
1344 void query_identities_cb (SignonAuthService *auth_service,
1345 SignonIdentityList *identity_list, const GError *error, gpointer user_data)
1347 SignonIdentityList *iter = identity_list;
1349 while (iter && !error)
1351 SignonIdentityInfo *info = (SignonIdentityInfo *) iter->data;
1352 const gchar *caption = signon_identity_info_get_caption (info);
1354 g_print ("\tid=%d username='%s' caption='%s'\n",
1355 signon_identity_info_get_id (info),
1356 signon_identity_info_get_username (info),
1359 fail_unless (g_strcmp0 (caption, "MI-6") == 0,
1360 "Wrong caption in identity");
1362 iter = g_list_next (iter);
1364 g_list_free_full (identity_list, free_identity_info_cb);
1366 fail_unless (error == NULL, "There should be no error in callback");
1370 START_TEST(test_query_identities)
1372 g_debug("%s", G_STRFUNC);
1374 SignonAuthService *asrv = signon_auth_service_new ();
1376 signon_auth_service_query_identities (asrv, NULL, NULL, query_identities_cb, NULL);
1378 g_timeout_add_seconds (5, test_quit_main_loop_cb, main_loop);
1381 g_object_unref (asrv);
1386 test_regression_unref_process_cb (SignonAuthSession *self,
1388 const GError *error,
1395 g_warning ("%s: %s", G_STRFUNC, error->message);
1400 fail_unless (reply != NULL, "The result is empty");
1402 fail_unless (g_strcmp0 (user_data, "Hi there!") == 0,
1403 "Didn't get expected user_data");
1405 v_string = g_hash_table_lookup(reply, "James");
1406 fail_unless (v_string != 0);
1407 fail_unless (g_strcmp0 (g_value_get_string (v_string), "Bond") == 0,
1408 "Wrong reply data");
1410 g_hash_table_destroy (reply);
1412 /* The next line is actually the regression we want to test */
1413 g_object_unref (self);
1418 START_TEST(test_regression_unref)
1420 SignonIdentity *idty;
1421 SignonAuthSession *auth_session;
1422 GHashTable *session_data;
1423 GError *error = NULL;
1424 GValue v_string = G_VALUE_INIT;
1425 gchar *test_msg = g_strdup ("Hi there!");
1427 g_debug ("%s", G_STRFUNC);
1429 guint id = new_identity();
1430 fail_unless (id != 0);
1431 idty = signon_identity_new_from_db (id);
1433 fail_unless (idty != NULL);
1434 auth_session = signon_auth_session_new_for_identity (idty, "ssotest",
1436 fail_unless (auth_session != NULL);
1438 session_data = g_hash_table_new (g_str_hash, g_str_equal);
1439 g_value_init (&v_string, G_TYPE_STRING);
1440 g_value_set_static_string (&v_string, "Bond");
1441 g_hash_table_insert (session_data, "James", &v_string);
1444 signon_auth_session_process (auth_session,
1447 test_regression_unref_process_cb,
1452 g_object_unref (idty);
1453 g_hash_table_unref (session_data);
1461 Suite *s = suite_create ("signon-glib");
1463 /* Core test case */
1464 TCase * tc_core = tcase_create("Core");
1465 tcase_add_checked_fixture (tc_core, _setup, _teardown);
1468 * 18 minutes timeout
1470 tcase_set_timeout(tc_core, 1080);
1471 tcase_add_test (tc_core, test_init);
1472 tcase_add_test (tc_core, test_query_methods);
1474 tcase_add_test (tc_core, test_query_mechanisms);
1475 tcase_add_test (tc_core, test_get_existing_identity);
1476 tcase_add_test (tc_core, test_get_nonexisting_identity);
1478 tcase_add_test (tc_core, test_auth_session_creation);
1479 tcase_add_test (tc_core, test_auth_session_query_mechanisms);
1480 tcase_add_test (tc_core, test_auth_session_query_mechanisms_nonexisting);
1481 tcase_add_test (tc_core, test_auth_session_process);
1482 tcase_add_test (tc_core, test_auth_session_process_failure);
1483 tcase_add_test (tc_core, test_auth_session_process_after_store);
1484 tcase_add_test (tc_core, test_store_credentials_identity);
1485 tcase_add_test (tc_core, test_remove_identity);
1486 tcase_add_test (tc_core, test_info_identity);
1488 tcase_add_test (tc_core, test_query_identities);
1490 tcase_add_test (tc_core, test_signout_identity);
1491 tcase_add_test (tc_core, test_unregistered_identity);
1492 tcase_add_test (tc_core, test_unregistered_auth_session);
1494 tcase_add_test (tc_core, test_regression_unref);
1495 suite_add_tcase (s, tc_core);
1503 Suite * s = signon_suite();
1504 SRunner * sr = srunner_create(s);
1506 srunner_set_xml(sr, "/tmp/result.xml");
1507 srunner_run_all(sr, CK_NORMAL);
1508 number_failed = srunner_ntests_failed(sr);
1511 return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
1514 /* vim: set ai et tw=75 ts=4 sw=4: */