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