Use upstream tag
[platform/upstream/libsecret.git] / libsecret / test-collection.c
1 /* libsecret - GLib wrapper for Secret Service
2  *
3  * Copyright 2012 Red Hat Inc.
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  * Author: Stef Walter <stefw@gnome.org>
13  */
14
15
16 #include "config.h"
17
18 #include "secret-collection.h"
19 #include "secret-service.h"
20 #include "secret-paths.h"
21 #include "secret-private.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 typedef struct {
33         SecretService *service;
34 } Test;
35
36 static const SecretSchema MOCK_SCHEMA = {
37         "org.mock.Schema",
38         SECRET_SCHEMA_NONE,
39         {
40                 { "number", SECRET_SCHEMA_ATTRIBUTE_INTEGER },
41                 { "string", SECRET_SCHEMA_ATTRIBUTE_STRING },
42                 { "even", SECRET_SCHEMA_ATTRIBUTE_BOOLEAN },
43         }
44 };
45
46 static void
47 setup (Test *test,
48        gconstpointer data)
49 {
50         GError *error = NULL;
51         const gchar *mock_script = data;
52
53         mock_service_start (mock_script, &error);
54         g_assert_no_error (error);
55
56         test->service = secret_service_get_sync (SECRET_SERVICE_NONE, NULL, &error);
57         g_assert_no_error (error);
58         g_object_add_weak_pointer (G_OBJECT (test->service), (gpointer *)&test->service);
59 }
60
61 static void
62 teardown (Test *test,
63           gconstpointer unused)
64 {
65         g_object_unref (test->service);
66         secret_service_disconnect ();
67         g_assert (test->service == NULL);
68
69         mock_service_stop ();
70 }
71
72 static void
73 on_async_result (GObject *source,
74                  GAsyncResult *result,
75                  gpointer user_data)
76 {
77         GAsyncResult **ret = user_data;
78         g_assert (ret != NULL);
79         g_assert (*ret == NULL);
80         *ret = g_object_ref (result);
81         egg_test_wait_stop ();
82 }
83
84 static void
85 on_notify_stop (GObject *obj,
86                 GParamSpec *spec,
87                 gpointer user_data)
88 {
89         guint *sigs = user_data;
90         g_assert (sigs != NULL);
91         g_assert (*sigs > 0);
92         if (--(*sigs) == 0)
93                 egg_test_wait_stop ();
94 }
95
96 static void
97 test_new_sync (Test *test,
98                gconstpointer unused)
99 {
100         const gchar *collection_path = "/org/freedesktop/secrets/collection/english";
101         GError *error = NULL;
102         SecretCollection *collection;
103
104         collection = secret_collection_new_for_dbus_path_sync (test->service, collection_path,
105                                                                SECRET_COLLECTION_NONE, NULL, &error);
106         g_assert_no_error (error);
107         g_object_add_weak_pointer (G_OBJECT (collection), (gpointer *)&collection);
108
109         g_assert_cmpstr (g_dbus_proxy_get_object_path (G_DBUS_PROXY (collection)), ==, collection_path);
110
111         g_object_unref (collection);
112         g_assert (collection == NULL);
113 }
114
115 static void
116 test_new_async (Test *test,
117                gconstpointer unused)
118 {
119         const gchar *collection_path = "/org/freedesktop/secrets/collection/english";
120         GError *error = NULL;
121         SecretCollection *collection;
122         GAsyncResult *result = NULL;
123
124         secret_collection_new_for_dbus_path (test->service, collection_path,
125                                              SECRET_COLLECTION_NONE, NULL, on_async_result, &result);
126         g_assert (result == NULL);
127
128         egg_test_wait ();
129
130         collection = secret_collection_new_for_dbus_path_finish (result, &error);
131         g_assert_no_error (error);
132         g_object_unref (result);
133         g_object_add_weak_pointer (G_OBJECT (collection), (gpointer *)&collection);
134
135         g_assert_cmpstr (g_dbus_proxy_get_object_path (G_DBUS_PROXY (collection)), ==, collection_path);
136
137         g_object_unref (collection);
138         g_assert (collection == NULL);
139 }
140
141 static void
142 test_new_sync_noexist (Test *test,
143                        gconstpointer unused)
144 {
145         const gchar *collection_path = "/org/freedesktop/secrets/collection/nonexistant";
146         GError *error = NULL;
147         SecretCollection *collection;
148
149         collection = secret_collection_new_for_dbus_path_sync (test->service, collection_path,
150                                                                SECRET_COLLECTION_NONE, NULL, &error);
151         g_assert_error (error, G_DBUS_ERROR, G_DBUS_ERROR_UNKNOWN_METHOD);
152         g_assert (collection == NULL);
153 }
154
155 static void
156 test_new_async_noexist (Test *test,
157                         gconstpointer unused)
158 {
159         const gchar *collection_path = "/org/freedesktop/secrets/collection/nonexistant";
160         GError *error = NULL;
161         SecretCollection *collection;
162         GAsyncResult *result = NULL;
163
164         secret_collection_new_for_dbus_path (test->service, collection_path,
165                                              SECRET_COLLECTION_NONE, NULL, on_async_result, &result);
166         g_assert (result == NULL);
167
168         egg_test_wait ();
169
170         collection = secret_collection_new_for_dbus_path_finish (result, &error);
171         g_assert_error (error, G_DBUS_ERROR, G_DBUS_ERROR_UNKNOWN_METHOD);
172         g_assert (collection == NULL);
173         g_object_unref (result);
174 }
175
176
177 static void
178 test_for_alias_sync (Test *test,
179                      gconstpointer used)
180 {
181         const gchar *collection_path;
182         SecretCollection *collection;
183         GError *error = NULL;
184
185         collection = secret_collection_for_alias_sync (test->service, "default",
186                                                        SECRET_COLLECTION_NONE, NULL, &error);
187         g_assert_no_error (error);
188
189         collection_path = g_dbus_proxy_get_object_path (G_DBUS_PROXY (collection));
190         g_assert_cmpstr (collection_path, ==, "/org/freedesktop/secrets/collection/english");
191         g_assert_cmpuint (secret_collection_get_flags (collection), ==, SECRET_COLLECTION_NONE);
192         g_assert (secret_collection_get_items (collection) == NULL);
193         g_object_unref (collection);
194
195         collection = secret_collection_for_alias_sync (test->service, "unknown",
196                                                        SECRET_COLLECTION_NONE, NULL, &error);
197         g_assert_no_error (error);
198         g_assert (collection == NULL);
199 }
200
201 static void
202 test_for_alias_async (Test *test,
203                       gconstpointer used)
204 {
205         const gchar *collection_path;
206         SecretCollection *collection;
207         GAsyncResult *result = NULL;
208         GError *error = NULL;
209
210         secret_collection_for_alias (test->service, "default",
211                                      SECRET_COLLECTION_NONE,
212                                      NULL, on_async_result, &result);
213         g_assert (result == NULL);
214         egg_test_wait ();
215
216         collection = secret_collection_for_alias_finish (result, &error);
217         g_assert_no_error (error);
218         g_object_unref (result);
219
220         collection_path = g_dbus_proxy_get_object_path (G_DBUS_PROXY (collection));
221         g_assert_cmpstr (collection_path, ==, "/org/freedesktop/secrets/collection/english");
222         g_assert_cmpuint (secret_collection_get_flags (collection), ==, SECRET_COLLECTION_NONE);
223         g_assert (secret_collection_get_items (collection) == NULL);
224         g_object_unref (collection);
225         result = NULL;
226
227         secret_collection_for_alias (test->service, "unknown",
228                                      SECRET_COLLECTION_NONE,
229                                      NULL, on_async_result, &result);
230         g_assert (result == NULL);
231         egg_test_wait ();
232
233         collection = secret_collection_for_alias_finish (result, &error);
234         g_assert_no_error (error);
235         g_assert (collection == NULL);
236         g_object_unref (result);
237 }
238
239 static void
240 test_for_alias_load_sync (Test *test,
241                           gconstpointer used)
242 {
243         const gchar *collection_path;
244         SecretCollection *collection;
245         GError *error = NULL;
246         GList *items;
247
248         collection = secret_collection_for_alias_sync (test->service, "default",
249                                                        SECRET_COLLECTION_LOAD_ITEMS,
250                                                        NULL, &error);
251         g_assert_no_error (error);
252
253         collection_path = g_dbus_proxy_get_object_path (G_DBUS_PROXY (collection));
254         g_assert_cmpstr (collection_path, ==, "/org/freedesktop/secrets/collection/english");
255         g_assert_cmpuint (secret_collection_get_flags (collection), ==, SECRET_COLLECTION_LOAD_ITEMS);
256         items = secret_collection_get_items (collection);
257         g_assert (items != NULL);
258         g_list_free_full (items, g_object_unref);
259         g_object_unref (collection);
260 }
261
262 static void
263 test_for_alias_load_async (Test *test,
264                            gconstpointer used)
265 {
266         const gchar *collection_path;
267         SecretCollection *collection;
268         GAsyncResult *result = NULL;
269         GError *error = NULL;
270         GList *items;
271
272         secret_collection_for_alias (test->service, "default",
273                                      SECRET_COLLECTION_LOAD_ITEMS,
274                                      NULL, on_async_result, &result);
275         g_assert (result == NULL);
276         egg_test_wait ();
277
278         collection = secret_collection_for_alias_finish (result, &error);
279         g_assert_no_error (error);
280         g_object_unref (result);
281
282         collection_path = g_dbus_proxy_get_object_path (G_DBUS_PROXY (collection));
283         g_assert_cmpstr (collection_path, ==, "/org/freedesktop/secrets/collection/english");
284         g_assert_cmpuint (secret_collection_get_flags (collection), ==, SECRET_COLLECTION_LOAD_ITEMS);
285         items = secret_collection_get_items (collection);
286         g_assert (items != NULL);
287         g_list_free_full (items, g_object_unref);
288         g_object_unref (collection);
289         result = NULL;
290 }
291
292 static void
293 test_create_sync (Test *test,
294                   gconstpointer unused)
295 {
296         GError *error = NULL;
297         SecretCollection *collection;
298
299         collection = secret_collection_create_sync (test->service, "Train", NULL,
300                                                     SECRET_COLLECTION_CREATE_NONE, NULL, &error);
301         g_assert_no_error (error);
302         g_object_add_weak_pointer (G_OBJECT (collection), (gpointer *)&collection);
303
304         g_assert (g_str_has_prefix (g_dbus_proxy_get_object_path (G_DBUS_PROXY (collection)), "/org/freedesktop/secrets/collection"));
305         g_assert_cmpstr (secret_collection_get_label (collection), ==, "Train");
306         g_assert (secret_collection_get_locked (collection) == FALSE);
307
308         g_object_unref (collection);
309         g_assert (collection == NULL);
310 }
311
312 static void
313 test_create_async (Test *test,
314                    gconstpointer unused)
315 {
316         GError *error = NULL;
317         SecretCollection *collection;
318         GAsyncResult *result = NULL;
319
320         secret_collection_create (test->service, "Train", NULL,
321                                   SECRET_COLLECTION_CREATE_NONE,
322                                   NULL, on_async_result, &result);
323         g_assert (result == NULL);
324
325         egg_test_wait ();
326
327         collection = secret_collection_create_finish (result, &error);
328         g_assert_no_error (error);
329         g_object_unref (result);
330         g_object_add_weak_pointer (G_OBJECT (collection), (gpointer *)&collection);
331
332         g_assert (g_str_has_prefix (g_dbus_proxy_get_object_path (G_DBUS_PROXY (collection)), "/org/freedesktop/secrets/collection"));
333         g_assert_cmpstr (secret_collection_get_label (collection), ==, "Train");
334         g_assert (secret_collection_get_locked (collection) == FALSE);
335
336         g_object_unref (collection);
337         g_assert (collection == NULL);
338 }
339
340 static void
341 test_properties (Test *test,
342                  gconstpointer unused)
343 {
344         const gchar *collection_path = "/org/freedesktop/secrets/collection/english";
345         SecretCollection *collection;
346         SecretService *service;
347         GError *error = NULL;
348         guint64 created;
349         guint64 modified;
350         gboolean locked;
351         gchar *label;
352
353         collection = secret_collection_new_for_dbus_path_sync (test->service, collection_path,
354                                                                SECRET_COLLECTION_NONE, NULL, &error);
355         g_assert_no_error (error);
356
357         g_assert (secret_collection_get_locked (collection) == FALSE);
358         g_assert_cmpuint (secret_collection_get_created (collection), <=, time (NULL));
359         g_assert_cmpuint (secret_collection_get_modified (collection), <=, time (NULL));
360
361         label = secret_collection_get_label (collection);
362         g_assert_cmpstr (label, ==, "Collection One");
363         g_free (label);
364
365         g_object_get (collection,
366                       "locked", &locked,
367                       "created", &created,
368                       "modified", &modified,
369                       "label", &label,
370                       "service", &service,
371                       NULL);
372
373         g_assert (locked == FALSE);
374         g_assert_cmpuint (created, <=, time (NULL));
375         g_assert_cmpuint (modified, <=, time (NULL));
376
377         g_assert_cmpstr (label, ==, "Collection One");
378         g_free (label);
379
380         g_assert (service == test->service);
381         g_object_unref (service);
382
383         g_object_unref (collection);
384 }
385
386 static void
387 check_items_equal (GList *items,
388                    ...)
389 {
390         GHashTable *paths;
391         gboolean have_item;
392         const gchar *path;
393         guint num_items;
394         va_list va;
395         GList *l;
396
397         va_start (va, items);
398         paths = g_hash_table_new (g_str_hash, g_str_equal);
399         while ((path = va_arg (va, gchar *)) != NULL)
400                 g_hash_table_insert (paths, (gpointer)path, (gpointer)path);
401         va_end (va);
402
403         num_items = g_hash_table_size (paths);
404         g_assert_cmpuint (num_items, ==, g_list_length (items));
405
406         for (l = items; l != NULL; l = g_list_next (l)) {
407                 path = g_dbus_proxy_get_object_path (l->data);
408                 have_item = g_hash_table_remove (paths, path);
409                 g_assert (have_item);
410         }
411
412         g_hash_table_destroy (paths);
413 }
414
415 static void
416 test_items (Test *test,
417             gconstpointer unused)
418 {
419         const gchar *collection_path = "/org/freedesktop/secrets/collection/english";
420         SecretCollection *collection;
421         GError *error = NULL;
422         GList *items;
423
424         collection = secret_collection_new_for_dbus_path_sync (test->service, collection_path,
425                                                                SECRET_COLLECTION_LOAD_ITEMS, NULL, &error);
426         g_assert_no_error (error);
427
428         items = secret_collection_get_items (collection);
429         check_items_equal (items,
430                            "/org/freedesktop/secrets/collection/english/1",
431                            "/org/freedesktop/secrets/collection/english/2",
432                            "/org/freedesktop/secrets/collection/english/3",
433                            NULL);
434         g_list_free_full (items, g_object_unref);
435
436         g_object_get (collection, "items", &items, NULL);
437         check_items_equal (items,
438                            "/org/freedesktop/secrets/collection/english/1",
439                            "/org/freedesktop/secrets/collection/english/2",
440                            "/org/freedesktop/secrets/collection/english/3",
441                            NULL);
442         g_list_free_full (items, g_object_unref);
443
444         g_object_unref (collection);
445 }
446
447 static void
448 test_items_empty (Test *test,
449                   gconstpointer unused)
450 {
451         const gchar *collection_path = "/org/freedesktop/secrets/collection/empty";
452         SecretCollection *collection;
453         GError *error = NULL;
454         GList *items;
455
456         collection = secret_collection_new_for_dbus_path_sync (test->service, collection_path,
457                                                                SECRET_COLLECTION_LOAD_ITEMS, NULL, &error);
458         g_assert_no_error (error);
459
460         items = secret_collection_get_items (collection);
461         check_items_equal (items, NULL);
462         g_list_free_full (items, g_object_unref);
463
464         g_object_get (collection, "items", &items, NULL);
465         check_items_equal (items, NULL);
466         g_list_free_full (items, g_object_unref);
467
468         g_object_unref (collection);
469 }
470
471 static void
472 test_items_empty_async (Test *test,
473                         gconstpointer unused)
474 {
475         const gchar *collection_path = "/org/freedesktop/secrets/collection/empty";
476         SecretCollection *collection;
477         GAsyncResult *result = NULL;
478         GError *error = NULL;
479         GList *items;
480
481         secret_collection_new_for_dbus_path (test->service, collection_path,
482                                              SECRET_COLLECTION_LOAD_ITEMS,
483                                              NULL, on_async_result, &result);
484         g_assert (result == NULL);
485
486         egg_test_wait ();
487
488         collection = secret_collection_new_for_dbus_path_finish (result, &error);
489         g_assert_no_error (error);
490         g_object_unref (result);
491
492         items = secret_collection_get_items (collection);
493         check_items_equal (items, NULL);
494         g_list_free_full (items, g_object_unref);
495
496         g_object_get (collection, "items", &items, NULL);
497         check_items_equal (items, NULL);
498         g_list_free_full (items, g_object_unref);
499
500         g_object_unref (collection);
501 }
502
503 static void
504 test_set_label_sync (Test *test,
505                      gconstpointer unused)
506 {
507         const gchar *collection_path = "/org/freedesktop/secrets/collection/english";
508         GError *error = NULL;
509         SecretCollection *collection;
510         gboolean ret;
511         gchar *label;
512
513         collection = secret_collection_new_for_dbus_path_sync (test->service, collection_path,
514                                                                SECRET_COLLECTION_NONE, NULL, &error);
515         g_assert_no_error (error);
516
517         label = secret_collection_get_label (collection);
518         g_assert_cmpstr (label, ==, "Collection One");
519         g_free (label);
520
521         ret = secret_collection_set_label_sync (collection, "Another label", NULL, &error);
522         g_assert_no_error (error);
523         g_assert (ret == TRUE);
524
525         label = secret_collection_get_label (collection);
526         g_assert_cmpstr (label, ==, "Another label");
527         g_free (label);
528
529         g_object_unref (collection);
530 }
531
532 static void
533 test_set_label_async (Test *test,
534                       gconstpointer unused)
535 {
536         const gchar *collection_path = "/org/freedesktop/secrets/collection/english";
537         GAsyncResult *result = NULL;
538         GError *error = NULL;
539         SecretCollection *collection;
540         gboolean ret;
541         gchar *label;
542
543         collection = secret_collection_new_for_dbus_path_sync (test->service, collection_path,
544                                                                SECRET_COLLECTION_NONE, NULL, &error);
545         g_assert_no_error (error);
546
547         label = secret_collection_get_label (collection);
548         g_assert_cmpstr (label, ==, "Collection One");
549         g_free (label);
550
551         secret_collection_set_label (collection, "Another label", NULL, on_async_result, &result);
552         g_assert (result == NULL);
553
554         egg_test_wait ();
555
556         ret = secret_collection_set_label_finish (collection, result, &error);
557         g_assert_no_error (error);
558         g_assert (ret == TRUE);
559         g_object_unref (result);
560
561         label = secret_collection_get_label (collection);
562         g_assert_cmpstr (label, ==, "Another label");
563         g_free (label);
564
565         g_object_unref (collection);
566 }
567
568 static void
569 test_set_label_prop (Test *test,
570                      gconstpointer unused)
571 {
572         const gchar *collection_path = "/org/freedesktop/secrets/collection/english";
573         GAsyncResult *result = NULL;
574         GError *error = NULL;
575         SecretCollection *collection;
576         guint sigs = 2;
577         gchar *label;
578
579         secret_collection_new_for_dbus_path (test->service, collection_path, SECRET_COLLECTION_NONE,
580                                              NULL, on_async_result, &result);
581         g_assert (result == NULL);
582         egg_test_wait ();
583         collection = secret_collection_new_for_dbus_path_finish (result, &error);
584         g_assert_no_error (error);
585         g_object_unref (result);
586
587         label = secret_collection_get_label (collection);
588         g_assert_cmpstr (label, ==, "Collection One");
589         g_free (label);
590
591         g_signal_connect (collection, "notify::label", G_CALLBACK (on_notify_stop), &sigs);
592         g_object_set (collection, "label", "Blah blah", NULL);
593
594         /* Wait for the property to actually 'take' */
595         egg_test_wait ();
596
597         label = secret_collection_get_label (collection);
598         g_assert_cmpstr (label, ==, "Blah blah");
599         g_free (label);
600
601         g_object_unref (collection);
602 }
603
604 static void
605 test_delete_sync (Test *test,
606                   gconstpointer unused)
607 {
608         const gchar *collection_path = "/org/freedesktop/secrets/collection/english";
609         SecretCollection *collection;
610         GError *error = NULL;
611         gboolean ret;
612
613         collection = secret_collection_new_for_dbus_path_sync (test->service, collection_path,
614                                                                SECRET_COLLECTION_NONE, NULL, &error);
615         g_assert_no_error (error);
616
617         ret = secret_collection_delete_sync (collection, NULL, &error);
618         g_assert_no_error (error);
619         g_assert (ret == TRUE);
620
621         g_object_unref (collection);
622
623         collection = secret_collection_new_for_dbus_path_sync (test->service, collection_path,
624                                                                SECRET_COLLECTION_NONE, NULL, &error);
625         g_assert_error (error, G_DBUS_ERROR, G_DBUS_ERROR_UNKNOWN_METHOD);
626         g_assert (collection == NULL);
627 }
628
629 static void
630 test_delete_async (Test *test,
631                    gconstpointer unused)
632 {
633         const gchar *collection_path = "/org/freedesktop/secrets/collection/english";
634         SecretCollection *collection;
635         GAsyncResult *result = NULL;
636         GError *error = NULL;
637         gboolean ret;
638
639         collection = secret_collection_new_for_dbus_path_sync (test->service, collection_path,
640                                                                SECRET_COLLECTION_NONE, NULL, &error);
641         g_assert_no_error (error);
642
643         secret_collection_delete (collection, NULL, on_async_result, &result);
644         g_assert (result == NULL);
645
646         egg_test_wait ();
647
648         ret = secret_collection_delete_finish (collection, result, &error);
649         g_assert_no_error (error);
650         g_object_unref (result);
651         g_assert (ret == TRUE);
652
653         g_object_unref (collection);
654
655         collection = secret_collection_new_for_dbus_path_sync (test->service, collection_path,
656                                                                SECRET_COLLECTION_NONE, NULL, &error);
657         g_assert_error (error, G_DBUS_ERROR, G_DBUS_ERROR_UNKNOWN_METHOD);
658         g_assert (collection == NULL);
659 }
660
661 static void
662 test_search_sync (Test *test,
663                   gconstpointer used)
664 {
665         const gchar *collection_path = "/org/freedesktop/secrets/collection/english";
666         SecretCollection *collection;
667         GHashTable *attributes;
668         GError *error = NULL;
669         GList *items;
670
671         collection = secret_collection_new_for_dbus_path_sync (test->service, collection_path,
672                                                                SECRET_COLLECTION_NONE, NULL, &error);
673         g_assert_no_error (error);
674
675         attributes = g_hash_table_new (g_str_hash, g_str_equal);
676         g_hash_table_insert (attributes, "number", "1");
677
678         items = secret_collection_search_sync (collection, &MOCK_SCHEMA, attributes,
679                                                SECRET_SEARCH_NONE, NULL, &error);
680         g_assert_no_error (error);
681         g_hash_table_unref (attributes);
682
683         g_assert (items != NULL);
684         g_assert_cmpstr (g_dbus_proxy_get_object_path (items->data), ==, "/org/freedesktop/secrets/collection/english/1");
685
686         g_assert (items->next == NULL);
687         g_list_free_full (items, g_object_unref);
688
689         g_object_unref (collection);
690 }
691
692 static void
693 test_search_async (Test *test,
694                    gconstpointer used)
695 {
696         const gchar *collection_path = "/org/freedesktop/secrets/collection/english";
697         SecretCollection *collection;
698         GAsyncResult *result = NULL;
699         GHashTable *attributes;
700         GError *error = NULL;
701         GList *items;
702
703         collection = secret_collection_new_for_dbus_path_sync (test->service, collection_path,
704                                                                SECRET_COLLECTION_NONE, NULL, &error);
705         g_assert_no_error (error);
706
707         attributes = g_hash_table_new (g_str_hash, g_str_equal);
708         g_hash_table_insert (attributes, "number", "1");
709
710         secret_collection_search (collection, &MOCK_SCHEMA, attributes,
711                                   SECRET_SEARCH_NONE, NULL,
712                                   on_async_result, &result);
713         g_hash_table_unref (attributes);
714         g_assert (result == NULL);
715
716         egg_test_wait ();
717
718         g_assert (G_IS_ASYNC_RESULT (result));
719         items = secret_collection_search_finish (collection, result, &error);
720         g_assert_no_error (error);
721         g_object_unref (result);
722
723         g_assert (items != NULL);
724         g_assert_cmpstr (g_dbus_proxy_get_object_path (items->data), ==, "/org/freedesktop/secrets/collection/english/1");
725
726         g_assert (items->next == NULL);
727         g_list_free_full (items, g_object_unref);
728
729         g_object_unref (collection);
730 }
731
732 static gint
733 sort_by_object_path (gconstpointer a,
734                      gconstpointer b)
735 {
736         const gchar *pa = g_dbus_proxy_get_object_path ((GDBusProxy *)a);
737         const gchar *pb = g_dbus_proxy_get_object_path ((GDBusProxy *)b);
738
739         return g_strcmp0 (pa, pb);
740 }
741
742 static void
743 test_search_all_sync (Test *test,
744                   gconstpointer used)
745 {
746         const gchar *collection_path = "/org/freedesktop/secrets/collection/english";
747         SecretCollection *collection;
748         GHashTable *attributes;
749         GError *error = NULL;
750         GList *items;
751
752         collection = secret_collection_new_for_dbus_path_sync (test->service, collection_path,
753                                                                SECRET_COLLECTION_NONE, NULL, &error);
754         g_assert_no_error (error);
755
756         attributes = g_hash_table_new (g_str_hash, g_str_equal);
757
758         items = secret_collection_search_sync (collection, &MOCK_SCHEMA, attributes,
759                                                SECRET_SEARCH_ALL, NULL, &error);
760         g_assert_no_error (error);
761         g_hash_table_unref (attributes);
762
763         items = g_list_sort (items, sort_by_object_path);
764
765         g_assert (items != NULL);
766         g_assert_cmpstr (g_dbus_proxy_get_object_path (items->data), ==, "/org/freedesktop/secrets/collection/english/1");
767         g_assert (secret_item_get_secret (items->data) == NULL);
768
769         g_assert (items->next != NULL);
770         g_assert_cmpstr (g_dbus_proxy_get_object_path (items->next->data), ==, "/org/freedesktop/secrets/collection/english/2");
771         g_assert (secret_item_get_secret (items->next->data) == NULL);
772
773         g_assert (items->next->next != NULL);
774         g_assert_cmpstr (g_dbus_proxy_get_object_path (items->next->next->data), ==, "/org/freedesktop/secrets/collection/english/3");
775         g_assert (secret_item_get_secret (items->next->next->data) == NULL);
776
777         g_assert (items->next->next->next == NULL);
778         g_list_free_full (items, g_object_unref);
779
780         g_object_unref (collection);
781 }
782
783 static void
784 test_search_all_async (Test *test,
785                    gconstpointer used)
786 {
787         const gchar *collection_path = "/org/freedesktop/secrets/collection/english";
788         SecretCollection *collection;
789         GAsyncResult *result = NULL;
790         GHashTable *attributes;
791         GError *error = NULL;
792         GList *items;
793
794         collection = secret_collection_new_for_dbus_path_sync (test->service, collection_path,
795                                                                SECRET_COLLECTION_NONE, NULL, &error);
796         g_assert_no_error (error);
797
798         attributes = g_hash_table_new (g_str_hash, g_str_equal);
799
800         secret_collection_search (collection, &MOCK_SCHEMA, attributes,
801                                   SECRET_SEARCH_ALL, NULL,
802                                   on_async_result, &result);
803         g_hash_table_unref (attributes);
804         g_assert (result == NULL);
805
806         egg_test_wait ();
807
808         g_assert (G_IS_ASYNC_RESULT (result));
809         items = secret_collection_search_finish (collection, result, &error);
810         g_assert_no_error (error);
811         g_object_unref (result);
812
813         items = g_list_sort (items, sort_by_object_path);
814
815         g_assert (items != NULL);
816         g_assert_cmpstr (g_dbus_proxy_get_object_path (items->data), ==, "/org/freedesktop/secrets/collection/english/1");
817         g_assert (secret_item_get_secret (items->data) == NULL);
818
819         g_assert (items->next != NULL);
820         g_assert_cmpstr (g_dbus_proxy_get_object_path (items->next->data), ==, "/org/freedesktop/secrets/collection/english/2");
821         g_assert (secret_item_get_secret (items->next->data) == NULL);
822
823         g_assert (items->next->next != NULL);
824         g_assert_cmpstr (g_dbus_proxy_get_object_path (items->next->next->data), ==, "/org/freedesktop/secrets/collection/english/3");
825         g_assert (secret_item_get_secret (items->next->next->data) == NULL);
826
827         g_assert (items->next->next->next == NULL);
828         g_list_free_full (items, g_object_unref);
829
830         g_object_unref (collection);
831 }
832
833 static void
834 test_search_unlock_sync (Test *test,
835                          gconstpointer used)
836 {
837         const gchar *collection_path = "/org/freedesktop/secrets/collection/spanish";
838         SecretCollection *collection;
839         GHashTable *attributes;
840         GError *error = NULL;
841         GList *items;
842
843         collection = secret_collection_new_for_dbus_path_sync (test->service, collection_path,
844                                                                SECRET_COLLECTION_NONE, NULL, &error);
845         g_assert_no_error (error);
846
847         attributes = g_hash_table_new (g_str_hash, g_str_equal);
848         g_hash_table_insert (attributes, "number", "1");
849
850         items = secret_collection_search_sync (collection, &MOCK_SCHEMA, attributes,
851                                                SECRET_SEARCH_UNLOCK, NULL, &error);
852         g_assert_no_error (error);
853         g_hash_table_unref (attributes);
854
855         g_assert (items != NULL);
856         g_assert_cmpstr (g_dbus_proxy_get_object_path (items->data), ==, "/org/freedesktop/secrets/collection/spanish/10");
857         g_assert (secret_item_get_locked (items->data) == FALSE);
858         g_assert (secret_item_get_secret (items->data) == NULL);
859
860         g_assert (items->next == NULL);
861         g_list_free_full (items, g_object_unref);
862
863         g_object_unref (collection);
864 }
865
866 static void
867 test_search_unlock_async (Test *test,
868                           gconstpointer used)
869 {
870         const gchar *collection_path = "/org/freedesktop/secrets/collection/spanish";
871         SecretCollection *collection;
872         GAsyncResult *result = NULL;
873         GHashTable *attributes;
874         GError *error = NULL;
875         GList *items;
876
877         collection = secret_collection_new_for_dbus_path_sync (test->service, collection_path,
878                                                                SECRET_COLLECTION_NONE, NULL, &error);
879         g_assert_no_error (error);
880
881         attributes = g_hash_table_new (g_str_hash, g_str_equal);
882         g_hash_table_insert (attributes, "number", "1");
883
884         secret_collection_search (collection, &MOCK_SCHEMA, attributes,
885                                   SECRET_SEARCH_UNLOCK, NULL,
886                                   on_async_result, &result);
887         g_hash_table_unref (attributes);
888         g_assert (result == NULL);
889
890         egg_test_wait ();
891
892         g_assert (G_IS_ASYNC_RESULT (result));
893         items = secret_collection_search_finish (collection, result, &error);
894         g_assert_no_error (error);
895         g_object_unref (result);
896
897         g_assert (items != NULL);
898         g_assert_cmpstr (g_dbus_proxy_get_object_path (items->data), ==, "/org/freedesktop/secrets/collection/spanish/10");
899         g_assert (secret_item_get_locked (items->data) == FALSE);
900         g_assert (secret_item_get_secret (items->data) == NULL);
901
902         g_assert (items->next == NULL);
903         g_list_free_full (items, g_object_unref);
904
905         g_object_unref (collection);
906 }
907
908 static void
909 test_search_secrets_sync (Test *test,
910                           gconstpointer used)
911 {
912         const gchar *collection_path = "/org/freedesktop/secrets/collection/english";
913         SecretCollection *collection;
914         GHashTable *attributes;
915         GError *error = NULL;
916         SecretValue *value;
917         GList *items;
918
919         collection = secret_collection_new_for_dbus_path_sync (test->service, collection_path,
920                                                                SECRET_COLLECTION_NONE, NULL, &error);
921         g_assert_no_error (error);
922
923         attributes = g_hash_table_new (g_str_hash, g_str_equal);
924         g_hash_table_insert (attributes, "number", "1");
925
926         items = secret_collection_search_sync (collection, &MOCK_SCHEMA, attributes,
927                                                SECRET_SEARCH_LOAD_SECRETS,
928                                                NULL, &error);
929         g_assert_no_error (error);
930         g_hash_table_unref (attributes);
931
932         g_assert (items != NULL);
933         g_assert_cmpstr (g_dbus_proxy_get_object_path (items->data), ==, "/org/freedesktop/secrets/collection/english/1");
934         g_assert (secret_item_get_locked (items->data) == FALSE);
935         value = secret_item_get_secret (items->data);
936         g_assert (value != NULL);
937         secret_value_unref (value);
938
939         g_assert (items->next == NULL);
940         g_list_free_full (items, g_object_unref);
941
942         g_object_unref (collection);
943 }
944
945 static void
946 test_search_secrets_async (Test *test,
947                            gconstpointer used)
948 {
949         const gchar *collection_path = "/org/freedesktop/secrets/collection/english";
950         SecretCollection *collection;
951         GAsyncResult *result = NULL;
952         GHashTable *attributes;
953         GError *error = NULL;
954         SecretValue *value;
955         GList *items;
956
957         collection = secret_collection_new_for_dbus_path_sync (test->service, collection_path,
958                                                                SECRET_COLLECTION_NONE, NULL, &error);
959         g_assert_no_error (error);
960
961         attributes = g_hash_table_new (g_str_hash, g_str_equal);
962         g_hash_table_insert (attributes, "number", "1");
963
964         secret_collection_search (collection, &MOCK_SCHEMA, attributes,
965                                   SECRET_SEARCH_LOAD_SECRETS, NULL,
966                                   on_async_result, &result);
967         g_hash_table_unref (attributes);
968         g_assert (result == NULL);
969
970         egg_test_wait ();
971
972         g_assert (G_IS_ASYNC_RESULT (result));
973         items = secret_collection_search_finish (collection, result, &error);
974         g_assert_no_error (error);
975         g_object_unref (result);
976
977         g_assert (items != NULL);
978         g_assert_cmpstr (g_dbus_proxy_get_object_path (items->data), ==, "/org/freedesktop/secrets/collection/english/1");
979         g_assert (secret_item_get_locked (items->data) == FALSE);
980         value = secret_item_get_secret (items->data);
981         g_assert (value != NULL);
982         secret_value_unref (value);
983
984         g_assert (items->next == NULL);
985         g_list_free_full (items, g_object_unref);
986
987         g_object_unref (collection);
988 }
989
990 int
991 main (int argc, char **argv)
992 {
993         g_test_init (&argc, &argv, NULL);
994         g_set_prgname ("test-collection");
995 #if !GLIB_CHECK_VERSION(2,35,0)
996         g_type_init ();
997 #endif
998
999         g_test_add ("/collection/new-sync", Test, "mock-service-normal.py", setup, test_new_sync, teardown);
1000         g_test_add ("/collection/new-async", Test, "mock-service-normal.py", setup, test_new_async, teardown);
1001         g_test_add ("/collection/new-sync-noexist", Test, "mock-service-normal.py", setup, test_new_sync_noexist, teardown);
1002         g_test_add ("/collection/new-async-noexist", Test, "mock-service-normal.py", setup, test_new_async_noexist, teardown);
1003         g_test_add ("/collection/for-alias-sync", Test, "mock-service-normal.py", setup, test_for_alias_sync, teardown);
1004         g_test_add ("/collection/for-alias-async", Test, "mock-service-normal.py", setup, test_for_alias_async, teardown);
1005         g_test_add ("/collection/for-alias-load-sync", Test, "mock-service-normal.py", setup, test_for_alias_load_sync, teardown);
1006         g_test_add ("/collection/for-alias-load-async", Test, "mock-service-normal.py", setup, test_for_alias_load_async, teardown);
1007         g_test_add ("/collection/create-sync", Test, "mock-service-normal.py", setup, test_create_sync, teardown);
1008         g_test_add ("/collection/create-async", Test, "mock-service-normal.py", setup, test_create_async, teardown);
1009         g_test_add ("/collection/properties", Test, "mock-service-normal.py", setup, test_properties, teardown);
1010         g_test_add ("/collection/items", Test, "mock-service-normal.py", setup, test_items, teardown);
1011         g_test_add ("/collection/items-empty", Test, "mock-service-normal.py", setup, test_items_empty, teardown);
1012         g_test_add ("/collection/items-empty-async", Test, "mock-service-normal.py", setup, test_items_empty_async, teardown);
1013         g_test_add ("/collection/set-label-sync", Test, "mock-service-normal.py", setup, test_set_label_sync, teardown);
1014         g_test_add ("/collection/set-label-async", Test, "mock-service-normal.py", setup, test_set_label_async, teardown);
1015         g_test_add ("/collection/set-label-prop", Test, "mock-service-normal.py", setup, test_set_label_prop, teardown);
1016         g_test_add ("/collection/delete-sync", Test, "mock-service-normal.py", setup, test_delete_sync, teardown);
1017         g_test_add ("/collection/delete-async", Test, "mock-service-normal.py", setup, test_delete_async, teardown);
1018
1019         g_test_add ("/collection/search-sync", Test, "mock-service-normal.py", setup, test_search_sync, teardown);
1020         g_test_add ("/collection/search-async", Test, "mock-service-normal.py", setup, test_search_async, teardown);
1021         g_test_add ("/collection/search-all-sync", Test, "mock-service-normal.py", setup, test_search_all_sync, teardown);
1022         g_test_add ("/collection/search-all-async", Test, "mock-service-normal.py", setup, test_search_all_async, teardown);
1023         g_test_add ("/collection/search-unlock-sync", Test, "mock-service-normal.py", setup, test_search_unlock_sync, teardown);
1024         g_test_add ("/collection/search-unlock-async", Test, "mock-service-normal.py", setup, test_search_unlock_async, teardown);
1025         g_test_add ("/collection/search-secrets-sync", Test, "mock-service-normal.py", setup, test_search_secrets_sync, teardown);
1026         g_test_add ("/collection/search-secrets-async", Test, "mock-service-normal.py", setup, test_search_secrets_async, teardown);
1027
1028         return egg_tests_run_with_loop ();
1029 }