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