collection: Have gobject-introspection and glib-mkenums recognize flags
[platform/upstream/libsecret.git] / libsecret / tests / test-methods.c
1 /* libsecret - GLib wrapper for Secret Service
2  *
3  * Copyright 2011 Collabora Ltd.
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public License as published
7  * by the Free Software Foundation; either version 2 of the licence or (at
8  * your option) any later version.
9  *
10  * See the included COPYING file for more information.
11  */
12
13
14 #include "config.h"
15
16 #include "secret-attributes.h"
17 #include "secret-collection.h"
18 #include "secret-item.h"
19 #include "secret-paths.h"
20 #include "secret-private.h"
21 #include "secret-service.h"
22
23 #include "mock-service.h"
24
25 #include "egg/egg-testing.h"
26
27 #include <glib.h>
28
29 #include <errno.h>
30 #include <stdlib.h>
31
32 static const SecretSchema MOCK_SCHEMA = {
33         "org.mock.Schema",
34         SECRET_SCHEMA_NONE,
35         {
36                 { "number", SECRET_SCHEMA_ATTRIBUTE_INTEGER },
37                 { "string", SECRET_SCHEMA_ATTRIBUTE_STRING },
38                 { "even", SECRET_SCHEMA_ATTRIBUTE_BOOLEAN },
39         }
40 };
41
42 static const SecretSchema PRIME_SCHEMA = {
43         "org.mock.Prime",
44         SECRET_SCHEMA_NONE,
45         {
46                 { "number", SECRET_SCHEMA_ATTRIBUTE_INTEGER },
47                 { "string", SECRET_SCHEMA_ATTRIBUTE_STRING },
48                 { "prime", SECRET_SCHEMA_ATTRIBUTE_BOOLEAN },
49         }
50 };
51
52 static const SecretSchema NO_NAME_SCHEMA = {
53         "unused.Schema.Name",
54         SECRET_SCHEMA_DONT_MATCH_NAME,
55         {
56                 { "number", SECRET_SCHEMA_ATTRIBUTE_INTEGER },
57                 { "string", SECRET_SCHEMA_ATTRIBUTE_STRING },
58         }
59 };
60
61 typedef struct {
62         SecretService *service;
63 } Test;
64
65 static void
66 setup_mock (Test *test,
67             gconstpointer data)
68 {
69         GError *error = NULL;
70         const gchar *mock_script = data;
71
72         mock_service_start (mock_script, &error);
73         g_assert_no_error (error);
74 }
75
76 static void
77 setup (Test *test,
78        gconstpointer data)
79 {
80         GError *error = NULL;
81
82         setup_mock (test, data);
83
84         test->service = secret_service_get_sync (SECRET_SERVICE_NONE, NULL, &error);
85         g_assert_no_error (error);
86 }
87
88 static void
89 teardown_mock (Test *test,
90                gconstpointer unused)
91 {
92         secret_service_disconnect ();
93         mock_service_stop ();
94 }
95
96 static void
97 teardown (Test *test,
98           gconstpointer unused)
99 {
100         egg_test_wait_idle ();
101
102         g_object_unref (test->service);
103         secret_service_disconnect ();
104         egg_assert_not_object (test->service);
105
106         teardown_mock (test, unused);
107 }
108
109 static void
110 on_complete_get_result (GObject *source,
111                         GAsyncResult *result,
112                         gpointer user_data)
113 {
114         GAsyncResult **ret = user_data;
115         g_assert (ret != NULL);
116         g_assert (*ret == NULL);
117         *ret = g_object_ref (result);
118         egg_test_wait_stop ();
119 }
120
121 static void
122 test_search_sync (Test *test,
123                   gconstpointer used)
124 {
125         GHashTable *attributes;
126         GError *error = NULL;
127         GList *items;
128
129         attributes = g_hash_table_new (g_str_hash, g_str_equal);
130         g_hash_table_insert (attributes, "number", "1");
131
132         items = secret_service_search_sync (test->service, &MOCK_SCHEMA, attributes,
133                                             SECRET_SEARCH_NONE, NULL, &error);
134         g_assert_no_error (error);
135         g_hash_table_unref (attributes);
136
137         g_assert (items != NULL);
138         g_assert_cmpstr (g_dbus_proxy_get_object_path (items->data), ==, "/org/freedesktop/secrets/collection/english/1");
139
140         g_assert (items->next == NULL);
141         g_list_free_full (items, g_object_unref);
142 }
143
144 static void
145 test_search_async (Test *test,
146                    gconstpointer used)
147 {
148         GAsyncResult *result = NULL;
149         GHashTable *attributes;
150         GError *error = NULL;
151         GList *items;
152
153         attributes = g_hash_table_new (g_str_hash, g_str_equal);
154         g_hash_table_insert (attributes, "number", "1");
155
156         secret_service_search (test->service, &MOCK_SCHEMA, attributes,
157                                SECRET_SEARCH_NONE, NULL,
158                                on_complete_get_result, &result);
159         g_hash_table_unref (attributes);
160         g_assert (result == NULL);
161
162         egg_test_wait ();
163
164         g_assert (G_IS_ASYNC_RESULT (result));
165         items = secret_service_search_finish (test->service, result, &error);
166         g_assert_no_error (error);
167         g_object_unref (result);
168
169         g_assert (items != NULL);
170         g_assert_cmpstr (g_dbus_proxy_get_object_path (items->data), ==, "/org/freedesktop/secrets/collection/english/1");
171
172         g_assert (items->next == NULL);
173         g_list_free_full (items, g_object_unref);
174 }
175
176 static void
177 test_search_all_sync (Test *test,
178                   gconstpointer used)
179 {
180         GHashTable *attributes;
181         GError *error = NULL;
182         GList *items;
183
184         attributes = g_hash_table_new (g_str_hash, g_str_equal);
185         g_hash_table_insert (attributes, "number", "1");
186
187         items = secret_service_search_sync (test->service, &MOCK_SCHEMA, attributes,
188                                             SECRET_SEARCH_ALL, NULL, &error);
189         g_assert_no_error (error);
190         g_hash_table_unref (attributes);
191
192         g_assert (items != NULL);
193         g_assert_cmpstr (g_dbus_proxy_get_object_path (items->data), ==, "/org/freedesktop/secrets/collection/english/1");
194         g_assert (secret_item_get_locked (items->data) == FALSE);
195         g_assert (secret_item_get_secret (items->data) == NULL);
196
197         g_assert (items->next != NULL);
198         g_assert_cmpstr (g_dbus_proxy_get_object_path (items->next->data), ==, "/org/freedesktop/secrets/collection/spanish/10");
199         g_assert (secret_item_get_locked (items->next->data) == TRUE);
200         g_assert (secret_item_get_secret (items->next->data) == NULL);
201
202         g_assert (items->next->next == NULL);
203         g_list_free_full (items, g_object_unref);
204 }
205
206 static void
207 test_search_all_async (Test *test,
208                    gconstpointer used)
209 {
210         GAsyncResult *result = NULL;
211         GHashTable *attributes;
212         GError *error = NULL;
213         GList *items;
214
215         attributes = g_hash_table_new (g_str_hash, g_str_equal);
216         g_hash_table_insert (attributes, "number", "1");
217
218         secret_service_search (test->service, &MOCK_SCHEMA, attributes,
219                                SECRET_SEARCH_ALL, NULL,
220                                on_complete_get_result, &result);
221         g_hash_table_unref (attributes);
222         g_assert (result == NULL);
223
224         egg_test_wait ();
225
226         g_assert (G_IS_ASYNC_RESULT (result));
227         items = secret_service_search_finish (test->service, result, &error);
228         g_assert_no_error (error);
229         g_object_unref (result);
230
231         g_assert (items != NULL);
232         g_assert_cmpstr (g_dbus_proxy_get_object_path (items->data), ==, "/org/freedesktop/secrets/collection/english/1");
233         g_assert (secret_item_get_locked (items->data) == FALSE);
234         g_assert (secret_item_get_secret (items->data) == NULL);
235
236         g_assert (items->next != NULL);
237         g_assert_cmpstr (g_dbus_proxy_get_object_path (items->next->data), ==, "/org/freedesktop/secrets/collection/spanish/10");
238         g_assert (secret_item_get_locked (items->next->data) == TRUE);
239         g_assert (secret_item_get_secret (items->next->data) == NULL);
240
241         g_assert (items->next->next == NULL);
242         g_list_free_full (items, g_object_unref);
243 }
244
245 static void
246 test_search_unlock_sync (Test *test,
247                          gconstpointer used)
248 {
249         GHashTable *attributes;
250         GError *error = NULL;
251         GList *items;
252
253         attributes = g_hash_table_new (g_str_hash, g_str_equal);
254         g_hash_table_insert (attributes, "number", "1");
255
256         items = secret_service_search_sync (test->service, &MOCK_SCHEMA, attributes,
257                                             SECRET_SEARCH_ALL | SECRET_SEARCH_UNLOCK,
258                                             NULL, &error);
259         g_assert_no_error (error);
260         g_hash_table_unref (attributes);
261
262         g_assert (items != NULL);
263         g_assert_cmpstr (g_dbus_proxy_get_object_path (items->data), ==, "/org/freedesktop/secrets/collection/english/1");
264         g_assert (secret_item_get_locked (items->data) == FALSE);
265         g_assert (secret_item_get_secret (items->data) == NULL);
266
267         g_assert (items->next != NULL);
268         g_assert_cmpstr (g_dbus_proxy_get_object_path (items->next->data), ==, "/org/freedesktop/secrets/collection/spanish/10");
269         g_assert (secret_item_get_locked (items->next->data) == FALSE);
270         g_assert (secret_item_get_secret (items->next->data) == NULL);
271
272         g_assert (items->next->next == NULL);
273         g_list_free_full (items, g_object_unref);
274 }
275
276 static void
277 test_search_unlock_async (Test *test,
278                           gconstpointer used)
279 {
280         GAsyncResult *result = NULL;
281         GHashTable *attributes;
282         GError *error = NULL;
283         GList *items;
284
285         attributes = g_hash_table_new (g_str_hash, g_str_equal);
286         g_hash_table_insert (attributes, "number", "1");
287
288         secret_service_search (test->service, &MOCK_SCHEMA, attributes,
289                                SECRET_SEARCH_ALL | SECRET_SEARCH_UNLOCK, NULL,
290                                on_complete_get_result, &result);
291         g_hash_table_unref (attributes);
292         g_assert (result == NULL);
293
294         egg_test_wait ();
295
296         g_assert (G_IS_ASYNC_RESULT (result));
297         items = secret_service_search_finish (test->service, result, &error);
298         g_assert_no_error (error);
299         g_object_unref (result);
300
301         g_assert (items != NULL);
302         g_assert_cmpstr (g_dbus_proxy_get_object_path (items->data), ==, "/org/freedesktop/secrets/collection/english/1");
303         g_assert (secret_item_get_locked (items->data) == FALSE);
304         g_assert (secret_item_get_secret (items->data) == NULL);
305
306         g_assert (items->next != NULL);
307         g_assert_cmpstr (g_dbus_proxy_get_object_path (items->next->data), ==, "/org/freedesktop/secrets/collection/spanish/10");
308         g_assert (secret_item_get_locked (items->next->data) == FALSE);
309         g_assert (secret_item_get_secret (items->next->data) == NULL);
310
311         g_assert (items->next->next == NULL);
312         g_list_free_full (items, g_object_unref);
313 }
314
315 static void
316 test_search_secrets_sync (Test *test,
317                           gconstpointer used)
318 {
319         GHashTable *attributes;
320         GError *error = NULL;
321         SecretValue *value;
322         GList *items;
323
324         attributes = g_hash_table_new (g_str_hash, g_str_equal);
325         g_hash_table_insert (attributes, "number", "1");
326
327         items = secret_service_search_sync (test->service, &MOCK_SCHEMA, attributes,
328                                             SECRET_SEARCH_ALL | SECRET_SEARCH_LOAD_SECRETS,
329                                             NULL, &error);
330         g_assert_no_error (error);
331         g_hash_table_unref (attributes);
332
333         g_assert (items != NULL);
334         g_assert_cmpstr (g_dbus_proxy_get_object_path (items->data), ==, "/org/freedesktop/secrets/collection/english/1");
335         g_assert (secret_item_get_locked (items->data) == FALSE);
336         value = secret_item_get_secret (items->data);
337         g_assert (value != NULL);
338         secret_value_unref (value);
339
340         g_assert (items->next != NULL);
341         g_assert_cmpstr (g_dbus_proxy_get_object_path (items->next->data), ==, "/org/freedesktop/secrets/collection/spanish/10");
342         g_assert (secret_item_get_locked (items->next->data) == TRUE);
343         g_assert (secret_item_get_secret (items->next->data) == NULL);
344
345         g_assert (items->next->next == NULL);
346         g_list_free_full (items, g_object_unref);
347 }
348
349 static void
350 test_search_secrets_async (Test *test,
351                            gconstpointer used)
352 {
353         GAsyncResult *result = NULL;
354         GHashTable *attributes;
355         GError *error = NULL;
356         SecretValue *value;
357         GList *items;
358
359         attributes = g_hash_table_new (g_str_hash, g_str_equal);
360         g_hash_table_insert (attributes, "number", "1");
361
362         secret_service_search (test->service, &MOCK_SCHEMA, attributes,
363                                SECRET_SEARCH_ALL | SECRET_SEARCH_LOAD_SECRETS, NULL,
364                                on_complete_get_result, &result);
365         g_hash_table_unref (attributes);
366         g_assert (result == NULL);
367
368         egg_test_wait ();
369
370         g_assert (G_IS_ASYNC_RESULT (result));
371         items = secret_service_search_finish (test->service, result, &error);
372         g_assert_no_error (error);
373         g_object_unref (result);
374
375         g_assert (items != NULL);
376         g_assert_cmpstr (g_dbus_proxy_get_object_path (items->data), ==, "/org/freedesktop/secrets/collection/english/1");
377         g_assert (secret_item_get_locked (items->data) == FALSE);
378         value = secret_item_get_secret (items->data);
379         g_assert (value != NULL);
380         secret_value_unref (value);
381
382         g_assert (items->next != NULL);
383         g_assert_cmpstr (g_dbus_proxy_get_object_path (items->next->data), ==, "/org/freedesktop/secrets/collection/spanish/10");
384         g_assert (secret_item_get_locked (items->next->data) == TRUE);
385         g_assert (secret_item_get_secret (items->next->data) == NULL);
386
387         g_assert (items->next->next == NULL);
388         g_list_free_full (items, g_object_unref);
389 }
390
391 static void
392 test_lock_sync (Test *test,
393                 gconstpointer used)
394 {
395         const gchar *collection_path = "/org/freedesktop/secrets/collection/lockone";
396         SecretCollection *collection;
397         GError *error = NULL;
398         GList *locked;
399         GList *objects;
400         gboolean ret;
401
402         collection = secret_collection_new_for_dbus_path_sync (test->service, collection_path,
403                                                                SECRET_COLLECTION_NONE, NULL, &error);
404         g_assert_no_error (error);
405
406         objects = g_list_append (NULL, collection);
407
408         ret = secret_service_lock_sync (test->service, objects, NULL, &locked, &error);
409         g_assert_no_error (error);
410         g_assert (ret == TRUE);
411
412         g_assert (locked != NULL);
413         g_assert (locked->data == collection);
414         g_assert (locked->next == NULL);
415         g_list_free_full (locked, g_object_unref);
416
417         g_list_free (objects);
418         g_object_unref (collection);
419 }
420
421 static void
422 test_unlock_sync (Test *test,
423                   gconstpointer used)
424 {
425         const gchar *collection_path = "/org/freedesktop/secrets/collection/lockone";
426         SecretCollection *collection;
427         GError *error = NULL;
428         GList *unlocked;
429         GList *objects;
430         gboolean ret;
431
432         collection = secret_collection_new_for_dbus_path_sync (test->service, collection_path,
433                                                                SECRET_COLLECTION_NONE, NULL, &error);
434         g_assert_no_error (error);
435
436         objects = g_list_append (NULL, collection);
437
438         ret = secret_service_unlock_sync (test->service, objects, NULL, &unlocked, &error);
439         g_assert_no_error (error);
440         g_assert (ret == TRUE);
441
442         g_assert (unlocked != NULL);
443         g_assert (unlocked->data == collection);
444         g_assert (unlocked->next == NULL);
445         g_list_free_full (unlocked, g_object_unref);
446
447         g_list_free (objects);
448         g_object_unref (collection);
449 }
450
451 static void
452 test_clear_sync (Test *test,
453                  gconstpointer used)
454 {
455         GError *error = NULL;
456         GHashTable *attributes;
457         gboolean ret;
458
459         attributes = secret_attributes_build (&MOCK_SCHEMA,
460                                               "even", FALSE,
461                                               "string", "one",
462                                               "number", 1,
463                                               NULL);
464
465         ret = secret_service_clear_sync (test->service, &MOCK_SCHEMA, attributes, NULL, &error);
466
467         g_assert_no_error (error);
468         g_assert (ret == TRUE);
469
470         g_hash_table_unref (attributes);
471 }
472
473 static void
474 test_clear_async (Test *test,
475                   gconstpointer used)
476 {
477         GError *error = NULL;
478         GAsyncResult *result = NULL;
479         GHashTable *attributes;
480         gboolean ret;
481
482         attributes = secret_attributes_build (&MOCK_SCHEMA,
483                                               "even", FALSE,
484                                               "string", "one",
485                                               "number", 1,
486                                               NULL);
487
488         secret_service_clear (test->service, &MOCK_SCHEMA, attributes, NULL,
489                               on_complete_get_result, &result);
490
491         g_hash_table_unref (attributes);
492         g_assert (result == NULL);
493
494         egg_test_wait ();
495
496         ret = secret_service_clear_finish (test->service, result, &error);
497         g_assert_no_error (error);
498         g_assert (ret == TRUE);
499
500         g_object_unref (result);
501 }
502
503 static void
504 test_clear_locked (Test *test,
505                    gconstpointer used)
506 {
507         GError *error = NULL;
508         GHashTable *attributes;
509         gboolean ret;
510
511         attributes = secret_attributes_build (&MOCK_SCHEMA,
512                                               "even", FALSE,
513                                               "string", "tres",
514                                               "number", 3,
515                                               NULL);
516
517         /* Locked items can't be removed via this API */
518         ret = secret_service_clear_sync (test->service, &MOCK_SCHEMA, attributes, NULL, &error);
519
520         g_hash_table_unref (attributes);
521         g_assert_no_error (error);
522         g_assert (ret == FALSE);
523 }
524
525 static void
526 test_clear_no_match (Test *test,
527                      gconstpointer used)
528 {
529         GError *error = NULL;
530         GHashTable *attributes;
531         gboolean ret;
532
533         attributes = secret_attributes_build (&MOCK_SCHEMA,
534                                               "even", TRUE,
535                                               "string", "one",
536                                               NULL);
537
538         /* Won't match anything */
539         ret = secret_service_clear_sync (test->service, &MOCK_SCHEMA, attributes, NULL, &error);
540
541         g_hash_table_unref (attributes);
542         g_assert_no_error (error);
543         g_assert (ret == FALSE);
544 }
545
546 static void
547 test_clear_no_name (Test *test,
548                     gconstpointer used)
549 {
550         const gchar *paths[] = { "/org/freedesktop/secrets/collection/german", NULL };
551         GError *error = NULL;
552         GHashTable *attributes;
553         gboolean ret;
554
555         attributes = secret_attributes_build (&MOCK_SCHEMA,
556                                               "number", 5,
557                                               NULL);
558
559         /* Shouldn't match anything, because no item with 5 in mock schema */
560         ret = secret_service_clear_sync (test->service, &MOCK_SCHEMA, attributes, NULL, &error);
561         g_assert_no_error (error);
562         g_assert (ret == FALSE);
563
564         /* We need this collection unlocked for the next test */
565         secret_service_unlock_dbus_paths_sync (test->service, paths, NULL, NULL, &error);
566         g_assert_no_error (error);
567
568         /* We have an item with 5 in prime schema, but should match anyway becase of flags */
569         ret = secret_service_clear_sync (test->service, &NO_NAME_SCHEMA, attributes, NULL, &error);
570         g_assert_no_error (error);
571         g_assert (ret == TRUE);
572
573         g_hash_table_unref (attributes);
574 }
575
576 static void
577 test_lookup_sync (Test *test,
578                   gconstpointer used)
579 {
580         GError *error = NULL;
581         GHashTable *attributes;
582         SecretValue *value;
583         gsize length;
584
585         attributes = secret_attributes_build (&MOCK_SCHEMA,
586                                               "even", FALSE,
587                                               "string", "one",
588                                               "number", 1,
589                                               NULL);
590
591         value = secret_service_lookup_sync (test->service, &MOCK_SCHEMA, attributes, NULL, &error);
592
593         g_assert_no_error (error);
594         g_hash_table_unref (attributes);
595
596         g_assert (value != NULL);
597         g_assert_cmpstr (secret_value_get (value, &length), ==, "111");
598         g_assert_cmpuint (length, ==, 3);
599
600         secret_value_unref (value);
601 }
602
603 static void
604 test_lookup_async (Test *test,
605                    gconstpointer used)
606 {
607         GError *error = NULL;
608         GHashTable *attributes;
609         GAsyncResult *result = NULL;
610         SecretValue *value;
611         gsize length;
612
613         attributes = secret_attributes_build (&MOCK_SCHEMA,
614                                               "even", FALSE,
615                                               "string", "one",
616                                               "number", 1,
617                                               NULL);
618
619         secret_service_lookup (test->service, &MOCK_SCHEMA, attributes, NULL,
620                                 on_complete_get_result, &result);
621
622         g_assert (result == NULL);
623         g_hash_table_unref (attributes);
624
625         egg_test_wait ();
626
627         value = secret_service_lookup_finish (test->service, result, &error);
628         g_assert_no_error (error);
629
630         g_assert (value != NULL);
631         g_assert_cmpstr (secret_value_get (value, &length), ==, "111");
632         g_assert_cmpuint (length, ==, 3);
633
634         secret_value_unref (value);
635         g_object_unref (result);
636 }
637
638 static void
639 test_lookup_locked (Test *test,
640                     gconstpointer used)
641 {
642         GError *error = NULL;
643         GHashTable *attributes;
644         SecretValue *value;
645         gsize length;
646
647         attributes = secret_attributes_build (&MOCK_SCHEMA,
648                                               "even", FALSE,
649                                               "string", "tres",
650                                               "number", 3,
651                                               NULL);
652
653         value = secret_service_lookup_sync (test->service, &MOCK_SCHEMA, attributes, NULL, &error);
654
655         g_assert_no_error (error);
656         g_hash_table_unref (attributes);
657
658         g_assert (value != NULL);
659         g_assert_cmpstr (secret_value_get (value, &length), ==, "3333");
660         g_assert_cmpuint (length, ==, 4);
661
662         secret_value_unref (value);
663 }
664
665 static void
666 test_lookup_no_match (Test *test,
667                       gconstpointer used)
668 {
669         GError *error = NULL;
670         GHashTable *attributes;
671         SecretValue *value;
672
673         attributes = secret_attributes_build (&MOCK_SCHEMA,
674                                               "even", TRUE,
675                                               "string", "one",
676                                               NULL);
677
678         /* Won't match anything */
679         value = secret_service_lookup_sync (test->service, &MOCK_SCHEMA, attributes, NULL, &error);
680
681         g_assert_no_error (error);
682         g_assert (value == NULL);
683         g_hash_table_unref (attributes);
684 }
685
686 static void
687 test_lookup_no_name (Test *test,
688                      gconstpointer used)
689 {
690         GError *error = NULL;
691         GHashTable *attributes;
692         SecretValue *value;
693         gsize length;
694
695         attributes = secret_attributes_build (&MOCK_SCHEMA,
696                                               "number", 5,
697                                               NULL);
698
699         /* should return null, because nothing with mock schema and 5 */
700         value = secret_service_lookup_sync (test->service, &MOCK_SCHEMA, attributes, NULL, &error);
701         g_assert_no_error (error);
702         g_assert (value == NULL);
703
704         /* should return an item, because we have a prime schema with 5, and flags not to match name */
705         value = secret_service_lookup_sync (test->service, &NO_NAME_SCHEMA, attributes, NULL, &error);
706
707         g_assert_no_error (error);
708
709         g_assert (value != NULL);
710         g_assert_cmpstr (secret_value_get (value, &length), ==, "555");
711         g_assert_cmpuint (length, ==, 3);
712
713         secret_value_unref (value);
714         g_hash_table_unref (attributes);
715 }
716
717 static void
718 test_store_sync (Test *test,
719                  gconstpointer used)
720 {
721         const gchar *collection_path = "/org/freedesktop/secrets/collection/english";
722         SecretValue *value = secret_value_new ("apassword", -1, "text/plain");
723         GHashTable *attributes;
724         GError *error = NULL;
725         gchar **paths;
726         gboolean ret;
727         gsize length;
728
729         attributes = secret_attributes_build (&MOCK_SCHEMA,
730                                               "even", FALSE,
731                                               "string", "seventeen",
732                                               "number", 17,
733                                               NULL);
734
735         ret = secret_service_store_sync (test->service, &MOCK_SCHEMA, attributes, collection_path,
736                                          "New Item Label", value, NULL, &error);
737         g_assert_no_error (error);
738         secret_value_unref (value);
739         g_hash_table_unref (attributes);
740
741         attributes = g_hash_table_new (g_str_hash, g_str_equal);
742         g_hash_table_insert (attributes, "even", "false");
743         g_hash_table_insert (attributes, "string", "seventeen");
744         g_hash_table_insert (attributes, "number", "17");
745
746         ret = secret_service_search_for_dbus_paths_sync (test->service, &MOCK_SCHEMA, attributes,
747                                                          NULL, &paths, NULL, &error);
748         g_hash_table_unref (attributes);
749         g_assert (ret == TRUE);
750
751         g_assert (paths != NULL);
752         g_assert (paths[0] != NULL);
753         g_assert (paths[1] == NULL);
754
755         value = secret_service_get_secret_for_dbus_path_sync (test->service, paths[0],
756                                                               NULL, &error);
757         g_assert_no_error (error);
758
759         g_assert (value != NULL);
760         g_assert_cmpstr (secret_value_get (value, &length), ==, "apassword");
761         g_assert_cmpuint (length, ==, 9);
762
763         secret_value_unref (value);
764         g_strfreev (paths);
765 }
766
767 static void
768 test_store_replace (Test *test,
769                     gconstpointer used)
770 {
771         const gchar *collection_path = "/org/freedesktop/secrets/collection/english";
772         SecretValue *value = secret_value_new ("apassword", -1, "text/plain");
773         GHashTable *attributes;
774         GError *error = NULL;
775         gchar **paths;
776         gboolean ret;
777
778         attributes = secret_attributes_build (&MOCK_SCHEMA,
779                                               "even", FALSE,
780                                               "string", "seventeen",
781                                               "number", 17,
782                                               NULL);
783
784         ret = secret_service_store_sync (test->service, &MOCK_SCHEMA, attributes, collection_path,
785                                           "New Item Label", value, NULL, &error);
786         g_assert_no_error (error);
787
788         ret = secret_service_store_sync (test->service, &MOCK_SCHEMA, attributes, collection_path,
789                                           "Another Label", value, NULL, &error);
790         g_assert_no_error (error);
791         secret_value_unref (value);
792         g_hash_table_unref (attributes);
793
794         attributes = g_hash_table_new (g_str_hash, g_str_equal);
795         g_hash_table_insert (attributes, "even", "false");
796         g_hash_table_insert (attributes, "string", "seventeen");
797         g_hash_table_insert (attributes, "number", "17");
798
799         ret = secret_service_search_for_dbus_paths_sync (test->service, &MOCK_SCHEMA, attributes,
800                                                          NULL, &paths, NULL, &error);
801         g_hash_table_unref (attributes);
802         g_assert (ret == TRUE);
803
804         g_assert (paths != NULL);
805         g_assert (paths[0] != NULL);
806         g_assert (paths[1] == NULL);
807
808         g_strfreev (paths);
809 }
810
811 static void
812 test_store_async (Test *test,
813                   gconstpointer used)
814 {
815         const gchar *collection_path = "/org/freedesktop/secrets/collection/english";
816         SecretValue *value = secret_value_new ("apassword", -1, "text/plain");
817         GAsyncResult *result = NULL;
818         GHashTable *attributes;
819         GError *error = NULL;
820         gchar **paths;
821         gboolean ret;
822         gsize length;
823
824         attributes = secret_attributes_build (&MOCK_SCHEMA,
825                                               "even", FALSE,
826                                               "string", "seventeen",
827                                               "number", 17,
828                                               NULL);
829
830         secret_service_store (test->service, &MOCK_SCHEMA, attributes, collection_path,
831                                "New Item Label", value, NULL, on_complete_get_result, &result);
832         g_assert (result == NULL);
833         secret_value_unref (value);
834         g_hash_table_unref (attributes);
835
836         egg_test_wait ();
837
838         ret = secret_service_store_finish (test->service, result, &error);
839         g_assert_no_error (error);
840         g_object_unref (result);
841
842         attributes = g_hash_table_new (g_str_hash, g_str_equal);
843         g_hash_table_insert (attributes, "even", "false");
844         g_hash_table_insert (attributes, "string", "seventeen");
845         g_hash_table_insert (attributes, "number", "17");
846
847         ret = secret_service_search_for_dbus_paths_sync (test->service, &MOCK_SCHEMA, attributes,
848                                                          NULL, &paths, NULL, &error);
849         g_hash_table_unref (attributes);
850         g_assert (ret == TRUE);
851
852         g_assert (paths != NULL);
853         g_assert (paths[0] != NULL);
854         g_assert (paths[1] == NULL);
855
856         value = secret_service_get_secret_for_dbus_path_sync (test->service, paths[0],
857                                                               NULL, &error);
858         g_assert_no_error (error);
859
860         g_assert (value != NULL);
861         g_assert_cmpstr (secret_value_get (value, &length), ==, "apassword");
862         g_assert_cmpuint (length, ==, 9);
863
864         secret_value_unref (value);
865         g_strfreev (paths);
866 }
867
868 static void
869 test_store_no_default (Test *test,
870                        gconstpointer used)
871 {
872         SecretValue *value = secret_value_new ("apassword", -1, "text/plain");
873         GHashTable *attributes;
874         GError *error = NULL;
875         gchar **paths;
876         gboolean ret;
877         gsize length;
878
879         attributes = secret_attributes_build (&MOCK_SCHEMA,
880                                               "even", FALSE,
881                                               "string", "seventeen",
882                                               "number", 17,
883                                               NULL);
884
885         ret = secret_service_store_sync (test->service, &MOCK_SCHEMA, attributes, SECRET_COLLECTION_DEFAULT,
886                                          "New Item Label", value, NULL, &error);
887         g_assert_no_error (error);
888         secret_value_unref (value);
889         g_hash_table_unref (attributes);
890
891         attributes = g_hash_table_new (g_str_hash, g_str_equal);
892         g_hash_table_insert (attributes, "even", "false");
893         g_hash_table_insert (attributes, "string", "seventeen");
894         g_hash_table_insert (attributes, "number", "17");
895
896         ret = secret_service_search_for_dbus_paths_sync (test->service, &MOCK_SCHEMA, attributes,
897                                                          NULL, &paths, NULL, &error);
898         g_hash_table_unref (attributes);
899         g_assert (ret == TRUE);
900
901         g_assert (paths != NULL);
902         g_assert (paths[0] != NULL);
903         g_assert (paths[1] == NULL);
904
905         value = secret_service_get_secret_for_dbus_path_sync (test->service, paths[0],
906                                                               NULL, &error);
907         g_assert_no_error (error);
908
909         g_assert (value != NULL);
910         g_assert_cmpstr (secret_value_get (value, &length), ==, "apassword");
911         g_assert_cmpuint (length, ==, 9);
912
913         secret_value_unref (value);
914         g_strfreev (paths);
915 }
916
917 static void
918 test_set_alias_sync (Test *test,
919                      gconstpointer used)
920 {
921         SecretCollection *collection;
922         gchar *blah;
923         GError *error = NULL;
924         gboolean ret;
925
926         blah = secret_service_read_alias_dbus_path_sync (test->service, "blah", NULL, &error);
927         g_assert_no_error (error);
928         g_assert (blah == NULL);
929
930         collection = secret_collection_new_for_dbus_path_sync (test->service,
931                                                                "/org/freedesktop/secrets/collection/english",
932                                                                SECRET_COLLECTION_NONE, NULL, &error);
933         g_assert_no_error (error);
934         g_assert (SECRET_IS_COLLECTION (collection));
935
936         ret = secret_service_set_alias_sync (test->service, "blah", collection, NULL, &error);
937         g_assert_no_error (error);
938         g_assert (ret == TRUE);
939
940         blah = secret_service_read_alias_dbus_path_sync (test->service, "blah", NULL, &error);
941         g_assert_no_error (error);
942         g_assert_cmpstr (blah, ==, g_dbus_proxy_get_object_path (G_DBUS_PROXY (collection)));
943         g_free (blah);
944
945         ret = secret_service_set_alias_sync (test->service, "blah", NULL, NULL, &error);
946         g_assert_no_error (error);
947         g_assert (ret == TRUE);
948
949         blah = secret_service_read_alias_dbus_path_sync (test->service, "blah", NULL, &error);
950         g_assert_no_error (error);
951         g_assert (blah == NULL);
952
953         g_object_unref (collection);
954 }
955
956 int
957 main (int argc, char **argv)
958 {
959         g_test_init (&argc, &argv, NULL);
960         g_set_prgname ("test-service");
961 #if !GLIB_CHECK_VERSION(2,35,0)
962         g_type_init ();
963 #endif
964
965         g_test_add ("/service/search-sync", Test, "mock-service-normal.py", setup, test_search_sync, teardown);
966         g_test_add ("/service/search-async", Test, "mock-service-normal.py", setup, test_search_async, teardown);
967         g_test_add ("/service/search-all-sync", Test, "mock-service-normal.py", setup, test_search_all_sync, teardown);
968         g_test_add ("/service/search-all-async", Test, "mock-service-normal.py", setup, test_search_all_async, teardown);
969         g_test_add ("/service/search-unlock-sync", Test, "mock-service-normal.py", setup, test_search_unlock_sync, teardown);
970         g_test_add ("/service/search-unlock-async", Test, "mock-service-normal.py", setup, test_search_unlock_async, teardown);
971         g_test_add ("/service/search-secrets-sync", Test, "mock-service-normal.py", setup, test_search_secrets_sync, teardown);
972         g_test_add ("/service/search-secrets-async", Test, "mock-service-normal.py", setup, test_search_secrets_async, teardown);
973
974         g_test_add ("/service/lock-sync", Test, "mock-service-lock.py", setup, test_lock_sync, teardown);
975
976         g_test_add ("/service/unlock-sync", Test, "mock-service-lock.py", setup, test_unlock_sync, teardown);
977
978         g_test_add ("/service/lookup-sync", Test, "mock-service-normal.py", setup, test_lookup_sync, teardown);
979         g_test_add ("/service/lookup-async", Test, "mock-service-normal.py", setup, test_lookup_async, teardown);
980         g_test_add ("/service/lookup-locked", Test, "mock-service-normal.py", setup, test_lookup_locked, teardown);
981         g_test_add ("/service/lookup-no-match", Test, "mock-service-normal.py", setup, test_lookup_no_match, teardown);
982         g_test_add ("/service/lookup-no-name", Test, "mock-service-normal.py", setup, test_lookup_no_name, teardown);
983
984         g_test_add ("/service/clear-sync", Test, "mock-service-delete.py", setup, test_clear_sync, teardown);
985         g_test_add ("/service/clear-async", Test, "mock-service-delete.py", setup, test_clear_async, teardown);
986         g_test_add ("/service/clear-locked", Test, "mock-service-delete.py", setup, test_clear_locked, teardown);
987         g_test_add ("/service/clear-no-match", Test, "mock-service-delete.py", setup, test_clear_no_match, teardown);
988         g_test_add ("/service/clear-no-name", Test, "mock-service-delete.py", setup, test_clear_no_name, teardown);
989
990         g_test_add ("/service/store-sync", Test, "mock-service-normal.py", setup, test_store_sync, teardown);
991         g_test_add ("/service/store-async", Test, "mock-service-normal.py", setup, test_store_async, teardown);
992         g_test_add ("/service/store-replace", Test, "mock-service-normal.py", setup, test_store_replace, teardown);
993         g_test_add ("/service/store-no-default", Test, "mock-service-empty.py", setup, test_store_no_default, teardown);
994
995         g_test_add ("/service/set-alias-sync", Test, "mock-service-normal.py", setup, test_set_alias_sync, teardown);
996
997         return egg_tests_run_with_loop ();
998 }