Update to upstream 1.0.1
[profile/ivi/gsignond.git] / test / daemon / daemon-test.c
1 /* vi: set et sw=4 ts=4 cino=t0,(0: */
2 /* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 /*
4  * This file is part of gsignond
5  *
6  * Copyright (C) 2012 Intel Corporation.
7  *
8  * Contact: Amarnaht Valluri <amarnath.valluri@linux.intel.com>
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
23  * 02110-1301 USA
24  */
25
26 #include "config.h"
27 #include <check.h>
28 #include <error.h>
29 #include <errno.h>
30 #include <stdlib.h>
31 #include <gio/gio.h>
32 #include <glib.h>
33 #include <string.h>
34 #include <unistd.h>
35
36 #include "daemon/dbus/gsignond-dbus.h"
37 #include "daemon/dbus/gsignond-dbus-auth-service-gen.h"
38 #include "daemon/dbus/gsignond-dbus-identity-gen.h"
39 #include "daemon/dbus/gsignond-dbus-auth-session-gen.h"
40 #include "common/gsignond-identity-info.h"
41 #include "gsignond/gsignond-log.h"
42
43 #ifdef USE_P2P
44 #  ifdef GSIGNOND_SERVICE
45 #    undef GSIGNOND_SERVICE
46 #  endif
47 #  define GSIGNOND_SERVICE NULL
48 #endif
49
50 struct IdentityData {
51     gchar *key;
52     gchar *type;
53     void *value;
54 } data[] = {
55     { "UserName", "s", "test_user" },
56     { "Caption", "s", "test_caption" },
57     { "Secret", "s", "test_pass" },
58     { "StoreSecret", "b", (void *)TRUE}
59 };
60
61 #if HAVE_GTESTDBUS
62 GTestDBus *dbus = NULL;
63 #else
64 GPid daemon_pid = 0;
65 #endif
66
67 static gchar* _get_executable_name()
68 {
69     gchar *procfname;
70     char *path;
71     ssize_t res;
72     pid_t pid = getpid();
73
74     //valgrind does some magic with tasks, so we read the executable name of
75     //the 'main' task, instead of the current task
76     procfname = g_strdup_printf ("/proc/%d/task/%d/exe", pid, pid);
77     path = g_malloc0 (PATH_MAX + 1);
78     res = readlink (procfname, path, PATH_MAX);
79     g_free (procfname);
80
81     if (res <= 0) {
82         WARN ("failed to follow link for pid %d", pid);
83         g_free (path);
84         return NULL;
85     }
86     return path;
87 }
88
89 static void
90 setup_daemon (void)
91 {
92     gchar* exe_name = _get_executable_name();
93     fail_if(exe_name == NULL);
94     
95     fail_if (g_setenv ("GSIGNOND_CONFIG", "daemontest.conf", TRUE) == FALSE);
96     fail_if (g_setenv ("SSO_STORAGE_PATH", "/tmp/gsignond", TRUE) == FALSE);
97     fail_if (g_setenv ("SSO_KEYCHAIN_SYSCTX", exe_name, TRUE) == FALSE);
98
99     DBG ("Programe pid %d, name : %s\n", getpid(), exe_name);
100     free(exe_name);
101
102     if (system("rm -rf /tmp/gsignond") != 0) {
103         DBG("Failed to clean db path : %s\n", strerror(errno));
104     }
105 #if HAVE_GTESTDBUS
106     dbus = g_test_dbus_new (G_TEST_DBUS_NONE);
107     fail_unless (dbus != NULL, "could not create test dbus");
108
109     g_test_dbus_add_service_dir (dbus, GSIGNOND_TEST_DBUS_SERVICE_DIR);
110
111     g_test_dbus_up (dbus);
112     DBG ("Test dbus server address : %s\n", g_test_dbus_get_bus_address(dbus));
113 #else
114     GError *error = NULL;
115 #   ifdef USE_P2P
116     /* start daemon maually */
117     gchar *argv[2];
118     gchar *test_daemon_path = g_build_filename (g_getenv("SSO_BIN_DIR"),
119             "gsignond", NULL);
120     fail_if (test_daemon_path == NULL, "No SSO daemon path found");
121
122     argv[0] = test_daemon_path;
123     argv[1] = NULL;
124     g_spawn_async (NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL,
125             &daemon_pid, &error);
126     g_free (test_daemon_path);
127     fail_if (error != NULL, "Failed to span daemon : %s",
128             error ? error->message : "");
129     sleep (5); /* 5 seconds */
130 #   else
131     /* session bus where no GTestBus support */
132     GIOChannel *channel = NULL;
133     gchar *bus_address = NULL;
134     gint tmp_fd = 0;
135     gint pipe_fd[2];
136     gchar *argv[] = {"dbus-daemon", "--config-file=<<conf-file>>", "--print-address=<<fd>>", NULL};
137     gsize len = 0;
138     const gchar *dbus_monitor = NULL;
139
140     argv[1] = g_strdup_printf ("--config-file=%s", "gsignond-dbus.conf");
141
142     if (pipe(pipe_fd)== -1) {
143         WARN("Failed to open temp file : %s", error->message);
144         argv[2] = g_strdup_printf ("--print-address=1");
145         g_spawn_async_with_pipes (NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL, &daemon_pid, NULL, NULL, &tmp_fd, &error);
146     } else {
147         tmp_fd = pipe_fd[0];
148         argv[2] = g_strdup_printf ("--print-address=%d", pipe_fd[1]);
149         g_spawn_async(NULL, argv, NULL, G_SPAWN_SEARCH_PATH|G_SPAWN_LEAVE_DESCRIPTORS_OPEN, NULL, NULL, &daemon_pid, &error);
150     }
151     fail_if (error != NULL, "Failed to span daemon : %s", error ? error->message : "");
152     fail_if (daemon_pid == 0, "Failed to get daemon pid");
153     g_free (argv[1]);
154     g_free (argv[2]);
155     sleep (5); /* 5 seconds */
156
157     channel = g_io_channel_unix_new (tmp_fd);
158     g_io_channel_read_line (channel, &bus_address, NULL, &len, &error);
159     fail_if (error != NULL, "Failed to daemon address : %s", error ? error->message : "");
160     g_io_channel_unref (channel);
161     
162     if (pipe_fd[0]) close (pipe_fd[0]);
163     if (pipe_fd[1]) close (pipe_fd[1]);
164
165     if (bus_address) bus_address[len] = '\0';
166     fail_if(bus_address == NULL || strlen(bus_address) == 0);
167
168     if (GSIGNOND_BUS_TYPE == G_BUS_TYPE_SYSTEM)
169         fail_if (g_setenv("DBUS_SYSTEM_BUS_ADDRESS", bus_address, TRUE) == FALSE);
170     else
171         fail_if (g_setenv("DBUS_SESSION_BUS_ADDRESS", bus_address, TRUE) == FALSE);
172
173     DBG ("Daemon Address : %s\n", bus_address);
174     g_free (bus_address);
175
176     if ((dbus_monitor = g_getenv("SSO_DBUS_DEBUG")) != NULL && g_strcmp0 (dbus_monitor, "0")) {
177         /* start dbus-monitor */
178         char *argv[] = {"dbus-monitor", "<<bus_type>>", NULL };
179         argv[1] = GSIGNOND_BUS_TYPE == G_BUS_TYPE_SYSTEM ? "--system" : "--session" ;
180         g_spawn_async (NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL, NULL, &error);
181         if (error) {
182                 DBG ("Error while running dbus-monitor : %s", error->message);
183                 g_error_free (error);
184         }
185     }
186 #   endif
187
188     DBG ("Daemon PID = %d\n", daemon_pid);
189 #endif
190 }
191
192 static void
193 teardown_daemon (void)
194 {
195 #if HAVE_GTESTDBUS
196     g_test_dbus_down (dbus);
197 #else
198     if (daemon_pid) kill (daemon_pid, SIGTERM);
199 #endif
200
201     g_unsetenv ("SSO_IDENTITY_TIMEOUT");
202     g_unsetenv ("SSO_DAEMON_TIMEOUT");
203     g_unsetenv ("SSO_AUTH_SESSION_TIMEOUT");
204     g_unsetenv ("SSO_STORAGE_PATH");
205     g_unsetenv ("SSO_KEYCHAIN_SYSCTX");
206 }
207
208 gboolean _validate_identity_info (GVariant *identity_info)
209 {
210     GSignondIdentityInfo *identity = 0;
211     const gchar *username = 0;
212     if (!identity_info) return FALSE;
213
214     identity = (GSignondIdentityInfo *)gsignond_identity_info_new_from_variant (identity_info);
215     if (!identity) return FALSE;
216
217     username = gsignond_identity_info_get_username (identity);
218
219     if (!username || strcmp (username, "test_user")) return FALSE;
220
221     gsignond_identity_info_unref (identity);
222
223     return TRUE;
224 }
225
226 GVariant * _get_test_identity_data()
227 {
228     GVariantBuilder builder, method_builder;
229     const gchar *mechanisms[] = {"mech1","mech2", NULL };
230     int i;
231
232     g_variant_builder_init (&builder, G_VARIANT_TYPE_VARDICT);
233
234     for (i=0; i < sizeof(data)/sizeof(struct IdentityData); i++) {
235         g_variant_builder_add (&builder, "{sv}", data[i].key, g_variant_new (data[i].type, data[i].value));
236     }
237
238     g_variant_builder_init (&method_builder, (const GVariantType *)"a{sas}");
239     g_variant_builder_add (&method_builder, "{s^as}", "ssotest", mechanisms);
240
241     g_variant_builder_add (&builder, "{sv}", "AuthMethods", g_variant_builder_end (&method_builder));
242
243     return g_variant_builder_end (&builder);
244 }
245
246 GVariant * _create_identity_info_with_data (const gchar *username,
247                                             const gchar *caption, 
248                                             gint type, 
249                                             const gchar *methods[],
250                                             const gchar **mechanisms[])
251 {
252     GVariantBuilder builder;
253
254     g_variant_builder_init (&builder, G_VARIANT_TYPE_VARDICT);
255
256     if(username) g_variant_builder_add (&builder, "{sv}", "UserName", g_variant_new_string (username));
257     if(caption) g_variant_builder_add (&builder, "{sv}", "Caption", g_variant_new_string (caption));
258     if (type != 0) g_variant_builder_add (&builder, "{sv}", "Type", g_variant_new_int32(type));
259     if (methods && mechanisms) {
260         GVariantBuilder method_builder;
261         int i;
262  
263         g_variant_builder_init (&method_builder, (const GVariantType *)"a{sas}");
264
265         for (i=0; methods[i]; i++) {
266             g_variant_builder_add (&method_builder, "{s^as}", methods[i], mechanisms[i]);
267         }
268
269         g_variant_builder_add (&builder, "{sv}", "AuthMethods", g_variant_builder_end (&method_builder));
270     }
271
272     return g_variant_builder_end (&builder);
273 }
274
275
276 GDBusConnection * _get_bus_connection (GError **error)
277 {
278 #if USE_P2P
279     gchar address[128];
280
281     g_snprintf (address, 127, GSIGNOND_DBUS_ADDRESS, g_get_user_runtime_dir());
282     return g_dbus_connection_new_for_address_sync (
283         address,
284         G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
285         NULL,
286         NULL,
287         error);
288 #else
289     return g_bus_get_sync (GSIGNOND_BUS_TYPE, NULL, error);
290 #endif
291 }
292
293 GSignondDbusAuthService * _get_auth_service (GDBusConnection *connection,
294                                              GError **error)
295 {
296     return gsignond_dbus_auth_service_proxy_new_sync (
297                 connection,
298                 G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES,
299                 GSIGNOND_SERVICE,
300                 GSIGNOND_DAEMON_OBJECTPATH,
301                 NULL, error);
302 }
303
304 GSignondDbusIdentity * _get_identity_for_path (GDBusConnection *connection,
305                                                const gchar *identity_path,
306                                                GError **error)
307 {
308     return gsignond_dbus_identity_proxy_new_sync (
309         connection,
310         G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES,
311         GSIGNOND_SERVICE,
312         identity_path,
313         NULL, error);
314 }
315
316 GSignondDbusAuthSession * _get_auth_session_for_path (GDBusConnection *connection,
317                                                       const gchar *session_path,
318                                                       GError **error)
319 {
320     return gsignond_dbus_auth_session_proxy_new_sync (
321         connection,
322         G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES,
323         GSIGNOND_SERVICE,
324         session_path,
325         NULL, error);
326 }
327
328 GSignondDbusIdentity * _register_identity (GSignondDbusAuthService *auth_service,
329                                            const gchar *app_context,
330                                            GError **error) 
331 {
332     GDBusConnection *connection = NULL;
333     GSignondDbusIdentity *identity = NULL;
334     gchar *identity_path = NULL;
335
336     gboolean res = gsignond_dbus_auth_service_call_register_new_identity_sync (
337         auth_service,
338         app_context,
339         &identity_path,
340         NULL,
341         error);
342
343     if (res == FALSE) {
344         DBG (" ERROR :: %s", error ? (*error)->message : "");
345         return NULL;
346     }
347
348     connection = g_dbus_proxy_get_connection (G_DBUS_PROXY (auth_service));
349     identity = _get_identity_for_path (connection, identity_path, error);
350
351     g_free (identity_path);
352
353     return identity;
354 }
355
356 GSignondDbusIdentity * _get_identity (GSignondDbusAuthService *auth_service,
357                                       guint32 id,
358                                       const gchar *app_context,
359                                       GVariant **identity_info,
360                                       GError **error)
361 {
362     gboolean res;
363     gchar *identity_path = NULL;
364     GDBusConnection *connection = NULL;
365     GSignondDbusIdentity *identity = NULL;
366     
367     res = gsignond_dbus_auth_service_call_get_identity_sync(
368         auth_service,
369         id,
370         app_context,        
371         &identity_path,
372         identity_info,
373         NULL,
374         error);
375
376     if (res == FALSE || !identity_path) {
377         DBG ("ERROR :: %s", error ? (*error)->message : "");
378         return NULL;
379     }
380
381     connection = g_dbus_proxy_get_connection (G_DBUS_PROXY (auth_service));
382     identity = _get_identity_for_path (connection, identity_path, error);
383
384     g_free (identity_path);
385
386     return identity;
387 }
388
389 /*
390  * Test cases
391  */
392 START_TEST (test_register_new_identity)
393 {
394     GError *error = 0;
395     GDBusConnection *connection = NULL;
396     GSignondDbusAuthService *auth_service = NULL;
397     GSignondDbusIdentity *identity = NULL;
398
399     connection = _get_bus_connection (&error);
400     fail_if (connection == NULL, "failed to get bus connection : %s", error ? error->message : "(null)");
401
402     auth_service = _get_auth_service (connection, &error);
403     fail_if (auth_service == NULL, "failed to get auth_service : %s", error ? error->message : "");
404
405     identity = _register_identity (auth_service, "test_app", &error);
406     fail_if (identity == NULL, "Failed to register identity : %s", error ? error->message : "");
407
408     g_object_unref (identity);
409     g_object_unref (auth_service);
410     g_object_unref (connection);
411 }
412 END_TEST
413
414 START_TEST (test_register_new_identity_with_no_app_context)
415 {
416     GError *error = NULL;
417     GSignondDbusAuthService *auth_service = NULL;
418     GSignondDbusIdentity *identity = NULL;
419     GDBusConnection *connection = _get_bus_connection (&error);
420     fail_if (connection == NULL, "failed to get bus connection : %s", error ? error->message : "(null)");
421     fail_if (error != NULL, "failed to get bus connection : %s", error ? error->message : "(null)");
422  
423     auth_service = _get_auth_service (connection, &error);
424     fail_if (auth_service == NULL, "failed to get auth_service : %s", error ? error->message : "");
425
426     identity = _register_identity (auth_service, "", &error);
427     fail_if (identity == NULL, "Failed to register identity : %s", error ? error->message : "");
428
429     g_object_unref (identity);
430     g_object_unref (auth_service);
431     g_object_unref (connection);
432 }
433 END_TEST
434
435 START_TEST (test_identity_store)
436 {
437     GError *error = NULL; gboolean res = FALSE;
438     GSignondDbusAuthService *auth_service = 0;
439     GSignondDbusIdentity *identity = 0;
440     guint id;
441     GVariant *identity_info = NULL;
442
443     GDBusConnection *connection = _get_bus_connection (&error);
444     fail_if (connection == NULL, "failed to get bus connection : %s", error ? error->message : "(null)");
445     fail_if (error != NULL, "failed to get bus connection : %s", error ? error->message : "(null)");
446
447     auth_service = _get_auth_service (connection, &error);
448     fail_if (auth_service == NULL, "failed to get auth_service : %s", error ? error->message : "");
449
450     identity_info = _get_test_identity_data ();
451     fail_if (identity_info == NULL, "Failed to get test identity data");
452
453     identity = _register_identity (auth_service, "test_app", &error);
454     fail_if (identity == NULL, "Failed to register identity : %s", error ? error->message : "");
455
456     res = gsignond_dbus_identity_call_store_sync (identity, identity_info, 
457                                     &id, NULL, &error); 
458     fail_if (res == FALSE, "Failed to store identity : %s", error ? error->message : "");
459     fail_if (id == 0);
460
461     DBG ("Identity id : %d\n", id);
462
463     g_object_unref (identity);
464     g_object_unref (auth_service);
465     g_object_unref (connection);
466 }
467 END_TEST
468
469 START_TEST(test_identity_get_identity)
470 {
471     GError *error = NULL;
472     GSignondDbusAuthService *auth_service = 0;
473     guint32 id = 1; /* identity id created in test_identity_store */
474     GVariant *identity_info = NULL;
475     GSignondDbusIdentity *identity = NULL;
476     GDBusConnection *connection = _get_bus_connection (&error);
477     fail_if (connection == NULL, "failed to get bus connection : %s", error ? error->message : "(null)");
478     fail_if (error != NULL, "failed to get bus connection : %s", error ? error->message : "(null)");
479
480     auth_service = _get_auth_service (connection, &error);
481     fail_if (auth_service == NULL, "failed to get auth_service : %s", error ? error->message : "");
482
483     identity = _get_identity (auth_service, id, "test_app", &identity_info, &error);
484     fail_if (identity == NULL, "Failed to get identity for id '%u' : %s", id, error ? error->message : "");
485     fail_if (identity_info == NULL);
486
487     fail_if (_validate_identity_info(identity_info) == FALSE);
488
489     g_object_unref (auth_service);
490     g_object_unref (identity);
491     g_object_unref (connection);
492 }
493 END_TEST
494
495 START_TEST(test_clear_database)
496 {
497     GError *error = 0;
498     gboolean res, ret;
499     GSignondDbusAuthService *auth_service = 0;
500     GDBusConnection *connection = _get_bus_connection (&error);
501     fail_if (connection == NULL, "failed to get bus connection : %s", error ? error->message : "(null)");
502     fail_if (error != NULL, "failed to get bus connection : %s", error ? error->message : "(null)");
503  
504     auth_service = _get_auth_service (connection, &error);
505     fail_if (auth_service == NULL, "failed to get auth_service : %s", error ? error->message : "");
506
507     res = gsignond_dbus_auth_service_call_clear_sync (
508         auth_service,
509         &ret,
510         NULL,
511         &error);
512     fail_if (res == FALSE || ret == FALSE, "Failed to wipe databases : %s", error ? error->message : "");
513
514     g_object_unref (auth_service);
515     g_object_unref (connection);
516 }
517 END_TEST
518
519 static void _on_session_unregistered (GSignondDbusAuthSession *sesssion, gpointer userdata)
520 {
521     gboolean *out = (gboolean*) userdata;
522     g_return_if_fail (out);
523
524     *out = TRUE;
525 }
526
527 static void _on_identity_updated (GSignondDbusIdentity *identity, gint change_type, gpointer userdata)
528 {
529     gboolean *out = (gboolean *)userdata;
530     g_return_if_fail (out);
531
532     if (change_type == 2 /* GSIGNOND_IDENTITY_SIGNED_OUT */) 
533         *out = TRUE;
534 }
535
536 static void _on_sign_out_reply (GSignondDbusIdentity *sender, GAsyncResult *reply, gpointer data)
537 {
538     GError *error = NULL;
539
540     gboolean res = FALSE, ret = FALSE;
541     
542     ret = gsignond_dbus_identity_call_sign_out_finish (sender, &res, reply, &error);
543
544     fail_if (ret == FALSE, "failed to finish signout, %s", error ? error->message : "");
545     fail_if (res == FALSE, "failed to call signout on identity : %s", error ? error->message : "");
546
547     g_main_loop_quit ((GMainLoop *)data);
548     g_main_loop_unref((GMainLoop *)data);
549 }
550
551 START_TEST(test_identity_signout)
552 {
553     GError *error = 0;
554     gboolean res;
555     GSignondDbusAuthService *auth_service = 0;
556     GSignondDbusIdentity *identity = 0;
557     GSignondDbusAuthSession *auth_session = 0;
558     GVariant *identity_info = NULL;
559     gchar *session_path = NULL;
560     guint id;
561     gboolean identity_signed_out = FALSE;
562     gboolean session_unregistered = FALSE;
563     GMainLoop *loop = NULL;
564     GDBusConnection *connection = _get_bus_connection (&error);
565     fail_if (connection == NULL, "failed to get bus connection : %s", error ? error->message : "(null)");
566     fail_if (error != NULL, "failed to get bus connection : %s", error ? error->message : "(null)");
567
568     loop = g_main_loop_new (NULL, FALSE);
569
570     auth_service = _get_auth_service (connection, &error);
571     fail_if (auth_service == NULL, "failed to get auth_service : %s", error ? error->message : "");
572
573     identity = _register_identity (auth_service, "", &error);
574     fail_if (identity == NULL, "Failed to register new identity : %s", error ? error->message : "");
575
576     identity_info = _get_test_identity_data ();
577     fail_if (identity_info == NULL);
578
579     res = gsignond_dbus_identity_call_store_sync (identity, identity_info,
580                                                   &id, NULL, &error);
581     fail_if (res == FALSE, "Failed to store identity");
582     fail_if (id == 0);
583
584     g_signal_connect (identity, "info-updated", G_CALLBACK(_on_identity_updated), &identity_signed_out);
585
586     res = gsignond_dbus_identity_call_get_auth_session_sync (
587             identity, "ssotest", &session_path, NULL, &error);
588
589     fail_if (res == FALSE, "Failed to create authentication session on identity for method 'ssotest', error : %s",
590         error ? error->message : "");
591     fail_if (session_path == NULL, "(null) session_path");
592
593     auth_session = _get_auth_session_for_path (connection, session_path, &error);
594
595     g_free (session_path);
596     fail_if (error != NULL, "failed to created session proxy for path '%s', error: %s", 
597         session_path, error ? error->message : "");
598     fail_if (auth_session == NULL, "(null) session object");
599
600     g_signal_connect (auth_session, "unregistered", G_CALLBACK (_on_session_unregistered), &session_unregistered);
601
602     /* Call SignOut on identity */
603     gsignond_dbus_identity_call_sign_out (identity, NULL, (GAsyncReadyCallback)_on_sign_out_reply, loop);
604
605     g_main_loop_run (loop);
606
607     fail_unless (session_unregistered == TRUE, "Session unregistred not reached");
608     fail_unless (identity_signed_out == TRUE, "Identity signed out signal not reached");
609
610     g_object_unref (auth_service);
611     g_object_unref (identity);
612     g_object_unref (auth_session);
613     g_object_unref (connection);
614 }
615 END_TEST
616
617 START_TEST(test_query_identities)
618 {
619     GDBusConnection *connection = NULL;
620     GSignondDbusAuthService *auth_service = NULL;
621     GSignondDbusIdentity *identity = NULL;
622     GVariant *v_info = NULL;
623     GSignondIdentityInfo *info1 = NULL, *info2 = NULL, *info3 = NULL, *tmp_info = NULL;
624     GSignondDictionary *filter = NULL;
625     GVariant *v_identities = NULL;
626     const gchar *methods[] = { "ssotest", NULL };
627     const gchar *mech[] = {"mech1", "mech2", NULL};
628     const gchar **mechanisms[] = { mech };
629     gboolean res;
630     guint32 id = 0;
631     GError *error = NULL;
632
633     connection = _get_bus_connection (&error);
634     fail_if (connection == NULL, "Failed to get bus connection : %s", error ? error->message : "");
635
636     auth_service = _get_auth_service (connection, &error);
637     fail_if (auth_service == NULL, "Failed to get auth_service : %s", error ? error->message : "");
638
639     /* created identity1 */
640     identity = _register_identity (auth_service, "app_context_A", &error);
641     fail_if (identity == NULL, "Failed to register new identity : %s", error ? error->message : "");
642
643     v_info = _create_identity_info_with_data ("user1", "caption1", 1, methods, mechanisms);
644     fail_if (v_info == NULL);
645     res = gsignond_dbus_identity_call_store_sync (identity, v_info, &id, NULL, &error);
646     fail_if (res == FALSE || id == 0, "Failed to store identity : %s", error ? error->message : "");
647     g_object_unref (identity);
648
649     identity = _get_identity (auth_service, id, "app_context_A", &v_info, &error);
650     fail_if (identity == NULL || v_info == NULL, "Failed to load identity for id '%d' : %s", id, error ? error->message : "");
651     g_object_unref (identity);
652     info1 = gsignond_identity_info_new_from_variant (v_info);
653
654     /* created identity2 */
655     identity = _register_identity (auth_service, "app_context_B", &error);
656     fail_if (identity == NULL, "Failed to register new identity : %s", error ? error->message : "");
657
658     v_info = _create_identity_info_with_data ("user2", "caption2", 2, methods, mechanisms);
659     fail_if (v_info == NULL);
660     res = gsignond_dbus_identity_call_store_sync (identity, v_info, &id, NULL, &error);
661     fail_if (res == FALSE || id == 0, "Failed to store identity : %s", error ? error->message : "");
662     g_object_unref (identity);
663
664     identity = _get_identity (auth_service, id, "app_context_B", &v_info, &error);
665     fail_if (identity == NULL || v_info == NULL, "Failed to load identity for id '%d' : %s", id, error ? error->message : "");
666     g_object_unref (identity);
667     info2 = gsignond_identity_info_new_from_variant (v_info);
668
669     /* create identity3 */
670     identity = _register_identity (auth_service, "app_context_A", &error);
671     fail_if (identity == NULL, "Failed to register new identity : %s", error ? error->message : "");
672
673     v_info = _create_identity_info_with_data ("user2", "caption3", 2, methods, mechanisms);
674     fail_if (v_info == NULL);
675     res = gsignond_dbus_identity_call_store_sync (identity, v_info, &id, NULL, &error);
676     fail_if (res == FALSE || id == 0, "Failed to store identity : %s", error ? error->message : "");
677     g_object_unref (identity);
678
679     identity = _get_identity (auth_service, id, "app_context_A", &v_info, &error);
680     fail_if (identity == NULL || v_info == NULL, "Failed to load identity for id '%d' : %s", id, error ? error->message : "");
681     g_object_unref (identity);
682     info3 = gsignond_identity_info_new_from_variant (v_info);
683
684     /* query identities for app-context: app_context_A */
685     v_identities = NULL;
686     filter = gsignond_dictionary_new();
687     res = gsignond_dbus_auth_service_call_query_identities_sync (auth_service,
688             gsignond_dictionary_to_variant (filter),
689             "app_context_A", &v_identities, NULL, &error);
690     gsignond_dictionary_unref (filter);
691     fail_if (res == FALSE || !v_identities, "Failed to query identities for "
692                            "app context 'app_context_A' : %s",
693                            error ? error->message : "");
694     fail_if (g_variant_n_children (v_identities) != 2, 
695         "Expected no of identities '%d', got '%d'", 2,
696         g_variant_n_children(v_identities));
697     /* validated query results */
698     tmp_info = gsignond_identity_info_new_from_variant (
699                             g_variant_get_child_value (v_identities, 0));
700     fail_if (gsignond_identity_info_compare (info1, tmp_info) == FALSE);
701     gsignond_identity_info_unref (tmp_info);
702
703     tmp_info = gsignond_identity_info_new_from_variant (
704                             g_variant_get_child_value (v_identities, 1));
705     fail_if (gsignond_identity_info_compare (info3, tmp_info) == FALSE);
706     gsignond_identity_info_unref (tmp_info);
707
708     /* query identities for app-context: app_context_B, Identity type : 2 */
709     v_identities = NULL;
710     filter = gsignond_dictionary_new();
711     gsignond_dictionary_set_int32 (filter, "Type", 2);
712     res = gsignond_dbus_auth_service_call_query_identities_sync (auth_service,
713             gsignond_dictionary_to_variant (filter),
714             "app_context_B", &v_identities, NULL, &error);
715     gsignond_dictionary_unref (filter);
716     fail_if (res == FALSE || !v_identities, "Failed to query identities for "
717                            "app context 'app_context_B' and Type: 2 : %s",
718                            error ? error->message : "");
719     /* validated query results */
720     fail_if (g_variant_n_children (v_identities) != 1, 
721         "Expected no of identities '%d', got '%d'", 1,
722         g_variant_n_children(v_identities));
723     tmp_info = gsignond_identity_info_new_from_variant (
724                             g_variant_get_child_value (v_identities, 0));
725     fail_if (gsignond_identity_info_compare (info2, tmp_info) == FALSE);
726     gsignond_identity_info_unref (tmp_info);
727
728     /* query identities for app-context: app_context_A, Caption: "cap*" */
729     v_identities = NULL;
730     filter = gsignond_dictionary_new();
731     gsignond_dictionary_set_string (filter, "Caption", "cap");
732     res = gsignond_dbus_auth_service_call_query_identities_sync (auth_service,
733             gsignond_dictionary_to_variant (filter),
734             "app_context_B", &v_identities, NULL, &error);
735     gsignond_dictionary_unref (filter);
736     fail_if (res == FALSE || !v_identities, "Failed to query identities for "
737                            "app context 'app_context_A' : %s",
738                            error ? error->message : "");
739     /* validated query results */
740     fail_if (g_variant_n_children (v_identities) != 1, 
741         "Expected no of identities '%d', got '%d'", 1,
742         g_variant_n_children(v_identities));
743     tmp_info = gsignond_identity_info_new_from_variant (
744                             g_variant_get_child_value (v_identities, 0));
745     fail_if (gsignond_identity_info_compare 
746             (info2, tmp_info) == FALSE);
747     gsignond_identity_info_unref (tmp_info);
748
749     gsignond_identity_info_unref (info1);
750     gsignond_identity_info_unref (info2);
751     gsignond_identity_info_unref (info3);
752
753     g_object_unref (auth_service);
754     g_object_unref (connection);
755 }
756 END_TEST
757
758 Suite* daemon_suite (void)
759 {
760     Suite *s = suite_create ("Gsignon daemon");
761     
762     TCase *tc = tcase_create ("Identity");
763
764     tcase_set_timeout(tc, 10);
765     tcase_add_unchecked_fixture (tc, setup_daemon, teardown_daemon);
766
767     tcase_add_test (tc, test_register_new_identity);
768     tcase_add_test (tc, test_register_new_identity_with_no_app_context);
769     tcase_add_test (tc, test_identity_store);
770     tcase_add_test (tc, test_identity_get_identity);
771     tcase_add_test (tc, test_clear_database);
772     tcase_add_test (tc, test_identity_signout);
773     tcase_add_test (tc, test_query_identities);
774
775     suite_add_tcase (s, tc);
776
777     return s;
778 }
779
780 int main (int argc, char *argv[])
781 {
782     int number_failed;
783     Suite *s = 0;
784     SRunner *sr = 0;
785    
786 #if !GLIB_CHECK_VERSION (2, 36, 0)
787     g_type_init ();
788 #endif
789
790     s = daemon_suite();
791     sr = srunner_create(s);
792
793     srunner_run_all(sr, CK_VERBOSE);
794
795     number_failed = srunner_ntests_failed(sr);
796     srunner_free(sr);
797
798     return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
799 }